blob: 02938710fac8e3aff6ecd128ca7e7926876f4ecb [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
101 virtual PassManagerType getPassManagerType() {
102 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 Patel67d6a5e2006-12-19 19:46:59 +0000160 }
Devang Patel0f080042007-01-12 17:23:48 +0000161
Devang Patel67d6a5e2006-12-19 19:46:59 +0000162 }
163
164 FPPassManager *getContainedManager(unsigned N) {
165 assert ( N < PassManagers.size() && "Pass number out of range!");
166 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
167 return FP;
168 }
169
Devang Patel67d6a5e2006-12-19 19:46:59 +0000170};
171
172//===----------------------------------------------------------------------===//
173// MPPassManager
174//
175/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000176/// It batches all Module passes passes and function pass managers together and
177/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000178class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000179
180public:
Devang Patel0f080042007-01-12 17:23:48 +0000181 MPPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000182
183 /// run - Execute all of the passes scheduled for execution. Keep track of
184 /// whether any of the passes modifies the module, and if so, return true.
185 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000186
Devang Patelf9d96b92006-12-07 19:57:52 +0000187 /// Pass Manager itself does not invalidate any analysis info.
188 void getAnalysisUsage(AnalysisUsage &Info) const {
189 Info.setPreservesAll();
190 }
191
Devang Patele3858e62007-02-01 22:08:25 +0000192 virtual const char *getPassName() const {
193 return "Module Pass Manager";
194 }
195
Devang Pateleda56172006-12-12 23:34:33 +0000196 // Print passes managed by this manager
197 void dumpPassStructure(unsigned Offset) {
198 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000199 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
200 ModulePass *MP = getContainedPass(Index);
201 MP->dumpPassStructure(Offset + 1);
202 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000203 }
204 }
205
Devang Patelabfbe3b2006-12-16 00:56:26 +0000206 ModulePass *getContainedPass(unsigned N) {
207 assert ( N < PassVector.size() && "Pass number out of range!");
208 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
209 return MP;
210 }
211
Devang Patel3b3f8992007-01-11 01:10:25 +0000212 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
Devang Patelca58e352006-11-08 10:05:38 +0000213};
214
Devang Patel10c2ca62006-12-12 22:47:13 +0000215//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000216// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000217//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000218/// PassManagerImpl manages MPPassManagers
219class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000220 public PMDataManager,
221 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000222
223public:
224
Devang Patel4268fc02007-01-16 02:00:38 +0000225 PassManagerImpl(int Depth) : PMDataManager(Depth),
226 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000227
Devang Patel376fefa2006-11-08 10:29:57 +0000228 /// add - Add a pass to the queue of passes to run. This passes ownership of
229 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
230 /// will be destroyed as well, so there is no need to delete the pass. This
231 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000232 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000233 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000234 }
Devang Patel376fefa2006-11-08 10:29:57 +0000235
236 /// run - Execute all of the passes scheduled for execution. Keep track of
237 /// whether any of the passes modifies the module, and if so, return true.
238 bool run(Module &M);
239
Devang Patelf9d96b92006-12-07 19:57:52 +0000240 /// Pass Manager itself does not invalidate any analysis info.
241 void getAnalysisUsage(AnalysisUsage &Info) const {
242 Info.setPreservesAll();
243 }
244
Devang Patelabcd1d32006-12-07 21:27:23 +0000245 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000246
Devang Patelfa971cd2006-12-08 23:57:43 +0000247 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000248
249 // P is a immutable pass and it will be managed by this
250 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000251 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000252 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000253 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000254 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000255 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000256 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000257 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000258 }
Devang Patel0f080042007-01-12 17:23:48 +0000259
Devang Patelabcd1d32006-12-07 21:27:23 +0000260 }
261
Devang Patel67d6a5e2006-12-19 19:46:59 +0000262 MPPassManager *getContainedManager(unsigned N) {
263 assert ( N < PassManagers.size() && "Pass number out of range!");
264 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
265 return MP;
266 }
267
Devang Patel376fefa2006-11-08 10:29:57 +0000268};
269
Devang Patel1c3633e2007-01-29 23:10:37 +0000270} // End of llvm namespace
271
272namespace {
273
274//===----------------------------------------------------------------------===//
275// TimingInfo Class - This class is used to calculate information about the
276// amount of time each pass takes to execute. This only happens when
277// -time-passes is enabled on the command line.
278//
279
280class VISIBILITY_HIDDEN TimingInfo {
281 std::map<Pass*, Timer> TimingData;
282 TimerGroup TG;
283
284public:
285 // Use 'create' member to get this.
286 TimingInfo() : TG("... Pass execution timing report ...") {}
287
288 // TimingDtor - Print out information about timing information
289 ~TimingInfo() {
290 // Delete all of the timers...
291 TimingData.clear();
292 // TimerGroup is deleted next, printing the report.
293 }
294
295 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
296 // to a non null value (if the -time-passes option is enabled) or it leaves it
297 // null. It may be called multiple times.
298 static void createTheTimeInfo();
299
300 void passStarted(Pass *P) {
301
302 if (dynamic_cast<PMDataManager *>(P))
303 return;
304
305 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
306 if (I == TimingData.end())
307 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
308 I->second.startTimer();
309 }
310 void passEnded(Pass *P) {
311
312 if (dynamic_cast<PMDataManager *>(P))
313 return;
314
315 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
316 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
317 I->second.stopTimer();
318 }
319};
320
Devang Patelb8817b92006-12-14 00:59:42 +0000321static TimingInfo *TheTimeInfo;
322
Devang Patel1c3633e2007-01-29 23:10:37 +0000323} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000324
Devang Patela1514cb2006-12-07 19:39:39 +0000325//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000326// PMTopLevelManager implementation
327
Devang Patel4268fc02007-01-16 02:00:38 +0000328/// Initialize top level manager. Create first pass manager.
329PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
330
331 if (t == TLM_Pass) {
332 MPPassManager *MPP = new MPPassManager(1);
333 MPP->setTopLevelManager(this);
334 addPassManager(MPP);
335 activeStack.push(MPP);
336 }
337 else if (t == TLM_Function) {
338 FPPassManager *FPP = new FPPassManager(1);
339 FPP->setTopLevelManager(this);
340 addPassManager(FPP);
341 activeStack.push(FPP);
342 }
343}
344
Devang Patelafb1f3622006-12-12 22:35:25 +0000345/// Set pass P as the last user of the given analysis passes.
346void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
347 Pass *P) {
348
349 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
350 E = AnalysisPasses.end(); I != E; ++I) {
351 Pass *AP = *I;
352 LastUser[AP] = P;
353 // If AP is the last user of other passes then make P last user of
354 // such passes.
355 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
356 LUE = LastUser.end(); LUI != LUE; ++LUI) {
357 if (LUI->second == AP)
358 LastUser[LUI->first] = P;
359 }
360 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000361}
362
363/// Collect passes whose last user is P
364void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
365 Pass *P) {
366 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
367 LUE = LastUser.end(); LUI != LUE; ++LUI)
368 if (LUI->second == P)
369 LastUses.push_back(LUI->first);
370}
371
372/// Schedule pass P for execution. Make sure that passes required by
373/// P are run before P is run. Update analysis info maintained by
374/// the manager. Remove dead passes. This is a recursive function.
375void PMTopLevelManager::schedulePass(Pass *P) {
376
Devang Patel3312f752007-01-16 21:43:18 +0000377 // TODO : Allocate function manager for this pass, other wise required set
378 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000379
Devang Patel3f806962007-02-05 19:34:17 +0000380 // If this Analysis is already requested by one of the previous pass
381 // and it is still available then do not insert new pass in the queue again.
382 if (findAnalysisPass(P->getPassInfo()))
383 return;
384
Devang Patelafb1f3622006-12-12 22:35:25 +0000385 AnalysisUsage AnUsage;
386 P->getAnalysisUsage(AnUsage);
387 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
388 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
389 E = RequiredSet.end(); I != E; ++I) {
390
391 Pass *AnalysisPass = findAnalysisPass(*I);
392 if (!AnalysisPass) {
393 // Schedule this analysis run first.
394 AnalysisPass = (*I)->createPass();
395 schedulePass(AnalysisPass);
396 }
397 }
398
399 // Now all required passes are available.
400 addTopLevelPass(P);
401}
402
403/// Find the pass that implements Analysis AID. Search immutable
404/// passes and all pass managers. If desired pass is not found
405/// then return NULL.
406Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
407
408 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000409 // Check pass managers
410 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
411 E = PassManagers.end(); P == NULL && I != E; ++I) {
412 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
413 assert(PMD && "This is not a PassManager");
414 P = PMD->findAnalysisPass(AID, false);
415 }
416
417 // Check other pass managers
418 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
419 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
420 P = (*I)->findAnalysisPass(AID, false);
421
Devang Patelafb1f3622006-12-12 22:35:25 +0000422 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
423 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
424 const PassInfo *PI = (*I)->getPassInfo();
425 if (PI == AID)
426 P = *I;
427
428 // If Pass not found then check the interfaces implemented by Immutable Pass
429 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000430 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
431 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
432 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000433 }
434 }
435
Devang Patelafb1f3622006-12-12 22:35:25 +0000436 return P;
437}
438
Devang Pateleda56172006-12-12 23:34:33 +0000439// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000440void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000441
Devang Patelfd4184322007-01-17 20:33:36 +0000442 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000443 return;
444
Devang Pateleda56172006-12-12 23:34:33 +0000445 // Print out the immutable passes
446 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
447 ImmutablePasses[i]->dumpPassStructure(0);
448 }
449
Devang Patel991aeba2006-12-15 20:13:01 +0000450 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000451 E = PassManagers.end(); I != E; ++I)
452 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000453}
454
Devang Patel991aeba2006-12-15 20:13:01 +0000455void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000456
Devang Patelfd4184322007-01-17 20:33:36 +0000457 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000458 return;
459
460 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000461 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000462 E = PassManagers.end(); I != E; ++I) {
463 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
464 assert(PMD && "This is not a PassManager");
465 PMD->dumpPassArguments();
466 }
467 cerr << "\n";
468}
469
Devang Patele3068402006-12-21 00:16:50 +0000470void PMTopLevelManager::initializeAllAnalysisInfo() {
471
472 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
473 E = PassManagers.end(); I != E; ++I) {
474 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
475 assert(PMD && "This is not a PassManager");
476 PMD->initializeAnalysisInfo();
477 }
478
479 // Initailize other pass managers
480 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
481 E = IndirectPassManagers.end(); I != E; ++I)
482 (*I)->initializeAnalysisInfo();
483}
484
Devang Patele7599552007-01-12 18:52:44 +0000485/// Destructor
486PMTopLevelManager::~PMTopLevelManager() {
487 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
488 E = PassManagers.end(); I != E; ++I)
489 delete *I;
490
491 for (std::vector<ImmutablePass *>::iterator
492 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
493 delete *I;
494
495 PassManagers.clear();
496}
497
Devang Patelafb1f3622006-12-12 22:35:25 +0000498//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000499// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000500
Devang Pateld65e9e92006-11-08 01:31:28 +0000501/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000502/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000503bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000504
Devang Patel8f677ce2006-12-07 18:47:25 +0000505 // TODO
506 // If this pass is not preserving information that is required by a
507 // pass maintained by higher level pass manager then do not insert
508 // this pass into current manager. Use new manager. For example,
509 // For example, If FunctionPass F is not preserving ModulePass Info M1
510 // that is used by another ModulePass M2 then do not insert F in
511 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000512 return true;
513}
514
Devang Patel643676c2006-11-11 01:10:19 +0000515/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000516void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000517
Devang Patel643676c2006-11-11 01:10:19 +0000518 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000519 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000520
Devang Patele9976aa2006-12-07 19:33:53 +0000521 //This pass is the current implementation of all of the interfaces it
522 //implements as well.
523 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
524 for (unsigned i = 0, e = II.size(); i != e; ++i)
525 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000526 }
527}
528
Devang Patelf68a3492006-11-07 22:35:17 +0000529/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000530void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000531 AnalysisUsage AnUsage;
532 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000533
Devang Patel2e169c32006-12-07 20:03:49 +0000534 if (AnUsage.getPreservesAll())
535 return;
536
537 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000538 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000539 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000540 std::map<AnalysisID, Pass*>::iterator Info = I++;
541 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000542 PreservedSet.end()) {
543 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000544 if (!dynamic_cast<ImmutablePass*>(Info->second))
545 AvailableAnalysis.erase(Info);
546 }
Devang Patel349170f2006-11-11 01:24:55 +0000547 }
Devang Patelf68a3492006-11-07 22:35:17 +0000548}
549
Devang Patelca189262006-11-14 03:05:08 +0000550/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000551void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000552
553 std::vector<Pass *> DeadPasses;
554 TPM->collectLastUses(DeadPasses, P);
555
556 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
557 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000558
559 std::string Msg1 = " Freeing Pass '";
560 dumpPassInfo(*I, Msg1, Msg);
561
Devang Patelb8817b92006-12-14 00:59:42 +0000562 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000563 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000564 if (TheTimeInfo) TheTimeInfo->passEnded(P);
565
Devang Patel17ad0962006-12-08 00:37:52 +0000566 std::map<AnalysisID, Pass*>::iterator Pos =
567 AvailableAnalysis.find((*I)->getPassInfo());
568
Devang Patel475c4532006-12-08 00:59:05 +0000569 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000570 if (Pos != AvailableAnalysis.end())
571 AvailableAnalysis.erase(Pos);
572 }
Devang Patelca189262006-11-14 03:05:08 +0000573}
574
Devang Patel8f677ce2006-12-07 18:47:25 +0000575/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000576/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000577void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000578 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000579
Devang Pateld440cd92006-12-08 23:53:00 +0000580 // This manager is going to manage pass P. Set up analysis resolver
581 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000582 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000583 P->setResolver(AR);
584
Devang Patel90b05e02006-11-11 02:04:19 +0000585 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000586
587 // At the moment, this pass is the last user of all required passes.
588 std::vector<Pass *> LastUses;
589 std::vector<Pass *> RequiredPasses;
590 unsigned PDepth = this->getDepth();
591
592 collectRequiredAnalysisPasses(RequiredPasses, P);
593 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
594 E = RequiredPasses.end(); I != E; ++I) {
595 Pass *PRequired = *I;
596 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000597
598 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
599 RDepth = DM.getDepth();
600
Devang Patelbc03f132006-12-07 23:55:10 +0000601 if (PDepth == RDepth)
602 LastUses.push_back(PRequired);
603 else if (PDepth > RDepth) {
604 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000605 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000606 } else {
607 // Note : This feature is not yet implemented
608 assert (0 &&
609 "Unable to handle Pass that requires lower level Analysis pass");
610 }
611 }
612
Devang Patel39786a92007-01-15 20:31:54 +0000613 // Set P as P's last user until someone starts using P.
614 // However, if P is a Pass Manager then it does not need
615 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000616 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000617 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000618 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000619
Devang Patel17bff0d2006-12-07 22:09:36 +0000620 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000621 // Remove the analysis not preserved by this pass
622 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000623 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000624 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000625
626 // Add pass
627 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000628}
629
Devang Patel1d6267c2006-12-07 23:05:44 +0000630/// Populate RequiredPasses with the analysis pass that are required by
631/// pass P.
632void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
633 Pass *P) {
634 AnalysisUsage AnUsage;
635 P->getAnalysisUsage(AnUsage);
636 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
637 for (std::vector<AnalysisID>::const_iterator
638 I = RequiredSet.begin(), E = RequiredSet.end();
639 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000640 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000641 assert (AnalysisPass && "Analysis pass is not available");
642 RP.push_back(AnalysisPass);
643 }
Devang Patelf58183d2006-12-12 23:09:32 +0000644
645 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
646 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
647 E = IDs.end(); I != E; ++I) {
648 Pass *AnalysisPass = findAnalysisPass(*I, true);
649 assert (AnalysisPass && "Analysis pass is not available");
650 RP.push_back(AnalysisPass);
651 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000652}
653
Devang Patel07f4f582006-11-14 21:49:36 +0000654// All Required analyses should be available to the pass as it runs! Here
655// we fill in the AnalysisImpls member of the pass so that it can
656// successfully use the getAnalysis() method to retrieve the
657// implementations it needs.
658//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000659void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000660 AnalysisUsage AnUsage;
661 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000662
663 for (std::vector<const PassInfo *>::const_iterator
664 I = AnUsage.getRequiredSet().begin(),
665 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000666 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000667 if (Impl == 0)
668 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000669 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000670 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000671 }
672}
673
Devang Patel640c5bb2006-12-08 22:30:11 +0000674/// Find the pass that implements Analysis AID. If desired pass is not found
675/// then return NULL.
676Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
677
678 // Check if AvailableAnalysis map has one entry.
679 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
680
681 if (I != AvailableAnalysis.end())
682 return I->second;
683
684 // Search Parents through TopLevelManager
685 if (SearchParent)
686 return TPM->findAnalysisPass(AID);
687
Devang Patel9d759b82006-12-09 00:09:12 +0000688 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000689}
690
Devang Patel991aeba2006-12-15 20:13:01 +0000691// Print list of passes that are last used by P.
692void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
693
694 std::vector<Pass *> LUses;
695
696 assert (TPM && "Top Level Manager is missing");
697 TPM->collectLastUses(LUses, P);
698
699 for (std::vector<Pass *>::iterator I = LUses.begin(),
700 E = LUses.end(); I != E; ++I) {
701 llvm::cerr << "--" << std::string(Offset*2, ' ');
702 (*I)->dumpPassStructure(0);
703 }
704}
705
706void PMDataManager::dumpPassArguments() const {
707 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
708 E = PassVector.end(); I != E; ++I) {
709 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
710 PMD->dumpPassArguments();
711 else
712 if (const PassInfo *PI = (*I)->getPassInfo())
713 if (!PI->isAnalysisGroup())
714 cerr << " -" << PI->getPassArgument();
715 }
716}
717
718void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
719 std::string &Msg2) const {
Devang Patelfd4184322007-01-17 20:33:36 +0000720 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000721 return;
722 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
723 cerr << Msg1;
724 cerr << P->getPassName();
725 cerr << Msg2;
726}
727
728void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
729 const std::vector<AnalysisID> &Set)
730 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000731 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000732 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
733 for (unsigned i = 0; i != Set.size(); ++i) {
734 if (i) cerr << ",";
735 cerr << " " << Set[i]->getPassName();
736 }
737 cerr << "\n";
738 }
739}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000740
Devang Patele7599552007-01-12 18:52:44 +0000741// Destructor
742PMDataManager::~PMDataManager() {
743
744 for (std::vector<Pass *>::iterator I = PassVector.begin(),
745 E = PassVector.end(); I != E; ++I)
746 delete *I;
747
748 PassVector.clear();
749}
750
Devang Patel9bdf7d42006-12-08 23:28:54 +0000751//===----------------------------------------------------------------------===//
752// NOTE: Is this the right place to define this method ?
753// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000754Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000755 return PM.findAnalysisPass(ID, dir);
756}
757
Devang Patela1514cb2006-12-07 19:39:39 +0000758//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000759// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000760
Devang Patel6e5a1132006-11-07 21:31:57 +0000761/// Execute all of the passes scheduled for execution by invoking
762/// runOnBasicBlock method. Keep track of whether any of the passes modifies
763/// the function, and if so, return true.
764bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000765BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000766
Reid Spencer5301e7c2007-01-30 20:08:39 +0000767 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000768 return false;
769
Devang Patele9585592006-12-08 01:38:28 +0000770 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000771
Devang Patel93a197c2006-12-14 00:08:04 +0000772 std::string Msg1 = "Executing Pass '";
773 std::string Msg3 = "' Made Modification '";
774
Devang Patel6e5a1132006-11-07 21:31:57 +0000775 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000776 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
777 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000778 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000779 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000780
Devang Patel200d3052006-12-13 23:50:44 +0000781 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000782 dumpPassInfo(BP, Msg1, Msg2);
783 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000784
Devang Patelabfbe3b2006-12-16 00:56:26 +0000785 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000786
Devang Patelabfbe3b2006-12-16 00:56:26 +0000787 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000788 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000789 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000790
791 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000792 dumpPassInfo(BP, Msg3, Msg2);
793 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000794
Devang Patelabfbe3b2006-12-16 00:56:26 +0000795 removeNotPreservedAnalysis(BP);
796 recordAvailableAnalysis(BP);
797 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +0000798 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000799 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000800}
801
Devang Patel475c4532006-12-08 00:59:05 +0000802// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000803inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000804 bool Changed = false;
805
Devang Patelabfbe3b2006-12-16 00:56:26 +0000806 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
807 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000808 Changed |= BP->doInitialization(M);
809 }
810
811 return Changed;
812}
813
Devang Patel67d6a5e2006-12-19 19:46:59 +0000814inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000815 bool Changed = false;
816
Devang Patelabfbe3b2006-12-16 00:56:26 +0000817 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
818 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000819 Changed |= BP->doFinalization(M);
820 }
821
822 return Changed;
823}
824
Devang Patel67d6a5e2006-12-19 19:46:59 +0000825inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000826 bool Changed = false;
827
Devang Patelabfbe3b2006-12-16 00:56:26 +0000828 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
829 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000830 Changed |= BP->doInitialization(F);
831 }
832
833 return Changed;
834}
835
Devang Patel67d6a5e2006-12-19 19:46:59 +0000836inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000837 bool Changed = false;
838
Devang Patelabfbe3b2006-12-16 00:56:26 +0000839 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
840 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000841 Changed |= BP->doFinalization(F);
842 }
843
844 return Changed;
845}
846
847
Devang Patela1514cb2006-12-07 19:39:39 +0000848//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000849// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000850
Devang Patel4e12f862006-11-08 10:44:40 +0000851/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +0000852FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000853 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000854 // FPM is the top level manager.
855 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000856
857 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +0000858 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +0000859 FPM->setResolver(AR);
860
Devang Patel1f653682006-12-08 18:57:16 +0000861 MP = P;
862}
863
Devang Patelb67904d2006-12-13 02:36:01 +0000864FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000865 delete FPM;
866}
867
Devang Patel4e12f862006-11-08 10:44:40 +0000868/// add - Add a pass to the queue of passes to run. This passes
869/// ownership of the Pass to the PassManager. When the
870/// PassManager_X is destroyed, the pass will be destroyed as well, so
871/// there is no need to delete the pass. (TODO delete passes.)
872/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +0000873void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000874 FPM->add(P);
875}
876
Devang Patel9f3083e2006-11-15 19:39:54 +0000877/// run - Execute all of the passes scheduled for execution. Keep
878/// track of whether any of the passes modifies the function, and if
879/// so, return true.
880///
Devang Patelb67904d2006-12-13 02:36:01 +0000881bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000882 std::string errstr;
883 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000884 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000885 abort();
886 }
Devang Patel272908d2006-12-08 22:57:48 +0000887 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000888}
889
890
Devang Patelff631ae2006-11-15 01:27:05 +0000891/// doInitialization - Run all of the initializers for the function passes.
892///
Devang Patelb67904d2006-12-13 02:36:01 +0000893bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000894 return FPM->doInitialization(*MP->getModule());
895}
896
897/// doFinalization - Run all of the initializers for the function passes.
898///
Devang Patelb67904d2006-12-13 02:36:01 +0000899bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000900 return FPM->doFinalization(*MP->getModule());
901}
902
Devang Patela1514cb2006-12-07 19:39:39 +0000903//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000904// FunctionPassManagerImpl implementation
905//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000906inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
907 bool Changed = false;
908
909 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
910 FPPassManager *FP = getContainedManager(Index);
911 Changed |= FP->doInitialization(M);
912 }
913
914 return Changed;
915}
916
917inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
918 bool Changed = false;
919
920 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
921 FPPassManager *FP = getContainedManager(Index);
922 Changed |= FP->doFinalization(M);
923 }
924
925 return Changed;
926}
927
928// Execute all the passes managed by this top level manager.
929// Return true if any function is modified by a pass.
930bool FunctionPassManagerImpl::run(Function &F) {
931
932 bool Changed = false;
933
934 TimingInfo::createTheTimeInfo();
935
936 dumpArguments();
937 dumpPasses();
938
Devang Patele3068402006-12-21 00:16:50 +0000939 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +0000940 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
941 FPPassManager *FP = getContainedManager(Index);
942 Changed |= FP->runOnFunction(F);
943 }
944 return Changed;
945}
946
947//===----------------------------------------------------------------------===//
948// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000949
Devang Patele7599552007-01-12 18:52:44 +0000950/// Print passes managed by this manager
951void FPPassManager::dumpPassStructure(unsigned Offset) {
952 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
953 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
954 FunctionPass *FP = getContainedPass(Index);
955 FP->dumpPassStructure(Offset + 1);
956 dumpLastUses(FP, Offset+1);
957 }
958}
959
960
Devang Patel0c2012f2006-11-07 21:49:50 +0000961/// Execute all of the passes scheduled for execution by invoking
962/// runOnFunction method. Keep track of whether any of the passes modifies
963/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000964bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000965
966 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +0000967
Reid Spencer5301e7c2007-01-30 20:08:39 +0000968 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000969 return false;
970
Devang Patel93a197c2006-12-14 00:08:04 +0000971 std::string Msg1 = "Executing Pass '";
972 std::string Msg3 = "' Made Modification '";
973
Devang Patelabfbe3b2006-12-16 00:56:26 +0000974 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
975 FunctionPass *FP = getContainedPass(Index);
976
Devang Patelf6d1d212006-12-14 00:25:06 +0000977 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000978 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +0000979
Devang Patel200d3052006-12-13 23:50:44 +0000980 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000981 dumpPassInfo(FP, Msg1, Msg2);
982 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000983
Devang Patelabfbe3b2006-12-16 00:56:26 +0000984 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +0000985
Devang Patelabfbe3b2006-12-16 00:56:26 +0000986 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +0000987 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000988 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +0000989
990 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000991 dumpPassInfo(FP, Msg3, Msg2);
992 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000993
Devang Patelabfbe3b2006-12-16 00:56:26 +0000994 removeNotPreservedAnalysis(FP);
995 recordAvailableAnalysis(FP);
996 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +0000997 }
998 return Changed;
999}
1000
Devang Patel67d6a5e2006-12-19 19:46:59 +00001001bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001002
Devang Patel67d6a5e2006-12-19 19:46:59 +00001003 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001004
1005 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1006 this->runOnFunction(*I);
1007
1008 return Changed |= doFinalization(M);
1009}
1010
1011inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001012 bool Changed = false;
1013
Devang Patelabfbe3b2006-12-16 00:56:26 +00001014 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1015 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001016 Changed |= FP->doInitialization(M);
1017 }
1018
1019 return Changed;
1020}
1021
Devang Patel67d6a5e2006-12-19 19:46:59 +00001022inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001023 bool Changed = false;
1024
Devang Patelabfbe3b2006-12-16 00:56:26 +00001025 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1026 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001027 Changed |= FP->doFinalization(M);
1028 }
1029
Devang Patelff631ae2006-11-15 01:27:05 +00001030 return Changed;
1031}
1032
Devang Patela1514cb2006-12-07 19:39:39 +00001033//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001034// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001035
Devang Patel05e1a972006-11-07 22:03:15 +00001036/// Execute all of the passes scheduled for execution by invoking
1037/// runOnModule method. Keep track of whether any of the passes modifies
1038/// the module, and if so, return true.
1039bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001040MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001041 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001042
Devang Patel93a197c2006-12-14 00:08:04 +00001043 std::string Msg1 = "Executing Pass '";
1044 std::string Msg3 = "' Made Modification '";
1045
Devang Patelabfbe3b2006-12-16 00:56:26 +00001046 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1047 ModulePass *MP = getContainedPass(Index);
1048
Devang Patelf6d1d212006-12-14 00:25:06 +00001049 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001050 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001051
Devang Patel200d3052006-12-13 23:50:44 +00001052 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001053 dumpPassInfo(MP, Msg1, Msg2);
1054 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001055
Devang Patelabfbe3b2006-12-16 00:56:26 +00001056 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001057
Devang Patelabfbe3b2006-12-16 00:56:26 +00001058 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001059 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001060 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001061
1062 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001063 dumpPassInfo(MP, Msg3, Msg2);
1064 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001065
Devang Patelabfbe3b2006-12-16 00:56:26 +00001066 removeNotPreservedAnalysis(MP);
1067 recordAvailableAnalysis(MP);
1068 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001069 }
1070 return Changed;
1071}
1072
Devang Patela1514cb2006-12-07 19:39:39 +00001073//===----------------------------------------------------------------------===//
1074// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001075//
Devang Patelc290c8a2006-11-07 22:23:34 +00001076/// run - Execute all of the passes scheduled for execution. Keep track of
1077/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001078bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001079
Devang Patelc290c8a2006-11-07 22:23:34 +00001080 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001081
Devang Patelb8817b92006-12-14 00:59:42 +00001082 TimingInfo::createTheTimeInfo();
1083
Devang Patelcfd70c42006-12-13 22:10:00 +00001084 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001085 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001086
Devang Patele3068402006-12-21 00:16:50 +00001087 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001088 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1089 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001090 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001091 }
1092 return Changed;
1093}
Devang Patel376fefa2006-11-08 10:29:57 +00001094
Devang Patela1514cb2006-12-07 19:39:39 +00001095//===----------------------------------------------------------------------===//
1096// PassManager implementation
1097
Devang Patel376fefa2006-11-08 10:29:57 +00001098/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001099PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001100 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001101 // PM is the top level manager
1102 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001103}
1104
Devang Patelb67904d2006-12-13 02:36:01 +00001105PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001106 delete PM;
1107}
1108
Devang Patel376fefa2006-11-08 10:29:57 +00001109/// add - Add a pass to the queue of passes to run. This passes ownership of
1110/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1111/// will be destroyed as well, so there is no need to delete the pass. This
1112/// implies that all passes MUST be allocated with 'new'.
1113void
Devang Patelb67904d2006-12-13 02:36:01 +00001114PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001115 PM->add(P);
1116}
1117
1118/// run - Execute all of the passes scheduled for execution. Keep track of
1119/// whether any of the passes modifies the module, and if so, return true.
1120bool
Devang Patelb67904d2006-12-13 02:36:01 +00001121PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001122 return PM->run(M);
1123}
1124
Devang Patelb8817b92006-12-14 00:59:42 +00001125//===----------------------------------------------------------------------===//
1126// TimingInfo Class - This class is used to calculate information about the
1127// amount of time each pass takes to execute. This only happens with
1128// -time-passes is enabled on the command line.
1129//
1130bool llvm::TimePassesIsEnabled = false;
1131static cl::opt<bool,true>
1132EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1133 cl::desc("Time each pass, printing elapsed time for each on exit"));
1134
1135// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1136// a non null value (if the -time-passes option is enabled) or it leaves it
1137// null. It may be called multiple times.
1138void TimingInfo::createTheTimeInfo() {
1139 if (!TimePassesIsEnabled || TheTimeInfo) return;
1140
1141 // Constructed the first time this is called, iff -time-passes is enabled.
1142 // This guarantees that the object will be constructed before static globals,
1143 // thus it will be destroyed before them.
1144 static ManagedStatic<TimingInfo> TTI;
1145 TheTimeInfo = &*TTI;
1146}
1147
Devang Patel1c3633e2007-01-29 23:10:37 +00001148/// If TimingInfo is enabled then start pass timer.
1149void StartPassTimer(Pass *P) {
1150 if (TheTimeInfo)
1151 TheTimeInfo->passStarted(P);
1152}
1153
1154/// If TimingInfo is enabled then stop pass timer.
1155void StopPassTimer(Pass *P) {
1156 if (TheTimeInfo)
1157 TheTimeInfo->passEnded(P);
1158}
1159
Devang Patel1c56a632007-01-08 19:29:38 +00001160//===----------------------------------------------------------------------===//
1161// PMStack implementation
1162//
Devang Patelad98d232007-01-11 22:15:30 +00001163
Devang Patel1c56a632007-01-08 19:29:38 +00001164// Pop Pass Manager from the stack and clear its analysis info.
1165void PMStack::pop() {
1166
1167 PMDataManager *Top = this->top();
1168 Top->initializeAnalysisInfo();
1169
1170 S.pop_back();
1171}
1172
1173// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001174void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001175
Devang Patel15701b52007-01-11 00:19:00 +00001176 PMDataManager *Top = NULL;
1177 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1178 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001179
Devang Patel15701b52007-01-11 00:19:00 +00001180 if (this->empty()) {
1181 Top = PM;
1182 }
1183 else {
1184 Top = this->top();
1185 PMTopLevelManager *TPM = Top->getTopLevelManager();
1186
1187 assert (TPM && "Unable to find top level manager");
1188 TPM->addIndirectPassManager(PM);
1189 PM->setTopLevelManager(TPM);
1190 }
1191
1192 AnalysisResolver *AR = new AnalysisResolver(*Top);
1193 P->setResolver(AR);
1194
1195 S.push_back(PM);
1196}
1197
1198// Dump content of the pass manager stack.
1199void PMStack::dump() {
1200 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1201 E = S.end(); I != E; ++I) {
1202 Pass *P = dynamic_cast<Pass *>(*I);
1203 printf ("%s ", P->getPassName());
1204 }
1205 if (!S.empty())
1206 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001207}
1208
1209// Walk Pass Manager stack and set LastUse markers if any
1210// manager is transfering this priviledge to its parent manager
1211void PMStack::handleLastUserOverflow() {
1212
1213 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1214
1215 PMDataManager *Child = *I++;
1216 if (I != E) {
1217 PMDataManager *Parent = *I++;
1218 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1219 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1220 if (!TLU.empty()) {
1221 Pass *P = dynamic_cast<Pass *>(Parent);
1222 TPM->setLastUser(TLU, P);
1223 }
1224 }
1225 }
1226}
1227
1228/// Find appropriate Module Pass Manager in the PM Stack and
1229/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001230void ModulePass::assignPassManager(PMStack &PMS,
1231 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001232
Devang Patel1c56a632007-01-08 19:29:38 +00001233 // Find Module Pass Manager
1234 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001235 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1236 if (TopPMType == PreferredType)
1237 break; // We found desired pass manager
1238 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001239 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001240 else
1241 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001242 }
1243
Devang Patel23f8aa92007-01-17 21:19:23 +00001244 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001245}
1246
Devang Patel3312f752007-01-16 21:43:18 +00001247/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1248/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001249void FunctionPass::assignPassManager(PMStack &PMS,
1250 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001251
Devang Patel15701b52007-01-11 00:19:00 +00001252 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001253 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001254 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1255 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001256 else
Devang Patel3312f752007-01-16 21:43:18 +00001257 break;
1258 }
1259 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1260
1261 // Create new Function Pass Manager
1262 if (!FPP) {
1263 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1264 PMDataManager *PMD = PMS.top();
1265
1266 // [1] Create new Function Pass Manager
1267 FPP = new FPPassManager(PMD->getDepth() + 1);
1268
1269 // [2] Set up new manager's top level manager
1270 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1271 TPM->addIndirectPassManager(FPP);
1272
1273 // [3] Assign manager to manage this new manager. This may create
1274 // and push new managers into PMS
1275 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001276
1277 // If Call Graph Pass Manager is active then use it to manage
1278 // this new Function Pass manager.
1279 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1280 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1281 else
1282 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001283
1284 // [4] Push new manager into PMS
1285 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001286 }
1287
Devang Patel3312f752007-01-16 21:43:18 +00001288 // Assign FPP as the manager of this pass.
1289 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001290}
1291
Devang Patel3312f752007-01-16 21:43:18 +00001292/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001293/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001294void BasicBlockPass::assignPassManager(PMStack &PMS,
1295 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001296
1297 BBPassManager *BBP = NULL;
1298
Devang Patel15701b52007-01-11 00:19:00 +00001299 // Basic Pass Manager is a leaf pass manager. It does not handle
1300 // any other pass manager.
1301 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001302 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001303 }
1304
Devang Patel3312f752007-01-16 21:43:18 +00001305 // If leaf manager is not Basic Block Pass manager then create new
1306 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001307
Devang Patel3312f752007-01-16 21:43:18 +00001308 if (!BBP) {
1309 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1310 PMDataManager *PMD = PMS.top();
1311
1312 // [1] Create new Basic Block Manager
1313 BBP = new BBPassManager(PMD->getDepth() + 1);
1314
1315 // [2] Set up new manager's top level manager
1316 // Basic Block Pass Manager does not live by itself
1317 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1318 TPM->addIndirectPassManager(BBP);
1319
Devang Patel15701b52007-01-11 00:19:00 +00001320 // [3] Assign manager to manage this new manager. This may create
1321 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001322 Pass *P = dynamic_cast<Pass *>(BBP);
1323 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001324
Devang Patel3312f752007-01-16 21:43:18 +00001325 // [4] Push new manager into PMS
1326 PMS.push(BBP);
1327 }
Devang Patel1c56a632007-01-08 19:29:38 +00001328
Devang Patel3312f752007-01-16 21:43:18 +00001329 // Assign BBP as the manager of this pass.
1330 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001331}
1332
Devang Patelc6b5a552007-01-05 20:16:23 +00001333