blob: 174aa474dfb6e88a4bb921d11a1b1f83434262b2 [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"
Chris Lattner4c1e9542009-03-06 06:45:05 +000022#include "llvm/Support/raw_ostream.h"
Devang Patel9dbe4d12008-07-01 17:44:24 +000023#include "llvm/Analysis/Dominators.h"
Gordon Henriksen878114b2008-03-16 04:20:44 +000024#include "llvm-c/Core.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000025#include <algorithm>
Duncan Sands26ff6f92008-10-08 07:23:46 +000026#include <cstdio>
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
Chris Lattner4c1e9542009-03-06 06:45:05 +000063void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
64 if (V == 0 && M == 0)
65 OS << "Releasing pass '";
66 else
67 OS << "Running pass '";
68
69 OS << P->getPassName() << "'";
70
71 if (M) {
72 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
73 return;
74 }
75 if (V == 0) {
76 OS << '\n';
77 return;
78 }
79
80 std::string Name = V->getNameStr();
81 if (Name.empty())
82 Name = "<anonymous>";
83 else if (isa<GlobalValue>(V))
84 Name = "@" + Name;
85 else
86 Name = "%" + Name;
87
88 if (isa<Function>(V))
89 OS << " on function '" << Name << "'\n";
90 else if (isa<BasicBlock>(V))
91 OS << " on basic block '" << Name << "'\n";
92 else
93 OS << " on value '" << Name << "'\n";
94}
95
96
Devang Patelffca9102006-12-15 19:39:30 +000097namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000098
Devang Patelf33f3eb2006-12-07 19:21:29 +000099//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000100// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000101//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000102/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000103/// pass together and sequence them to process one basic block before
104/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000105class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
106 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000107
108public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000109 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000110 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +0000111 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000112
Devang Patelca58e352006-11-08 10:05:38 +0000113 /// Execute all of the passes scheduled for execution. Keep track of
114 /// whether any of the passes modifies the function, and if so, return true.
115 bool runOnFunction(Function &F);
116
Devang Patelf9d96b92006-12-07 19:57:52 +0000117 /// Pass Manager itself does not invalidate any analysis info.
118 void getAnalysisUsage(AnalysisUsage &Info) const {
119 Info.setPreservesAll();
120 }
121
Devang Patel475c4532006-12-08 00:59:05 +0000122 bool doInitialization(Module &M);
123 bool doInitialization(Function &F);
124 bool doFinalization(Module &M);
125 bool doFinalization(Function &F);
126
Devang Patele3858e62007-02-01 22:08:25 +0000127 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000128 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000129 }
130
Devang Pateleda56172006-12-12 23:34:33 +0000131 // Print passes managed by this manager
132 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +0000133 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000134 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
135 BasicBlockPass *BP = getContainedPass(Index);
136 BP->dumpPassStructure(Offset + 1);
137 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000138 }
139 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000140
141 BasicBlockPass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000142 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000143 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
144 return BP;
145 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000146
Devang Patel28349ab2007-02-27 15:00:39 +0000147 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000148 return PMT_BasicBlockPassManager;
149 }
Devang Patelca58e352006-11-08 10:05:38 +0000150};
151
Devang Patel8c78a0b2007-05-03 01:11:54 +0000152char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000153}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000154
Devang Patele7599552007-01-12 18:52:44 +0000155namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000156
Devang Patel10c2ca62006-12-12 22:47:13 +0000157//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000158// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000159//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000160/// FunctionPassManagerImpl manages FPPassManagers
161class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000162 public PMDataManager,
163 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000164public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000165 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000166 explicit FunctionPassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000167 Pass(&ID), PMDataManager(Depth),
Devang Patel09f162c2007-05-01 21:15:47 +0000168 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000169
170 /// add - Add a pass to the queue of passes to run. This passes ownership of
171 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
172 /// will be destroyed as well, so there is no need to delete the pass. This
173 /// implies that all passes MUST be allocated with 'new'.
174 void add(Pass *P) {
175 schedulePass(P);
176 }
177
178 /// run - Execute all of the passes scheduled for execution. Keep track of
179 /// whether any of the passes modifies the module, and if so, return true.
180 bool run(Function &F);
181
182 /// doInitialization - Run all of the initializers for the function passes.
183 ///
184 bool doInitialization(Module &M);
185
Dan Gohmane6656eb2007-07-30 14:51:13 +0000186 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000187 ///
188 bool doFinalization(Module &M);
189
190 /// Pass Manager itself does not invalidate any analysis info.
191 void getAnalysisUsage(AnalysisUsage &Info) const {
192 Info.setPreservesAll();
193 }
194
195 inline void addTopLevelPass(Pass *P) {
196
197 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
198
199 // P is a immutable pass and it will be managed by this
200 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000201 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000202 P->setResolver(AR);
203 initializeAnalysisImpl(P);
204 addImmutablePass(IP);
205 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000206 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000207 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000208 }
Devang Patel0f080042007-01-12 17:23:48 +0000209
Devang Patel67d6a5e2006-12-19 19:46:59 +0000210 }
211
212 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000213 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000214 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
215 return FP;
216 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000217};
218
Devang Patel8c78a0b2007-05-03 01:11:54 +0000219char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000220//===----------------------------------------------------------------------===//
221// MPPassManager
222//
223/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000224/// It batches all Module passes and function pass managers together and
225/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000226class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000227public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000228 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000229 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000230 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000231
232 // Delete on the fly managers.
233 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000234 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000235 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
236 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000237 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000238 delete FPP;
239 }
240 }
241
Devang Patelca58e352006-11-08 10:05:38 +0000242 /// run - Execute all of the passes scheduled for execution. Keep track of
243 /// whether any of the passes modifies the module, and if so, return true.
244 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000245
Devang Patelf9d96b92006-12-07 19:57:52 +0000246 /// Pass Manager itself does not invalidate any analysis info.
247 void getAnalysisUsage(AnalysisUsage &Info) const {
248 Info.setPreservesAll();
249 }
250
Devang Patele64d3052007-04-16 20:12:57 +0000251 /// Add RequiredPass into list of lower level passes required by pass P.
252 /// RequiredPass is run on the fly by Pass Manager when P requests it
253 /// through getAnalysis interface.
254 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
255
Devang Patel69e9f6d2007-04-16 20:27:05 +0000256 /// Return function pass corresponding to PassInfo PI, that is
257 /// required by module pass MP. Instantiate analysis pass, by using
258 /// its runOnFunction() for function F.
259 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
260
Devang Patele3858e62007-02-01 22:08:25 +0000261 virtual const char *getPassName() const {
262 return "Module Pass Manager";
263 }
264
Devang Pateleda56172006-12-12 23:34:33 +0000265 // Print passes managed by this manager
266 void dumpPassStructure(unsigned Offset) {
267 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000268 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
269 ModulePass *MP = getContainedPass(Index);
270 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000271 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000272 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000273 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000274 }
275 }
276
Devang Patelabfbe3b2006-12-16 00:56:26 +0000277 ModulePass *getContainedPass(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000278 assert(N < PassVector.size() && "Pass number out of range!");
279 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000280 }
281
Devang Patel28349ab2007-02-27 15:00:39 +0000282 virtual PassManagerType getPassManagerType() const {
283 return PMT_ModulePassManager;
284 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000285
286 private:
287 /// Collection of on the fly FPPassManagers. These managers manage
288 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000289 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000290};
291
Devang Patel8c78a0b2007-05-03 01:11:54 +0000292char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000293//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000294// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000295//
Devang Patel09f162c2007-05-01 21:15:47 +0000296
Devang Patel67d6a5e2006-12-19 19:46:59 +0000297/// PassManagerImpl manages MPPassManagers
298class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000299 public PMDataManager,
300 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000301
302public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000303 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000304 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000305 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000306
Devang Patel376fefa2006-11-08 10:29:57 +0000307 /// add - Add a pass to the queue of passes to run. This passes ownership of
308 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
309 /// will be destroyed as well, so there is no need to delete the pass. This
310 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000311 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000312 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000313 }
Devang Patel376fefa2006-11-08 10:29:57 +0000314
315 /// run - Execute all of the passes scheduled for execution. Keep track of
316 /// whether any of the passes modifies the module, and if so, return true.
317 bool run(Module &M);
318
Devang Patelf9d96b92006-12-07 19:57:52 +0000319 /// Pass Manager itself does not invalidate any analysis info.
320 void getAnalysisUsage(AnalysisUsage &Info) const {
321 Info.setPreservesAll();
322 }
323
Devang Patelabcd1d32006-12-07 21:27:23 +0000324 inline void addTopLevelPass(Pass *P) {
Devang Patelfa971cd2006-12-08 23:57:43 +0000325 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000326
327 // P is a immutable pass and it will be managed by this
328 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000329 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000330 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000331 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000332 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000333 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000334 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000335 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000336 }
Devang Patelabcd1d32006-12-07 21:27:23 +0000337 }
338
Devang Patel67d6a5e2006-12-19 19:46:59 +0000339 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000340 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000341 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
342 return MP;
343 }
Devang Patel376fefa2006-11-08 10:29:57 +0000344};
345
Devang Patel8c78a0b2007-05-03 01:11:54 +0000346char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000347} // End of llvm namespace
348
349namespace {
350
351//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000352/// TimingInfo Class - This class is used to calculate information about the
353/// amount of time each pass takes to execute. This only happens when
354/// -time-passes is enabled on the command line.
355///
Devang Patel1c3633e2007-01-29 23:10:37 +0000356class VISIBILITY_HIDDEN TimingInfo {
357 std::map<Pass*, Timer> TimingData;
358 TimerGroup TG;
359
360public:
361 // Use 'create' member to get this.
362 TimingInfo() : TG("... Pass execution timing report ...") {}
363
364 // TimingDtor - Print out information about timing information
365 ~TimingInfo() {
366 // Delete all of the timers...
367 TimingData.clear();
368 // TimerGroup is deleted next, printing the report.
369 }
370
371 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
372 // to a non null value (if the -time-passes option is enabled) or it leaves it
373 // null. It may be called multiple times.
374 static void createTheTimeInfo();
375
376 void passStarted(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000377 if (dynamic_cast<PMDataManager *>(P))
378 return;
379
380 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
381 if (I == TimingData.end())
382 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
383 I->second.startTimer();
384 }
385 void passEnded(Pass *P) {
Devang Patel1c3633e2007-01-29 23:10:37 +0000386 if (dynamic_cast<PMDataManager *>(P))
387 return;
388
389 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
Chris Lattner60987362009-03-06 05:53:14 +0000390 assert(I != TimingData.end() && "passStarted/passEnded not nested right!");
Devang Patel1c3633e2007-01-29 23:10:37 +0000391 I->second.stopTimer();
392 }
393};
394
Devang Patel1c3633e2007-01-29 23:10:37 +0000395} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000396
Dan Gohmand78c4002008-05-13 00:00:25 +0000397static TimingInfo *TheTimeInfo;
398
Devang Patela1514cb2006-12-07 19:39:39 +0000399//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000400// PMTopLevelManager implementation
401
Devang Patel4268fc02007-01-16 02:00:38 +0000402/// Initialize top level manager. Create first pass manager.
Chris Lattner4c1e9542009-03-06 06:45:05 +0000403PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) {
Devang Patel4268fc02007-01-16 02:00:38 +0000404 if (t == TLM_Pass) {
405 MPPassManager *MPP = new MPPassManager(1);
406 MPP->setTopLevelManager(this);
407 addPassManager(MPP);
408 activeStack.push(MPP);
Chris Lattner4c1e9542009-03-06 06:45:05 +0000409 } else if (t == TLM_Function) {
Devang Patel4268fc02007-01-16 02:00:38 +0000410 FPPassManager *FPP = new FPPassManager(1);
411 FPP->setTopLevelManager(this);
412 addPassManager(FPP);
413 activeStack.push(FPP);
414 }
415}
416
Devang Patelafb1f3622006-12-12 22:35:25 +0000417/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000418void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000419 Pass *P) {
Devang Patel8adae862007-07-20 18:04:54 +0000420 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000421 E = AnalysisPasses.end(); I != E; ++I) {
422 Pass *AP = *I;
423 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000424
425 if (P == AP)
426 continue;
427
Devang Patelafb1f3622006-12-12 22:35:25 +0000428 // If AP is the last user of other passes then make P last user of
429 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000430 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000431 LUE = LastUser.end(); LUI != LUE; ++LUI) {
432 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000433 // DenseMap iterator is not invalidated here because
434 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000435 LastUser[LUI->first] = P;
436 }
437 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000438}
439
440/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000441void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000442 Pass *P) {
443 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
444 InversedLastUser.find(P);
445 if (DMI == InversedLastUser.end())
446 return;
447
448 SmallPtrSet<Pass *, 8> &LU = DMI->second;
449 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
450 E = LU.end(); I != E; ++I) {
451 LastUses.push_back(*I);
452 }
453
Devang Patelafb1f3622006-12-12 22:35:25 +0000454}
455
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000456AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
457 AnalysisUsage *AnUsage = NULL;
458 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
459 if (DMI != AnUsageMap.end())
460 AnUsage = DMI->second;
461 else {
462 AnUsage = new AnalysisUsage();
463 P->getAnalysisUsage(*AnUsage);
464 AnUsageMap[P] = AnUsage;
465 }
466 return AnUsage;
467}
468
Devang Patelafb1f3622006-12-12 22:35:25 +0000469/// Schedule pass P for execution. Make sure that passes required by
470/// P are run before P is run. Update analysis info maintained by
471/// the manager. Remove dead passes. This is a recursive function.
472void PMTopLevelManager::schedulePass(Pass *P) {
473
Devang Patel3312f752007-01-16 21:43:18 +0000474 // TODO : Allocate function manager for this pass, other wise required set
475 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000476
Devang Pateld74ede72007-03-06 01:06:16 +0000477 // Give pass a chance to prepare the stage.
478 P->preparePassManager(activeStack);
479
Devang Patel864970e2008-03-18 00:39:19 +0000480 // If P is an analysis pass and it is available then do not
481 // generate the analysis again. Stale analysis info should not be
482 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000483 if (P->getPassInfo() &&
Nuno Lopes0460bb22008-11-04 23:03:58 +0000484 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) {
485 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000486 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000487 }
Devang Patel864970e2008-03-18 00:39:19 +0000488
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000489 AnalysisUsage *AnUsage = findAnalysisUsage(P);
490
Devang Patelfdee7032008-08-14 23:07:48 +0000491 bool checkAnalysis = true;
492 while (checkAnalysis) {
493 checkAnalysis = false;
494
495 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
496 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
497 E = RequiredSet.end(); I != E; ++I) {
498
499 Pass *AnalysisPass = findAnalysisPass(*I);
500 if (!AnalysisPass) {
501 AnalysisPass = (*I)->createPass();
502 if (P->getPotentialPassManagerType () ==
503 AnalysisPass->getPotentialPassManagerType())
504 // Schedule analysis pass that is managed by the same pass manager.
505 schedulePass(AnalysisPass);
506 else if (P->getPotentialPassManagerType () >
507 AnalysisPass->getPotentialPassManagerType()) {
508 // Schedule analysis pass that is managed by a new manager.
509 schedulePass(AnalysisPass);
510 // Recheck analysis passes to ensure that required analysises that
511 // are already checked are still available.
512 checkAnalysis = true;
513 }
514 else
515 // Do not schedule this analysis. Lower level analsyis
516 // passes are run on the fly.
517 delete AnalysisPass;
518 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000519 }
520 }
521
522 // Now all required passes are available.
523 addTopLevelPass(P);
524}
525
526/// Find the pass that implements Analysis AID. Search immutable
527/// passes and all pass managers. If desired pass is not found
528/// then return NULL.
529Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
530
531 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000532 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000533 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000534 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000535 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000536 P = PMD->findAnalysisPass(AID, false);
537 }
538
539 // Check other pass managers
Chris Lattner60987362009-03-06 05:53:14 +0000540 for (SmallVector<PMDataManager *, 8>::iterator
541 I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000542 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
543 P = (*I)->findAnalysisPass(AID, false);
544
Devang Patel0d29ae02008-08-12 15:44:31 +0000545 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000546 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
547 const PassInfo *PI = (*I)->getPassInfo();
548 if (PI == AID)
549 P = *I;
550
551 // If Pass not found then check the interfaces implemented by Immutable Pass
552 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000553 const std::vector<const PassInfo*> &ImmPI =
554 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000555 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
556 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000557 }
558 }
559
Devang Patelafb1f3622006-12-12 22:35:25 +0000560 return P;
561}
562
Devang Pateleda56172006-12-12 23:34:33 +0000563// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000564void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000565
Devang Patelfd4184322007-01-17 20:33:36 +0000566 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000567 return;
568
Devang Pateleda56172006-12-12 23:34:33 +0000569 // Print out the immutable passes
570 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
571 ImmutablePasses[i]->dumpPassStructure(0);
572 }
573
Dan Gohman73caf5f2008-03-13 01:48:32 +0000574 // Every class that derives from PMDataManager also derives from Pass
575 // (sometimes indirectly), but there's no inheritance relationship
576 // between PMDataManager and Pass, so we have to dynamic_cast to get
577 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000578 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000579 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000580 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000581}
582
Devang Patel991aeba2006-12-15 20:13:01 +0000583void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000584
Devang Patelfd4184322007-01-17 20:33:36 +0000585 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000586 return;
587
588 cerr << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000589 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000590 E = PassManagers.end(); I != E; ++I)
591 (*I)->dumpPassArguments();
Devang Patelcfd70c42006-12-13 22:10:00 +0000592 cerr << "\n";
593}
594
Devang Patele3068402006-12-21 00:16:50 +0000595void PMTopLevelManager::initializeAllAnalysisInfo() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000596 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000597 E = PassManagers.end(); I != E; ++I)
598 (*I)->initializeAnalysisInfo();
Devang Patele3068402006-12-21 00:16:50 +0000599
600 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000601 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000602 E = IndirectPassManagers.end(); I != E; ++I)
603 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000604
Chris Lattner60987362009-03-06 05:53:14 +0000605 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000606 DME = LastUser.end(); DMI != DME; ++DMI) {
607 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
608 InversedLastUser.find(DMI->second);
609 if (InvDMI != InversedLastUser.end()) {
610 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
611 L.insert(DMI->first);
612 } else {
613 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
614 InversedLastUser[DMI->second] = L;
615 }
616 }
Devang Patele3068402006-12-21 00:16:50 +0000617}
618
Devang Patele7599552007-01-12 18:52:44 +0000619/// Destructor
620PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000621 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000622 E = PassManagers.end(); I != E; ++I)
623 delete *I;
624
Devang Patel0d29ae02008-08-12 15:44:31 +0000625 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000626 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
627 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000628
629 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000630 DME = AnUsageMap.end(); DMI != DME; ++DMI)
631 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000632}
633
Devang Patelafb1f3622006-12-12 22:35:25 +0000634//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000635// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000636
Devang Patel643676c2006-11-11 01:10:19 +0000637/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000638void PMDataManager::recordAvailableAnalysis(Pass *P) {
Chris Lattner60987362009-03-06 05:53:14 +0000639 const PassInfo *PI = P->getPassInfo();
640 if (PI == 0) return;
641
642 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000643
Chris Lattner60987362009-03-06 05:53:14 +0000644 //This pass is the current implementation of all of the interfaces it
645 //implements as well.
646 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
647 for (unsigned i = 0, e = II.size(); i != e; ++i)
648 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000649}
650
Devang Patel9d9fc902007-03-06 17:52:53 +0000651// Return true if P preserves high level analysis used by other
652// passes managed by this manager
653bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000654 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000655 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000656 return true;
657
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000658 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000659 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000660 E = HigherLevelAnalysis.end(); I != E; ++I) {
661 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000662 if (!dynamic_cast<ImmutablePass*>(P1) &&
663 std::find(PreservedSet.begin(), PreservedSet.end(),
664 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000665 PreservedSet.end())
666 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000667 }
668
669 return true;
670}
671
Chris Lattner02eb94c2008-08-07 07:34:50 +0000672/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000673void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000674 // Don't do this unless assertions are enabled.
675#ifdef NDEBUG
676 return;
677#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000678 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
679 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000680
Devang Patelef432532007-07-19 05:36:09 +0000681 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000682 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000683 E = PreservedSet.end(); I != E; ++I) {
684 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000685 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000686 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000687 }
Devang Patela273d1c2007-07-19 18:02:32 +0000688}
689
Devang Patel9dbe4d12008-07-01 17:44:24 +0000690/// verifyDomInfo - Verify dominator information if it is available.
691void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
Devang Patel9dbe4d12008-07-01 17:44:24 +0000692 if (!VerifyDomInfo || !P.getResolver())
693 return;
694
Duncan Sands5a913d62009-01-28 13:14:17 +0000695 DominatorTree *DT = P.getAnalysisIfAvailable<DominatorTree>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000696 if (!DT)
697 return;
698
699 DominatorTree OtherDT;
700 OtherDT.getBase().recalculate(F);
701 if (DT->compare(OtherDT)) {
702 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000703 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000704 cerr << "----- Valid -----\n";
705 OtherDT.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000706 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000707 DT->dump();
Chris Lattner60987362009-03-06 05:53:14 +0000708 assert(0 && "Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000709 }
710
Duncan Sands5a913d62009-01-28 13:14:17 +0000711 DominanceFrontier *DF = P.getAnalysisIfAvailable<DominanceFrontier>();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000712 if (!DF)
713 return;
714
715 DominanceFrontier OtherDF;
716 std::vector<BasicBlock*> DTRoots = DT->getRoots();
717 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
718 if (DF->compare(OtherDF)) {
719 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000720 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000721 cerr << "----- Valid -----\n";
722 OtherDF.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000723 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000724 DF->dump();
Chris Lattner60987362009-03-06 05:53:14 +0000725 assert(0 && "Invalid dominator info");
Devang Patel9dbe4d12008-07-01 17:44:24 +0000726 }
727}
728
Devang Patel67c79a42008-07-01 19:50:56 +0000729/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000730void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000731 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
732 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000733 return;
734
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000735 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000736 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000737 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000738 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000739 if (!dynamic_cast<ImmutablePass*>(Info->second)
740 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000741 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000742 // Remove this analysis
Devang Patelbb4720c2008-06-03 01:02:16 +0000743 if (PassDebugging >= Details) {
744 Pass *S = Info->second;
Dan Gohman15269852008-07-09 00:50:40 +0000745 cerr << " -- '" << P->getPassName() << "' is not preserving '";
746 cerr << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000747 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000748 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000749 }
Devang Patel349170f2006-11-11 01:24:55 +0000750 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000751
752 // Check inherited analysis also. If P is not preserving analysis
753 // provided by parent manager then remove it here.
754 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
755
756 if (!InheritedAnalysis[Index])
757 continue;
758
759 for (std::map<AnalysisID, Pass*>::iterator
760 I = InheritedAnalysis[Index]->begin(),
761 E = InheritedAnalysis[Index]->end(); I != E; ) {
762 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000763 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
764 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000765 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000766 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000767 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000768 }
769 }
Devang Patelf68a3492006-11-07 22:35:17 +0000770}
771
Devang Patelca189262006-11-14 03:05:08 +0000772/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000773void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000774 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000775
Devang Patel8adae862007-07-20 18:04:54 +0000776 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000777
Devang Patel2ff44922007-04-16 20:39:59 +0000778 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000779 if (!TPM)
780 return;
781
Devang Patel17ad0962006-12-08 00:37:52 +0000782 TPM->collectLastUses(DeadPasses, P);
783
Devang Patel656a9172008-06-06 17:50:36 +0000784 if (PassDebugging >= Details && !DeadPasses.empty()) {
Dan Gohman15269852008-07-09 00:50:40 +0000785 cerr << " -*- '" << P->getPassName();
786 cerr << "' is the last user of following pass instances.";
Devang Patel656a9172008-06-06 17:50:36 +0000787 cerr << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000788 }
789
Devang Patel8adae862007-07-20 18:04:54 +0000790 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000791 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000792
Devang Patel003a5592007-03-05 20:01:30 +0000793 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000794
Chris Lattner4c1e9542009-03-06 06:45:05 +0000795 {
796 // If the pass crashes releasing memory, remember this.
797 PassManagerPrettyStackEntry X(*I);
798
799 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
800 (*I)->releaseMemory();
801 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
802 }
Devang Patelc3e3ca92008-10-06 20:36:36 +0000803 if (const PassInfo *PI = (*I)->getPassInfo()) {
804 std::map<AnalysisID, Pass*>::iterator Pos =
805 AvailableAnalysis.find(PI);
Devang Patelb8817b92006-12-14 00:59:42 +0000806
Devang Patelc3e3ca92008-10-06 20:36:36 +0000807 // It is possible that pass is already removed from the AvailableAnalysis
808 if (Pos != AvailableAnalysis.end())
809 AvailableAnalysis.erase(Pos);
810
811 // Remove all interfaces this pass implements, for which it is also
812 // listed as the available implementation.
813 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
814 for (unsigned i = 0, e = II.size(); i != e; ++i) {
815 Pos = AvailableAnalysis.find(II[i]);
816 if (Pos != AvailableAnalysis.end() && Pos->second == *I)
817 AvailableAnalysis.erase(Pos);
818 }
819 }
Devang Patel17ad0962006-12-08 00:37:52 +0000820 }
Devang Patelca189262006-11-14 03:05:08 +0000821}
822
Devang Patel8f677ce2006-12-07 18:47:25 +0000823/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000824/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000825void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000826 // This manager is going to manage pass P. Set up analysis resolver
827 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000828 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000829 P->setResolver(AR);
830
Devang Patelec2b9a72007-03-05 22:57:49 +0000831 // If a FunctionPass F is the last user of ModulePass info M
832 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000833 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000834
Chris Lattner60987362009-03-06 05:53:14 +0000835 if (!ProcessAnalysis) {
836 // Add pass
837 PassVector.push_back(P);
838 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000839 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000840
Chris Lattner60987362009-03-06 05:53:14 +0000841 // At the moment, this pass is the last user of all required passes.
842 SmallVector<Pass *, 12> LastUses;
843 SmallVector<Pass *, 8> RequiredPasses;
844 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
845
846 unsigned PDepth = this->getDepth();
847
848 collectRequiredAnalysis(RequiredPasses,
849 ReqAnalysisNotAvailable, P);
850 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
851 E = RequiredPasses.end(); I != E; ++I) {
852 Pass *PRequired = *I;
853 unsigned RDepth = 0;
854
855 assert(PRequired->getResolver() && "Analysis Resolver is not set");
856 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
857 RDepth = DM.getDepth();
858
859 if (PDepth == RDepth)
860 LastUses.push_back(PRequired);
861 else if (PDepth > RDepth) {
862 // Let the parent claim responsibility of last use
863 TransferLastUses.push_back(PRequired);
864 // Keep track of higher level analysis used by this manager.
865 HigherLevelAnalysis.push_back(PRequired);
866 } else
867 assert(0 && "Unable to accomodate Required Pass");
868 }
869
870 // Set P as P's last user until someone starts using P.
871 // However, if P is a Pass Manager then it does not need
872 // to record its last user.
873 if (!dynamic_cast<PMDataManager *>(P))
874 LastUses.push_back(P);
875 TPM->setLastUser(LastUses, P);
876
877 if (!TransferLastUses.empty()) {
878 Pass *My_PM = dynamic_cast<Pass *>(this);
879 TPM->setLastUser(TransferLastUses, My_PM);
880 TransferLastUses.clear();
881 }
882
883 // Now, take care of required analysises that are not available.
884 for (SmallVector<AnalysisID, 8>::iterator
885 I = ReqAnalysisNotAvailable.begin(),
886 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
887 Pass *AnalysisPass = (*I)->createPass();
888 this->addLowerLevelRequiredPass(P, AnalysisPass);
889 }
890
891 // Take a note of analysis required and made available by this pass.
892 // Remove the analysis not preserved by this pass
893 removeNotPreservedAnalysis(P);
894 recordAvailableAnalysis(P);
895
Devang Patel8cad70d2006-11-11 01:51:02 +0000896 // Add pass
897 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000898}
899
Devang Patele64d3052007-04-16 20:12:57 +0000900
901/// Populate RP with analysis pass that are required by
902/// pass P and are available. Populate RP_NotAvail with analysis
903/// pass that are required by pass P but are not available.
904void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
905 SmallVector<AnalysisID, 8> &RP_NotAvail,
906 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000907 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
908 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000909 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +0000910 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000911 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
912 RP.push_back(AnalysisPass);
913 else
Chris Lattner60987362009-03-06 05:53:14 +0000914 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +0000915 }
Devang Patelf58183d2006-12-12 23:09:32 +0000916
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000917 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000918 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000919 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000920 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
921 RP.push_back(AnalysisPass);
922 else
Chris Lattner60987362009-03-06 05:53:14 +0000923 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +0000924 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000925}
926
Devang Patel07f4f582006-11-14 21:49:36 +0000927// All Required analyses should be available to the pass as it runs! Here
928// we fill in the AnalysisImpls member of the pass so that it can
929// successfully use the getAnalysis() method to retrieve the
930// implementations it needs.
931//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000932void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000933 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
934
Chris Lattnercbd160f2008-08-08 05:33:04 +0000935 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000936 I = AnUsage->getRequiredSet().begin(),
937 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000938 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000939 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000940 // This may be analysis pass that is initialized on the fly.
941 // If that is not the case then it will raise an assert when it is used.
942 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000943 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +0000944 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000945 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000946 }
947}
948
Devang Patel640c5bb2006-12-08 22:30:11 +0000949/// Find the pass that implements Analysis AID. If desired pass is not found
950/// then return NULL.
951Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
952
953 // Check if AvailableAnalysis map has one entry.
954 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
955
956 if (I != AvailableAnalysis.end())
957 return I->second;
958
959 // Search Parents through TopLevelManager
960 if (SearchParent)
961 return TPM->findAnalysisPass(AID);
962
Devang Patel9d759b82006-12-09 00:09:12 +0000963 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000964}
965
Devang Patel991aeba2006-12-15 20:13:01 +0000966// Print list of passes that are last used by P.
967void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
968
Devang Patel8adae862007-07-20 18:04:54 +0000969 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000970
971 // If this is a on the fly manager then it does not have TPM.
972 if (!TPM)
973 return;
974
Devang Patel991aeba2006-12-15 20:13:01 +0000975 TPM->collectLastUses(LUses, P);
976
Devang Patel8adae862007-07-20 18:04:54 +0000977 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000978 E = LUses.end(); I != E; ++I) {
979 llvm::cerr << "--" << std::string(Offset*2, ' ');
980 (*I)->dumpPassStructure(0);
981 }
982}
983
984void PMDataManager::dumpPassArguments() const {
Chris Lattner60987362009-03-06 05:53:14 +0000985 for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000986 E = PassVector.end(); I != E; ++I) {
987 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
988 PMD->dumpPassArguments();
989 else
990 if (const PassInfo *PI = (*I)->getPassInfo())
991 if (!PI->isAnalysisGroup())
992 cerr << " -" << PI->getPassArgument();
993 }
994}
995
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000996void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
997 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000998 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000999 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001000 return;
1001 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001002 switch (S1) {
1003 case EXECUTION_MSG:
1004 cerr << "Executing Pass '" << P->getPassName();
1005 break;
1006 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001007 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001008 break;
1009 case FREEING_MSG:
1010 cerr << " Freeing Pass '" << P->getPassName();
1011 break;
1012 default:
1013 break;
1014 }
1015 switch (S2) {
1016 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001017 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001018 break;
1019 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001020 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001021 break;
1022 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001023 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001024 break;
1025 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001026 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001027 break;
1028 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001029 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001030 break;
1031 default:
1032 break;
1033 }
Devang Patel991aeba2006-12-15 20:13:01 +00001034}
1035
Chris Lattner4c1e9542009-03-06 06:45:05 +00001036void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001037 if (PassDebugging < Details)
1038 return;
1039
1040 AnalysisUsage analysisUsage;
1041 P->getAnalysisUsage(analysisUsage);
1042 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1043}
1044
Chris Lattner4c1e9542009-03-06 06:45:05 +00001045void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001046 if (PassDebugging < Details)
1047 return;
1048
1049 AnalysisUsage analysisUsage;
1050 P->getAnalysisUsage(analysisUsage);
1051 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1052}
1053
1054void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001055 const AnalysisUsage::VectorType &Set) const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001056 assert(PassDebugging >= Details);
1057 if (Set.empty())
1058 return;
1059 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001060 for (unsigned i = 0; i != Set.size(); ++i) {
1061 if (i) cerr << ",";
1062 cerr << " " << Set[i]->getPassName();
1063 }
1064 cerr << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001065}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001066
Devang Patel004937b2007-07-27 20:06:09 +00001067/// Add RequiredPass into list of lower level passes required by pass P.
1068/// RequiredPass is run on the fly by Pass Manager when P requests it
1069/// through getAnalysis interface.
1070/// This should be handled by specific pass manager.
1071void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1072 if (TPM) {
1073 TPM->dumpArguments();
1074 TPM->dumpPasses();
1075 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001076
1077 // Module Level pass may required Function Level analysis info
1078 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1079 // to provide this on demand. In that case, in Pass manager terminology,
1080 // module level pass is requiring lower level analysis info managed by
1081 // lower level pass manager.
1082
1083 // When Pass manager is not able to order required analysis info, Pass manager
1084 // checks whether any lower level manager will be able to provide this
1085 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001086#ifndef NDEBUG
Dan Gohman15269852008-07-09 00:50:40 +00001087 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1088 cerr << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001089#endif
Chris Lattner60987362009-03-06 05:53:14 +00001090 assert(0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001091}
1092
Devang Patele7599552007-01-12 18:52:44 +00001093// Destructor
1094PMDataManager::~PMDataManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +00001095 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001096 E = PassVector.end(); I != E; ++I)
1097 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001098}
1099
Devang Patel9bdf7d42006-12-08 23:28:54 +00001100//===----------------------------------------------------------------------===//
1101// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001102// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1103Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001104 return PM.findAnalysisPass(ID, dir);
1105}
1106
Devang Patel92942812007-04-16 20:56:24 +00001107Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1108 Function &F) {
1109 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1110}
1111
Devang Patela1514cb2006-12-07 19:39:39 +00001112//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001113// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001114
Devang Patel6e5a1132006-11-07 21:31:57 +00001115/// Execute all of the passes scheduled for execution by invoking
1116/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1117/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001118bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001119 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001120 return false;
1121
Devang Patele9585592006-12-08 01:38:28 +00001122 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001123
Devang Patel6e5a1132006-11-07 21:31:57 +00001124 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001125 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1126 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001127
Devang Pateld305c402007-08-10 18:29:32 +00001128 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001129 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001130
Devang Patelabfbe3b2006-12-16 00:56:26 +00001131 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001132
Chris Lattner4c1e9542009-03-06 06:45:05 +00001133 {
1134 // If the pass crashes, remember this.
1135 PassManagerPrettyStackEntry X(BP, *I);
1136
1137 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1138 Changed |= BP->runOnBasicBlock(*I);
1139 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1140 }
Devang Patel93a197c2006-12-14 00:08:04 +00001141
Devang Patel003a5592007-03-05 20:01:30 +00001142 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001143 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1144 I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001145 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001146
Devang Patela273d1c2007-07-19 18:02:32 +00001147 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001148 removeNotPreservedAnalysis(BP);
1149 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001150 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001151 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001152
Devang Patel56d48ec2006-12-15 22:57:49 +00001153 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001154}
1155
Devang Patel475c4532006-12-08 00:59:05 +00001156// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001157bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001158 bool Changed = false;
1159
Chris Lattner4c1e9542009-03-06 06:45:05 +00001160 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1161 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001162
1163 return Changed;
1164}
1165
Duncan Sands51495602009-02-13 09:42:34 +00001166bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001167 bool Changed = false;
1168
Chris Lattner4c1e9542009-03-06 06:45:05 +00001169 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1170 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001171
1172 return Changed;
1173}
1174
Duncan Sands51495602009-02-13 09:42:34 +00001175bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001176 bool Changed = false;
1177
Devang Patelabfbe3b2006-12-16 00:56:26 +00001178 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1179 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001180 Changed |= BP->doInitialization(F);
1181 }
1182
1183 return Changed;
1184}
1185
Duncan Sands51495602009-02-13 09:42:34 +00001186bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001187 bool Changed = false;
1188
Devang Patelabfbe3b2006-12-16 00:56:26 +00001189 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1190 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001191 Changed |= BP->doFinalization(F);
1192 }
1193
1194 return Changed;
1195}
1196
1197
Devang Patela1514cb2006-12-07 19:39:39 +00001198//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001199// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001200
Devang Patel4e12f862006-11-08 10:44:40 +00001201/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001202FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001203 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001204 // FPM is the top level manager.
1205 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001206
Dan Gohman565df952008-03-13 02:08:36 +00001207 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001208 FPM->setResolver(AR);
1209
Devang Patel1f653682006-12-08 18:57:16 +00001210 MP = P;
1211}
1212
Devang Patelb67904d2006-12-13 02:36:01 +00001213FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001214 delete FPM;
1215}
1216
Devang Patel4e12f862006-11-08 10:44:40 +00001217/// add - Add a pass to the queue of passes to run. This passes
1218/// ownership of the Pass to the PassManager. When the
1219/// PassManager_X is destroyed, the pass will be destroyed as well, so
1220/// there is no need to delete the pass. (TODO delete passes.)
1221/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001222void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001223 FPM->add(P);
1224}
1225
Devang Patel9f3083e2006-11-15 19:39:54 +00001226/// run - Execute all of the passes scheduled for execution. Keep
1227/// track of whether any of the passes modifies the function, and if
1228/// so, return true.
1229///
Devang Patelb67904d2006-12-13 02:36:01 +00001230bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001231 std::string errstr;
1232 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001233 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001234 abort();
1235 }
Devang Patel272908d2006-12-08 22:57:48 +00001236 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001237}
1238
1239
Devang Patelff631ae2006-11-15 01:27:05 +00001240/// doInitialization - Run all of the initializers for the function passes.
1241///
Devang Patelb67904d2006-12-13 02:36:01 +00001242bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001243 return FPM->doInitialization(*MP->getModule());
1244}
1245
Dan Gohmane6656eb2007-07-30 14:51:13 +00001246/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001247///
Devang Patelb67904d2006-12-13 02:36:01 +00001248bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001249 return FPM->doFinalization(*MP->getModule());
1250}
1251
Devang Patela1514cb2006-12-07 19:39:39 +00001252//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001253// FunctionPassManagerImpl implementation
1254//
Duncan Sands51495602009-02-13 09:42:34 +00001255bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001256 bool Changed = false;
1257
Chris Lattner4c1e9542009-03-06 06:45:05 +00001258 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1259 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001260
1261 return Changed;
1262}
1263
Duncan Sands51495602009-02-13 09:42:34 +00001264bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001265 bool Changed = false;
1266
Chris Lattner4c1e9542009-03-06 06:45:05 +00001267 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1268 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001269
1270 return Changed;
1271}
1272
1273// Execute all the passes managed by this top level manager.
1274// Return true if any function is modified by a pass.
1275bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001276 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001277 TimingInfo::createTheTimeInfo();
1278
1279 dumpArguments();
1280 dumpPasses();
1281
Devang Patele3068402006-12-21 00:16:50 +00001282 initializeAllAnalysisInfo();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001283 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1284 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001285 return Changed;
1286}
1287
1288//===----------------------------------------------------------------------===//
1289// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001290
Devang Patel8c78a0b2007-05-03 01:11:54 +00001291char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001292/// Print passes managed by this manager
1293void FPPassManager::dumpPassStructure(unsigned Offset) {
1294 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1295 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1296 FunctionPass *FP = getContainedPass(Index);
1297 FP->dumpPassStructure(Offset + 1);
1298 dumpLastUses(FP, Offset+1);
1299 }
1300}
1301
1302
Devang Patel0c2012f2006-11-07 21:49:50 +00001303/// Execute all of the passes scheduled for execution by invoking
1304/// runOnFunction method. Keep track of whether any of the passes modifies
1305/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001306bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001307 if (F.isDeclaration())
1308 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001309
1310 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001311
Devang Patelcbbf2912008-03-20 01:09:53 +00001312 // Collect inherited analysis from Module level pass manager.
1313 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001314
Devang Patelabfbe3b2006-12-16 00:56:26 +00001315 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1316 FunctionPass *FP = getContainedPass(Index);
1317
Devang Pateld305c402007-08-10 18:29:32 +00001318 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001319 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001320
Devang Patelabfbe3b2006-12-16 00:56:26 +00001321 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001322
Chris Lattner4c1e9542009-03-06 06:45:05 +00001323 {
1324 PassManagerPrettyStackEntry X(FP, F);
1325
1326 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1327 Changed |= FP->runOnFunction(F);
1328 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1329 }
Devang Patel93a197c2006-12-14 00:08:04 +00001330
Devang Patel003a5592007-03-05 20:01:30 +00001331 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001332 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001333 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001334
Devang Patela273d1c2007-07-19 18:02:32 +00001335 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001336 removeNotPreservedAnalysis(FP);
1337 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001338 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001339
Devang Patel67c79a42008-07-01 19:50:56 +00001340 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001341 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001342 }
1343 return Changed;
1344}
1345
Devang Patel67d6a5e2006-12-19 19:46:59 +00001346bool FPPassManager::runOnModule(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001347 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001348
Chris Lattner60987362009-03-06 05:53:14 +00001349 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1350 runOnFunction(*I);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001351
1352 return Changed |= doFinalization(M);
1353}
1354
Duncan Sands51495602009-02-13 09:42:34 +00001355bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001356 bool Changed = false;
1357
Chris Lattner4c1e9542009-03-06 06:45:05 +00001358 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1359 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001360
1361 return Changed;
1362}
1363
Duncan Sands51495602009-02-13 09:42:34 +00001364bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001365 bool Changed = false;
1366
Chris Lattner4c1e9542009-03-06 06:45:05 +00001367 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1368 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patelff631ae2006-11-15 01:27:05 +00001369
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
Chris Lattner4c1e9542009-03-06 06:45:05 +00001392 {
1393 PassManagerPrettyStackEntry X(MP, M);
1394 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1395 Changed |= MP->runOnModule(M);
1396 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1397 }
Devang Patel93a197c2006-12-14 00:08:04 +00001398
Devang Patel003a5592007-03-05 20:01:30 +00001399 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001400 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1401 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001402 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001403
Devang Patela273d1c2007-07-19 18:02:32 +00001404 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001405 removeNotPreservedAnalysis(MP);
1406 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001407 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001408 }
1409 return Changed;
1410}
1411
Devang Patele64d3052007-04-16 20:12:57 +00001412/// Add RequiredPass into list of lower level passes required by pass P.
1413/// RequiredPass is run on the fly by Pass Manager when P requests it
1414/// through getAnalysis interface.
1415void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001416 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1417 "Unable to handle Pass that requires lower level Analysis pass");
1418 assert((P->getPotentialPassManagerType() <
1419 RequiredPass->getPotentialPassManagerType()) &&
1420 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patele64d3052007-04-16 20:12:57 +00001421
Devang Patel68f72b12007-04-26 17:50:19 +00001422 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001423 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001424 FPP = new FunctionPassManagerImpl(0);
1425 // FPP is the top level manager.
1426 FPP->setTopLevelManager(FPP);
1427
Devang Patel69e9f6d2007-04-16 20:27:05 +00001428 OnTheFlyManagers[P] = FPP;
1429 }
Devang Patel68f72b12007-04-26 17:50:19 +00001430 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001431
Devang Patel68f72b12007-04-26 17:50:19 +00001432 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001433 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001434 LU.push_back(RequiredPass);
1435 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001436}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001437
1438/// Return function pass corresponding to PassInfo PI, that is
1439/// required by module pass MP. Instantiate analysis pass, by using
1440/// its runOnFunction() for function F.
Chris Lattner60987362009-03-06 05:53:14 +00001441Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001442 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001443 assert(FPP && "Unable to find on the fly pass");
Devang Patel69e9f6d2007-04-16 20:27:05 +00001444
Devang Patel68f72b12007-04-26 17:50:19 +00001445 FPP->run(F);
Chris Lattner60987362009-03-06 05:53:14 +00001446 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI);
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 bool Changed = false;
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();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001463 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1464 Changed |= getContainedManager(Index)->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001465 return Changed;
1466}
Devang Patel376fefa2006-11-08 10:29:57 +00001467
Devang Patela1514cb2006-12-07 19:39:39 +00001468//===----------------------------------------------------------------------===//
1469// PassManager implementation
1470
Devang Patel376fefa2006-11-08 10:29:57 +00001471/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001472PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001473 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001474 // PM is the top level manager
1475 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001476}
1477
Devang Patelb67904d2006-12-13 02:36:01 +00001478PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001479 delete PM;
1480}
1481
Devang Patel376fefa2006-11-08 10:29:57 +00001482/// add - Add a pass to the queue of passes to run. This passes ownership of
1483/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1484/// will be destroyed as well, so there is no need to delete the pass. This
1485/// implies that all passes MUST be allocated with 'new'.
Chris Lattner60987362009-03-06 05:53:14 +00001486void PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001487 PM->add(P);
1488}
1489
1490/// run - Execute all of the passes scheduled for execution. Keep track of
1491/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001492bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001493 return PM->run(M);
1494}
1495
Devang Patelb8817b92006-12-14 00:59:42 +00001496//===----------------------------------------------------------------------===//
1497// TimingInfo Class - This class is used to calculate information about the
1498// amount of time each pass takes to execute. This only happens with
1499// -time-passes is enabled on the command line.
1500//
1501bool llvm::TimePassesIsEnabled = false;
1502static cl::opt<bool,true>
1503EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1504 cl::desc("Time each pass, printing elapsed time for each on exit"));
1505
1506// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1507// a non null value (if the -time-passes option is enabled) or it leaves it
1508// null. It may be called multiple times.
1509void TimingInfo::createTheTimeInfo() {
1510 if (!TimePassesIsEnabled || TheTimeInfo) return;
1511
1512 // Constructed the first time this is called, iff -time-passes is enabled.
1513 // This guarantees that the object will be constructed before static globals,
1514 // thus it will be destroyed before them.
1515 static ManagedStatic<TimingInfo> TTI;
1516 TheTimeInfo = &*TTI;
1517}
1518
Devang Patel1c3633e2007-01-29 23:10:37 +00001519/// If TimingInfo is enabled then start pass timer.
1520void StartPassTimer(Pass *P) {
1521 if (TheTimeInfo)
1522 TheTimeInfo->passStarted(P);
1523}
1524
1525/// If TimingInfo is enabled then stop pass timer.
1526void StopPassTimer(Pass *P) {
1527 if (TheTimeInfo)
1528 TheTimeInfo->passEnded(P);
1529}
1530
Devang Patel1c56a632007-01-08 19:29:38 +00001531//===----------------------------------------------------------------------===//
1532// PMStack implementation
1533//
Devang Patelad98d232007-01-11 22:15:30 +00001534
Devang Patel1c56a632007-01-08 19:29:38 +00001535// Pop Pass Manager from the stack and clear its analysis info.
1536void PMStack::pop() {
1537
1538 PMDataManager *Top = this->top();
1539 Top->initializeAnalysisInfo();
1540
1541 S.pop_back();
1542}
1543
1544// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001545void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001546 assert(PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001547
Chris Lattner60987362009-03-06 05:53:14 +00001548 if (!this->empty()) {
1549 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001550
Chris Lattner60987362009-03-06 05:53:14 +00001551 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001552 TPM->addIndirectPassManager(PM);
1553 PM->setTopLevelManager(TPM);
1554 }
1555
Devang Patel15701b52007-01-11 00:19:00 +00001556 S.push_back(PM);
1557}
1558
1559// Dump content of the pass manager stack.
1560void PMStack::dump() {
Chris Lattner60987362009-03-06 05:53:14 +00001561 for (std::deque<PMDataManager *>::iterator I = S.begin(),
1562 E = S.end(); I != E; ++I)
1563 printf("%s ", dynamic_cast<Pass *>(*I)->getPassName());
1564
Devang Patel15701b52007-01-11 00:19:00 +00001565 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001566 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001567}
1568
Devang Patel1c56a632007-01-08 19:29:38 +00001569/// Find appropriate Module Pass Manager in the PM Stack and
1570/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001571void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001572 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001573 // Find Module Pass Manager
1574 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001575 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1576 if (TopPMType == PreferredType)
1577 break; // We found desired pass manager
1578 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001579 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001580 else
1581 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001582 }
Devang Patel18ff6362008-09-09 21:38:40 +00001583 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001584 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001585}
1586
Devang Patel3312f752007-01-16 21:43:18 +00001587/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1588/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001589void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001590 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001591
Devang Patela3286902008-09-09 17:56:50 +00001592 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001593 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001594 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1595 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001596 else
Devang Patel3312f752007-01-16 21:43:18 +00001597 break;
1598 }
1599 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1600
1601 // Create new Function Pass Manager
1602 if (!FPP) {
1603 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1604 PMDataManager *PMD = PMS.top();
1605
1606 // [1] Create new Function Pass Manager
1607 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001608 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001609
1610 // [2] Set up new manager's top level manager
1611 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1612 TPM->addIndirectPassManager(FPP);
1613
1614 // [3] Assign manager to manage this new manager. This may create
1615 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001616 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001617
1618 // [4] Push new manager into PMS
1619 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001620 }
1621
Devang Patel3312f752007-01-16 21:43:18 +00001622 // Assign FPP as the manager of this pass.
1623 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001624}
1625
Devang Patel3312f752007-01-16 21:43:18 +00001626/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001627/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001628void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001629 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001630 BBPassManager *BBP = NULL;
1631
Devang Patel15701b52007-01-11 00:19:00 +00001632 // Basic Pass Manager is a leaf pass manager. It does not handle
1633 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001634 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001635 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001636
Devang Patel3312f752007-01-16 21:43:18 +00001637 // If leaf manager is not Basic Block Pass manager then create new
1638 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001639
Devang Patel3312f752007-01-16 21:43:18 +00001640 if (!BBP) {
1641 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1642 PMDataManager *PMD = PMS.top();
1643
1644 // [1] Create new Basic Block Manager
1645 BBP = new BBPassManager(PMD->getDepth() + 1);
1646
1647 // [2] Set up new manager's top level manager
1648 // Basic Block Pass Manager does not live by itself
1649 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1650 TPM->addIndirectPassManager(BBP);
1651
Devang Patel15701b52007-01-11 00:19:00 +00001652 // [3] Assign manager to manage this new manager. This may create
1653 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001654 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001655
Devang Patel3312f752007-01-16 21:43:18 +00001656 // [4] Push new manager into PMS
1657 PMS.push(BBP);
1658 }
Devang Patel1c56a632007-01-08 19:29:38 +00001659
Devang Patel3312f752007-01-16 21:43:18 +00001660 // Assign BBP as the manager of this pass.
1661 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001662}
1663
Dan Gohmand3a20c92008-03-11 16:41:42 +00001664PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001665
1666/*===-- C Bindings --------------------------------------------------------===*/
1667
1668LLVMPassManagerRef LLVMCreatePassManager() {
1669 return wrap(new PassManager());
1670}
1671
1672LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1673 return wrap(new FunctionPassManager(unwrap(P)));
1674}
1675
1676int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1677 return unwrap<PassManager>(PM)->run(*unwrap(M));
1678}
1679
1680int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1681 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1682}
1683
1684int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1685 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1686}
1687
1688int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1689 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1690}
1691
1692void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1693 delete unwrap(PM);
1694}