blob: 28b0b89bba89ea83d94f42dc65584f978078978c [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 Patelb8817b92006-12-14 00:59:42 +000017#include "llvm/Support/Timer.h"
Devang Patel6e5a1132006-11-07 21:31:57 +000018#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000019#include "llvm/ModuleProvider.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000020#include "llvm/Support/Streams.h"
Devang Patelb8817b92006-12-14 00:59:42 +000021#include "llvm/Support/ManagedStatic.h"
Devang Patela9844592006-11-11 01:31:05 +000022#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000023#include <map>
Devang Patelffca9102006-12-15 19:39:30 +000024
Devang Patele7599552007-01-12 18:52:44 +000025// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000026
Devang Patelf1567a52006-12-13 20:03:48 +000027namespace llvm {
28
29//===----------------------------------------------------------------------===//
30// Pass debugging information. Often it is useful to find out what pass is
31// running when a crash occurs in a utility. When this library is compiled with
32// debugging on, a command line option (--debug-pass) is enabled that causes the
33// pass name to be printed before it executes.
34//
35
Devang Patel03fb5872006-12-13 21:13:31 +000036// Different debug levels that can be enabled...
37enum PassDebugLevel {
38 None, Arguments, Structure, Executions, Details
39};
40
Devang Patelf1567a52006-12-13 20:03:48 +000041static cl::opt<enum PassDebugLevel>
42PassDebugging_New("debug-pass", cl::Hidden,
43 cl::desc("Print PassManager debugging information"),
44 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000045 clEnumVal(None , "disable debug output"),
46 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
47 clEnumVal(Structure , "print pass structure before run()"),
48 clEnumVal(Executions, "print pass name before it is executed"),
49 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000050 clEnumValEnd));
51} // End of llvm namespace
52
Devang Patelffca9102006-12-15 19:39:30 +000053namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000054
Devang Patelf33f3eb2006-12-07 19:21:29 +000055//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000056// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000057//
Devang Patel67d6a5e2006-12-19 19:46:59 +000058/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000059/// pass together and sequence them to process one basic block before
60/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000061class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
62 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000063
64public:
Devang Patel67d6a5e2006-12-19 19:46:59 +000065 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +000066
Devang Patelca58e352006-11-08 10:05:38 +000067 /// Execute all of the passes scheduled for execution. Keep track of
68 /// whether any of the passes modifies the function, and if so, return true.
69 bool runOnFunction(Function &F);
70
Devang Patelf9d96b92006-12-07 19:57:52 +000071 /// Pass Manager itself does not invalidate any analysis info.
72 void getAnalysisUsage(AnalysisUsage &Info) const {
73 Info.setPreservesAll();
74 }
75
Devang Patel475c4532006-12-08 00:59:05 +000076 bool doInitialization(Module &M);
77 bool doInitialization(Function &F);
78 bool doFinalization(Module &M);
79 bool doFinalization(Function &F);
80
Devang Pateleda56172006-12-12 23:34:33 +000081 // Print passes managed by this manager
82 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000083 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000084 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
85 BasicBlockPass *BP = getContainedPass(Index);
86 BP->dumpPassStructure(Offset + 1);
87 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000088 }
89 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000090
91 BasicBlockPass *getContainedPass(unsigned N) {
92 assert ( N < PassVector.size() && "Pass number out of range!");
93 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
94 return BP;
95 }
Devang Patel3b3f8992007-01-11 01:10:25 +000096
97 virtual PassManagerType getPassManagerType() {
98 return PMT_BasicBlockPassManager;
99 }
Devang Patelca58e352006-11-08 10:05:38 +0000100};
101
Devang Patele7599552007-01-12 18:52:44 +0000102}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000103
Devang Patele7599552007-01-12 18:52:44 +0000104namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000105
Devang Patel10c2ca62006-12-12 22:47:13 +0000106//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000107// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000108//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000109/// FunctionPassManagerImpl manages FPPassManagers
110class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000111 public PMDataManager,
112 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000113public:
114
Devang Patel0f080042007-01-12 17:23:48 +0000115 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000116
117 /// add - Add a pass to the queue of passes to run. This passes ownership of
118 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
119 /// will be destroyed as well, so there is no need to delete the pass. This
120 /// implies that all passes MUST be allocated with 'new'.
121 void add(Pass *P) {
122 schedulePass(P);
123 }
124
125 /// run - Execute all of the passes scheduled for execution. Keep track of
126 /// whether any of the passes modifies the module, and if so, return true.
127 bool run(Function &F);
128
129 /// doInitialization - Run all of the initializers for the function passes.
130 ///
131 bool doInitialization(Module &M);
132
133 /// doFinalization - Run all of the initializers for the function passes.
134 ///
135 bool doFinalization(Module &M);
136
137 /// Pass Manager itself does not invalidate any analysis info.
138 void getAnalysisUsage(AnalysisUsage &Info) const {
139 Info.setPreservesAll();
140 }
141
142 inline void addTopLevelPass(Pass *P) {
143
144 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
145
146 // P is a immutable pass and it will be managed by this
147 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000148 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000149 P->setResolver(AR);
150 initializeAnalysisImpl(P);
151 addImmutablePass(IP);
152 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000153 } else {
154 // Assign manager
155 if (activeStack.empty()) {
156 FPPassManager *FPP = new FPPassManager(getDepth() + 1);
157 FPP->setTopLevelManager(this->getTopLevelManager());
158 addPassManager(FPP);
159 activeStack.push(FPP);
160 }
161 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000162 }
Devang Patel0f080042007-01-12 17:23:48 +0000163
Devang Patel67d6a5e2006-12-19 19:46:59 +0000164 }
165
166 FPPassManager *getContainedManager(unsigned N) {
167 assert ( N < PassManagers.size() && "Pass number out of range!");
168 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
169 return FP;
170 }
171
Devang Patel67d6a5e2006-12-19 19:46:59 +0000172};
173
174//===----------------------------------------------------------------------===//
175// MPPassManager
176//
177/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000178/// It batches all Module passes passes and function pass managers together and
179/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000180class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000181
182public:
Devang Patel0f080042007-01-12 17:23:48 +0000183 MPPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000184
185 /// run - Execute all of the passes scheduled for execution. Keep track of
186 /// whether any of the passes modifies the module, and if so, return true.
187 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000188
Devang Patelf9d96b92006-12-07 19:57:52 +0000189 /// Pass Manager itself does not invalidate any analysis info.
190 void getAnalysisUsage(AnalysisUsage &Info) const {
191 Info.setPreservesAll();
192 }
193
Devang Pateleda56172006-12-12 23:34:33 +0000194 // Print passes managed by this manager
195 void dumpPassStructure(unsigned Offset) {
196 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000197 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
198 ModulePass *MP = getContainedPass(Index);
199 MP->dumpPassStructure(Offset + 1);
200 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000201 }
202 }
203
Devang Patelabfbe3b2006-12-16 00:56:26 +0000204 ModulePass *getContainedPass(unsigned N) {
205 assert ( N < PassVector.size() && "Pass number out of range!");
206 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
207 return MP;
208 }
209
Devang Patel3b3f8992007-01-11 01:10:25 +0000210 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
Devang Patelca58e352006-11-08 10:05:38 +0000211};
212
Devang Patel10c2ca62006-12-12 22:47:13 +0000213//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000214// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000215//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000216/// PassManagerImpl manages MPPassManagers
217class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000218 public PMDataManager,
219 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000220
221public:
222
Devang Patel0f080042007-01-12 17:23:48 +0000223 PassManagerImpl(int Depth) : PMDataManager(Depth) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000224
Devang Patel376fefa2006-11-08 10:29:57 +0000225 /// add - Add a pass to the queue of passes to run. This passes ownership of
226 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
227 /// will be destroyed as well, so there is no need to delete the pass. This
228 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000229 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000230 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000231 }
Devang Patel376fefa2006-11-08 10:29:57 +0000232
233 /// run - Execute all of the passes scheduled for execution. Keep track of
234 /// whether any of the passes modifies the module, and if so, return true.
235 bool run(Module &M);
236
Devang Patelf9d96b92006-12-07 19:57:52 +0000237 /// Pass Manager itself does not invalidate any analysis info.
238 void getAnalysisUsage(AnalysisUsage &Info) const {
239 Info.setPreservesAll();
240 }
241
Devang Patelabcd1d32006-12-07 21:27:23 +0000242 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000243
Devang Patelfa971cd2006-12-08 23:57:43 +0000244 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000245
246 // P is a immutable pass and it will be managed by this
247 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000248 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000249 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000250 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000251 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000252 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000253 } else {
254
255 // Assign manager
256 if (activeStack.empty()) {
257 MPPassManager *MPP = new MPPassManager(getDepth() + 1);
258 MPP->setTopLevelManager(this->getTopLevelManager());
259 addPassManager(MPP);
260 activeStack.push(MPP);
261 }
262
263 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000264 }
Devang Patel0f080042007-01-12 17:23:48 +0000265
Devang Patelabcd1d32006-12-07 21:27:23 +0000266 }
267
Devang Patel67d6a5e2006-12-19 19:46:59 +0000268 MPPassManager *getContainedManager(unsigned N) {
269 assert ( N < PassManagers.size() && "Pass number out of range!");
270 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
271 return MP;
272 }
273
Devang Patel376fefa2006-11-08 10:29:57 +0000274};
275
Devang Patelffca9102006-12-15 19:39:30 +0000276} // End of llvm namespace
277
278namespace {
279
Devang Patelb8817b92006-12-14 00:59:42 +0000280//===----------------------------------------------------------------------===//
281// TimingInfo Class - This class is used to calculate information about the
282// amount of time each pass takes to execute. This only happens when
283// -time-passes is enabled on the command line.
284//
285
Devang Patelffca9102006-12-15 19:39:30 +0000286class VISIBILITY_HIDDEN TimingInfo {
Devang Patelb8817b92006-12-14 00:59:42 +0000287 std::map<Pass*, Timer> TimingData;
288 TimerGroup TG;
289
290public:
291 // Use 'create' member to get this.
292 TimingInfo() : TG("... Pass execution timing report ...") {}
293
294 // TimingDtor - Print out information about timing information
295 ~TimingInfo() {
296 // Delete all of the timers...
297 TimingData.clear();
298 // TimerGroup is deleted next, printing the report.
299 }
300
301 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
302 // to a non null value (if the -time-passes option is enabled) or it leaves it
303 // null. It may be called multiple times.
304 static void createTheTimeInfo();
305
306 void passStarted(Pass *P) {
307
308 if (dynamic_cast<PMDataManager *>(P))
309 return;
310
311 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
312 if (I == TimingData.end())
313 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
314 I->second.startTimer();
315 }
316 void passEnded(Pass *P) {
317
318 if (dynamic_cast<PMDataManager *>(P))
319 return;
320
321 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
322 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
323 I->second.stopTimer();
324 }
325};
326
327static TimingInfo *TheTimeInfo;
328
Devang Patelffca9102006-12-15 19:39:30 +0000329} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000330
Devang Patela1514cb2006-12-07 19:39:39 +0000331//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000332// PMTopLevelManager implementation
333
334/// Set pass P as the last user of the given analysis passes.
335void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
336 Pass *P) {
337
338 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
339 E = AnalysisPasses.end(); I != E; ++I) {
340 Pass *AP = *I;
341 LastUser[AP] = P;
342 // If AP is the last user of other passes then make P last user of
343 // such passes.
344 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
345 LUE = LastUser.end(); LUI != LUE; ++LUI) {
346 if (LUI->second == AP)
347 LastUser[LUI->first] = P;
348 }
349 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000350}
351
352/// Collect passes whose last user is P
353void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
354 Pass *P) {
355 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
356 LUE = LastUser.end(); LUI != LUE; ++LUI)
357 if (LUI->second == P)
358 LastUses.push_back(LUI->first);
359}
360
361/// Schedule pass P for execution. Make sure that passes required by
362/// P are run before P is run. Update analysis info maintained by
363/// the manager. Remove dead passes. This is a recursive function.
364void PMTopLevelManager::schedulePass(Pass *P) {
365
366 // TODO : Allocate function manager for this pass, other wise required set
367 // may be inserted into previous function manager
368
369 AnalysisUsage AnUsage;
370 P->getAnalysisUsage(AnUsage);
371 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
372 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
373 E = RequiredSet.end(); I != E; ++I) {
374
375 Pass *AnalysisPass = findAnalysisPass(*I);
376 if (!AnalysisPass) {
377 // Schedule this analysis run first.
378 AnalysisPass = (*I)->createPass();
379 schedulePass(AnalysisPass);
380 }
381 }
382
383 // Now all required passes are available.
384 addTopLevelPass(P);
385}
386
387/// Find the pass that implements Analysis AID. Search immutable
388/// passes and all pass managers. If desired pass is not found
389/// then return NULL.
390Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
391
392 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000393 // Check pass managers
394 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
395 E = PassManagers.end(); P == NULL && I != E; ++I) {
396 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
397 assert(PMD && "This is not a PassManager");
398 P = PMD->findAnalysisPass(AID, false);
399 }
400
401 // Check other pass managers
402 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
403 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
404 P = (*I)->findAnalysisPass(AID, false);
405
Devang Patelafb1f3622006-12-12 22:35:25 +0000406 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
407 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
408 const PassInfo *PI = (*I)->getPassInfo();
409 if (PI == AID)
410 P = *I;
411
412 // If Pass not found then check the interfaces implemented by Immutable Pass
413 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000414 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
415 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
416 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000417 }
418 }
419
Devang Patelafb1f3622006-12-12 22:35:25 +0000420 return P;
421}
422
Devang Pateleda56172006-12-12 23:34:33 +0000423// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000424void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000425
Devang Patel67d6a5e2006-12-19 19:46:59 +0000426 if (PassDebugging_New < Structure)
427 return;
428
Devang Pateleda56172006-12-12 23:34:33 +0000429 // Print out the immutable passes
430 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
431 ImmutablePasses[i]->dumpPassStructure(0);
432 }
433
Devang Patel991aeba2006-12-15 20:13:01 +0000434 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000435 E = PassManagers.end(); I != E; ++I)
436 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000437}
438
Devang Patel991aeba2006-12-15 20:13:01 +0000439void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000440
441 if (PassDebugging_New < Arguments)
442 return;
443
444 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000445 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000446 E = PassManagers.end(); I != E; ++I) {
447 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
448 assert(PMD && "This is not a PassManager");
449 PMD->dumpPassArguments();
450 }
451 cerr << "\n";
452}
453
Devang Patele3068402006-12-21 00:16:50 +0000454void PMTopLevelManager::initializeAllAnalysisInfo() {
455
456 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
457 E = PassManagers.end(); I != E; ++I) {
458 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
459 assert(PMD && "This is not a PassManager");
460 PMD->initializeAnalysisInfo();
461 }
462
463 // Initailize other pass managers
464 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
465 E = IndirectPassManagers.end(); I != E; ++I)
466 (*I)->initializeAnalysisInfo();
467}
468
Devang Patele7599552007-01-12 18:52:44 +0000469/// Destructor
470PMTopLevelManager::~PMTopLevelManager() {
471 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
472 E = PassManagers.end(); I != E; ++I)
473 delete *I;
474
475 for (std::vector<ImmutablePass *>::iterator
476 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
477 delete *I;
478
479 PassManagers.clear();
480}
481
Devang Patelafb1f3622006-12-12 22:35:25 +0000482//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000483// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000484
Devang Pateld65e9e92006-11-08 01:31:28 +0000485/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000486/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000487bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000488
Devang Patel8f677ce2006-12-07 18:47:25 +0000489 // TODO
490 // If this pass is not preserving information that is required by a
491 // pass maintained by higher level pass manager then do not insert
492 // this pass into current manager. Use new manager. For example,
493 // For example, If FunctionPass F is not preserving ModulePass Info M1
494 // that is used by another ModulePass M2 then do not insert F in
495 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000496 return true;
497}
498
Devang Patel643676c2006-11-11 01:10:19 +0000499/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000500void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000501
Devang Patel643676c2006-11-11 01:10:19 +0000502 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000503 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000504
Devang Patele9976aa2006-12-07 19:33:53 +0000505 //This pass is the current implementation of all of the interfaces it
506 //implements as well.
507 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
508 for (unsigned i = 0, e = II.size(); i != e; ++i)
509 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000510 }
511}
512
Devang Patelf68a3492006-11-07 22:35:17 +0000513/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000514void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000515 AnalysisUsage AnUsage;
516 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000517
Devang Patel2e169c32006-12-07 20:03:49 +0000518 if (AnUsage.getPreservesAll())
519 return;
520
521 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000522 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000523 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000524 std::map<AnalysisID, Pass*>::iterator Info = I++;
525 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000526 PreservedSet.end()) {
527 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000528 if (!dynamic_cast<ImmutablePass*>(Info->second))
529 AvailableAnalysis.erase(Info);
530 }
Devang Patel349170f2006-11-11 01:24:55 +0000531 }
Devang Patelf68a3492006-11-07 22:35:17 +0000532}
533
Devang Patelca189262006-11-14 03:05:08 +0000534/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000535void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000536
537 std::vector<Pass *> DeadPasses;
538 TPM->collectLastUses(DeadPasses, P);
539
540 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
541 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000542
543 std::string Msg1 = " Freeing Pass '";
544 dumpPassInfo(*I, Msg1, Msg);
545
Devang Patelb8817b92006-12-14 00:59:42 +0000546 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000547 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000548 if (TheTimeInfo) TheTimeInfo->passEnded(P);
549
Devang Patel17ad0962006-12-08 00:37:52 +0000550 std::map<AnalysisID, Pass*>::iterator Pos =
551 AvailableAnalysis.find((*I)->getPassInfo());
552
Devang Patel475c4532006-12-08 00:59:05 +0000553 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000554 if (Pos != AvailableAnalysis.end())
555 AvailableAnalysis.erase(Pos);
556 }
Devang Patelca189262006-11-14 03:05:08 +0000557}
558
Devang Patel8f677ce2006-12-07 18:47:25 +0000559/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000560/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000561void PMDataManager::addPassToManager(Pass *P,
562 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000563
Devang Pateld440cd92006-12-08 23:53:00 +0000564 // This manager is going to manage pass P. Set up analysis resolver
565 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000566 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000567 P->setResolver(AR);
568
Devang Patel90b05e02006-11-11 02:04:19 +0000569 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000570
571 // At the moment, this pass is the last user of all required passes.
572 std::vector<Pass *> LastUses;
573 std::vector<Pass *> RequiredPasses;
574 unsigned PDepth = this->getDepth();
575
576 collectRequiredAnalysisPasses(RequiredPasses, P);
577 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
578 E = RequiredPasses.end(); I != E; ++I) {
579 Pass *PRequired = *I;
580 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000581
582 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
583 RDepth = DM.getDepth();
584
Devang Patelbc03f132006-12-07 23:55:10 +0000585 if (PDepth == RDepth)
586 LastUses.push_back(PRequired);
587 else if (PDepth > RDepth) {
588 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000589 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000590 } else {
591 // Note : This feature is not yet implemented
592 assert (0 &&
593 "Unable to handle Pass that requires lower level Analysis pass");
594 }
595 }
596
Devang Patel832bc072006-12-15 00:08:26 +0000597 LastUses.push_back(P);
598 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000599
Devang Patel17bff0d2006-12-07 22:09:36 +0000600 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000601 // Remove the analysis not preserved by this pass
602 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000603 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000604 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000605
606 // Add pass
607 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000608}
609
Devang Patel1d6267c2006-12-07 23:05:44 +0000610/// Populate RequiredPasses with the analysis pass that are required by
611/// pass P.
612void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
613 Pass *P) {
614 AnalysisUsage AnUsage;
615 P->getAnalysisUsage(AnUsage);
616 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
617 for (std::vector<AnalysisID>::const_iterator
618 I = RequiredSet.begin(), E = RequiredSet.end();
619 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000620 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000621 assert (AnalysisPass && "Analysis pass is not available");
622 RP.push_back(AnalysisPass);
623 }
Devang Patelf58183d2006-12-12 23:09:32 +0000624
625 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
626 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
627 E = IDs.end(); I != E; ++I) {
628 Pass *AnalysisPass = findAnalysisPass(*I, true);
629 assert (AnalysisPass && "Analysis pass is not available");
630 RP.push_back(AnalysisPass);
631 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000632}
633
Devang Patel07f4f582006-11-14 21:49:36 +0000634// All Required analyses should be available to the pass as it runs! Here
635// we fill in the AnalysisImpls member of the pass so that it can
636// successfully use the getAnalysis() method to retrieve the
637// implementations it needs.
638//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000639void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000640 AnalysisUsage AnUsage;
641 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000642
643 for (std::vector<const PassInfo *>::const_iterator
644 I = AnUsage.getRequiredSet().begin(),
645 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000646 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000647 if (Impl == 0)
648 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000649 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000650 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000651 }
652}
653
Devang Patel640c5bb2006-12-08 22:30:11 +0000654/// Find the pass that implements Analysis AID. If desired pass is not found
655/// then return NULL.
656Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
657
658 // Check if AvailableAnalysis map has one entry.
659 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
660
661 if (I != AvailableAnalysis.end())
662 return I->second;
663
664 // Search Parents through TopLevelManager
665 if (SearchParent)
666 return TPM->findAnalysisPass(AID);
667
Devang Patel9d759b82006-12-09 00:09:12 +0000668 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000669}
670
Devang Patel991aeba2006-12-15 20:13:01 +0000671// Print list of passes that are last used by P.
672void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
673
674 std::vector<Pass *> LUses;
675
676 assert (TPM && "Top Level Manager is missing");
677 TPM->collectLastUses(LUses, P);
678
679 for (std::vector<Pass *>::iterator I = LUses.begin(),
680 E = LUses.end(); I != E; ++I) {
681 llvm::cerr << "--" << std::string(Offset*2, ' ');
682 (*I)->dumpPassStructure(0);
683 }
684}
685
686void PMDataManager::dumpPassArguments() const {
687 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
688 E = PassVector.end(); I != E; ++I) {
689 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
690 PMD->dumpPassArguments();
691 else
692 if (const PassInfo *PI = (*I)->getPassInfo())
693 if (!PI->isAnalysisGroup())
694 cerr << " -" << PI->getPassArgument();
695 }
696}
697
698void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
699 std::string &Msg2) const {
700 if (PassDebugging_New < Executions)
701 return;
702 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
703 cerr << Msg1;
704 cerr << P->getPassName();
705 cerr << Msg2;
706}
707
708void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
709 const std::vector<AnalysisID> &Set)
710 const {
711 if (PassDebugging_New >= Details && !Set.empty()) {
712 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
713 for (unsigned i = 0; i != Set.size(); ++i) {
714 if (i) cerr << ",";
715 cerr << " " << Set[i]->getPassName();
716 }
717 cerr << "\n";
718 }
719}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000720
Devang Patele7599552007-01-12 18:52:44 +0000721// Destructor
722PMDataManager::~PMDataManager() {
723
724 for (std::vector<Pass *>::iterator I = PassVector.begin(),
725 E = PassVector.end(); I != E; ++I)
726 delete *I;
727
728 PassVector.clear();
729}
730
Devang Patel9bdf7d42006-12-08 23:28:54 +0000731//===----------------------------------------------------------------------===//
732// NOTE: Is this the right place to define this method ?
733// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000734Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000735 return PM.findAnalysisPass(ID, dir);
736}
737
Devang Patela1514cb2006-12-07 19:39:39 +0000738//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000739// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000740
Devang Patel6e5a1132006-11-07 21:31:57 +0000741/// Execute all of the passes scheduled for execution by invoking
742/// runOnBasicBlock method. Keep track of whether any of the passes modifies
743/// the function, and if so, return true.
744bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000745BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000746
Devang Patel745a6962006-12-12 23:15:28 +0000747 if (F.isExternal())
748 return false;
749
Devang Patele9585592006-12-08 01:38:28 +0000750 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000751
Devang Patel93a197c2006-12-14 00:08:04 +0000752 std::string Msg1 = "Executing Pass '";
753 std::string Msg3 = "' Made Modification '";
754
Devang Patel6e5a1132006-11-07 21:31:57 +0000755 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000756 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
757 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000758 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000759 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000760
Devang Patel200d3052006-12-13 23:50:44 +0000761 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000762 dumpPassInfo(BP, Msg1, Msg2);
763 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000764
Devang Patelabfbe3b2006-12-16 00:56:26 +0000765 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000766
Devang Patelabfbe3b2006-12-16 00:56:26 +0000767 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000768 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000769 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000770
771 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000772 dumpPassInfo(BP, Msg3, Msg2);
773 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000774
Devang Patelabfbe3b2006-12-16 00:56:26 +0000775 removeNotPreservedAnalysis(BP);
776 recordAvailableAnalysis(BP);
777 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +0000778 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000779 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000780}
781
Devang Patel475c4532006-12-08 00:59:05 +0000782// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000783inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000784 bool Changed = false;
785
Devang Patelabfbe3b2006-12-16 00:56:26 +0000786 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
787 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000788 Changed |= BP->doInitialization(M);
789 }
790
791 return Changed;
792}
793
Devang Patel67d6a5e2006-12-19 19:46:59 +0000794inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000795 bool Changed = false;
796
Devang Patelabfbe3b2006-12-16 00:56:26 +0000797 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
798 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000799 Changed |= BP->doFinalization(M);
800 }
801
802 return Changed;
803}
804
Devang Patel67d6a5e2006-12-19 19:46:59 +0000805inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000806 bool Changed = false;
807
Devang Patelabfbe3b2006-12-16 00:56:26 +0000808 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
809 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000810 Changed |= BP->doInitialization(F);
811 }
812
813 return Changed;
814}
815
Devang Patel67d6a5e2006-12-19 19:46:59 +0000816inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000817 bool Changed = false;
818
Devang Patelabfbe3b2006-12-16 00:56:26 +0000819 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
820 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000821 Changed |= BP->doFinalization(F);
822 }
823
824 return Changed;
825}
826
827
Devang Patela1514cb2006-12-07 19:39:39 +0000828//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000829// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000830
Devang Patel4e12f862006-11-08 10:44:40 +0000831/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +0000832FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000833 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000834 // FPM is the top level manager.
835 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000836
837 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +0000838 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +0000839 FPM->setResolver(AR);
840
Devang Patel1f653682006-12-08 18:57:16 +0000841 MP = P;
842}
843
Devang Patelb67904d2006-12-13 02:36:01 +0000844FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000845 delete FPM;
846}
847
Devang Patel4e12f862006-11-08 10:44:40 +0000848/// add - Add a pass to the queue of passes to run. This passes
849/// ownership of the Pass to the PassManager. When the
850/// PassManager_X is destroyed, the pass will be destroyed as well, so
851/// there is no need to delete the pass. (TODO delete passes.)
852/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +0000853void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000854 FPM->add(P);
855}
856
Devang Patel9f3083e2006-11-15 19:39:54 +0000857/// run - Execute all of the passes scheduled for execution. Keep
858/// track of whether any of the passes modifies the function, and if
859/// so, return true.
860///
Devang Patelb67904d2006-12-13 02:36:01 +0000861bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000862 std::string errstr;
863 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000864 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000865 abort();
866 }
Devang Patel272908d2006-12-08 22:57:48 +0000867 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000868}
869
870
Devang Patelff631ae2006-11-15 01:27:05 +0000871/// doInitialization - Run all of the initializers for the function passes.
872///
Devang Patelb67904d2006-12-13 02:36:01 +0000873bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000874 return FPM->doInitialization(*MP->getModule());
875}
876
877/// doFinalization - Run all of the initializers for the function passes.
878///
Devang Patelb67904d2006-12-13 02:36:01 +0000879bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000880 return FPM->doFinalization(*MP->getModule());
881}
882
Devang Patela1514cb2006-12-07 19:39:39 +0000883//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000884// FunctionPassManagerImpl implementation
885//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000886inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
887 bool Changed = false;
888
889 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
890 FPPassManager *FP = getContainedManager(Index);
891 Changed |= FP->doInitialization(M);
892 }
893
894 return Changed;
895}
896
897inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
898 bool Changed = false;
899
900 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
901 FPPassManager *FP = getContainedManager(Index);
902 Changed |= FP->doFinalization(M);
903 }
904
905 return Changed;
906}
907
908// Execute all the passes managed by this top level manager.
909// Return true if any function is modified by a pass.
910bool FunctionPassManagerImpl::run(Function &F) {
911
912 bool Changed = false;
913
914 TimingInfo::createTheTimeInfo();
915
916 dumpArguments();
917 dumpPasses();
918
Devang Patele3068402006-12-21 00:16:50 +0000919 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +0000920 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
921 FPPassManager *FP = getContainedManager(Index);
922 Changed |= FP->runOnFunction(F);
923 }
924 return Changed;
925}
926
927//===----------------------------------------------------------------------===//
928// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000929
Devang Patele7599552007-01-12 18:52:44 +0000930/// Print passes managed by this manager
931void FPPassManager::dumpPassStructure(unsigned Offset) {
932 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
933 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
934 FunctionPass *FP = getContainedPass(Index);
935 FP->dumpPassStructure(Offset + 1);
936 dumpLastUses(FP, Offset+1);
937 }
938}
939
940
Devang Patel0c2012f2006-11-07 21:49:50 +0000941/// Execute all of the passes scheduled for execution by invoking
942/// runOnFunction method. Keep track of whether any of the passes modifies
943/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000944bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000945
946 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +0000947
948 if (F.isExternal())
949 return false;
950
Devang Patel93a197c2006-12-14 00:08:04 +0000951 std::string Msg1 = "Executing Pass '";
952 std::string Msg3 = "' Made Modification '";
953
Devang Patelabfbe3b2006-12-16 00:56:26 +0000954 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
955 FunctionPass *FP = getContainedPass(Index);
956
Devang Patelf6d1d212006-12-14 00:25:06 +0000957 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000958 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +0000959
Devang Patel200d3052006-12-13 23:50:44 +0000960 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000961 dumpPassInfo(FP, Msg1, Msg2);
962 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000963
Devang Patelabfbe3b2006-12-16 00:56:26 +0000964 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +0000965
Devang Patelabfbe3b2006-12-16 00:56:26 +0000966 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +0000967 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000968 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +0000969
970 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000971 dumpPassInfo(FP, Msg3, Msg2);
972 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000973
Devang Patelabfbe3b2006-12-16 00:56:26 +0000974 removeNotPreservedAnalysis(FP);
975 recordAvailableAnalysis(FP);
976 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +0000977 }
978 return Changed;
979}
980
Devang Patel67d6a5e2006-12-19 19:46:59 +0000981bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000982
Devang Patel67d6a5e2006-12-19 19:46:59 +0000983 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000984
985 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
986 this->runOnFunction(*I);
987
988 return Changed |= doFinalization(M);
989}
990
991inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +0000992 bool Changed = false;
993
Devang Patelabfbe3b2006-12-16 00:56:26 +0000994 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
995 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +0000996 Changed |= FP->doInitialization(M);
997 }
998
999 return Changed;
1000}
1001
Devang Patel67d6a5e2006-12-19 19:46:59 +00001002inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001003 bool Changed = false;
1004
Devang Patelabfbe3b2006-12-16 00:56:26 +00001005 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1006 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001007 Changed |= FP->doFinalization(M);
1008 }
1009
Devang Patelff631ae2006-11-15 01:27:05 +00001010 return Changed;
1011}
1012
Devang Patela1514cb2006-12-07 19:39:39 +00001013//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001014// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001015
Devang Patel05e1a972006-11-07 22:03:15 +00001016/// Execute all of the passes scheduled for execution by invoking
1017/// runOnModule method. Keep track of whether any of the passes modifies
1018/// the module, and if so, return true.
1019bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001020MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001021 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001022
Devang Patel93a197c2006-12-14 00:08:04 +00001023 std::string Msg1 = "Executing Pass '";
1024 std::string Msg3 = "' Made Modification '";
1025
Devang Patelabfbe3b2006-12-16 00:56:26 +00001026 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1027 ModulePass *MP = getContainedPass(Index);
1028
Devang Patelf6d1d212006-12-14 00:25:06 +00001029 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001030 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001031
Devang Patel200d3052006-12-13 23:50:44 +00001032 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +00001033 dumpPassInfo(MP, Msg1, Msg2);
1034 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001035
Devang Patelabfbe3b2006-12-16 00:56:26 +00001036 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001037
Devang Patelabfbe3b2006-12-16 00:56:26 +00001038 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001039 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001040 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001041
1042 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001043 dumpPassInfo(MP, Msg3, Msg2);
1044 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001045
Devang Patelabfbe3b2006-12-16 00:56:26 +00001046 removeNotPreservedAnalysis(MP);
1047 recordAvailableAnalysis(MP);
1048 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001049 }
1050 return Changed;
1051}
1052
Devang Patela1514cb2006-12-07 19:39:39 +00001053//===----------------------------------------------------------------------===//
1054// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001055//
Devang Patelc290c8a2006-11-07 22:23:34 +00001056/// run - Execute all of the passes scheduled for execution. Keep track of
1057/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001058bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001059
Devang Patelc290c8a2006-11-07 22:23:34 +00001060 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001061
Devang Patelb8817b92006-12-14 00:59:42 +00001062 TimingInfo::createTheTimeInfo();
1063
Devang Patelcfd70c42006-12-13 22:10:00 +00001064 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001065 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001066
Devang Patele3068402006-12-21 00:16:50 +00001067 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001068 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1069 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001070 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001071 }
1072 return Changed;
1073}
Devang Patel376fefa2006-11-08 10:29:57 +00001074
Devang Patela1514cb2006-12-07 19:39:39 +00001075//===----------------------------------------------------------------------===//
1076// PassManager implementation
1077
Devang Patel376fefa2006-11-08 10:29:57 +00001078/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001079PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001080 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001081 // PM is the top level manager
1082 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001083}
1084
Devang Patelb67904d2006-12-13 02:36:01 +00001085PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001086 delete PM;
1087}
1088
Devang Patel376fefa2006-11-08 10:29:57 +00001089/// add - Add a pass to the queue of passes to run. This passes ownership of
1090/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1091/// will be destroyed as well, so there is no need to delete the pass. This
1092/// implies that all passes MUST be allocated with 'new'.
1093void
Devang Patelb67904d2006-12-13 02:36:01 +00001094PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001095 PM->add(P);
1096}
1097
1098/// run - Execute all of the passes scheduled for execution. Keep track of
1099/// whether any of the passes modifies the module, and if so, return true.
1100bool
Devang Patelb67904d2006-12-13 02:36:01 +00001101PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001102 return PM->run(M);
1103}
1104
Devang Patelb8817b92006-12-14 00:59:42 +00001105//===----------------------------------------------------------------------===//
1106// TimingInfo Class - This class is used to calculate information about the
1107// amount of time each pass takes to execute. This only happens with
1108// -time-passes is enabled on the command line.
1109//
1110bool llvm::TimePassesIsEnabled = false;
1111static cl::opt<bool,true>
1112EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1113 cl::desc("Time each pass, printing elapsed time for each on exit"));
1114
1115// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1116// a non null value (if the -time-passes option is enabled) or it leaves it
1117// null. It may be called multiple times.
1118void TimingInfo::createTheTimeInfo() {
1119 if (!TimePassesIsEnabled || TheTimeInfo) return;
1120
1121 // Constructed the first time this is called, iff -time-passes is enabled.
1122 // This guarantees that the object will be constructed before static globals,
1123 // thus it will be destroyed before them.
1124 static ManagedStatic<TimingInfo> TTI;
1125 TheTimeInfo = &*TTI;
1126}
1127
Devang Patel1c56a632007-01-08 19:29:38 +00001128//===----------------------------------------------------------------------===//
1129// PMStack implementation
1130//
Devang Patelad98d232007-01-11 22:15:30 +00001131
Devang Patel1c56a632007-01-08 19:29:38 +00001132// Pop Pass Manager from the stack and clear its analysis info.
1133void PMStack::pop() {
1134
1135 PMDataManager *Top = this->top();
1136 Top->initializeAnalysisInfo();
1137
1138 S.pop_back();
1139}
1140
1141// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001142void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001143
Devang Patel15701b52007-01-11 00:19:00 +00001144 PMDataManager *Top = NULL;
1145 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1146 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001147
Devang Patel15701b52007-01-11 00:19:00 +00001148 if (this->empty()) {
1149 Top = PM;
1150 }
1151 else {
1152 Top = this->top();
1153 PMTopLevelManager *TPM = Top->getTopLevelManager();
1154
1155 assert (TPM && "Unable to find top level manager");
1156 TPM->addIndirectPassManager(PM);
1157 PM->setTopLevelManager(TPM);
1158 }
1159
1160 AnalysisResolver *AR = new AnalysisResolver(*Top);
1161 P->setResolver(AR);
1162
1163 S.push_back(PM);
1164}
1165
1166// Dump content of the pass manager stack.
1167void PMStack::dump() {
1168 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1169 E = S.end(); I != E; ++I) {
1170 Pass *P = dynamic_cast<Pass *>(*I);
1171 printf ("%s ", P->getPassName());
1172 }
1173 if (!S.empty())
1174 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001175}
1176
1177// Walk Pass Manager stack and set LastUse markers if any
1178// manager is transfering this priviledge to its parent manager
1179void PMStack::handleLastUserOverflow() {
1180
1181 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1182
1183 PMDataManager *Child = *I++;
1184 if (I != E) {
1185 PMDataManager *Parent = *I++;
1186 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1187 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1188 if (!TLU.empty()) {
1189 Pass *P = dynamic_cast<Pass *>(Parent);
1190 TPM->setLastUser(TLU, P);
1191 }
1192 }
1193 }
1194}
1195
1196/// Find appropriate Module Pass Manager in the PM Stack and
1197/// add self into that manager.
1198void ModulePass::assignPassManager(PMStack &PMS) {
1199
Devang Patel1c56a632007-01-08 19:29:38 +00001200 // Find Module Pass Manager
1201 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001202 if (PMS.top()->getPassManagerType() > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001203 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001204 else
1205 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001206 }
Devang Patelac99eca2007-01-11 19:59:06 +00001207 MPPassManager *MPP = dynamic_cast<MPPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001208
1209 assert(MPP && "Unable to find Module Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001210 MPP->addPassToManager(this);
1211}
1212
1213/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1214/// in the PM Stack and add self into that manager.
1215void FunctionPass::assignPassManager(PMStack &PMS) {
1216
Devang Patel15701b52007-01-11 00:19:00 +00001217 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001218 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001219 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1220 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001221 else
Devang Patelac99eca2007-01-11 19:59:06 +00001222 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001223 }
Devang Patelac99eca2007-01-11 19:59:06 +00001224 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001225
Devang Patel15701b52007-01-11 00:19:00 +00001226 // Create new Function Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001227 if (!FPP) {
Devang Patel1c56a632007-01-08 19:29:38 +00001228 assert(!PMS.empty() && "Unable to create Function Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001229 PMDataManager *PMD = PMS.top();
Devang Patel1c56a632007-01-08 19:29:38 +00001230
Devang Patel15701b52007-01-11 00:19:00 +00001231 // [1] Create new Function Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001232 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patel15701b52007-01-11 00:19:00 +00001233
1234 // [2] Set up new manager's top level manager
1235 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1236 TPM->addIndirectPassManager(FPP);
1237
1238 // [3] Assign manager to manage this new manager. This may create
1239 // and push new managers into PMS
1240 Pass *P = dynamic_cast<Pass *>(FPP);
1241 P->assignPassManager(PMS);
1242
1243 // [4] Push new manager into PMS
Devang Patel1c56a632007-01-08 19:29:38 +00001244 PMS.push(FPP);
1245 }
1246
Devang Patel15701b52007-01-11 00:19:00 +00001247 // Assign FPP as the manager of this pass.
Devang Patel1c56a632007-01-08 19:29:38 +00001248 FPP->addPassToManager(this);
1249}
1250
1251/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1252/// in the PM Stack and add self into that manager.
1253void BasicBlockPass::assignPassManager(PMStack &PMS) {
1254
1255 BBPassManager *BBP = NULL;
1256
Devang Patel15701b52007-01-11 00:19:00 +00001257 // Basic Pass Manager is a leaf pass manager. It does not handle
1258 // any other pass manager.
1259 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001260 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001261 }
1262
Devang Patel15701b52007-01-11 00:19:00 +00001263 // If leaf manager is not Basic Block Pass manager then create new
1264 // basic Block Pass manager.
1265
Devang Patel1c56a632007-01-08 19:29:38 +00001266 if (!BBP) {
Devang Patel1c56a632007-01-08 19:29:38 +00001267 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
Devang Patel1c56a632007-01-08 19:29:38 +00001268 PMDataManager *PMD = PMS.top();
Devang Patel1c56a632007-01-08 19:29:38 +00001269
Devang Patel15701b52007-01-11 00:19:00 +00001270 // [1] Create new Basic Block Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001271 BBP = new BBPassManager(PMD->getDepth() + 1);
Devang Patel15701b52007-01-11 00:19:00 +00001272
1273 // [2] Set up new manager's top level manager
1274 // Basic Block Pass Manager does not live by itself
1275 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1276 TPM->addIndirectPassManager(BBP);
1277
1278 // [3] Assign manager to manage this new manager. This may create
1279 // and push new managers into PMS
1280 Pass *P = dynamic_cast<Pass *>(BBP);
1281 P->assignPassManager(PMS);
1282
1283 // [4] Push new manager into PMS
Devang Patel1c56a632007-01-08 19:29:38 +00001284 PMS.push(BBP);
1285 }
1286
Devang Patel15701b52007-01-11 00:19:00 +00001287 // Assign BBP as the manager of this pass.
Devang Patel1c56a632007-01-08 19:29:38 +00001288 BBP->addPassToManager(this);
1289}
1290
Devang Patelc6b5a552007-01-05 20:16:23 +00001291