blob: b0afaef1317233f47c801a91865cb125b93bd0d3 [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"
Gordon Henriksen878114b2008-03-16 04:20:44 +000022#include "llvm-c/Core.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000023#include <algorithm>
Devang Patela9844592006-11-11 01:31:05 +000024#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000025#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000026using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000027
Devang Patele7599552007-01-12 18:52:44 +000028// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000029
Devang Patelf1567a52006-12-13 20:03:48 +000030namespace llvm {
31
32//===----------------------------------------------------------------------===//
33// Pass debugging information. Often it is useful to find out what pass is
34// running when a crash occurs in a utility. When this library is compiled with
35// debugging on, a command line option (--debug-pass) is enabled that causes the
36// pass name to be printed before it executes.
37//
38
Devang Patel03fb5872006-12-13 21:13:31 +000039// Different debug levels that can be enabled...
40enum PassDebugLevel {
41 None, Arguments, Structure, Executions, Details
42};
43
Devang Patelf1567a52006-12-13 20:03:48 +000044static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000045PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000046 cl::desc("Print PassManager debugging information"),
47 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000048 clEnumVal(None , "disable debug output"),
49 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
50 clEnumVal(Structure , "print pass structure before run()"),
51 clEnumVal(Executions, "print pass name before it is executed"),
52 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000053 clEnumValEnd));
54} // End of llvm namespace
55
Devang Patelffca9102006-12-15 19:39:30 +000056namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000057
Devang Patelf33f3eb2006-12-07 19:21:29 +000058//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000059// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000060//
Devang Patel67d6a5e2006-12-19 19:46:59 +000061/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000062/// pass together and sequence them to process one basic block before
63/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000064class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
65 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000066
67public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000068 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +000069 explicit BBPassManager(int Depth)
Devang Patel09f162c2007-05-01 21:15:47 +000070 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +000071
Devang Patelca58e352006-11-08 10:05:38 +000072 /// Execute all of the passes scheduled for execution. Keep track of
73 /// whether any of the passes modifies the function, and if so, return true.
74 bool runOnFunction(Function &F);
75
Devang Patelf9d96b92006-12-07 19:57:52 +000076 /// Pass Manager itself does not invalidate any analysis info.
77 void getAnalysisUsage(AnalysisUsage &Info) const {
78 Info.setPreservesAll();
79 }
80
Devang Patel475c4532006-12-08 00:59:05 +000081 bool doInitialization(Module &M);
82 bool doInitialization(Function &F);
83 bool doFinalization(Module &M);
84 bool doFinalization(Function &F);
85
Devang Patele3858e62007-02-01 22:08:25 +000086 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +000087 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +000088 }
89
Devang Pateleda56172006-12-12 23:34:33 +000090 // Print passes managed by this manager
91 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000092 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000093 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
94 BasicBlockPass *BP = getContainedPass(Index);
95 BP->dumpPassStructure(Offset + 1);
96 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000097 }
98 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000099
100 BasicBlockPass *getContainedPass(unsigned N) {
101 assert ( N < PassVector.size() && "Pass number out of range!");
102 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
103 return BP;
104 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000105
Devang Patel28349ab2007-02-27 15:00:39 +0000106 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000107 return PMT_BasicBlockPassManager;
108 }
Devang Patelca58e352006-11-08 10:05:38 +0000109};
110
Devang Patel8c78a0b2007-05-03 01:11:54 +0000111char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000112}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000113
Devang Patele7599552007-01-12 18:52:44 +0000114namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000115
Devang Patel10c2ca62006-12-12 22:47:13 +0000116//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000117// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000118//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000119/// FunctionPassManagerImpl manages FPPassManagers
120class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000121 public PMDataManager,
122 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000123public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000124 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000125 explicit FunctionPassManagerImpl(int Depth) :
Devang Patel09f162c2007-05-01 21:15:47 +0000126 Pass((intptr_t)&ID), PMDataManager(Depth),
127 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000128
129 /// add - Add a pass to the queue of passes to run. This passes ownership of
130 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
131 /// will be destroyed as well, so there is no need to delete the pass. This
132 /// implies that all passes MUST be allocated with 'new'.
133 void add(Pass *P) {
134 schedulePass(P);
135 }
136
137 /// run - Execute all of the passes scheduled for execution. Keep track of
138 /// whether any of the passes modifies the module, and if so, return true.
139 bool run(Function &F);
140
141 /// doInitialization - Run all of the initializers for the function passes.
142 ///
143 bool doInitialization(Module &M);
144
Dan Gohmane6656eb2007-07-30 14:51:13 +0000145 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000146 ///
147 bool doFinalization(Module &M);
148
149 /// Pass Manager itself does not invalidate any analysis info.
150 void getAnalysisUsage(AnalysisUsage &Info) const {
151 Info.setPreservesAll();
152 }
153
154 inline void addTopLevelPass(Pass *P) {
155
156 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
157
158 // P is a immutable pass and it will be managed by this
159 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000160 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000161 P->setResolver(AR);
162 initializeAnalysisImpl(P);
163 addImmutablePass(IP);
164 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000165 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000166 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000167 }
Devang Patel0f080042007-01-12 17:23:48 +0000168
Devang Patel67d6a5e2006-12-19 19:46:59 +0000169 }
170
171 FPPassManager *getContainedManager(unsigned N) {
172 assert ( N < PassManagers.size() && "Pass number out of range!");
173 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
174 return FP;
175 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000176};
177
Devang Patel8c78a0b2007-05-03 01:11:54 +0000178char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000179//===----------------------------------------------------------------------===//
180// MPPassManager
181//
182/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000183/// It batches all Module passes and function pass managers together and
184/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000185class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000186
187public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000188 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000189 explicit MPPassManager(int Depth) :
190 Pass((intptr_t)&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000191
192 // Delete on the fly managers.
193 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000194 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000195 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
196 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000197 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000198 delete FPP;
199 }
200 }
201
Devang Patelca58e352006-11-08 10:05:38 +0000202 /// run - Execute all of the passes scheduled for execution. Keep track of
203 /// whether any of the passes modifies the module, and if so, return true.
204 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000205
Devang Patelf9d96b92006-12-07 19:57:52 +0000206 /// Pass Manager itself does not invalidate any analysis info.
207 void getAnalysisUsage(AnalysisUsage &Info) const {
208 Info.setPreservesAll();
209 }
210
Devang Patele64d3052007-04-16 20:12:57 +0000211 /// Add RequiredPass into list of lower level passes required by pass P.
212 /// RequiredPass is run on the fly by Pass Manager when P requests it
213 /// through getAnalysis interface.
214 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
215
Devang Patel69e9f6d2007-04-16 20:27:05 +0000216 /// Return function pass corresponding to PassInfo PI, that is
217 /// required by module pass MP. Instantiate analysis pass, by using
218 /// its runOnFunction() for function F.
219 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
220
Devang Patele3858e62007-02-01 22:08:25 +0000221 virtual const char *getPassName() const {
222 return "Module Pass Manager";
223 }
224
Devang Pateleda56172006-12-12 23:34:33 +0000225 // Print passes managed by this manager
226 void dumpPassStructure(unsigned Offset) {
227 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000228 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
229 ModulePass *MP = getContainedPass(Index);
230 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000231 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000232 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000233 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000234 }
235 }
236
Devang Patelabfbe3b2006-12-16 00:56:26 +0000237 ModulePass *getContainedPass(unsigned N) {
238 assert ( N < PassVector.size() && "Pass number out of range!");
239 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
240 return MP;
241 }
242
Devang Patel28349ab2007-02-27 15:00:39 +0000243 virtual PassManagerType getPassManagerType() const {
244 return PMT_ModulePassManager;
245 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000246
247 private:
248 /// Collection of on the fly FPPassManagers. These managers manage
249 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000250 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000251};
252
Devang Patel8c78a0b2007-05-03 01:11:54 +0000253char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000254//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000255// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000256//
Devang Patel09f162c2007-05-01 21:15:47 +0000257
Devang Patel67d6a5e2006-12-19 19:46:59 +0000258/// PassManagerImpl manages MPPassManagers
259class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000260 public PMDataManager,
261 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000262
263public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000264 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000265 explicit PassManagerImpl(int Depth) :
266 Pass((intptr_t)&ID), PMDataManager(Depth),
267 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000268
Devang Patel376fefa2006-11-08 10:29:57 +0000269 /// add - Add a pass to the queue of passes to run. This passes ownership of
270 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
271 /// will be destroyed as well, so there is no need to delete the pass. This
272 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000273 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000274 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000275 }
Devang Patel376fefa2006-11-08 10:29:57 +0000276
277 /// run - Execute all of the passes scheduled for execution. Keep track of
278 /// whether any of the passes modifies the module, and if so, return true.
279 bool run(Module &M);
280
Devang Patelf9d96b92006-12-07 19:57:52 +0000281 /// Pass Manager itself does not invalidate any analysis info.
282 void getAnalysisUsage(AnalysisUsage &Info) const {
283 Info.setPreservesAll();
284 }
285
Devang Patelabcd1d32006-12-07 21:27:23 +0000286 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000287
Devang Patelfa971cd2006-12-08 23:57:43 +0000288 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000289
290 // P is a immutable pass and it will be managed by this
291 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000292 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000293 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000294 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000295 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000296 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000297 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000298 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000299 }
Devang Patel0f080042007-01-12 17:23:48 +0000300
Devang Patelabcd1d32006-12-07 21:27:23 +0000301 }
302
Devang Patel67d6a5e2006-12-19 19:46:59 +0000303 MPPassManager *getContainedManager(unsigned N) {
304 assert ( N < PassManagers.size() && "Pass number out of range!");
305 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
306 return MP;
307 }
308
Devang Patel376fefa2006-11-08 10:29:57 +0000309};
310
Devang Patel8c78a0b2007-05-03 01:11:54 +0000311char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000312} // End of llvm namespace
313
314namespace {
315
316//===----------------------------------------------------------------------===//
317// TimingInfo Class - This class is used to calculate information about the
318// amount of time each pass takes to execute. This only happens when
319// -time-passes is enabled on the command line.
320//
321
322class VISIBILITY_HIDDEN TimingInfo {
323 std::map<Pass*, Timer> TimingData;
324 TimerGroup TG;
325
326public:
327 // Use 'create' member to get this.
328 TimingInfo() : TG("... Pass execution timing report ...") {}
329
330 // TimingDtor - Print out information about timing information
331 ~TimingInfo() {
332 // Delete all of the timers...
333 TimingData.clear();
334 // TimerGroup is deleted next, printing the report.
335 }
336
337 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
338 // to a non null value (if the -time-passes option is enabled) or it leaves it
339 // null. It may be called multiple times.
340 static void createTheTimeInfo();
341
342 void passStarted(Pass *P) {
343
344 if (dynamic_cast<PMDataManager *>(P))
345 return;
346
347 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
348 if (I == TimingData.end())
349 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
350 I->second.startTimer();
351 }
352 void passEnded(Pass *P) {
353
354 if (dynamic_cast<PMDataManager *>(P))
355 return;
356
357 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
358 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
359 I->second.stopTimer();
360 }
361};
362
Devang Patelb8817b92006-12-14 00:59:42 +0000363static TimingInfo *TheTimeInfo;
364
Devang Patel1c3633e2007-01-29 23:10:37 +0000365} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000366
Devang Patela1514cb2006-12-07 19:39:39 +0000367//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000368// PMTopLevelManager implementation
369
Devang Patel4268fc02007-01-16 02:00:38 +0000370/// Initialize top level manager. Create first pass manager.
371PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
372
373 if (t == TLM_Pass) {
374 MPPassManager *MPP = new MPPassManager(1);
375 MPP->setTopLevelManager(this);
376 addPassManager(MPP);
377 activeStack.push(MPP);
378 }
379 else if (t == TLM_Function) {
380 FPPassManager *FPP = new FPPassManager(1);
381 FPP->setTopLevelManager(this);
382 addPassManager(FPP);
383 activeStack.push(FPP);
384 }
385}
386
Devang Patelafb1f3622006-12-12 22:35:25 +0000387/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000388void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000389 Pass *P) {
390
Devang Patel8adae862007-07-20 18:04:54 +0000391 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000392 E = AnalysisPasses.end(); I != E; ++I) {
393 Pass *AP = *I;
394 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000395
396 if (P == AP)
397 continue;
398
Devang Patelafb1f3622006-12-12 22:35:25 +0000399 // If AP is the last user of other passes then make P last user of
400 // such passes.
401 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
402 LUE = LastUser.end(); LUI != LUE; ++LUI) {
403 if (LUI->second == AP)
404 LastUser[LUI->first] = P;
405 }
406 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000407}
408
409/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000410void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000411 Pass *P) {
Chris Lattner9df8be42007-02-17 23:14:24 +0000412 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
413 LUE = LastUser.end(); LUI != LUE; ++LUI)
414 if (LUI->second == P)
415 LastUses.push_back(LUI->first);
Devang Patelafb1f3622006-12-12 22:35:25 +0000416}
417
418/// Schedule pass P for execution. Make sure that passes required by
419/// P are run before P is run. Update analysis info maintained by
420/// the manager. Remove dead passes. This is a recursive function.
421void PMTopLevelManager::schedulePass(Pass *P) {
422
Devang Patel3312f752007-01-16 21:43:18 +0000423 // TODO : Allocate function manager for this pass, other wise required set
424 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000425
Devang Pateld74ede72007-03-06 01:06:16 +0000426 // Give pass a chance to prepare the stage.
427 P->preparePassManager(activeStack);
428
Devang Patel864970e2008-03-18 00:39:19 +0000429 // If P is an analysis pass and it is available then do not
430 // generate the analysis again. Stale analysis info should not be
431 // available at this point.
432 if (P->isAnalysis() && findAnalysisPass(P->getPassInfo()))
433 return;
434
Devang Patelafb1f3622006-12-12 22:35:25 +0000435 AnalysisUsage AnUsage;
436 P->getAnalysisUsage(AnUsage);
437 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
438 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
439 E = RequiredSet.end(); I != E; ++I) {
440
441 Pass *AnalysisPass = findAnalysisPass(*I);
442 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000443 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000444 // Schedule this analysis run first only if it is not a lower level
445 // analysis pass. Lower level analsyis passes are run on the fly.
446 if (P->getPotentialPassManagerType () >=
447 AnalysisPass->getPotentialPassManagerType())
448 schedulePass(AnalysisPass);
449 else
450 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000451 }
452 }
453
454 // Now all required passes are available.
455 addTopLevelPass(P);
456}
457
458/// Find the pass that implements Analysis AID. Search immutable
459/// passes and all pass managers. If desired pass is not found
460/// then return NULL.
461Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
462
463 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000464 // Check pass managers
Dan Gohman73caf5f2008-03-13 01:48:32 +0000465 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000466 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000467 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000468 P = PMD->findAnalysisPass(AID, false);
469 }
470
471 // Check other pass managers
472 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
473 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
474 P = (*I)->findAnalysisPass(AID, false);
475
Devang Patelafb1f3622006-12-12 22:35:25 +0000476 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
477 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
478 const PassInfo *PI = (*I)->getPassInfo();
479 if (PI == AID)
480 P = *I;
481
482 // If Pass not found then check the interfaces implemented by Immutable Pass
483 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000484 const std::vector<const PassInfo*> &ImmPI =
485 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000486 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
487 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000488 }
489 }
490
Devang Patelafb1f3622006-12-12 22:35:25 +0000491 return P;
492}
493
Devang Pateleda56172006-12-12 23:34:33 +0000494// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000495void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000496
Devang Patelfd4184322007-01-17 20:33:36 +0000497 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000498 return;
499
Devang Pateleda56172006-12-12 23:34:33 +0000500 // Print out the immutable passes
501 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
502 ImmutablePasses[i]->dumpPassStructure(0);
503 }
504
Dan Gohman73caf5f2008-03-13 01:48:32 +0000505 // Every class that derives from PMDataManager also derives from Pass
506 // (sometimes indirectly), but there's no inheritance relationship
507 // between PMDataManager and Pass, so we have to dynamic_cast to get
508 // from a PMDataManager* to a Pass*.
509 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000510 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000511 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000512}
513
Devang Patel991aeba2006-12-15 20:13:01 +0000514void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000515
Devang Patelfd4184322007-01-17 20:33:36 +0000516 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000517 return;
518
519 cerr << "Pass Arguments: ";
Dan Gohman73caf5f2008-03-13 01:48:32 +0000520 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000521 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000522 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000523 PMD->dumpPassArguments();
524 }
525 cerr << "\n";
526}
527
Devang Patele3068402006-12-21 00:16:50 +0000528void PMTopLevelManager::initializeAllAnalysisInfo() {
529
Dan Gohman73caf5f2008-03-13 01:48:32 +0000530 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000531 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000532 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000533 PMD->initializeAnalysisInfo();
534 }
535
536 // Initailize other pass managers
537 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
538 E = IndirectPassManagers.end(); I != E; ++I)
539 (*I)->initializeAnalysisInfo();
540}
541
Devang Patele7599552007-01-12 18:52:44 +0000542/// Destructor
543PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000544 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000545 E = PassManagers.end(); I != E; ++I)
546 delete *I;
547
548 for (std::vector<ImmutablePass *>::iterator
549 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
550 delete *I;
Devang Patele7599552007-01-12 18:52:44 +0000551}
552
Devang Patelafb1f3622006-12-12 22:35:25 +0000553//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000554// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000555
Devang Patel643676c2006-11-11 01:10:19 +0000556/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000557void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000558
Devang Patel643676c2006-11-11 01:10:19 +0000559 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000560 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000561
Devang Patele9976aa2006-12-07 19:33:53 +0000562 //This pass is the current implementation of all of the interfaces it
563 //implements as well.
564 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
565 for (unsigned i = 0, e = II.size(); i != e; ++i)
566 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000567 }
568}
569
Devang Patel9d9fc902007-03-06 17:52:53 +0000570// Return true if P preserves high level analysis used by other
571// passes managed by this manager
572bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
573
574 AnalysisUsage AnUsage;
575 P->getAnalysisUsage(AnUsage);
576
577 if (AnUsage.getPreservesAll())
578 return true;
579
580 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
581 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
582 E = HigherLevelAnalysis.end(); I != E; ++I) {
583 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000584 if (!dynamic_cast<ImmutablePass*>(P1) &&
585 std::find(PreservedSet.begin(), PreservedSet.end(),
586 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000587 PreservedSet.end())
588 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000589 }
590
591 return true;
592}
593
Devang Patela273d1c2007-07-19 18:02:32 +0000594/// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
595void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000596 AnalysisUsage AnUsage;
597 P->getAnalysisUsage(AnUsage);
Devang Patelef432532007-07-19 05:36:09 +0000598 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000599
Devang Patelef432532007-07-19 05:36:09 +0000600 // Verify preserved analysis
Devang Patela273d1c2007-07-19 18:02:32 +0000601 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
602 E = PreservedSet.end(); I != E; ++I) {
603 AnalysisID AID = *I;
604 Pass *AP = findAnalysisPass(AID, true);
605 if (AP)
606 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000607 }
Devang Patela273d1c2007-07-19 18:02:32 +0000608}
609
610/// Remove Analyss not preserved by Pass P
611void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
612 AnalysisUsage AnUsage;
613 P->getAnalysisUsage(AnUsage);
Devang Patel2e169c32006-12-07 20:03:49 +0000614 if (AnUsage.getPreservesAll())
615 return;
616
Devang Patela273d1c2007-07-19 18:02:32 +0000617 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000618 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000619 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000620 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000621 if (!dynamic_cast<ImmutablePass*>(Info->second)
622 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
623 PreservedSet.end())
Devang Patel349170f2006-11-11 01:24:55 +0000624 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000625 AvailableAnalysis.erase(Info);
Devang Patel349170f2006-11-11 01:24:55 +0000626 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000627
628 // Check inherited analysis also. If P is not preserving analysis
629 // provided by parent manager then remove it here.
630 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
631
632 if (!InheritedAnalysis[Index])
633 continue;
634
635 for (std::map<AnalysisID, Pass*>::iterator
636 I = InheritedAnalysis[Index]->begin(),
637 E = InheritedAnalysis[Index]->end(); I != E; ) {
638 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000639 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
640 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000641 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000642 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000643 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000644 }
645 }
646
Devang Patelf68a3492006-11-07 22:35:17 +0000647}
648
Devang Patelca189262006-11-14 03:05:08 +0000649/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000650void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000651 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000652
Devang Patel8adae862007-07-20 18:04:54 +0000653 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000654
Devang Patel2ff44922007-04-16 20:39:59 +0000655 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000656 if (!TPM)
657 return;
658
Devang Patel17ad0962006-12-08 00:37:52 +0000659 TPM->collectLastUses(DeadPasses, P);
660
Devang Patel8adae862007-07-20 18:04:54 +0000661 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000662 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000663
Devang Patel003a5592007-03-05 20:01:30 +0000664 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000665
Devang Patel7ebf09d2007-03-05 18:20:51 +0000666 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000667 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000668 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000669
Devang Patel17ad0962006-12-08 00:37:52 +0000670 std::map<AnalysisID, Pass*>::iterator Pos =
671 AvailableAnalysis.find((*I)->getPassInfo());
672
Devang Patel475c4532006-12-08 00:59:05 +0000673 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000674 if (Pos != AvailableAnalysis.end())
675 AvailableAnalysis.erase(Pos);
676 }
Devang Patelca189262006-11-14 03:05:08 +0000677}
678
Devang Patel8f677ce2006-12-07 18:47:25 +0000679/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000680/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000681void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000682 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000683
Devang Pateld440cd92006-12-08 23:53:00 +0000684 // This manager is going to manage pass P. Set up analysis resolver
685 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000686 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000687 P->setResolver(AR);
688
Devang Patelec2b9a72007-03-05 22:57:49 +0000689 // If a FunctionPass F is the last user of ModulePass info M
690 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000691 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000692
Devang Patel90b05e02006-11-11 02:04:19 +0000693 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000694
695 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000696 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000697 SmallVector<Pass *, 8> RequiredPasses;
698 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
699
Devang Patelbc03f132006-12-07 23:55:10 +0000700 unsigned PDepth = this->getDepth();
701
Devang Patele64d3052007-04-16 20:12:57 +0000702 collectRequiredAnalysis(RequiredPasses,
703 ReqAnalysisNotAvailable, P);
704 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000705 E = RequiredPasses.end(); I != E; ++I) {
706 Pass *PRequired = *I;
707 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000708
709 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
710 RDepth = DM.getDepth();
711
Devang Patelbc03f132006-12-07 23:55:10 +0000712 if (PDepth == RDepth)
713 LastUses.push_back(PRequired);
714 else if (PDepth > RDepth) {
715 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000716 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000717 // Keep track of higher level analysis used by this manager.
718 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000719 } else
720 assert (0 && "Unable to accomodate Required Pass");
721 }
722
Devang Patel39786a92007-01-15 20:31:54 +0000723 // Set P as P's last user until someone starts using P.
724 // However, if P is a Pass Manager then it does not need
725 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000726 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000727 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000728 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000729
Devang Patelec2b9a72007-03-05 22:57:49 +0000730 if (!TransferLastUses.empty()) {
731 Pass *My_PM = dynamic_cast<Pass *>(this);
732 TPM->setLastUser(TransferLastUses, My_PM);
733 TransferLastUses.clear();
734 }
735
Devang Patel69e9f6d2007-04-16 20:27:05 +0000736 // Now, take care of required analysises that are not available.
737 for (SmallVector<AnalysisID, 8>::iterator
738 I = ReqAnalysisNotAvailable.begin(),
739 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
740 Pass *AnalysisPass = (*I)->createPass();
741 this->addLowerLevelRequiredPass(P, AnalysisPass);
742 }
743
Devang Patel17bff0d2006-12-07 22:09:36 +0000744 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000745 // Remove the analysis not preserved by this pass
746 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000747 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000748 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000749
750 // Add pass
751 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000752}
753
Devang Patele64d3052007-04-16 20:12:57 +0000754
755/// Populate RP with analysis pass that are required by
756/// pass P and are available. Populate RP_NotAvail with analysis
757/// pass that are required by pass P but are not available.
758void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
759 SmallVector<AnalysisID, 8> &RP_NotAvail,
760 Pass *P) {
Devang Patel1d6267c2006-12-07 23:05:44 +0000761 AnalysisUsage AnUsage;
762 P->getAnalysisUsage(AnUsage);
763 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
764 for (std::vector<AnalysisID>::const_iterator
765 I = RequiredSet.begin(), E = RequiredSet.end();
766 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000767 AnalysisID AID = *I;
768 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
769 RP.push_back(AnalysisPass);
770 else
771 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000772 }
Devang Patelf58183d2006-12-12 23:09:32 +0000773
774 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
775 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
776 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000777 AnalysisID AID = *I;
778 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
779 RP.push_back(AnalysisPass);
780 else
781 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000782 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000783}
784
Devang Patel07f4f582006-11-14 21:49:36 +0000785// All Required analyses should be available to the pass as it runs! Here
786// we fill in the AnalysisImpls member of the pass so that it can
787// successfully use the getAnalysis() method to retrieve the
788// implementations it needs.
789//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000790void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000791 AnalysisUsage AnUsage;
792 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000793
794 for (std::vector<const PassInfo *>::const_iterator
795 I = AnUsage.getRequiredSet().begin(),
796 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000797 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000798 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000799 // This may be analysis pass that is initialized on the fly.
800 // If that is not the case then it will raise an assert when it is used.
801 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000802 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000803 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000804 }
805}
806
Devang Patel640c5bb2006-12-08 22:30:11 +0000807/// Find the pass that implements Analysis AID. If desired pass is not found
808/// then return NULL.
809Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
810
811 // Check if AvailableAnalysis map has one entry.
812 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
813
814 if (I != AvailableAnalysis.end())
815 return I->second;
816
817 // Search Parents through TopLevelManager
818 if (SearchParent)
819 return TPM->findAnalysisPass(AID);
820
Devang Patel9d759b82006-12-09 00:09:12 +0000821 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000822}
823
Devang Patel991aeba2006-12-15 20:13:01 +0000824// Print list of passes that are last used by P.
825void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
826
Devang Patel8adae862007-07-20 18:04:54 +0000827 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000828
829 // If this is a on the fly manager then it does not have TPM.
830 if (!TPM)
831 return;
832
Devang Patel991aeba2006-12-15 20:13:01 +0000833 TPM->collectLastUses(LUses, P);
834
Devang Patel8adae862007-07-20 18:04:54 +0000835 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000836 E = LUses.end(); I != E; ++I) {
837 llvm::cerr << "--" << std::string(Offset*2, ' ');
838 (*I)->dumpPassStructure(0);
839 }
840}
841
842void PMDataManager::dumpPassArguments() const {
843 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
844 E = PassVector.end(); I != E; ++I) {
845 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
846 PMD->dumpPassArguments();
847 else
848 if (const PassInfo *PI = (*I)->getPassInfo())
849 if (!PI->isAnalysisGroup())
850 cerr << " -" << PI->getPassArgument();
851 }
852}
853
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000854void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
855 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000856 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000857 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000858 return;
859 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000860 switch (S1) {
861 case EXECUTION_MSG:
862 cerr << "Executing Pass '" << P->getPassName();
863 break;
864 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000865 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000866 break;
867 case FREEING_MSG:
868 cerr << " Freeing Pass '" << P->getPassName();
869 break;
870 default:
871 break;
872 }
873 switch (S2) {
874 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000875 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000876 break;
877 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000878 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000879 break;
880 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000881 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000882 break;
883 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000884 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000885 break;
886 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000887 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000888 break;
889 default:
890 break;
891 }
Devang Patel991aeba2006-12-15 20:13:01 +0000892}
893
894void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
895 const std::vector<AnalysisID> &Set)
896 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000897 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000898 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
899 for (unsigned i = 0; i != Set.size(); ++i) {
900 if (i) cerr << ",";
901 cerr << " " << Set[i]->getPassName();
902 }
903 cerr << "\n";
904 }
905}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000906
Devang Patel004937b2007-07-27 20:06:09 +0000907/// Add RequiredPass into list of lower level passes required by pass P.
908/// RequiredPass is run on the fly by Pass Manager when P requests it
909/// through getAnalysis interface.
910/// This should be handled by specific pass manager.
911void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
912 if (TPM) {
913 TPM->dumpArguments();
914 TPM->dumpPasses();
915 }
Devang Patel8df7cc12008-02-02 01:43:30 +0000916
917 // Module Level pass may required Function Level analysis info
918 // (e.g. dominator info). Pass manager uses on the fly function pass manager
919 // to provide this on demand. In that case, in Pass manager terminology,
920 // module level pass is requiring lower level analysis info managed by
921 // lower level pass manager.
922
923 // When Pass manager is not able to order required analysis info, Pass manager
924 // checks whether any lower level manager will be able to provide this
925 // analysis info on demand or not.
Devang Patel004937b2007-07-27 20:06:09 +0000926 assert (0 && "Unable to handle Pass that requires lower level Analysis pass");
927}
928
Devang Patele7599552007-01-12 18:52:44 +0000929// Destructor
930PMDataManager::~PMDataManager() {
931
932 for (std::vector<Pass *>::iterator I = PassVector.begin(),
933 E = PassVector.end(); I != E; ++I)
934 delete *I;
935
Devang Patele7599552007-01-12 18:52:44 +0000936}
937
Devang Patel9bdf7d42006-12-08 23:28:54 +0000938//===----------------------------------------------------------------------===//
939// NOTE: Is this the right place to define this method ?
940// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000941Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000942 return PM.findAnalysisPass(ID, dir);
943}
944
Devang Patel92942812007-04-16 20:56:24 +0000945Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
946 Function &F) {
947 return PM.getOnTheFlyPass(P, AnalysisPI, F);
948}
949
Devang Patela1514cb2006-12-07 19:39:39 +0000950//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000951// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000952
Devang Patel6e5a1132006-11-07 21:31:57 +0000953/// Execute all of the passes scheduled for execution by invoking
954/// runOnBasicBlock method. Keep track of whether any of the passes modifies
955/// the function, and if so, return true.
956bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000957BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000958
Reid Spencer5301e7c2007-01-30 20:08:39 +0000959 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +0000960 return false;
961
Devang Patele9585592006-12-08 01:38:28 +0000962 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000963
Devang Patel6e5a1132006-11-07 21:31:57 +0000964 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000965 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
966 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000967 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000968 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000969
Devang Pateld305c402007-08-10 18:29:32 +0000970 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000971 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000972
Devang Patelabfbe3b2006-12-16 00:56:26 +0000973 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000974
Devang Patelabfbe3b2006-12-16 00:56:26 +0000975 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000976 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000977 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000978
Devang Patel003a5592007-03-05 20:01:30 +0000979 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +0000980 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
981 I->getNameStart());
Devang Patelabfbe3b2006-12-16 00:56:26 +0000982 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000983
Devang Patela273d1c2007-07-19 18:02:32 +0000984 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000985 removeNotPreservedAnalysis(BP);
986 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +0000987 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +0000988 }
Chris Lattnerde2aa652007-08-10 06:22:25 +0000989
Devang Patel56d48ec2006-12-15 22:57:49 +0000990 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000991}
992
Devang Patel475c4532006-12-08 00:59:05 +0000993// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000994inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000995 bool Changed = false;
996
Devang Patelabfbe3b2006-12-16 00:56:26 +0000997 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
998 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000999 Changed |= BP->doInitialization(M);
1000 }
1001
1002 return Changed;
1003}
1004
Devang Patel67d6a5e2006-12-19 19:46:59 +00001005inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001006 bool Changed = false;
1007
Devang Patelabfbe3b2006-12-16 00:56:26 +00001008 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1009 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001010 Changed |= BP->doFinalization(M);
1011 }
1012
1013 return Changed;
1014}
1015
Devang Patel67d6a5e2006-12-19 19:46:59 +00001016inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001017 bool Changed = false;
1018
Devang Patelabfbe3b2006-12-16 00:56:26 +00001019 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1020 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001021 Changed |= BP->doInitialization(F);
1022 }
1023
1024 return Changed;
1025}
1026
Devang Patel67d6a5e2006-12-19 19:46:59 +00001027inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001028 bool Changed = false;
1029
Devang Patelabfbe3b2006-12-16 00:56:26 +00001030 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1031 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001032 Changed |= BP->doFinalization(F);
1033 }
1034
1035 return Changed;
1036}
1037
1038
Devang Patela1514cb2006-12-07 19:39:39 +00001039//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001040// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001041
Devang Patel4e12f862006-11-08 10:44:40 +00001042/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001043FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001044 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001045 // FPM is the top level manager.
1046 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001047
Dan Gohman565df952008-03-13 02:08:36 +00001048 AnalysisResolver *AR = new AnalysisResolver(*FPM);
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.
Dan Gohman11eecd62008-03-13 01:21:31 +00001402void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001403
Devang Patel15701b52007-01-11 00:19:00 +00001404 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001405 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001406
Devang Patel15701b52007-01-11 00:19:00 +00001407 if (this->empty()) {
1408 Top = PM;
1409 }
1410 else {
1411 Top = this->top();
1412 PMTopLevelManager *TPM = Top->getTopLevelManager();
1413
1414 assert (TPM && "Unable to find top level manager");
1415 TPM->addIndirectPassManager(PM);
1416 PM->setTopLevelManager(TPM);
1417 }
1418
Devang Patel15701b52007-01-11 00:19:00 +00001419 S.push_back(PM);
1420}
1421
1422// Dump content of the pass manager stack.
1423void PMStack::dump() {
1424 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1425 E = S.end(); I != E; ++I) {
1426 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001427 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001428 }
1429 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001430 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001431}
1432
Devang Patel1c56a632007-01-08 19:29:38 +00001433/// Find appropriate Module Pass Manager in the PM Stack and
1434/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001435void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001436 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001437
Devang Patel1c56a632007-01-08 19:29:38 +00001438 // Find Module Pass Manager
1439 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001440 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1441 if (TopPMType == PreferredType)
1442 break; // We found desired pass manager
1443 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001444 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001445 else
1446 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001447 }
1448
Devang Patel23f8aa92007-01-17 21:19:23 +00001449 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001450}
1451
Devang Patel3312f752007-01-16 21:43:18 +00001452/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1453/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001454void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001455 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001456
Devang Patel15701b52007-01-11 00:19:00 +00001457 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001458 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001459 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1460 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001461 else
Devang Patel3312f752007-01-16 21:43:18 +00001462 break;
1463 }
1464 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1465
1466 // Create new Function Pass Manager
1467 if (!FPP) {
1468 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1469 PMDataManager *PMD = PMS.top();
1470
1471 // [1] Create new Function Pass Manager
1472 FPP = new FPPassManager(PMD->getDepth() + 1);
1473
1474 // [2] Set up new manager's top level manager
1475 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1476 TPM->addIndirectPassManager(FPP);
1477
1478 // [3] Assign manager to manage this new manager. This may create
1479 // and push new managers into PMS
Devang Pateldffca632007-01-17 20:30:17 +00001480
1481 // If Call Graph Pass Manager is active then use it to manage
1482 // this new Function Pass manager.
1483 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
Dan Gohman565df952008-03-13 02:08:36 +00001484 FPP->assignPassManager(PMS, PMT_CallGraphPassManager);
Devang Pateldffca632007-01-17 20:30:17 +00001485 else
Dan Gohman565df952008-03-13 02:08:36 +00001486 FPP->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001487
1488 // [4] Push new manager into PMS
1489 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001490 }
1491
Devang Patel3312f752007-01-16 21:43:18 +00001492 // Assign FPP as the manager of this pass.
1493 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001494}
1495
Devang Patel3312f752007-01-16 21:43:18 +00001496/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001497/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001498void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001499 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001500
1501 BBPassManager *BBP = NULL;
1502
Devang Patel15701b52007-01-11 00:19:00 +00001503 // Basic Pass Manager is a leaf pass manager. It does not handle
1504 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001505 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001506 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001507
Devang Patel3312f752007-01-16 21:43:18 +00001508 // If leaf manager is not Basic Block Pass manager then create new
1509 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001510
Devang Patel3312f752007-01-16 21:43:18 +00001511 if (!BBP) {
1512 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1513 PMDataManager *PMD = PMS.top();
1514
1515 // [1] Create new Basic Block Manager
1516 BBP = new BBPassManager(PMD->getDepth() + 1);
1517
1518 // [2] Set up new manager's top level manager
1519 // Basic Block Pass Manager does not live by itself
1520 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1521 TPM->addIndirectPassManager(BBP);
1522
Devang Patel15701b52007-01-11 00:19:00 +00001523 // [3] Assign manager to manage this new manager. This may create
1524 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001525 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001526
Devang Patel3312f752007-01-16 21:43:18 +00001527 // [4] Push new manager into PMS
1528 PMS.push(BBP);
1529 }
Devang Patel1c56a632007-01-08 19:29:38 +00001530
Devang Patel3312f752007-01-16 21:43:18 +00001531 // Assign BBP as the manager of this pass.
1532 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001533}
1534
Dan Gohmand3a20c92008-03-11 16:41:42 +00001535PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001536
1537/*===-- C Bindings --------------------------------------------------------===*/
1538
1539LLVMPassManagerRef LLVMCreatePassManager() {
1540 return wrap(new PassManager());
1541}
1542
1543LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1544 return wrap(new FunctionPassManager(unwrap(P)));
1545}
1546
1547int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1548 return unwrap<PassManager>(PM)->run(*unwrap(M));
1549}
1550
1551int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1552 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1553}
1554
1555int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1556 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1557}
1558
1559int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1560 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1561}
1562
1563void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1564 delete unwrap(PM);
1565}