blob: cd7138cad13194154fa14527fd1116da8097907e [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"
Gordon Henriksen878114b2008-03-16 04:20:44 +000022#include "llvm-c/Core.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000023#include <algorithm>
Devang Patela9844592006-11-11 01:31:05 +000024#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000025#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000026using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000027
Devang Patele7599552007-01-12 18:52:44 +000028// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000029
Devang Patelf1567a52006-12-13 20:03:48 +000030namespace llvm {
31
32//===----------------------------------------------------------------------===//
33// Pass debugging information. Often it is useful to find out what pass is
34// running when a crash occurs in a utility. When this library is compiled with
35// debugging on, a command line option (--debug-pass) is enabled that causes the
36// pass name to be printed before it executes.
37//
38
Devang Patel03fb5872006-12-13 21:13:31 +000039// Different debug levels that can be enabled...
40enum PassDebugLevel {
41 None, Arguments, Structure, Executions, Details
42};
43
Devang Patelf1567a52006-12-13 20:03:48 +000044static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000045PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000046 cl::desc("Print PassManager debugging information"),
47 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000048 clEnumVal(None , "disable debug output"),
49 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
50 clEnumVal(Structure , "print pass structure before run()"),
51 clEnumVal(Executions, "print pass name before it is executed"),
52 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000053 clEnumValEnd));
54} // End of llvm namespace
55
Devang Patelffca9102006-12-15 19:39:30 +000056namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000057
Devang Patelf33f3eb2006-12-07 19:21:29 +000058//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000059// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000060//
Devang Patel67d6a5e2006-12-19 19:46:59 +000061/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000062/// pass together and sequence them to process one basic block before
63/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000064class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
65 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000066
67public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000068 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +000069 explicit BBPassManager(int Depth)
Devang Patel09f162c2007-05-01 21:15:47 +000070 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +000071
Devang Patelca58e352006-11-08 10:05:38 +000072 /// Execute all of the passes scheduled for execution. Keep track of
73 /// whether any of the passes modifies the function, and if so, return true.
74 bool runOnFunction(Function &F);
75
Devang Patelf9d96b92006-12-07 19:57:52 +000076 /// Pass Manager itself does not invalidate any analysis info.
77 void getAnalysisUsage(AnalysisUsage &Info) const {
78 Info.setPreservesAll();
79 }
80
Devang Patel475c4532006-12-08 00:59:05 +000081 bool doInitialization(Module &M);
82 bool doInitialization(Function &F);
83 bool doFinalization(Module &M);
84 bool doFinalization(Function &F);
85
Devang Patele3858e62007-02-01 22:08:25 +000086 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +000087 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +000088 }
89
Devang Pateleda56172006-12-12 23:34:33 +000090 // Print passes managed by this manager
91 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000092 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000093 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
94 BasicBlockPass *BP = getContainedPass(Index);
95 BP->dumpPassStructure(Offset + 1);
96 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000097 }
98 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000099
100 BasicBlockPass *getContainedPass(unsigned N) {
101 assert ( N < PassVector.size() && "Pass number out of range!");
102 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
103 return BP;
104 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000105
Devang Patel28349ab2007-02-27 15:00:39 +0000106 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000107 return PMT_BasicBlockPassManager;
108 }
Devang Patelca58e352006-11-08 10:05:38 +0000109};
110
Devang Patel8c78a0b2007-05-03 01:11:54 +0000111char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000112}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000113
Devang Patele7599552007-01-12 18:52:44 +0000114namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000115
Devang Patel10c2ca62006-12-12 22:47:13 +0000116//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000117// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000118//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000119/// FunctionPassManagerImpl manages FPPassManagers
120class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000121 public PMDataManager,
122 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000123public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000124 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000125 explicit FunctionPassManagerImpl(int Depth) :
Devang Patel09f162c2007-05-01 21:15:47 +0000126 Pass((intptr_t)&ID), PMDataManager(Depth),
127 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000128
129 /// add - Add a pass to the queue of passes to run. This passes ownership of
130 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
131 /// will be destroyed as well, so there is no need to delete the pass. This
132 /// implies that all passes MUST be allocated with 'new'.
133 void add(Pass *P) {
134 schedulePass(P);
135 }
136
137 /// run - Execute all of the passes scheduled for execution. Keep track of
138 /// whether any of the passes modifies the module, and if so, return true.
139 bool run(Function &F);
140
141 /// doInitialization - Run all of the initializers for the function passes.
142 ///
143 bool doInitialization(Module &M);
144
Dan Gohmane6656eb2007-07-30 14:51:13 +0000145 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000146 ///
147 bool doFinalization(Module &M);
148
149 /// Pass Manager itself does not invalidate any analysis info.
150 void getAnalysisUsage(AnalysisUsage &Info) const {
151 Info.setPreservesAll();
152 }
153
154 inline void addTopLevelPass(Pass *P) {
155
156 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
157
158 // P is a immutable pass and it will be managed by this
159 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000160 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000161 P->setResolver(AR);
162 initializeAnalysisImpl(P);
163 addImmutablePass(IP);
164 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000165 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000166 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000167 }
Devang Patel0f080042007-01-12 17:23:48 +0000168
Devang Patel67d6a5e2006-12-19 19:46:59 +0000169 }
170
171 FPPassManager *getContainedManager(unsigned N) {
172 assert ( N < PassManagers.size() && "Pass number out of range!");
173 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
174 return FP;
175 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000176};
177
Devang Patel8c78a0b2007-05-03 01:11:54 +0000178char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000179//===----------------------------------------------------------------------===//
180// MPPassManager
181//
182/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000183/// It batches all Module passes and function pass managers together and
184/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000185class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000186
187public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000188 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000189 explicit MPPassManager(int Depth) :
190 Pass((intptr_t)&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000191
192 // Delete on the fly managers.
193 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000194 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000195 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
196 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000197 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000198 delete FPP;
199 }
200 }
201
Devang Patelca58e352006-11-08 10:05:38 +0000202 /// run - Execute all of the passes scheduled for execution. Keep track of
203 /// whether any of the passes modifies the module, and if so, return true.
204 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000205
Devang Patelf9d96b92006-12-07 19:57:52 +0000206 /// Pass Manager itself does not invalidate any analysis info.
207 void getAnalysisUsage(AnalysisUsage &Info) const {
208 Info.setPreservesAll();
209 }
210
Devang Patele64d3052007-04-16 20:12:57 +0000211 /// Add RequiredPass into list of lower level passes required by pass P.
212 /// RequiredPass is run on the fly by Pass Manager when P requests it
213 /// through getAnalysis interface.
214 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
215
Devang Patel69e9f6d2007-04-16 20:27:05 +0000216 /// Return function pass corresponding to PassInfo PI, that is
217 /// required by module pass MP. Instantiate analysis pass, by using
218 /// its runOnFunction() for function F.
219 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
220
Devang Patele3858e62007-02-01 22:08:25 +0000221 virtual const char *getPassName() const {
222 return "Module Pass Manager";
223 }
224
Devang Pateleda56172006-12-12 23:34:33 +0000225 // Print passes managed by this manager
226 void dumpPassStructure(unsigned Offset) {
227 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000228 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
229 ModulePass *MP = getContainedPass(Index);
230 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000231 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000232 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000233 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000234 }
235 }
236
Devang Patelabfbe3b2006-12-16 00:56:26 +0000237 ModulePass *getContainedPass(unsigned N) {
238 assert ( N < PassVector.size() && "Pass number out of range!");
239 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
240 return MP;
241 }
242
Devang Patel28349ab2007-02-27 15:00:39 +0000243 virtual PassManagerType getPassManagerType() const {
244 return PMT_ModulePassManager;
245 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000246
247 private:
248 /// Collection of on the fly FPPassManagers. These managers manage
249 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000250 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000251};
252
Devang Patel8c78a0b2007-05-03 01:11:54 +0000253char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000254//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000255// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000256//
Devang Patel09f162c2007-05-01 21:15:47 +0000257
Devang Patel67d6a5e2006-12-19 19:46:59 +0000258/// PassManagerImpl manages MPPassManagers
259class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000260 public PMDataManager,
261 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000262
263public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000264 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000265 explicit PassManagerImpl(int Depth) :
266 Pass((intptr_t)&ID), PMDataManager(Depth),
267 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000268
Devang Patel376fefa2006-11-08 10:29:57 +0000269 /// add - Add a pass to the queue of passes to run. This passes ownership of
270 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
271 /// will be destroyed as well, so there is no need to delete the pass. This
272 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000273 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000274 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000275 }
Devang Patel376fefa2006-11-08 10:29:57 +0000276
277 /// run - Execute all of the passes scheduled for execution. Keep track of
278 /// whether any of the passes modifies the module, and if so, return true.
279 bool run(Module &M);
280
Devang Patelf9d96b92006-12-07 19:57:52 +0000281 /// Pass Manager itself does not invalidate any analysis info.
282 void getAnalysisUsage(AnalysisUsage &Info) const {
283 Info.setPreservesAll();
284 }
285
Devang Patelabcd1d32006-12-07 21:27:23 +0000286 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000287
Devang Patelfa971cd2006-12-08 23:57:43 +0000288 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000289
290 // P is a immutable pass and it will be managed by this
291 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000292 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000293 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000294 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000295 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000296 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000297 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000298 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000299 }
Devang Patel0f080042007-01-12 17:23:48 +0000300
Devang Patelabcd1d32006-12-07 21:27:23 +0000301 }
302
Devang Patel67d6a5e2006-12-19 19:46:59 +0000303 MPPassManager *getContainedManager(unsigned N) {
304 assert ( N < PassManagers.size() && "Pass number out of range!");
305 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
306 return MP;
307 }
308
Devang Patel376fefa2006-11-08 10:29:57 +0000309};
310
Devang Patel8c78a0b2007-05-03 01:11:54 +0000311char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000312} // End of llvm namespace
313
314namespace {
315
316//===----------------------------------------------------------------------===//
317// TimingInfo Class - This class is used to calculate information about the
318// amount of time each pass takes to execute. This only happens when
319// -time-passes is enabled on the command line.
320//
321
322class VISIBILITY_HIDDEN TimingInfo {
323 std::map<Pass*, Timer> TimingData;
324 TimerGroup TG;
325
326public:
327 // Use 'create' member to get this.
328 TimingInfo() : TG("... Pass execution timing report ...") {}
329
330 // TimingDtor - Print out information about timing information
331 ~TimingInfo() {
332 // Delete all of the timers...
333 TimingData.clear();
334 // TimerGroup is deleted next, printing the report.
335 }
336
337 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
338 // to a non null value (if the -time-passes option is enabled) or it leaves it
339 // null. It may be called multiple times.
340 static void createTheTimeInfo();
341
342 void passStarted(Pass *P) {
343
344 if (dynamic_cast<PMDataManager *>(P))
345 return;
346
347 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
348 if (I == TimingData.end())
349 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
350 I->second.startTimer();
351 }
352 void passEnded(Pass *P) {
353
354 if (dynamic_cast<PMDataManager *>(P))
355 return;
356
357 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
358 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
359 I->second.stopTimer();
360 }
361};
362
Devang Patel1c3633e2007-01-29 23:10:37 +0000363} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000364
Dan Gohmand78c4002008-05-13 00:00:25 +0000365static TimingInfo *TheTimeInfo;
366
Devang Patela1514cb2006-12-07 19:39:39 +0000367//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000368// PMTopLevelManager implementation
369
Devang Patel4268fc02007-01-16 02:00:38 +0000370/// Initialize top level manager. Create first pass manager.
371PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
372
373 if (t == TLM_Pass) {
374 MPPassManager *MPP = new MPPassManager(1);
375 MPP->setTopLevelManager(this);
376 addPassManager(MPP);
377 activeStack.push(MPP);
378 }
379 else if (t == TLM_Function) {
380 FPPassManager *FPP = new FPPassManager(1);
381 FPP->setTopLevelManager(this);
382 addPassManager(FPP);
383 activeStack.push(FPP);
384 }
385}
386
Devang Patelafb1f3622006-12-12 22:35:25 +0000387/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000388void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000389 Pass *P) {
390
Devang Patel8adae862007-07-20 18:04:54 +0000391 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000392 E = AnalysisPasses.end(); I != E; ++I) {
393 Pass *AP = *I;
394 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000395
396 if (P == AP)
397 continue;
398
Devang Patelafb1f3622006-12-12 22:35:25 +0000399 // If AP is the last user of other passes then make P last user of
400 // such passes.
401 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
402 LUE = LastUser.end(); LUI != LUE; ++LUI) {
403 if (LUI->second == AP)
404 LastUser[LUI->first] = P;
405 }
406 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000407}
408
409/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000410void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000411 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000412 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
413 LUE = LastUser.end(); LUI != LUE; ++LUI)
414 if (LUI->second == P)
415 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000416}
417
418/// Schedule pass P for execution. Make sure that passes required by
419/// P are run before P is run. Update analysis info maintained by
420/// the manager. Remove dead passes. This is a recursive function.
421void PMTopLevelManager::schedulePass(Pass *P) {
422
Devang Patel3312f752007-01-16 21:43:18 +0000423 // TODO : Allocate function manager for this pass, other wise required set
424 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000425
Devang Pateld74ede72007-03-06 01:06:16 +0000426 // Give pass a chance to prepare the stage.
427 P->preparePassManager(activeStack);
428
Devang Patel864970e2008-03-18 00:39:19 +0000429 // If P is an analysis pass and it is available then do not
430 // generate the analysis again. Stale analysis info should not be
431 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000432 if (P->getPassInfo() &&
433 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
Devang Patelaf75ab82008-03-19 00:48:41 +0000434 return;
Devang Patel864970e2008-03-18 00:39:19 +0000435
Devang Patelafb1f3622006-12-12 22:35:25 +0000436 AnalysisUsage AnUsage;
437 P->getAnalysisUsage(AnUsage);
438 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
439 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
440 E = RequiredSet.end(); I != E; ++I) {
441
442 Pass *AnalysisPass = findAnalysisPass(*I);
443 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000444 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000445 // Schedule this analysis run first only if it is not a lower level
446 // analysis pass. Lower level analsyis passes are run on the fly.
447 if (P->getPotentialPassManagerType () >=
448 AnalysisPass->getPotentialPassManagerType())
449 schedulePass(AnalysisPass);
450 else
451 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000452 }
453 }
454
455 // Now all required passes are available.
456 addTopLevelPass(P);
457}
458
459/// Find the pass that implements Analysis AID. Search immutable
460/// passes and all pass managers. If desired pass is not found
461/// then return NULL.
462Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
463
464 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000465 // Check pass managers
Dan Gohman73caf5f2008-03-13 01:48:32 +0000466 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000467 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000468 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000469 P = PMD->findAnalysisPass(AID, false);
470 }
471
472 // Check other pass managers
473 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
474 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
475 P = (*I)->findAnalysisPass(AID, false);
476
Devang Patelafb1f3622006-12-12 22:35:25 +0000477 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
478 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
479 const PassInfo *PI = (*I)->getPassInfo();
480 if (PI == AID)
481 P = *I;
482
483 // If Pass not found then check the interfaces implemented by Immutable Pass
484 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000485 const std::vector<const PassInfo*> &ImmPI =
486 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000487 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
488 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000489 }
490 }
491
Devang Patelafb1f3622006-12-12 22:35:25 +0000492 return P;
493}
494
Devang Pateleda56172006-12-12 23:34:33 +0000495// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000496void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000497
Devang Patelfd4184322007-01-17 20:33:36 +0000498 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000499 return;
500
Devang Pateleda56172006-12-12 23:34:33 +0000501 // Print out the immutable passes
502 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
503 ImmutablePasses[i]->dumpPassStructure(0);
504 }
505
Dan Gohman73caf5f2008-03-13 01:48:32 +0000506 // Every class that derives from PMDataManager also derives from Pass
507 // (sometimes indirectly), but there's no inheritance relationship
508 // between PMDataManager and Pass, so we have to dynamic_cast to get
509 // from a PMDataManager* to a Pass*.
510 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000511 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000512 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000513}
514
Devang Patel991aeba2006-12-15 20:13:01 +0000515void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000516
Devang Patelfd4184322007-01-17 20:33:36 +0000517 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000518 return;
519
520 cerr << "Pass Arguments: ";
Dan Gohman73caf5f2008-03-13 01:48:32 +0000521 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000522 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000523 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000524 PMD->dumpPassArguments();
525 }
526 cerr << "\n";
527}
528
Devang Patele3068402006-12-21 00:16:50 +0000529void PMTopLevelManager::initializeAllAnalysisInfo() {
530
Dan Gohman73caf5f2008-03-13 01:48:32 +0000531 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000532 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000533 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000534 PMD->initializeAnalysisInfo();
535 }
536
537 // Initailize other pass managers
538 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
539 E = IndirectPassManagers.end(); I != E; ++I)
540 (*I)->initializeAnalysisInfo();
541}
542
Devang Patele7599552007-01-12 18:52:44 +0000543/// Destructor
544PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000545 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000546 E = PassManagers.end(); I != E; ++I)
547 delete *I;
548
549 for (std::vector<ImmutablePass *>::iterator
550 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
551 delete *I;
Devang Patele7599552007-01-12 18:52:44 +0000552}
553
Devang Patelafb1f3622006-12-12 22:35:25 +0000554//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000555// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000556
Devang Patel643676c2006-11-11 01:10:19 +0000557/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000558void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000559
Devang Patel643676c2006-11-11 01:10:19 +0000560 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000561 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000562
Devang Patele9976aa2006-12-07 19:33:53 +0000563 //This pass is the current implementation of all of the interfaces it
564 //implements as well.
565 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
566 for (unsigned i = 0, e = II.size(); i != e; ++i)
567 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000568 }
569}
570
Devang Patel9d9fc902007-03-06 17:52:53 +0000571// Return true if P preserves high level analysis used by other
572// passes managed by this manager
573bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
574
575 AnalysisUsage AnUsage;
576 P->getAnalysisUsage(AnUsage);
577
578 if (AnUsage.getPreservesAll())
579 return true;
580
581 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
582 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
583 E = HigherLevelAnalysis.end(); I != E; ++I) {
584 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000585 if (!dynamic_cast<ImmutablePass*>(P1) &&
586 std::find(PreservedSet.begin(), PreservedSet.end(),
587 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000588 PreservedSet.end())
589 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000590 }
591
592 return true;
593}
594
Devang Patela273d1c2007-07-19 18:02:32 +0000595/// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
596void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000597 AnalysisUsage AnUsage;
598 P->getAnalysisUsage(AnUsage);
Devang Patelef432532007-07-19 05:36:09 +0000599 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000600
Devang Patelef432532007-07-19 05:36:09 +0000601 // Verify preserved analysis
Devang Patela273d1c2007-07-19 18:02:32 +0000602 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
603 E = PreservedSet.end(); I != E; ++I) {
604 AnalysisID AID = *I;
605 Pass *AP = findAnalysisPass(AID, true);
606 if (AP)
607 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000608 }
Devang Patela273d1c2007-07-19 18:02:32 +0000609}
610
611/// Remove Analyss not preserved by Pass P
612void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
613 AnalysisUsage AnUsage;
614 P->getAnalysisUsage(AnUsage);
Devang Patel2e169c32006-12-07 20:03:49 +0000615 if (AnUsage.getPreservesAll())
616 return;
617
Devang Patela273d1c2007-07-19 18:02:32 +0000618 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000619 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000620 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000621 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000622 if (!dynamic_cast<ImmutablePass*>(Info->second)
623 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000624 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000625 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000626 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000627 if (PassDebugging >= Details) {
628 Pass *S = Info->second;
629 cerr << " -- " << P->getPassName() << " is not preserving ";
630 cerr << S->getPassName() << "\n";
631 }
632 }
Devang Patel349170f2006-11-11 01:24:55 +0000633 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000634
635 // Check inherited analysis also. If P is not preserving analysis
636 // provided by parent manager then remove it here.
637 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
638
639 if (!InheritedAnalysis[Index])
640 continue;
641
642 for (std::map<AnalysisID, Pass*>::iterator
643 I = InheritedAnalysis[Index]->begin(),
644 E = InheritedAnalysis[Index]->end(); I != E; ) {
645 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000646 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
647 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000648 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000649 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000650 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000651 }
652 }
653
Devang Patelf68a3492006-11-07 22:35:17 +0000654}
655
Devang Patelca189262006-11-14 03:05:08 +0000656/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000657void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000658 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000659
Devang Patel8adae862007-07-20 18:04:54 +0000660 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000661
Devang Patel2ff44922007-04-16 20:39:59 +0000662 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000663 if (!TPM)
664 return;
665
Devang Patel17ad0962006-12-08 00:37:52 +0000666 TPM->collectLastUses(DeadPasses, P);
667
Evan Cheng93af6ce2008-06-04 09:13:31 +0000668 if (PassDebugging >= Details) {
669 cerr << " -*- " << P->getPassName();
670 cerr << " is the last users of following passes. Free them\n";
671 }
672
Devang Patel8adae862007-07-20 18:04:54 +0000673 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000674 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000675
Devang Patel003a5592007-03-05 20:01:30 +0000676 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000677
Devang Patel7ebf09d2007-03-05 18:20:51 +0000678 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000679 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000680 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000681
Devang Patel17ad0962006-12-08 00:37:52 +0000682 std::map<AnalysisID, Pass*>::iterator Pos =
683 AvailableAnalysis.find((*I)->getPassInfo());
684
Devang Patel475c4532006-12-08 00:59:05 +0000685 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000686 if (Pos != AvailableAnalysis.end())
687 AvailableAnalysis.erase(Pos);
688 }
Devang Patelca189262006-11-14 03:05:08 +0000689}
690
Devang Patel8f677ce2006-12-07 18:47:25 +0000691/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000692/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000693void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000694 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000695
Devang Pateld440cd92006-12-08 23:53:00 +0000696 // This manager is going to manage pass P. Set up analysis resolver
697 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000698 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000699 P->setResolver(AR);
700
Devang Patelec2b9a72007-03-05 22:57:49 +0000701 // If a FunctionPass F is the last user of ModulePass info M
702 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000703 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000704
Devang Patel90b05e02006-11-11 02:04:19 +0000705 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000706
707 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000708 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000709 SmallVector<Pass *, 8> RequiredPasses;
710 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
711
Devang Patelbc03f132006-12-07 23:55:10 +0000712 unsigned PDepth = this->getDepth();
713
Devang Patele64d3052007-04-16 20:12:57 +0000714 collectRequiredAnalysis(RequiredPasses,
715 ReqAnalysisNotAvailable, P);
716 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000717 E = RequiredPasses.end(); I != E; ++I) {
718 Pass *PRequired = *I;
719 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000720
721 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
722 RDepth = DM.getDepth();
723
Devang Patelbc03f132006-12-07 23:55:10 +0000724 if (PDepth == RDepth)
725 LastUses.push_back(PRequired);
726 else if (PDepth > RDepth) {
727 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000728 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000729 // Keep track of higher level analysis used by this manager.
730 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000731 } else
732 assert (0 && "Unable to accomodate Required Pass");
733 }
734
Devang Patel39786a92007-01-15 20:31:54 +0000735 // Set P as P's last user until someone starts using P.
736 // However, if P is a Pass Manager then it does not need
737 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000738 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000739 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000740 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000741
Devang Patelec2b9a72007-03-05 22:57:49 +0000742 if (!TransferLastUses.empty()) {
743 Pass *My_PM = dynamic_cast<Pass *>(this);
744 TPM->setLastUser(TransferLastUses, My_PM);
745 TransferLastUses.clear();
746 }
747
Devang Patel69e9f6d2007-04-16 20:27:05 +0000748 // Now, take care of required analysises that are not available.
749 for (SmallVector<AnalysisID, 8>::iterator
750 I = ReqAnalysisNotAvailable.begin(),
751 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
752 Pass *AnalysisPass = (*I)->createPass();
753 this->addLowerLevelRequiredPass(P, AnalysisPass);
754 }
755
Devang Patel17bff0d2006-12-07 22:09:36 +0000756 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000757 // Remove the analysis not preserved by this pass
758 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000759 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000760 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000761
762 // Add pass
763 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000764}
765
Devang Patele64d3052007-04-16 20:12:57 +0000766
767/// Populate RP with analysis pass that are required by
768/// pass P and are available. Populate RP_NotAvail with analysis
769/// pass that are required by pass P but are not available.
770void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
771 SmallVector<AnalysisID, 8> &RP_NotAvail,
772 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000773 AnalysisUsage AnUsage;
774 P->getAnalysisUsage(AnUsage);
775 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
776 for (std::vector<AnalysisID>::const_iterator
777 I = RequiredSet.begin(), E = RequiredSet.end();
778 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000779 AnalysisID AID = *I;
780 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
781 RP.push_back(AnalysisPass);
782 else
783 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000784 }
Devang Patelf58183d2006-12-12 23:09:32 +0000785
786 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
787 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
788 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000789 AnalysisID AID = *I;
790 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
791 RP.push_back(AnalysisPass);
792 else
793 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000794 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000795}
796
Devang Patel07f4f582006-11-14 21:49:36 +0000797// All Required analyses should be available to the pass as it runs! Here
798// we fill in the AnalysisImpls member of the pass so that it can
799// successfully use the getAnalysis() method to retrieve the
800// implementations it needs.
801//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000802void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000803 AnalysisUsage AnUsage;
804 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000805
806 for (std::vector<const PassInfo *>::const_iterator
807 I = AnUsage.getRequiredSet().begin(),
808 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000809 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000810 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000811 // This may be analysis pass that is initialized on the fly.
812 // If that is not the case then it will raise an assert when it is used.
813 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000814 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000815 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000816 }
817}
818
Devang Patel640c5bb2006-12-08 22:30:11 +0000819/// Find the pass that implements Analysis AID. If desired pass is not found
820/// then return NULL.
821Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
822
823 // Check if AvailableAnalysis map has one entry.
824 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
825
826 if (I != AvailableAnalysis.end())
827 return I->second;
828
829 // Search Parents through TopLevelManager
830 if (SearchParent)
831 return TPM->findAnalysisPass(AID);
832
Devang Patel9d759b82006-12-09 00:09:12 +0000833 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000834}
835
Devang Patel991aeba2006-12-15 20:13:01 +0000836// Print list of passes that are last used by P.
837void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
838
Devang Patel8adae862007-07-20 18:04:54 +0000839 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000840
841 // If this is a on the fly manager then it does not have TPM.
842 if (!TPM)
843 return;
844
Devang Patel991aeba2006-12-15 20:13:01 +0000845 TPM->collectLastUses(LUses, P);
846
Devang Patel8adae862007-07-20 18:04:54 +0000847 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000848 E = LUses.end(); I != E; ++I) {
849 llvm::cerr << "--" << std::string(Offset*2, ' ');
850 (*I)->dumpPassStructure(0);
851 }
852}
853
854void PMDataManager::dumpPassArguments() const {
855 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
856 E = PassVector.end(); I != E; ++I) {
857 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
858 PMD->dumpPassArguments();
859 else
860 if (const PassInfo *PI = (*I)->getPassInfo())
861 if (!PI->isAnalysisGroup())
862 cerr << " -" << PI->getPassArgument();
863 }
864}
865
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000866void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
867 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000868 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000869 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000870 return;
871 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000872 switch (S1) {
873 case EXECUTION_MSG:
874 cerr << "Executing Pass '" << P->getPassName();
875 break;
876 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000877 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000878 break;
879 case FREEING_MSG:
880 cerr << " Freeing Pass '" << P->getPassName();
881 break;
882 default:
883 break;
884 }
885 switch (S2) {
886 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000887 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000888 break;
889 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000890 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000891 break;
892 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000893 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000894 break;
895 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000896 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000897 break;
898 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000899 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000900 break;
901 default:
902 break;
903 }
Devang Patel991aeba2006-12-15 20:13:01 +0000904}
905
906void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
907 const std::vector<AnalysisID> &Set)
908 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000909 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000910 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
911 for (unsigned i = 0; i != Set.size(); ++i) {
912 if (i) cerr << ",";
913 cerr << " " << Set[i]->getPassName();
914 }
915 cerr << "\n";
916 }
917}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000918
Devang Patel004937b2007-07-27 20:06:09 +0000919/// Add RequiredPass into list of lower level passes required by pass P.
920/// RequiredPass is run on the fly by Pass Manager when P requests it
921/// through getAnalysis interface.
922/// This should be handled by specific pass manager.
923void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
924 if (TPM) {
925 TPM->dumpArguments();
926 TPM->dumpPasses();
927 }
Devang Patel8df7cc12008-02-02 01:43:30 +0000928
929 // Module Level pass may required Function Level analysis info
930 // (e.g. dominator info). Pass manager uses on the fly function pass manager
931 // to provide this on demand. In that case, in Pass manager terminology,
932 // module level pass is requiring lower level analysis info managed by
933 // lower level pass manager.
934
935 // When Pass manager is not able to order required analysis info, Pass manager
936 // checks whether any lower level manager will be able to provide this
937 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +0000938#ifndef NDEBUG
939 cerr << "Unable to schedule " << RequiredPass->getPassName();
940 cerr << " required by " << P->getPassName() << "\n";
941#endif
942 assert (0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +0000943}
944
Devang Patele7599552007-01-12 18:52:44 +0000945// Destructor
946PMDataManager::~PMDataManager() {
947
948 for (std::vector<Pass *>::iterator I = PassVector.begin(),
949 E = PassVector.end(); I != E; ++I)
950 delete *I;
951
Devang Patele7599552007-01-12 18:52:44 +0000952}
953
Devang Patel9bdf7d42006-12-08 23:28:54 +0000954//===----------------------------------------------------------------------===//
955// NOTE: Is this the right place to define this method ?
956// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000957Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000958 return PM.findAnalysisPass(ID, dir);
959}
960
Devang Patel92942812007-04-16 20:56:24 +0000961Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
962 Function &F) {
963 return PM.getOnTheFlyPass(P, AnalysisPI, F);
964}
965
Devang Patela1514cb2006-12-07 19:39:39 +0000966//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000967// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000968
Devang Patel6e5a1132006-11-07 21:31:57 +0000969/// Execute all of the passes scheduled for execution by invoking
970/// runOnBasicBlock method. Keep track of whether any of the passes modifies
971/// the function, and if so, return true.
972bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000973BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000974
Reid Spencer5301e7c2007-01-30 20:08:39 +0000975 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000976 return false;
977
Devang Patele9585592006-12-08 01:38:28 +0000978 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000979
Devang Patel6e5a1132006-11-07 21:31:57 +0000980 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000981 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
982 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000983 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000984 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000985
Devang Pateld305c402007-08-10 18:29:32 +0000986 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000987 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000988
Devang Patelabfbe3b2006-12-16 00:56:26 +0000989 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000990
Devang Patelabfbe3b2006-12-16 00:56:26 +0000991 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000992 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000993 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000994
Devang Patel003a5592007-03-05 20:01:30 +0000995 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +0000996 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
997 I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000998 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000999
Devang Patela273d1c2007-07-19 18:02:32 +00001000 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001001 removeNotPreservedAnalysis(BP);
1002 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001003 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001004 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001005
Devang Patel56d48ec2006-12-15 22:57:49 +00001006 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001007}
1008
Devang Patel475c4532006-12-08 00:59:05 +00001009// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001010inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001011 bool Changed = false;
1012
Devang Patelabfbe3b2006-12-16 00:56:26 +00001013 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1014 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001015 Changed |= BP->doInitialization(M);
1016 }
1017
1018 return Changed;
1019}
1020
Devang Patel67d6a5e2006-12-19 19:46:59 +00001021inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001022 bool Changed = false;
1023
Devang Patelabfbe3b2006-12-16 00:56:26 +00001024 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1025 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001026 Changed |= BP->doFinalization(M);
1027 }
1028
1029 return Changed;
1030}
1031
Devang Patel67d6a5e2006-12-19 19:46:59 +00001032inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001033 bool Changed = false;
1034
Devang Patelabfbe3b2006-12-16 00:56:26 +00001035 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1036 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001037 Changed |= BP->doInitialization(F);
1038 }
1039
1040 return Changed;
1041}
1042
Devang Patel67d6a5e2006-12-19 19:46:59 +00001043inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001044 bool Changed = false;
1045
Devang Patelabfbe3b2006-12-16 00:56:26 +00001046 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1047 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001048 Changed |= BP->doFinalization(F);
1049 }
1050
1051 return Changed;
1052}
1053
1054
Devang Patela1514cb2006-12-07 19:39:39 +00001055//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001056// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001057
Devang Patel4e12f862006-11-08 10:44:40 +00001058/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001059FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001060 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001061 // FPM is the top level manager.
1062 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001063
Dan Gohman565df952008-03-13 02:08:36 +00001064 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001065 FPM->setResolver(AR);
1066
Devang Patel1f653682006-12-08 18:57:16 +00001067 MP = P;
1068}
1069
Devang Patelb67904d2006-12-13 02:36:01 +00001070FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001071 delete FPM;
1072}
1073
Devang Patel4e12f862006-11-08 10:44:40 +00001074/// add - Add a pass to the queue of passes to run. This passes
1075/// ownership of the Pass to the PassManager. When the
1076/// PassManager_X is destroyed, the pass will be destroyed as well, so
1077/// there is no need to delete the pass. (TODO delete passes.)
1078/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001079void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001080 FPM->add(P);
1081}
1082
Devang Patel9f3083e2006-11-15 19:39:54 +00001083/// run - Execute all of the passes scheduled for execution. Keep
1084/// track of whether any of the passes modifies the function, and if
1085/// so, return true.
1086///
Devang Patelb67904d2006-12-13 02:36:01 +00001087bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001088 std::string errstr;
1089 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001090 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001091 abort();
1092 }
Devang Patel272908d2006-12-08 22:57:48 +00001093 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001094}
1095
1096
Devang Patelff631ae2006-11-15 01:27:05 +00001097/// doInitialization - Run all of the initializers for the function passes.
1098///
Devang Patelb67904d2006-12-13 02:36:01 +00001099bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001100 return FPM->doInitialization(*MP->getModule());
1101}
1102
Dan Gohmane6656eb2007-07-30 14:51:13 +00001103/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001104///
Devang Patelb67904d2006-12-13 02:36:01 +00001105bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001106 return FPM->doFinalization(*MP->getModule());
1107}
1108
Devang Patela1514cb2006-12-07 19:39:39 +00001109//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001110// FunctionPassManagerImpl implementation
1111//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001112inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1113 bool Changed = false;
1114
1115 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1116 FPPassManager *FP = getContainedManager(Index);
1117 Changed |= FP->doInitialization(M);
1118 }
1119
1120 return Changed;
1121}
1122
1123inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1124 bool Changed = false;
1125
1126 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1127 FPPassManager *FP = getContainedManager(Index);
1128 Changed |= FP->doFinalization(M);
1129 }
1130
1131 return Changed;
1132}
1133
1134// Execute all the passes managed by this top level manager.
1135// Return true if any function is modified by a pass.
1136bool FunctionPassManagerImpl::run(Function &F) {
1137
1138 bool Changed = false;
1139
1140 TimingInfo::createTheTimeInfo();
1141
1142 dumpArguments();
1143 dumpPasses();
1144
Devang Patele3068402006-12-21 00:16:50 +00001145 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001146 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1147 FPPassManager *FP = getContainedManager(Index);
1148 Changed |= FP->runOnFunction(F);
1149 }
1150 return Changed;
1151}
1152
1153//===----------------------------------------------------------------------===//
1154// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001155
Devang Patel8c78a0b2007-05-03 01:11:54 +00001156char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001157/// Print passes managed by this manager
1158void FPPassManager::dumpPassStructure(unsigned Offset) {
1159 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1160 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1161 FunctionPass *FP = getContainedPass(Index);
1162 FP->dumpPassStructure(Offset + 1);
1163 dumpLastUses(FP, Offset+1);
1164 }
1165}
1166
1167
Devang Patel0c2012f2006-11-07 21:49:50 +00001168/// Execute all of the passes scheduled for execution by invoking
1169/// runOnFunction method. Keep track of whether any of the passes modifies
1170/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001171bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001172
1173 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001174
Reid Spencer5301e7c2007-01-30 20:08:39 +00001175 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001176 return false;
Devang Patelcbbf2912008-03-20 01:09:53 +00001177
1178 // Collect inherited analysis from Module level pass manager.
1179 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001180
Devang Patelabfbe3b2006-12-16 00:56:26 +00001181 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1182 FunctionPass *FP = getContainedPass(Index);
1183
Devang Patelf6d1d212006-12-14 00:25:06 +00001184 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001185 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001186
Devang Pateld305c402007-08-10 18:29:32 +00001187 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001188 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001189
Devang Patelabfbe3b2006-12-16 00:56:26 +00001190 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001191
Devang Patelabfbe3b2006-12-16 00:56:26 +00001192 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001193 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001194 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001195
Devang Patel003a5592007-03-05 20:01:30 +00001196 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001197 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001198 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001199
Devang Patela273d1c2007-07-19 18:02:32 +00001200 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001201 removeNotPreservedAnalysis(FP);
1202 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001203 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001204 }
1205 return Changed;
1206}
1207
Devang Patel67d6a5e2006-12-19 19:46:59 +00001208bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001209
Devang Patel67d6a5e2006-12-19 19:46:59 +00001210 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001211
1212 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1213 this->runOnFunction(*I);
1214
1215 return Changed |= doFinalization(M);
1216}
1217
1218inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001219 bool Changed = false;
1220
Devang Patelabfbe3b2006-12-16 00:56:26 +00001221 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1222 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001223 Changed |= FP->doInitialization(M);
1224 }
1225
1226 return Changed;
1227}
1228
Devang Patel67d6a5e2006-12-19 19:46:59 +00001229inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001230 bool Changed = false;
1231
Devang Patelabfbe3b2006-12-16 00:56:26 +00001232 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1233 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001234 Changed |= FP->doFinalization(M);
1235 }
1236
Devang Patelff631ae2006-11-15 01:27:05 +00001237 return Changed;
1238}
1239
Devang Patela1514cb2006-12-07 19:39:39 +00001240//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001241// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001242
Devang Patel05e1a972006-11-07 22:03:15 +00001243/// Execute all of the passes scheduled for execution by invoking
1244/// runOnModule method. Keep track of whether any of the passes modifies
1245/// the module, and if so, return true.
1246bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001247MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001248 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001249
Devang Patelabfbe3b2006-12-16 00:56:26 +00001250 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1251 ModulePass *MP = getContainedPass(Index);
1252
Devang Patelf6d1d212006-12-14 00:25:06 +00001253 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001254 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001255
Dan Gohman929391a2008-01-29 12:09:55 +00001256 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1257 M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001258 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001259
Devang Patelabfbe3b2006-12-16 00:56:26 +00001260 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001261
Devang Patelabfbe3b2006-12-16 00:56:26 +00001262 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001263 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001264 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001265
Devang Patel003a5592007-03-05 20:01:30 +00001266 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001267 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1268 M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001269 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001270
Devang Patela273d1c2007-07-19 18:02:32 +00001271 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001272 removeNotPreservedAnalysis(MP);
1273 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001274 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001275 }
1276 return Changed;
1277}
1278
Devang Patele64d3052007-04-16 20:12:57 +00001279/// Add RequiredPass into list of lower level passes required by pass P.
1280/// RequiredPass is run on the fly by Pass Manager when P requests it
1281/// through getAnalysis interface.
1282void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1283
1284 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1285 && "Unable to handle Pass that requires lower level Analysis pass");
1286 assert ((P->getPotentialPassManagerType() <
1287 RequiredPass->getPotentialPassManagerType())
1288 && "Unable to handle Pass that requires lower level Analysis pass");
1289
Devang Patel68f72b12007-04-26 17:50:19 +00001290 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001291 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001292 FPP = new FunctionPassManagerImpl(0);
1293 // FPP is the top level manager.
1294 FPP->setTopLevelManager(FPP);
1295
Devang Patel69e9f6d2007-04-16 20:27:05 +00001296 OnTheFlyManagers[P] = FPP;
1297 }
Devang Patel68f72b12007-04-26 17:50:19 +00001298 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001299
Devang Patel68f72b12007-04-26 17:50:19 +00001300 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001301 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001302 LU.push_back(RequiredPass);
1303 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001304}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001305
1306/// Return function pass corresponding to PassInfo PI, that is
1307/// required by module pass MP. Instantiate analysis pass, by using
1308/// its runOnFunction() for function F.
1309Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1310 Function &F) {
1311 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001312 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001313 assert (FPP && "Unable to find on the fly pass");
1314
Devang Patel68f72b12007-04-26 17:50:19 +00001315 FPP->run(F);
1316 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001317}
1318
1319
Devang Patela1514cb2006-12-07 19:39:39 +00001320//===----------------------------------------------------------------------===//
1321// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001322//
Devang Patelc290c8a2006-11-07 22:23:34 +00001323/// run - Execute all of the passes scheduled for execution. Keep track of
1324/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001325bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001326
Devang Patelc290c8a2006-11-07 22:23:34 +00001327 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001328
Devang Patelb8817b92006-12-14 00:59:42 +00001329 TimingInfo::createTheTimeInfo();
1330
Devang Patelcfd70c42006-12-13 22:10:00 +00001331 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001332 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001333
Devang Patele3068402006-12-21 00:16:50 +00001334 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001335 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1336 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001337 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001338 }
1339 return Changed;
1340}
Devang Patel376fefa2006-11-08 10:29:57 +00001341
Devang Patela1514cb2006-12-07 19:39:39 +00001342//===----------------------------------------------------------------------===//
1343// PassManager implementation
1344
Devang Patel376fefa2006-11-08 10:29:57 +00001345/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001346PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001347 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001348 // PM is the top level manager
1349 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001350}
1351
Devang Patelb67904d2006-12-13 02:36:01 +00001352PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001353 delete PM;
1354}
1355
Devang Patel376fefa2006-11-08 10:29:57 +00001356/// add - Add a pass to the queue of passes to run. This passes ownership of
1357/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1358/// will be destroyed as well, so there is no need to delete the pass. This
1359/// implies that all passes MUST be allocated with 'new'.
1360void
Devang Patelb67904d2006-12-13 02:36:01 +00001361PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001362 PM->add(P);
1363}
1364
1365/// run - Execute all of the passes scheduled for execution. Keep track of
1366/// whether any of the passes modifies the module, and if so, return true.
1367bool
Devang Patelb67904d2006-12-13 02:36:01 +00001368PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001369 return PM->run(M);
1370}
1371
Devang Patelb8817b92006-12-14 00:59:42 +00001372//===----------------------------------------------------------------------===//
1373// TimingInfo Class - This class is used to calculate information about the
1374// amount of time each pass takes to execute. This only happens with
1375// -time-passes is enabled on the command line.
1376//
1377bool llvm::TimePassesIsEnabled = false;
1378static cl::opt<bool,true>
1379EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1380 cl::desc("Time each pass, printing elapsed time for each on exit"));
1381
1382// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1383// a non null value (if the -time-passes option is enabled) or it leaves it
1384// null. It may be called multiple times.
1385void TimingInfo::createTheTimeInfo() {
1386 if (!TimePassesIsEnabled || TheTimeInfo) return;
1387
1388 // Constructed the first time this is called, iff -time-passes is enabled.
1389 // This guarantees that the object will be constructed before static globals,
1390 // thus it will be destroyed before them.
1391 static ManagedStatic<TimingInfo> TTI;
1392 TheTimeInfo = &*TTI;
1393}
1394
Devang Patel1c3633e2007-01-29 23:10:37 +00001395/// If TimingInfo is enabled then start pass timer.
1396void StartPassTimer(Pass *P) {
1397 if (TheTimeInfo)
1398 TheTimeInfo->passStarted(P);
1399}
1400
1401/// If TimingInfo is enabled then stop pass timer.
1402void StopPassTimer(Pass *P) {
1403 if (TheTimeInfo)
1404 TheTimeInfo->passEnded(P);
1405}
1406
Devang Patel1c56a632007-01-08 19:29:38 +00001407//===----------------------------------------------------------------------===//
1408// PMStack implementation
1409//
Devang Patelad98d232007-01-11 22:15:30 +00001410
Devang Patel1c56a632007-01-08 19:29:38 +00001411// Pop Pass Manager from the stack and clear its analysis info.
1412void PMStack::pop() {
1413
1414 PMDataManager *Top = this->top();
1415 Top->initializeAnalysisInfo();
1416
1417 S.pop_back();
1418}
1419
1420// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001421void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001422
Devang Patel15701b52007-01-11 00:19:00 +00001423 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001424 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001425
Devang Patel15701b52007-01-11 00:19:00 +00001426 if (this->empty()) {
1427 Top = PM;
1428 }
1429 else {
1430 Top = this->top();
1431 PMTopLevelManager *TPM = Top->getTopLevelManager();
1432
1433 assert (TPM && "Unable to find top level manager");
1434 TPM->addIndirectPassManager(PM);
1435 PM->setTopLevelManager(TPM);
1436 }
1437
Devang Patel15701b52007-01-11 00:19:00 +00001438 S.push_back(PM);
1439}
1440
1441// Dump content of the pass manager stack.
1442void PMStack::dump() {
1443 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1444 E = S.end(); I != E; ++I) {
1445 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001446 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001447 }
1448 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001449 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001450}
1451
Devang Patel1c56a632007-01-08 19:29:38 +00001452/// Find appropriate Module Pass Manager in the PM Stack and
1453/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001454void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001455 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001456
Devang Patel1c56a632007-01-08 19:29:38 +00001457 // Find Module Pass Manager
1458 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001459 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1460 if (TopPMType == PreferredType)
1461 break; // We found desired pass manager
1462 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001463 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001464 else
1465 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001466 }
1467
Devang Patel23f8aa92007-01-17 21:19:23 +00001468 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001469}
1470
Devang Patel3312f752007-01-16 21:43:18 +00001471/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1472/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001473void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001474 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001475
Devang Patel15701b52007-01-11 00:19:00 +00001476 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001477 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001478 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1479 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001480 else
Devang Patel3312f752007-01-16 21:43:18 +00001481 break;
1482 }
1483 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1484
1485 // Create new Function Pass Manager
1486 if (!FPP) {
1487 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1488 PMDataManager *PMD = PMS.top();
1489
1490 // [1] Create new Function Pass Manager
1491 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001492 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001493
1494 // [2] Set up new manager's top level manager
1495 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1496 TPM->addIndirectPassManager(FPP);
1497
1498 // [3] Assign manager to manage this new manager. This may create
1499 // and push new managers into PMS
Devang Pateldffca632007-01-17 20:30:17 +00001500
1501 // If Call Graph Pass Manager is active then use it to manage
1502 // this new Function Pass manager.
1503 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
Dan Gohman565df952008-03-13 02:08:36 +00001504 FPP->assignPassManager(PMS, PMT_CallGraphPassManager);
Devang Pateldffca632007-01-17 20:30:17 +00001505 else
Dan Gohman565df952008-03-13 02:08:36 +00001506 FPP->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001507
1508 // [4] Push new manager into PMS
1509 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001510 }
1511
Devang Patel3312f752007-01-16 21:43:18 +00001512 // Assign FPP as the manager of this pass.
1513 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001514}
1515
Devang Patel3312f752007-01-16 21:43:18 +00001516/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001517/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001518void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001519 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001520
1521 BBPassManager *BBP = NULL;
1522
Devang Patel15701b52007-01-11 00:19:00 +00001523 // Basic Pass Manager is a leaf pass manager. It does not handle
1524 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001525 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001526 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001527
Devang Patel3312f752007-01-16 21:43:18 +00001528 // If leaf manager is not Basic Block Pass manager then create new
1529 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001530
Devang Patel3312f752007-01-16 21:43:18 +00001531 if (!BBP) {
1532 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1533 PMDataManager *PMD = PMS.top();
1534
1535 // [1] Create new Basic Block Manager
1536 BBP = new BBPassManager(PMD->getDepth() + 1);
1537
1538 // [2] Set up new manager's top level manager
1539 // Basic Block Pass Manager does not live by itself
1540 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1541 TPM->addIndirectPassManager(BBP);
1542
Devang Patel15701b52007-01-11 00:19:00 +00001543 // [3] Assign manager to manage this new manager. This may create
1544 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001545 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001546
Devang Patel3312f752007-01-16 21:43:18 +00001547 // [4] Push new manager into PMS
1548 PMS.push(BBP);
1549 }
Devang Patel1c56a632007-01-08 19:29:38 +00001550
Devang Patel3312f752007-01-16 21:43:18 +00001551 // Assign BBP as the manager of this pass.
1552 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001553}
1554
Dan Gohmand3a20c92008-03-11 16:41:42 +00001555PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001556
1557/*===-- C Bindings --------------------------------------------------------===*/
1558
1559LLVMPassManagerRef LLVMCreatePassManager() {
1560 return wrap(new PassManager());
1561}
1562
1563LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1564 return wrap(new FunctionPassManager(unwrap(P)));
1565}
1566
1567int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1568 return unwrap<PassManager>(PM)->run(*unwrap(M));
1569}
1570
1571int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1572 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1573}
1574
1575int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1576 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1577}
1578
1579int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1580 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1581}
1582
1583void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1584 delete unwrap(PM);
1585}