blob: ae73373b8572040a87dd33842a3cd60a5090c939 [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
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>
Dan Gohman8c43e412007-10-03 19:04:09 +000025using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000026
Devang Patele7599552007-01-12 18:52:44 +000027// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000028
Devang Patelf1567a52006-12-13 20:03:48 +000029namespace llvm {
30
31//===----------------------------------------------------------------------===//
32// Pass debugging information. Often it is useful to find out what pass is
33// running when a crash occurs in a utility. When this library is compiled with
34// debugging on, a command line option (--debug-pass) is enabled that causes the
35// pass name to be printed before it executes.
36//
37
Devang Patel03fb5872006-12-13 21:13:31 +000038// Different debug levels that can be enabled...
39enum PassDebugLevel {
40 None, Arguments, Structure, Executions, Details
41};
42
Devang Patelf1567a52006-12-13 20:03:48 +000043static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000044PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000045 cl::desc("Print PassManager debugging information"),
46 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000047 clEnumVal(None , "disable debug output"),
48 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
49 clEnumVal(Structure , "print pass structure before run()"),
50 clEnumVal(Executions, "print pass name before it is executed"),
51 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000052 clEnumValEnd));
53} // End of llvm namespace
54
Devang Patelffca9102006-12-15 19:39:30 +000055namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000056
Devang Patelf33f3eb2006-12-07 19:21:29 +000057//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000058// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000059//
Devang Patel67d6a5e2006-12-19 19:46:59 +000060/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000061/// pass together and sequence them to process one basic block before
62/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000063class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
64 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000065
66public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000067 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +000068 explicit BBPassManager(int Depth)
Devang Patel09f162c2007-05-01 21:15:47 +000069 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +000070
Devang Patelca58e352006-11-08 10:05:38 +000071 /// Execute all of the passes scheduled for execution. Keep track of
72 /// whether any of the passes modifies the function, and if so, return true.
73 bool runOnFunction(Function &F);
74
Devang Patelf9d96b92006-12-07 19:57:52 +000075 /// Pass Manager itself does not invalidate any analysis info.
76 void getAnalysisUsage(AnalysisUsage &Info) const {
77 Info.setPreservesAll();
78 }
79
Devang Patel475c4532006-12-08 00:59:05 +000080 bool doInitialization(Module &M);
81 bool doInitialization(Function &F);
82 bool doFinalization(Module &M);
83 bool doFinalization(Function &F);
84
Devang Patele3858e62007-02-01 22:08:25 +000085 virtual const char *getPassName() const {
86 return "BasicBlock Pass Manager";
87 }
88
Devang Pateleda56172006-12-12 23:34:33 +000089 // Print passes managed by this manager
90 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000091 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000092 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
93 BasicBlockPass *BP = getContainedPass(Index);
94 BP->dumpPassStructure(Offset + 1);
95 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000096 }
97 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000098
99 BasicBlockPass *getContainedPass(unsigned N) {
100 assert ( N < PassVector.size() && "Pass number out of range!");
101 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
102 return BP;
103 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000104
Devang Patel28349ab2007-02-27 15:00:39 +0000105 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000106 return PMT_BasicBlockPassManager;
107 }
Devang Patelca58e352006-11-08 10:05:38 +0000108};
109
Devang Patel8c78a0b2007-05-03 01:11:54 +0000110char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000111}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000112
Devang Patele7599552007-01-12 18:52:44 +0000113namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000114
Devang Patel10c2ca62006-12-12 22:47:13 +0000115//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000116// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000117//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000118/// FunctionPassManagerImpl manages FPPassManagers
119class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000120 public PMDataManager,
121 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000122public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000123 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000124 explicit FunctionPassManagerImpl(int Depth) :
Devang Patel09f162c2007-05-01 21:15:47 +0000125 Pass((intptr_t)&ID), PMDataManager(Depth),
126 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000127
128 /// add - Add a pass to the queue of passes to run. This passes ownership of
129 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
130 /// will be destroyed as well, so there is no need to delete the pass. This
131 /// implies that all passes MUST be allocated with 'new'.
132 void add(Pass *P) {
133 schedulePass(P);
134 }
135
136 /// run - Execute all of the passes scheduled for execution. Keep track of
137 /// whether any of the passes modifies the module, and if so, return true.
138 bool run(Function &F);
139
140 /// doInitialization - Run all of the initializers for the function passes.
141 ///
142 bool doInitialization(Module &M);
143
Dan Gohmane6656eb2007-07-30 14:51:13 +0000144 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000145 ///
146 bool doFinalization(Module &M);
147
148 /// Pass Manager itself does not invalidate any analysis info.
149 void getAnalysisUsage(AnalysisUsage &Info) const {
150 Info.setPreservesAll();
151 }
152
153 inline void addTopLevelPass(Pass *P) {
154
155 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
156
157 // P is a immutable pass and it will be managed by this
158 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000159 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000160 P->setResolver(AR);
161 initializeAnalysisImpl(P);
162 addImmutablePass(IP);
163 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000164 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000165 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000166 }
Devang Patel0f080042007-01-12 17:23:48 +0000167
Devang Patel67d6a5e2006-12-19 19:46:59 +0000168 }
169
170 FPPassManager *getContainedManager(unsigned N) {
171 assert ( N < PassManagers.size() && "Pass number out of range!");
172 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
173 return FP;
174 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000175};
176
Devang Patel8c78a0b2007-05-03 01:11:54 +0000177char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000178//===----------------------------------------------------------------------===//
179// MPPassManager
180//
181/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000182/// It batches all Module passes passes and function pass managers together and
183/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000184class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000185
186public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000187 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000188 explicit MPPassManager(int Depth) :
189 Pass((intptr_t)&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000190
191 // Delete on the fly managers.
192 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000193 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000194 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
195 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000196 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000197 delete FPP;
198 }
199 }
200
Devang Patelca58e352006-11-08 10:05:38 +0000201 /// run - Execute all of the passes scheduled for execution. Keep track of
202 /// whether any of the passes modifies the module, and if so, return true.
203 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000204
Devang Patelf9d96b92006-12-07 19:57:52 +0000205 /// Pass Manager itself does not invalidate any analysis info.
206 void getAnalysisUsage(AnalysisUsage &Info) const {
207 Info.setPreservesAll();
208 }
209
Devang Patele64d3052007-04-16 20:12:57 +0000210 /// Add RequiredPass into list of lower level passes required by pass P.
211 /// RequiredPass is run on the fly by Pass Manager when P requests it
212 /// through getAnalysis interface.
213 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
214
Devang Patel69e9f6d2007-04-16 20:27:05 +0000215 /// Return function pass corresponding to PassInfo PI, that is
216 /// required by module pass MP. Instantiate analysis pass, by using
217 /// its runOnFunction() for function F.
218 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
219
Devang Patele3858e62007-02-01 22:08:25 +0000220 virtual const char *getPassName() const {
221 return "Module Pass Manager";
222 }
223
Devang Pateleda56172006-12-12 23:34:33 +0000224 // Print passes managed by this manager
225 void dumpPassStructure(unsigned Offset) {
226 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000227 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
228 ModulePass *MP = getContainedPass(Index);
229 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000230 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000231 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000232 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000233 }
234 }
235
Devang Patelabfbe3b2006-12-16 00:56:26 +0000236 ModulePass *getContainedPass(unsigned N) {
237 assert ( N < PassVector.size() && "Pass number out of range!");
238 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
239 return MP;
240 }
241
Devang Patel28349ab2007-02-27 15:00:39 +0000242 virtual PassManagerType getPassManagerType() const {
243 return PMT_ModulePassManager;
244 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000245
246 private:
247 /// Collection of on the fly FPPassManagers. These managers manage
248 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000249 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000250};
251
Devang Patel8c78a0b2007-05-03 01:11:54 +0000252char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000253//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000254// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000255//
Devang Patel09f162c2007-05-01 21:15:47 +0000256
Devang Patel67d6a5e2006-12-19 19:46:59 +0000257/// PassManagerImpl manages MPPassManagers
258class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000259 public PMDataManager,
260 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000261
262public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000263 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000264 explicit PassManagerImpl(int Depth) :
265 Pass((intptr_t)&ID), PMDataManager(Depth),
266 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000267
Devang Patel376fefa2006-11-08 10:29:57 +0000268 /// add - Add a pass to the queue of passes to run. This passes ownership of
269 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
270 /// will be destroyed as well, so there is no need to delete the pass. This
271 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000272 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000273 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000274 }
Devang Patel376fefa2006-11-08 10:29:57 +0000275
276 /// run - Execute all of the passes scheduled for execution. Keep track of
277 /// whether any of the passes modifies the module, and if so, return true.
278 bool run(Module &M);
279
Devang Patelf9d96b92006-12-07 19:57:52 +0000280 /// Pass Manager itself does not invalidate any analysis info.
281 void getAnalysisUsage(AnalysisUsage &Info) const {
282 Info.setPreservesAll();
283 }
284
Devang Patelabcd1d32006-12-07 21:27:23 +0000285 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000286
Devang Patelfa971cd2006-12-08 23:57:43 +0000287 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000288
289 // P is a immutable pass and it will be managed by this
290 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000291 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000292 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000293 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000294 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000295 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000296 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000297 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000298 }
Devang Patel0f080042007-01-12 17:23:48 +0000299
Devang Patelabcd1d32006-12-07 21:27:23 +0000300 }
301
Devang Patel67d6a5e2006-12-19 19:46:59 +0000302 MPPassManager *getContainedManager(unsigned N) {
303 assert ( N < PassManagers.size() && "Pass number out of range!");
304 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
305 return MP;
306 }
307
Devang Patel376fefa2006-11-08 10:29:57 +0000308};
309
Devang Patel8c78a0b2007-05-03 01:11:54 +0000310char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000311} // End of llvm namespace
312
313namespace {
314
315//===----------------------------------------------------------------------===//
316// TimingInfo Class - This class is used to calculate information about the
317// amount of time each pass takes to execute. This only happens when
318// -time-passes is enabled on the command line.
319//
320
321class VISIBILITY_HIDDEN TimingInfo {
322 std::map<Pass*, Timer> TimingData;
323 TimerGroup TG;
324
325public:
326 // Use 'create' member to get this.
327 TimingInfo() : TG("... Pass execution timing report ...") {}
328
329 // TimingDtor - Print out information about timing information
330 ~TimingInfo() {
331 // Delete all of the timers...
332 TimingData.clear();
333 // TimerGroup is deleted next, printing the report.
334 }
335
336 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
337 // to a non null value (if the -time-passes option is enabled) or it leaves it
338 // null. It may be called multiple times.
339 static void createTheTimeInfo();
340
341 void passStarted(Pass *P) {
342
343 if (dynamic_cast<PMDataManager *>(P))
344 return;
345
346 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
347 if (I == TimingData.end())
348 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
349 I->second.startTimer();
350 }
351 void passEnded(Pass *P) {
352
353 if (dynamic_cast<PMDataManager *>(P))
354 return;
355
356 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
357 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
358 I->second.stopTimer();
359 }
360};
361
Devang Patelb8817b92006-12-14 00:59:42 +0000362static TimingInfo *TheTimeInfo;
363
Devang Patel1c3633e2007-01-29 23:10:37 +0000364} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000365
Devang Patela1514cb2006-12-07 19:39:39 +0000366//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000367// PMTopLevelManager implementation
368
Devang Patel4268fc02007-01-16 02:00:38 +0000369/// Initialize top level manager. Create first pass manager.
370PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
371
372 if (t == TLM_Pass) {
373 MPPassManager *MPP = new MPPassManager(1);
374 MPP->setTopLevelManager(this);
375 addPassManager(MPP);
376 activeStack.push(MPP);
377 }
378 else if (t == TLM_Function) {
379 FPPassManager *FPP = new FPPassManager(1);
380 FPP->setTopLevelManager(this);
381 addPassManager(FPP);
382 activeStack.push(FPP);
383 }
384}
385
Devang Patelafb1f3622006-12-12 22:35:25 +0000386/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000387void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000388 Pass *P) {
389
Devang Patel8adae862007-07-20 18:04:54 +0000390 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000391 E = AnalysisPasses.end(); I != E; ++I) {
392 Pass *AP = *I;
393 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000394
395 if (P == AP)
396 continue;
397
Devang Patelafb1f3622006-12-12 22:35:25 +0000398 // If AP is the last user of other passes then make P last user of
399 // such passes.
400 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
401 LUE = LastUser.end(); LUI != LUE; ++LUI) {
402 if (LUI->second == AP)
403 LastUser[LUI->first] = P;
404 }
405 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000406}
407
408/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000409void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000410 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000411 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
412 LUE = LastUser.end(); LUI != LUE; ++LUI)
413 if (LUI->second == P)
414 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000415}
416
417/// Schedule pass P for execution. Make sure that passes required by
418/// P are run before P is run. Update analysis info maintained by
419/// the manager. Remove dead passes. This is a recursive function.
420void PMTopLevelManager::schedulePass(Pass *P) {
421
Devang Patel3312f752007-01-16 21:43:18 +0000422 // TODO : Allocate function manager for this pass, other wise required set
423 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000424
Devang Pateld74ede72007-03-06 01:06:16 +0000425 // Give pass a chance to prepare the stage.
426 P->preparePassManager(activeStack);
427
Devang Patelafb1f3622006-12-12 22:35:25 +0000428 AnalysisUsage AnUsage;
429 P->getAnalysisUsage(AnUsage);
430 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
431 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
432 E = RequiredSet.end(); I != E; ++I) {
433
434 Pass *AnalysisPass = findAnalysisPass(*I);
435 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000436 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000437 // Schedule this analysis run first only if it is not a lower level
438 // analysis pass. Lower level analsyis passes are run on the fly.
439 if (P->getPotentialPassManagerType () >=
440 AnalysisPass->getPotentialPassManagerType())
441 schedulePass(AnalysisPass);
442 else
443 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000444 }
445 }
446
447 // Now all required passes are available.
448 addTopLevelPass(P);
449}
450
451/// Find the pass that implements Analysis AID. Search immutable
452/// passes and all pass managers. If desired pass is not found
453/// then return NULL.
454Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
455
456 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000457 // Check pass managers
458 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
459 E = PassManagers.end(); P == NULL && I != E; ++I) {
460 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
461 assert(PMD && "This is not a PassManager");
462 P = PMD->findAnalysisPass(AID, false);
463 }
464
465 // Check other pass managers
466 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
467 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
468 P = (*I)->findAnalysisPass(AID, false);
469
Devang Patelafb1f3622006-12-12 22:35:25 +0000470 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
471 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
472 const PassInfo *PI = (*I)->getPassInfo();
473 if (PI == AID)
474 P = *I;
475
476 // If Pass not found then check the interfaces implemented by Immutable Pass
477 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000478 const std::vector<const PassInfo*> &ImmPI =
479 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000480 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
481 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000482 }
483 }
484
Devang Patelafb1f3622006-12-12 22:35:25 +0000485 return P;
486}
487
Devang Pateleda56172006-12-12 23:34:33 +0000488// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000489void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000490
Devang Patelfd4184322007-01-17 20:33:36 +0000491 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000492 return;
493
Devang Pateleda56172006-12-12 23:34:33 +0000494 // Print out the immutable passes
495 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
496 ImmutablePasses[i]->dumpPassStructure(0);
497 }
498
Devang Patel991aeba2006-12-15 20:13:01 +0000499 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000500 E = PassManagers.end(); I != E; ++I)
501 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000502}
503
Devang Patel991aeba2006-12-15 20:13:01 +0000504void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000505
Devang Patelfd4184322007-01-17 20:33:36 +0000506 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000507 return;
508
509 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000510 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000511 E = PassManagers.end(); I != E; ++I) {
512 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
513 assert(PMD && "This is not a PassManager");
514 PMD->dumpPassArguments();
515 }
516 cerr << "\n";
517}
518
Devang Patele3068402006-12-21 00:16:50 +0000519void PMTopLevelManager::initializeAllAnalysisInfo() {
520
521 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
522 E = PassManagers.end(); I != E; ++I) {
523 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
524 assert(PMD && "This is not a PassManager");
525 PMD->initializeAnalysisInfo();
526 }
527
528 // Initailize other pass managers
529 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
530 E = IndirectPassManagers.end(); I != E; ++I)
531 (*I)->initializeAnalysisInfo();
532}
533
Devang Patele7599552007-01-12 18:52:44 +0000534/// Destructor
535PMTopLevelManager::~PMTopLevelManager() {
536 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
537 E = PassManagers.end(); I != E; ++I)
538 delete *I;
539
540 for (std::vector<ImmutablePass *>::iterator
541 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
542 delete *I;
543
544 PassManagers.clear();
545}
546
Devang Patelafb1f3622006-12-12 22:35:25 +0000547//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000548// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000549
Devang Pateld65e9e92006-11-08 01:31:28 +0000550/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000551/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000552bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000553
Devang Patel8f677ce2006-12-07 18:47:25 +0000554 // TODO
555 // If this pass is not preserving information that is required by a
556 // pass maintained by higher level pass manager then do not insert
557 // this pass into current manager. Use new manager. For example,
558 // For example, If FunctionPass F is not preserving ModulePass Info M1
559 // that is used by another ModulePass M2 then do not insert F in
560 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000561 return true;
562}
563
Devang Patel643676c2006-11-11 01:10:19 +0000564/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000565void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000566
Devang Patel643676c2006-11-11 01:10:19 +0000567 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000568 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000569
Devang Patele9976aa2006-12-07 19:33:53 +0000570 //This pass is the current implementation of all of the interfaces it
571 //implements as well.
572 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
573 for (unsigned i = 0, e = II.size(); i != e; ++i)
574 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000575 }
576}
577
Devang Patel9d9fc902007-03-06 17:52:53 +0000578// Return true if P preserves high level analysis used by other
579// passes managed by this manager
580bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
581
582 AnalysisUsage AnUsage;
583 P->getAnalysisUsage(AnUsage);
584
585 if (AnUsage.getPreservesAll())
586 return true;
587
588 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
589 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
590 E = HigherLevelAnalysis.end(); I != E; ++I) {
591 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000592 if (!dynamic_cast<ImmutablePass*>(P1) &&
593 std::find(PreservedSet.begin(), PreservedSet.end(),
594 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000595 PreservedSet.end())
596 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000597 }
598
599 return true;
600}
601
Devang Patela273d1c2007-07-19 18:02:32 +0000602/// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
603void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000604 AnalysisUsage AnUsage;
605 P->getAnalysisUsage(AnUsage);
Devang Patelef432532007-07-19 05:36:09 +0000606 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000607
Devang Patelef432532007-07-19 05:36:09 +0000608 // Verify preserved analysis
Devang Patela273d1c2007-07-19 18:02:32 +0000609 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
610 E = PreservedSet.end(); I != E; ++I) {
611 AnalysisID AID = *I;
612 Pass *AP = findAnalysisPass(AID, true);
613 if (AP)
614 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000615 }
Devang Patela273d1c2007-07-19 18:02:32 +0000616}
617
618/// Remove Analyss not preserved by Pass P
619void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
620 AnalysisUsage AnUsage;
621 P->getAnalysisUsage(AnUsage);
Devang Patel2e169c32006-12-07 20:03:49 +0000622 if (AnUsage.getPreservesAll())
623 return;
624
Devang Patela273d1c2007-07-19 18:02:32 +0000625 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000626 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000627 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000628 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000629 if (!dynamic_cast<ImmutablePass*>(Info->second)
630 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
631 PreservedSet.end())
Devang Patel349170f2006-11-11 01:24:55 +0000632 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000633 AvailableAnalysis.erase(Info);
Devang Patel349170f2006-11-11 01:24:55 +0000634 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000635
636 // Check inherited analysis also. If P is not preserving analysis
637 // provided by parent manager then remove it here.
638 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
639
640 if (!InheritedAnalysis[Index])
641 continue;
642
643 for (std::map<AnalysisID, Pass*>::iterator
644 I = InheritedAnalysis[Index]->begin(),
645 E = InheritedAnalysis[Index]->end(); I != E; ) {
646 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000647 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
648 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000649 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000650 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000651 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000652 }
653 }
654
Devang Patelf68a3492006-11-07 22:35:17 +0000655}
656
Devang Patelca189262006-11-14 03:05:08 +0000657/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000658void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000659 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000660
Devang Patel8adae862007-07-20 18:04:54 +0000661 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000662
Devang Patel2ff44922007-04-16 20:39:59 +0000663 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000664 if (!TPM)
665 return;
666
Devang Patel17ad0962006-12-08 00:37:52 +0000667 TPM->collectLastUses(DeadPasses, P);
668
Devang Patel8adae862007-07-20 18:04:54 +0000669 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000670 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000671
Devang Patel003a5592007-03-05 20:01:30 +0000672 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000673
Devang Patel7ebf09d2007-03-05 18:20:51 +0000674 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000675 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000676 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000677
Devang Patel17ad0962006-12-08 00:37:52 +0000678 std::map<AnalysisID, Pass*>::iterator Pos =
679 AvailableAnalysis.find((*I)->getPassInfo());
680
Devang Patel475c4532006-12-08 00:59:05 +0000681 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000682 if (Pos != AvailableAnalysis.end())
683 AvailableAnalysis.erase(Pos);
684 }
Devang Patelca189262006-11-14 03:05:08 +0000685}
686
Devang Patel8f677ce2006-12-07 18:47:25 +0000687/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000688/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000689void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000690 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000691
Devang Pateld440cd92006-12-08 23:53:00 +0000692 // This manager is going to manage pass P. Set up analysis resolver
693 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000694 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000695 P->setResolver(AR);
696
Devang Patelec2b9a72007-03-05 22:57:49 +0000697 // If a FunctionPass F is the last user of ModulePass info M
698 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000699 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000700
Devang Patel90b05e02006-11-11 02:04:19 +0000701 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000702
703 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000704 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000705 SmallVector<Pass *, 8> RequiredPasses;
706 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
707
Devang Patelbc03f132006-12-07 23:55:10 +0000708 unsigned PDepth = this->getDepth();
709
Devang Patele64d3052007-04-16 20:12:57 +0000710 collectRequiredAnalysis(RequiredPasses,
711 ReqAnalysisNotAvailable, P);
712 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000713 E = RequiredPasses.end(); I != E; ++I) {
714 Pass *PRequired = *I;
715 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000716
717 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
718 RDepth = DM.getDepth();
719
Devang Patelbc03f132006-12-07 23:55:10 +0000720 if (PDepth == RDepth)
721 LastUses.push_back(PRequired);
722 else if (PDepth > RDepth) {
723 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000724 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000725 // Keep track of higher level analysis used by this manager.
726 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000727 } else
728 assert (0 && "Unable to accomodate Required Pass");
729 }
730
Devang Patel39786a92007-01-15 20:31:54 +0000731 // Set P as P's last user until someone starts using P.
732 // However, if P is a Pass Manager then it does not need
733 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000734 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000735 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000736 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000737
Devang Patelec2b9a72007-03-05 22:57:49 +0000738 if (!TransferLastUses.empty()) {
739 Pass *My_PM = dynamic_cast<Pass *>(this);
740 TPM->setLastUser(TransferLastUses, My_PM);
741 TransferLastUses.clear();
742 }
743
Devang Patel69e9f6d2007-04-16 20:27:05 +0000744 // Now, take care of required analysises that are not available.
745 for (SmallVector<AnalysisID, 8>::iterator
746 I = ReqAnalysisNotAvailable.begin(),
747 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
748 Pass *AnalysisPass = (*I)->createPass();
749 this->addLowerLevelRequiredPass(P, AnalysisPass);
750 }
751
Devang Patel17bff0d2006-12-07 22:09:36 +0000752 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000753 // Remove the analysis not preserved by this pass
754 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000755 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000756 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000757
758 // Add pass
759 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000760}
761
Devang Patele64d3052007-04-16 20:12:57 +0000762
763/// Populate RP with analysis pass that are required by
764/// pass P and are available. Populate RP_NotAvail with analysis
765/// pass that are required by pass P but are not available.
766void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
767 SmallVector<AnalysisID, 8> &RP_NotAvail,
768 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000769 AnalysisUsage AnUsage;
770 P->getAnalysisUsage(AnUsage);
771 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
772 for (std::vector<AnalysisID>::const_iterator
773 I = RequiredSet.begin(), E = RequiredSet.end();
774 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000775 AnalysisID AID = *I;
776 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
777 RP.push_back(AnalysisPass);
778 else
779 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000780 }
Devang Patelf58183d2006-12-12 23:09:32 +0000781
782 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
783 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
784 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000785 AnalysisID AID = *I;
786 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
787 RP.push_back(AnalysisPass);
788 else
789 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000790 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000791}
792
Devang Patel07f4f582006-11-14 21:49:36 +0000793// All Required analyses should be available to the pass as it runs! Here
794// we fill in the AnalysisImpls member of the pass so that it can
795// successfully use the getAnalysis() method to retrieve the
796// implementations it needs.
797//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000798void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000799 AnalysisUsage AnUsage;
800 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000801
802 for (std::vector<const PassInfo *>::const_iterator
803 I = AnUsage.getRequiredSet().begin(),
804 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000805 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000806 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000807 // This may be analysis pass that is initialized on the fly.
808 // If that is not the case then it will raise an assert when it is used.
809 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000810 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000811 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000812 }
813}
814
Devang Patel640c5bb2006-12-08 22:30:11 +0000815/// Find the pass that implements Analysis AID. If desired pass is not found
816/// then return NULL.
817Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
818
819 // Check if AvailableAnalysis map has one entry.
820 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
821
822 if (I != AvailableAnalysis.end())
823 return I->second;
824
825 // Search Parents through TopLevelManager
826 if (SearchParent)
827 return TPM->findAnalysisPass(AID);
828
Devang Patel9d759b82006-12-09 00:09:12 +0000829 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000830}
831
Devang Patel991aeba2006-12-15 20:13:01 +0000832// Print list of passes that are last used by P.
833void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
834
Devang Patel8adae862007-07-20 18:04:54 +0000835 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000836
837 // If this is a on the fly manager then it does not have TPM.
838 if (!TPM)
839 return;
840
Devang Patel991aeba2006-12-15 20:13:01 +0000841 TPM->collectLastUses(LUses, P);
842
Devang Patel8adae862007-07-20 18:04:54 +0000843 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000844 E = LUses.end(); I != E; ++I) {
845 llvm::cerr << "--" << std::string(Offset*2, ' ');
846 (*I)->dumpPassStructure(0);
847 }
848}
849
850void PMDataManager::dumpPassArguments() const {
851 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
852 E = PassVector.end(); I != E; ++I) {
853 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
854 PMD->dumpPassArguments();
855 else
856 if (const PassInfo *PI = (*I)->getPassInfo())
857 if (!PI->isAnalysisGroup())
858 cerr << " -" << PI->getPassArgument();
859 }
860}
861
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000862void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
863 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000864 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000865 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000866 return;
867 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000868 switch (S1) {
869 case EXECUTION_MSG:
870 cerr << "Executing Pass '" << P->getPassName();
871 break;
872 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000873 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000874 break;
875 case FREEING_MSG:
876 cerr << " Freeing Pass '" << P->getPassName();
877 break;
878 default:
879 break;
880 }
881 switch (S2) {
882 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000883 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000884 break;
885 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000886 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000887 break;
888 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000889 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000890 break;
891 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000892 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000893 break;
894 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000895 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000896 break;
897 default:
898 break;
899 }
Devang Patel991aeba2006-12-15 20:13:01 +0000900}
901
902void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
903 const std::vector<AnalysisID> &Set)
904 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000905 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000906 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
907 for (unsigned i = 0; i != Set.size(); ++i) {
908 if (i) cerr << ",";
909 cerr << " " << Set[i]->getPassName();
910 }
911 cerr << "\n";
912 }
913}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000914
Devang Patel004937b2007-07-27 20:06:09 +0000915/// Add RequiredPass into list of lower level passes required by pass P.
916/// RequiredPass is run on the fly by Pass Manager when P requests it
917/// through getAnalysis interface.
918/// This should be handled by specific pass manager.
919void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
920 if (TPM) {
921 TPM->dumpArguments();
922 TPM->dumpPasses();
923 }
924 assert (0 && "Unable to handle Pass that requires lower level Analysis pass");
925}
926
Devang Patele7599552007-01-12 18:52:44 +0000927// Destructor
928PMDataManager::~PMDataManager() {
929
930 for (std::vector<Pass *>::iterator I = PassVector.begin(),
931 E = PassVector.end(); I != E; ++I)
932 delete *I;
933
934 PassVector.clear();
935}
936
Devang Patel9bdf7d42006-12-08 23:28:54 +0000937//===----------------------------------------------------------------------===//
938// NOTE: Is this the right place to define this method ?
939// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000940Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000941 return PM.findAnalysisPass(ID, dir);
942}
943
Devang Patel92942812007-04-16 20:56:24 +0000944Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
945 Function &F) {
946 return PM.getOnTheFlyPass(P, AnalysisPI, F);
947}
948
Devang Patela1514cb2006-12-07 19:39:39 +0000949//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000950// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000951
Devang Patel6e5a1132006-11-07 21:31:57 +0000952/// Execute all of the passes scheduled for execution by invoking
953/// runOnBasicBlock method. Keep track of whether any of the passes modifies
954/// the function, and if so, return true.
955bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000956BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000957
Reid Spencer5301e7c2007-01-30 20:08:39 +0000958 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000959 return false;
960
Devang Patele9585592006-12-08 01:38:28 +0000961 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000962
Devang Patel6e5a1132006-11-07 21:31:57 +0000963 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000964 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
965 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000966 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000967 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000968
Devang Pateld305c402007-08-10 18:29:32 +0000969 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000970 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000971
Devang Patelabfbe3b2006-12-16 00:56:26 +0000972 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000973
Devang Patelabfbe3b2006-12-16 00:56:26 +0000974 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000975 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000976 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000977
Devang Patel003a5592007-03-05 20:01:30 +0000978 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +0000979 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
980 I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000981 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000982
Devang Patela273d1c2007-07-19 18:02:32 +0000983 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000984 removeNotPreservedAnalysis(BP);
985 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +0000986 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +0000987 }
Chris Lattnerde2aa652007-08-10 06:22:25 +0000988
Devang Patel56d48ec2006-12-15 22:57:49 +0000989 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000990}
991
Devang Patel475c4532006-12-08 00:59:05 +0000992// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000993inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000994 bool Changed = false;
995
Devang Patelabfbe3b2006-12-16 00:56:26 +0000996 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
997 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000998 Changed |= BP->doInitialization(M);
999 }
1000
1001 return Changed;
1002}
1003
Devang Patel67d6a5e2006-12-19 19:46:59 +00001004inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001005 bool Changed = false;
1006
Devang Patelabfbe3b2006-12-16 00:56:26 +00001007 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1008 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001009 Changed |= BP->doFinalization(M);
1010 }
1011
1012 return Changed;
1013}
1014
Devang Patel67d6a5e2006-12-19 19:46:59 +00001015inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001016 bool Changed = false;
1017
Devang Patelabfbe3b2006-12-16 00:56:26 +00001018 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1019 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001020 Changed |= BP->doInitialization(F);
1021 }
1022
1023 return Changed;
1024}
1025
Devang Patel67d6a5e2006-12-19 19:46:59 +00001026inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001027 bool Changed = false;
1028
Devang Patelabfbe3b2006-12-16 00:56:26 +00001029 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1030 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001031 Changed |= BP->doFinalization(F);
1032 }
1033
1034 return Changed;
1035}
1036
1037
Devang Patela1514cb2006-12-07 19:39:39 +00001038//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001039// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001040
Devang Patel4e12f862006-11-08 10:44:40 +00001041/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001042FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001043 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001044 // FPM is the top level manager.
1045 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001046
1047 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +00001048 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +00001049 FPM->setResolver(AR);
1050
Devang Patel1f653682006-12-08 18:57:16 +00001051 MP = P;
1052}
1053
Devang Patelb67904d2006-12-13 02:36:01 +00001054FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001055 delete FPM;
1056}
1057
Devang Patel4e12f862006-11-08 10:44:40 +00001058/// add - Add a pass to the queue of passes to run. This passes
1059/// ownership of the Pass to the PassManager. When the
1060/// PassManager_X is destroyed, the pass will be destroyed as well, so
1061/// there is no need to delete the pass. (TODO delete passes.)
1062/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001063void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001064 FPM->add(P);
1065}
1066
Devang Patel9f3083e2006-11-15 19:39:54 +00001067/// run - Execute all of the passes scheduled for execution. Keep
1068/// track of whether any of the passes modifies the function, and if
1069/// so, return true.
1070///
Devang Patelb67904d2006-12-13 02:36:01 +00001071bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001072 std::string errstr;
1073 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001074 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001075 abort();
1076 }
Devang Patel272908d2006-12-08 22:57:48 +00001077 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001078}
1079
1080
Devang Patelff631ae2006-11-15 01:27:05 +00001081/// doInitialization - Run all of the initializers for the function passes.
1082///
Devang Patelb67904d2006-12-13 02:36:01 +00001083bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001084 return FPM->doInitialization(*MP->getModule());
1085}
1086
Dan Gohmane6656eb2007-07-30 14:51:13 +00001087/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001088///
Devang Patelb67904d2006-12-13 02:36:01 +00001089bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001090 return FPM->doFinalization(*MP->getModule());
1091}
1092
Devang Patela1514cb2006-12-07 19:39:39 +00001093//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001094// FunctionPassManagerImpl implementation
1095//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001096inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1097 bool Changed = false;
1098
1099 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1100 FPPassManager *FP = getContainedManager(Index);
1101 Changed |= FP->doInitialization(M);
1102 }
1103
1104 return Changed;
1105}
1106
1107inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1108 bool Changed = false;
1109
1110 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1111 FPPassManager *FP = getContainedManager(Index);
1112 Changed |= FP->doFinalization(M);
1113 }
1114
1115 return Changed;
1116}
1117
1118// Execute all the passes managed by this top level manager.
1119// Return true if any function is modified by a pass.
1120bool FunctionPassManagerImpl::run(Function &F) {
1121
1122 bool Changed = false;
1123
1124 TimingInfo::createTheTimeInfo();
1125
1126 dumpArguments();
1127 dumpPasses();
1128
Devang Patele3068402006-12-21 00:16:50 +00001129 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001130 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1131 FPPassManager *FP = getContainedManager(Index);
1132 Changed |= FP->runOnFunction(F);
1133 }
1134 return Changed;
1135}
1136
1137//===----------------------------------------------------------------------===//
1138// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001139
Devang Patel8c78a0b2007-05-03 01:11:54 +00001140char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001141/// Print passes managed by this manager
1142void FPPassManager::dumpPassStructure(unsigned Offset) {
1143 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1144 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1145 FunctionPass *FP = getContainedPass(Index);
1146 FP->dumpPassStructure(Offset + 1);
1147 dumpLastUses(FP, Offset+1);
1148 }
1149}
1150
1151
Devang Patel0c2012f2006-11-07 21:49:50 +00001152/// Execute all of the passes scheduled for execution by invoking
1153/// runOnFunction method. Keep track of whether any of the passes modifies
1154/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001155bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001156
1157 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001158
Reid Spencer5301e7c2007-01-30 20:08:39 +00001159 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001160 return false;
1161
Devang Patelabfbe3b2006-12-16 00:56:26 +00001162 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1163 FunctionPass *FP = getContainedPass(Index);
1164
Devang Patelf6d1d212006-12-14 00:25:06 +00001165 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001166 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001167
Devang Pateld305c402007-08-10 18:29:32 +00001168 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001169 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001170
Devang Patelabfbe3b2006-12-16 00:56:26 +00001171 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001172
Devang Patelabfbe3b2006-12-16 00:56:26 +00001173 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001174 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001175 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001176
Devang Patel003a5592007-03-05 20:01:30 +00001177 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001178 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001179 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001180
Devang Patela273d1c2007-07-19 18:02:32 +00001181 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001182 removeNotPreservedAnalysis(FP);
1183 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001184 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001185 }
1186 return Changed;
1187}
1188
Devang Patel67d6a5e2006-12-19 19:46:59 +00001189bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001190
Devang Patel67d6a5e2006-12-19 19:46:59 +00001191 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001192
1193 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1194 this->runOnFunction(*I);
1195
1196 return Changed |= doFinalization(M);
1197}
1198
1199inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001200 bool Changed = false;
1201
Devang Patelabfbe3b2006-12-16 00:56:26 +00001202 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1203 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001204 Changed |= FP->doInitialization(M);
1205 }
1206
1207 return Changed;
1208}
1209
Devang Patel67d6a5e2006-12-19 19:46:59 +00001210inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001211 bool Changed = false;
1212
Devang Patelabfbe3b2006-12-16 00:56:26 +00001213 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1214 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001215 Changed |= FP->doFinalization(M);
1216 }
1217
Devang Patelff631ae2006-11-15 01:27:05 +00001218 return Changed;
1219}
1220
Devang Patela1514cb2006-12-07 19:39:39 +00001221//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001222// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001223
Devang Patel05e1a972006-11-07 22:03:15 +00001224/// Execute all of the passes scheduled for execution by invoking
1225/// runOnModule method. Keep track of whether any of the passes modifies
1226/// the module, and if so, return true.
1227bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001228MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001229 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001230
Devang Patelabfbe3b2006-12-16 00:56:26 +00001231 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1232 ModulePass *MP = getContainedPass(Index);
1233
Devang Patelf6d1d212006-12-14 00:25:06 +00001234 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001235 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001236
Dan Gohman929391a2008-01-29 12:09:55 +00001237 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1238 M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001239 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001240
Devang Patelabfbe3b2006-12-16 00:56:26 +00001241 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001242
Devang Patelabfbe3b2006-12-16 00:56:26 +00001243 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001244 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001245 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001246
Devang Patel003a5592007-03-05 20:01:30 +00001247 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001248 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1249 M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001250 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001251
Devang Patela273d1c2007-07-19 18:02:32 +00001252 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001253 removeNotPreservedAnalysis(MP);
1254 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001255 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001256 }
1257 return Changed;
1258}
1259
Devang Patele64d3052007-04-16 20:12:57 +00001260/// Add RequiredPass into list of lower level passes required by pass P.
1261/// RequiredPass is run on the fly by Pass Manager when P requests it
1262/// through getAnalysis interface.
1263void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1264
1265 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1266 && "Unable to handle Pass that requires lower level Analysis pass");
1267 assert ((P->getPotentialPassManagerType() <
1268 RequiredPass->getPotentialPassManagerType())
1269 && "Unable to handle Pass that requires lower level Analysis pass");
1270
Devang Patel68f72b12007-04-26 17:50:19 +00001271 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001272 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001273 FPP = new FunctionPassManagerImpl(0);
1274 // FPP is the top level manager.
1275 FPP->setTopLevelManager(FPP);
1276
Devang Patel69e9f6d2007-04-16 20:27:05 +00001277 OnTheFlyManagers[P] = FPP;
1278 }
Devang Patel68f72b12007-04-26 17:50:19 +00001279 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001280
Devang Patel68f72b12007-04-26 17:50:19 +00001281 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001282 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001283 LU.push_back(RequiredPass);
1284 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001285}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001286
1287/// Return function pass corresponding to PassInfo PI, that is
1288/// required by module pass MP. Instantiate analysis pass, by using
1289/// its runOnFunction() for function F.
1290Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1291 Function &F) {
1292 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001293 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001294 assert (FPP && "Unable to find on the fly pass");
1295
Devang Patel68f72b12007-04-26 17:50:19 +00001296 FPP->run(F);
1297 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001298}
1299
1300
Devang Patela1514cb2006-12-07 19:39:39 +00001301//===----------------------------------------------------------------------===//
1302// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001303//
Devang Patelc290c8a2006-11-07 22:23:34 +00001304/// run - Execute all of the passes scheduled for execution. Keep track of
1305/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001306bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001307
Devang Patelc290c8a2006-11-07 22:23:34 +00001308 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001309
Devang Patelb8817b92006-12-14 00:59:42 +00001310 TimingInfo::createTheTimeInfo();
1311
Devang Patelcfd70c42006-12-13 22:10:00 +00001312 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001313 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001314
Devang Patele3068402006-12-21 00:16:50 +00001315 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001316 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1317 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001318 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001319 }
1320 return Changed;
1321}
Devang Patel376fefa2006-11-08 10:29:57 +00001322
Devang Patela1514cb2006-12-07 19:39:39 +00001323//===----------------------------------------------------------------------===//
1324// PassManager implementation
1325
Devang Patel376fefa2006-11-08 10:29:57 +00001326/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001327PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001328 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001329 // PM is the top level manager
1330 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001331}
1332
Devang Patelb67904d2006-12-13 02:36:01 +00001333PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001334 delete PM;
1335}
1336
Devang Patel376fefa2006-11-08 10:29:57 +00001337/// add - Add a pass to the queue of passes to run. This passes ownership of
1338/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1339/// will be destroyed as well, so there is no need to delete the pass. This
1340/// implies that all passes MUST be allocated with 'new'.
1341void
Devang Patelb67904d2006-12-13 02:36:01 +00001342PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001343 PM->add(P);
1344}
1345
1346/// run - Execute all of the passes scheduled for execution. Keep track of
1347/// whether any of the passes modifies the module, and if so, return true.
1348bool
Devang Patelb67904d2006-12-13 02:36:01 +00001349PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001350 return PM->run(M);
1351}
1352
Devang Patelb8817b92006-12-14 00:59:42 +00001353//===----------------------------------------------------------------------===//
1354// TimingInfo Class - This class is used to calculate information about the
1355// amount of time each pass takes to execute. This only happens with
1356// -time-passes is enabled on the command line.
1357//
1358bool llvm::TimePassesIsEnabled = false;
1359static cl::opt<bool,true>
1360EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1361 cl::desc("Time each pass, printing elapsed time for each on exit"));
1362
1363// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1364// a non null value (if the -time-passes option is enabled) or it leaves it
1365// null. It may be called multiple times.
1366void TimingInfo::createTheTimeInfo() {
1367 if (!TimePassesIsEnabled || TheTimeInfo) return;
1368
1369 // Constructed the first time this is called, iff -time-passes is enabled.
1370 // This guarantees that the object will be constructed before static globals,
1371 // thus it will be destroyed before them.
1372 static ManagedStatic<TimingInfo> TTI;
1373 TheTimeInfo = &*TTI;
1374}
1375
Devang Patel1c3633e2007-01-29 23:10:37 +00001376/// If TimingInfo is enabled then start pass timer.
1377void StartPassTimer(Pass *P) {
1378 if (TheTimeInfo)
1379 TheTimeInfo->passStarted(P);
1380}
1381
1382/// If TimingInfo is enabled then stop pass timer.
1383void StopPassTimer(Pass *P) {
1384 if (TheTimeInfo)
1385 TheTimeInfo->passEnded(P);
1386}
1387
Devang Patel1c56a632007-01-08 19:29:38 +00001388//===----------------------------------------------------------------------===//
1389// PMStack implementation
1390//
Devang Patelad98d232007-01-11 22:15:30 +00001391
Devang Patel1c56a632007-01-08 19:29:38 +00001392// Pop Pass Manager from the stack and clear its analysis info.
1393void PMStack::pop() {
1394
1395 PMDataManager *Top = this->top();
1396 Top->initializeAnalysisInfo();
1397
1398 S.pop_back();
1399}
1400
1401// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001402void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001403
Devang Patel15701b52007-01-11 00:19:00 +00001404 PMDataManager *Top = NULL;
1405 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1406 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001407
Devang Patel15701b52007-01-11 00:19:00 +00001408 if (this->empty()) {
1409 Top = PM;
1410 }
1411 else {
1412 Top = this->top();
1413 PMTopLevelManager *TPM = Top->getTopLevelManager();
1414
1415 assert (TPM && "Unable to find top level manager");
1416 TPM->addIndirectPassManager(PM);
1417 PM->setTopLevelManager(TPM);
1418 }
1419
Devang Patel15701b52007-01-11 00:19:00 +00001420 S.push_back(PM);
1421}
1422
1423// Dump content of the pass manager stack.
1424void PMStack::dump() {
1425 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1426 E = S.end(); I != E; ++I) {
1427 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001428 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001429 }
1430 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001431 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001432}
1433
Devang Patel1c56a632007-01-08 19:29:38 +00001434/// Find appropriate Module Pass Manager in the PM Stack and
1435/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001436void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001437 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001438
Devang Patel1c56a632007-01-08 19:29:38 +00001439 // Find Module Pass Manager
1440 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001441 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1442 if (TopPMType == PreferredType)
1443 break; // We found desired pass manager
1444 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001445 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001446 else
1447 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001448 }
1449
Devang Patel23f8aa92007-01-17 21:19:23 +00001450 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001451}
1452
Devang Patel3312f752007-01-16 21:43:18 +00001453/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1454/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001455void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001456 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001457
Devang Patel15701b52007-01-11 00:19:00 +00001458 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001459 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001460 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1461 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001462 else
Devang Patel3312f752007-01-16 21:43:18 +00001463 break;
1464 }
1465 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1466
1467 // Create new Function Pass Manager
1468 if (!FPP) {
1469 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1470 PMDataManager *PMD = PMS.top();
1471
1472 // [1] Create new Function Pass Manager
1473 FPP = new FPPassManager(PMD->getDepth() + 1);
1474
1475 // [2] Set up new manager's top level manager
1476 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1477 TPM->addIndirectPassManager(FPP);
1478
1479 // [3] Assign manager to manage this new manager. This may create
1480 // and push new managers into PMS
1481 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001482
1483 // If Call Graph Pass Manager is active then use it to manage
1484 // this new Function Pass manager.
1485 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1486 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1487 else
1488 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001489
1490 // [4] Push new manager into PMS
1491 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001492 }
1493
Devang Patel3312f752007-01-16 21:43:18 +00001494 // Assign FPP as the manager of this pass.
1495 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001496}
1497
Devang Patel3312f752007-01-16 21:43:18 +00001498/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001499/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001500void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001501 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001502
1503 BBPassManager *BBP = NULL;
1504
Devang Patel15701b52007-01-11 00:19:00 +00001505 // Basic Pass Manager is a leaf pass manager. It does not handle
1506 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001507 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001508 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001509
Devang Patel3312f752007-01-16 21:43:18 +00001510 // If leaf manager is not Basic Block Pass manager then create new
1511 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001512
Devang Patel3312f752007-01-16 21:43:18 +00001513 if (!BBP) {
1514 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1515 PMDataManager *PMD = PMS.top();
1516
1517 // [1] Create new Basic Block Manager
1518 BBP = new BBPassManager(PMD->getDepth() + 1);
1519
1520 // [2] Set up new manager's top level manager
1521 // Basic Block Pass Manager does not live by itself
1522 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1523 TPM->addIndirectPassManager(BBP);
1524
Devang Patel15701b52007-01-11 00:19:00 +00001525 // [3] Assign manager to manage this new manager. This may create
1526 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001527 Pass *P = dynamic_cast<Pass *>(BBP);
1528 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001529
Devang Patel3312f752007-01-16 21:43:18 +00001530 // [4] Push new manager into PMS
1531 PMS.push(BBP);
1532 }
Devang Patel1c56a632007-01-08 19:29:38 +00001533
Devang Patel3312f752007-01-16 21:43:18 +00001534 // Assign BBP as the manager of this pass.
1535 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001536}
1537
Devang Patelc6b5a552007-01-05 20:16:23 +00001538