blob: 02a27c252732a7de8355e1227600fc49e2cd4dda [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Devang Patelca58e352006-11-08 10:05:38 +00005// This file was developed by Devang Patel and is distributed under
Devang Patel6e5a1132006-11-07 21:31:57 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM Pass Manager infrastructure.
11//
12//===----------------------------------------------------------------------===//
13
14
Devang Patele7599552007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
Devang Patelf1567a52006-12-13 20:03:48 +000016#include "llvm/Support/CommandLine.h"
Devang Patel1c3633e2007-01-29 23:10:37 +000017#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000018#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000019#include "llvm/ModuleProvider.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000020#include "llvm/Support/Streams.h"
Devang Patelb8817b92006-12-14 00:59:42 +000021#include "llvm/Support/ManagedStatic.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000022#include <algorithm>
Devang Patela9844592006-11-11 01:31:05 +000023#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000024#include <map>
Devang Patelffca9102006-12-15 19:39:30 +000025
Devang Patele7599552007-01-12 18:52:44 +000026// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000027
Devang Patelf1567a52006-12-13 20:03:48 +000028namespace llvm {
29
30//===----------------------------------------------------------------------===//
31// Pass debugging information. Often it is useful to find out what pass is
32// running when a crash occurs in a utility. When this library is compiled with
33// debugging on, a command line option (--debug-pass) is enabled that causes the
34// pass name to be printed before it executes.
35//
36
Devang Patel03fb5872006-12-13 21:13:31 +000037// Different debug levels that can be enabled...
38enum PassDebugLevel {
39 None, Arguments, Structure, Executions, Details
40};
41
Devang Patelf1567a52006-12-13 20:03:48 +000042static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000043PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000044 cl::desc("Print PassManager debugging information"),
45 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000046 clEnumVal(None , "disable debug output"),
47 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
48 clEnumVal(Structure , "print pass structure before run()"),
49 clEnumVal(Executions, "print pass name before it is executed"),
50 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000051 clEnumValEnd));
52} // End of llvm namespace
53
Devang Patelffca9102006-12-15 19:39:30 +000054namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000055
Devang Patelf33f3eb2006-12-07 19:21:29 +000056//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000057// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000058//
Devang Patel67d6a5e2006-12-19 19:46:59 +000059/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000060/// pass together and sequence them to process one basic block before
61/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000062class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
63 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000064
65public:
Devang Patel67d6a5e2006-12-19 19:46:59 +000066 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +000067
Devang Patelca58e352006-11-08 10:05:38 +000068 /// Execute all of the passes scheduled for execution. Keep track of
69 /// whether any of the passes modifies the function, and if so, return true.
70 bool runOnFunction(Function &F);
71
Devang Patelf9d96b92006-12-07 19:57:52 +000072 /// Pass Manager itself does not invalidate any analysis info.
73 void getAnalysisUsage(AnalysisUsage &Info) const {
74 Info.setPreservesAll();
75 }
76
Devang Patel475c4532006-12-08 00:59:05 +000077 bool doInitialization(Module &M);
78 bool doInitialization(Function &F);
79 bool doFinalization(Module &M);
80 bool doFinalization(Function &F);
81
Devang Patele3858e62007-02-01 22:08:25 +000082 virtual const char *getPassName() const {
83 return "BasicBlock Pass Manager";
84 }
85
Devang Pateleda56172006-12-12 23:34:33 +000086 // Print passes managed by this manager
87 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000088 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000089 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
90 BasicBlockPass *BP = getContainedPass(Index);
91 BP->dumpPassStructure(Offset + 1);
92 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000093 }
94 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000095
96 BasicBlockPass *getContainedPass(unsigned N) {
97 assert ( N < PassVector.size() && "Pass number out of range!");
98 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
99 return BP;
100 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000101
Devang Patel28349ab2007-02-27 15:00:39 +0000102 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000103 return PMT_BasicBlockPassManager;
104 }
Devang Patelca58e352006-11-08 10:05:38 +0000105};
106
Devang Patele7599552007-01-12 18:52:44 +0000107}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000108
Devang Patele7599552007-01-12 18:52:44 +0000109namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000110
Devang Patel10c2ca62006-12-12 22:47:13 +0000111//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000112// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000113//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000114/// FunctionPassManagerImpl manages FPPassManagers
115class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000116 public PMDataManager,
117 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000118public:
119
Devang Patel4268fc02007-01-16 02:00:38 +0000120 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth),
121 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000122
123 /// add - Add a pass to the queue of passes to run. This passes ownership of
124 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
125 /// will be destroyed as well, so there is no need to delete the pass. This
126 /// implies that all passes MUST be allocated with 'new'.
127 void add(Pass *P) {
128 schedulePass(P);
129 }
130
131 /// run - Execute all of the passes scheduled for execution. Keep track of
132 /// whether any of the passes modifies the module, and if so, return true.
133 bool run(Function &F);
134
135 /// doInitialization - Run all of the initializers for the function passes.
136 ///
137 bool doInitialization(Module &M);
138
139 /// doFinalization - Run all of the initializers for the function passes.
140 ///
141 bool doFinalization(Module &M);
142
143 /// Pass Manager itself does not invalidate any analysis info.
144 void getAnalysisUsage(AnalysisUsage &Info) const {
145 Info.setPreservesAll();
146 }
147
148 inline void addTopLevelPass(Pass *P) {
149
150 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
151
152 // P is a immutable pass and it will be managed by this
153 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000154 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000155 P->setResolver(AR);
156 initializeAnalysisImpl(P);
157 addImmutablePass(IP);
158 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000159 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000160 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000161 }
Devang Patel0f080042007-01-12 17:23:48 +0000162
Devang Patel67d6a5e2006-12-19 19:46:59 +0000163 }
164
165 FPPassManager *getContainedManager(unsigned N) {
166 assert ( N < PassManagers.size() && "Pass number out of range!");
167 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
168 return FP;
169 }
170
Devang Patel67d6a5e2006-12-19 19:46:59 +0000171};
172
173//===----------------------------------------------------------------------===//
174// MPPassManager
175//
176/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000177/// It batches all Module passes passes and function pass managers together and
178/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000179class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000180
181public:
Devang Patel0f080042007-01-12 17:23:48 +0000182 MPPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000183
184 /// run - Execute all of the passes scheduled for execution. Keep track of
185 /// whether any of the passes modifies the module, and if so, return true.
186 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000187
Devang Patelf9d96b92006-12-07 19:57:52 +0000188 /// Pass Manager itself does not invalidate any analysis info.
189 void getAnalysisUsage(AnalysisUsage &Info) const {
190 Info.setPreservesAll();
191 }
192
Devang Patele64d3052007-04-16 20:12:57 +0000193 /// Add RequiredPass into list of lower level passes required by pass P.
194 /// RequiredPass is run on the fly by Pass Manager when P requests it
195 /// through getAnalysis interface.
196 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
197
Devang Patele3858e62007-02-01 22:08:25 +0000198 virtual const char *getPassName() const {
199 return "Module Pass Manager";
200 }
201
Devang Pateleda56172006-12-12 23:34:33 +0000202 // Print passes managed by this manager
203 void dumpPassStructure(unsigned Offset) {
204 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000205 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
206 ModulePass *MP = getContainedPass(Index);
207 MP->dumpPassStructure(Offset + 1);
208 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000209 }
210 }
211
Devang Patelabfbe3b2006-12-16 00:56:26 +0000212 ModulePass *getContainedPass(unsigned N) {
213 assert ( N < PassVector.size() && "Pass number out of range!");
214 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
215 return MP;
216 }
217
Devang Patel28349ab2007-02-27 15:00:39 +0000218 virtual PassManagerType getPassManagerType() const {
219 return PMT_ModulePassManager;
220 }
Devang Patelca58e352006-11-08 10:05:38 +0000221};
222
Devang Patel10c2ca62006-12-12 22:47:13 +0000223//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000224// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000225//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000226/// PassManagerImpl manages MPPassManagers
227class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000228 public PMDataManager,
229 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000230
231public:
232
Devang Patel4268fc02007-01-16 02:00:38 +0000233 PassManagerImpl(int Depth) : PMDataManager(Depth),
234 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000235
Devang Patel376fefa2006-11-08 10:29:57 +0000236 /// add - Add a pass to the queue of passes to run. This passes ownership of
237 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
238 /// will be destroyed as well, so there is no need to delete the pass. This
239 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000240 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000241 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000242 }
Devang Patel376fefa2006-11-08 10:29:57 +0000243
244 /// run - Execute all of the passes scheduled for execution. Keep track of
245 /// whether any of the passes modifies the module, and if so, return true.
246 bool run(Module &M);
247
Devang Patelf9d96b92006-12-07 19:57:52 +0000248 /// Pass Manager itself does not invalidate any analysis info.
249 void getAnalysisUsage(AnalysisUsage &Info) const {
250 Info.setPreservesAll();
251 }
252
Devang Patelabcd1d32006-12-07 21:27:23 +0000253 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000254
Devang Patelfa971cd2006-12-08 23:57:43 +0000255 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000256
257 // P is a immutable pass and it will be managed by this
258 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000259 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000260 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000261 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000262 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000263 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000264 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000265 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000266 }
Devang Patel0f080042007-01-12 17:23:48 +0000267
Devang Patelabcd1d32006-12-07 21:27:23 +0000268 }
269
Devang Patel67d6a5e2006-12-19 19:46:59 +0000270 MPPassManager *getContainedManager(unsigned N) {
271 assert ( N < PassManagers.size() && "Pass number out of range!");
272 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
273 return MP;
274 }
275
Devang Patel376fefa2006-11-08 10:29:57 +0000276};
277
Devang Patel1c3633e2007-01-29 23:10:37 +0000278} // End of llvm namespace
279
280namespace {
281
282//===----------------------------------------------------------------------===//
283// TimingInfo Class - This class is used to calculate information about the
284// amount of time each pass takes to execute. This only happens when
285// -time-passes is enabled on the command line.
286//
287
288class VISIBILITY_HIDDEN TimingInfo {
289 std::map<Pass*, Timer> TimingData;
290 TimerGroup TG;
291
292public:
293 // Use 'create' member to get this.
294 TimingInfo() : TG("... Pass execution timing report ...") {}
295
296 // TimingDtor - Print out information about timing information
297 ~TimingInfo() {
298 // Delete all of the timers...
299 TimingData.clear();
300 // TimerGroup is deleted next, printing the report.
301 }
302
303 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
304 // to a non null value (if the -time-passes option is enabled) or it leaves it
305 // null. It may be called multiple times.
306 static void createTheTimeInfo();
307
308 void passStarted(Pass *P) {
309
310 if (dynamic_cast<PMDataManager *>(P))
311 return;
312
313 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
314 if (I == TimingData.end())
315 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
316 I->second.startTimer();
317 }
318 void passEnded(Pass *P) {
319
320 if (dynamic_cast<PMDataManager *>(P))
321 return;
322
323 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
324 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
325 I->second.stopTimer();
326 }
327};
328
Devang Patelb8817b92006-12-14 00:59:42 +0000329static TimingInfo *TheTimeInfo;
330
Devang Patel1c3633e2007-01-29 23:10:37 +0000331} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000332
Devang Patela1514cb2006-12-07 19:39:39 +0000333//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000334// PMTopLevelManager implementation
335
Devang Patel4268fc02007-01-16 02:00:38 +0000336/// Initialize top level manager. Create first pass manager.
337PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
338
339 if (t == TLM_Pass) {
340 MPPassManager *MPP = new MPPassManager(1);
341 MPP->setTopLevelManager(this);
342 addPassManager(MPP);
343 activeStack.push(MPP);
344 }
345 else if (t == TLM_Function) {
346 FPPassManager *FPP = new FPPassManager(1);
347 FPP->setTopLevelManager(this);
348 addPassManager(FPP);
349 activeStack.push(FPP);
350 }
351}
352
Devang Patelafb1f3622006-12-12 22:35:25 +0000353/// Set pass P as the last user of the given analysis passes.
354void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
355 Pass *P) {
356
357 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
358 E = AnalysisPasses.end(); I != E; ++I) {
359 Pass *AP = *I;
360 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000361
362 if (P == AP)
363 continue;
364
Devang Patelafb1f3622006-12-12 22:35:25 +0000365 // If AP is the last user of other passes then make P last user of
366 // such passes.
367 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
368 LUE = LastUser.end(); LUI != LUE; ++LUI) {
369 if (LUI->second == AP)
370 LastUser[LUI->first] = P;
371 }
372 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000373}
374
375/// Collect passes whose last user is P
376void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
377 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000378 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
379 LUE = LastUser.end(); LUI != LUE; ++LUI)
380 if (LUI->second == P)
381 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000382}
383
384/// Schedule pass P for execution. Make sure that passes required by
385/// P are run before P is run. Update analysis info maintained by
386/// the manager. Remove dead passes. This is a recursive function.
387void PMTopLevelManager::schedulePass(Pass *P) {
388
Devang Patel3312f752007-01-16 21:43:18 +0000389 // TODO : Allocate function manager for this pass, other wise required set
390 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000391
Devang Patel3f806962007-02-05 19:34:17 +0000392 // If this Analysis is already requested by one of the previous pass
393 // and it is still available then do not insert new pass in the queue again.
394 if (findAnalysisPass(P->getPassInfo()))
395 return;
396
Devang Pateld74ede72007-03-06 01:06:16 +0000397 // Give pass a chance to prepare the stage.
398 P->preparePassManager(activeStack);
399
Devang Patelafb1f3622006-12-12 22:35:25 +0000400 AnalysisUsage AnUsage;
401 P->getAnalysisUsage(AnUsage);
402 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
403 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
404 E = RequiredSet.end(); I != E; ++I) {
405
406 Pass *AnalysisPass = findAnalysisPass(*I);
407 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000408 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000409 // Schedule this analysis run first only if it is not a lower level
410 // analysis pass. Lower level analsyis passes are run on the fly.
411 if (P->getPotentialPassManagerType () >=
412 AnalysisPass->getPotentialPassManagerType())
413 schedulePass(AnalysisPass);
414 else
415 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000416 }
417 }
418
419 // Now all required passes are available.
420 addTopLevelPass(P);
421}
422
423/// Find the pass that implements Analysis AID. Search immutable
424/// passes and all pass managers. If desired pass is not found
425/// then return NULL.
426Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
427
428 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000429 // Check pass managers
430 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
431 E = PassManagers.end(); P == NULL && I != E; ++I) {
432 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
433 assert(PMD && "This is not a PassManager");
434 P = PMD->findAnalysisPass(AID, false);
435 }
436
437 // Check other pass managers
438 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
439 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
440 P = (*I)->findAnalysisPass(AID, false);
441
Devang Patelafb1f3622006-12-12 22:35:25 +0000442 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
443 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
444 const PassInfo *PI = (*I)->getPassInfo();
445 if (PI == AID)
446 P = *I;
447
448 // If Pass not found then check the interfaces implemented by Immutable Pass
449 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000450 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
451 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
452 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000453 }
454 }
455
Devang Patelafb1f3622006-12-12 22:35:25 +0000456 return P;
457}
458
Devang Pateleda56172006-12-12 23:34:33 +0000459// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000460void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000461
Devang Patelfd4184322007-01-17 20:33:36 +0000462 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000463 return;
464
Devang Pateleda56172006-12-12 23:34:33 +0000465 // Print out the immutable passes
466 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
467 ImmutablePasses[i]->dumpPassStructure(0);
468 }
469
Devang Patel991aeba2006-12-15 20:13:01 +0000470 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000471 E = PassManagers.end(); I != E; ++I)
472 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000473}
474
Devang Patel991aeba2006-12-15 20:13:01 +0000475void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000476
Devang Patelfd4184322007-01-17 20:33:36 +0000477 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000478 return;
479
480 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000481 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000482 E = PassManagers.end(); I != E; ++I) {
483 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
484 assert(PMD && "This is not a PassManager");
485 PMD->dumpPassArguments();
486 }
487 cerr << "\n";
488}
489
Devang Patele3068402006-12-21 00:16:50 +0000490void PMTopLevelManager::initializeAllAnalysisInfo() {
491
492 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
493 E = PassManagers.end(); I != E; ++I) {
494 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
495 assert(PMD && "This is not a PassManager");
496 PMD->initializeAnalysisInfo();
497 }
498
499 // Initailize other pass managers
500 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
501 E = IndirectPassManagers.end(); I != E; ++I)
502 (*I)->initializeAnalysisInfo();
503}
504
Devang Patele7599552007-01-12 18:52:44 +0000505/// Destructor
506PMTopLevelManager::~PMTopLevelManager() {
507 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
508 E = PassManagers.end(); I != E; ++I)
509 delete *I;
510
511 for (std::vector<ImmutablePass *>::iterator
512 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
513 delete *I;
514
515 PassManagers.clear();
516}
517
Devang Patelafb1f3622006-12-12 22:35:25 +0000518//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000519// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000520
Devang Pateld65e9e92006-11-08 01:31:28 +0000521/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000522/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000523bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000524
Devang Patel8f677ce2006-12-07 18:47:25 +0000525 // TODO
526 // If this pass is not preserving information that is required by a
527 // pass maintained by higher level pass manager then do not insert
528 // this pass into current manager. Use new manager. For example,
529 // For example, If FunctionPass F is not preserving ModulePass Info M1
530 // that is used by another ModulePass M2 then do not insert F in
531 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000532 return true;
533}
534
Devang Patel643676c2006-11-11 01:10:19 +0000535/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000536void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000537
Devang Patel643676c2006-11-11 01:10:19 +0000538 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000539 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000540
Devang Patele9976aa2006-12-07 19:33:53 +0000541 //This pass is the current implementation of all of the interfaces it
542 //implements as well.
543 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
544 for (unsigned i = 0, e = II.size(); i != e; ++i)
545 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000546 }
547}
548
Devang Patel9d9fc902007-03-06 17:52:53 +0000549// Return true if P preserves high level analysis used by other
550// passes managed by this manager
551bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
552
553 AnalysisUsage AnUsage;
554 P->getAnalysisUsage(AnUsage);
555
556 if (AnUsage.getPreservesAll())
557 return true;
558
559 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
560 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
561 E = HigherLevelAnalysis.end(); I != E; ++I) {
562 Pass *P1 = *I;
Devang Patel01919d22007-03-08 19:05:01 +0000563 if (!dynamic_cast<ImmutablePass*>(P1)
564 && std::find(PreservedSet.begin(), PreservedSet.end(), P1->getPassInfo()) ==
565 PreservedSet.end())
566 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000567 }
568
569 return true;
570}
571
Devang Patelf68a3492006-11-07 22:35:17 +0000572/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000573void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000574 AnalysisUsage AnUsage;
575 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000576
Devang Patel2e169c32006-12-07 20:03:49 +0000577 if (AnUsage.getPreservesAll())
578 return;
579
580 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000581 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000582 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000583 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000584 if (!dynamic_cast<ImmutablePass*>(Info->second)
585 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
586 PreservedSet.end())
Devang Patel349170f2006-11-11 01:24:55 +0000587 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000588 AvailableAnalysis.erase(Info);
Devang Patel349170f2006-11-11 01:24:55 +0000589 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000590
591 // Check inherited analysis also. If P is not preserving analysis
592 // provided by parent manager then remove it here.
593 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
594
595 if (!InheritedAnalysis[Index])
596 continue;
597
598 for (std::map<AnalysisID, Pass*>::iterator
599 I = InheritedAnalysis[Index]->begin(),
600 E = InheritedAnalysis[Index]->end(); I != E; ) {
601 std::map<AnalysisID, Pass *>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000602 if (!dynamic_cast<ImmutablePass*>(Info->second)
603 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
604 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000605 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000606 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000607 }
608 }
609
Devang Patelf68a3492006-11-07 22:35:17 +0000610}
611
Devang Patelca189262006-11-14 03:05:08 +0000612/// Remove analysis passes that are not used any longer
Devang Patel003a5592007-03-05 20:01:30 +0000613void PMDataManager::removeDeadPasses(Pass *P, std::string Msg,
614 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000615
616 std::vector<Pass *> DeadPasses;
617 TPM->collectLastUses(DeadPasses, P);
618
619 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
620 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000621
Devang Patel003a5592007-03-05 20:01:30 +0000622 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000623
Devang Patel7ebf09d2007-03-05 18:20:51 +0000624 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000625 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000626 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000627
Devang Patel17ad0962006-12-08 00:37:52 +0000628 std::map<AnalysisID, Pass*>::iterator Pos =
629 AvailableAnalysis.find((*I)->getPassInfo());
630
Devang Patel475c4532006-12-08 00:59:05 +0000631 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000632 if (Pos != AvailableAnalysis.end())
633 AvailableAnalysis.erase(Pos);
634 }
Devang Patelca189262006-11-14 03:05:08 +0000635}
636
Devang Patel8f677ce2006-12-07 18:47:25 +0000637/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000638/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000639void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000640 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000641
Devang Pateld440cd92006-12-08 23:53:00 +0000642 // This manager is going to manage pass P. Set up analysis resolver
643 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000644 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000645 P->setResolver(AR);
646
Devang Patelec2b9a72007-03-05 22:57:49 +0000647 // If a FunctionPass F is the last user of ModulePass info M
648 // then the F's manager, not F, records itself as a last user of M.
649 std::vector<Pass *> TransferLastUses;
650
Devang Patel90b05e02006-11-11 02:04:19 +0000651 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000652
653 // At the moment, this pass is the last user of all required passes.
654 std::vector<Pass *> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000655 SmallVector<Pass *, 8> RequiredPasses;
656 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
657
Devang Patelbc03f132006-12-07 23:55:10 +0000658 unsigned PDepth = this->getDepth();
659
Devang Patele64d3052007-04-16 20:12:57 +0000660 collectRequiredAnalysis(RequiredPasses,
661 ReqAnalysisNotAvailable, P);
662 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000663 E = RequiredPasses.end(); I != E; ++I) {
664 Pass *PRequired = *I;
665 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000666
667 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
668 RDepth = DM.getDepth();
669
Devang Patelbc03f132006-12-07 23:55:10 +0000670 if (PDepth == RDepth)
671 LastUses.push_back(PRequired);
672 else if (PDepth > RDepth) {
673 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000674 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000675 // Keep track of higher level analysis used by this manager.
676 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000677 } else
678 assert (0 && "Unable to accomodate Required Pass");
679 }
680
681 // Now, take care of required analysises that are not available.
682 for (SmallVector<AnalysisID, 8>::iterator
683 I = ReqAnalysisNotAvailable.begin(),
684 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
685 Pass *AnalysisPass = (*I)->createPass();
686 this->addLowerLevelRequiredPass(P, AnalysisPass);
Devang Patelbc03f132006-12-07 23:55:10 +0000687 }
688
Devang Patel39786a92007-01-15 20:31:54 +0000689 // Set P as P's last user until someone starts using P.
690 // However, if P is a Pass Manager then it does not need
691 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000692 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000693 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000694 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000695
Devang Patelec2b9a72007-03-05 22:57:49 +0000696 if (!TransferLastUses.empty()) {
697 Pass *My_PM = dynamic_cast<Pass *>(this);
698 TPM->setLastUser(TransferLastUses, My_PM);
699 TransferLastUses.clear();
700 }
701
Devang Patel17bff0d2006-12-07 22:09:36 +0000702 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000703 // Remove the analysis not preserved by this pass
704 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000705 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000706 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000707
708 // Add pass
709 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000710}
711
Devang Patele64d3052007-04-16 20:12:57 +0000712
713/// Populate RP with analysis pass that are required by
714/// pass P and are available. Populate RP_NotAvail with analysis
715/// pass that are required by pass P but are not available.
716void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
717 SmallVector<AnalysisID, 8> &RP_NotAvail,
718 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000719 AnalysisUsage AnUsage;
720 P->getAnalysisUsage(AnUsage);
721 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
722 for (std::vector<AnalysisID>::const_iterator
723 I = RequiredSet.begin(), E = RequiredSet.end();
724 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000725 AnalysisID AID = *I;
726 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
727 RP.push_back(AnalysisPass);
728 else
729 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000730 }
Devang Patelf58183d2006-12-12 23:09:32 +0000731
732 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
733 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
734 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000735 AnalysisID AID = *I;
736 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
737 RP.push_back(AnalysisPass);
738 else
739 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000740 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000741}
742
Devang Patel07f4f582006-11-14 21:49:36 +0000743// All Required analyses should be available to the pass as it runs! Here
744// we fill in the AnalysisImpls member of the pass so that it can
745// successfully use the getAnalysis() method to retrieve the
746// implementations it needs.
747//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000748void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000749 AnalysisUsage AnUsage;
750 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000751
752 for (std::vector<const PassInfo *>::const_iterator
753 I = AnUsage.getRequiredSet().begin(),
754 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000755 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000756 if (Impl == 0)
757 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000758 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000759 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000760 }
761}
762
Devang Patel640c5bb2006-12-08 22:30:11 +0000763/// Find the pass that implements Analysis AID. If desired pass is not found
764/// then return NULL.
765Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
766
767 // Check if AvailableAnalysis map has one entry.
768 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
769
770 if (I != AvailableAnalysis.end())
771 return I->second;
772
773 // Search Parents through TopLevelManager
774 if (SearchParent)
775 return TPM->findAnalysisPass(AID);
776
Devang Patel9d759b82006-12-09 00:09:12 +0000777 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000778}
779
Devang Patel991aeba2006-12-15 20:13:01 +0000780// Print list of passes that are last used by P.
781void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
782
783 std::vector<Pass *> LUses;
784
785 assert (TPM && "Top Level Manager is missing");
786 TPM->collectLastUses(LUses, P);
787
788 for (std::vector<Pass *>::iterator I = LUses.begin(),
789 E = LUses.end(); I != E; ++I) {
790 llvm::cerr << "--" << std::string(Offset*2, ' ');
791 (*I)->dumpPassStructure(0);
792 }
793}
794
795void PMDataManager::dumpPassArguments() const {
796 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
797 E = PassVector.end(); I != E; ++I) {
798 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
799 PMD->dumpPassArguments();
800 else
801 if (const PassInfo *PI = (*I)->getPassInfo())
802 if (!PI->isAnalysisGroup())
803 cerr << " -" << PI->getPassArgument();
804 }
805}
806
Devang Patel003a5592007-03-05 20:01:30 +0000807void PMDataManager:: dumpPassInfo(Pass *P, enum PassDebuggingString S1,
808 enum PassDebuggingString S2,
809 std::string Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000810 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000811 return;
812 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000813 switch (S1) {
814 case EXECUTION_MSG:
815 cerr << "Executing Pass '" << P->getPassName();
816 break;
817 case MODIFICATION_MSG:
818 cerr << "' Made Modification '" << P->getPassName();
819 break;
820 case FREEING_MSG:
821 cerr << " Freeing Pass '" << P->getPassName();
822 break;
823 default:
824 break;
825 }
826 switch (S2) {
827 case ON_BASICBLOCK_MSG:
828 cerr << "' on BasicBlock '" << Msg << "...\n";
829 break;
830 case ON_FUNCTION_MSG:
831 cerr << "' on Function '" << Msg << "...\n";
832 break;
833 case ON_MODULE_MSG:
834 cerr << "' on Module '" << Msg << "...\n";
835 break;
836 case ON_LOOP_MSG:
837 cerr << "' on Loop " << Msg << "...\n";
838 break;
839 case ON_CG_MSG:
840 cerr << "' on Call Graph " << Msg << "...\n";
841 break;
842 default:
843 break;
844 }
Devang Patel991aeba2006-12-15 20:13:01 +0000845}
846
847void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
848 const std::vector<AnalysisID> &Set)
849 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000850 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000851 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
852 for (unsigned i = 0; i != Set.size(); ++i) {
853 if (i) cerr << ",";
854 cerr << " " << Set[i]->getPassName();
855 }
856 cerr << "\n";
857 }
858}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000859
Devang Patele7599552007-01-12 18:52:44 +0000860// Destructor
861PMDataManager::~PMDataManager() {
862
863 for (std::vector<Pass *>::iterator I = PassVector.begin(),
864 E = PassVector.end(); I != E; ++I)
865 delete *I;
866
867 PassVector.clear();
868}
869
Devang Patel9bdf7d42006-12-08 23:28:54 +0000870//===----------------------------------------------------------------------===//
871// NOTE: Is this the right place to define this method ?
872// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000873Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000874 return PM.findAnalysisPass(ID, dir);
875}
876
Devang Patela1514cb2006-12-07 19:39:39 +0000877//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000878// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000879
Devang Patel6e5a1132006-11-07 21:31:57 +0000880/// Execute all of the passes scheduled for execution by invoking
881/// runOnBasicBlock method. Keep track of whether any of the passes modifies
882/// the function, and if so, return true.
883bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000884BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000885
Reid Spencer5301e7c2007-01-30 20:08:39 +0000886 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000887 return false;
888
Devang Patele9585592006-12-08 01:38:28 +0000889 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000890
Devang Patel6e5a1132006-11-07 21:31:57 +0000891 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000892 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
893 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000894 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000895 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000896
Devang Patel003a5592007-03-05 20:01:30 +0000897 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000898 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000899
Devang Patelabfbe3b2006-12-16 00:56:26 +0000900 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000901
Devang Patelabfbe3b2006-12-16 00:56:26 +0000902 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000903 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000904 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000905
Devang Patel003a5592007-03-05 20:01:30 +0000906 if (Changed)
907 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000908 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000909
Devang Patelabfbe3b2006-12-16 00:56:26 +0000910 removeNotPreservedAnalysis(BP);
911 recordAvailableAnalysis(BP);
Devang Patel003a5592007-03-05 20:01:30 +0000912 removeDeadPasses(BP, (*I).getName(), ON_BASICBLOCK_MSG);
913
Devang Patel6e5a1132006-11-07 21:31:57 +0000914 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000915 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000916}
917
Devang Patel475c4532006-12-08 00:59:05 +0000918// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000919inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000920 bool Changed = false;
921
Devang Patelabfbe3b2006-12-16 00:56:26 +0000922 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
923 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000924 Changed |= BP->doInitialization(M);
925 }
926
927 return Changed;
928}
929
Devang Patel67d6a5e2006-12-19 19:46:59 +0000930inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000931 bool Changed = false;
932
Devang Patelabfbe3b2006-12-16 00:56:26 +0000933 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
934 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000935 Changed |= BP->doFinalization(M);
936 }
937
938 return Changed;
939}
940
Devang Patel67d6a5e2006-12-19 19:46:59 +0000941inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000942 bool Changed = false;
943
Devang Patelabfbe3b2006-12-16 00:56:26 +0000944 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
945 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000946 Changed |= BP->doInitialization(F);
947 }
948
949 return Changed;
950}
951
Devang Patel67d6a5e2006-12-19 19:46:59 +0000952inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000953 bool Changed = false;
954
Devang Patelabfbe3b2006-12-16 00:56:26 +0000955 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
956 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000957 Changed |= BP->doFinalization(F);
958 }
959
960 return Changed;
961}
962
963
Devang Patela1514cb2006-12-07 19:39:39 +0000964//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000965// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000966
Devang Patel4e12f862006-11-08 10:44:40 +0000967/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +0000968FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000969 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000970 // FPM is the top level manager.
971 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000972
973 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +0000974 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +0000975 FPM->setResolver(AR);
976
Devang Patel1f653682006-12-08 18:57:16 +0000977 MP = P;
978}
979
Devang Patelb67904d2006-12-13 02:36:01 +0000980FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000981 delete FPM;
982}
983
Devang Patel4e12f862006-11-08 10:44:40 +0000984/// add - Add a pass to the queue of passes to run. This passes
985/// ownership of the Pass to the PassManager. When the
986/// PassManager_X is destroyed, the pass will be destroyed as well, so
987/// there is no need to delete the pass. (TODO delete passes.)
988/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +0000989void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000990 FPM->add(P);
991}
992
Devang Patel9f3083e2006-11-15 19:39:54 +0000993/// run - Execute all of the passes scheduled for execution. Keep
994/// track of whether any of the passes modifies the function, and if
995/// so, return true.
996///
Devang Patelb67904d2006-12-13 02:36:01 +0000997bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000998 std::string errstr;
999 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001000 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001001 abort();
1002 }
Devang Patel272908d2006-12-08 22:57:48 +00001003 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001004}
1005
1006
Devang Patelff631ae2006-11-15 01:27:05 +00001007/// doInitialization - Run all of the initializers for the function passes.
1008///
Devang Patelb67904d2006-12-13 02:36:01 +00001009bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001010 return FPM->doInitialization(*MP->getModule());
1011}
1012
1013/// doFinalization - Run all of the initializers for the function passes.
1014///
Devang Patelb67904d2006-12-13 02:36:01 +00001015bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001016 return FPM->doFinalization(*MP->getModule());
1017}
1018
Devang Patela1514cb2006-12-07 19:39:39 +00001019//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001020// FunctionPassManagerImpl implementation
1021//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001022inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1023 bool Changed = false;
1024
1025 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1026 FPPassManager *FP = getContainedManager(Index);
1027 Changed |= FP->doInitialization(M);
1028 }
1029
1030 return Changed;
1031}
1032
1033inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1034 bool Changed = false;
1035
1036 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1037 FPPassManager *FP = getContainedManager(Index);
1038 Changed |= FP->doFinalization(M);
1039 }
1040
1041 return Changed;
1042}
1043
1044// Execute all the passes managed by this top level manager.
1045// Return true if any function is modified by a pass.
1046bool FunctionPassManagerImpl::run(Function &F) {
1047
1048 bool Changed = false;
1049
1050 TimingInfo::createTheTimeInfo();
1051
1052 dumpArguments();
1053 dumpPasses();
1054
Devang Patele3068402006-12-21 00:16:50 +00001055 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001056 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1057 FPPassManager *FP = getContainedManager(Index);
1058 Changed |= FP->runOnFunction(F);
1059 }
1060 return Changed;
1061}
1062
1063//===----------------------------------------------------------------------===//
1064// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001065
Devang Patele7599552007-01-12 18:52:44 +00001066/// Print passes managed by this manager
1067void FPPassManager::dumpPassStructure(unsigned Offset) {
1068 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1069 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1070 FunctionPass *FP = getContainedPass(Index);
1071 FP->dumpPassStructure(Offset + 1);
1072 dumpLastUses(FP, Offset+1);
1073 }
1074}
1075
1076
Devang Patel0c2012f2006-11-07 21:49:50 +00001077/// Execute all of the passes scheduled for execution by invoking
1078/// runOnFunction method. Keep track of whether any of the passes modifies
1079/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001080bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001081
1082 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001083
Reid Spencer5301e7c2007-01-30 20:08:39 +00001084 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001085 return false;
1086
Devang Patelabfbe3b2006-12-16 00:56:26 +00001087 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1088 FunctionPass *FP = getContainedPass(Index);
1089
Devang Patelf6d1d212006-12-14 00:25:06 +00001090 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001091 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001092
Devang Patel003a5592007-03-05 20:01:30 +00001093 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001094 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001095
Devang Patelabfbe3b2006-12-16 00:56:26 +00001096 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001097
Devang Patelabfbe3b2006-12-16 00:56:26 +00001098 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001099 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001100 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001101
Devang Patel003a5592007-03-05 20:01:30 +00001102 if (Changed)
1103 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001104 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001105
Devang Patelabfbe3b2006-12-16 00:56:26 +00001106 removeNotPreservedAnalysis(FP);
1107 recordAvailableAnalysis(FP);
Devang Patel003a5592007-03-05 20:01:30 +00001108 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001109 }
1110 return Changed;
1111}
1112
Devang Patel67d6a5e2006-12-19 19:46:59 +00001113bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001114
Devang Patel67d6a5e2006-12-19 19:46:59 +00001115 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001116
1117 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1118 this->runOnFunction(*I);
1119
1120 return Changed |= doFinalization(M);
1121}
1122
1123inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001124 bool Changed = false;
1125
Devang Patelabfbe3b2006-12-16 00:56:26 +00001126 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1127 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001128 Changed |= FP->doInitialization(M);
1129 }
1130
1131 return Changed;
1132}
1133
Devang Patel67d6a5e2006-12-19 19:46:59 +00001134inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001135 bool Changed = false;
1136
Devang Patelabfbe3b2006-12-16 00:56:26 +00001137 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1138 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001139 Changed |= FP->doFinalization(M);
1140 }
1141
Devang Patelff631ae2006-11-15 01:27:05 +00001142 return Changed;
1143}
1144
Devang Patela1514cb2006-12-07 19:39:39 +00001145//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001146// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001147
Devang Patel05e1a972006-11-07 22:03:15 +00001148/// Execute all of the passes scheduled for execution by invoking
1149/// runOnModule method. Keep track of whether any of the passes modifies
1150/// the module, and if so, return true.
1151bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001152MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001153 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001154
Devang Patelabfbe3b2006-12-16 00:56:26 +00001155 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1156 ModulePass *MP = getContainedPass(Index);
1157
Devang Patelf6d1d212006-12-14 00:25:06 +00001158 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001159 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001160
Devang Patel003a5592007-03-05 20:01:30 +00001161 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001162 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001163
Devang Patelabfbe3b2006-12-16 00:56:26 +00001164 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001165
Devang Patelabfbe3b2006-12-16 00:56:26 +00001166 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001167 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001168 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001169
Devang Patel003a5592007-03-05 20:01:30 +00001170 if (Changed)
1171 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1172 M.getModuleIdentifier());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001173 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001174
Devang Patelabfbe3b2006-12-16 00:56:26 +00001175 removeNotPreservedAnalysis(MP);
1176 recordAvailableAnalysis(MP);
Devang Patel003a5592007-03-05 20:01:30 +00001177 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001178 }
1179 return Changed;
1180}
1181
Devang Patele64d3052007-04-16 20:12:57 +00001182/// Add RequiredPass into list of lower level passes required by pass P.
1183/// RequiredPass is run on the fly by Pass Manager when P requests it
1184/// through getAnalysis interface.
1185void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1186
1187 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1188 && "Unable to handle Pass that requires lower level Analysis pass");
1189 assert ((P->getPotentialPassManagerType() <
1190 RequiredPass->getPotentialPassManagerType())
1191 && "Unable to handle Pass that requires lower level Analysis pass");
1192
1193 assert (0 &&
1194 "Unable to handle Pass that requires lower level Analysis pass");
1195}
1196
Devang Patela1514cb2006-12-07 19:39:39 +00001197//===----------------------------------------------------------------------===//
1198// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001199//
Devang Patelc290c8a2006-11-07 22:23:34 +00001200/// run - Execute all of the passes scheduled for execution. Keep track of
1201/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001202bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001203
Devang Patelc290c8a2006-11-07 22:23:34 +00001204 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001205
Devang Patelb8817b92006-12-14 00:59:42 +00001206 TimingInfo::createTheTimeInfo();
1207
Devang Patelcfd70c42006-12-13 22:10:00 +00001208 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001209 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001210
Devang Patele3068402006-12-21 00:16:50 +00001211 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001212 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1213 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001214 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001215 }
1216 return Changed;
1217}
Devang Patel376fefa2006-11-08 10:29:57 +00001218
Devang Patela1514cb2006-12-07 19:39:39 +00001219//===----------------------------------------------------------------------===//
1220// PassManager implementation
1221
Devang Patel376fefa2006-11-08 10:29:57 +00001222/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001223PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001224 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001225 // PM is the top level manager
1226 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001227}
1228
Devang Patelb67904d2006-12-13 02:36:01 +00001229PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001230 delete PM;
1231}
1232
Devang Patel376fefa2006-11-08 10:29:57 +00001233/// add - Add a pass to the queue of passes to run. This passes ownership of
1234/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1235/// will be destroyed as well, so there is no need to delete the pass. This
1236/// implies that all passes MUST be allocated with 'new'.
1237void
Devang Patelb67904d2006-12-13 02:36:01 +00001238PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001239 PM->add(P);
1240}
1241
1242/// run - Execute all of the passes scheduled for execution. Keep track of
1243/// whether any of the passes modifies the module, and if so, return true.
1244bool
Devang Patelb67904d2006-12-13 02:36:01 +00001245PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001246 return PM->run(M);
1247}
1248
Devang Patelb8817b92006-12-14 00:59:42 +00001249//===----------------------------------------------------------------------===//
1250// TimingInfo Class - This class is used to calculate information about the
1251// amount of time each pass takes to execute. This only happens with
1252// -time-passes is enabled on the command line.
1253//
1254bool llvm::TimePassesIsEnabled = false;
1255static cl::opt<bool,true>
1256EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1257 cl::desc("Time each pass, printing elapsed time for each on exit"));
1258
1259// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1260// a non null value (if the -time-passes option is enabled) or it leaves it
1261// null. It may be called multiple times.
1262void TimingInfo::createTheTimeInfo() {
1263 if (!TimePassesIsEnabled || TheTimeInfo) return;
1264
1265 // Constructed the first time this is called, iff -time-passes is enabled.
1266 // This guarantees that the object will be constructed before static globals,
1267 // thus it will be destroyed before them.
1268 static ManagedStatic<TimingInfo> TTI;
1269 TheTimeInfo = &*TTI;
1270}
1271
Devang Patel1c3633e2007-01-29 23:10:37 +00001272/// If TimingInfo is enabled then start pass timer.
1273void StartPassTimer(Pass *P) {
1274 if (TheTimeInfo)
1275 TheTimeInfo->passStarted(P);
1276}
1277
1278/// If TimingInfo is enabled then stop pass timer.
1279void StopPassTimer(Pass *P) {
1280 if (TheTimeInfo)
1281 TheTimeInfo->passEnded(P);
1282}
1283
Devang Patel1c56a632007-01-08 19:29:38 +00001284//===----------------------------------------------------------------------===//
1285// PMStack implementation
1286//
Devang Patelad98d232007-01-11 22:15:30 +00001287
Devang Patel1c56a632007-01-08 19:29:38 +00001288// Pop Pass Manager from the stack and clear its analysis info.
1289void PMStack::pop() {
1290
1291 PMDataManager *Top = this->top();
1292 Top->initializeAnalysisInfo();
1293
1294 S.pop_back();
1295}
1296
1297// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001298void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001299
Devang Patel15701b52007-01-11 00:19:00 +00001300 PMDataManager *Top = NULL;
1301 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1302 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001303
Devang Patel15701b52007-01-11 00:19:00 +00001304 if (this->empty()) {
1305 Top = PM;
1306 }
1307 else {
1308 Top = this->top();
1309 PMTopLevelManager *TPM = Top->getTopLevelManager();
1310
1311 assert (TPM && "Unable to find top level manager");
1312 TPM->addIndirectPassManager(PM);
1313 PM->setTopLevelManager(TPM);
1314 }
1315
1316 AnalysisResolver *AR = new AnalysisResolver(*Top);
1317 P->setResolver(AR);
1318
1319 S.push_back(PM);
1320}
1321
1322// Dump content of the pass manager stack.
1323void PMStack::dump() {
1324 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1325 E = S.end(); I != E; ++I) {
1326 Pass *P = dynamic_cast<Pass *>(*I);
1327 printf ("%s ", P->getPassName());
1328 }
1329 if (!S.empty())
1330 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001331}
1332
Devang Patel1c56a632007-01-08 19:29:38 +00001333/// Find appropriate Module Pass Manager in the PM Stack and
1334/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001335void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001336 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001337
Devang Patel1c56a632007-01-08 19:29:38 +00001338 // Find Module Pass Manager
1339 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001340 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1341 if (TopPMType == PreferredType)
1342 break; // We found desired pass manager
1343 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001344 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001345 else
1346 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001347 }
1348
Devang Patel23f8aa92007-01-17 21:19:23 +00001349 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001350}
1351
Devang Patel3312f752007-01-16 21:43:18 +00001352/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1353/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001354void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001355 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001356
Devang Patel15701b52007-01-11 00:19:00 +00001357 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001358 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001359 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1360 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001361 else
Devang Patel3312f752007-01-16 21:43:18 +00001362 break;
1363 }
1364 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1365
1366 // Create new Function Pass Manager
1367 if (!FPP) {
1368 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1369 PMDataManager *PMD = PMS.top();
1370
1371 // [1] Create new Function Pass Manager
1372 FPP = new FPPassManager(PMD->getDepth() + 1);
1373
1374 // [2] Set up new manager's top level manager
1375 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1376 TPM->addIndirectPassManager(FPP);
1377
1378 // [3] Assign manager to manage this new manager. This may create
1379 // and push new managers into PMS
1380 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001381
1382 // If Call Graph Pass Manager is active then use it to manage
1383 // this new Function Pass manager.
1384 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1385 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1386 else
1387 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001388
1389 // [4] Push new manager into PMS
1390 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001391 }
1392
Devang Patel3312f752007-01-16 21:43:18 +00001393 // Assign FPP as the manager of this pass.
1394 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001395}
1396
Devang Patel3312f752007-01-16 21:43:18 +00001397/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001398/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001399void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001400 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001401
1402 BBPassManager *BBP = NULL;
1403
Devang Patel15701b52007-01-11 00:19:00 +00001404 // Basic Pass Manager is a leaf pass manager. It does not handle
1405 // any other pass manager.
1406 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001407 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001408 }
1409
Devang Patel3312f752007-01-16 21:43:18 +00001410 // If leaf manager is not Basic Block Pass manager then create new
1411 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001412
Devang Patel3312f752007-01-16 21:43:18 +00001413 if (!BBP) {
1414 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1415 PMDataManager *PMD = PMS.top();
1416
1417 // [1] Create new Basic Block Manager
1418 BBP = new BBPassManager(PMD->getDepth() + 1);
1419
1420 // [2] Set up new manager's top level manager
1421 // Basic Block Pass Manager does not live by itself
1422 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1423 TPM->addIndirectPassManager(BBP);
1424
Devang Patel15701b52007-01-11 00:19:00 +00001425 // [3] Assign manager to manage this new manager. This may create
1426 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001427 Pass *P = dynamic_cast<Pass *>(BBP);
1428 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001429
Devang Patel3312f752007-01-16 21:43:18 +00001430 // [4] Push new manager into PMS
1431 PMS.push(BBP);
1432 }
Devang Patel1c56a632007-01-08 19:29:38 +00001433
Devang Patel3312f752007-01-16 21:43:18 +00001434 // Assign BBP as the manager of this pass.
1435 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001436}
1437
Devang Patelc6b5a552007-01-05 20:16:23 +00001438