blob: 72804be10072c8338c832b88b66fdbb0d3fcd2eb [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 Patel67d6a5e2006-12-19 19:46:59 +000066 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +000067
Devang Patelca58e352006-11-08 10:05:38 +000068 /// Execute all of the passes scheduled for execution. Keep track of
69 /// whether any of the passes modifies the function, and if so, return true.
70 bool runOnFunction(Function &F);
71
Devang Patelf9d96b92006-12-07 19:57:52 +000072 /// Pass Manager itself does not invalidate any analysis info.
73 void getAnalysisUsage(AnalysisUsage &Info) const {
74 Info.setPreservesAll();
75 }
76
Devang Patel475c4532006-12-08 00:59:05 +000077 bool doInitialization(Module &M);
78 bool doInitialization(Function &F);
79 bool doFinalization(Module &M);
80 bool doFinalization(Function &F);
81
Devang Patele3858e62007-02-01 22:08:25 +000082 virtual const char *getPassName() const {
83 return "BasicBlock Pass Manager";
84 }
85
Devang Pateleda56172006-12-12 23:34:33 +000086 // Print passes managed by this manager
87 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000088 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000089 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
90 BasicBlockPass *BP = getContainedPass(Index);
91 BP->dumpPassStructure(Offset + 1);
92 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000093 }
94 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000095
96 BasicBlockPass *getContainedPass(unsigned N) {
97 assert ( N < PassVector.size() && "Pass number out of range!");
98 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
99 return BP;
100 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000101
Devang Patel28349ab2007-02-27 15:00:39 +0000102 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000103 return PMT_BasicBlockPassManager;
104 }
Devang Patelca58e352006-11-08 10:05:38 +0000105};
106
Devang Patele7599552007-01-12 18:52:44 +0000107}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000108
Devang Patele7599552007-01-12 18:52:44 +0000109namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000110
Devang Patel10c2ca62006-12-12 22:47:13 +0000111//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000112// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000113//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000114/// FunctionPassManagerImpl manages FPPassManagers
115class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000116 public PMDataManager,
117 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000118public:
119
Devang Patel4268fc02007-01-16 02:00:38 +0000120 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth),
121 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000122
123 /// add - Add a pass to the queue of passes to run. This passes ownership of
124 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
125 /// will be destroyed as well, so there is no need to delete the pass. This
126 /// implies that all passes MUST be allocated with 'new'.
127 void add(Pass *P) {
128 schedulePass(P);
129 }
130
131 /// run - Execute all of the passes scheduled for execution. Keep track of
132 /// whether any of the passes modifies the module, and if so, return true.
133 bool run(Function &F);
134
135 /// doInitialization - Run all of the initializers for the function passes.
136 ///
137 bool doInitialization(Module &M);
138
139 /// doFinalization - Run all of the initializers for the function passes.
140 ///
141 bool doFinalization(Module &M);
142
143 /// Pass Manager itself does not invalidate any analysis info.
144 void getAnalysisUsage(AnalysisUsage &Info) const {
145 Info.setPreservesAll();
146 }
147
148 inline void addTopLevelPass(Pass *P) {
149
150 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
151
152 // P is a immutable pass and it will be managed by this
153 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000154 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000155 P->setResolver(AR);
156 initializeAnalysisImpl(P);
157 addImmutablePass(IP);
158 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000159 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000160 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000161 }
Devang Patel0f080042007-01-12 17:23:48 +0000162
Devang Patel67d6a5e2006-12-19 19:46:59 +0000163 }
164
165 FPPassManager *getContainedManager(unsigned N) {
166 assert ( N < PassManagers.size() && "Pass number out of range!");
167 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
168 return FP;
169 }
170
Devang Patel67d6a5e2006-12-19 19:46:59 +0000171};
172
173//===----------------------------------------------------------------------===//
174// MPPassManager
175//
176/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000177/// It batches all Module passes passes and function pass managers together and
178/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000179class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000180
181public:
Devang Patel0f080042007-01-12 17:23:48 +0000182 MPPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000183
184 /// run - Execute all of the passes scheduled for execution. Keep track of
185 /// whether any of the passes modifies the module, and if so, return true.
186 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000187
Devang Patelf9d96b92006-12-07 19:57:52 +0000188 /// Pass Manager itself does not invalidate any analysis info.
189 void getAnalysisUsage(AnalysisUsage &Info) const {
190 Info.setPreservesAll();
191 }
192
Devang Patele3858e62007-02-01 22:08:25 +0000193 virtual const char *getPassName() const {
194 return "Module Pass Manager";
195 }
196
Devang Pateleda56172006-12-12 23:34:33 +0000197 // Print passes managed by this manager
198 void dumpPassStructure(unsigned Offset) {
199 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000200 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
201 ModulePass *MP = getContainedPass(Index);
202 MP->dumpPassStructure(Offset + 1);
203 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000204 }
205 }
206
Devang Patelabfbe3b2006-12-16 00:56:26 +0000207 ModulePass *getContainedPass(unsigned N) {
208 assert ( N < PassVector.size() && "Pass number out of range!");
209 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
210 return MP;
211 }
212
Devang Patel28349ab2007-02-27 15:00:39 +0000213 virtual PassManagerType getPassManagerType() const {
214 return PMT_ModulePassManager;
215 }
Devang Patelca58e352006-11-08 10:05:38 +0000216};
217
Devang Patel10c2ca62006-12-12 22:47:13 +0000218//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000219// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000220//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000221/// PassManagerImpl manages MPPassManagers
222class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000223 public PMDataManager,
224 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000225
226public:
227
Devang Patel4268fc02007-01-16 02:00:38 +0000228 PassManagerImpl(int Depth) : PMDataManager(Depth),
229 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000230
Devang Patel376fefa2006-11-08 10:29:57 +0000231 /// add - Add a pass to the queue of passes to run. This passes ownership of
232 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
233 /// will be destroyed as well, so there is no need to delete the pass. This
234 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000235 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000236 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000237 }
Devang Patel376fefa2006-11-08 10:29:57 +0000238
239 /// run - Execute all of the passes scheduled for execution. Keep track of
240 /// whether any of the passes modifies the module, and if so, return true.
241 bool run(Module &M);
242
Devang Patelf9d96b92006-12-07 19:57:52 +0000243 /// Pass Manager itself does not invalidate any analysis info.
244 void getAnalysisUsage(AnalysisUsage &Info) const {
245 Info.setPreservesAll();
246 }
247
Devang Patelabcd1d32006-12-07 21:27:23 +0000248 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000249
Devang Patelfa971cd2006-12-08 23:57:43 +0000250 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000251
252 // P is a immutable pass and it will be managed by this
253 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000254 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000255 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000256 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000257 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000258 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000259 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000260 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000261 }
Devang Patel0f080042007-01-12 17:23:48 +0000262
Devang Patelabcd1d32006-12-07 21:27:23 +0000263 }
264
Devang Patel67d6a5e2006-12-19 19:46:59 +0000265 MPPassManager *getContainedManager(unsigned N) {
266 assert ( N < PassManagers.size() && "Pass number out of range!");
267 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
268 return MP;
269 }
270
Devang Patel376fefa2006-11-08 10:29:57 +0000271};
272
Devang Patel1c3633e2007-01-29 23:10:37 +0000273} // End of llvm namespace
274
275namespace {
276
277//===----------------------------------------------------------------------===//
278// TimingInfo Class - This class is used to calculate information about the
279// amount of time each pass takes to execute. This only happens when
280// -time-passes is enabled on the command line.
281//
282
283class VISIBILITY_HIDDEN TimingInfo {
284 std::map<Pass*, Timer> TimingData;
285 TimerGroup TG;
286
287public:
288 // Use 'create' member to get this.
289 TimingInfo() : TG("... Pass execution timing report ...") {}
290
291 // TimingDtor - Print out information about timing information
292 ~TimingInfo() {
293 // Delete all of the timers...
294 TimingData.clear();
295 // TimerGroup is deleted next, printing the report.
296 }
297
298 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
299 // to a non null value (if the -time-passes option is enabled) or it leaves it
300 // null. It may be called multiple times.
301 static void createTheTimeInfo();
302
303 void passStarted(Pass *P) {
304
305 if (dynamic_cast<PMDataManager *>(P))
306 return;
307
308 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
309 if (I == TimingData.end())
310 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
311 I->second.startTimer();
312 }
313 void passEnded(Pass *P) {
314
315 if (dynamic_cast<PMDataManager *>(P))
316 return;
317
318 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
319 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
320 I->second.stopTimer();
321 }
322};
323
Devang Patelb8817b92006-12-14 00:59:42 +0000324static TimingInfo *TheTimeInfo;
325
Devang Patel1c3633e2007-01-29 23:10:37 +0000326} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000327
Devang Patela1514cb2006-12-07 19:39:39 +0000328//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000329// PMTopLevelManager implementation
330
Devang Patel4268fc02007-01-16 02:00:38 +0000331/// Initialize top level manager. Create first pass manager.
332PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
333
334 if (t == TLM_Pass) {
335 MPPassManager *MPP = new MPPassManager(1);
336 MPP->setTopLevelManager(this);
337 addPassManager(MPP);
338 activeStack.push(MPP);
339 }
340 else if (t == TLM_Function) {
341 FPPassManager *FPP = new FPPassManager(1);
342 FPP->setTopLevelManager(this);
343 addPassManager(FPP);
344 activeStack.push(FPP);
345 }
346}
347
Devang Patelafb1f3622006-12-12 22:35:25 +0000348/// Set pass P as the last user of the given analysis passes.
349void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
350 Pass *P) {
351
352 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
353 E = AnalysisPasses.end(); I != E; ++I) {
354 Pass *AP = *I;
355 LastUser[AP] = P;
356 // If AP is the last user of other passes then make P last user of
357 // such passes.
358 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
359 LUE = LastUser.end(); LUI != LUE; ++LUI) {
360 if (LUI->second == AP)
361 LastUser[LUI->first] = P;
362 }
363 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000364}
365
366/// Collect passes whose last user is P
367void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
368 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000369 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
370 LUE = LastUser.end(); LUI != LUE; ++LUI)
371 if (LUI->second == P)
372 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000373}
374
375/// Schedule pass P for execution. Make sure that passes required by
376/// P are run before P is run. Update analysis info maintained by
377/// the manager. Remove dead passes. This is a recursive function.
378void PMTopLevelManager::schedulePass(Pass *P) {
379
Devang Patel3312f752007-01-16 21:43:18 +0000380 // TODO : Allocate function manager for this pass, other wise required set
381 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000382
Devang Patel3f806962007-02-05 19:34:17 +0000383 // If this Analysis is already requested by one of the previous pass
384 // and it is still available then do not insert new pass in the queue again.
385 if (findAnalysisPass(P->getPassInfo()))
386 return;
387
Devang Pateld74ede72007-03-06 01:06:16 +0000388 // Give pass a chance to prepare the stage.
389 P->preparePassManager(activeStack);
390
Devang Patelafb1f3622006-12-12 22:35:25 +0000391 AnalysisUsage AnUsage;
392 P->getAnalysisUsage(AnUsage);
393 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
394 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
395 E = RequiredSet.end(); I != E; ++I) {
396
397 Pass *AnalysisPass = findAnalysisPass(*I);
398 if (!AnalysisPass) {
399 // Schedule this analysis run first.
400 AnalysisPass = (*I)->createPass();
401 schedulePass(AnalysisPass);
402 }
403 }
404
405 // Now all required passes are available.
406 addTopLevelPass(P);
407}
408
409/// Find the pass that implements Analysis AID. Search immutable
410/// passes and all pass managers. If desired pass is not found
411/// then return NULL.
412Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
413
414 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000415 // Check pass managers
416 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
417 E = PassManagers.end(); P == NULL && I != E; ++I) {
418 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
419 assert(PMD && "This is not a PassManager");
420 P = PMD->findAnalysisPass(AID, false);
421 }
422
423 // Check other pass managers
424 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
425 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
426 P = (*I)->findAnalysisPass(AID, false);
427
Devang Patelafb1f3622006-12-12 22:35:25 +0000428 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
429 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
430 const PassInfo *PI = (*I)->getPassInfo();
431 if (PI == AID)
432 P = *I;
433
434 // If Pass not found then check the interfaces implemented by Immutable Pass
435 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000436 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
437 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
438 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000439 }
440 }
441
Devang Patelafb1f3622006-12-12 22:35:25 +0000442 return P;
443}
444
Devang Pateleda56172006-12-12 23:34:33 +0000445// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000446void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000447
Devang Patelfd4184322007-01-17 20:33:36 +0000448 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000449 return;
450
Devang Pateleda56172006-12-12 23:34:33 +0000451 // Print out the immutable passes
452 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
453 ImmutablePasses[i]->dumpPassStructure(0);
454 }
455
Devang Patel991aeba2006-12-15 20:13:01 +0000456 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000457 E = PassManagers.end(); I != E; ++I)
458 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000459}
460
Devang Patel991aeba2006-12-15 20:13:01 +0000461void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000462
Devang Patelfd4184322007-01-17 20:33:36 +0000463 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000464 return;
465
466 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000467 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000468 E = PassManagers.end(); I != E; ++I) {
469 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
470 assert(PMD && "This is not a PassManager");
471 PMD->dumpPassArguments();
472 }
473 cerr << "\n";
474}
475
Devang Patele3068402006-12-21 00:16:50 +0000476void PMTopLevelManager::initializeAllAnalysisInfo() {
477
478 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
479 E = PassManagers.end(); I != E; ++I) {
480 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
481 assert(PMD && "This is not a PassManager");
482 PMD->initializeAnalysisInfo();
483 }
484
485 // Initailize other pass managers
486 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
487 E = IndirectPassManagers.end(); I != E; ++I)
488 (*I)->initializeAnalysisInfo();
489}
490
Devang Patele7599552007-01-12 18:52:44 +0000491/// Destructor
492PMTopLevelManager::~PMTopLevelManager() {
493 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
494 E = PassManagers.end(); I != E; ++I)
495 delete *I;
496
497 for (std::vector<ImmutablePass *>::iterator
498 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
499 delete *I;
500
501 PassManagers.clear();
502}
503
Devang Patelafb1f3622006-12-12 22:35:25 +0000504//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000505// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000506
Devang Pateld65e9e92006-11-08 01:31:28 +0000507/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000508/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000509bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000510
Devang Patel8f677ce2006-12-07 18:47:25 +0000511 // TODO
512 // If this pass is not preserving information that is required by a
513 // pass maintained by higher level pass manager then do not insert
514 // this pass into current manager. Use new manager. For example,
515 // For example, If FunctionPass F is not preserving ModulePass Info M1
516 // that is used by another ModulePass M2 then do not insert F in
517 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000518 return true;
519}
520
Devang Patel643676c2006-11-11 01:10:19 +0000521/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000522void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000523
Devang Patel643676c2006-11-11 01:10:19 +0000524 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000525 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000526
Devang Patele9976aa2006-12-07 19:33:53 +0000527 //This pass is the current implementation of all of the interfaces it
528 //implements as well.
529 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
530 for (unsigned i = 0, e = II.size(); i != e; ++i)
531 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000532 }
533}
534
Devang Patelf68a3492006-11-07 22:35:17 +0000535/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000536void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000537 AnalysisUsage AnUsage;
538 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000539
Devang Patel2e169c32006-12-07 20:03:49 +0000540 if (AnUsage.getPreservesAll())
541 return;
542
543 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000544 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000545 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000546 std::map<AnalysisID, Pass*>::iterator Info = I++;
547 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000548 PreservedSet.end()) {
549 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000550 if (!dynamic_cast<ImmutablePass*>(Info->second))
551 AvailableAnalysis.erase(Info);
552 }
Devang Patel349170f2006-11-11 01:24:55 +0000553 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000554
555 // Check inherited analysis also. If P is not preserving analysis
556 // provided by parent manager then remove it here.
557 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
558
559 if (!InheritedAnalysis[Index])
560 continue;
561
562 for (std::map<AnalysisID, Pass*>::iterator
563 I = InheritedAnalysis[Index]->begin(),
564 E = InheritedAnalysis[Index]->end(); I != E; ) {
565 std::map<AnalysisID, Pass *>::iterator Info = I++;
566 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
567 PreservedSet.end()) {
568 // Remove this analysis
569 if (!dynamic_cast<ImmutablePass*>(Info->second))
570 InheritedAnalysis[Index]->erase(Info);
571 }
572 }
573 }
574
Devang Patelf68a3492006-11-07 22:35:17 +0000575}
576
Devang Patelca189262006-11-14 03:05:08 +0000577/// Remove analysis passes that are not used any longer
Devang Patel003a5592007-03-05 20:01:30 +0000578void PMDataManager::removeDeadPasses(Pass *P, std::string Msg,
579 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000580
581 std::vector<Pass *> DeadPasses;
582 TPM->collectLastUses(DeadPasses, P);
583
584 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
585 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000586
Devang Patel003a5592007-03-05 20:01:30 +0000587 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000588
Devang Patel7ebf09d2007-03-05 18:20:51 +0000589 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000590 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000591 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000592
Devang Patel17ad0962006-12-08 00:37:52 +0000593 std::map<AnalysisID, Pass*>::iterator Pos =
594 AvailableAnalysis.find((*I)->getPassInfo());
595
Devang Patel475c4532006-12-08 00:59:05 +0000596 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000597 if (Pos != AvailableAnalysis.end())
598 AvailableAnalysis.erase(Pos);
599 }
Devang Patelca189262006-11-14 03:05:08 +0000600}
601
Devang Patel8f677ce2006-12-07 18:47:25 +0000602/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000603/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000604void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000605 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000606
Devang Pateld440cd92006-12-08 23:53:00 +0000607 // This manager is going to manage pass P. Set up analysis resolver
608 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000609 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000610 P->setResolver(AR);
611
Devang Patelec2b9a72007-03-05 22:57:49 +0000612 // If a FunctionPass F is the last user of ModulePass info M
613 // then the F's manager, not F, records itself as a last user of M.
614 std::vector<Pass *> TransferLastUses;
615
Devang Patel90b05e02006-11-11 02:04:19 +0000616 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000617
618 // At the moment, this pass is the last user of all required passes.
619 std::vector<Pass *> LastUses;
620 std::vector<Pass *> RequiredPasses;
621 unsigned PDepth = this->getDepth();
622
623 collectRequiredAnalysisPasses(RequiredPasses, P);
624 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
625 E = RequiredPasses.end(); I != E; ++I) {
626 Pass *PRequired = *I;
627 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000628
629 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
630 RDepth = DM.getDepth();
631
Devang Patelbc03f132006-12-07 23:55:10 +0000632 if (PDepth == RDepth)
633 LastUses.push_back(PRequired);
634 else if (PDepth > RDepth) {
635 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000636 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000637 } else {
638 // Note : This feature is not yet implemented
639 assert (0 &&
640 "Unable to handle Pass that requires lower level Analysis pass");
641 }
642 }
643
Devang Patel39786a92007-01-15 20:31:54 +0000644 // Set P as P's last user until someone starts using P.
645 // However, if P is a Pass Manager then it does not need
646 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000647 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000648 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000649 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000650
Devang Patelec2b9a72007-03-05 22:57:49 +0000651 if (!TransferLastUses.empty()) {
652 Pass *My_PM = dynamic_cast<Pass *>(this);
653 TPM->setLastUser(TransferLastUses, My_PM);
654 TransferLastUses.clear();
655 }
656
Devang Patel17bff0d2006-12-07 22:09:36 +0000657 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000658 // Remove the analysis not preserved by this pass
659 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000660 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000661 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000662
663 // Add pass
664 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000665}
666
Devang Patel1d6267c2006-12-07 23:05:44 +0000667/// Populate RequiredPasses with the analysis pass that are required by
668/// pass P.
669void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
670 Pass *P) {
671 AnalysisUsage AnUsage;
672 P->getAnalysisUsage(AnUsage);
673 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
674 for (std::vector<AnalysisID>::const_iterator
675 I = RequiredSet.begin(), E = RequiredSet.end();
676 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000677 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000678 assert (AnalysisPass && "Analysis pass is not available");
679 RP.push_back(AnalysisPass);
680 }
Devang Patelf58183d2006-12-12 23:09:32 +0000681
682 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
683 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
684 E = IDs.end(); I != E; ++I) {
685 Pass *AnalysisPass = findAnalysisPass(*I, true);
686 assert (AnalysisPass && "Analysis pass is not available");
687 RP.push_back(AnalysisPass);
688 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000689}
690
Devang Patel07f4f582006-11-14 21:49:36 +0000691// All Required analyses should be available to the pass as it runs! Here
692// we fill in the AnalysisImpls member of the pass so that it can
693// successfully use the getAnalysis() method to retrieve the
694// implementations it needs.
695//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000696void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000697 AnalysisUsage AnUsage;
698 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000699
700 for (std::vector<const PassInfo *>::const_iterator
701 I = AnUsage.getRequiredSet().begin(),
702 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000703 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000704 if (Impl == 0)
705 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000706 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000707 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000708 }
709}
710
Devang Patel640c5bb2006-12-08 22:30:11 +0000711/// Find the pass that implements Analysis AID. If desired pass is not found
712/// then return NULL.
713Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
714
715 // Check if AvailableAnalysis map has one entry.
716 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
717
718 if (I != AvailableAnalysis.end())
719 return I->second;
720
721 // Search Parents through TopLevelManager
722 if (SearchParent)
723 return TPM->findAnalysisPass(AID);
724
Devang Patel9d759b82006-12-09 00:09:12 +0000725 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000726}
727
Devang Patel991aeba2006-12-15 20:13:01 +0000728// Print list of passes that are last used by P.
729void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
730
731 std::vector<Pass *> LUses;
732
733 assert (TPM && "Top Level Manager is missing");
734 TPM->collectLastUses(LUses, P);
735
736 for (std::vector<Pass *>::iterator I = LUses.begin(),
737 E = LUses.end(); I != E; ++I) {
738 llvm::cerr << "--" << std::string(Offset*2, ' ');
739 (*I)->dumpPassStructure(0);
740 }
741}
742
743void PMDataManager::dumpPassArguments() const {
744 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
745 E = PassVector.end(); I != E; ++I) {
746 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
747 PMD->dumpPassArguments();
748 else
749 if (const PassInfo *PI = (*I)->getPassInfo())
750 if (!PI->isAnalysisGroup())
751 cerr << " -" << PI->getPassArgument();
752 }
753}
754
Devang Patel003a5592007-03-05 20:01:30 +0000755void PMDataManager:: dumpPassInfo(Pass *P, enum PassDebuggingString S1,
756 enum PassDebuggingString S2,
757 std::string Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000758 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000759 return;
760 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000761 switch (S1) {
762 case EXECUTION_MSG:
763 cerr << "Executing Pass '" << P->getPassName();
764 break;
765 case MODIFICATION_MSG:
766 cerr << "' Made Modification '" << P->getPassName();
767 break;
768 case FREEING_MSG:
769 cerr << " Freeing Pass '" << P->getPassName();
770 break;
771 default:
772 break;
773 }
774 switch (S2) {
775 case ON_BASICBLOCK_MSG:
776 cerr << "' on BasicBlock '" << Msg << "...\n";
777 break;
778 case ON_FUNCTION_MSG:
779 cerr << "' on Function '" << Msg << "...\n";
780 break;
781 case ON_MODULE_MSG:
782 cerr << "' on Module '" << Msg << "...\n";
783 break;
784 case ON_LOOP_MSG:
785 cerr << "' on Loop " << Msg << "...\n";
786 break;
787 case ON_CG_MSG:
788 cerr << "' on Call Graph " << Msg << "...\n";
789 break;
790 default:
791 break;
792 }
Devang Patel991aeba2006-12-15 20:13:01 +0000793}
794
795void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
796 const std::vector<AnalysisID> &Set)
797 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000798 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000799 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
800 for (unsigned i = 0; i != Set.size(); ++i) {
801 if (i) cerr << ",";
802 cerr << " " << Set[i]->getPassName();
803 }
804 cerr << "\n";
805 }
806}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000807
Devang Patele7599552007-01-12 18:52:44 +0000808// Destructor
809PMDataManager::~PMDataManager() {
810
811 for (std::vector<Pass *>::iterator I = PassVector.begin(),
812 E = PassVector.end(); I != E; ++I)
813 delete *I;
814
815 PassVector.clear();
816}
817
Devang Patel9bdf7d42006-12-08 23:28:54 +0000818//===----------------------------------------------------------------------===//
819// NOTE: Is this the right place to define this method ?
820// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000821Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000822 return PM.findAnalysisPass(ID, dir);
823}
824
Devang Patela1514cb2006-12-07 19:39:39 +0000825//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000826// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000827
Devang Patel6e5a1132006-11-07 21:31:57 +0000828/// Execute all of the passes scheduled for execution by invoking
829/// runOnBasicBlock method. Keep track of whether any of the passes modifies
830/// the function, and if so, return true.
831bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000832BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000833
Reid Spencer5301e7c2007-01-30 20:08:39 +0000834 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000835 return false;
836
Devang Patele9585592006-12-08 01:38:28 +0000837 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000838
Devang Patel6e5a1132006-11-07 21:31:57 +0000839 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000840 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
841 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000842 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000843 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000844
Devang Patel003a5592007-03-05 20:01:30 +0000845 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000846 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000847
Devang Patelabfbe3b2006-12-16 00:56:26 +0000848 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000849
Devang Patelabfbe3b2006-12-16 00:56:26 +0000850 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000851 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000852 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000853
Devang Patel003a5592007-03-05 20:01:30 +0000854 if (Changed)
855 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000856 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000857
Devang Patelabfbe3b2006-12-16 00:56:26 +0000858 removeNotPreservedAnalysis(BP);
859 recordAvailableAnalysis(BP);
Devang Patel003a5592007-03-05 20:01:30 +0000860 removeDeadPasses(BP, (*I).getName(), ON_BASICBLOCK_MSG);
861
Devang Patel6e5a1132006-11-07 21:31:57 +0000862 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000863 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000864}
865
Devang Patel475c4532006-12-08 00:59:05 +0000866// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000867inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000868 bool Changed = false;
869
Devang Patelabfbe3b2006-12-16 00:56:26 +0000870 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
871 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000872 Changed |= BP->doInitialization(M);
873 }
874
875 return Changed;
876}
877
Devang Patel67d6a5e2006-12-19 19:46:59 +0000878inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000879 bool Changed = false;
880
Devang Patelabfbe3b2006-12-16 00:56:26 +0000881 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
882 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000883 Changed |= BP->doFinalization(M);
884 }
885
886 return Changed;
887}
888
Devang Patel67d6a5e2006-12-19 19:46:59 +0000889inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000890 bool Changed = false;
891
Devang Patelabfbe3b2006-12-16 00:56:26 +0000892 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
893 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000894 Changed |= BP->doInitialization(F);
895 }
896
897 return Changed;
898}
899
Devang Patel67d6a5e2006-12-19 19:46:59 +0000900inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000901 bool Changed = false;
902
Devang Patelabfbe3b2006-12-16 00:56:26 +0000903 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
904 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000905 Changed |= BP->doFinalization(F);
906 }
907
908 return Changed;
909}
910
911
Devang Patela1514cb2006-12-07 19:39:39 +0000912//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000913// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000914
Devang Patel4e12f862006-11-08 10:44:40 +0000915/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +0000916FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000917 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000918 // FPM is the top level manager.
919 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000920
921 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +0000922 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +0000923 FPM->setResolver(AR);
924
Devang Patel1f653682006-12-08 18:57:16 +0000925 MP = P;
926}
927
Devang Patelb67904d2006-12-13 02:36:01 +0000928FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000929 delete FPM;
930}
931
Devang Patel4e12f862006-11-08 10:44:40 +0000932/// add - Add a pass to the queue of passes to run. This passes
933/// ownership of the Pass to the PassManager. When the
934/// PassManager_X is destroyed, the pass will be destroyed as well, so
935/// there is no need to delete the pass. (TODO delete passes.)
936/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +0000937void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000938 FPM->add(P);
939}
940
Devang Patel9f3083e2006-11-15 19:39:54 +0000941/// run - Execute all of the passes scheduled for execution. Keep
942/// track of whether any of the passes modifies the function, and if
943/// so, return true.
944///
Devang Patelb67904d2006-12-13 02:36:01 +0000945bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000946 std::string errstr;
947 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000948 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000949 abort();
950 }
Devang Patel272908d2006-12-08 22:57:48 +0000951 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000952}
953
954
Devang Patelff631ae2006-11-15 01:27:05 +0000955/// doInitialization - Run all of the initializers for the function passes.
956///
Devang Patelb67904d2006-12-13 02:36:01 +0000957bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000958 return FPM->doInitialization(*MP->getModule());
959}
960
961/// doFinalization - Run all of the initializers for the function passes.
962///
Devang Patelb67904d2006-12-13 02:36:01 +0000963bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000964 return FPM->doFinalization(*MP->getModule());
965}
966
Devang Patela1514cb2006-12-07 19:39:39 +0000967//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000968// FunctionPassManagerImpl implementation
969//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000970inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
971 bool Changed = false;
972
973 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
974 FPPassManager *FP = getContainedManager(Index);
975 Changed |= FP->doInitialization(M);
976 }
977
978 return Changed;
979}
980
981inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
982 bool Changed = false;
983
984 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
985 FPPassManager *FP = getContainedManager(Index);
986 Changed |= FP->doFinalization(M);
987 }
988
989 return Changed;
990}
991
992// Execute all the passes managed by this top level manager.
993// Return true if any function is modified by a pass.
994bool FunctionPassManagerImpl::run(Function &F) {
995
996 bool Changed = false;
997
998 TimingInfo::createTheTimeInfo();
999
1000 dumpArguments();
1001 dumpPasses();
1002
Devang Patele3068402006-12-21 00:16:50 +00001003 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001004 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1005 FPPassManager *FP = getContainedManager(Index);
1006 Changed |= FP->runOnFunction(F);
1007 }
1008 return Changed;
1009}
1010
1011//===----------------------------------------------------------------------===//
1012// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001013
Devang Patele7599552007-01-12 18:52:44 +00001014/// Print passes managed by this manager
1015void FPPassManager::dumpPassStructure(unsigned Offset) {
1016 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1017 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1018 FunctionPass *FP = getContainedPass(Index);
1019 FP->dumpPassStructure(Offset + 1);
1020 dumpLastUses(FP, Offset+1);
1021 }
1022}
1023
1024
Devang Patel0c2012f2006-11-07 21:49:50 +00001025/// Execute all of the passes scheduled for execution by invoking
1026/// runOnFunction method. Keep track of whether any of the passes modifies
1027/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001028bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001029
1030 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001031
Reid Spencer5301e7c2007-01-30 20:08:39 +00001032 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001033 return false;
1034
Devang Patelabfbe3b2006-12-16 00:56:26 +00001035 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1036 FunctionPass *FP = getContainedPass(Index);
1037
Devang Patelf6d1d212006-12-14 00:25:06 +00001038 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001039 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001040
Devang Patel003a5592007-03-05 20:01:30 +00001041 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001042 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001043
Devang Patelabfbe3b2006-12-16 00:56:26 +00001044 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001045
Devang Patelabfbe3b2006-12-16 00:56:26 +00001046 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001047 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001048 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001049
Devang Patel003a5592007-03-05 20:01:30 +00001050 if (Changed)
1051 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001052 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001053
Devang Patelabfbe3b2006-12-16 00:56:26 +00001054 removeNotPreservedAnalysis(FP);
1055 recordAvailableAnalysis(FP);
Devang Patel003a5592007-03-05 20:01:30 +00001056 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001057 }
1058 return Changed;
1059}
1060
Devang Patel67d6a5e2006-12-19 19:46:59 +00001061bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001062
Devang Patel67d6a5e2006-12-19 19:46:59 +00001063 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001064
1065 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1066 this->runOnFunction(*I);
1067
1068 return Changed |= doFinalization(M);
1069}
1070
1071inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001072 bool Changed = false;
1073
Devang Patelabfbe3b2006-12-16 00:56:26 +00001074 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1075 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001076 Changed |= FP->doInitialization(M);
1077 }
1078
1079 return Changed;
1080}
1081
Devang Patel67d6a5e2006-12-19 19:46:59 +00001082inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001083 bool Changed = false;
1084
Devang Patelabfbe3b2006-12-16 00:56:26 +00001085 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1086 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001087 Changed |= FP->doFinalization(M);
1088 }
1089
Devang Patelff631ae2006-11-15 01:27:05 +00001090 return Changed;
1091}
1092
Devang Patela1514cb2006-12-07 19:39:39 +00001093//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001094// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001095
Devang Patel05e1a972006-11-07 22:03:15 +00001096/// Execute all of the passes scheduled for execution by invoking
1097/// runOnModule method. Keep track of whether any of the passes modifies
1098/// the module, and if so, return true.
1099bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001100MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001101 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001102
Devang Patelabfbe3b2006-12-16 00:56:26 +00001103 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1104 ModulePass *MP = getContainedPass(Index);
1105
Devang Patelf6d1d212006-12-14 00:25:06 +00001106 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001107 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001108
Devang Patel003a5592007-03-05 20:01:30 +00001109 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001110 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001111
Devang Patelabfbe3b2006-12-16 00:56:26 +00001112 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001113
Devang Patelabfbe3b2006-12-16 00:56:26 +00001114 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001115 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001116 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001117
Devang Patel003a5592007-03-05 20:01:30 +00001118 if (Changed)
1119 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1120 M.getModuleIdentifier());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001121 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001122
Devang Patelabfbe3b2006-12-16 00:56:26 +00001123 removeNotPreservedAnalysis(MP);
1124 recordAvailableAnalysis(MP);
Devang Patel003a5592007-03-05 20:01:30 +00001125 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001126 }
1127 return Changed;
1128}
1129
Devang Patela1514cb2006-12-07 19:39:39 +00001130//===----------------------------------------------------------------------===//
1131// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001132//
Devang Patelc290c8a2006-11-07 22:23:34 +00001133/// run - Execute all of the passes scheduled for execution. Keep track of
1134/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001135bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001136
Devang Patelc290c8a2006-11-07 22:23:34 +00001137 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001138
Devang Patelb8817b92006-12-14 00:59:42 +00001139 TimingInfo::createTheTimeInfo();
1140
Devang Patelcfd70c42006-12-13 22:10:00 +00001141 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001142 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001143
Devang Patele3068402006-12-21 00:16:50 +00001144 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001145 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1146 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001147 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001148 }
1149 return Changed;
1150}
Devang Patel376fefa2006-11-08 10:29:57 +00001151
Devang Patela1514cb2006-12-07 19:39:39 +00001152//===----------------------------------------------------------------------===//
1153// PassManager implementation
1154
Devang Patel376fefa2006-11-08 10:29:57 +00001155/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001156PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001157 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001158 // PM is the top level manager
1159 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001160}
1161
Devang Patelb67904d2006-12-13 02:36:01 +00001162PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001163 delete PM;
1164}
1165
Devang Patel376fefa2006-11-08 10:29:57 +00001166/// add - Add a pass to the queue of passes to run. This passes ownership of
1167/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1168/// will be destroyed as well, so there is no need to delete the pass. This
1169/// implies that all passes MUST be allocated with 'new'.
1170void
Devang Patelb67904d2006-12-13 02:36:01 +00001171PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001172 PM->add(P);
1173}
1174
1175/// run - Execute all of the passes scheduled for execution. Keep track of
1176/// whether any of the passes modifies the module, and if so, return true.
1177bool
Devang Patelb67904d2006-12-13 02:36:01 +00001178PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001179 return PM->run(M);
1180}
1181
Devang Patelb8817b92006-12-14 00:59:42 +00001182//===----------------------------------------------------------------------===//
1183// TimingInfo Class - This class is used to calculate information about the
1184// amount of time each pass takes to execute. This only happens with
1185// -time-passes is enabled on the command line.
1186//
1187bool llvm::TimePassesIsEnabled = false;
1188static cl::opt<bool,true>
1189EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1190 cl::desc("Time each pass, printing elapsed time for each on exit"));
1191
1192// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1193// a non null value (if the -time-passes option is enabled) or it leaves it
1194// null. It may be called multiple times.
1195void TimingInfo::createTheTimeInfo() {
1196 if (!TimePassesIsEnabled || TheTimeInfo) return;
1197
1198 // Constructed the first time this is called, iff -time-passes is enabled.
1199 // This guarantees that the object will be constructed before static globals,
1200 // thus it will be destroyed before them.
1201 static ManagedStatic<TimingInfo> TTI;
1202 TheTimeInfo = &*TTI;
1203}
1204
Devang Patel1c3633e2007-01-29 23:10:37 +00001205/// If TimingInfo is enabled then start pass timer.
1206void StartPassTimer(Pass *P) {
1207 if (TheTimeInfo)
1208 TheTimeInfo->passStarted(P);
1209}
1210
1211/// If TimingInfo is enabled then stop pass timer.
1212void StopPassTimer(Pass *P) {
1213 if (TheTimeInfo)
1214 TheTimeInfo->passEnded(P);
1215}
1216
Devang Patel1c56a632007-01-08 19:29:38 +00001217//===----------------------------------------------------------------------===//
1218// PMStack implementation
1219//
Devang Patelad98d232007-01-11 22:15:30 +00001220
Devang Patel1c56a632007-01-08 19:29:38 +00001221// Pop Pass Manager from the stack and clear its analysis info.
1222void PMStack::pop() {
1223
1224 PMDataManager *Top = this->top();
1225 Top->initializeAnalysisInfo();
1226
1227 S.pop_back();
1228}
1229
1230// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001231void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001232
Devang Patel15701b52007-01-11 00:19:00 +00001233 PMDataManager *Top = NULL;
1234 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1235 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001236
Devang Patel15701b52007-01-11 00:19:00 +00001237 if (this->empty()) {
1238 Top = PM;
1239 }
1240 else {
1241 Top = this->top();
1242 PMTopLevelManager *TPM = Top->getTopLevelManager();
1243
1244 assert (TPM && "Unable to find top level manager");
1245 TPM->addIndirectPassManager(PM);
1246 PM->setTopLevelManager(TPM);
1247 }
1248
1249 AnalysisResolver *AR = new AnalysisResolver(*Top);
1250 P->setResolver(AR);
1251
1252 S.push_back(PM);
1253}
1254
1255// Dump content of the pass manager stack.
1256void PMStack::dump() {
1257 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1258 E = S.end(); I != E; ++I) {
1259 Pass *P = dynamic_cast<Pass *>(*I);
1260 printf ("%s ", P->getPassName());
1261 }
1262 if (!S.empty())
1263 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001264}
1265
Devang Patel1c56a632007-01-08 19:29:38 +00001266/// Find appropriate Module Pass Manager in the PM Stack and
1267/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001268void ModulePass::assignPassManager(PMStack &PMS,
1269 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001270
Devang Patel1c56a632007-01-08 19:29:38 +00001271 // Find Module Pass Manager
1272 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001273 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1274 if (TopPMType == PreferredType)
1275 break; // We found desired pass manager
1276 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001277 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001278 else
1279 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001280 }
1281
Devang Patel23f8aa92007-01-17 21:19:23 +00001282 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001283}
1284
Devang Patel3312f752007-01-16 21:43:18 +00001285/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1286/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001287void FunctionPass::assignPassManager(PMStack &PMS,
1288 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001289
Devang Patel15701b52007-01-11 00:19:00 +00001290 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001291 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001292 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1293 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001294 else
Devang Patel3312f752007-01-16 21:43:18 +00001295 break;
1296 }
1297 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1298
1299 // Create new Function Pass Manager
1300 if (!FPP) {
1301 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1302 PMDataManager *PMD = PMS.top();
1303
1304 // [1] Create new Function Pass Manager
1305 FPP = new FPPassManager(PMD->getDepth() + 1);
1306
1307 // [2] Set up new manager's top level manager
1308 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1309 TPM->addIndirectPassManager(FPP);
1310
1311 // [3] Assign manager to manage this new manager. This may create
1312 // and push new managers into PMS
1313 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001314
1315 // If Call Graph Pass Manager is active then use it to manage
1316 // this new Function Pass manager.
1317 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1318 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1319 else
1320 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001321
1322 // [4] Push new manager into PMS
1323 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001324 }
1325
Devang Patel3312f752007-01-16 21:43:18 +00001326 // Assign FPP as the manager of this pass.
1327 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001328}
1329
Devang Patel3312f752007-01-16 21:43:18 +00001330/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001331/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001332void BasicBlockPass::assignPassManager(PMStack &PMS,
1333 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001334
1335 BBPassManager *BBP = NULL;
1336
Devang Patel15701b52007-01-11 00:19:00 +00001337 // Basic Pass Manager is a leaf pass manager. It does not handle
1338 // any other pass manager.
1339 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001340 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001341 }
1342
Devang Patel3312f752007-01-16 21:43:18 +00001343 // If leaf manager is not Basic Block Pass manager then create new
1344 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001345
Devang Patel3312f752007-01-16 21:43:18 +00001346 if (!BBP) {
1347 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1348 PMDataManager *PMD = PMS.top();
1349
1350 // [1] Create new Basic Block Manager
1351 BBP = new BBPassManager(PMD->getDepth() + 1);
1352
1353 // [2] Set up new manager's top level manager
1354 // Basic Block Pass Manager does not live by itself
1355 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1356 TPM->addIndirectPassManager(BBP);
1357
Devang Patel15701b52007-01-11 00:19:00 +00001358 // [3] Assign manager to manage this new manager. This may create
1359 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001360 Pass *P = dynamic_cast<Pass *>(BBP);
1361 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001362
Devang Patel3312f752007-01-16 21:43:18 +00001363 // [4] Push new manager into PMS
1364 PMS.push(BBP);
1365 }
Devang Patel1c56a632007-01-08 19:29:38 +00001366
Devang Patel3312f752007-01-16 21:43:18 +00001367 // Assign BBP as the manager of this pass.
1368 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001369}
1370
Devang Patelc6b5a552007-01-05 20:16:23 +00001371