blob: 6fadc84200642643563c5a6d44aabd2d14365e92 [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 Patel2ff44922007-04-16 20:39:59 +0000183
184 // Delete on the fly managers.
185 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000186 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000187 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
188 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000189 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000190 delete FPP;
191 }
192 }
193
Devang Patelca58e352006-11-08 10:05:38 +0000194 /// run - Execute all of the passes scheduled for execution. Keep track of
195 /// whether any of the passes modifies the module, and if so, return true.
196 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000197
Devang Patelf9d96b92006-12-07 19:57:52 +0000198 /// Pass Manager itself does not invalidate any analysis info.
199 void getAnalysisUsage(AnalysisUsage &Info) const {
200 Info.setPreservesAll();
201 }
202
Devang Patele64d3052007-04-16 20:12:57 +0000203 /// Add RequiredPass into list of lower level passes required by pass P.
204 /// RequiredPass is run on the fly by Pass Manager when P requests it
205 /// through getAnalysis interface.
206 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
207
Devang Patel69e9f6d2007-04-16 20:27:05 +0000208 /// Return function pass corresponding to PassInfo PI, that is
209 /// required by module pass MP. Instantiate analysis pass, by using
210 /// its runOnFunction() for function F.
211 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
212
Devang Patele3858e62007-02-01 22:08:25 +0000213 virtual const char *getPassName() const {
214 return "Module Pass Manager";
215 }
216
Devang Pateleda56172006-12-12 23:34:33 +0000217 // Print passes managed by this manager
218 void dumpPassStructure(unsigned Offset) {
219 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000220 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
221 ModulePass *MP = getContainedPass(Index);
222 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000223 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000224 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000225 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000226 }
227 }
228
Devang Patelabfbe3b2006-12-16 00:56:26 +0000229 ModulePass *getContainedPass(unsigned N) {
230 assert ( N < PassVector.size() && "Pass number out of range!");
231 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
232 return MP;
233 }
234
Devang Patel28349ab2007-02-27 15:00:39 +0000235 virtual PassManagerType getPassManagerType() const {
236 return PMT_ModulePassManager;
237 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000238
239 private:
240 /// Collection of on the fly FPPassManagers. These managers manage
241 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000242 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000243};
244
Devang Patel10c2ca62006-12-12 22:47:13 +0000245//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000246// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000247//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000248/// PassManagerImpl manages MPPassManagers
249class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000250 public PMDataManager,
251 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000252
253public:
254
Devang Patel4268fc02007-01-16 02:00:38 +0000255 PassManagerImpl(int Depth) : PMDataManager(Depth),
256 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000257
Devang Patel376fefa2006-11-08 10:29:57 +0000258 /// add - Add a pass to the queue of passes to run. This passes ownership of
259 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
260 /// will be destroyed as well, so there is no need to delete the pass. This
261 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000262 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000263 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000264 }
Devang Patel376fefa2006-11-08 10:29:57 +0000265
266 /// run - Execute all of the passes scheduled for execution. Keep track of
267 /// whether any of the passes modifies the module, and if so, return true.
268 bool run(Module &M);
269
Devang Patelf9d96b92006-12-07 19:57:52 +0000270 /// Pass Manager itself does not invalidate any analysis info.
271 void getAnalysisUsage(AnalysisUsage &Info) const {
272 Info.setPreservesAll();
273 }
274
Devang Patelabcd1d32006-12-07 21:27:23 +0000275 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000276
Devang Patelfa971cd2006-12-08 23:57:43 +0000277 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000278
279 // P is a immutable pass and it will be managed by this
280 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000281 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000282 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000283 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000284 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000285 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000286 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000287 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000288 }
Devang Patel0f080042007-01-12 17:23:48 +0000289
Devang Patelabcd1d32006-12-07 21:27:23 +0000290 }
291
Devang Patel67d6a5e2006-12-19 19:46:59 +0000292 MPPassManager *getContainedManager(unsigned N) {
293 assert ( N < PassManagers.size() && "Pass number out of range!");
294 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
295 return MP;
296 }
297
Devang Patel376fefa2006-11-08 10:29:57 +0000298};
299
Devang Patel1c3633e2007-01-29 23:10:37 +0000300} // End of llvm namespace
301
302namespace {
303
304//===----------------------------------------------------------------------===//
305// TimingInfo Class - This class is used to calculate information about the
306// amount of time each pass takes to execute. This only happens when
307// -time-passes is enabled on the command line.
308//
309
310class VISIBILITY_HIDDEN TimingInfo {
311 std::map<Pass*, Timer> TimingData;
312 TimerGroup TG;
313
314public:
315 // Use 'create' member to get this.
316 TimingInfo() : TG("... Pass execution timing report ...") {}
317
318 // TimingDtor - Print out information about timing information
319 ~TimingInfo() {
320 // Delete all of the timers...
321 TimingData.clear();
322 // TimerGroup is deleted next, printing the report.
323 }
324
325 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
326 // to a non null value (if the -time-passes option is enabled) or it leaves it
327 // null. It may be called multiple times.
328 static void createTheTimeInfo();
329
330 void passStarted(Pass *P) {
331
332 if (dynamic_cast<PMDataManager *>(P))
333 return;
334
335 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
336 if (I == TimingData.end())
337 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
338 I->second.startTimer();
339 }
340 void passEnded(Pass *P) {
341
342 if (dynamic_cast<PMDataManager *>(P))
343 return;
344
345 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
346 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
347 I->second.stopTimer();
348 }
349};
350
Devang Patelb8817b92006-12-14 00:59:42 +0000351static TimingInfo *TheTimeInfo;
352
Devang Patel1c3633e2007-01-29 23:10:37 +0000353} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000354
Devang Patela1514cb2006-12-07 19:39:39 +0000355//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000356// PMTopLevelManager implementation
357
Devang Patel4268fc02007-01-16 02:00:38 +0000358/// Initialize top level manager. Create first pass manager.
359PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
360
361 if (t == TLM_Pass) {
362 MPPassManager *MPP = new MPPassManager(1);
363 MPP->setTopLevelManager(this);
364 addPassManager(MPP);
365 activeStack.push(MPP);
366 }
367 else if (t == TLM_Function) {
368 FPPassManager *FPP = new FPPassManager(1);
369 FPP->setTopLevelManager(this);
370 addPassManager(FPP);
371 activeStack.push(FPP);
372 }
373}
374
Devang Patelafb1f3622006-12-12 22:35:25 +0000375/// Set pass P as the last user of the given analysis passes.
376void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
377 Pass *P) {
378
379 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
380 E = AnalysisPasses.end(); I != E; ++I) {
381 Pass *AP = *I;
382 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000383
384 if (P == AP)
385 continue;
386
Devang Patelafb1f3622006-12-12 22:35:25 +0000387 // If AP is the last user of other passes then make P last user of
388 // such passes.
389 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
390 LUE = LastUser.end(); LUI != LUE; ++LUI) {
391 if (LUI->second == AP)
392 LastUser[LUI->first] = P;
393 }
394 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000395}
396
397/// Collect passes whose last user is P
398void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
399 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000400 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
401 LUE = LastUser.end(); LUI != LUE; ++LUI)
402 if (LUI->second == P)
403 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000404}
405
406/// Schedule pass P for execution. Make sure that passes required by
407/// P are run before P is run. Update analysis info maintained by
408/// the manager. Remove dead passes. This is a recursive function.
409void PMTopLevelManager::schedulePass(Pass *P) {
410
Devang Patel3312f752007-01-16 21:43:18 +0000411 // TODO : Allocate function manager for this pass, other wise required set
412 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000413
Devang Patel3f806962007-02-05 19:34:17 +0000414 // If this Analysis is already requested by one of the previous pass
415 // and it is still available then do not insert new pass in the queue again.
416 if (findAnalysisPass(P->getPassInfo()))
417 return;
418
Devang Pateld74ede72007-03-06 01:06:16 +0000419 // Give pass a chance to prepare the stage.
420 P->preparePassManager(activeStack);
421
Devang Patelafb1f3622006-12-12 22:35:25 +0000422 AnalysisUsage AnUsage;
423 P->getAnalysisUsage(AnUsage);
424 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
425 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
426 E = RequiredSet.end(); I != E; ++I) {
427
428 Pass *AnalysisPass = findAnalysisPass(*I);
429 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000430 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000431 // Schedule this analysis run first only if it is not a lower level
432 // analysis pass. Lower level analsyis passes are run on the fly.
433 if (P->getPotentialPassManagerType () >=
434 AnalysisPass->getPotentialPassManagerType())
435 schedulePass(AnalysisPass);
436 else
437 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000438 }
439 }
440
441 // Now all required passes are available.
442 addTopLevelPass(P);
443}
444
445/// Find the pass that implements Analysis AID. Search immutable
446/// passes and all pass managers. If desired pass is not found
447/// then return NULL.
448Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
449
450 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000451 // Check pass managers
452 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
453 E = PassManagers.end(); P == NULL && I != E; ++I) {
454 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
455 assert(PMD && "This is not a PassManager");
456 P = PMD->findAnalysisPass(AID, false);
457 }
458
459 // Check other pass managers
460 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
461 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
462 P = (*I)->findAnalysisPass(AID, false);
463
Devang Patelafb1f3622006-12-12 22:35:25 +0000464 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
465 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
466 const PassInfo *PI = (*I)->getPassInfo();
467 if (PI == AID)
468 P = *I;
469
470 // If Pass not found then check the interfaces implemented by Immutable Pass
471 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000472 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
473 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
474 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000475 }
476 }
477
Devang Patelafb1f3622006-12-12 22:35:25 +0000478 return P;
479}
480
Devang Pateleda56172006-12-12 23:34:33 +0000481// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000482void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000483
Devang Patelfd4184322007-01-17 20:33:36 +0000484 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000485 return;
486
Devang Pateleda56172006-12-12 23:34:33 +0000487 // Print out the immutable passes
488 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
489 ImmutablePasses[i]->dumpPassStructure(0);
490 }
491
Devang Patel991aeba2006-12-15 20:13:01 +0000492 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000493 E = PassManagers.end(); I != E; ++I)
494 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000495}
496
Devang Patel991aeba2006-12-15 20:13:01 +0000497void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000498
Devang Patelfd4184322007-01-17 20:33:36 +0000499 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000500 return;
501
502 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000503 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000504 E = PassManagers.end(); I != E; ++I) {
505 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
506 assert(PMD && "This is not a PassManager");
507 PMD->dumpPassArguments();
508 }
509 cerr << "\n";
510}
511
Devang Patele3068402006-12-21 00:16:50 +0000512void PMTopLevelManager::initializeAllAnalysisInfo() {
513
514 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
515 E = PassManagers.end(); I != E; ++I) {
516 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
517 assert(PMD && "This is not a PassManager");
518 PMD->initializeAnalysisInfo();
519 }
520
521 // Initailize other pass managers
522 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
523 E = IndirectPassManagers.end(); I != E; ++I)
524 (*I)->initializeAnalysisInfo();
525}
526
Devang Patele7599552007-01-12 18:52:44 +0000527/// Destructor
528PMTopLevelManager::~PMTopLevelManager() {
529 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
530 E = PassManagers.end(); I != E; ++I)
531 delete *I;
532
533 for (std::vector<ImmutablePass *>::iterator
534 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
535 delete *I;
536
537 PassManagers.clear();
538}
539
Devang Patelafb1f3622006-12-12 22:35:25 +0000540//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000541// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000542
Devang Pateld65e9e92006-11-08 01:31:28 +0000543/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000544/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000545bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000546
Devang Patel8f677ce2006-12-07 18:47:25 +0000547 // TODO
548 // If this pass is not preserving information that is required by a
549 // pass maintained by higher level pass manager then do not insert
550 // this pass into current manager. Use new manager. For example,
551 // For example, If FunctionPass F is not preserving ModulePass Info M1
552 // that is used by another ModulePass M2 then do not insert F in
553 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000554 return true;
555}
556
Devang Patel643676c2006-11-11 01:10:19 +0000557/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000558void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000559
Devang Patel643676c2006-11-11 01:10:19 +0000560 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000561 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000562
Devang Patele9976aa2006-12-07 19:33:53 +0000563 //This pass is the current implementation of all of the interfaces it
564 //implements as well.
565 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
566 for (unsigned i = 0, e = II.size(); i != e; ++i)
567 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000568 }
569}
570
Devang Patel9d9fc902007-03-06 17:52:53 +0000571// Return true if P preserves high level analysis used by other
572// passes managed by this manager
573bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
574
575 AnalysisUsage AnUsage;
576 P->getAnalysisUsage(AnUsage);
577
578 if (AnUsage.getPreservesAll())
579 return true;
580
581 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
582 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
583 E = HigherLevelAnalysis.end(); I != E; ++I) {
584 Pass *P1 = *I;
Devang Patel01919d22007-03-08 19:05:01 +0000585 if (!dynamic_cast<ImmutablePass*>(P1)
586 && std::find(PreservedSet.begin(), PreservedSet.end(), P1->getPassInfo()) ==
587 PreservedSet.end())
588 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000589 }
590
591 return true;
592}
593
Devang Patelf68a3492006-11-07 22:35:17 +0000594/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000595void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000596 AnalysisUsage AnUsage;
597 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000598
Devang Patel2e169c32006-12-07 20:03:49 +0000599 if (AnUsage.getPreservesAll())
600 return;
601
602 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000603 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000604 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000605 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000606 if (!dynamic_cast<ImmutablePass*>(Info->second)
607 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
608 PreservedSet.end())
Devang Patel349170f2006-11-11 01:24:55 +0000609 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000610 AvailableAnalysis.erase(Info);
Devang Patel349170f2006-11-11 01:24:55 +0000611 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000612
613 // Check inherited analysis also. If P is not preserving analysis
614 // provided by parent manager then remove it here.
615 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
616
617 if (!InheritedAnalysis[Index])
618 continue;
619
620 for (std::map<AnalysisID, Pass*>::iterator
621 I = InheritedAnalysis[Index]->begin(),
622 E = InheritedAnalysis[Index]->end(); I != E; ) {
623 std::map<AnalysisID, Pass *>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000624 if (!dynamic_cast<ImmutablePass*>(Info->second)
625 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
626 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000627 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000628 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000629 }
630 }
631
Devang Patelf68a3492006-11-07 22:35:17 +0000632}
633
Devang Patelca189262006-11-14 03:05:08 +0000634/// Remove analysis passes that are not used any longer
Devang Patel003a5592007-03-05 20:01:30 +0000635void PMDataManager::removeDeadPasses(Pass *P, std::string Msg,
636 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000637
638 std::vector<Pass *> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000639
Devang Patel2ff44922007-04-16 20:39:59 +0000640 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000641 if (!TPM)
642 return;
643
Devang Patel17ad0962006-12-08 00:37:52 +0000644 TPM->collectLastUses(DeadPasses, P);
645
646 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
647 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000648
Devang Patel003a5592007-03-05 20:01:30 +0000649 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000650
Devang Patel7ebf09d2007-03-05 18:20:51 +0000651 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000652 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000653 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000654
Devang Patel17ad0962006-12-08 00:37:52 +0000655 std::map<AnalysisID, Pass*>::iterator Pos =
656 AvailableAnalysis.find((*I)->getPassInfo());
657
Devang Patel475c4532006-12-08 00:59:05 +0000658 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000659 if (Pos != AvailableAnalysis.end())
660 AvailableAnalysis.erase(Pos);
661 }
Devang Patelca189262006-11-14 03:05:08 +0000662}
663
Devang Patel8f677ce2006-12-07 18:47:25 +0000664/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000665/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000666void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000667 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000668
Devang Pateld440cd92006-12-08 23:53:00 +0000669 // This manager is going to manage pass P. Set up analysis resolver
670 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000671 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000672 P->setResolver(AR);
673
Devang Patelec2b9a72007-03-05 22:57:49 +0000674 // If a FunctionPass F is the last user of ModulePass info M
675 // then the F's manager, not F, records itself as a last user of M.
676 std::vector<Pass *> TransferLastUses;
677
Devang Patel90b05e02006-11-11 02:04:19 +0000678 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000679
680 // At the moment, this pass is the last user of all required passes.
681 std::vector<Pass *> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000682 SmallVector<Pass *, 8> RequiredPasses;
683 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
684
Devang Patelbc03f132006-12-07 23:55:10 +0000685 unsigned PDepth = this->getDepth();
686
Devang Patele64d3052007-04-16 20:12:57 +0000687 collectRequiredAnalysis(RequiredPasses,
688 ReqAnalysisNotAvailable, P);
689 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000690 E = RequiredPasses.end(); I != E; ++I) {
691 Pass *PRequired = *I;
692 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000693
694 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
695 RDepth = DM.getDepth();
696
Devang Patelbc03f132006-12-07 23:55:10 +0000697 if (PDepth == RDepth)
698 LastUses.push_back(PRequired);
699 else if (PDepth > RDepth) {
700 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000701 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000702 // Keep track of higher level analysis used by this manager.
703 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000704 } else
705 assert (0 && "Unable to accomodate Required Pass");
706 }
707
Devang Patel39786a92007-01-15 20:31:54 +0000708 // Set P as P's last user until someone starts using P.
709 // However, if P is a Pass Manager then it does not need
710 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000711 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000712 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000713 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000714
Devang Patelec2b9a72007-03-05 22:57:49 +0000715 if (!TransferLastUses.empty()) {
716 Pass *My_PM = dynamic_cast<Pass *>(this);
717 TPM->setLastUser(TransferLastUses, My_PM);
718 TransferLastUses.clear();
719 }
720
Devang Patel69e9f6d2007-04-16 20:27:05 +0000721 // Now, take care of required analysises that are not available.
722 for (SmallVector<AnalysisID, 8>::iterator
723 I = ReqAnalysisNotAvailable.begin(),
724 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
725 Pass *AnalysisPass = (*I)->createPass();
726 this->addLowerLevelRequiredPass(P, AnalysisPass);
727 }
728
Devang Patel17bff0d2006-12-07 22:09:36 +0000729 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000730 // Remove the analysis not preserved by this pass
731 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000732 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000733 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000734
735 // Add pass
736 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000737}
738
Devang Patele64d3052007-04-16 20:12:57 +0000739
740/// Populate RP with analysis pass that are required by
741/// pass P and are available. Populate RP_NotAvail with analysis
742/// pass that are required by pass P but are not available.
743void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
744 SmallVector<AnalysisID, 8> &RP_NotAvail,
745 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000746 AnalysisUsage AnUsage;
747 P->getAnalysisUsage(AnUsage);
748 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
749 for (std::vector<AnalysisID>::const_iterator
750 I = RequiredSet.begin(), E = RequiredSet.end();
751 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000752 AnalysisID AID = *I;
753 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
754 RP.push_back(AnalysisPass);
755 else
756 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000757 }
Devang Patelf58183d2006-12-12 23:09:32 +0000758
759 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
760 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
761 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000762 AnalysisID AID = *I;
763 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
764 RP.push_back(AnalysisPass);
765 else
766 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000767 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000768}
769
Devang Patel07f4f582006-11-14 21:49:36 +0000770// All Required analyses should be available to the pass as it runs! Here
771// we fill in the AnalysisImpls member of the pass so that it can
772// successfully use the getAnalysis() method to retrieve the
773// implementations it needs.
774//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000775void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000776 AnalysisUsage AnUsage;
777 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000778
779 for (std::vector<const PassInfo *>::const_iterator
780 I = AnUsage.getRequiredSet().begin(),
781 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000782 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000783 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000784 // This may be analysis pass that is initialized on the fly.
785 // If that is not the case then it will raise an assert when it is used.
786 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000787 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000788 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000789 }
790}
791
Devang Patel640c5bb2006-12-08 22:30:11 +0000792/// Find the pass that implements Analysis AID. If desired pass is not found
793/// then return NULL.
794Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
795
796 // Check if AvailableAnalysis map has one entry.
797 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
798
799 if (I != AvailableAnalysis.end())
800 return I->second;
801
802 // Search Parents through TopLevelManager
803 if (SearchParent)
804 return TPM->findAnalysisPass(AID);
805
Devang Patel9d759b82006-12-09 00:09:12 +0000806 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000807}
808
Devang Patel991aeba2006-12-15 20:13:01 +0000809// Print list of passes that are last used by P.
810void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
811
812 std::vector<Pass *> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000813
814 // If this is a on the fly manager then it does not have TPM.
815 if (!TPM)
816 return;
817
Devang Patel991aeba2006-12-15 20:13:01 +0000818 TPM->collectLastUses(LUses, P);
819
820 for (std::vector<Pass *>::iterator I = LUses.begin(),
821 E = LUses.end(); I != E; ++I) {
822 llvm::cerr << "--" << std::string(Offset*2, ' ');
823 (*I)->dumpPassStructure(0);
824 }
825}
826
827void PMDataManager::dumpPassArguments() const {
828 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
829 E = PassVector.end(); I != E; ++I) {
830 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
831 PMD->dumpPassArguments();
832 else
833 if (const PassInfo *PI = (*I)->getPassInfo())
834 if (!PI->isAnalysisGroup())
835 cerr << " -" << PI->getPassArgument();
836 }
837}
838
Devang Patel003a5592007-03-05 20:01:30 +0000839void PMDataManager:: dumpPassInfo(Pass *P, enum PassDebuggingString S1,
840 enum PassDebuggingString S2,
841 std::string Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000842 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000843 return;
844 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000845 switch (S1) {
846 case EXECUTION_MSG:
847 cerr << "Executing Pass '" << P->getPassName();
848 break;
849 case MODIFICATION_MSG:
850 cerr << "' Made Modification '" << P->getPassName();
851 break;
852 case FREEING_MSG:
853 cerr << " Freeing Pass '" << P->getPassName();
854 break;
855 default:
856 break;
857 }
858 switch (S2) {
859 case ON_BASICBLOCK_MSG:
860 cerr << "' on BasicBlock '" << Msg << "...\n";
861 break;
862 case ON_FUNCTION_MSG:
863 cerr << "' on Function '" << Msg << "...\n";
864 break;
865 case ON_MODULE_MSG:
866 cerr << "' on Module '" << Msg << "...\n";
867 break;
868 case ON_LOOP_MSG:
869 cerr << "' on Loop " << Msg << "...\n";
870 break;
871 case ON_CG_MSG:
872 cerr << "' on Call Graph " << Msg << "...\n";
873 break;
874 default:
875 break;
876 }
Devang Patel991aeba2006-12-15 20:13:01 +0000877}
878
879void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
880 const std::vector<AnalysisID> &Set)
881 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000882 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000883 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
884 for (unsigned i = 0; i != Set.size(); ++i) {
885 if (i) cerr << ",";
886 cerr << " " << Set[i]->getPassName();
887 }
888 cerr << "\n";
889 }
890}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000891
Devang Patele7599552007-01-12 18:52:44 +0000892// Destructor
893PMDataManager::~PMDataManager() {
894
895 for (std::vector<Pass *>::iterator I = PassVector.begin(),
896 E = PassVector.end(); I != E; ++I)
897 delete *I;
898
899 PassVector.clear();
900}
901
Devang Patel9bdf7d42006-12-08 23:28:54 +0000902//===----------------------------------------------------------------------===//
903// NOTE: Is this the right place to define this method ?
904// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000905Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000906 return PM.findAnalysisPass(ID, dir);
907}
908
Devang Patel92942812007-04-16 20:56:24 +0000909Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
910 Function &F) {
911 return PM.getOnTheFlyPass(P, AnalysisPI, F);
912}
913
Devang Patela1514cb2006-12-07 19:39:39 +0000914//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000915// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000916
Devang Patel6e5a1132006-11-07 21:31:57 +0000917/// Execute all of the passes scheduled for execution by invoking
918/// runOnBasicBlock method. Keep track of whether any of the passes modifies
919/// the function, and if so, return true.
920bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000921BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000922
Reid Spencer5301e7c2007-01-30 20:08:39 +0000923 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000924 return false;
925
Devang Patele9585592006-12-08 01:38:28 +0000926 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000927
Devang Patel6e5a1132006-11-07 21:31:57 +0000928 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000929 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
930 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000931 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000932 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000933
Devang Patel003a5592007-03-05 20:01:30 +0000934 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000935 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000936
Devang Patelabfbe3b2006-12-16 00:56:26 +0000937 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000938
Devang Patelabfbe3b2006-12-16 00:56:26 +0000939 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000940 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000941 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000942
Devang Patel003a5592007-03-05 20:01:30 +0000943 if (Changed)
944 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000945 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000946
Devang Patelabfbe3b2006-12-16 00:56:26 +0000947 removeNotPreservedAnalysis(BP);
948 recordAvailableAnalysis(BP);
Devang Patel003a5592007-03-05 20:01:30 +0000949 removeDeadPasses(BP, (*I).getName(), ON_BASICBLOCK_MSG);
950
Devang Patel6e5a1132006-11-07 21:31:57 +0000951 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000952 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000953}
954
Devang Patel475c4532006-12-08 00:59:05 +0000955// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000956inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000957 bool Changed = false;
958
Devang Patelabfbe3b2006-12-16 00:56:26 +0000959 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
960 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000961 Changed |= BP->doInitialization(M);
962 }
963
964 return Changed;
965}
966
Devang Patel67d6a5e2006-12-19 19:46:59 +0000967inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000968 bool Changed = false;
969
Devang Patelabfbe3b2006-12-16 00:56:26 +0000970 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
971 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000972 Changed |= BP->doFinalization(M);
973 }
974
975 return Changed;
976}
977
Devang Patel67d6a5e2006-12-19 19:46:59 +0000978inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000979 bool Changed = false;
980
Devang Patelabfbe3b2006-12-16 00:56:26 +0000981 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
982 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000983 Changed |= BP->doInitialization(F);
984 }
985
986 return Changed;
987}
988
Devang Patel67d6a5e2006-12-19 19:46:59 +0000989inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000990 bool Changed = false;
991
Devang Patelabfbe3b2006-12-16 00:56:26 +0000992 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
993 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000994 Changed |= BP->doFinalization(F);
995 }
996
997 return Changed;
998}
999
1000
Devang Patela1514cb2006-12-07 19:39:39 +00001001//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001002// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001003
Devang Patel4e12f862006-11-08 10:44:40 +00001004/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001005FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001006 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001007 // FPM is the top level manager.
1008 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001009
1010 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +00001011 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +00001012 FPM->setResolver(AR);
1013
Devang Patel1f653682006-12-08 18:57:16 +00001014 MP = P;
1015}
1016
Devang Patelb67904d2006-12-13 02:36:01 +00001017FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001018 delete FPM;
1019}
1020
Devang Patel4e12f862006-11-08 10:44:40 +00001021/// add - Add a pass to the queue of passes to run. This passes
1022/// ownership of the Pass to the PassManager. When the
1023/// PassManager_X is destroyed, the pass will be destroyed as well, so
1024/// there is no need to delete the pass. (TODO delete passes.)
1025/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001026void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001027 FPM->add(P);
1028}
1029
Devang Patel9f3083e2006-11-15 19:39:54 +00001030/// run - Execute all of the passes scheduled for execution. Keep
1031/// track of whether any of the passes modifies the function, and if
1032/// so, return true.
1033///
Devang Patelb67904d2006-12-13 02:36:01 +00001034bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001035 std::string errstr;
1036 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001037 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001038 abort();
1039 }
Devang Patel272908d2006-12-08 22:57:48 +00001040 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001041}
1042
1043
Devang Patelff631ae2006-11-15 01:27:05 +00001044/// doInitialization - Run all of the initializers for the function passes.
1045///
Devang Patelb67904d2006-12-13 02:36:01 +00001046bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001047 return FPM->doInitialization(*MP->getModule());
1048}
1049
1050/// doFinalization - Run all of the initializers for the function passes.
1051///
Devang Patelb67904d2006-12-13 02:36:01 +00001052bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001053 return FPM->doFinalization(*MP->getModule());
1054}
1055
Devang Patela1514cb2006-12-07 19:39:39 +00001056//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001057// FunctionPassManagerImpl implementation
1058//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001059inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1060 bool Changed = false;
1061
1062 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1063 FPPassManager *FP = getContainedManager(Index);
1064 Changed |= FP->doInitialization(M);
1065 }
1066
1067 return Changed;
1068}
1069
1070inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1071 bool Changed = false;
1072
1073 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1074 FPPassManager *FP = getContainedManager(Index);
1075 Changed |= FP->doFinalization(M);
1076 }
1077
1078 return Changed;
1079}
1080
1081// Execute all the passes managed by this top level manager.
1082// Return true if any function is modified by a pass.
1083bool FunctionPassManagerImpl::run(Function &F) {
1084
1085 bool Changed = false;
1086
1087 TimingInfo::createTheTimeInfo();
1088
1089 dumpArguments();
1090 dumpPasses();
1091
Devang Patele3068402006-12-21 00:16:50 +00001092 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001093 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1094 FPPassManager *FP = getContainedManager(Index);
1095 Changed |= FP->runOnFunction(F);
1096 }
1097 return Changed;
1098}
1099
1100//===----------------------------------------------------------------------===//
1101// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001102
Devang Patele7599552007-01-12 18:52:44 +00001103/// Print passes managed by this manager
1104void FPPassManager::dumpPassStructure(unsigned Offset) {
1105 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1106 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1107 FunctionPass *FP = getContainedPass(Index);
1108 FP->dumpPassStructure(Offset + 1);
1109 dumpLastUses(FP, Offset+1);
1110 }
1111}
1112
1113
Devang Patel0c2012f2006-11-07 21:49:50 +00001114/// Execute all of the passes scheduled for execution by invoking
1115/// runOnFunction method. Keep track of whether any of the passes modifies
1116/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001117bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001118
1119 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001120
Reid Spencer5301e7c2007-01-30 20:08:39 +00001121 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001122 return false;
1123
Devang Patelabfbe3b2006-12-16 00:56:26 +00001124 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1125 FunctionPass *FP = getContainedPass(Index);
1126
Devang Patelf6d1d212006-12-14 00:25:06 +00001127 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001128 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001129
Devang Patel003a5592007-03-05 20:01:30 +00001130 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001131 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001132
Devang Patelabfbe3b2006-12-16 00:56:26 +00001133 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001134
Devang Patelabfbe3b2006-12-16 00:56:26 +00001135 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001136 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001137 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001138
Devang Patel003a5592007-03-05 20:01:30 +00001139 if (Changed)
1140 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001141 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001142
Devang Patelabfbe3b2006-12-16 00:56:26 +00001143 removeNotPreservedAnalysis(FP);
1144 recordAvailableAnalysis(FP);
Devang Patel003a5592007-03-05 20:01:30 +00001145 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001146 }
1147 return Changed;
1148}
1149
Devang Patel67d6a5e2006-12-19 19:46:59 +00001150bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001151
Devang Patel67d6a5e2006-12-19 19:46:59 +00001152 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001153
1154 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1155 this->runOnFunction(*I);
1156
1157 return Changed |= doFinalization(M);
1158}
1159
1160inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001161 bool Changed = false;
1162
Devang Patelabfbe3b2006-12-16 00:56:26 +00001163 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1164 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001165 Changed |= FP->doInitialization(M);
1166 }
1167
1168 return Changed;
1169}
1170
Devang Patel67d6a5e2006-12-19 19:46:59 +00001171inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001172 bool Changed = false;
1173
Devang Patelabfbe3b2006-12-16 00:56:26 +00001174 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1175 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001176 Changed |= FP->doFinalization(M);
1177 }
1178
Devang Patelff631ae2006-11-15 01:27:05 +00001179 return Changed;
1180}
1181
Devang Patela1514cb2006-12-07 19:39:39 +00001182//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001183// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001184
Devang Patel05e1a972006-11-07 22:03:15 +00001185/// Execute all of the passes scheduled for execution by invoking
1186/// runOnModule method. Keep track of whether any of the passes modifies
1187/// the module, and if so, return true.
1188bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001189MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001190 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001191
Devang Patelabfbe3b2006-12-16 00:56:26 +00001192 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1193 ModulePass *MP = getContainedPass(Index);
1194
Devang Patelf6d1d212006-12-14 00:25:06 +00001195 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001196 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001197
Devang Patel003a5592007-03-05 20:01:30 +00001198 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001199 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001200
Devang Patelabfbe3b2006-12-16 00:56:26 +00001201 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001202
Devang Patelabfbe3b2006-12-16 00:56:26 +00001203 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001204 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001205 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001206
Devang Patel003a5592007-03-05 20:01:30 +00001207 if (Changed)
1208 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1209 M.getModuleIdentifier());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001210 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001211
Devang Patelabfbe3b2006-12-16 00:56:26 +00001212 removeNotPreservedAnalysis(MP);
1213 recordAvailableAnalysis(MP);
Devang Patel003a5592007-03-05 20:01:30 +00001214 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001215 }
1216 return Changed;
1217}
1218
Devang Patele64d3052007-04-16 20:12:57 +00001219/// Add RequiredPass into list of lower level passes required by pass P.
1220/// RequiredPass is run on the fly by Pass Manager when P requests it
1221/// through getAnalysis interface.
1222void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1223
1224 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1225 && "Unable to handle Pass that requires lower level Analysis pass");
1226 assert ((P->getPotentialPassManagerType() <
1227 RequiredPass->getPotentialPassManagerType())
1228 && "Unable to handle Pass that requires lower level Analysis pass");
1229
Devang Patel68f72b12007-04-26 17:50:19 +00001230 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001231 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001232 FPP = new FunctionPassManagerImpl(0);
1233 // FPP is the top level manager.
1234 FPP->setTopLevelManager(FPP);
1235
Devang Patel69e9f6d2007-04-16 20:27:05 +00001236 OnTheFlyManagers[P] = FPP;
1237 }
Devang Patel68f72b12007-04-26 17:50:19 +00001238 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001239
Devang Patel68f72b12007-04-26 17:50:19 +00001240 // Register P as the last user of RequiredPass.
1241 std::vector<Pass *> LU;
1242 LU.push_back(RequiredPass);
1243 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001244}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001245
1246/// Return function pass corresponding to PassInfo PI, that is
1247/// required by module pass MP. Instantiate analysis pass, by using
1248/// its runOnFunction() for function F.
1249Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1250 Function &F) {
1251 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001252 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001253 assert (FPP && "Unable to find on the fly pass");
1254
Devang Patel68f72b12007-04-26 17:50:19 +00001255 FPP->run(F);
1256 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001257}
1258
1259
Devang Patela1514cb2006-12-07 19:39:39 +00001260//===----------------------------------------------------------------------===//
1261// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001262//
Devang Patelc290c8a2006-11-07 22:23:34 +00001263/// run - Execute all of the passes scheduled for execution. Keep track of
1264/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001265bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001266
Devang Patelc290c8a2006-11-07 22:23:34 +00001267 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001268
Devang Patelb8817b92006-12-14 00:59:42 +00001269 TimingInfo::createTheTimeInfo();
1270
Devang Patelcfd70c42006-12-13 22:10:00 +00001271 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001272 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001273
Devang Patele3068402006-12-21 00:16:50 +00001274 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001275 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1276 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001277 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001278 }
1279 return Changed;
1280}
Devang Patel376fefa2006-11-08 10:29:57 +00001281
Devang Patela1514cb2006-12-07 19:39:39 +00001282//===----------------------------------------------------------------------===//
1283// PassManager implementation
1284
Devang Patel376fefa2006-11-08 10:29:57 +00001285/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001286PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001287 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001288 // PM is the top level manager
1289 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001290}
1291
Devang Patelb67904d2006-12-13 02:36:01 +00001292PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001293 delete PM;
1294}
1295
Devang Patel376fefa2006-11-08 10:29:57 +00001296/// add - Add a pass to the queue of passes to run. This passes ownership of
1297/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1298/// will be destroyed as well, so there is no need to delete the pass. This
1299/// implies that all passes MUST be allocated with 'new'.
1300void
Devang Patelb67904d2006-12-13 02:36:01 +00001301PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001302 PM->add(P);
1303}
1304
1305/// run - Execute all of the passes scheduled for execution. Keep track of
1306/// whether any of the passes modifies the module, and if so, return true.
1307bool
Devang Patelb67904d2006-12-13 02:36:01 +00001308PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001309 return PM->run(M);
1310}
1311
Devang Patelb8817b92006-12-14 00:59:42 +00001312//===----------------------------------------------------------------------===//
1313// TimingInfo Class - This class is used to calculate information about the
1314// amount of time each pass takes to execute. This only happens with
1315// -time-passes is enabled on the command line.
1316//
1317bool llvm::TimePassesIsEnabled = false;
1318static cl::opt<bool,true>
1319EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1320 cl::desc("Time each pass, printing elapsed time for each on exit"));
1321
1322// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1323// a non null value (if the -time-passes option is enabled) or it leaves it
1324// null. It may be called multiple times.
1325void TimingInfo::createTheTimeInfo() {
1326 if (!TimePassesIsEnabled || TheTimeInfo) return;
1327
1328 // Constructed the first time this is called, iff -time-passes is enabled.
1329 // This guarantees that the object will be constructed before static globals,
1330 // thus it will be destroyed before them.
1331 static ManagedStatic<TimingInfo> TTI;
1332 TheTimeInfo = &*TTI;
1333}
1334
Devang Patel1c3633e2007-01-29 23:10:37 +00001335/// If TimingInfo is enabled then start pass timer.
1336void StartPassTimer(Pass *P) {
1337 if (TheTimeInfo)
1338 TheTimeInfo->passStarted(P);
1339}
1340
1341/// If TimingInfo is enabled then stop pass timer.
1342void StopPassTimer(Pass *P) {
1343 if (TheTimeInfo)
1344 TheTimeInfo->passEnded(P);
1345}
1346
Devang Patel1c56a632007-01-08 19:29:38 +00001347//===----------------------------------------------------------------------===//
1348// PMStack implementation
1349//
Devang Patelad98d232007-01-11 22:15:30 +00001350
Devang Patel1c56a632007-01-08 19:29:38 +00001351// Pop Pass Manager from the stack and clear its analysis info.
1352void PMStack::pop() {
1353
1354 PMDataManager *Top = this->top();
1355 Top->initializeAnalysisInfo();
1356
1357 S.pop_back();
1358}
1359
1360// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001361void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001362
Devang Patel15701b52007-01-11 00:19:00 +00001363 PMDataManager *Top = NULL;
1364 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1365 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001366
Devang Patel15701b52007-01-11 00:19:00 +00001367 if (this->empty()) {
1368 Top = PM;
1369 }
1370 else {
1371 Top = this->top();
1372 PMTopLevelManager *TPM = Top->getTopLevelManager();
1373
1374 assert (TPM && "Unable to find top level manager");
1375 TPM->addIndirectPassManager(PM);
1376 PM->setTopLevelManager(TPM);
1377 }
1378
1379 AnalysisResolver *AR = new AnalysisResolver(*Top);
1380 P->setResolver(AR);
1381
1382 S.push_back(PM);
1383}
1384
1385// Dump content of the pass manager stack.
1386void PMStack::dump() {
1387 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1388 E = S.end(); I != E; ++I) {
1389 Pass *P = dynamic_cast<Pass *>(*I);
1390 printf ("%s ", P->getPassName());
1391 }
1392 if (!S.empty())
1393 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001394}
1395
Devang Patel1c56a632007-01-08 19:29:38 +00001396/// Find appropriate Module Pass Manager in the PM Stack and
1397/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001398void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001399 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001400
Devang Patel1c56a632007-01-08 19:29:38 +00001401 // Find Module Pass Manager
1402 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001403 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1404 if (TopPMType == PreferredType)
1405 break; // We found desired pass manager
1406 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001407 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001408 else
1409 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001410 }
1411
Devang Patel23f8aa92007-01-17 21:19:23 +00001412 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001413}
1414
Devang Patel3312f752007-01-16 21:43:18 +00001415/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1416/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001417void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001418 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001419
Devang Patel15701b52007-01-11 00:19:00 +00001420 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001421 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001422 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1423 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001424 else
Devang Patel3312f752007-01-16 21:43:18 +00001425 break;
1426 }
1427 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1428
1429 // Create new Function Pass Manager
1430 if (!FPP) {
1431 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1432 PMDataManager *PMD = PMS.top();
1433
1434 // [1] Create new Function Pass Manager
1435 FPP = new FPPassManager(PMD->getDepth() + 1);
1436
1437 // [2] Set up new manager's top level manager
1438 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1439 TPM->addIndirectPassManager(FPP);
1440
1441 // [3] Assign manager to manage this new manager. This may create
1442 // and push new managers into PMS
1443 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001444
1445 // If Call Graph Pass Manager is active then use it to manage
1446 // this new Function Pass manager.
1447 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1448 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1449 else
1450 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001451
1452 // [4] Push new manager into PMS
1453 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001454 }
1455
Devang Patel3312f752007-01-16 21:43:18 +00001456 // Assign FPP as the manager of this pass.
1457 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001458}
1459
Devang Patel3312f752007-01-16 21:43:18 +00001460/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001461/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001462void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001463 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001464
1465 BBPassManager *BBP = NULL;
1466
Devang Patel15701b52007-01-11 00:19:00 +00001467 // Basic Pass Manager is a leaf pass manager. It does not handle
1468 // any other pass manager.
1469 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001470 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001471 }
1472
Devang Patel3312f752007-01-16 21:43:18 +00001473 // If leaf manager is not Basic Block Pass manager then create new
1474 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001475
Devang Patel3312f752007-01-16 21:43:18 +00001476 if (!BBP) {
1477 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1478 PMDataManager *PMD = PMS.top();
1479
1480 // [1] Create new Basic Block Manager
1481 BBP = new BBPassManager(PMD->getDepth() + 1);
1482
1483 // [2] Set up new manager's top level manager
1484 // Basic Block Pass Manager does not live by itself
1485 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1486 TPM->addIndirectPassManager(BBP);
1487
Devang Patel15701b52007-01-11 00:19:00 +00001488 // [3] Assign manager to manage this new manager. This may create
1489 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001490 Pass *P = dynamic_cast<Pass *>(BBP);
1491 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001492
Devang Patel3312f752007-01-16 21:43:18 +00001493 // [4] Push new manager into PMS
1494 PMS.push(BBP);
1495 }
Devang Patel1c56a632007-01-08 19:29:38 +00001496
Devang Patel3312f752007-01-16 21:43:18 +00001497 // Assign BBP as the manager of this pass.
1498 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001499}
1500
Devang Patelc6b5a552007-01-05 20:16:23 +00001501