blob: 1b2349006af9b6c9875f5e8a5b8de66a2c2af33d [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 Patel69e9f6d2007-04-16 20:27:05 +0000198 /// Return function pass corresponding to PassInfo PI, that is
199 /// required by module pass MP. Instantiate analysis pass, by using
200 /// its runOnFunction() for function F.
201 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
202
Devang Patele3858e62007-02-01 22:08:25 +0000203 virtual const char *getPassName() const {
204 return "Module Pass Manager";
205 }
206
Devang Pateleda56172006-12-12 23:34:33 +0000207 // Print passes managed by this manager
208 void dumpPassStructure(unsigned Offset) {
209 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000210 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
211 ModulePass *MP = getContainedPass(Index);
212 MP->dumpPassStructure(Offset + 1);
213 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000214 }
215 }
216
Devang Patelabfbe3b2006-12-16 00:56:26 +0000217 ModulePass *getContainedPass(unsigned N) {
218 assert ( N < PassVector.size() && "Pass number out of range!");
219 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
220 return MP;
221 }
222
Devang Patel28349ab2007-02-27 15:00:39 +0000223 virtual PassManagerType getPassManagerType() const {
224 return PMT_ModulePassManager;
225 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000226
227 private:
228 /// Collection of on the fly FPPassManagers. These managers manage
229 /// function passes that are required by module passes.
230 std::map<Pass *, FPPassManager *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000231};
232
Devang Patel10c2ca62006-12-12 22:47:13 +0000233//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000234// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000235//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000236/// PassManagerImpl manages MPPassManagers
237class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000238 public PMDataManager,
239 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000240
241public:
242
Devang Patel4268fc02007-01-16 02:00:38 +0000243 PassManagerImpl(int Depth) : PMDataManager(Depth),
244 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000245
Devang Patel376fefa2006-11-08 10:29:57 +0000246 /// add - Add a pass to the queue of passes to run. This passes ownership of
247 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
248 /// will be destroyed as well, so there is no need to delete the pass. This
249 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000250 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000251 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000252 }
Devang Patel376fefa2006-11-08 10:29:57 +0000253
254 /// run - Execute all of the passes scheduled for execution. Keep track of
255 /// whether any of the passes modifies the module, and if so, return true.
256 bool run(Module &M);
257
Devang Patelf9d96b92006-12-07 19:57:52 +0000258 /// Pass Manager itself does not invalidate any analysis info.
259 void getAnalysisUsage(AnalysisUsage &Info) const {
260 Info.setPreservesAll();
261 }
262
Devang Patelabcd1d32006-12-07 21:27:23 +0000263 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000264
Devang Patelfa971cd2006-12-08 23:57:43 +0000265 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000266
267 // P is a immutable pass and it will be managed by this
268 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000269 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000270 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000271 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000272 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000273 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000274 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000275 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000276 }
Devang Patel0f080042007-01-12 17:23:48 +0000277
Devang Patelabcd1d32006-12-07 21:27:23 +0000278 }
279
Devang Patel67d6a5e2006-12-19 19:46:59 +0000280 MPPassManager *getContainedManager(unsigned N) {
281 assert ( N < PassManagers.size() && "Pass number out of range!");
282 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
283 return MP;
284 }
285
Devang Patel376fefa2006-11-08 10:29:57 +0000286};
287
Devang Patel1c3633e2007-01-29 23:10:37 +0000288} // End of llvm namespace
289
290namespace {
291
292//===----------------------------------------------------------------------===//
293// TimingInfo Class - This class is used to calculate information about the
294// amount of time each pass takes to execute. This only happens when
295// -time-passes is enabled on the command line.
296//
297
298class VISIBILITY_HIDDEN TimingInfo {
299 std::map<Pass*, Timer> TimingData;
300 TimerGroup TG;
301
302public:
303 // Use 'create' member to get this.
304 TimingInfo() : TG("... Pass execution timing report ...") {}
305
306 // TimingDtor - Print out information about timing information
307 ~TimingInfo() {
308 // Delete all of the timers...
309 TimingData.clear();
310 // TimerGroup is deleted next, printing the report.
311 }
312
313 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
314 // to a non null value (if the -time-passes option is enabled) or it leaves it
315 // null. It may be called multiple times.
316 static void createTheTimeInfo();
317
318 void passStarted(Pass *P) {
319
320 if (dynamic_cast<PMDataManager *>(P))
321 return;
322
323 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
324 if (I == TimingData.end())
325 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
326 I->second.startTimer();
327 }
328 void passEnded(Pass *P) {
329
330 if (dynamic_cast<PMDataManager *>(P))
331 return;
332
333 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
334 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
335 I->second.stopTimer();
336 }
337};
338
Devang Patelb8817b92006-12-14 00:59:42 +0000339static TimingInfo *TheTimeInfo;
340
Devang Patel1c3633e2007-01-29 23:10:37 +0000341} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000342
Devang Patela1514cb2006-12-07 19:39:39 +0000343//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000344// PMTopLevelManager implementation
345
Devang Patel4268fc02007-01-16 02:00:38 +0000346/// Initialize top level manager. Create first pass manager.
347PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
348
349 if (t == TLM_Pass) {
350 MPPassManager *MPP = new MPPassManager(1);
351 MPP->setTopLevelManager(this);
352 addPassManager(MPP);
353 activeStack.push(MPP);
354 }
355 else if (t == TLM_Function) {
356 FPPassManager *FPP = new FPPassManager(1);
357 FPP->setTopLevelManager(this);
358 addPassManager(FPP);
359 activeStack.push(FPP);
360 }
361}
362
Devang Patelafb1f3622006-12-12 22:35:25 +0000363/// Set pass P as the last user of the given analysis passes.
364void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
365 Pass *P) {
366
367 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
368 E = AnalysisPasses.end(); I != E; ++I) {
369 Pass *AP = *I;
370 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000371
372 if (P == AP)
373 continue;
374
Devang Patelafb1f3622006-12-12 22:35:25 +0000375 // If AP is the last user of other passes then make P last user of
376 // such passes.
377 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
378 LUE = LastUser.end(); LUI != LUE; ++LUI) {
379 if (LUI->second == AP)
380 LastUser[LUI->first] = P;
381 }
382 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000383}
384
385/// Collect passes whose last user is P
386void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
387 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000388 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
389 LUE = LastUser.end(); LUI != LUE; ++LUI)
390 if (LUI->second == P)
391 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000392}
393
394/// Schedule pass P for execution. Make sure that passes required by
395/// P are run before P is run. Update analysis info maintained by
396/// the manager. Remove dead passes. This is a recursive function.
397void PMTopLevelManager::schedulePass(Pass *P) {
398
Devang Patel3312f752007-01-16 21:43:18 +0000399 // TODO : Allocate function manager for this pass, other wise required set
400 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000401
Devang Patel3f806962007-02-05 19:34:17 +0000402 // If this Analysis is already requested by one of the previous pass
403 // and it is still available then do not insert new pass in the queue again.
404 if (findAnalysisPass(P->getPassInfo()))
405 return;
406
Devang Pateld74ede72007-03-06 01:06:16 +0000407 // Give pass a chance to prepare the stage.
408 P->preparePassManager(activeStack);
409
Devang Patelafb1f3622006-12-12 22:35:25 +0000410 AnalysisUsage AnUsage;
411 P->getAnalysisUsage(AnUsage);
412 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
413 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
414 E = RequiredSet.end(); I != E; ++I) {
415
416 Pass *AnalysisPass = findAnalysisPass(*I);
417 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000418 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000419 // Schedule this analysis run first only if it is not a lower level
420 // analysis pass. Lower level analsyis passes are run on the fly.
421 if (P->getPotentialPassManagerType () >=
422 AnalysisPass->getPotentialPassManagerType())
423 schedulePass(AnalysisPass);
424 else
425 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000426 }
427 }
428
429 // Now all required passes are available.
430 addTopLevelPass(P);
431}
432
433/// Find the pass that implements Analysis AID. Search immutable
434/// passes and all pass managers. If desired pass is not found
435/// then return NULL.
436Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
437
438 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000439 // Check pass managers
440 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
441 E = PassManagers.end(); P == NULL && I != E; ++I) {
442 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
443 assert(PMD && "This is not a PassManager");
444 P = PMD->findAnalysisPass(AID, false);
445 }
446
447 // Check other pass managers
448 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
449 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
450 P = (*I)->findAnalysisPass(AID, false);
451
Devang Patelafb1f3622006-12-12 22:35:25 +0000452 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
453 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
454 const PassInfo *PI = (*I)->getPassInfo();
455 if (PI == AID)
456 P = *I;
457
458 // If Pass not found then check the interfaces implemented by Immutable Pass
459 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000460 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
461 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
462 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000463 }
464 }
465
Devang Patelafb1f3622006-12-12 22:35:25 +0000466 return P;
467}
468
Devang Pateleda56172006-12-12 23:34:33 +0000469// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000470void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000471
Devang Patelfd4184322007-01-17 20:33:36 +0000472 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000473 return;
474
Devang Pateleda56172006-12-12 23:34:33 +0000475 // Print out the immutable passes
476 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
477 ImmutablePasses[i]->dumpPassStructure(0);
478 }
479
Devang Patel991aeba2006-12-15 20:13:01 +0000480 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000481 E = PassManagers.end(); I != E; ++I)
482 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000483}
484
Devang Patel991aeba2006-12-15 20:13:01 +0000485void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000486
Devang Patelfd4184322007-01-17 20:33:36 +0000487 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000488 return;
489
490 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000491 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000492 E = PassManagers.end(); I != E; ++I) {
493 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
494 assert(PMD && "This is not a PassManager");
495 PMD->dumpPassArguments();
496 }
497 cerr << "\n";
498}
499
Devang Patele3068402006-12-21 00:16:50 +0000500void PMTopLevelManager::initializeAllAnalysisInfo() {
501
502 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
503 E = PassManagers.end(); I != E; ++I) {
504 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
505 assert(PMD && "This is not a PassManager");
506 PMD->initializeAnalysisInfo();
507 }
508
509 // Initailize other pass managers
510 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
511 E = IndirectPassManagers.end(); I != E; ++I)
512 (*I)->initializeAnalysisInfo();
513}
514
Devang Patele7599552007-01-12 18:52:44 +0000515/// Destructor
516PMTopLevelManager::~PMTopLevelManager() {
517 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
518 E = PassManagers.end(); I != E; ++I)
519 delete *I;
520
521 for (std::vector<ImmutablePass *>::iterator
522 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
523 delete *I;
524
525 PassManagers.clear();
526}
527
Devang Patelafb1f3622006-12-12 22:35:25 +0000528//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000529// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000530
Devang Pateld65e9e92006-11-08 01:31:28 +0000531/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000532/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000533bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000534
Devang Patel8f677ce2006-12-07 18:47:25 +0000535 // TODO
536 // If this pass is not preserving information that is required by a
537 // pass maintained by higher level pass manager then do not insert
538 // this pass into current manager. Use new manager. For example,
539 // For example, If FunctionPass F is not preserving ModulePass Info M1
540 // that is used by another ModulePass M2 then do not insert F in
541 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000542 return true;
543}
544
Devang Patel643676c2006-11-11 01:10:19 +0000545/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000546void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000547
Devang Patel643676c2006-11-11 01:10:19 +0000548 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000549 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000550
Devang Patele9976aa2006-12-07 19:33:53 +0000551 //This pass is the current implementation of all of the interfaces it
552 //implements as well.
553 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
554 for (unsigned i = 0, e = II.size(); i != e; ++i)
555 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000556 }
557}
558
Devang Patel9d9fc902007-03-06 17:52:53 +0000559// Return true if P preserves high level analysis used by other
560// passes managed by this manager
561bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
562
563 AnalysisUsage AnUsage;
564 P->getAnalysisUsage(AnUsage);
565
566 if (AnUsage.getPreservesAll())
567 return true;
568
569 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
570 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
571 E = HigherLevelAnalysis.end(); I != E; ++I) {
572 Pass *P1 = *I;
Devang Patel01919d22007-03-08 19:05:01 +0000573 if (!dynamic_cast<ImmutablePass*>(P1)
574 && std::find(PreservedSet.begin(), PreservedSet.end(), P1->getPassInfo()) ==
575 PreservedSet.end())
576 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000577 }
578
579 return true;
580}
581
Devang Patelf68a3492006-11-07 22:35:17 +0000582/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000583void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000584 AnalysisUsage AnUsage;
585 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000586
Devang Patel2e169c32006-12-07 20:03:49 +0000587 if (AnUsage.getPreservesAll())
588 return;
589
590 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000591 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000592 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000593 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000594 if (!dynamic_cast<ImmutablePass*>(Info->second)
595 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
596 PreservedSet.end())
Devang Patel349170f2006-11-11 01:24:55 +0000597 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000598 AvailableAnalysis.erase(Info);
Devang Patel349170f2006-11-11 01:24:55 +0000599 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000600
601 // Check inherited analysis also. If P is not preserving analysis
602 // provided by parent manager then remove it here.
603 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
604
605 if (!InheritedAnalysis[Index])
606 continue;
607
608 for (std::map<AnalysisID, Pass*>::iterator
609 I = InheritedAnalysis[Index]->begin(),
610 E = InheritedAnalysis[Index]->end(); I != E; ) {
611 std::map<AnalysisID, Pass *>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000612 if (!dynamic_cast<ImmutablePass*>(Info->second)
613 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
614 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000615 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000616 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000617 }
618 }
619
Devang Patelf68a3492006-11-07 22:35:17 +0000620}
621
Devang Patelca189262006-11-14 03:05:08 +0000622/// Remove analysis passes that are not used any longer
Devang Patel003a5592007-03-05 20:01:30 +0000623void PMDataManager::removeDeadPasses(Pass *P, std::string Msg,
624 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000625
626 std::vector<Pass *> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000627
628 if (!TPM)
629 return;
630
Devang Patel17ad0962006-12-08 00:37:52 +0000631 TPM->collectLastUses(DeadPasses, P);
632
633 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
634 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000635
Devang Patel003a5592007-03-05 20:01:30 +0000636 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000637
Devang Patel7ebf09d2007-03-05 18:20:51 +0000638 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000639 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000640 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000641
Devang Patel17ad0962006-12-08 00:37:52 +0000642 std::map<AnalysisID, Pass*>::iterator Pos =
643 AvailableAnalysis.find((*I)->getPassInfo());
644
Devang Patel475c4532006-12-08 00:59:05 +0000645 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000646 if (Pos != AvailableAnalysis.end())
647 AvailableAnalysis.erase(Pos);
648 }
Devang Patelca189262006-11-14 03:05:08 +0000649}
650
Devang Patel8f677ce2006-12-07 18:47:25 +0000651/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000652/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000653void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000654 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000655
Devang Pateld440cd92006-12-08 23:53:00 +0000656 // This manager is going to manage pass P. Set up analysis resolver
657 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000658 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000659 P->setResolver(AR);
660
Devang Patelec2b9a72007-03-05 22:57:49 +0000661 // If a FunctionPass F is the last user of ModulePass info M
662 // then the F's manager, not F, records itself as a last user of M.
663 std::vector<Pass *> TransferLastUses;
664
Devang Patel90b05e02006-11-11 02:04:19 +0000665 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000666
667 // At the moment, this pass is the last user of all required passes.
668 std::vector<Pass *> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000669 SmallVector<Pass *, 8> RequiredPasses;
670 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
671
Devang Patelbc03f132006-12-07 23:55:10 +0000672 unsigned PDepth = this->getDepth();
673
Devang Patele64d3052007-04-16 20:12:57 +0000674 collectRequiredAnalysis(RequiredPasses,
675 ReqAnalysisNotAvailable, P);
676 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000677 E = RequiredPasses.end(); I != E; ++I) {
678 Pass *PRequired = *I;
679 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000680
681 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
682 RDepth = DM.getDepth();
683
Devang Patelbc03f132006-12-07 23:55:10 +0000684 if (PDepth == RDepth)
685 LastUses.push_back(PRequired);
686 else if (PDepth > RDepth) {
687 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000688 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000689 // Keep track of higher level analysis used by this manager.
690 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000691 } else
692 assert (0 && "Unable to accomodate Required Pass");
693 }
694
Devang Patel39786a92007-01-15 20:31:54 +0000695 // Set P as P's last user until someone starts using P.
696 // However, if P is a Pass Manager then it does not need
697 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000698 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000699 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000700 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000701
Devang Patelec2b9a72007-03-05 22:57:49 +0000702 if (!TransferLastUses.empty()) {
703 Pass *My_PM = dynamic_cast<Pass *>(this);
704 TPM->setLastUser(TransferLastUses, My_PM);
705 TransferLastUses.clear();
706 }
707
Devang Patel69e9f6d2007-04-16 20:27:05 +0000708 // Now, take care of required analysises that are not available.
709 for (SmallVector<AnalysisID, 8>::iterator
710 I = ReqAnalysisNotAvailable.begin(),
711 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
712 Pass *AnalysisPass = (*I)->createPass();
713 this->addLowerLevelRequiredPass(P, AnalysisPass);
714 }
715
Devang Patel17bff0d2006-12-07 22:09:36 +0000716 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000717 // Remove the analysis not preserved by this pass
718 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000719 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000720 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000721
722 // Add pass
723 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000724}
725
Devang Patele64d3052007-04-16 20:12:57 +0000726
727/// Populate RP with analysis pass that are required by
728/// pass P and are available. Populate RP_NotAvail with analysis
729/// pass that are required by pass P but are not available.
730void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
731 SmallVector<AnalysisID, 8> &RP_NotAvail,
732 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000733 AnalysisUsage AnUsage;
734 P->getAnalysisUsage(AnUsage);
735 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
736 for (std::vector<AnalysisID>::const_iterator
737 I = RequiredSet.begin(), E = RequiredSet.end();
738 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000739 AnalysisID AID = *I;
740 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
741 RP.push_back(AnalysisPass);
742 else
743 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000744 }
Devang Patelf58183d2006-12-12 23:09:32 +0000745
746 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
747 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
748 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000749 AnalysisID AID = *I;
750 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
751 RP.push_back(AnalysisPass);
752 else
753 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000754 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000755}
756
Devang Patel07f4f582006-11-14 21:49:36 +0000757// All Required analyses should be available to the pass as it runs! Here
758// we fill in the AnalysisImpls member of the pass so that it can
759// successfully use the getAnalysis() method to retrieve the
760// implementations it needs.
761//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000762void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000763 AnalysisUsage AnUsage;
764 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000765
766 for (std::vector<const PassInfo *>::const_iterator
767 I = AnUsage.getRequiredSet().begin(),
768 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000769 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000770 if (Impl == 0)
771 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000772 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000773 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000774 }
775}
776
Devang Patel640c5bb2006-12-08 22:30:11 +0000777/// Find the pass that implements Analysis AID. If desired pass is not found
778/// then return NULL.
779Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
780
781 // Check if AvailableAnalysis map has one entry.
782 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
783
784 if (I != AvailableAnalysis.end())
785 return I->second;
786
787 // Search Parents through TopLevelManager
788 if (SearchParent)
789 return TPM->findAnalysisPass(AID);
790
Devang Patel9d759b82006-12-09 00:09:12 +0000791 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000792}
793
Devang Patel991aeba2006-12-15 20:13:01 +0000794// Print list of passes that are last used by P.
795void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
796
797 std::vector<Pass *> LUses;
798
799 assert (TPM && "Top Level Manager is missing");
800 TPM->collectLastUses(LUses, P);
801
802 for (std::vector<Pass *>::iterator I = LUses.begin(),
803 E = LUses.end(); I != E; ++I) {
804 llvm::cerr << "--" << std::string(Offset*2, ' ');
805 (*I)->dumpPassStructure(0);
806 }
807}
808
809void PMDataManager::dumpPassArguments() const {
810 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
811 E = PassVector.end(); I != E; ++I) {
812 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
813 PMD->dumpPassArguments();
814 else
815 if (const PassInfo *PI = (*I)->getPassInfo())
816 if (!PI->isAnalysisGroup())
817 cerr << " -" << PI->getPassArgument();
818 }
819}
820
Devang Patel003a5592007-03-05 20:01:30 +0000821void PMDataManager:: dumpPassInfo(Pass *P, enum PassDebuggingString S1,
822 enum PassDebuggingString S2,
823 std::string Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000824 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000825 return;
826 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000827 switch (S1) {
828 case EXECUTION_MSG:
829 cerr << "Executing Pass '" << P->getPassName();
830 break;
831 case MODIFICATION_MSG:
832 cerr << "' Made Modification '" << P->getPassName();
833 break;
834 case FREEING_MSG:
835 cerr << " Freeing Pass '" << P->getPassName();
836 break;
837 default:
838 break;
839 }
840 switch (S2) {
841 case ON_BASICBLOCK_MSG:
842 cerr << "' on BasicBlock '" << Msg << "...\n";
843 break;
844 case ON_FUNCTION_MSG:
845 cerr << "' on Function '" << Msg << "...\n";
846 break;
847 case ON_MODULE_MSG:
848 cerr << "' on Module '" << Msg << "...\n";
849 break;
850 case ON_LOOP_MSG:
851 cerr << "' on Loop " << Msg << "...\n";
852 break;
853 case ON_CG_MSG:
854 cerr << "' on Call Graph " << Msg << "...\n";
855 break;
856 default:
857 break;
858 }
Devang Patel991aeba2006-12-15 20:13:01 +0000859}
860
861void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
862 const std::vector<AnalysisID> &Set)
863 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000864 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000865 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
866 for (unsigned i = 0; i != Set.size(); ++i) {
867 if (i) cerr << ",";
868 cerr << " " << Set[i]->getPassName();
869 }
870 cerr << "\n";
871 }
872}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000873
Devang Patele7599552007-01-12 18:52:44 +0000874// Destructor
875PMDataManager::~PMDataManager() {
876
877 for (std::vector<Pass *>::iterator I = PassVector.begin(),
878 E = PassVector.end(); I != E; ++I)
879 delete *I;
880
881 PassVector.clear();
882}
883
Devang Patel9bdf7d42006-12-08 23:28:54 +0000884//===----------------------------------------------------------------------===//
885// NOTE: Is this the right place to define this method ?
886// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000887Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000888 return PM.findAnalysisPass(ID, dir);
889}
890
Devang Patela1514cb2006-12-07 19:39:39 +0000891//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000892// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000893
Devang Patel6e5a1132006-11-07 21:31:57 +0000894/// Execute all of the passes scheduled for execution by invoking
895/// runOnBasicBlock method. Keep track of whether any of the passes modifies
896/// the function, and if so, return true.
897bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000898BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000899
Reid Spencer5301e7c2007-01-30 20:08:39 +0000900 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000901 return false;
902
Devang Patele9585592006-12-08 01:38:28 +0000903 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000904
Devang Patel6e5a1132006-11-07 21:31:57 +0000905 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000906 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
907 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000908 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000909 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000910
Devang Patel003a5592007-03-05 20:01:30 +0000911 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000912 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000913
Devang Patelabfbe3b2006-12-16 00:56:26 +0000914 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000915
Devang Patelabfbe3b2006-12-16 00:56:26 +0000916 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000917 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000918 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000919
Devang Patel003a5592007-03-05 20:01:30 +0000920 if (Changed)
921 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000922 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000923
Devang Patelabfbe3b2006-12-16 00:56:26 +0000924 removeNotPreservedAnalysis(BP);
925 recordAvailableAnalysis(BP);
Devang Patel003a5592007-03-05 20:01:30 +0000926 removeDeadPasses(BP, (*I).getName(), ON_BASICBLOCK_MSG);
927
Devang Patel6e5a1132006-11-07 21:31:57 +0000928 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000929 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000930}
931
Devang Patel475c4532006-12-08 00:59:05 +0000932// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000933inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000934 bool Changed = false;
935
Devang Patelabfbe3b2006-12-16 00:56:26 +0000936 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
937 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000938 Changed |= BP->doInitialization(M);
939 }
940
941 return Changed;
942}
943
Devang Patel67d6a5e2006-12-19 19:46:59 +0000944inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000945 bool Changed = false;
946
Devang Patelabfbe3b2006-12-16 00:56:26 +0000947 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
948 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000949 Changed |= BP->doFinalization(M);
950 }
951
952 return Changed;
953}
954
Devang Patel67d6a5e2006-12-19 19:46:59 +0000955inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000956 bool Changed = false;
957
Devang Patelabfbe3b2006-12-16 00:56:26 +0000958 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
959 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000960 Changed |= BP->doInitialization(F);
961 }
962
963 return Changed;
964}
965
Devang Patel67d6a5e2006-12-19 19:46:59 +0000966inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000967 bool Changed = false;
968
Devang Patelabfbe3b2006-12-16 00:56:26 +0000969 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
970 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000971 Changed |= BP->doFinalization(F);
972 }
973
974 return Changed;
975}
976
977
Devang Patela1514cb2006-12-07 19:39:39 +0000978//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000979// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000980
Devang Patel4e12f862006-11-08 10:44:40 +0000981/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +0000982FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000983 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000984 // FPM is the top level manager.
985 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000986
987 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +0000988 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +0000989 FPM->setResolver(AR);
990
Devang Patel1f653682006-12-08 18:57:16 +0000991 MP = P;
992}
993
Devang Patelb67904d2006-12-13 02:36:01 +0000994FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000995 delete FPM;
996}
997
Devang Patel4e12f862006-11-08 10:44:40 +0000998/// add - Add a pass to the queue of passes to run. This passes
999/// ownership of the Pass to the PassManager. When the
1000/// PassManager_X is destroyed, the pass will be destroyed as well, so
1001/// there is no need to delete the pass. (TODO delete passes.)
1002/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001003void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001004 FPM->add(P);
1005}
1006
Devang Patel9f3083e2006-11-15 19:39:54 +00001007/// run - Execute all of the passes scheduled for execution. Keep
1008/// track of whether any of the passes modifies the function, and if
1009/// so, return true.
1010///
Devang Patelb67904d2006-12-13 02:36:01 +00001011bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001012 std::string errstr;
1013 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001014 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001015 abort();
1016 }
Devang Patel272908d2006-12-08 22:57:48 +00001017 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001018}
1019
1020
Devang Patelff631ae2006-11-15 01:27:05 +00001021/// doInitialization - Run all of the initializers for the function passes.
1022///
Devang Patelb67904d2006-12-13 02:36:01 +00001023bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001024 return FPM->doInitialization(*MP->getModule());
1025}
1026
1027/// doFinalization - Run all of the initializers for the function passes.
1028///
Devang Patelb67904d2006-12-13 02:36:01 +00001029bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001030 return FPM->doFinalization(*MP->getModule());
1031}
1032
Devang Patela1514cb2006-12-07 19:39:39 +00001033//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001034// FunctionPassManagerImpl implementation
1035//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001036inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1037 bool Changed = false;
1038
1039 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1040 FPPassManager *FP = getContainedManager(Index);
1041 Changed |= FP->doInitialization(M);
1042 }
1043
1044 return Changed;
1045}
1046
1047inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1048 bool Changed = false;
1049
1050 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1051 FPPassManager *FP = getContainedManager(Index);
1052 Changed |= FP->doFinalization(M);
1053 }
1054
1055 return Changed;
1056}
1057
1058// Execute all the passes managed by this top level manager.
1059// Return true if any function is modified by a pass.
1060bool FunctionPassManagerImpl::run(Function &F) {
1061
1062 bool Changed = false;
1063
1064 TimingInfo::createTheTimeInfo();
1065
1066 dumpArguments();
1067 dumpPasses();
1068
Devang Patele3068402006-12-21 00:16:50 +00001069 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001070 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1071 FPPassManager *FP = getContainedManager(Index);
1072 Changed |= FP->runOnFunction(F);
1073 }
1074 return Changed;
1075}
1076
1077//===----------------------------------------------------------------------===//
1078// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001079
Devang Patele7599552007-01-12 18:52:44 +00001080/// Print passes managed by this manager
1081void FPPassManager::dumpPassStructure(unsigned Offset) {
1082 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1083 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1084 FunctionPass *FP = getContainedPass(Index);
1085 FP->dumpPassStructure(Offset + 1);
1086 dumpLastUses(FP, Offset+1);
1087 }
1088}
1089
1090
Devang Patel0c2012f2006-11-07 21:49:50 +00001091/// Execute all of the passes scheduled for execution by invoking
1092/// runOnFunction method. Keep track of whether any of the passes modifies
1093/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001094bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001095
1096 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001097
Reid Spencer5301e7c2007-01-30 20:08:39 +00001098 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001099 return false;
1100
Devang Patelabfbe3b2006-12-16 00:56:26 +00001101 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1102 FunctionPass *FP = getContainedPass(Index);
1103
Devang Patelf6d1d212006-12-14 00:25:06 +00001104 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001105 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001106
Devang Patel003a5592007-03-05 20:01:30 +00001107 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001108 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001109
Devang Patelabfbe3b2006-12-16 00:56:26 +00001110 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001111
Devang Patelabfbe3b2006-12-16 00:56:26 +00001112 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001113 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001114 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001115
Devang Patel003a5592007-03-05 20:01:30 +00001116 if (Changed)
1117 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001118 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001119
Devang Patelabfbe3b2006-12-16 00:56:26 +00001120 removeNotPreservedAnalysis(FP);
1121 recordAvailableAnalysis(FP);
Devang Patel003a5592007-03-05 20:01:30 +00001122 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001123 }
1124 return Changed;
1125}
1126
Devang Patel67d6a5e2006-12-19 19:46:59 +00001127bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001128
Devang Patel67d6a5e2006-12-19 19:46:59 +00001129 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001130
1131 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1132 this->runOnFunction(*I);
1133
1134 return Changed |= doFinalization(M);
1135}
1136
1137inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001138 bool Changed = false;
1139
Devang Patelabfbe3b2006-12-16 00:56:26 +00001140 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1141 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001142 Changed |= FP->doInitialization(M);
1143 }
1144
1145 return Changed;
1146}
1147
Devang Patel67d6a5e2006-12-19 19:46:59 +00001148inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001149 bool Changed = false;
1150
Devang Patelabfbe3b2006-12-16 00:56:26 +00001151 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1152 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001153 Changed |= FP->doFinalization(M);
1154 }
1155
Devang Patelff631ae2006-11-15 01:27:05 +00001156 return Changed;
1157}
1158
Devang Patela1514cb2006-12-07 19:39:39 +00001159//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001160// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001161
Devang Patel05e1a972006-11-07 22:03:15 +00001162/// Execute all of the passes scheduled for execution by invoking
1163/// runOnModule method. Keep track of whether any of the passes modifies
1164/// the module, and if so, return true.
1165bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001166MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001167 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001168
Devang Patelabfbe3b2006-12-16 00:56:26 +00001169 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1170 ModulePass *MP = getContainedPass(Index);
1171
Devang Patelf6d1d212006-12-14 00:25:06 +00001172 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001173 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001174
Devang Patel003a5592007-03-05 20:01:30 +00001175 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001176 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001177
Devang Patelabfbe3b2006-12-16 00:56:26 +00001178 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001179
Devang Patelabfbe3b2006-12-16 00:56:26 +00001180 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001181 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001182 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001183
Devang Patel003a5592007-03-05 20:01:30 +00001184 if (Changed)
1185 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1186 M.getModuleIdentifier());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001187 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001188
Devang Patelabfbe3b2006-12-16 00:56:26 +00001189 removeNotPreservedAnalysis(MP);
1190 recordAvailableAnalysis(MP);
Devang Patel003a5592007-03-05 20:01:30 +00001191 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001192 }
1193 return Changed;
1194}
1195
Devang Patele64d3052007-04-16 20:12:57 +00001196/// Add RequiredPass into list of lower level passes required by pass P.
1197/// RequiredPass is run on the fly by Pass Manager when P requests it
1198/// through getAnalysis interface.
1199void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1200
1201 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1202 && "Unable to handle Pass that requires lower level Analysis pass");
1203 assert ((P->getPotentialPassManagerType() <
1204 RequiredPass->getPotentialPassManagerType())
1205 && "Unable to handle Pass that requires lower level Analysis pass");
1206
Devang Patel69e9f6d2007-04-16 20:27:05 +00001207 FPPassManager *FPP = OnTheFlyManagers[P];
1208 if (!FPP) {
1209 FPP = new FPPassManager(getDepth() + 1);
1210 OnTheFlyManagers[P] = FPP;
1211 }
1212
1213 FPP->add(RequiredPass, false);
Devang Patele64d3052007-04-16 20:12:57 +00001214}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001215
1216/// Return function pass corresponding to PassInfo PI, that is
1217/// required by module pass MP. Instantiate analysis pass, by using
1218/// its runOnFunction() for function F.
1219Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1220 Function &F) {
1221 AnalysisID AID = PI;
1222 FPPassManager *FPP =OnTheFlyManagers[MP];
1223 assert (FPP && "Unable to find on the fly pass");
1224
1225 FPP->runOnFunction(F);
1226 return FPP->findAnalysisPass(AID, false);
1227}
1228
1229
Devang Patela1514cb2006-12-07 19:39:39 +00001230//===----------------------------------------------------------------------===//
1231// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001232//
Devang Patelc290c8a2006-11-07 22:23:34 +00001233/// run - Execute all of the passes scheduled for execution. Keep track of
1234/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001235bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001236
Devang Patelc290c8a2006-11-07 22:23:34 +00001237 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001238
Devang Patelb8817b92006-12-14 00:59:42 +00001239 TimingInfo::createTheTimeInfo();
1240
Devang Patelcfd70c42006-12-13 22:10:00 +00001241 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001242 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001243
Devang Patele3068402006-12-21 00:16:50 +00001244 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001245 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1246 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001247 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001248 }
1249 return Changed;
1250}
Devang Patel376fefa2006-11-08 10:29:57 +00001251
Devang Patela1514cb2006-12-07 19:39:39 +00001252//===----------------------------------------------------------------------===//
1253// PassManager implementation
1254
Devang Patel376fefa2006-11-08 10:29:57 +00001255/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001256PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001257 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001258 // PM is the top level manager
1259 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001260}
1261
Devang Patelb67904d2006-12-13 02:36:01 +00001262PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001263 delete PM;
1264}
1265
Devang Patel376fefa2006-11-08 10:29:57 +00001266/// add - Add a pass to the queue of passes to run. This passes ownership of
1267/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1268/// will be destroyed as well, so there is no need to delete the pass. This
1269/// implies that all passes MUST be allocated with 'new'.
1270void
Devang Patelb67904d2006-12-13 02:36:01 +00001271PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001272 PM->add(P);
1273}
1274
1275/// run - Execute all of the passes scheduled for execution. Keep track of
1276/// whether any of the passes modifies the module, and if so, return true.
1277bool
Devang Patelb67904d2006-12-13 02:36:01 +00001278PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001279 return PM->run(M);
1280}
1281
Devang Patelb8817b92006-12-14 00:59:42 +00001282//===----------------------------------------------------------------------===//
1283// TimingInfo Class - This class is used to calculate information about the
1284// amount of time each pass takes to execute. This only happens with
1285// -time-passes is enabled on the command line.
1286//
1287bool llvm::TimePassesIsEnabled = false;
1288static cl::opt<bool,true>
1289EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1290 cl::desc("Time each pass, printing elapsed time for each on exit"));
1291
1292// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1293// a non null value (if the -time-passes option is enabled) or it leaves it
1294// null. It may be called multiple times.
1295void TimingInfo::createTheTimeInfo() {
1296 if (!TimePassesIsEnabled || TheTimeInfo) return;
1297
1298 // Constructed the first time this is called, iff -time-passes is enabled.
1299 // This guarantees that the object will be constructed before static globals,
1300 // thus it will be destroyed before them.
1301 static ManagedStatic<TimingInfo> TTI;
1302 TheTimeInfo = &*TTI;
1303}
1304
Devang Patel1c3633e2007-01-29 23:10:37 +00001305/// If TimingInfo is enabled then start pass timer.
1306void StartPassTimer(Pass *P) {
1307 if (TheTimeInfo)
1308 TheTimeInfo->passStarted(P);
1309}
1310
1311/// If TimingInfo is enabled then stop pass timer.
1312void StopPassTimer(Pass *P) {
1313 if (TheTimeInfo)
1314 TheTimeInfo->passEnded(P);
1315}
1316
Devang Patel1c56a632007-01-08 19:29:38 +00001317//===----------------------------------------------------------------------===//
1318// PMStack implementation
1319//
Devang Patelad98d232007-01-11 22:15:30 +00001320
Devang Patel1c56a632007-01-08 19:29:38 +00001321// Pop Pass Manager from the stack and clear its analysis info.
1322void PMStack::pop() {
1323
1324 PMDataManager *Top = this->top();
1325 Top->initializeAnalysisInfo();
1326
1327 S.pop_back();
1328}
1329
1330// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001331void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001332
Devang Patel15701b52007-01-11 00:19:00 +00001333 PMDataManager *Top = NULL;
1334 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1335 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001336
Devang Patel15701b52007-01-11 00:19:00 +00001337 if (this->empty()) {
1338 Top = PM;
1339 }
1340 else {
1341 Top = this->top();
1342 PMTopLevelManager *TPM = Top->getTopLevelManager();
1343
1344 assert (TPM && "Unable to find top level manager");
1345 TPM->addIndirectPassManager(PM);
1346 PM->setTopLevelManager(TPM);
1347 }
1348
1349 AnalysisResolver *AR = new AnalysisResolver(*Top);
1350 P->setResolver(AR);
1351
1352 S.push_back(PM);
1353}
1354
1355// Dump content of the pass manager stack.
1356void PMStack::dump() {
1357 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1358 E = S.end(); I != E; ++I) {
1359 Pass *P = dynamic_cast<Pass *>(*I);
1360 printf ("%s ", P->getPassName());
1361 }
1362 if (!S.empty())
1363 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001364}
1365
Devang Patel1c56a632007-01-08 19:29:38 +00001366/// Find appropriate Module Pass Manager in the PM Stack and
1367/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001368void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001369 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001370
Devang Patel1c56a632007-01-08 19:29:38 +00001371 // Find Module Pass Manager
1372 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001373 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1374 if (TopPMType == PreferredType)
1375 break; // We found desired pass manager
1376 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001377 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001378 else
1379 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001380 }
1381
Devang Patel23f8aa92007-01-17 21:19:23 +00001382 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001383}
1384
Devang Patel3312f752007-01-16 21:43:18 +00001385/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1386/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001387void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001388 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001389
Devang Patel15701b52007-01-11 00:19:00 +00001390 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001391 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001392 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1393 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001394 else
Devang Patel3312f752007-01-16 21:43:18 +00001395 break;
1396 }
1397 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1398
1399 // Create new Function Pass Manager
1400 if (!FPP) {
1401 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1402 PMDataManager *PMD = PMS.top();
1403
1404 // [1] Create new Function Pass Manager
1405 FPP = new FPPassManager(PMD->getDepth() + 1);
1406
1407 // [2] Set up new manager's top level manager
1408 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1409 TPM->addIndirectPassManager(FPP);
1410
1411 // [3] Assign manager to manage this new manager. This may create
1412 // and push new managers into PMS
1413 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001414
1415 // If Call Graph Pass Manager is active then use it to manage
1416 // this new Function Pass manager.
1417 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1418 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1419 else
1420 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001421
1422 // [4] Push new manager into PMS
1423 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001424 }
1425
Devang Patel3312f752007-01-16 21:43:18 +00001426 // Assign FPP as the manager of this pass.
1427 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001428}
1429
Devang Patel3312f752007-01-16 21:43:18 +00001430/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001431/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001432void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001433 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001434
1435 BBPassManager *BBP = NULL;
1436
Devang Patel15701b52007-01-11 00:19:00 +00001437 // Basic Pass Manager is a leaf pass manager. It does not handle
1438 // any other pass manager.
1439 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001440 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001441 }
1442
Devang Patel3312f752007-01-16 21:43:18 +00001443 // If leaf manager is not Basic Block Pass manager then create new
1444 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001445
Devang Patel3312f752007-01-16 21:43:18 +00001446 if (!BBP) {
1447 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1448 PMDataManager *PMD = PMS.top();
1449
1450 // [1] Create new Basic Block Manager
1451 BBP = new BBPassManager(PMD->getDepth() + 1);
1452
1453 // [2] Set up new manager's top level manager
1454 // Basic Block Pass Manager does not live by itself
1455 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1456 TPM->addIndirectPassManager(BBP);
1457
Devang Patel15701b52007-01-11 00:19:00 +00001458 // [3] Assign manager to manage this new manager. This may create
1459 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001460 Pass *P = dynamic_cast<Pass *>(BBP);
1461 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001462
Devang Patel3312f752007-01-16 21:43:18 +00001463 // [4] Push new manager into PMS
1464 PMS.push(BBP);
1465 }
Devang Patel1c56a632007-01-08 19:29:38 +00001466
Devang Patel3312f752007-01-16 21:43:18 +00001467 // Assign BBP as the manager of this pass.
1468 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001469}
1470
Devang Patelc6b5a552007-01-05 20:16:23 +00001471