blob: 7e0baeba89982b5c46168909b81c573e5b20e852 [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Devang Patelca58e352006-11-08 10:05:38 +00005// This file was developed by Devang Patel and is distributed under
Devang Patel6e5a1132006-11-07 21:31:57 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Jeff Cohenb622c112007-03-05 00:00:42 +000022#include <algorithm>
Devang Patela9844592006-11-11 01:31:05 +000023#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000024#include <map>
Devang Patelffca9102006-12-15 19:39:30 +000025
Devang Patele7599552007-01-12 18:52:44 +000026// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000027
Devang Patelf1567a52006-12-13 20:03:48 +000028namespace llvm {
29
30//===----------------------------------------------------------------------===//
31// Pass debugging information. Often it is useful to find out what pass is
32// running when a crash occurs in a utility. When this library is compiled with
33// debugging on, a command line option (--debug-pass) is enabled that causes the
34// pass name to be printed before it executes.
35//
36
Devang Patel03fb5872006-12-13 21:13:31 +000037// Different debug levels that can be enabled...
38enum PassDebugLevel {
39 None, Arguments, Structure, Executions, Details
40};
41
Devang Patelf1567a52006-12-13 20:03:48 +000042static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000043PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000044 cl::desc("Print PassManager debugging information"),
45 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000046 clEnumVal(None , "disable debug output"),
47 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
48 clEnumVal(Structure , "print pass structure before run()"),
49 clEnumVal(Executions, "print pass name before it is executed"),
50 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000051 clEnumValEnd));
52} // End of llvm namespace
53
Devang Patelffca9102006-12-15 19:39:30 +000054namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000055
Devang Patelf33f3eb2006-12-07 19:21:29 +000056//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000057// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000058//
Devang Patel67d6a5e2006-12-19 19:46:59 +000059/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000060/// pass together and sequence them to process one basic block before
61/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000062class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
63 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000064
65public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000066 static char ID;
Devang Patel09f162c2007-05-01 21:15:47 +000067 BBPassManager(int Depth)
68 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +000069
Devang Patelca58e352006-11-08 10:05:38 +000070 /// Execute all of the passes scheduled for execution. Keep track of
71 /// whether any of the passes modifies the function, and if so, return true.
72 bool runOnFunction(Function &F);
73
Devang Patelf9d96b92006-12-07 19:57:52 +000074 /// Pass Manager itself does not invalidate any analysis info.
75 void getAnalysisUsage(AnalysisUsage &Info) const {
76 Info.setPreservesAll();
77 }
78
Devang Patel475c4532006-12-08 00:59:05 +000079 bool doInitialization(Module &M);
80 bool doInitialization(Function &F);
81 bool doFinalization(Module &M);
82 bool doFinalization(Function &F);
83
Devang Patele3858e62007-02-01 22:08:25 +000084 virtual const char *getPassName() const {
85 return "BasicBlock Pass Manager";
86 }
87
Devang Pateleda56172006-12-12 23:34:33 +000088 // Print passes managed by this manager
89 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000090 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000091 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
92 BasicBlockPass *BP = getContainedPass(Index);
93 BP->dumpPassStructure(Offset + 1);
94 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000095 }
96 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000097
98 BasicBlockPass *getContainedPass(unsigned N) {
99 assert ( N < PassVector.size() && "Pass number out of range!");
100 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
101 return BP;
102 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000103
Devang Patel28349ab2007-02-27 15:00:39 +0000104 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000105 return PMT_BasicBlockPassManager;
106 }
Devang Patelca58e352006-11-08 10:05:38 +0000107};
108
Devang Patel8c78a0b2007-05-03 01:11:54 +0000109char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000110}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000111
Devang Patele7599552007-01-12 18:52:44 +0000112namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000113
Devang Patel10c2ca62006-12-12 22:47:13 +0000114//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000115// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000116//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000117/// FunctionPassManagerImpl manages FPPassManagers
118class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000119 public PMDataManager,
120 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000121public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000122 static char ID;
Devang Patel09f162c2007-05-01 21:15:47 +0000123 FunctionPassManagerImpl(int Depth) :
124 Pass((intptr_t)&ID), PMDataManager(Depth),
125 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000126
127 /// add - Add a pass to the queue of passes to run. This passes ownership of
128 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
129 /// will be destroyed as well, so there is no need to delete the pass. This
130 /// implies that all passes MUST be allocated with 'new'.
131 void add(Pass *P) {
132 schedulePass(P);
133 }
134
135 /// run - Execute all of the passes scheduled for execution. Keep track of
136 /// whether any of the passes modifies the module, and if so, return true.
137 bool run(Function &F);
138
139 /// doInitialization - Run all of the initializers for the function passes.
140 ///
141 bool doInitialization(Module &M);
142
Dan Gohmane6656eb2007-07-30 14:51:13 +0000143 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000144 ///
145 bool doFinalization(Module &M);
146
147 /// Pass Manager itself does not invalidate any analysis info.
148 void getAnalysisUsage(AnalysisUsage &Info) const {
149 Info.setPreservesAll();
150 }
151
152 inline void addTopLevelPass(Pass *P) {
153
154 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
155
156 // P is a immutable pass and it will be managed by this
157 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000158 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000159 P->setResolver(AR);
160 initializeAnalysisImpl(P);
161 addImmutablePass(IP);
162 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000163 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000164 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000165 }
Devang Patel0f080042007-01-12 17:23:48 +0000166
Devang Patel67d6a5e2006-12-19 19:46:59 +0000167 }
168
169 FPPassManager *getContainedManager(unsigned N) {
170 assert ( N < PassManagers.size() && "Pass number out of range!");
171 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
172 return FP;
173 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000174};
175
Devang Patel8c78a0b2007-05-03 01:11:54 +0000176char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000177//===----------------------------------------------------------------------===//
178// MPPassManager
179//
180/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000181/// It batches all Module passes passes and function pass managers together and
182/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000183class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000184
185public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000186 static char ID;
Devang Patel09f162c2007-05-01 21:15:47 +0000187 MPPassManager(int Depth) : Pass((intptr_t)&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000188
189 // Delete on the fly managers.
190 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000191 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000192 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
193 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000194 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000195 delete FPP;
196 }
197 }
198
Devang Patelca58e352006-11-08 10:05:38 +0000199 /// run - Execute all of the passes scheduled for execution. Keep track of
200 /// whether any of the passes modifies the module, and if so, return true.
201 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000202
Devang Patelf9d96b92006-12-07 19:57:52 +0000203 /// Pass Manager itself does not invalidate any analysis info.
204 void getAnalysisUsage(AnalysisUsage &Info) const {
205 Info.setPreservesAll();
206 }
207
Devang Patele64d3052007-04-16 20:12:57 +0000208 /// Add RequiredPass into list of lower level passes required by pass P.
209 /// RequiredPass is run on the fly by Pass Manager when P requests it
210 /// through getAnalysis interface.
211 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
212
Devang Patel69e9f6d2007-04-16 20:27:05 +0000213 /// Return function pass corresponding to PassInfo PI, that is
214 /// required by module pass MP. Instantiate analysis pass, by using
215 /// its runOnFunction() for function F.
216 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
217
Devang Patele3858e62007-02-01 22:08:25 +0000218 virtual const char *getPassName() const {
219 return "Module Pass Manager";
220 }
221
Devang Pateleda56172006-12-12 23:34:33 +0000222 // Print passes managed by this manager
223 void dumpPassStructure(unsigned Offset) {
224 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000225 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
226 ModulePass *MP = getContainedPass(Index);
227 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000228 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000229 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000230 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000231 }
232 }
233
Devang Patelabfbe3b2006-12-16 00:56:26 +0000234 ModulePass *getContainedPass(unsigned N) {
235 assert ( N < PassVector.size() && "Pass number out of range!");
236 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
237 return MP;
238 }
239
Devang Patel28349ab2007-02-27 15:00:39 +0000240 virtual PassManagerType getPassManagerType() const {
241 return PMT_ModulePassManager;
242 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000243
244 private:
245 /// Collection of on the fly FPPassManagers. These managers manage
246 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000247 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000248};
249
Devang Patel8c78a0b2007-05-03 01:11:54 +0000250char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000251//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000252// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000253//
Devang Patel09f162c2007-05-01 21:15:47 +0000254
Devang Patel67d6a5e2006-12-19 19:46:59 +0000255/// PassManagerImpl manages MPPassManagers
256class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000257 public PMDataManager,
258 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000259
260public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000261 static char ID;
Devang Patel09f162c2007-05-01 21:15:47 +0000262 PassManagerImpl(int Depth) : Pass((intptr_t)&ID), PMDataManager(Depth),
Devang Patel4268fc02007-01-16 02:00:38 +0000263 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000264
Devang Patel376fefa2006-11-08 10:29:57 +0000265 /// add - Add a pass to the queue of passes to run. This passes ownership of
266 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
267 /// will be destroyed as well, so there is no need to delete the pass. This
268 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000269 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000270 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000271 }
Devang Patel376fefa2006-11-08 10:29:57 +0000272
273 /// run - Execute all of the passes scheduled for execution. Keep track of
274 /// whether any of the passes modifies the module, and if so, return true.
275 bool run(Module &M);
276
Devang Patelf9d96b92006-12-07 19:57:52 +0000277 /// Pass Manager itself does not invalidate any analysis info.
278 void getAnalysisUsage(AnalysisUsage &Info) const {
279 Info.setPreservesAll();
280 }
281
Devang Patelabcd1d32006-12-07 21:27:23 +0000282 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000283
Devang Patelfa971cd2006-12-08 23:57:43 +0000284 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000285
286 // P is a immutable pass and it will be managed by this
287 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000288 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000289 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000290 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000291 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000292 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000293 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000294 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000295 }
Devang Patel0f080042007-01-12 17:23:48 +0000296
Devang Patelabcd1d32006-12-07 21:27:23 +0000297 }
298
Devang Patel67d6a5e2006-12-19 19:46:59 +0000299 MPPassManager *getContainedManager(unsigned N) {
300 assert ( N < PassManagers.size() && "Pass number out of range!");
301 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
302 return MP;
303 }
304
Devang Patel376fefa2006-11-08 10:29:57 +0000305};
306
Devang Patel8c78a0b2007-05-03 01:11:54 +0000307char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000308} // End of llvm namespace
309
310namespace {
311
312//===----------------------------------------------------------------------===//
313// TimingInfo Class - This class is used to calculate information about the
314// amount of time each pass takes to execute. This only happens when
315// -time-passes is enabled on the command line.
316//
317
318class VISIBILITY_HIDDEN TimingInfo {
319 std::map<Pass*, Timer> TimingData;
320 TimerGroup TG;
321
322public:
323 // Use 'create' member to get this.
324 TimingInfo() : TG("... Pass execution timing report ...") {}
325
326 // TimingDtor - Print out information about timing information
327 ~TimingInfo() {
328 // Delete all of the timers...
329 TimingData.clear();
330 // TimerGroup is deleted next, printing the report.
331 }
332
333 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
334 // to a non null value (if the -time-passes option is enabled) or it leaves it
335 // null. It may be called multiple times.
336 static void createTheTimeInfo();
337
338 void passStarted(Pass *P) {
339
340 if (dynamic_cast<PMDataManager *>(P))
341 return;
342
343 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
344 if (I == TimingData.end())
345 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
346 I->second.startTimer();
347 }
348 void passEnded(Pass *P) {
349
350 if (dynamic_cast<PMDataManager *>(P))
351 return;
352
353 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
354 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
355 I->second.stopTimer();
356 }
357};
358
Devang Patelb8817b92006-12-14 00:59:42 +0000359static TimingInfo *TheTimeInfo;
360
Devang Patel1c3633e2007-01-29 23:10:37 +0000361} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000362
Devang Patela1514cb2006-12-07 19:39:39 +0000363//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000364// PMTopLevelManager implementation
365
Devang Patel4268fc02007-01-16 02:00:38 +0000366/// Initialize top level manager. Create first pass manager.
367PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
368
369 if (t == TLM_Pass) {
370 MPPassManager *MPP = new MPPassManager(1);
371 MPP->setTopLevelManager(this);
372 addPassManager(MPP);
373 activeStack.push(MPP);
374 }
375 else if (t == TLM_Function) {
376 FPPassManager *FPP = new FPPassManager(1);
377 FPP->setTopLevelManager(this);
378 addPassManager(FPP);
379 activeStack.push(FPP);
380 }
381}
382
Devang Patelafb1f3622006-12-12 22:35:25 +0000383/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000384void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000385 Pass *P) {
386
Devang Patel8adae862007-07-20 18:04:54 +0000387 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000388 E = AnalysisPasses.end(); I != E; ++I) {
389 Pass *AP = *I;
390 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000391
392 if (P == AP)
393 continue;
394
Devang Patelafb1f3622006-12-12 22:35:25 +0000395 // If AP is the last user of other passes then make P last user of
396 // such passes.
397 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
398 LUE = LastUser.end(); LUI != LUE; ++LUI) {
399 if (LUI->second == AP)
400 LastUser[LUI->first] = P;
401 }
402 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000403}
404
405/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000406void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000407 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000408 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
409 LUE = LastUser.end(); LUI != LUE; ++LUI)
410 if (LUI->second == P)
411 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000412}
413
414/// Schedule pass P for execution. Make sure that passes required by
415/// P are run before P is run. Update analysis info maintained by
416/// the manager. Remove dead passes. This is a recursive function.
417void PMTopLevelManager::schedulePass(Pass *P) {
418
Devang Patel3312f752007-01-16 21:43:18 +0000419 // TODO : Allocate function manager for this pass, other wise required set
420 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000421
Devang Pateld74ede72007-03-06 01:06:16 +0000422 // Give pass a chance to prepare the stage.
423 P->preparePassManager(activeStack);
424
Devang Patelafb1f3622006-12-12 22:35:25 +0000425 AnalysisUsage AnUsage;
426 P->getAnalysisUsage(AnUsage);
427 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
428 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
429 E = RequiredSet.end(); I != E; ++I) {
430
431 Pass *AnalysisPass = findAnalysisPass(*I);
432 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000433 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000434 // Schedule this analysis run first only if it is not a lower level
435 // analysis pass. Lower level analsyis passes are run on the fly.
436 if (P->getPotentialPassManagerType () >=
437 AnalysisPass->getPotentialPassManagerType())
438 schedulePass(AnalysisPass);
439 else
440 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000441 }
442 }
443
444 // Now all required passes are available.
445 addTopLevelPass(P);
446}
447
448/// Find the pass that implements Analysis AID. Search immutable
449/// passes and all pass managers. If desired pass is not found
450/// then return NULL.
451Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
452
453 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000454 // Check pass managers
455 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
456 E = PassManagers.end(); P == NULL && I != E; ++I) {
457 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
458 assert(PMD && "This is not a PassManager");
459 P = PMD->findAnalysisPass(AID, false);
460 }
461
462 // Check other pass managers
463 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
464 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
465 P = (*I)->findAnalysisPass(AID, false);
466
Devang Patelafb1f3622006-12-12 22:35:25 +0000467 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
468 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
469 const PassInfo *PI = (*I)->getPassInfo();
470 if (PI == AID)
471 P = *I;
472
473 // If Pass not found then check the interfaces implemented by Immutable Pass
474 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000475 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
476 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
477 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000478 }
479 }
480
Devang Patelafb1f3622006-12-12 22:35:25 +0000481 return P;
482}
483
Devang Pateleda56172006-12-12 23:34:33 +0000484// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000485void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000486
Devang Patelfd4184322007-01-17 20:33:36 +0000487 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000488 return;
489
Devang Pateleda56172006-12-12 23:34:33 +0000490 // Print out the immutable passes
491 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
492 ImmutablePasses[i]->dumpPassStructure(0);
493 }
494
Devang Patel991aeba2006-12-15 20:13:01 +0000495 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000496 E = PassManagers.end(); I != E; ++I)
497 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000498}
499
Devang Patel991aeba2006-12-15 20:13:01 +0000500void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000501
Devang Patelfd4184322007-01-17 20:33:36 +0000502 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000503 return;
504
505 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000506 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000507 E = PassManagers.end(); I != E; ++I) {
508 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
509 assert(PMD && "This is not a PassManager");
510 PMD->dumpPassArguments();
511 }
512 cerr << "\n";
513}
514
Devang Patele3068402006-12-21 00:16:50 +0000515void PMTopLevelManager::initializeAllAnalysisInfo() {
516
517 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
518 E = PassManagers.end(); I != E; ++I) {
519 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
520 assert(PMD && "This is not a PassManager");
521 PMD->initializeAnalysisInfo();
522 }
523
524 // Initailize other pass managers
525 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
526 E = IndirectPassManagers.end(); I != E; ++I)
527 (*I)->initializeAnalysisInfo();
528}
529
Devang Patele7599552007-01-12 18:52:44 +0000530/// Destructor
531PMTopLevelManager::~PMTopLevelManager() {
532 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
533 E = PassManagers.end(); I != E; ++I)
534 delete *I;
535
536 for (std::vector<ImmutablePass *>::iterator
537 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
538 delete *I;
539
540 PassManagers.clear();
541}
542
Devang Patelafb1f3622006-12-12 22:35:25 +0000543//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000544// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000545
Devang Pateld65e9e92006-11-08 01:31:28 +0000546/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000547/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000548bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000549
Devang Patel8f677ce2006-12-07 18:47:25 +0000550 // TODO
551 // If this pass is not preserving information that is required by a
552 // pass maintained by higher level pass manager then do not insert
553 // this pass into current manager. Use new manager. For example,
554 // For example, If FunctionPass F is not preserving ModulePass Info M1
555 // that is used by another ModulePass M2 then do not insert F in
556 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000557 return true;
558}
559
Devang Patel643676c2006-11-11 01:10:19 +0000560/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000561void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000562
Devang Patel643676c2006-11-11 01:10:19 +0000563 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000564 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000565
Devang Patele9976aa2006-12-07 19:33:53 +0000566 //This pass is the current implementation of all of the interfaces it
567 //implements as well.
568 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
569 for (unsigned i = 0, e = II.size(); i != e; ++i)
570 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000571 }
572}
573
Devang Patel9d9fc902007-03-06 17:52:53 +0000574// Return true if P preserves high level analysis used by other
575// passes managed by this manager
576bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
577
578 AnalysisUsage AnUsage;
579 P->getAnalysisUsage(AnUsage);
580
581 if (AnUsage.getPreservesAll())
582 return true;
583
584 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
585 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
586 E = HigherLevelAnalysis.end(); I != E; ++I) {
587 Pass *P1 = *I;
Devang Patel01919d22007-03-08 19:05:01 +0000588 if (!dynamic_cast<ImmutablePass*>(P1)
589 && std::find(PreservedSet.begin(), PreservedSet.end(), P1->getPassInfo()) ==
590 PreservedSet.end())
591 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000592 }
593
594 return true;
595}
596
Devang Patela273d1c2007-07-19 18:02:32 +0000597/// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
598void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000599 AnalysisUsage AnUsage;
600 P->getAnalysisUsage(AnUsage);
Devang Patelef432532007-07-19 05:36:09 +0000601 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000602
Devang Patelef432532007-07-19 05:36:09 +0000603 // Verify preserved analysis
Devang Patela273d1c2007-07-19 18:02:32 +0000604 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
605 E = PreservedSet.end(); I != E; ++I) {
606 AnalysisID AID = *I;
607 Pass *AP = findAnalysisPass(AID, true);
608 if (AP)
609 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000610 }
Devang Patela273d1c2007-07-19 18:02:32 +0000611}
612
613/// Remove Analyss not preserved by Pass P
614void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
615 AnalysisUsage AnUsage;
616 P->getAnalysisUsage(AnUsage);
Devang Patel2e169c32006-12-07 20:03:49 +0000617 if (AnUsage.getPreservesAll())
618 return;
619
Devang Patela273d1c2007-07-19 18:02:32 +0000620 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000621 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000622 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000623 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000624 if (!dynamic_cast<ImmutablePass*>(Info->second)
625 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
626 PreservedSet.end())
Devang Patel349170f2006-11-11 01:24:55 +0000627 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000628 AvailableAnalysis.erase(Info);
Devang Patel349170f2006-11-11 01:24:55 +0000629 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000630
631 // Check inherited analysis also. If P is not preserving analysis
632 // provided by parent manager then remove it here.
633 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
634
635 if (!InheritedAnalysis[Index])
636 continue;
637
638 for (std::map<AnalysisID, Pass*>::iterator
639 I = InheritedAnalysis[Index]->begin(),
640 E = InheritedAnalysis[Index]->end(); I != E; ) {
641 std::map<AnalysisID, Pass *>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000642 if (!dynamic_cast<ImmutablePass*>(Info->second)
643 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
644 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000645 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000646 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000647 }
648 }
649
Devang Patelf68a3492006-11-07 22:35:17 +0000650}
651
Devang Patelca189262006-11-14 03:05:08 +0000652/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000653void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000654 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000655
Devang Patel8adae862007-07-20 18:04:54 +0000656 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000657
Devang Patel2ff44922007-04-16 20:39:59 +0000658 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000659 if (!TPM)
660 return;
661
Devang Patel17ad0962006-12-08 00:37:52 +0000662 TPM->collectLastUses(DeadPasses, P);
663
Devang Patel8adae862007-07-20 18:04:54 +0000664 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000665 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000666
Devang Patel003a5592007-03-05 20:01:30 +0000667 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000668
Devang Patel7ebf09d2007-03-05 18:20:51 +0000669 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000670 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000671 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000672
Devang Patel17ad0962006-12-08 00:37:52 +0000673 std::map<AnalysisID, Pass*>::iterator Pos =
674 AvailableAnalysis.find((*I)->getPassInfo());
675
Devang Patel475c4532006-12-08 00:59:05 +0000676 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000677 if (Pos != AvailableAnalysis.end())
678 AvailableAnalysis.erase(Pos);
679 }
Devang Patelca189262006-11-14 03:05:08 +0000680}
681
Devang Patel8f677ce2006-12-07 18:47:25 +0000682/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000683/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000684void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000685 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000686
Devang Pateld440cd92006-12-08 23:53:00 +0000687 // This manager is going to manage pass P. Set up analysis resolver
688 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000689 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000690 P->setResolver(AR);
691
Devang Patelec2b9a72007-03-05 22:57:49 +0000692 // If a FunctionPass F is the last user of ModulePass info M
693 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000694 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000695
Devang Patel90b05e02006-11-11 02:04:19 +0000696 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000697
698 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000699 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000700 SmallVector<Pass *, 8> RequiredPasses;
701 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
702
Devang Patelbc03f132006-12-07 23:55:10 +0000703 unsigned PDepth = this->getDepth();
704
Devang Patele64d3052007-04-16 20:12:57 +0000705 collectRequiredAnalysis(RequiredPasses,
706 ReqAnalysisNotAvailable, P);
707 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000708 E = RequiredPasses.end(); I != E; ++I) {
709 Pass *PRequired = *I;
710 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000711
712 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
713 RDepth = DM.getDepth();
714
Devang Patelbc03f132006-12-07 23:55:10 +0000715 if (PDepth == RDepth)
716 LastUses.push_back(PRequired);
717 else if (PDepth > RDepth) {
718 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000719 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000720 // Keep track of higher level analysis used by this manager.
721 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000722 } else
723 assert (0 && "Unable to accomodate Required Pass");
724 }
725
Devang Patel39786a92007-01-15 20:31:54 +0000726 // Set P as P's last user until someone starts using P.
727 // However, if P is a Pass Manager then it does not need
728 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000729 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000730 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000731 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000732
Devang Patelec2b9a72007-03-05 22:57:49 +0000733 if (!TransferLastUses.empty()) {
734 Pass *My_PM = dynamic_cast<Pass *>(this);
735 TPM->setLastUser(TransferLastUses, My_PM);
736 TransferLastUses.clear();
737 }
738
Devang Patel69e9f6d2007-04-16 20:27:05 +0000739 // Now, take care of required analysises that are not available.
740 for (SmallVector<AnalysisID, 8>::iterator
741 I = ReqAnalysisNotAvailable.begin(),
742 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
743 Pass *AnalysisPass = (*I)->createPass();
744 this->addLowerLevelRequiredPass(P, AnalysisPass);
745 }
746
Devang Patel17bff0d2006-12-07 22:09:36 +0000747 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000748 // Remove the analysis not preserved by this pass
749 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000750 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000751 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000752
753 // Add pass
754 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000755}
756
Devang Patele64d3052007-04-16 20:12:57 +0000757
758/// Populate RP with analysis pass that are required by
759/// pass P and are available. Populate RP_NotAvail with analysis
760/// pass that are required by pass P but are not available.
761void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
762 SmallVector<AnalysisID, 8> &RP_NotAvail,
763 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000764 AnalysisUsage AnUsage;
765 P->getAnalysisUsage(AnUsage);
766 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
767 for (std::vector<AnalysisID>::const_iterator
768 I = RequiredSet.begin(), E = RequiredSet.end();
769 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000770 AnalysisID AID = *I;
771 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
772 RP.push_back(AnalysisPass);
773 else
774 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000775 }
Devang Patelf58183d2006-12-12 23:09:32 +0000776
777 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
778 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
779 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000780 AnalysisID AID = *I;
781 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
782 RP.push_back(AnalysisPass);
783 else
784 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000785 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000786}
787
Devang Patel07f4f582006-11-14 21:49:36 +0000788// All Required analyses should be available to the pass as it runs! Here
789// we fill in the AnalysisImpls member of the pass so that it can
790// successfully use the getAnalysis() method to retrieve the
791// implementations it needs.
792//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000793void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000794 AnalysisUsage AnUsage;
795 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000796
797 for (std::vector<const PassInfo *>::const_iterator
798 I = AnUsage.getRequiredSet().begin(),
799 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000800 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000801 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000802 // This may be analysis pass that is initialized on the fly.
803 // If that is not the case then it will raise an assert when it is used.
804 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000805 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000806 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000807 }
808}
809
Devang Patel640c5bb2006-12-08 22:30:11 +0000810/// Find the pass that implements Analysis AID. If desired pass is not found
811/// then return NULL.
812Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
813
814 // Check if AvailableAnalysis map has one entry.
815 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
816
817 if (I != AvailableAnalysis.end())
818 return I->second;
819
820 // Search Parents through TopLevelManager
821 if (SearchParent)
822 return TPM->findAnalysisPass(AID);
823
Devang Patel9d759b82006-12-09 00:09:12 +0000824 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000825}
826
Devang Patel991aeba2006-12-15 20:13:01 +0000827// Print list of passes that are last used by P.
828void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
829
Devang Patel8adae862007-07-20 18:04:54 +0000830 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000831
832 // If this is a on the fly manager then it does not have TPM.
833 if (!TPM)
834 return;
835
Devang Patel991aeba2006-12-15 20:13:01 +0000836 TPM->collectLastUses(LUses, P);
837
Devang Patel8adae862007-07-20 18:04:54 +0000838 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000839 E = LUses.end(); I != E; ++I) {
840 llvm::cerr << "--" << std::string(Offset*2, ' ');
841 (*I)->dumpPassStructure(0);
842 }
843}
844
845void PMDataManager::dumpPassArguments() const {
846 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
847 E = PassVector.end(); I != E; ++I) {
848 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
849 PMD->dumpPassArguments();
850 else
851 if (const PassInfo *PI = (*I)->getPassInfo())
852 if (!PI->isAnalysisGroup())
853 cerr << " -" << PI->getPassArgument();
854 }
855}
856
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000857void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
858 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000859 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000860 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000861 return;
862 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000863 switch (S1) {
864 case EXECUTION_MSG:
865 cerr << "Executing Pass '" << P->getPassName();
866 break;
867 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000868 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000869 break;
870 case FREEING_MSG:
871 cerr << " Freeing Pass '" << P->getPassName();
872 break;
873 default:
874 break;
875 }
876 switch (S2) {
877 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000878 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000879 break;
880 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000881 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000882 break;
883 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000884 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000885 break;
886 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000887 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000888 break;
889 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000890 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000891 break;
892 default:
893 break;
894 }
Devang Patel991aeba2006-12-15 20:13:01 +0000895}
896
897void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
898 const std::vector<AnalysisID> &Set)
899 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000900 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000901 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
902 for (unsigned i = 0; i != Set.size(); ++i) {
903 if (i) cerr << ",";
904 cerr << " " << Set[i]->getPassName();
905 }
906 cerr << "\n";
907 }
908}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000909
Devang Patel004937b2007-07-27 20:06:09 +0000910/// Add RequiredPass into list of lower level passes required by pass P.
911/// RequiredPass is run on the fly by Pass Manager when P requests it
912/// through getAnalysis interface.
913/// This should be handled by specific pass manager.
914void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
915 if (TPM) {
916 TPM->dumpArguments();
917 TPM->dumpPasses();
918 }
919 assert (0 && "Unable to handle Pass that requires lower level Analysis pass");
920}
921
Devang Patele7599552007-01-12 18:52:44 +0000922// Destructor
923PMDataManager::~PMDataManager() {
924
925 for (std::vector<Pass *>::iterator I = PassVector.begin(),
926 E = PassVector.end(); I != E; ++I)
927 delete *I;
928
929 PassVector.clear();
930}
931
Devang Patel9bdf7d42006-12-08 23:28:54 +0000932//===----------------------------------------------------------------------===//
933// NOTE: Is this the right place to define this method ?
934// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000935Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000936 return PM.findAnalysisPass(ID, dir);
937}
938
Devang Patel92942812007-04-16 20:56:24 +0000939Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
940 Function &F) {
941 return PM.getOnTheFlyPass(P, AnalysisPI, F);
942}
943
Devang Patela1514cb2006-12-07 19:39:39 +0000944//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000945// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000946
Devang Patel6e5a1132006-11-07 21:31:57 +0000947/// Execute all of the passes scheduled for execution by invoking
948/// runOnBasicBlock method. Keep track of whether any of the passes modifies
949/// the function, and if so, return true.
950bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000951BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000952
Reid Spencer5301e7c2007-01-30 20:08:39 +0000953 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000954 return false;
955
Devang Patele9585592006-12-08 01:38:28 +0000956 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000957
Devang Patel6e5a1132006-11-07 21:31:57 +0000958 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000959 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
960 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000961 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000962 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000963
Devang Pateld305c402007-08-10 18:29:32 +0000964 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000965 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000966
Devang Patelabfbe3b2006-12-16 00:56:26 +0000967 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000968
Devang Patelabfbe3b2006-12-16 00:56:26 +0000969 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000970 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000971 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000972
Devang Patel003a5592007-03-05 20:01:30 +0000973 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +0000974 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000975 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000976
Devang Patela273d1c2007-07-19 18:02:32 +0000977 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000978 removeNotPreservedAnalysis(BP);
979 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +0000980 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +0000981 }
Chris Lattnerde2aa652007-08-10 06:22:25 +0000982
Devang Patel56d48ec2006-12-15 22:57:49 +0000983 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000984}
985
Devang Patel475c4532006-12-08 00:59:05 +0000986// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000987inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000988 bool Changed = false;
989
Devang Patelabfbe3b2006-12-16 00:56:26 +0000990 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
991 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000992 Changed |= BP->doInitialization(M);
993 }
994
995 return Changed;
996}
997
Devang Patel67d6a5e2006-12-19 19:46:59 +0000998inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000999 bool Changed = false;
1000
Devang Patelabfbe3b2006-12-16 00:56:26 +00001001 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1002 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001003 Changed |= BP->doFinalization(M);
1004 }
1005
1006 return Changed;
1007}
1008
Devang Patel67d6a5e2006-12-19 19:46:59 +00001009inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001010 bool Changed = false;
1011
Devang Patelabfbe3b2006-12-16 00:56:26 +00001012 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1013 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001014 Changed |= BP->doInitialization(F);
1015 }
1016
1017 return Changed;
1018}
1019
Devang Patel67d6a5e2006-12-19 19:46:59 +00001020inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001021 bool Changed = false;
1022
Devang Patelabfbe3b2006-12-16 00:56:26 +00001023 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1024 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001025 Changed |= BP->doFinalization(F);
1026 }
1027
1028 return Changed;
1029}
1030
1031
Devang Patela1514cb2006-12-07 19:39:39 +00001032//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001033// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001034
Devang Patel4e12f862006-11-08 10:44:40 +00001035/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001036FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001037 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001038 // FPM is the top level manager.
1039 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001040
1041 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +00001042 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +00001043 FPM->setResolver(AR);
1044
Devang Patel1f653682006-12-08 18:57:16 +00001045 MP = P;
1046}
1047
Devang Patelb67904d2006-12-13 02:36:01 +00001048FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001049 delete FPM;
1050}
1051
Devang Patel4e12f862006-11-08 10:44:40 +00001052/// add - Add a pass to the queue of passes to run. This passes
1053/// ownership of the Pass to the PassManager. When the
1054/// PassManager_X is destroyed, the pass will be destroyed as well, so
1055/// there is no need to delete the pass. (TODO delete passes.)
1056/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001057void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001058 FPM->add(P);
1059}
1060
Devang Patel9f3083e2006-11-15 19:39:54 +00001061/// run - Execute all of the passes scheduled for execution. Keep
1062/// track of whether any of the passes modifies the function, and if
1063/// so, return true.
1064///
Devang Patelb67904d2006-12-13 02:36:01 +00001065bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001066 std::string errstr;
1067 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001068 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001069 abort();
1070 }
Devang Patel272908d2006-12-08 22:57:48 +00001071 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001072}
1073
1074
Devang Patelff631ae2006-11-15 01:27:05 +00001075/// doInitialization - Run all of the initializers for the function passes.
1076///
Devang Patelb67904d2006-12-13 02:36:01 +00001077bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001078 return FPM->doInitialization(*MP->getModule());
1079}
1080
Dan Gohmane6656eb2007-07-30 14:51:13 +00001081/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001082///
Devang Patelb67904d2006-12-13 02:36:01 +00001083bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001084 return FPM->doFinalization(*MP->getModule());
1085}
1086
Devang Patela1514cb2006-12-07 19:39:39 +00001087//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001088// FunctionPassManagerImpl implementation
1089//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001090inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1091 bool Changed = false;
1092
1093 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1094 FPPassManager *FP = getContainedManager(Index);
1095 Changed |= FP->doInitialization(M);
1096 }
1097
1098 return Changed;
1099}
1100
1101inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1102 bool Changed = false;
1103
1104 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1105 FPPassManager *FP = getContainedManager(Index);
1106 Changed |= FP->doFinalization(M);
1107 }
1108
1109 return Changed;
1110}
1111
1112// Execute all the passes managed by this top level manager.
1113// Return true if any function is modified by a pass.
1114bool FunctionPassManagerImpl::run(Function &F) {
1115
1116 bool Changed = false;
1117
1118 TimingInfo::createTheTimeInfo();
1119
1120 dumpArguments();
1121 dumpPasses();
1122
Devang Patele3068402006-12-21 00:16:50 +00001123 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001124 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1125 FPPassManager *FP = getContainedManager(Index);
1126 Changed |= FP->runOnFunction(F);
1127 }
1128 return Changed;
1129}
1130
1131//===----------------------------------------------------------------------===//
1132// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001133
Devang Patel8c78a0b2007-05-03 01:11:54 +00001134char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001135/// Print passes managed by this manager
1136void FPPassManager::dumpPassStructure(unsigned Offset) {
1137 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1138 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1139 FunctionPass *FP = getContainedPass(Index);
1140 FP->dumpPassStructure(Offset + 1);
1141 dumpLastUses(FP, Offset+1);
1142 }
1143}
1144
1145
Devang Patel0c2012f2006-11-07 21:49:50 +00001146/// Execute all of the passes scheduled for execution by invoking
1147/// runOnFunction method. Keep track of whether any of the passes modifies
1148/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001149bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001150
1151 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001152
Reid Spencer5301e7c2007-01-30 20:08:39 +00001153 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001154 return false;
1155
Devang Patelabfbe3b2006-12-16 00:56:26 +00001156 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1157 FunctionPass *FP = getContainedPass(Index);
1158
Devang Patelf6d1d212006-12-14 00:25:06 +00001159 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001160 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001161
Devang Pateld305c402007-08-10 18:29:32 +00001162 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001163 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001164
Devang Patelabfbe3b2006-12-16 00:56:26 +00001165 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001166
Devang Patelabfbe3b2006-12-16 00:56:26 +00001167 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001168 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001169 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001170
Devang Patel003a5592007-03-05 20:01:30 +00001171 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001172 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001173 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001174
Devang Patela273d1c2007-07-19 18:02:32 +00001175 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001176 removeNotPreservedAnalysis(FP);
1177 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001178 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001179 }
1180 return Changed;
1181}
1182
Devang Patel67d6a5e2006-12-19 19:46:59 +00001183bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001184
Devang Patel67d6a5e2006-12-19 19:46:59 +00001185 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001186
1187 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1188 this->runOnFunction(*I);
1189
1190 return Changed |= doFinalization(M);
1191}
1192
1193inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001194 bool Changed = false;
1195
Devang Patelabfbe3b2006-12-16 00:56:26 +00001196 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1197 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001198 Changed |= FP->doInitialization(M);
1199 }
1200
1201 return Changed;
1202}
1203
Devang Patel67d6a5e2006-12-19 19:46:59 +00001204inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001205 bool Changed = false;
1206
Devang Patelabfbe3b2006-12-16 00:56:26 +00001207 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1208 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001209 Changed |= FP->doFinalization(M);
1210 }
1211
Devang Patelff631ae2006-11-15 01:27:05 +00001212 return Changed;
1213}
1214
Devang Patela1514cb2006-12-07 19:39:39 +00001215//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001216// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001217
Devang Patel05e1a972006-11-07 22:03:15 +00001218/// Execute all of the passes scheduled for execution by invoking
1219/// runOnModule method. Keep track of whether any of the passes modifies
1220/// the module, and if so, return true.
1221bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001222MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001223 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001224
Devang Patelabfbe3b2006-12-16 00:56:26 +00001225 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1226 ModulePass *MP = getContainedPass(Index);
1227
Devang Patelf6d1d212006-12-14 00:25:06 +00001228 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001229 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001230
Devang Pateld305c402007-08-10 18:29:32 +00001231 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001232 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001233
Devang Patelabfbe3b2006-12-16 00:56:26 +00001234 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001235
Devang Patelabfbe3b2006-12-16 00:56:26 +00001236 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001237 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001238 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001239
Devang Patel003a5592007-03-05 20:01:30 +00001240 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001241 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001242 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001243
Devang Patela273d1c2007-07-19 18:02:32 +00001244 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001245 removeNotPreservedAnalysis(MP);
1246 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001247 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001248 }
1249 return Changed;
1250}
1251
Devang Patele64d3052007-04-16 20:12:57 +00001252/// Add RequiredPass into list of lower level passes required by pass P.
1253/// RequiredPass is run on the fly by Pass Manager when P requests it
1254/// through getAnalysis interface.
1255void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1256
1257 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1258 && "Unable to handle Pass that requires lower level Analysis pass");
1259 assert ((P->getPotentialPassManagerType() <
1260 RequiredPass->getPotentialPassManagerType())
1261 && "Unable to handle Pass that requires lower level Analysis pass");
1262
Devang Patel68f72b12007-04-26 17:50:19 +00001263 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001264 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001265 FPP = new FunctionPassManagerImpl(0);
1266 // FPP is the top level manager.
1267 FPP->setTopLevelManager(FPP);
1268
Devang Patel69e9f6d2007-04-16 20:27:05 +00001269 OnTheFlyManagers[P] = FPP;
1270 }
Devang Patel68f72b12007-04-26 17:50:19 +00001271 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001272
Devang Patel68f72b12007-04-26 17:50:19 +00001273 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001274 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001275 LU.push_back(RequiredPass);
1276 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001277}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001278
1279/// Return function pass corresponding to PassInfo PI, that is
1280/// required by module pass MP. Instantiate analysis pass, by using
1281/// its runOnFunction() for function F.
1282Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1283 Function &F) {
1284 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001285 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001286 assert (FPP && "Unable to find on the fly pass");
1287
Devang Patel68f72b12007-04-26 17:50:19 +00001288 FPP->run(F);
1289 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001290}
1291
1292
Devang Patela1514cb2006-12-07 19:39:39 +00001293//===----------------------------------------------------------------------===//
1294// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001295//
Devang Patelc290c8a2006-11-07 22:23:34 +00001296/// run - Execute all of the passes scheduled for execution. Keep track of
1297/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001298bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001299
Devang Patelc290c8a2006-11-07 22:23:34 +00001300 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001301
Devang Patelb8817b92006-12-14 00:59:42 +00001302 TimingInfo::createTheTimeInfo();
1303
Devang Patelcfd70c42006-12-13 22:10:00 +00001304 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001305 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001306
Devang Patele3068402006-12-21 00:16:50 +00001307 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001308 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1309 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001310 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001311 }
1312 return Changed;
1313}
Devang Patel376fefa2006-11-08 10:29:57 +00001314
Devang Patela1514cb2006-12-07 19:39:39 +00001315//===----------------------------------------------------------------------===//
1316// PassManager implementation
1317
Devang Patel376fefa2006-11-08 10:29:57 +00001318/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001319PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001320 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001321 // PM is the top level manager
1322 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001323}
1324
Devang Patelb67904d2006-12-13 02:36:01 +00001325PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001326 delete PM;
1327}
1328
Devang Patel376fefa2006-11-08 10:29:57 +00001329/// add - Add a pass to the queue of passes to run. This passes ownership of
1330/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1331/// will be destroyed as well, so there is no need to delete the pass. This
1332/// implies that all passes MUST be allocated with 'new'.
1333void
Devang Patelb67904d2006-12-13 02:36:01 +00001334PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001335 PM->add(P);
1336}
1337
1338/// run - Execute all of the passes scheduled for execution. Keep track of
1339/// whether any of the passes modifies the module, and if so, return true.
1340bool
Devang Patelb67904d2006-12-13 02:36:01 +00001341PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001342 return PM->run(M);
1343}
1344
Devang Patelb8817b92006-12-14 00:59:42 +00001345//===----------------------------------------------------------------------===//
1346// TimingInfo Class - This class is used to calculate information about the
1347// amount of time each pass takes to execute. This only happens with
1348// -time-passes is enabled on the command line.
1349//
1350bool llvm::TimePassesIsEnabled = false;
1351static cl::opt<bool,true>
1352EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1353 cl::desc("Time each pass, printing elapsed time for each on exit"));
1354
1355// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1356// a non null value (if the -time-passes option is enabled) or it leaves it
1357// null. It may be called multiple times.
1358void TimingInfo::createTheTimeInfo() {
1359 if (!TimePassesIsEnabled || TheTimeInfo) return;
1360
1361 // Constructed the first time this is called, iff -time-passes is enabled.
1362 // This guarantees that the object will be constructed before static globals,
1363 // thus it will be destroyed before them.
1364 static ManagedStatic<TimingInfo> TTI;
1365 TheTimeInfo = &*TTI;
1366}
1367
Devang Patel1c3633e2007-01-29 23:10:37 +00001368/// If TimingInfo is enabled then start pass timer.
1369void StartPassTimer(Pass *P) {
1370 if (TheTimeInfo)
1371 TheTimeInfo->passStarted(P);
1372}
1373
1374/// If TimingInfo is enabled then stop pass timer.
1375void StopPassTimer(Pass *P) {
1376 if (TheTimeInfo)
1377 TheTimeInfo->passEnded(P);
1378}
1379
Devang Patel1c56a632007-01-08 19:29:38 +00001380//===----------------------------------------------------------------------===//
1381// PMStack implementation
1382//
Devang Patelad98d232007-01-11 22:15:30 +00001383
Devang Patel1c56a632007-01-08 19:29:38 +00001384// Pop Pass Manager from the stack and clear its analysis info.
1385void PMStack::pop() {
1386
1387 PMDataManager *Top = this->top();
1388 Top->initializeAnalysisInfo();
1389
1390 S.pop_back();
1391}
1392
1393// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001394void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001395
Devang Patel15701b52007-01-11 00:19:00 +00001396 PMDataManager *Top = NULL;
1397 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1398 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001399
Devang Patel15701b52007-01-11 00:19:00 +00001400 if (this->empty()) {
1401 Top = PM;
1402 }
1403 else {
1404 Top = this->top();
1405 PMTopLevelManager *TPM = Top->getTopLevelManager();
1406
1407 assert (TPM && "Unable to find top level manager");
1408 TPM->addIndirectPassManager(PM);
1409 PM->setTopLevelManager(TPM);
1410 }
1411
Devang Patel15701b52007-01-11 00:19:00 +00001412 S.push_back(PM);
1413}
1414
1415// Dump content of the pass manager stack.
1416void PMStack::dump() {
1417 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1418 E = S.end(); I != E; ++I) {
1419 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001420 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001421 }
1422 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001423 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001424}
1425
Devang Patel1c56a632007-01-08 19:29:38 +00001426/// Find appropriate Module Pass Manager in the PM Stack and
1427/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001428void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001429 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001430
Devang Patel1c56a632007-01-08 19:29:38 +00001431 // Find Module Pass Manager
1432 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001433 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1434 if (TopPMType == PreferredType)
1435 break; // We found desired pass manager
1436 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001437 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001438 else
1439 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001440 }
1441
Devang Patel23f8aa92007-01-17 21:19:23 +00001442 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001443}
1444
Devang Patel3312f752007-01-16 21:43:18 +00001445/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1446/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001447void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001448 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001449
Devang Patel15701b52007-01-11 00:19:00 +00001450 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001451 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001452 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1453 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001454 else
Devang Patel3312f752007-01-16 21:43:18 +00001455 break;
1456 }
1457 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1458
1459 // Create new Function Pass Manager
1460 if (!FPP) {
1461 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1462 PMDataManager *PMD = PMS.top();
1463
1464 // [1] Create new Function Pass Manager
1465 FPP = new FPPassManager(PMD->getDepth() + 1);
1466
1467 // [2] Set up new manager's top level manager
1468 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1469 TPM->addIndirectPassManager(FPP);
1470
1471 // [3] Assign manager to manage this new manager. This may create
1472 // and push new managers into PMS
1473 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001474
1475 // If Call Graph Pass Manager is active then use it to manage
1476 // this new Function Pass manager.
1477 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1478 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1479 else
1480 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001481
1482 // [4] Push new manager into PMS
1483 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001484 }
1485
Devang Patel3312f752007-01-16 21:43:18 +00001486 // Assign FPP as the manager of this pass.
1487 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001488}
1489
Devang Patel3312f752007-01-16 21:43:18 +00001490/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001491/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001492void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001493 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001494
1495 BBPassManager *BBP = NULL;
1496
Devang Patel15701b52007-01-11 00:19:00 +00001497 // Basic Pass Manager is a leaf pass manager. It does not handle
1498 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001499 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001500 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001501
Devang Patel3312f752007-01-16 21:43:18 +00001502 // If leaf manager is not Basic Block Pass manager then create new
1503 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001504
Devang Patel3312f752007-01-16 21:43:18 +00001505 if (!BBP) {
1506 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1507 PMDataManager *PMD = PMS.top();
1508
1509 // [1] Create new Basic Block Manager
1510 BBP = new BBPassManager(PMD->getDepth() + 1);
1511
1512 // [2] Set up new manager's top level manager
1513 // Basic Block Pass Manager does not live by itself
1514 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1515 TPM->addIndirectPassManager(BBP);
1516
Devang Patel15701b52007-01-11 00:19:00 +00001517 // [3] Assign manager to manage this new manager. This may create
1518 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001519 Pass *P = dynamic_cast<Pass *>(BBP);
1520 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001521
Devang Patel3312f752007-01-16 21:43:18 +00001522 // [4] Push new manager into PMS
1523 PMS.push(BBP);
1524 }
Devang Patel1c56a632007-01-08 19:29:38 +00001525
Devang Patel3312f752007-01-16 21:43:18 +00001526 // Assign BBP as the manager of this pass.
1527 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001528}
1529
Devang Patelc6b5a552007-01-05 20:16:23 +00001530