blob: d72f8caa145b4356941fbbd6a38cb355e46c76b0 [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 Pateleda56172006-12-12 23:34:33 +000081 // Print passes managed by this manager
82 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000083 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000084 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
85 BasicBlockPass *BP = getContainedPass(Index);
86 BP->dumpPassStructure(Offset + 1);
87 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000088 }
89 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000090
91 BasicBlockPass *getContainedPass(unsigned N) {
92 assert ( N < PassVector.size() && "Pass number out of range!");
93 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
94 return BP;
95 }
Devang Patel3b3f8992007-01-11 01:10:25 +000096
97 virtual PassManagerType getPassManagerType() {
98 return PMT_BasicBlockPassManager;
99 }
Devang Patelca58e352006-11-08 10:05:38 +0000100};
101
Devang Patele7599552007-01-12 18:52:44 +0000102}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000103
Devang Patele7599552007-01-12 18:52:44 +0000104namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000105
Devang Patel10c2ca62006-12-12 22:47:13 +0000106//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000107// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000108//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000109/// FunctionPassManagerImpl manages FPPassManagers
110class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000111 public PMDataManager,
112 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000113public:
114
Devang Patel4268fc02007-01-16 02:00:38 +0000115 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth),
116 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000117
118 /// add - Add a pass to the queue of passes to run. This passes ownership of
119 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
120 /// will be destroyed as well, so there is no need to delete the pass. This
121 /// implies that all passes MUST be allocated with 'new'.
122 void add(Pass *P) {
123 schedulePass(P);
124 }
125
126 /// run - Execute all of the passes scheduled for execution. Keep track of
127 /// whether any of the passes modifies the module, and if so, return true.
128 bool run(Function &F);
129
130 /// doInitialization - Run all of the initializers for the function passes.
131 ///
132 bool doInitialization(Module &M);
133
134 /// doFinalization - Run all of the initializers for the function passes.
135 ///
136 bool doFinalization(Module &M);
137
138 /// Pass Manager itself does not invalidate any analysis info.
139 void getAnalysisUsage(AnalysisUsage &Info) const {
140 Info.setPreservesAll();
141 }
142
143 inline void addTopLevelPass(Pass *P) {
144
145 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
146
147 // P is a immutable pass and it will be managed by this
148 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000149 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000150 P->setResolver(AR);
151 initializeAnalysisImpl(P);
152 addImmutablePass(IP);
153 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000154 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000155 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000156 }
Devang Patel0f080042007-01-12 17:23:48 +0000157
Devang Patel67d6a5e2006-12-19 19:46:59 +0000158 }
159
160 FPPassManager *getContainedManager(unsigned N) {
161 assert ( N < PassManagers.size() && "Pass number out of range!");
162 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
163 return FP;
164 }
165
Devang Patel67d6a5e2006-12-19 19:46:59 +0000166};
167
168//===----------------------------------------------------------------------===//
169// MPPassManager
170//
171/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000172/// It batches all Module passes passes and function pass managers together and
173/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000174class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000175
176public:
Devang Patel0f080042007-01-12 17:23:48 +0000177 MPPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000178
179 /// run - Execute all of the passes scheduled for execution. Keep track of
180 /// whether any of the passes modifies the module, and if so, return true.
181 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000182
Devang Patelf9d96b92006-12-07 19:57:52 +0000183 /// Pass Manager itself does not invalidate any analysis info.
184 void getAnalysisUsage(AnalysisUsage &Info) const {
185 Info.setPreservesAll();
186 }
187
Devang Pateleda56172006-12-12 23:34:33 +0000188 // Print passes managed by this manager
189 void dumpPassStructure(unsigned Offset) {
190 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000191 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
192 ModulePass *MP = getContainedPass(Index);
193 MP->dumpPassStructure(Offset + 1);
194 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000195 }
196 }
197
Devang Patelabfbe3b2006-12-16 00:56:26 +0000198 ModulePass *getContainedPass(unsigned N) {
199 assert ( N < PassVector.size() && "Pass number out of range!");
200 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
201 return MP;
202 }
203
Devang Patel3b3f8992007-01-11 01:10:25 +0000204 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
Devang Patelca58e352006-11-08 10:05:38 +0000205};
206
Devang Patel10c2ca62006-12-12 22:47:13 +0000207//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000208// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000209//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000210/// PassManagerImpl manages MPPassManagers
211class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000212 public PMDataManager,
213 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000214
215public:
216
Devang Patel4268fc02007-01-16 02:00:38 +0000217 PassManagerImpl(int Depth) : PMDataManager(Depth),
218 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000219
Devang Patel376fefa2006-11-08 10:29:57 +0000220 /// add - Add a pass to the queue of passes to run. This passes ownership of
221 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
222 /// will be destroyed as well, so there is no need to delete the pass. This
223 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000224 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000225 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000226 }
Devang Patel376fefa2006-11-08 10:29:57 +0000227
228 /// run - Execute all of the passes scheduled for execution. Keep track of
229 /// whether any of the passes modifies the module, and if so, return true.
230 bool run(Module &M);
231
Devang Patelf9d96b92006-12-07 19:57:52 +0000232 /// Pass Manager itself does not invalidate any analysis info.
233 void getAnalysisUsage(AnalysisUsage &Info) const {
234 Info.setPreservesAll();
235 }
236
Devang Patelabcd1d32006-12-07 21:27:23 +0000237 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000238
Devang Patelfa971cd2006-12-08 23:57:43 +0000239 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000240
241 // P is a immutable pass and it will be managed by this
242 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000243 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000244 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000245 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000246 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000247 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000248 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000249 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000250 }
Devang Patel0f080042007-01-12 17:23:48 +0000251
Devang Patelabcd1d32006-12-07 21:27:23 +0000252 }
253
Devang Patel67d6a5e2006-12-19 19:46:59 +0000254 MPPassManager *getContainedManager(unsigned N) {
255 assert ( N < PassManagers.size() && "Pass number out of range!");
256 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
257 return MP;
258 }
259
Devang Patel376fefa2006-11-08 10:29:57 +0000260};
261
Devang Patel1c3633e2007-01-29 23:10:37 +0000262} // End of llvm namespace
263
264namespace {
265
266//===----------------------------------------------------------------------===//
267// TimingInfo Class - This class is used to calculate information about the
268// amount of time each pass takes to execute. This only happens when
269// -time-passes is enabled on the command line.
270//
271
272class VISIBILITY_HIDDEN TimingInfo {
273 std::map<Pass*, Timer> TimingData;
274 TimerGroup TG;
275
276public:
277 // Use 'create' member to get this.
278 TimingInfo() : TG("... Pass execution timing report ...") {}
279
280 // TimingDtor - Print out information about timing information
281 ~TimingInfo() {
282 // Delete all of the timers...
283 TimingData.clear();
284 // TimerGroup is deleted next, printing the report.
285 }
286
287 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
288 // to a non null value (if the -time-passes option is enabled) or it leaves it
289 // null. It may be called multiple times.
290 static void createTheTimeInfo();
291
292 void passStarted(Pass *P) {
293
294 if (dynamic_cast<PMDataManager *>(P))
295 return;
296
297 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
298 if (I == TimingData.end())
299 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
300 I->second.startTimer();
301 }
302 void passEnded(Pass *P) {
303
304 if (dynamic_cast<PMDataManager *>(P))
305 return;
306
307 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
308 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
309 I->second.stopTimer();
310 }
311};
312
Devang Patelb8817b92006-12-14 00:59:42 +0000313static TimingInfo *TheTimeInfo;
314
Devang Patel1c3633e2007-01-29 23:10:37 +0000315} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000316
Devang Patela1514cb2006-12-07 19:39:39 +0000317//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000318// PMTopLevelManager implementation
319
Devang Patel4268fc02007-01-16 02:00:38 +0000320/// Initialize top level manager. Create first pass manager.
321PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
322
323 if (t == TLM_Pass) {
324 MPPassManager *MPP = new MPPassManager(1);
325 MPP->setTopLevelManager(this);
326 addPassManager(MPP);
327 activeStack.push(MPP);
328 }
329 else if (t == TLM_Function) {
330 FPPassManager *FPP = new FPPassManager(1);
331 FPP->setTopLevelManager(this);
332 addPassManager(FPP);
333 activeStack.push(FPP);
334 }
335}
336
Devang Patelafb1f3622006-12-12 22:35:25 +0000337/// Set pass P as the last user of the given analysis passes.
338void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
339 Pass *P) {
340
341 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
342 E = AnalysisPasses.end(); I != E; ++I) {
343 Pass *AP = *I;
344 LastUser[AP] = P;
345 // If AP is the last user of other passes then make P last user of
346 // such passes.
347 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
348 LUE = LastUser.end(); LUI != LUE; ++LUI) {
349 if (LUI->second == AP)
350 LastUser[LUI->first] = P;
351 }
352 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000353}
354
355/// Collect passes whose last user is P
356void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
357 Pass *P) {
358 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
359 LUE = LastUser.end(); LUI != LUE; ++LUI)
360 if (LUI->second == P)
361 LastUses.push_back(LUI->first);
362}
363
364/// Schedule pass P for execution. Make sure that passes required by
365/// P are run before P is run. Update analysis info maintained by
366/// the manager. Remove dead passes. This is a recursive function.
367void PMTopLevelManager::schedulePass(Pass *P) {
368
Devang Patel3312f752007-01-16 21:43:18 +0000369 // TODO : Allocate function manager for this pass, other wise required set
370 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000371
372 AnalysisUsage AnUsage;
373 P->getAnalysisUsage(AnUsage);
374 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
375 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
376 E = RequiredSet.end(); I != E; ++I) {
377
378 Pass *AnalysisPass = findAnalysisPass(*I);
379 if (!AnalysisPass) {
380 // Schedule this analysis run first.
381 AnalysisPass = (*I)->createPass();
382 schedulePass(AnalysisPass);
383 }
384 }
385
386 // Now all required passes are available.
387 addTopLevelPass(P);
388}
389
390/// Find the pass that implements Analysis AID. Search immutable
391/// passes and all pass managers. If desired pass is not found
392/// then return NULL.
393Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
394
395 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000396 // Check pass managers
397 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
398 E = PassManagers.end(); P == NULL && I != E; ++I) {
399 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
400 assert(PMD && "This is not a PassManager");
401 P = PMD->findAnalysisPass(AID, false);
402 }
403
404 // Check other pass managers
405 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
406 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
407 P = (*I)->findAnalysisPass(AID, false);
408
Devang Patelafb1f3622006-12-12 22:35:25 +0000409 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
410 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
411 const PassInfo *PI = (*I)->getPassInfo();
412 if (PI == AID)
413 P = *I;
414
415 // If Pass not found then check the interfaces implemented by Immutable Pass
416 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000417 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
418 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
419 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000420 }
421 }
422
Devang Patelafb1f3622006-12-12 22:35:25 +0000423 return P;
424}
425
Devang Pateleda56172006-12-12 23:34:33 +0000426// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000427void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000428
Devang Patelfd4184322007-01-17 20:33:36 +0000429 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000430 return;
431
Devang Pateleda56172006-12-12 23:34:33 +0000432 // Print out the immutable passes
433 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
434 ImmutablePasses[i]->dumpPassStructure(0);
435 }
436
Devang Patel991aeba2006-12-15 20:13:01 +0000437 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000438 E = PassManagers.end(); I != E; ++I)
439 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000440}
441
Devang Patel991aeba2006-12-15 20:13:01 +0000442void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000443
Devang Patelfd4184322007-01-17 20:33:36 +0000444 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000445 return;
446
447 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000448 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000449 E = PassManagers.end(); I != E; ++I) {
450 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
451 assert(PMD && "This is not a PassManager");
452 PMD->dumpPassArguments();
453 }
454 cerr << "\n";
455}
456
Devang Patele3068402006-12-21 00:16:50 +0000457void PMTopLevelManager::initializeAllAnalysisInfo() {
458
459 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
460 E = PassManagers.end(); I != E; ++I) {
461 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
462 assert(PMD && "This is not a PassManager");
463 PMD->initializeAnalysisInfo();
464 }
465
466 // Initailize other pass managers
467 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
468 E = IndirectPassManagers.end(); I != E; ++I)
469 (*I)->initializeAnalysisInfo();
470}
471
Devang Patele7599552007-01-12 18:52:44 +0000472/// Destructor
473PMTopLevelManager::~PMTopLevelManager() {
474 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
475 E = PassManagers.end(); I != E; ++I)
476 delete *I;
477
478 for (std::vector<ImmutablePass *>::iterator
479 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
480 delete *I;
481
482 PassManagers.clear();
483}
484
Devang Patelafb1f3622006-12-12 22:35:25 +0000485//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000486// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000487
Devang Pateld65e9e92006-11-08 01:31:28 +0000488/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000489/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000490bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000491
Devang Patel8f677ce2006-12-07 18:47:25 +0000492 // TODO
493 // If this pass is not preserving information that is required by a
494 // pass maintained by higher level pass manager then do not insert
495 // this pass into current manager. Use new manager. For example,
496 // For example, If FunctionPass F is not preserving ModulePass Info M1
497 // that is used by another ModulePass M2 then do not insert F in
498 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000499 return true;
500}
501
Devang Patel643676c2006-11-11 01:10:19 +0000502/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000503void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000504
Devang Patel643676c2006-11-11 01:10:19 +0000505 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000506 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000507
Devang Patele9976aa2006-12-07 19:33:53 +0000508 //This pass is the current implementation of all of the interfaces it
509 //implements as well.
510 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
511 for (unsigned i = 0, e = II.size(); i != e; ++i)
512 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000513 }
514}
515
Devang Patelf68a3492006-11-07 22:35:17 +0000516/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000517void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000518 AnalysisUsage AnUsage;
519 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000520
Devang Patel2e169c32006-12-07 20:03:49 +0000521 if (AnUsage.getPreservesAll())
522 return;
523
524 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000525 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000526 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000527 std::map<AnalysisID, Pass*>::iterator Info = I++;
528 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000529 PreservedSet.end()) {
530 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000531 if (!dynamic_cast<ImmutablePass*>(Info->second))
532 AvailableAnalysis.erase(Info);
533 }
Devang Patel349170f2006-11-11 01:24:55 +0000534 }
Devang Patelf68a3492006-11-07 22:35:17 +0000535}
536
Devang Patelca189262006-11-14 03:05:08 +0000537/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000538void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000539
540 std::vector<Pass *> DeadPasses;
541 TPM->collectLastUses(DeadPasses, P);
542
543 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
544 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000545
546 std::string Msg1 = " Freeing Pass '";
547 dumpPassInfo(*I, Msg1, Msg);
548
Devang Patelb8817b92006-12-14 00:59:42 +0000549 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000550 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000551 if (TheTimeInfo) TheTimeInfo->passEnded(P);
552
Devang Patel17ad0962006-12-08 00:37:52 +0000553 std::map<AnalysisID, Pass*>::iterator Pos =
554 AvailableAnalysis.find((*I)->getPassInfo());
555
Devang Patel475c4532006-12-08 00:59:05 +0000556 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000557 if (Pos != AvailableAnalysis.end())
558 AvailableAnalysis.erase(Pos);
559 }
Devang Patelca189262006-11-14 03:05:08 +0000560}
561
Devang Patel8f677ce2006-12-07 18:47:25 +0000562/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000563/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000564void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000565 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000566
Devang Pateld440cd92006-12-08 23:53:00 +0000567 // This manager is going to manage pass P. Set up analysis resolver
568 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000569 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000570 P->setResolver(AR);
571
Devang Patel90b05e02006-11-11 02:04:19 +0000572 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000573
574 // At the moment, this pass is the last user of all required passes.
575 std::vector<Pass *> LastUses;
576 std::vector<Pass *> RequiredPasses;
577 unsigned PDepth = this->getDepth();
578
579 collectRequiredAnalysisPasses(RequiredPasses, P);
580 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
581 E = RequiredPasses.end(); I != E; ++I) {
582 Pass *PRequired = *I;
583 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000584
585 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
586 RDepth = DM.getDepth();
587
Devang Patelbc03f132006-12-07 23:55:10 +0000588 if (PDepth == RDepth)
589 LastUses.push_back(PRequired);
590 else if (PDepth > RDepth) {
591 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000592 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000593 } else {
594 // Note : This feature is not yet implemented
595 assert (0 &&
596 "Unable to handle Pass that requires lower level Analysis pass");
597 }
598 }
599
Devang Patel39786a92007-01-15 20:31:54 +0000600 // Set P as P's last user until someone starts using P.
601 // However, if P is a Pass Manager then it does not need
602 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000603 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000604 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000605 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000606
Devang Patel17bff0d2006-12-07 22:09:36 +0000607 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000608 // Remove the analysis not preserved by this pass
609 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000610 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000611 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000612
613 // Add pass
614 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000615}
616
Devang Patel1d6267c2006-12-07 23:05:44 +0000617/// Populate RequiredPasses with the analysis pass that are required by
618/// pass P.
619void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
620 Pass *P) {
621 AnalysisUsage AnUsage;
622 P->getAnalysisUsage(AnUsage);
623 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
624 for (std::vector<AnalysisID>::const_iterator
625 I = RequiredSet.begin(), E = RequiredSet.end();
626 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000627 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000628 assert (AnalysisPass && "Analysis pass is not available");
629 RP.push_back(AnalysisPass);
630 }
Devang Patelf58183d2006-12-12 23:09:32 +0000631
632 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
633 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
634 E = IDs.end(); I != E; ++I) {
635 Pass *AnalysisPass = findAnalysisPass(*I, true);
636 assert (AnalysisPass && "Analysis pass is not available");
637 RP.push_back(AnalysisPass);
638 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000639}
640
Devang Patel07f4f582006-11-14 21:49:36 +0000641// All Required analyses should be available to the pass as it runs! Here
642// we fill in the AnalysisImpls member of the pass so that it can
643// successfully use the getAnalysis() method to retrieve the
644// implementations it needs.
645//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000646void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000647 AnalysisUsage AnUsage;
648 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000649
650 for (std::vector<const PassInfo *>::const_iterator
651 I = AnUsage.getRequiredSet().begin(),
652 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000653 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000654 if (Impl == 0)
655 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000656 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000657 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000658 }
659}
660
Devang Patel640c5bb2006-12-08 22:30:11 +0000661/// Find the pass that implements Analysis AID. If desired pass is not found
662/// then return NULL.
663Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
664
665 // Check if AvailableAnalysis map has one entry.
666 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
667
668 if (I != AvailableAnalysis.end())
669 return I->second;
670
671 // Search Parents through TopLevelManager
672 if (SearchParent)
673 return TPM->findAnalysisPass(AID);
674
Devang Patel9d759b82006-12-09 00:09:12 +0000675 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000676}
677
Devang Patel991aeba2006-12-15 20:13:01 +0000678// Print list of passes that are last used by P.
679void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
680
681 std::vector<Pass *> LUses;
682
683 assert (TPM && "Top Level Manager is missing");
684 TPM->collectLastUses(LUses, P);
685
686 for (std::vector<Pass *>::iterator I = LUses.begin(),
687 E = LUses.end(); I != E; ++I) {
688 llvm::cerr << "--" << std::string(Offset*2, ' ');
689 (*I)->dumpPassStructure(0);
690 }
691}
692
693void PMDataManager::dumpPassArguments() const {
694 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
695 E = PassVector.end(); I != E; ++I) {
696 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
697 PMD->dumpPassArguments();
698 else
699 if (const PassInfo *PI = (*I)->getPassInfo())
700 if (!PI->isAnalysisGroup())
701 cerr << " -" << PI->getPassArgument();
702 }
703}
704
705void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
706 std::string &Msg2) const {
Devang Patelfd4184322007-01-17 20:33:36 +0000707 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000708 return;
709 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
710 cerr << Msg1;
711 cerr << P->getPassName();
712 cerr << Msg2;
713}
714
715void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
716 const std::vector<AnalysisID> &Set)
717 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000718 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000719 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
720 for (unsigned i = 0; i != Set.size(); ++i) {
721 if (i) cerr << ",";
722 cerr << " " << Set[i]->getPassName();
723 }
724 cerr << "\n";
725 }
726}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000727
Devang Patele7599552007-01-12 18:52:44 +0000728// Destructor
729PMDataManager::~PMDataManager() {
730
731 for (std::vector<Pass *>::iterator I = PassVector.begin(),
732 E = PassVector.end(); I != E; ++I)
733 delete *I;
734
735 PassVector.clear();
736}
737
Devang Patel9bdf7d42006-12-08 23:28:54 +0000738//===----------------------------------------------------------------------===//
739// NOTE: Is this the right place to define this method ?
740// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000741Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000742 return PM.findAnalysisPass(ID, dir);
743}
744
Devang Patela1514cb2006-12-07 19:39:39 +0000745//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000746// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000747
Devang Patel6e5a1132006-11-07 21:31:57 +0000748/// Execute all of the passes scheduled for execution by invoking
749/// runOnBasicBlock method. Keep track of whether any of the passes modifies
750/// the function, and if so, return true.
751bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000752BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000753
Devang Patel745a6962006-12-12 23:15:28 +0000754 if (F.isExternal())
755 return false;
756
Devang Patele9585592006-12-08 01:38:28 +0000757 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000758
Devang Patel93a197c2006-12-14 00:08:04 +0000759 std::string Msg1 = "Executing Pass '";
760 std::string Msg3 = "' Made Modification '";
761
Devang Patel6e5a1132006-11-07 21:31:57 +0000762 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000763 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
764 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000765 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000766 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000767
Devang Patel200d3052006-12-13 23:50:44 +0000768 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000769 dumpPassInfo(BP, Msg1, Msg2);
770 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000771
Devang Patelabfbe3b2006-12-16 00:56:26 +0000772 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000773
Devang Patelabfbe3b2006-12-16 00:56:26 +0000774 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000775 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000776 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000777
778 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000779 dumpPassInfo(BP, Msg3, Msg2);
780 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000781
Devang Patelabfbe3b2006-12-16 00:56:26 +0000782 removeNotPreservedAnalysis(BP);
783 recordAvailableAnalysis(BP);
784 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +0000785 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000786 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000787}
788
Devang Patel475c4532006-12-08 00:59:05 +0000789// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000790inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000791 bool Changed = false;
792
Devang Patelabfbe3b2006-12-16 00:56:26 +0000793 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
794 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000795 Changed |= BP->doInitialization(M);
796 }
797
798 return Changed;
799}
800
Devang Patel67d6a5e2006-12-19 19:46:59 +0000801inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000802 bool Changed = false;
803
Devang Patelabfbe3b2006-12-16 00:56:26 +0000804 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
805 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000806 Changed |= BP->doFinalization(M);
807 }
808
809 return Changed;
810}
811
Devang Patel67d6a5e2006-12-19 19:46:59 +0000812inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000813 bool Changed = false;
814
Devang Patelabfbe3b2006-12-16 00:56:26 +0000815 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
816 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000817 Changed |= BP->doInitialization(F);
818 }
819
820 return Changed;
821}
822
Devang Patel67d6a5e2006-12-19 19:46:59 +0000823inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000824 bool Changed = false;
825
Devang Patelabfbe3b2006-12-16 00:56:26 +0000826 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
827 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000828 Changed |= BP->doFinalization(F);
829 }
830
831 return Changed;
832}
833
834
Devang Patela1514cb2006-12-07 19:39:39 +0000835//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000836// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000837
Devang Patel4e12f862006-11-08 10:44:40 +0000838/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +0000839FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000840 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000841 // FPM is the top level manager.
842 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000843
844 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +0000845 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +0000846 FPM->setResolver(AR);
847
Devang Patel1f653682006-12-08 18:57:16 +0000848 MP = P;
849}
850
Devang Patelb67904d2006-12-13 02:36:01 +0000851FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000852 delete FPM;
853}
854
Devang Patel4e12f862006-11-08 10:44:40 +0000855/// add - Add a pass to the queue of passes to run. This passes
856/// ownership of the Pass to the PassManager. When the
857/// PassManager_X is destroyed, the pass will be destroyed as well, so
858/// there is no need to delete the pass. (TODO delete passes.)
859/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +0000860void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000861 FPM->add(P);
862}
863
Devang Patel9f3083e2006-11-15 19:39:54 +0000864/// run - Execute all of the passes scheduled for execution. Keep
865/// track of whether any of the passes modifies the function, and if
866/// so, return true.
867///
Devang Patelb67904d2006-12-13 02:36:01 +0000868bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000869 std::string errstr;
870 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000871 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000872 abort();
873 }
Devang Patel272908d2006-12-08 22:57:48 +0000874 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000875}
876
877
Devang Patelff631ae2006-11-15 01:27:05 +0000878/// doInitialization - Run all of the initializers for the function passes.
879///
Devang Patelb67904d2006-12-13 02:36:01 +0000880bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000881 return FPM->doInitialization(*MP->getModule());
882}
883
884/// doFinalization - Run all of the initializers for the function passes.
885///
Devang Patelb67904d2006-12-13 02:36:01 +0000886bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000887 return FPM->doFinalization(*MP->getModule());
888}
889
Devang Patela1514cb2006-12-07 19:39:39 +0000890//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000891// FunctionPassManagerImpl implementation
892//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000893inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
894 bool Changed = false;
895
896 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
897 FPPassManager *FP = getContainedManager(Index);
898 Changed |= FP->doInitialization(M);
899 }
900
901 return Changed;
902}
903
904inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
905 bool Changed = false;
906
907 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
908 FPPassManager *FP = getContainedManager(Index);
909 Changed |= FP->doFinalization(M);
910 }
911
912 return Changed;
913}
914
915// Execute all the passes managed by this top level manager.
916// Return true if any function is modified by a pass.
917bool FunctionPassManagerImpl::run(Function &F) {
918
919 bool Changed = false;
920
921 TimingInfo::createTheTimeInfo();
922
923 dumpArguments();
924 dumpPasses();
925
Devang Patele3068402006-12-21 00:16:50 +0000926 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +0000927 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
928 FPPassManager *FP = getContainedManager(Index);
929 Changed |= FP->runOnFunction(F);
930 }
931 return Changed;
932}
933
934//===----------------------------------------------------------------------===//
935// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000936
Devang Patele7599552007-01-12 18:52:44 +0000937/// Print passes managed by this manager
938void FPPassManager::dumpPassStructure(unsigned Offset) {
939 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
940 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
941 FunctionPass *FP = getContainedPass(Index);
942 FP->dumpPassStructure(Offset + 1);
943 dumpLastUses(FP, Offset+1);
944 }
945}
946
947
Devang Patel0c2012f2006-11-07 21:49:50 +0000948/// Execute all of the passes scheduled for execution by invoking
949/// runOnFunction method. Keep track of whether any of the passes modifies
950/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000951bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000952
953 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +0000954
955 if (F.isExternal())
956 return false;
957
Devang Patel93a197c2006-12-14 00:08:04 +0000958 std::string Msg1 = "Executing Pass '";
959 std::string Msg3 = "' Made Modification '";
960
Devang Patelabfbe3b2006-12-16 00:56:26 +0000961 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
962 FunctionPass *FP = getContainedPass(Index);
963
Devang Patelf6d1d212006-12-14 00:25:06 +0000964 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000965 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +0000966
Devang Patel200d3052006-12-13 23:50:44 +0000967 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000968 dumpPassInfo(FP, Msg1, Msg2);
969 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000970
Devang Patelabfbe3b2006-12-16 00:56:26 +0000971 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +0000972
Devang Patelabfbe3b2006-12-16 00:56:26 +0000973 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +0000974 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000975 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +0000976
977 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000978 dumpPassInfo(FP, Msg3, Msg2);
979 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000980
Devang Patelabfbe3b2006-12-16 00:56:26 +0000981 removeNotPreservedAnalysis(FP);
982 recordAvailableAnalysis(FP);
983 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +0000984 }
985 return Changed;
986}
987
Devang Patel67d6a5e2006-12-19 19:46:59 +0000988bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000989
Devang Patel67d6a5e2006-12-19 19:46:59 +0000990 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000991
992 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
993 this->runOnFunction(*I);
994
995 return Changed |= doFinalization(M);
996}
997
998inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +0000999 bool Changed = false;
1000
Devang Patelabfbe3b2006-12-16 00:56:26 +00001001 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1002 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001003 Changed |= FP->doInitialization(M);
1004 }
1005
1006 return Changed;
1007}
1008
Devang Patel67d6a5e2006-12-19 19:46:59 +00001009inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001010 bool Changed = false;
1011
Devang Patelabfbe3b2006-12-16 00:56:26 +00001012 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1013 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001014 Changed |= FP->doFinalization(M);
1015 }
1016
Devang Patelff631ae2006-11-15 01:27:05 +00001017 return Changed;
1018}
1019
Devang Patela1514cb2006-12-07 19:39:39 +00001020//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001021// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001022
Devang Patel05e1a972006-11-07 22:03:15 +00001023/// Execute all of the passes scheduled for execution by invoking
1024/// runOnModule method. Keep track of whether any of the passes modifies
1025/// the module, and if so, return true.
1026bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001027MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001028 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001029
Devang Patel93a197c2006-12-14 00:08:04 +00001030 std::string Msg1 = "Executing Pass '";
1031 std::string Msg3 = "' Made Modification '";
1032
Devang Patelabfbe3b2006-12-16 00:56:26 +00001033 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1034 ModulePass *MP = getContainedPass(Index);
1035
Devang Patelf6d1d212006-12-14 00:25:06 +00001036 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001037 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001038
Devang Patel200d3052006-12-13 23:50:44 +00001039 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001040 dumpPassInfo(MP, Msg1, Msg2);
1041 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001042
Devang Patelabfbe3b2006-12-16 00:56:26 +00001043 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001044
Devang Patelabfbe3b2006-12-16 00:56:26 +00001045 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001046 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001047 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001048
1049 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001050 dumpPassInfo(MP, Msg3, Msg2);
1051 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001052
Devang Patelabfbe3b2006-12-16 00:56:26 +00001053 removeNotPreservedAnalysis(MP);
1054 recordAvailableAnalysis(MP);
1055 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001056 }
1057 return Changed;
1058}
1059
Devang Patela1514cb2006-12-07 19:39:39 +00001060//===----------------------------------------------------------------------===//
1061// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001062//
Devang Patelc290c8a2006-11-07 22:23:34 +00001063/// run - Execute all of the passes scheduled for execution. Keep track of
1064/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001065bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001066
Devang Patelc290c8a2006-11-07 22:23:34 +00001067 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001068
Devang Patelb8817b92006-12-14 00:59:42 +00001069 TimingInfo::createTheTimeInfo();
1070
Devang Patelcfd70c42006-12-13 22:10:00 +00001071 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001072 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001073
Devang Patele3068402006-12-21 00:16:50 +00001074 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001075 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1076 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001077 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001078 }
1079 return Changed;
1080}
Devang Patel376fefa2006-11-08 10:29:57 +00001081
Devang Patela1514cb2006-12-07 19:39:39 +00001082//===----------------------------------------------------------------------===//
1083// PassManager implementation
1084
Devang Patel376fefa2006-11-08 10:29:57 +00001085/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001086PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001087 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001088 // PM is the top level manager
1089 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001090}
1091
Devang Patelb67904d2006-12-13 02:36:01 +00001092PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001093 delete PM;
1094}
1095
Devang Patel376fefa2006-11-08 10:29:57 +00001096/// add - Add a pass to the queue of passes to run. This passes ownership of
1097/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1098/// will be destroyed as well, so there is no need to delete the pass. This
1099/// implies that all passes MUST be allocated with 'new'.
1100void
Devang Patelb67904d2006-12-13 02:36:01 +00001101PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001102 PM->add(P);
1103}
1104
1105/// run - Execute all of the passes scheduled for execution. Keep track of
1106/// whether any of the passes modifies the module, and if so, return true.
1107bool
Devang Patelb67904d2006-12-13 02:36:01 +00001108PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001109 return PM->run(M);
1110}
1111
Devang Patelb8817b92006-12-14 00:59:42 +00001112//===----------------------------------------------------------------------===//
1113// TimingInfo Class - This class is used to calculate information about the
1114// amount of time each pass takes to execute. This only happens with
1115// -time-passes is enabled on the command line.
1116//
1117bool llvm::TimePassesIsEnabled = false;
1118static cl::opt<bool,true>
1119EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1120 cl::desc("Time each pass, printing elapsed time for each on exit"));
1121
1122// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1123// a non null value (if the -time-passes option is enabled) or it leaves it
1124// null. It may be called multiple times.
1125void TimingInfo::createTheTimeInfo() {
1126 if (!TimePassesIsEnabled || TheTimeInfo) return;
1127
1128 // Constructed the first time this is called, iff -time-passes is enabled.
1129 // This guarantees that the object will be constructed before static globals,
1130 // thus it will be destroyed before them.
1131 static ManagedStatic<TimingInfo> TTI;
1132 TheTimeInfo = &*TTI;
1133}
1134
Devang Patel1c3633e2007-01-29 23:10:37 +00001135/// If TimingInfo is enabled then start pass timer.
1136void StartPassTimer(Pass *P) {
1137 if (TheTimeInfo)
1138 TheTimeInfo->passStarted(P);
1139}
1140
1141/// If TimingInfo is enabled then stop pass timer.
1142void StopPassTimer(Pass *P) {
1143 if (TheTimeInfo)
1144 TheTimeInfo->passEnded(P);
1145}
1146
Devang Patel1c56a632007-01-08 19:29:38 +00001147//===----------------------------------------------------------------------===//
1148// PMStack implementation
1149//
Devang Patelad98d232007-01-11 22:15:30 +00001150
Devang Patel1c56a632007-01-08 19:29:38 +00001151// Pop Pass Manager from the stack and clear its analysis info.
1152void PMStack::pop() {
1153
1154 PMDataManager *Top = this->top();
1155 Top->initializeAnalysisInfo();
1156
1157 S.pop_back();
1158}
1159
1160// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001161void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001162
Devang Patel15701b52007-01-11 00:19:00 +00001163 PMDataManager *Top = NULL;
1164 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1165 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001166
Devang Patel15701b52007-01-11 00:19:00 +00001167 if (this->empty()) {
1168 Top = PM;
1169 }
1170 else {
1171 Top = this->top();
1172 PMTopLevelManager *TPM = Top->getTopLevelManager();
1173
1174 assert (TPM && "Unable to find top level manager");
1175 TPM->addIndirectPassManager(PM);
1176 PM->setTopLevelManager(TPM);
1177 }
1178
1179 AnalysisResolver *AR = new AnalysisResolver(*Top);
1180 P->setResolver(AR);
1181
1182 S.push_back(PM);
1183}
1184
1185// Dump content of the pass manager stack.
1186void PMStack::dump() {
1187 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1188 E = S.end(); I != E; ++I) {
1189 Pass *P = dynamic_cast<Pass *>(*I);
1190 printf ("%s ", P->getPassName());
1191 }
1192 if (!S.empty())
1193 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001194}
1195
1196// Walk Pass Manager stack and set LastUse markers if any
1197// manager is transfering this priviledge to its parent manager
1198void PMStack::handleLastUserOverflow() {
1199
1200 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1201
1202 PMDataManager *Child = *I++;
1203 if (I != E) {
1204 PMDataManager *Parent = *I++;
1205 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1206 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1207 if (!TLU.empty()) {
1208 Pass *P = dynamic_cast<Pass *>(Parent);
1209 TPM->setLastUser(TLU, P);
1210 }
1211 }
1212 }
1213}
1214
1215/// Find appropriate Module Pass Manager in the PM Stack and
1216/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001217void ModulePass::assignPassManager(PMStack &PMS,
1218 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001219
Devang Patel1c56a632007-01-08 19:29:38 +00001220 // Find Module Pass Manager
1221 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001222 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1223 if (TopPMType == PreferredType)
1224 break; // We found desired pass manager
1225 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001226 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001227 else
1228 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001229 }
1230
Devang Patel23f8aa92007-01-17 21:19:23 +00001231 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001232}
1233
Devang Patel3312f752007-01-16 21:43:18 +00001234/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1235/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001236void FunctionPass::assignPassManager(PMStack &PMS,
1237 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001238
Devang Patel15701b52007-01-11 00:19:00 +00001239 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001240 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001241 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1242 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001243 else
Devang Patel3312f752007-01-16 21:43:18 +00001244 break;
1245 }
1246 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1247
1248 // Create new Function Pass Manager
1249 if (!FPP) {
1250 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1251 PMDataManager *PMD = PMS.top();
1252
1253 // [1] Create new Function Pass Manager
1254 FPP = new FPPassManager(PMD->getDepth() + 1);
1255
1256 // [2] Set up new manager's top level manager
1257 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1258 TPM->addIndirectPassManager(FPP);
1259
1260 // [3] Assign manager to manage this new manager. This may create
1261 // and push new managers into PMS
1262 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001263
1264 // If Call Graph Pass Manager is active then use it to manage
1265 // this new Function Pass manager.
1266 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1267 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1268 else
1269 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001270
1271 // [4] Push new manager into PMS
1272 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001273 }
1274
Devang Patel3312f752007-01-16 21:43:18 +00001275 // Assign FPP as the manager of this pass.
1276 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001277}
1278
Devang Patel3312f752007-01-16 21:43:18 +00001279/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001280/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001281void BasicBlockPass::assignPassManager(PMStack &PMS,
1282 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001283
1284 BBPassManager *BBP = NULL;
1285
Devang Patel15701b52007-01-11 00:19:00 +00001286 // Basic Pass Manager is a leaf pass manager. It does not handle
1287 // any other pass manager.
1288 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001289 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001290 }
1291
Devang Patel3312f752007-01-16 21:43:18 +00001292 // If leaf manager is not Basic Block Pass manager then create new
1293 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001294
Devang Patel3312f752007-01-16 21:43:18 +00001295 if (!BBP) {
1296 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1297 PMDataManager *PMD = PMS.top();
1298
1299 // [1] Create new Basic Block Manager
1300 BBP = new BBPassManager(PMD->getDepth() + 1);
1301
1302 // [2] Set up new manager's top level manager
1303 // Basic Block Pass Manager does not live by itself
1304 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1305 TPM->addIndirectPassManager(BBP);
1306
Devang Patel15701b52007-01-11 00:19:00 +00001307 // [3] Assign manager to manage this new manager. This may create
1308 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001309 Pass *P = dynamic_cast<Pass *>(BBP);
1310 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001311
Devang Patel3312f752007-01-16 21:43:18 +00001312 // [4] Push new manager into PMS
1313 PMS.push(BBP);
1314 }
Devang Patel1c56a632007-01-08 19:29:38 +00001315
Devang Patel3312f752007-01-16 21:43:18 +00001316 // Assign BBP as the manager of this pass.
1317 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001318}
1319
Devang Patelc6b5a552007-01-05 20:16:23 +00001320