blob: a2bbb7d3ab9be167ffdbce79e9bef2d52d439a07 [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"
Devang Patela9844592006-11-11 01:31:05 +000022#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000023#include <map>
Devang Patelffca9102006-12-15 19:39:30 +000024
Devang Patele7599552007-01-12 18:52:44 +000025// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000026
Devang Patelf1567a52006-12-13 20:03:48 +000027namespace llvm {
28
29//===----------------------------------------------------------------------===//
30// Pass debugging information. Often it is useful to find out what pass is
31// running when a crash occurs in a utility. When this library is compiled with
32// debugging on, a command line option (--debug-pass) is enabled that causes the
33// pass name to be printed before it executes.
34//
35
Devang Patel03fb5872006-12-13 21:13:31 +000036// Different debug levels that can be enabled...
37enum PassDebugLevel {
38 None, Arguments, Structure, Executions, Details
39};
40
Devang Patelf1567a52006-12-13 20:03:48 +000041static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000042PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000043 cl::desc("Print PassManager debugging information"),
44 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000045 clEnumVal(None , "disable debug output"),
46 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
47 clEnumVal(Structure , "print pass structure before run()"),
48 clEnumVal(Executions, "print pass name before it is executed"),
49 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000050 clEnumValEnd));
51} // End of llvm namespace
52
Devang Patelffca9102006-12-15 19:39:30 +000053namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000054
Devang Patelf33f3eb2006-12-07 19:21:29 +000055//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000056// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000057//
Devang Patel67d6a5e2006-12-19 19:46:59 +000058/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000059/// pass together and sequence them to process one basic block before
60/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000061class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
62 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000063
64public:
Devang Patel67d6a5e2006-12-19 19:46:59 +000065 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +000066
Devang Patelca58e352006-11-08 10:05:38 +000067 /// Execute all of the passes scheduled for execution. Keep track of
68 /// whether any of the passes modifies the function, and if so, return true.
69 bool runOnFunction(Function &F);
70
Devang Patelf9d96b92006-12-07 19:57:52 +000071 /// Pass Manager itself does not invalidate any analysis info.
72 void getAnalysisUsage(AnalysisUsage &Info) const {
73 Info.setPreservesAll();
74 }
75
Devang Patel475c4532006-12-08 00:59:05 +000076 bool doInitialization(Module &M);
77 bool doInitialization(Function &F);
78 bool doFinalization(Module &M);
79 bool doFinalization(Function &F);
80
Devang Patele3858e62007-02-01 22:08:25 +000081 virtual const char *getPassName() const {
82 return "BasicBlock Pass Manager";
83 }
84
Devang Pateleda56172006-12-12 23:34:33 +000085 // Print passes managed by this manager
86 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000087 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000088 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
89 BasicBlockPass *BP = getContainedPass(Index);
90 BP->dumpPassStructure(Offset + 1);
91 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000092 }
93 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000094
95 BasicBlockPass *getContainedPass(unsigned N) {
96 assert ( N < PassVector.size() && "Pass number out of range!");
97 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
98 return BP;
99 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000100
Devang Patel28349ab2007-02-27 15:00:39 +0000101 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000102 return PMT_BasicBlockPassManager;
103 }
Devang Patelca58e352006-11-08 10:05:38 +0000104};
105
Devang Patele7599552007-01-12 18:52:44 +0000106}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000107
Devang Patele7599552007-01-12 18:52:44 +0000108namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000109
Devang Patel10c2ca62006-12-12 22:47:13 +0000110//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000111// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000112//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000113/// FunctionPassManagerImpl manages FPPassManagers
114class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000115 public PMDataManager,
116 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000117public:
118
Devang Patel4268fc02007-01-16 02:00:38 +0000119 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth),
120 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000121
122 /// add - Add a pass to the queue of passes to run. This passes ownership of
123 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
124 /// will be destroyed as well, so there is no need to delete the pass. This
125 /// implies that all passes MUST be allocated with 'new'.
126 void add(Pass *P) {
127 schedulePass(P);
128 }
129
130 /// run - Execute all of the passes scheduled for execution. Keep track of
131 /// whether any of the passes modifies the module, and if so, return true.
132 bool run(Function &F);
133
134 /// doInitialization - Run all of the initializers for the function passes.
135 ///
136 bool doInitialization(Module &M);
137
138 /// doFinalization - Run all of the initializers for the function passes.
139 ///
140 bool doFinalization(Module &M);
141
142 /// Pass Manager itself does not invalidate any analysis info.
143 void getAnalysisUsage(AnalysisUsage &Info) const {
144 Info.setPreservesAll();
145 }
146
147 inline void addTopLevelPass(Pass *P) {
148
149 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
150
151 // P is a immutable pass and it will be managed by this
152 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000153 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000154 P->setResolver(AR);
155 initializeAnalysisImpl(P);
156 addImmutablePass(IP);
157 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000158 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000159 P->assignPassManager(activeStack);
Devang Patel4d06ace2007-02-07 19:37:53 +0000160 activeStack.handleLastUserOverflow();
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 Patel4d06ace2007-02-07 19:37:53 +0000261 activeStack.handleLastUserOverflow();
Devang Pateld440cd92006-12-08 23:53:00 +0000262 }
Devang Patel0f080042007-01-12 17:23:48 +0000263
Devang Patelabcd1d32006-12-07 21:27:23 +0000264 }
265
Devang Patel67d6a5e2006-12-19 19:46:59 +0000266 MPPassManager *getContainedManager(unsigned N) {
267 assert ( N < PassManagers.size() && "Pass number out of range!");
268 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
269 return MP;
270 }
271
Devang Patel376fefa2006-11-08 10:29:57 +0000272};
273
Devang Patel1c3633e2007-01-29 23:10:37 +0000274} // End of llvm namespace
275
276namespace {
277
278//===----------------------------------------------------------------------===//
279// TimingInfo Class - This class is used to calculate information about the
280// amount of time each pass takes to execute. This only happens when
281// -time-passes is enabled on the command line.
282//
283
284class VISIBILITY_HIDDEN TimingInfo {
285 std::map<Pass*, Timer> TimingData;
286 TimerGroup TG;
287
288public:
289 // Use 'create' member to get this.
290 TimingInfo() : TG("... Pass execution timing report ...") {}
291
292 // TimingDtor - Print out information about timing information
293 ~TimingInfo() {
294 // Delete all of the timers...
295 TimingData.clear();
296 // TimerGroup is deleted next, printing the report.
297 }
298
299 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
300 // to a non null value (if the -time-passes option is enabled) or it leaves it
301 // null. It may be called multiple times.
302 static void createTheTimeInfo();
303
304 void passStarted(Pass *P) {
305
306 if (dynamic_cast<PMDataManager *>(P))
307 return;
308
309 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
310 if (I == TimingData.end())
311 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
312 I->second.startTimer();
313 }
314 void passEnded(Pass *P) {
315
316 if (dynamic_cast<PMDataManager *>(P))
317 return;
318
319 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
320 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
321 I->second.stopTimer();
322 }
323};
324
Devang Patelb8817b92006-12-14 00:59:42 +0000325static TimingInfo *TheTimeInfo;
326
Devang Patel1c3633e2007-01-29 23:10:37 +0000327} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000328
Devang Patela1514cb2006-12-07 19:39:39 +0000329//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000330// PMTopLevelManager implementation
331
Devang Patel4268fc02007-01-16 02:00:38 +0000332/// Initialize top level manager. Create first pass manager.
333PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
334
335 if (t == TLM_Pass) {
336 MPPassManager *MPP = new MPPassManager(1);
337 MPP->setTopLevelManager(this);
338 addPassManager(MPP);
339 activeStack.push(MPP);
340 }
341 else if (t == TLM_Function) {
342 FPPassManager *FPP = new FPPassManager(1);
343 FPP->setTopLevelManager(this);
344 addPassManager(FPP);
345 activeStack.push(FPP);
346 }
347}
348
Devang Patelafb1f3622006-12-12 22:35:25 +0000349/// Set pass P as the last user of the given analysis passes.
350void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
351 Pass *P) {
352
353 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
354 E = AnalysisPasses.end(); I != E; ++I) {
355 Pass *AP = *I;
356 LastUser[AP] = P;
357 // If AP is the last user of other passes then make P last user of
358 // such passes.
359 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
360 LUE = LastUser.end(); LUI != LUE; ++LUI) {
361 if (LUI->second == AP)
362 LastUser[LUI->first] = P;
363 }
364 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000365}
366
367/// Collect passes whose last user is P
368void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
369 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000370 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
371 LUE = LastUser.end(); LUI != LUE; ++LUI)
372 if (LUI->second == P)
373 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000374}
375
376/// Schedule pass P for execution. Make sure that passes required by
377/// P are run before P is run. Update analysis info maintained by
378/// the manager. Remove dead passes. This is a recursive function.
379void PMTopLevelManager::schedulePass(Pass *P) {
380
Devang Patel3312f752007-01-16 21:43:18 +0000381 // TODO : Allocate function manager for this pass, other wise required set
382 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000383
Devang Patel3f806962007-02-05 19:34:17 +0000384 // If this Analysis is already requested by one of the previous pass
385 // and it is still available then do not insert new pass in the queue again.
386 if (findAnalysisPass(P->getPassInfo()))
387 return;
388
Devang Patelafb1f3622006-12-12 22:35:25 +0000389 AnalysisUsage AnUsage;
390 P->getAnalysisUsage(AnUsage);
391 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
392 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
393 E = RequiredSet.end(); I != E; ++I) {
394
395 Pass *AnalysisPass = findAnalysisPass(*I);
396 if (!AnalysisPass) {
397 // Schedule this analysis run first.
398 AnalysisPass = (*I)->createPass();
399 schedulePass(AnalysisPass);
400 }
401 }
402
403 // Now all required passes are available.
404 addTopLevelPass(P);
405}
406
407/// Find the pass that implements Analysis AID. Search immutable
408/// passes and all pass managers. If desired pass is not found
409/// then return NULL.
410Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
411
412 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000413 // Check pass managers
414 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
415 E = PassManagers.end(); P == NULL && I != E; ++I) {
416 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
417 assert(PMD && "This is not a PassManager");
418 P = PMD->findAnalysisPass(AID, false);
419 }
420
421 // Check other pass managers
422 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
423 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
424 P = (*I)->findAnalysisPass(AID, false);
425
Devang Patelafb1f3622006-12-12 22:35:25 +0000426 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
427 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
428 const PassInfo *PI = (*I)->getPassInfo();
429 if (PI == AID)
430 P = *I;
431
432 // If Pass not found then check the interfaces implemented by Immutable Pass
433 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000434 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
435 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
436 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000437 }
438 }
439
Devang Patelafb1f3622006-12-12 22:35:25 +0000440 return P;
441}
442
Devang Pateleda56172006-12-12 23:34:33 +0000443// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000444void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000445
Devang Patelfd4184322007-01-17 20:33:36 +0000446 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000447 return;
448
Devang Pateleda56172006-12-12 23:34:33 +0000449 // Print out the immutable passes
450 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
451 ImmutablePasses[i]->dumpPassStructure(0);
452 }
453
Devang Patel991aeba2006-12-15 20:13:01 +0000454 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000455 E = PassManagers.end(); I != E; ++I)
456 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000457}
458
Devang Patel991aeba2006-12-15 20:13:01 +0000459void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000460
Devang Patelfd4184322007-01-17 20:33:36 +0000461 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000462 return;
463
464 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000465 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000466 E = PassManagers.end(); I != E; ++I) {
467 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
468 assert(PMD && "This is not a PassManager");
469 PMD->dumpPassArguments();
470 }
471 cerr << "\n";
472}
473
Devang Patele3068402006-12-21 00:16:50 +0000474void PMTopLevelManager::initializeAllAnalysisInfo() {
475
476 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
477 E = PassManagers.end(); I != E; ++I) {
478 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
479 assert(PMD && "This is not a PassManager");
480 PMD->initializeAnalysisInfo();
481 }
482
483 // Initailize other pass managers
484 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
485 E = IndirectPassManagers.end(); I != E; ++I)
486 (*I)->initializeAnalysisInfo();
487}
488
Devang Patele7599552007-01-12 18:52:44 +0000489/// Destructor
490PMTopLevelManager::~PMTopLevelManager() {
491 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
492 E = PassManagers.end(); I != E; ++I)
493 delete *I;
494
495 for (std::vector<ImmutablePass *>::iterator
496 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
497 delete *I;
498
499 PassManagers.clear();
500}
501
Devang Patelafb1f3622006-12-12 22:35:25 +0000502//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000503// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000504
Devang Pateld65e9e92006-11-08 01:31:28 +0000505/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000506/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000507bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000508
Devang Patel8f677ce2006-12-07 18:47:25 +0000509 // TODO
510 // If this pass is not preserving information that is required by a
511 // pass maintained by higher level pass manager then do not insert
512 // this pass into current manager. Use new manager. For example,
513 // For example, If FunctionPass F is not preserving ModulePass Info M1
514 // that is used by another ModulePass M2 then do not insert F in
515 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000516 return true;
517}
518
Devang Patel643676c2006-11-11 01:10:19 +0000519/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000520void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000521
Devang Patel643676c2006-11-11 01:10:19 +0000522 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000523 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000524
Devang Patele9976aa2006-12-07 19:33:53 +0000525 //This pass is the current implementation of all of the interfaces it
526 //implements as well.
527 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
528 for (unsigned i = 0, e = II.size(); i != e; ++i)
529 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000530 }
531}
532
Devang Patelf68a3492006-11-07 22:35:17 +0000533/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000534void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000535 AnalysisUsage AnUsage;
536 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000537
Devang Patel2e169c32006-12-07 20:03:49 +0000538 if (AnUsage.getPreservesAll())
539 return;
540
541 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000542 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000543 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000544 std::map<AnalysisID, Pass*>::iterator Info = I++;
545 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000546 PreservedSet.end()) {
547 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000548 if (!dynamic_cast<ImmutablePass*>(Info->second))
549 AvailableAnalysis.erase(Info);
550 }
Devang Patel349170f2006-11-11 01:24:55 +0000551 }
Devang Patelf68a3492006-11-07 22:35:17 +0000552}
553
Devang Patelca189262006-11-14 03:05:08 +0000554/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000555void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000556
557 std::vector<Pass *> DeadPasses;
558 TPM->collectLastUses(DeadPasses, P);
559
560 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
561 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000562
563 std::string Msg1 = " Freeing Pass '";
564 dumpPassInfo(*I, Msg1, Msg);
565
Devang Patelb8817b92006-12-14 00:59:42 +0000566 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000567 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000568 if (TheTimeInfo) TheTimeInfo->passEnded(P);
569
Devang Patel17ad0962006-12-08 00:37:52 +0000570 std::map<AnalysisID, Pass*>::iterator Pos =
571 AvailableAnalysis.find((*I)->getPassInfo());
572
Devang Patel475c4532006-12-08 00:59:05 +0000573 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000574 if (Pos != AvailableAnalysis.end())
575 AvailableAnalysis.erase(Pos);
576 }
Devang Patelca189262006-11-14 03:05:08 +0000577}
578
Devang Patel8f677ce2006-12-07 18:47:25 +0000579/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000580/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000581void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000582 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000583
Devang Pateld440cd92006-12-08 23:53:00 +0000584 // This manager is going to manage pass P. Set up analysis resolver
585 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000586 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000587 P->setResolver(AR);
588
Devang Patel90b05e02006-11-11 02:04:19 +0000589 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000590
591 // At the moment, this pass is the last user of all required passes.
592 std::vector<Pass *> LastUses;
593 std::vector<Pass *> RequiredPasses;
594 unsigned PDepth = this->getDepth();
595
596 collectRequiredAnalysisPasses(RequiredPasses, P);
597 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
598 E = RequiredPasses.end(); I != E; ++I) {
599 Pass *PRequired = *I;
600 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000601
602 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
603 RDepth = DM.getDepth();
604
Devang Patelbc03f132006-12-07 23:55:10 +0000605 if (PDepth == RDepth)
606 LastUses.push_back(PRequired);
607 else if (PDepth > RDepth) {
608 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000609 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000610 } else {
611 // Note : This feature is not yet implemented
612 assert (0 &&
613 "Unable to handle Pass that requires lower level Analysis pass");
614 }
615 }
616
Devang Patel39786a92007-01-15 20:31:54 +0000617 // Set P as P's last user until someone starts using P.
618 // However, if P is a Pass Manager then it does not need
619 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000620 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000621 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000622 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000623
Devang Patel17bff0d2006-12-07 22:09:36 +0000624 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000625 // Remove the analysis not preserved by this pass
626 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000627 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000628 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000629
630 // Add pass
631 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000632}
633
Devang Patel1d6267c2006-12-07 23:05:44 +0000634/// Populate RequiredPasses with the analysis pass that are required by
635/// pass P.
636void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
637 Pass *P) {
638 AnalysisUsage AnUsage;
639 P->getAnalysisUsage(AnUsage);
640 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
641 for (std::vector<AnalysisID>::const_iterator
642 I = RequiredSet.begin(), E = RequiredSet.end();
643 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000644 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000645 assert (AnalysisPass && "Analysis pass is not available");
646 RP.push_back(AnalysisPass);
647 }
Devang Patelf58183d2006-12-12 23:09:32 +0000648
649 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
650 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
651 E = IDs.end(); I != E; ++I) {
652 Pass *AnalysisPass = findAnalysisPass(*I, true);
653 assert (AnalysisPass && "Analysis pass is not available");
654 RP.push_back(AnalysisPass);
655 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000656}
657
Devang Patel07f4f582006-11-14 21:49:36 +0000658// All Required analyses should be available to the pass as it runs! Here
659// we fill in the AnalysisImpls member of the pass so that it can
660// successfully use the getAnalysis() method to retrieve the
661// implementations it needs.
662//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000663void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000664 AnalysisUsage AnUsage;
665 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000666
667 for (std::vector<const PassInfo *>::const_iterator
668 I = AnUsage.getRequiredSet().begin(),
669 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000670 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000671 if (Impl == 0)
672 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000673 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000674 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000675 }
676}
677
Devang Patel640c5bb2006-12-08 22:30:11 +0000678/// Find the pass that implements Analysis AID. If desired pass is not found
679/// then return NULL.
680Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
681
682 // Check if AvailableAnalysis map has one entry.
683 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
684
685 if (I != AvailableAnalysis.end())
686 return I->second;
687
688 // Search Parents through TopLevelManager
689 if (SearchParent)
690 return TPM->findAnalysisPass(AID);
691
Devang Patel9d759b82006-12-09 00:09:12 +0000692 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000693}
694
Devang Patel991aeba2006-12-15 20:13:01 +0000695// Print list of passes that are last used by P.
696void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
697
698 std::vector<Pass *> LUses;
699
700 assert (TPM && "Top Level Manager is missing");
701 TPM->collectLastUses(LUses, P);
702
703 for (std::vector<Pass *>::iterator I = LUses.begin(),
704 E = LUses.end(); I != E; ++I) {
705 llvm::cerr << "--" << std::string(Offset*2, ' ');
706 (*I)->dumpPassStructure(0);
707 }
708}
709
710void PMDataManager::dumpPassArguments() const {
711 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
712 E = PassVector.end(); I != E; ++I) {
713 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
714 PMD->dumpPassArguments();
715 else
716 if (const PassInfo *PI = (*I)->getPassInfo())
717 if (!PI->isAnalysisGroup())
718 cerr << " -" << PI->getPassArgument();
719 }
720}
721
722void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
723 std::string &Msg2) const {
Devang Patelfd4184322007-01-17 20:33:36 +0000724 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000725 return;
726 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
727 cerr << Msg1;
728 cerr << P->getPassName();
729 cerr << Msg2;
730}
731
732void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
733 const std::vector<AnalysisID> &Set)
734 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000735 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000736 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
737 for (unsigned i = 0; i != Set.size(); ++i) {
738 if (i) cerr << ",";
739 cerr << " " << Set[i]->getPassName();
740 }
741 cerr << "\n";
742 }
743}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000744
Devang Patele7599552007-01-12 18:52:44 +0000745// Destructor
746PMDataManager::~PMDataManager() {
747
748 for (std::vector<Pass *>::iterator I = PassVector.begin(),
749 E = PassVector.end(); I != E; ++I)
750 delete *I;
751
752 PassVector.clear();
753}
754
Devang Patel9bdf7d42006-12-08 23:28:54 +0000755//===----------------------------------------------------------------------===//
756// NOTE: Is this the right place to define this method ?
757// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000758Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000759 return PM.findAnalysisPass(ID, dir);
760}
761
Devang Patela1514cb2006-12-07 19:39:39 +0000762//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000763// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000764
Devang Patel6e5a1132006-11-07 21:31:57 +0000765/// Execute all of the passes scheduled for execution by invoking
766/// runOnBasicBlock method. Keep track of whether any of the passes modifies
767/// the function, and if so, return true.
768bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000769BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000770
Reid Spencer5301e7c2007-01-30 20:08:39 +0000771 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000772 return false;
773
Devang Patele9585592006-12-08 01:38:28 +0000774 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000775
Devang Patel93a197c2006-12-14 00:08:04 +0000776 std::string Msg1 = "Executing Pass '";
777 std::string Msg3 = "' Made Modification '";
778
Devang Patel6e5a1132006-11-07 21:31:57 +0000779 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000780 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
781 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000782 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000783 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000784
Devang Patel200d3052006-12-13 23:50:44 +0000785 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000786 dumpPassInfo(BP, Msg1, Msg2);
787 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000788
Devang Patelabfbe3b2006-12-16 00:56:26 +0000789 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000790
Devang Patelabfbe3b2006-12-16 00:56:26 +0000791 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000792 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000793 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000794
795 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000796 dumpPassInfo(BP, Msg3, Msg2);
797 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000798
Devang Patelabfbe3b2006-12-16 00:56:26 +0000799 removeNotPreservedAnalysis(BP);
800 recordAvailableAnalysis(BP);
801 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +0000802 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000803 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000804}
805
Devang Patel475c4532006-12-08 00:59:05 +0000806// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000807inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000808 bool Changed = false;
809
Devang Patelabfbe3b2006-12-16 00:56:26 +0000810 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
811 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000812 Changed |= BP->doInitialization(M);
813 }
814
815 return Changed;
816}
817
Devang Patel67d6a5e2006-12-19 19:46:59 +0000818inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000819 bool Changed = false;
820
Devang Patelabfbe3b2006-12-16 00:56:26 +0000821 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
822 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000823 Changed |= BP->doFinalization(M);
824 }
825
826 return Changed;
827}
828
Devang Patel67d6a5e2006-12-19 19:46:59 +0000829inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000830 bool Changed = false;
831
Devang Patelabfbe3b2006-12-16 00:56:26 +0000832 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
833 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000834 Changed |= BP->doInitialization(F);
835 }
836
837 return Changed;
838}
839
Devang Patel67d6a5e2006-12-19 19:46:59 +0000840inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000841 bool Changed = false;
842
Devang Patelabfbe3b2006-12-16 00:56:26 +0000843 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
844 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000845 Changed |= BP->doFinalization(F);
846 }
847
848 return Changed;
849}
850
851
Devang Patela1514cb2006-12-07 19:39:39 +0000852//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000853// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000854
Devang Patel4e12f862006-11-08 10:44:40 +0000855/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +0000856FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000857 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000858 // FPM is the top level manager.
859 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000860
861 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +0000862 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +0000863 FPM->setResolver(AR);
864
Devang Patel1f653682006-12-08 18:57:16 +0000865 MP = P;
866}
867
Devang Patelb67904d2006-12-13 02:36:01 +0000868FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000869 delete FPM;
870}
871
Devang Patel4e12f862006-11-08 10:44:40 +0000872/// add - Add a pass to the queue of passes to run. This passes
873/// ownership of the Pass to the PassManager. When the
874/// PassManager_X is destroyed, the pass will be destroyed as well, so
875/// there is no need to delete the pass. (TODO delete passes.)
876/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +0000877void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000878 FPM->add(P);
879}
880
Devang Patel9f3083e2006-11-15 19:39:54 +0000881/// run - Execute all of the passes scheduled for execution. Keep
882/// track of whether any of the passes modifies the function, and if
883/// so, return true.
884///
Devang Patelb67904d2006-12-13 02:36:01 +0000885bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000886 std::string errstr;
887 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000888 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000889 abort();
890 }
Devang Patel272908d2006-12-08 22:57:48 +0000891 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000892}
893
894
Devang Patelff631ae2006-11-15 01:27:05 +0000895/// doInitialization - Run all of the initializers for the function passes.
896///
Devang Patelb67904d2006-12-13 02:36:01 +0000897bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000898 return FPM->doInitialization(*MP->getModule());
899}
900
901/// doFinalization - Run all of the initializers for the function passes.
902///
Devang Patelb67904d2006-12-13 02:36:01 +0000903bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000904 return FPM->doFinalization(*MP->getModule());
905}
906
Devang Patela1514cb2006-12-07 19:39:39 +0000907//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000908// FunctionPassManagerImpl implementation
909//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000910inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
911 bool Changed = false;
912
913 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
914 FPPassManager *FP = getContainedManager(Index);
915 Changed |= FP->doInitialization(M);
916 }
917
918 return Changed;
919}
920
921inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
922 bool Changed = false;
923
924 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
925 FPPassManager *FP = getContainedManager(Index);
926 Changed |= FP->doFinalization(M);
927 }
928
929 return Changed;
930}
931
932// Execute all the passes managed by this top level manager.
933// Return true if any function is modified by a pass.
934bool FunctionPassManagerImpl::run(Function &F) {
935
936 bool Changed = false;
937
938 TimingInfo::createTheTimeInfo();
939
940 dumpArguments();
941 dumpPasses();
942
Devang Patele3068402006-12-21 00:16:50 +0000943 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +0000944 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
945 FPPassManager *FP = getContainedManager(Index);
946 Changed |= FP->runOnFunction(F);
947 }
948 return Changed;
949}
950
951//===----------------------------------------------------------------------===//
952// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000953
Devang Patele7599552007-01-12 18:52:44 +0000954/// Print passes managed by this manager
955void FPPassManager::dumpPassStructure(unsigned Offset) {
956 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
957 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
958 FunctionPass *FP = getContainedPass(Index);
959 FP->dumpPassStructure(Offset + 1);
960 dumpLastUses(FP, Offset+1);
961 }
962}
963
964
Devang Patel0c2012f2006-11-07 21:49:50 +0000965/// Execute all of the passes scheduled for execution by invoking
966/// runOnFunction method. Keep track of whether any of the passes modifies
967/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000968bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000969
970 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +0000971
Reid Spencer5301e7c2007-01-30 20:08:39 +0000972 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000973 return false;
974
Devang Patel93a197c2006-12-14 00:08:04 +0000975 std::string Msg1 = "Executing Pass '";
976 std::string Msg3 = "' Made Modification '";
977
Devang Patelabfbe3b2006-12-16 00:56:26 +0000978 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
979 FunctionPass *FP = getContainedPass(Index);
980
Devang Patelf6d1d212006-12-14 00:25:06 +0000981 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000982 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +0000983
Devang Patel200d3052006-12-13 23:50:44 +0000984 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000985 dumpPassInfo(FP, Msg1, Msg2);
986 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000987
Devang Patelabfbe3b2006-12-16 00:56:26 +0000988 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +0000989
Devang Patelabfbe3b2006-12-16 00:56:26 +0000990 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +0000991 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000992 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +0000993
994 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000995 dumpPassInfo(FP, Msg3, Msg2);
996 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000997
Devang Patelabfbe3b2006-12-16 00:56:26 +0000998 removeNotPreservedAnalysis(FP);
999 recordAvailableAnalysis(FP);
1000 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +00001001 }
1002 return Changed;
1003}
1004
Devang Patel67d6a5e2006-12-19 19:46:59 +00001005bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001006
Devang Patel67d6a5e2006-12-19 19:46:59 +00001007 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001008
1009 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1010 this->runOnFunction(*I);
1011
1012 return Changed |= doFinalization(M);
1013}
1014
1015inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001016 bool Changed = false;
1017
Devang Patelabfbe3b2006-12-16 00:56:26 +00001018 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1019 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001020 Changed |= FP->doInitialization(M);
1021 }
1022
1023 return Changed;
1024}
1025
Devang Patel67d6a5e2006-12-19 19:46:59 +00001026inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001027 bool Changed = false;
1028
Devang Patelabfbe3b2006-12-16 00:56:26 +00001029 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1030 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001031 Changed |= FP->doFinalization(M);
1032 }
1033
Devang Patelff631ae2006-11-15 01:27:05 +00001034 return Changed;
1035}
1036
Devang Patela1514cb2006-12-07 19:39:39 +00001037//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001038// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001039
Devang Patel05e1a972006-11-07 22:03:15 +00001040/// Execute all of the passes scheduled for execution by invoking
1041/// runOnModule method. Keep track of whether any of the passes modifies
1042/// the module, and if so, return true.
1043bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001044MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001045 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001046
Devang Patel93a197c2006-12-14 00:08:04 +00001047 std::string Msg1 = "Executing Pass '";
1048 std::string Msg3 = "' Made Modification '";
1049
Devang Patelabfbe3b2006-12-16 00:56:26 +00001050 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1051 ModulePass *MP = getContainedPass(Index);
1052
Devang Patelf6d1d212006-12-14 00:25:06 +00001053 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001054 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001055
Devang Patel200d3052006-12-13 23:50:44 +00001056 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001057 dumpPassInfo(MP, Msg1, Msg2);
1058 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001059
Devang Patelabfbe3b2006-12-16 00:56:26 +00001060 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001061
Devang Patelabfbe3b2006-12-16 00:56:26 +00001062 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001063 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001064 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001065
1066 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001067 dumpPassInfo(MP, Msg3, Msg2);
1068 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001069
Devang Patelabfbe3b2006-12-16 00:56:26 +00001070 removeNotPreservedAnalysis(MP);
1071 recordAvailableAnalysis(MP);
1072 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001073 }
1074 return Changed;
1075}
1076
Devang Patela1514cb2006-12-07 19:39:39 +00001077//===----------------------------------------------------------------------===//
1078// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001079//
Devang Patelc290c8a2006-11-07 22:23:34 +00001080/// run - Execute all of the passes scheduled for execution. Keep track of
1081/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001082bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001083
Devang Patelc290c8a2006-11-07 22:23:34 +00001084 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001085
Devang Patelb8817b92006-12-14 00:59:42 +00001086 TimingInfo::createTheTimeInfo();
1087
Devang Patelcfd70c42006-12-13 22:10:00 +00001088 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001089 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001090
Devang Patele3068402006-12-21 00:16:50 +00001091 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001092 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1093 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001094 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001095 }
1096 return Changed;
1097}
Devang Patel376fefa2006-11-08 10:29:57 +00001098
Devang Patela1514cb2006-12-07 19:39:39 +00001099//===----------------------------------------------------------------------===//
1100// PassManager implementation
1101
Devang Patel376fefa2006-11-08 10:29:57 +00001102/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001103PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001104 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001105 // PM is the top level manager
1106 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001107}
1108
Devang Patelb67904d2006-12-13 02:36:01 +00001109PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001110 delete PM;
1111}
1112
Devang Patel376fefa2006-11-08 10:29:57 +00001113/// add - Add a pass to the queue of passes to run. This passes ownership of
1114/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1115/// will be destroyed as well, so there is no need to delete the pass. This
1116/// implies that all passes MUST be allocated with 'new'.
1117void
Devang Patelb67904d2006-12-13 02:36:01 +00001118PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001119 PM->add(P);
1120}
1121
1122/// run - Execute all of the passes scheduled for execution. Keep track of
1123/// whether any of the passes modifies the module, and if so, return true.
1124bool
Devang Patelb67904d2006-12-13 02:36:01 +00001125PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001126 return PM->run(M);
1127}
1128
Devang Patelb8817b92006-12-14 00:59:42 +00001129//===----------------------------------------------------------------------===//
1130// TimingInfo Class - This class is used to calculate information about the
1131// amount of time each pass takes to execute. This only happens with
1132// -time-passes is enabled on the command line.
1133//
1134bool llvm::TimePassesIsEnabled = false;
1135static cl::opt<bool,true>
1136EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1137 cl::desc("Time each pass, printing elapsed time for each on exit"));
1138
1139// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1140// a non null value (if the -time-passes option is enabled) or it leaves it
1141// null. It may be called multiple times.
1142void TimingInfo::createTheTimeInfo() {
1143 if (!TimePassesIsEnabled || TheTimeInfo) return;
1144
1145 // Constructed the first time this is called, iff -time-passes is enabled.
1146 // This guarantees that the object will be constructed before static globals,
1147 // thus it will be destroyed before them.
1148 static ManagedStatic<TimingInfo> TTI;
1149 TheTimeInfo = &*TTI;
1150}
1151
Devang Patel1c3633e2007-01-29 23:10:37 +00001152/// If TimingInfo is enabled then start pass timer.
1153void StartPassTimer(Pass *P) {
1154 if (TheTimeInfo)
1155 TheTimeInfo->passStarted(P);
1156}
1157
1158/// If TimingInfo is enabled then stop pass timer.
1159void StopPassTimer(Pass *P) {
1160 if (TheTimeInfo)
1161 TheTimeInfo->passEnded(P);
1162}
1163
Devang Patel1c56a632007-01-08 19:29:38 +00001164//===----------------------------------------------------------------------===//
1165// PMStack implementation
1166//
Devang Patelad98d232007-01-11 22:15:30 +00001167
Devang Patel1c56a632007-01-08 19:29:38 +00001168// Pop Pass Manager from the stack and clear its analysis info.
1169void PMStack::pop() {
1170
1171 PMDataManager *Top = this->top();
1172 Top->initializeAnalysisInfo();
1173
1174 S.pop_back();
1175}
1176
1177// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001178void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001179
Devang Patel15701b52007-01-11 00:19:00 +00001180 PMDataManager *Top = NULL;
1181 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1182 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001183
Devang Patel15701b52007-01-11 00:19:00 +00001184 if (this->empty()) {
1185 Top = PM;
1186 }
1187 else {
1188 Top = this->top();
1189 PMTopLevelManager *TPM = Top->getTopLevelManager();
1190
1191 assert (TPM && "Unable to find top level manager");
1192 TPM->addIndirectPassManager(PM);
1193 PM->setTopLevelManager(TPM);
1194 }
1195
1196 AnalysisResolver *AR = new AnalysisResolver(*Top);
1197 P->setResolver(AR);
1198
1199 S.push_back(PM);
1200}
1201
1202// Dump content of the pass manager stack.
1203void PMStack::dump() {
1204 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1205 E = S.end(); I != E; ++I) {
1206 Pass *P = dynamic_cast<Pass *>(*I);
1207 printf ("%s ", P->getPassName());
1208 }
1209 if (!S.empty())
1210 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001211}
1212
1213// Walk Pass Manager stack and set LastUse markers if any
1214// manager is transfering this priviledge to its parent manager
1215void PMStack::handleLastUserOverflow() {
1216
1217 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1218
1219 PMDataManager *Child = *I++;
1220 if (I != E) {
1221 PMDataManager *Parent = *I++;
1222 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1223 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1224 if (!TLU.empty()) {
1225 Pass *P = dynamic_cast<Pass *>(Parent);
1226 TPM->setLastUser(TLU, P);
Devang Patel4d06ace2007-02-07 19:37:53 +00001227 TLU.clear();
Devang Patel1c56a632007-01-08 19:29:38 +00001228 }
1229 }
1230 }
1231}
1232
1233/// Find appropriate Module Pass Manager in the PM Stack and
1234/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001235void ModulePass::assignPassManager(PMStack &PMS,
1236 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001237
Devang Patel1c56a632007-01-08 19:29:38 +00001238 // Find Module Pass Manager
1239 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001240 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1241 if (TopPMType == PreferredType)
1242 break; // We found desired pass manager
1243 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001244 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001245 else
1246 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001247 }
1248
Devang Patel23f8aa92007-01-17 21:19:23 +00001249 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001250}
1251
Devang Patel3312f752007-01-16 21:43:18 +00001252/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1253/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001254void FunctionPass::assignPassManager(PMStack &PMS,
1255 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001256
Devang Patel15701b52007-01-11 00:19:00 +00001257 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001258 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001259 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1260 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001261 else
Devang Patel3312f752007-01-16 21:43:18 +00001262 break;
1263 }
1264 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1265
1266 // Create new Function Pass Manager
1267 if (!FPP) {
1268 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1269 PMDataManager *PMD = PMS.top();
1270
1271 // [1] Create new Function Pass Manager
1272 FPP = new FPPassManager(PMD->getDepth() + 1);
1273
1274 // [2] Set up new manager's top level manager
1275 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1276 TPM->addIndirectPassManager(FPP);
1277
1278 // [3] Assign manager to manage this new manager. This may create
1279 // and push new managers into PMS
1280 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001281
1282 // If Call Graph Pass Manager is active then use it to manage
1283 // this new Function Pass manager.
1284 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1285 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1286 else
1287 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001288
1289 // [4] Push new manager into PMS
1290 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001291 }
1292
Devang Patel3312f752007-01-16 21:43:18 +00001293 // Assign FPP as the manager of this pass.
1294 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001295}
1296
Devang Patel3312f752007-01-16 21:43:18 +00001297/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001298/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001299void BasicBlockPass::assignPassManager(PMStack &PMS,
1300 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001301
1302 BBPassManager *BBP = NULL;
1303
Devang Patel15701b52007-01-11 00:19:00 +00001304 // Basic Pass Manager is a leaf pass manager. It does not handle
1305 // any other pass manager.
1306 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001307 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001308 }
1309
Devang Patel3312f752007-01-16 21:43:18 +00001310 // If leaf manager is not Basic Block Pass manager then create new
1311 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001312
Devang Patel3312f752007-01-16 21:43:18 +00001313 if (!BBP) {
1314 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1315 PMDataManager *PMD = PMS.top();
1316
1317 // [1] Create new Basic Block Manager
1318 BBP = new BBPassManager(PMD->getDepth() + 1);
1319
1320 // [2] Set up new manager's top level manager
1321 // Basic Block Pass Manager does not live by itself
1322 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1323 TPM->addIndirectPassManager(BBP);
1324
Devang Patel15701b52007-01-11 00:19:00 +00001325 // [3] Assign manager to manage this new manager. This may create
1326 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001327 Pass *P = dynamic_cast<Pass *>(BBP);
1328 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001329
Devang Patel3312f752007-01-16 21:43:18 +00001330 // [4] Push new manager into PMS
1331 PMS.push(BBP);
1332 }
Devang Patel1c56a632007-01-08 19:29:38 +00001333
Devang Patel3312f752007-01-16 21:43:18 +00001334 // Assign BBP as the manager of this pass.
1335 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001336}
1337
Devang Patelc6b5a552007-01-05 20:16:23 +00001338