blob: 5a85a16d50309795db4b8f26f5ecfe535aab4841 [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 {
Dan Gohman1e9860a2008-03-13 01:58:48 +000086 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +000087 }
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.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000182/// It batches all Module passes and function pass managers together and
183/// sequences 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
Dan Gohman73caf5f2008-03-13 01:48:32 +0000458 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000459 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000460 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000461 P = PMD->findAnalysisPass(AID, false);
462 }
463
464 // Check other pass managers
465 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
466 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
467 P = (*I)->findAnalysisPass(AID, false);
468
Devang Patelafb1f3622006-12-12 22:35:25 +0000469 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
470 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
471 const PassInfo *PI = (*I)->getPassInfo();
472 if (PI == AID)
473 P = *I;
474
475 // If Pass not found then check the interfaces implemented by Immutable Pass
476 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000477 const std::vector<const PassInfo*> &ImmPI =
478 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000479 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
480 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000481 }
482 }
483
Devang Patelafb1f3622006-12-12 22:35:25 +0000484 return P;
485}
486
Devang Pateleda56172006-12-12 23:34:33 +0000487// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000488void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000489
Devang Patelfd4184322007-01-17 20:33:36 +0000490 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000491 return;
492
Devang Pateleda56172006-12-12 23:34:33 +0000493 // Print out the immutable passes
494 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
495 ImmutablePasses[i]->dumpPassStructure(0);
496 }
497
Dan Gohman73caf5f2008-03-13 01:48:32 +0000498 // Every class that derives from PMDataManager also derives from Pass
499 // (sometimes indirectly), but there's no inheritance relationship
500 // between PMDataManager and Pass, so we have to dynamic_cast to get
501 // from a PMDataManager* to a Pass*.
502 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000503 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000504 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000505}
506
Devang Patel991aeba2006-12-15 20:13:01 +0000507void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000508
Devang Patelfd4184322007-01-17 20:33:36 +0000509 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000510 return;
511
512 cerr << "Pass Arguments: ";
Dan Gohman73caf5f2008-03-13 01:48:32 +0000513 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000514 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000515 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000516 PMD->dumpPassArguments();
517 }
518 cerr << "\n";
519}
520
Devang Patele3068402006-12-21 00:16:50 +0000521void PMTopLevelManager::initializeAllAnalysisInfo() {
522
Dan Gohman73caf5f2008-03-13 01:48:32 +0000523 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000524 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000525 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000526 PMD->initializeAnalysisInfo();
527 }
528
529 // Initailize other pass managers
530 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
531 E = IndirectPassManagers.end(); I != E; ++I)
532 (*I)->initializeAnalysisInfo();
533}
534
Devang Patele7599552007-01-12 18:52:44 +0000535/// Destructor
536PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000537 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000538 E = PassManagers.end(); I != E; ++I)
539 delete *I;
540
541 for (std::vector<ImmutablePass *>::iterator
542 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
543 delete *I;
Devang Patele7599552007-01-12 18:52:44 +0000544}
545
Devang Patelafb1f3622006-12-12 22:35:25 +0000546//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000547// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000548
Devang Patel643676c2006-11-11 01:10:19 +0000549/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000550void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000551
Devang Patel643676c2006-11-11 01:10:19 +0000552 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000553 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000554
Devang Patele9976aa2006-12-07 19:33:53 +0000555 //This pass is the current implementation of all of the interfaces it
556 //implements as well.
557 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
558 for (unsigned i = 0, e = II.size(); i != e; ++i)
559 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000560 }
561}
562
Devang Patel9d9fc902007-03-06 17:52:53 +0000563// Return true if P preserves high level analysis used by other
564// passes managed by this manager
565bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
566
567 AnalysisUsage AnUsage;
568 P->getAnalysisUsage(AnUsage);
569
570 if (AnUsage.getPreservesAll())
571 return true;
572
573 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
574 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
575 E = HigherLevelAnalysis.end(); I != E; ++I) {
576 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000577 if (!dynamic_cast<ImmutablePass*>(P1) &&
578 std::find(PreservedSet.begin(), PreservedSet.end(),
579 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000580 PreservedSet.end())
581 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000582 }
583
584 return true;
585}
586
Devang Patela273d1c2007-07-19 18:02:32 +0000587/// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
588void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000589 AnalysisUsage AnUsage;
590 P->getAnalysisUsage(AnUsage);
Devang Patelef432532007-07-19 05:36:09 +0000591 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000592
Devang Patelef432532007-07-19 05:36:09 +0000593 // Verify preserved analysis
Devang Patela273d1c2007-07-19 18:02:32 +0000594 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
595 E = PreservedSet.end(); I != E; ++I) {
596 AnalysisID AID = *I;
597 Pass *AP = findAnalysisPass(AID, true);
598 if (AP)
599 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000600 }
Devang Patela273d1c2007-07-19 18:02:32 +0000601}
602
603/// Remove Analyss not preserved by Pass P
604void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
605 AnalysisUsage AnUsage;
606 P->getAnalysisUsage(AnUsage);
Devang Patel2e169c32006-12-07 20:03:49 +0000607 if (AnUsage.getPreservesAll())
608 return;
609
Devang Patela273d1c2007-07-19 18:02:32 +0000610 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000611 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000612 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000613 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000614 if (!dynamic_cast<ImmutablePass*>(Info->second)
615 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
616 PreservedSet.end())
Devang Patel349170f2006-11-11 01:24:55 +0000617 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000618 AvailableAnalysis.erase(Info);
Devang Patel349170f2006-11-11 01:24:55 +0000619 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000620
621 // Check inherited analysis also. If P is not preserving analysis
622 // provided by parent manager then remove it here.
623 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
624
625 if (!InheritedAnalysis[Index])
626 continue;
627
628 for (std::map<AnalysisID, Pass*>::iterator
629 I = InheritedAnalysis[Index]->begin(),
630 E = InheritedAnalysis[Index]->end(); I != E; ) {
631 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000632 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
633 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000634 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000635 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000636 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000637 }
638 }
639
Devang Patelf68a3492006-11-07 22:35:17 +0000640}
641
Devang Patelca189262006-11-14 03:05:08 +0000642/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000643void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000644 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000645
Devang Patel8adae862007-07-20 18:04:54 +0000646 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000647
Devang Patel2ff44922007-04-16 20:39:59 +0000648 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000649 if (!TPM)
650 return;
651
Devang Patel17ad0962006-12-08 00:37:52 +0000652 TPM->collectLastUses(DeadPasses, P);
653
Devang Patel8adae862007-07-20 18:04:54 +0000654 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000655 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000656
Devang Patel003a5592007-03-05 20:01:30 +0000657 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000658
Devang Patel7ebf09d2007-03-05 18:20:51 +0000659 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000660 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000661 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000662
Devang Patel17ad0962006-12-08 00:37:52 +0000663 std::map<AnalysisID, Pass*>::iterator Pos =
664 AvailableAnalysis.find((*I)->getPassInfo());
665
Devang Patel475c4532006-12-08 00:59:05 +0000666 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000667 if (Pos != AvailableAnalysis.end())
668 AvailableAnalysis.erase(Pos);
669 }
Devang Patelca189262006-11-14 03:05:08 +0000670}
671
Devang Patel8f677ce2006-12-07 18:47:25 +0000672/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000673/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000674void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000675 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000676
Devang Pateld440cd92006-12-08 23:53:00 +0000677 // This manager is going to manage pass P. Set up analysis resolver
678 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000679 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000680 P->setResolver(AR);
681
Devang Patelec2b9a72007-03-05 22:57:49 +0000682 // If a FunctionPass F is the last user of ModulePass info M
683 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000684 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000685
Devang Patel90b05e02006-11-11 02:04:19 +0000686 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000687
688 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000689 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000690 SmallVector<Pass *, 8> RequiredPasses;
691 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
692
Devang Patelbc03f132006-12-07 23:55:10 +0000693 unsigned PDepth = this->getDepth();
694
Devang Patele64d3052007-04-16 20:12:57 +0000695 collectRequiredAnalysis(RequiredPasses,
696 ReqAnalysisNotAvailable, P);
697 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000698 E = RequiredPasses.end(); I != E; ++I) {
699 Pass *PRequired = *I;
700 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000701
702 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
703 RDepth = DM.getDepth();
704
Devang Patelbc03f132006-12-07 23:55:10 +0000705 if (PDepth == RDepth)
706 LastUses.push_back(PRequired);
707 else if (PDepth > RDepth) {
708 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000709 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000710 // Keep track of higher level analysis used by this manager.
711 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000712 } else
713 assert (0 && "Unable to accomodate Required Pass");
714 }
715
Devang Patel39786a92007-01-15 20:31:54 +0000716 // Set P as P's last user until someone starts using P.
717 // However, if P is a Pass Manager then it does not need
718 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000719 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000720 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000721 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000722
Devang Patelec2b9a72007-03-05 22:57:49 +0000723 if (!TransferLastUses.empty()) {
724 Pass *My_PM = dynamic_cast<Pass *>(this);
725 TPM->setLastUser(TransferLastUses, My_PM);
726 TransferLastUses.clear();
727 }
728
Devang Patel69e9f6d2007-04-16 20:27:05 +0000729 // Now, take care of required analysises that are not available.
730 for (SmallVector<AnalysisID, 8>::iterator
731 I = ReqAnalysisNotAvailable.begin(),
732 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
733 Pass *AnalysisPass = (*I)->createPass();
734 this->addLowerLevelRequiredPass(P, AnalysisPass);
735 }
736
Devang Patel17bff0d2006-12-07 22:09:36 +0000737 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000738 // Remove the analysis not preserved by this pass
739 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000740 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000741 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000742
743 // Add pass
744 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000745}
746
Devang Patele64d3052007-04-16 20:12:57 +0000747
748/// Populate RP with analysis pass that are required by
749/// pass P and are available. Populate RP_NotAvail with analysis
750/// pass that are required by pass P but are not available.
751void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
752 SmallVector<AnalysisID, 8> &RP_NotAvail,
753 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000754 AnalysisUsage AnUsage;
755 P->getAnalysisUsage(AnUsage);
756 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
757 for (std::vector<AnalysisID>::const_iterator
758 I = RequiredSet.begin(), E = RequiredSet.end();
759 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000760 AnalysisID AID = *I;
761 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
762 RP.push_back(AnalysisPass);
763 else
764 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000765 }
Devang Patelf58183d2006-12-12 23:09:32 +0000766
767 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
768 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
769 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000770 AnalysisID AID = *I;
771 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
772 RP.push_back(AnalysisPass);
773 else
774 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000775 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000776}
777
Devang Patel07f4f582006-11-14 21:49:36 +0000778// All Required analyses should be available to the pass as it runs! Here
779// we fill in the AnalysisImpls member of the pass so that it can
780// successfully use the getAnalysis() method to retrieve the
781// implementations it needs.
782//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000783void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000784 AnalysisUsage AnUsage;
785 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000786
787 for (std::vector<const PassInfo *>::const_iterator
788 I = AnUsage.getRequiredSet().begin(),
789 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000790 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000791 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000792 // This may be analysis pass that is initialized on the fly.
793 // If that is not the case then it will raise an assert when it is used.
794 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000795 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000796 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000797 }
798}
799
Devang Patel640c5bb2006-12-08 22:30:11 +0000800/// Find the pass that implements Analysis AID. If desired pass is not found
801/// then return NULL.
802Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
803
804 // Check if AvailableAnalysis map has one entry.
805 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
806
807 if (I != AvailableAnalysis.end())
808 return I->second;
809
810 // Search Parents through TopLevelManager
811 if (SearchParent)
812 return TPM->findAnalysisPass(AID);
813
Devang Patel9d759b82006-12-09 00:09:12 +0000814 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000815}
816
Devang Patel991aeba2006-12-15 20:13:01 +0000817// Print list of passes that are last used by P.
818void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
819
Devang Patel8adae862007-07-20 18:04:54 +0000820 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000821
822 // If this is a on the fly manager then it does not have TPM.
823 if (!TPM)
824 return;
825
Devang Patel991aeba2006-12-15 20:13:01 +0000826 TPM->collectLastUses(LUses, P);
827
Devang Patel8adae862007-07-20 18:04:54 +0000828 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000829 E = LUses.end(); I != E; ++I) {
830 llvm::cerr << "--" << std::string(Offset*2, ' ');
831 (*I)->dumpPassStructure(0);
832 }
833}
834
835void PMDataManager::dumpPassArguments() const {
836 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
837 E = PassVector.end(); I != E; ++I) {
838 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
839 PMD->dumpPassArguments();
840 else
841 if (const PassInfo *PI = (*I)->getPassInfo())
842 if (!PI->isAnalysisGroup())
843 cerr << " -" << PI->getPassArgument();
844 }
845}
846
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000847void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
848 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000849 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000850 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000851 return;
852 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000853 switch (S1) {
854 case EXECUTION_MSG:
855 cerr << "Executing Pass '" << P->getPassName();
856 break;
857 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000858 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000859 break;
860 case FREEING_MSG:
861 cerr << " Freeing Pass '" << P->getPassName();
862 break;
863 default:
864 break;
865 }
866 switch (S2) {
867 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000868 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000869 break;
870 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000871 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000872 break;
873 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000874 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000875 break;
876 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000877 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000878 break;
879 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000880 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000881 break;
882 default:
883 break;
884 }
Devang Patel991aeba2006-12-15 20:13:01 +0000885}
886
887void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
888 const std::vector<AnalysisID> &Set)
889 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000890 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000891 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
892 for (unsigned i = 0; i != Set.size(); ++i) {
893 if (i) cerr << ",";
894 cerr << " " << Set[i]->getPassName();
895 }
896 cerr << "\n";
897 }
898}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000899
Devang Patel004937b2007-07-27 20:06:09 +0000900/// Add RequiredPass into list of lower level passes required by pass P.
901/// RequiredPass is run on the fly by Pass Manager when P requests it
902/// through getAnalysis interface.
903/// This should be handled by specific pass manager.
904void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
905 if (TPM) {
906 TPM->dumpArguments();
907 TPM->dumpPasses();
908 }
Devang Patel8df7cc12008-02-02 01:43:30 +0000909
910 // Module Level pass may required Function Level analysis info
911 // (e.g. dominator info). Pass manager uses on the fly function pass manager
912 // to provide this on demand. In that case, in Pass manager terminology,
913 // module level pass is requiring lower level analysis info managed by
914 // lower level pass manager.
915
916 // When Pass manager is not able to order required analysis info, Pass manager
917 // checks whether any lower level manager will be able to provide this
918 // analysis info on demand or not.
Devang Patel004937b2007-07-27 20:06:09 +0000919 assert (0 && "Unable to handle Pass that requires lower level Analysis pass");
920}
921
Devang Patele7599552007-01-12 18:52:44 +0000922// Destructor
923PMDataManager::~PMDataManager() {
924
925 for (std::vector<Pass *>::iterator I = PassVector.begin(),
926 E = PassVector.end(); I != E; ++I)
927 delete *I;
928
Devang Patele7599552007-01-12 18:52:44 +0000929}
930
Devang Patel9bdf7d42006-12-08 23:28:54 +0000931//===----------------------------------------------------------------------===//
932// NOTE: Is this the right place to define this method ?
933// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000934Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000935 return PM.findAnalysisPass(ID, dir);
936}
937
Devang Patel92942812007-04-16 20:56:24 +0000938Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
939 Function &F) {
940 return PM.getOnTheFlyPass(P, AnalysisPI, F);
941}
942
Devang Patela1514cb2006-12-07 19:39:39 +0000943//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000944// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000945
Devang Patel6e5a1132006-11-07 21:31:57 +0000946/// Execute all of the passes scheduled for execution by invoking
947/// runOnBasicBlock method. Keep track of whether any of the passes modifies
948/// the function, and if so, return true.
949bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000950BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000951
Reid Spencer5301e7c2007-01-30 20:08:39 +0000952 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000953 return false;
954
Devang Patele9585592006-12-08 01:38:28 +0000955 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000956
Devang Patel6e5a1132006-11-07 21:31:57 +0000957 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000958 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
959 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000960 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000961 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000962
Devang Pateld305c402007-08-10 18:29:32 +0000963 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000964 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000965
Devang Patelabfbe3b2006-12-16 00:56:26 +0000966 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000967
Devang Patelabfbe3b2006-12-16 00:56:26 +0000968 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000969 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000970 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000971
Devang Patel003a5592007-03-05 20:01:30 +0000972 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +0000973 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
974 I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000975 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000976
Devang Patela273d1c2007-07-19 18:02:32 +0000977 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000978 removeNotPreservedAnalysis(BP);
979 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +0000980 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +0000981 }
Chris Lattnerde2aa652007-08-10 06:22:25 +0000982
Devang Patel56d48ec2006-12-15 22:57:49 +0000983 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000984}
985
Devang Patel475c4532006-12-08 00:59:05 +0000986// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000987inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000988 bool Changed = false;
989
Devang Patelabfbe3b2006-12-16 00:56:26 +0000990 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
991 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000992 Changed |= BP->doInitialization(M);
993 }
994
995 return Changed;
996}
997
Devang Patel67d6a5e2006-12-19 19:46:59 +0000998inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000999 bool Changed = false;
1000
Devang Patelabfbe3b2006-12-16 00:56:26 +00001001 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1002 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001003 Changed |= BP->doFinalization(M);
1004 }
1005
1006 return Changed;
1007}
1008
Devang Patel67d6a5e2006-12-19 19:46:59 +00001009inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001010 bool Changed = false;
1011
Devang Patelabfbe3b2006-12-16 00:56:26 +00001012 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1013 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001014 Changed |= BP->doInitialization(F);
1015 }
1016
1017 return Changed;
1018}
1019
Devang Patel67d6a5e2006-12-19 19:46:59 +00001020inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001021 bool Changed = false;
1022
Devang Patelabfbe3b2006-12-16 00:56:26 +00001023 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1024 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001025 Changed |= BP->doFinalization(F);
1026 }
1027
1028 return Changed;
1029}
1030
1031
Devang Patela1514cb2006-12-07 19:39:39 +00001032//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001033// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001034
Devang Patel4e12f862006-11-08 10:44:40 +00001035/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001036FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001037 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001038 // FPM is the top level manager.
1039 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001040
Dan Gohman565df952008-03-13 02:08:36 +00001041 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001042 FPM->setResolver(AR);
1043
Devang Patel1f653682006-12-08 18:57:16 +00001044 MP = P;
1045}
1046
Devang Patelb67904d2006-12-13 02:36:01 +00001047FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001048 delete FPM;
1049}
1050
Devang Patel4e12f862006-11-08 10:44:40 +00001051/// add - Add a pass to the queue of passes to run. This passes
1052/// ownership of the Pass to the PassManager. When the
1053/// PassManager_X is destroyed, the pass will be destroyed as well, so
1054/// there is no need to delete the pass. (TODO delete passes.)
1055/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001056void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001057 FPM->add(P);
1058}
1059
Devang Patel9f3083e2006-11-15 19:39:54 +00001060/// run - Execute all of the passes scheduled for execution. Keep
1061/// track of whether any of the passes modifies the function, and if
1062/// so, return true.
1063///
Devang Patelb67904d2006-12-13 02:36:01 +00001064bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001065 std::string errstr;
1066 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001067 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001068 abort();
1069 }
Devang Patel272908d2006-12-08 22:57:48 +00001070 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001071}
1072
1073
Devang Patelff631ae2006-11-15 01:27:05 +00001074/// doInitialization - Run all of the initializers for the function passes.
1075///
Devang Patelb67904d2006-12-13 02:36:01 +00001076bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001077 return FPM->doInitialization(*MP->getModule());
1078}
1079
Dan Gohmane6656eb2007-07-30 14:51:13 +00001080/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001081///
Devang Patelb67904d2006-12-13 02:36:01 +00001082bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001083 return FPM->doFinalization(*MP->getModule());
1084}
1085
Devang Patela1514cb2006-12-07 19:39:39 +00001086//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001087// FunctionPassManagerImpl implementation
1088//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001089inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1090 bool Changed = false;
1091
1092 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1093 FPPassManager *FP = getContainedManager(Index);
1094 Changed |= FP->doInitialization(M);
1095 }
1096
1097 return Changed;
1098}
1099
1100inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1101 bool Changed = false;
1102
1103 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1104 FPPassManager *FP = getContainedManager(Index);
1105 Changed |= FP->doFinalization(M);
1106 }
1107
1108 return Changed;
1109}
1110
1111// Execute all the passes managed by this top level manager.
1112// Return true if any function is modified by a pass.
1113bool FunctionPassManagerImpl::run(Function &F) {
1114
1115 bool Changed = false;
1116
1117 TimingInfo::createTheTimeInfo();
1118
1119 dumpArguments();
1120 dumpPasses();
1121
Devang Patele3068402006-12-21 00:16:50 +00001122 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001123 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1124 FPPassManager *FP = getContainedManager(Index);
1125 Changed |= FP->runOnFunction(F);
1126 }
1127 return Changed;
1128}
1129
1130//===----------------------------------------------------------------------===//
1131// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001132
Devang Patel8c78a0b2007-05-03 01:11:54 +00001133char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001134/// Print passes managed by this manager
1135void FPPassManager::dumpPassStructure(unsigned Offset) {
1136 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1137 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1138 FunctionPass *FP = getContainedPass(Index);
1139 FP->dumpPassStructure(Offset + 1);
1140 dumpLastUses(FP, Offset+1);
1141 }
1142}
1143
1144
Devang Patel0c2012f2006-11-07 21:49:50 +00001145/// Execute all of the passes scheduled for execution by invoking
1146/// runOnFunction method. Keep track of whether any of the passes modifies
1147/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001148bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001149
1150 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001151
Reid Spencer5301e7c2007-01-30 20:08:39 +00001152 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001153 return false;
1154
Devang Patelabfbe3b2006-12-16 00:56:26 +00001155 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1156 FunctionPass *FP = getContainedPass(Index);
1157
Devang Patelf6d1d212006-12-14 00:25:06 +00001158 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001159 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001160
Devang Pateld305c402007-08-10 18:29:32 +00001161 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001162 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001163
Devang Patelabfbe3b2006-12-16 00:56:26 +00001164 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001165
Devang Patelabfbe3b2006-12-16 00:56:26 +00001166 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001167 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001168 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001169
Devang Patel003a5592007-03-05 20:01:30 +00001170 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001171 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001172 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001173
Devang Patela273d1c2007-07-19 18:02:32 +00001174 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001175 removeNotPreservedAnalysis(FP);
1176 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001177 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001178 }
1179 return Changed;
1180}
1181
Devang Patel67d6a5e2006-12-19 19:46:59 +00001182bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001183
Devang Patel67d6a5e2006-12-19 19:46:59 +00001184 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001185
1186 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1187 this->runOnFunction(*I);
1188
1189 return Changed |= doFinalization(M);
1190}
1191
1192inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001193 bool Changed = false;
1194
Devang Patelabfbe3b2006-12-16 00:56:26 +00001195 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1196 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001197 Changed |= FP->doInitialization(M);
1198 }
1199
1200 return Changed;
1201}
1202
Devang Patel67d6a5e2006-12-19 19:46:59 +00001203inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001204 bool Changed = false;
1205
Devang Patelabfbe3b2006-12-16 00:56:26 +00001206 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1207 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001208 Changed |= FP->doFinalization(M);
1209 }
1210
Devang Patelff631ae2006-11-15 01:27:05 +00001211 return Changed;
1212}
1213
Devang Patela1514cb2006-12-07 19:39:39 +00001214//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001215// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001216
Devang Patel05e1a972006-11-07 22:03:15 +00001217/// Execute all of the passes scheduled for execution by invoking
1218/// runOnModule method. Keep track of whether any of the passes modifies
1219/// the module, and if so, return true.
1220bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001221MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001222 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001223
Devang Patelabfbe3b2006-12-16 00:56:26 +00001224 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1225 ModulePass *MP = getContainedPass(Index);
1226
Devang Patelf6d1d212006-12-14 00:25:06 +00001227 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001228 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001229
Dan Gohman929391a2008-01-29 12:09:55 +00001230 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1231 M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001232 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001233
Devang Patelabfbe3b2006-12-16 00:56:26 +00001234 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001235
Devang Patelabfbe3b2006-12-16 00:56:26 +00001236 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001237 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001238 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001239
Devang Patel003a5592007-03-05 20:01:30 +00001240 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001241 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1242 M.getModuleIdentifier().c_str());
Devang Patelabfbe3b2006-12-16 00:56:26 +00001243 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001244
Devang Patela273d1c2007-07-19 18:02:32 +00001245 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001246 removeNotPreservedAnalysis(MP);
1247 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001248 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001249 }
1250 return Changed;
1251}
1252
Devang Patele64d3052007-04-16 20:12:57 +00001253/// Add RequiredPass into list of lower level passes required by pass P.
1254/// RequiredPass is run on the fly by Pass Manager when P requests it
1255/// through getAnalysis interface.
1256void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1257
1258 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1259 && "Unable to handle Pass that requires lower level Analysis pass");
1260 assert ((P->getPotentialPassManagerType() <
1261 RequiredPass->getPotentialPassManagerType())
1262 && "Unable to handle Pass that requires lower level Analysis pass");
1263
Devang Patel68f72b12007-04-26 17:50:19 +00001264 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001265 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001266 FPP = new FunctionPassManagerImpl(0);
1267 // FPP is the top level manager.
1268 FPP->setTopLevelManager(FPP);
1269
Devang Patel69e9f6d2007-04-16 20:27:05 +00001270 OnTheFlyManagers[P] = FPP;
1271 }
Devang Patel68f72b12007-04-26 17:50:19 +00001272 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001273
Devang Patel68f72b12007-04-26 17:50:19 +00001274 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001275 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001276 LU.push_back(RequiredPass);
1277 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001278}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001279
1280/// Return function pass corresponding to PassInfo PI, that is
1281/// required by module pass MP. Instantiate analysis pass, by using
1282/// its runOnFunction() for function F.
1283Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1284 Function &F) {
1285 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001286 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001287 assert (FPP && "Unable to find on the fly pass");
1288
Devang Patel68f72b12007-04-26 17:50:19 +00001289 FPP->run(F);
1290 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001291}
1292
1293
Devang Patela1514cb2006-12-07 19:39:39 +00001294//===----------------------------------------------------------------------===//
1295// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001296//
Devang Patelc290c8a2006-11-07 22:23:34 +00001297/// run - Execute all of the passes scheduled for execution. Keep track of
1298/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001299bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001300
Devang Patelc290c8a2006-11-07 22:23:34 +00001301 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001302
Devang Patelb8817b92006-12-14 00:59:42 +00001303 TimingInfo::createTheTimeInfo();
1304
Devang Patelcfd70c42006-12-13 22:10:00 +00001305 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001306 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001307
Devang Patele3068402006-12-21 00:16:50 +00001308 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001309 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1310 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001311 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001312 }
1313 return Changed;
1314}
Devang Patel376fefa2006-11-08 10:29:57 +00001315
Devang Patela1514cb2006-12-07 19:39:39 +00001316//===----------------------------------------------------------------------===//
1317// PassManager implementation
1318
Devang Patel376fefa2006-11-08 10:29:57 +00001319/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001320PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001321 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001322 // PM is the top level manager
1323 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001324}
1325
Devang Patelb67904d2006-12-13 02:36:01 +00001326PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001327 delete PM;
1328}
1329
Devang Patel376fefa2006-11-08 10:29:57 +00001330/// add - Add a pass to the queue of passes to run. This passes ownership of
1331/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1332/// will be destroyed as well, so there is no need to delete the pass. This
1333/// implies that all passes MUST be allocated with 'new'.
1334void
Devang Patelb67904d2006-12-13 02:36:01 +00001335PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001336 PM->add(P);
1337}
1338
1339/// run - Execute all of the passes scheduled for execution. Keep track of
1340/// whether any of the passes modifies the module, and if so, return true.
1341bool
Devang Patelb67904d2006-12-13 02:36:01 +00001342PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001343 return PM->run(M);
1344}
1345
Devang Patelb8817b92006-12-14 00:59:42 +00001346//===----------------------------------------------------------------------===//
1347// TimingInfo Class - This class is used to calculate information about the
1348// amount of time each pass takes to execute. This only happens with
1349// -time-passes is enabled on the command line.
1350//
1351bool llvm::TimePassesIsEnabled = false;
1352static cl::opt<bool,true>
1353EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1354 cl::desc("Time each pass, printing elapsed time for each on exit"));
1355
1356// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1357// a non null value (if the -time-passes option is enabled) or it leaves it
1358// null. It may be called multiple times.
1359void TimingInfo::createTheTimeInfo() {
1360 if (!TimePassesIsEnabled || TheTimeInfo) return;
1361
1362 // Constructed the first time this is called, iff -time-passes is enabled.
1363 // This guarantees that the object will be constructed before static globals,
1364 // thus it will be destroyed before them.
1365 static ManagedStatic<TimingInfo> TTI;
1366 TheTimeInfo = &*TTI;
1367}
1368
Devang Patel1c3633e2007-01-29 23:10:37 +00001369/// If TimingInfo is enabled then start pass timer.
1370void StartPassTimer(Pass *P) {
1371 if (TheTimeInfo)
1372 TheTimeInfo->passStarted(P);
1373}
1374
1375/// If TimingInfo is enabled then stop pass timer.
1376void StopPassTimer(Pass *P) {
1377 if (TheTimeInfo)
1378 TheTimeInfo->passEnded(P);
1379}
1380
Devang Patel1c56a632007-01-08 19:29:38 +00001381//===----------------------------------------------------------------------===//
1382// PMStack implementation
1383//
Devang Patelad98d232007-01-11 22:15:30 +00001384
Devang Patel1c56a632007-01-08 19:29:38 +00001385// Pop Pass Manager from the stack and clear its analysis info.
1386void PMStack::pop() {
1387
1388 PMDataManager *Top = this->top();
1389 Top->initializeAnalysisInfo();
1390
1391 S.pop_back();
1392}
1393
1394// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001395void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001396
Devang Patel15701b52007-01-11 00:19:00 +00001397 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001398 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001399
Devang Patel15701b52007-01-11 00:19:00 +00001400 if (this->empty()) {
1401 Top = PM;
1402 }
1403 else {
1404 Top = this->top();
1405 PMTopLevelManager *TPM = Top->getTopLevelManager();
1406
1407 assert (TPM && "Unable to find top level manager");
1408 TPM->addIndirectPassManager(PM);
1409 PM->setTopLevelManager(TPM);
1410 }
1411
Devang Patel15701b52007-01-11 00:19:00 +00001412 S.push_back(PM);
1413}
1414
1415// Dump content of the pass manager stack.
1416void PMStack::dump() {
1417 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1418 E = S.end(); I != E; ++I) {
1419 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001420 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001421 }
1422 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001423 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001424}
1425
Devang Patel1c56a632007-01-08 19:29:38 +00001426/// Find appropriate Module Pass Manager in the PM Stack and
1427/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001428void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001429 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001430
Devang Patel1c56a632007-01-08 19:29:38 +00001431 // Find Module Pass Manager
1432 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001433 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1434 if (TopPMType == PreferredType)
1435 break; // We found desired pass manager
1436 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001437 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001438 else
1439 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001440 }
1441
Devang Patel23f8aa92007-01-17 21:19:23 +00001442 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001443}
1444
Devang Patel3312f752007-01-16 21:43:18 +00001445/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1446/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001447void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001448 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001449
Devang Patel15701b52007-01-11 00:19:00 +00001450 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001451 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001452 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1453 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001454 else
Devang Patel3312f752007-01-16 21:43:18 +00001455 break;
1456 }
1457 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1458
1459 // Create new Function Pass Manager
1460 if (!FPP) {
1461 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1462 PMDataManager *PMD = PMS.top();
1463
1464 // [1] Create new Function Pass Manager
1465 FPP = new FPPassManager(PMD->getDepth() + 1);
1466
1467 // [2] Set up new manager's top level manager
1468 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1469 TPM->addIndirectPassManager(FPP);
1470
1471 // [3] Assign manager to manage this new manager. This may create
1472 // and push new managers into PMS
Devang Pateldffca632007-01-17 20:30:17 +00001473
1474 // If Call Graph Pass Manager is active then use it to manage
1475 // this new Function Pass manager.
1476 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
Dan Gohman565df952008-03-13 02:08:36 +00001477 FPP->assignPassManager(PMS, PMT_CallGraphPassManager);
Devang Pateldffca632007-01-17 20:30:17 +00001478 else
Dan Gohman565df952008-03-13 02:08:36 +00001479 FPP->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001480
1481 // [4] Push new manager into PMS
1482 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001483 }
1484
Devang Patel3312f752007-01-16 21:43:18 +00001485 // Assign FPP as the manager of this pass.
1486 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001487}
1488
Devang Patel3312f752007-01-16 21:43:18 +00001489/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001490/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001491void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001492 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001493
1494 BBPassManager *BBP = NULL;
1495
Devang Patel15701b52007-01-11 00:19:00 +00001496 // Basic Pass Manager is a leaf pass manager. It does not handle
1497 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001498 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001499 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001500
Devang Patel3312f752007-01-16 21:43:18 +00001501 // If leaf manager is not Basic Block Pass manager then create new
1502 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001503
Devang Patel3312f752007-01-16 21:43:18 +00001504 if (!BBP) {
1505 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1506 PMDataManager *PMD = PMS.top();
1507
1508 // [1] Create new Basic Block Manager
1509 BBP = new BBPassManager(PMD->getDepth() + 1);
1510
1511 // [2] Set up new manager's top level manager
1512 // Basic Block Pass Manager does not live by itself
1513 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1514 TPM->addIndirectPassManager(BBP);
1515
Devang Patel15701b52007-01-11 00:19:00 +00001516 // [3] Assign manager to manage this new manager. This may create
1517 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001518 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001519
Devang Patel3312f752007-01-16 21:43:18 +00001520 // [4] Push new manager into PMS
1521 PMS.push(BBP);
1522 }
Devang Patel1c56a632007-01-08 19:29:38 +00001523
Devang Patel3312f752007-01-16 21:43:18 +00001524 // Assign BBP as the manager of this pass.
1525 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001526}
1527
Dan Gohmand3a20c92008-03-11 16:41:42 +00001528PassManagerBase::~PassManagerBase() {}