blob: 20fc68c47486c75067d2c778a4ca6c079791036d [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"
Devang Patel9dbe4d12008-07-01 17:44:24 +000022#include "llvm/Analysis/Dominators.h"
Gordon Henriksen878114b2008-03-16 04:20:44 +000023#include "llvm-c/Core.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000024#include <algorithm>
Devang Patela9844592006-11-11 01:31:05 +000025#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000026#include <map>
Dan Gohman8c43e412007-10-03 19:04:09 +000027using namespace llvm;
Devang Patelffca9102006-12-15 19:39:30 +000028
Devang Patele7599552007-01-12 18:52:44 +000029// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000030
Devang Patelf1567a52006-12-13 20:03:48 +000031namespace llvm {
32
33//===----------------------------------------------------------------------===//
34// Pass debugging information. Often it is useful to find out what pass is
35// running when a crash occurs in a utility. When this library is compiled with
36// debugging on, a command line option (--debug-pass) is enabled that causes the
37// pass name to be printed before it executes.
38//
39
Devang Patel03fb5872006-12-13 21:13:31 +000040// Different debug levels that can be enabled...
41enum PassDebugLevel {
42 None, Arguments, Structure, Executions, Details
43};
44
Devang Patel99ad4ba2008-07-01 21:36:11 +000045bool VerifyDomInfo = false;
Devang Patel9dbe4d12008-07-01 17:44:24 +000046static cl::opt<bool,true>
47VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
48 cl::desc("Verify dominator info (time consuming)"));
49
Devang Patelf1567a52006-12-13 20:03:48 +000050static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000051PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000052 cl::desc("Print PassManager debugging information"),
53 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000054 clEnumVal(None , "disable debug output"),
55 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
56 clEnumVal(Structure , "print pass structure before run()"),
57 clEnumVal(Executions, "print pass name before it is executed"),
58 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000059 clEnumValEnd));
60} // End of llvm namespace
61
Devang Patelffca9102006-12-15 19:39:30 +000062namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000063
Devang Patelf33f3eb2006-12-07 19:21:29 +000064//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000065// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000066//
Devang Patel67d6a5e2006-12-19 19:46:59 +000067/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000068/// pass together and sequence them to process one basic block before
69/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000070class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
71 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000072
73public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000074 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +000075 explicit BBPassManager(int Depth)
Dan Gohmana79db302008-09-04 17:05:41 +000076 : PMDataManager(Depth), FunctionPass(&ID) {}
Devang Patelca58e352006-11-08 10:05:38 +000077
Devang Patelca58e352006-11-08 10:05:38 +000078 /// Execute all of the passes scheduled for execution. Keep track of
79 /// whether any of the passes modifies the function, and if so, return true.
80 bool runOnFunction(Function &F);
81
Devang Patelf9d96b92006-12-07 19:57:52 +000082 /// Pass Manager itself does not invalidate any analysis info.
83 void getAnalysisUsage(AnalysisUsage &Info) const {
84 Info.setPreservesAll();
85 }
86
Devang Patel475c4532006-12-08 00:59:05 +000087 bool doInitialization(Module &M);
88 bool doInitialization(Function &F);
89 bool doFinalization(Module &M);
90 bool doFinalization(Function &F);
91
Devang Patele3858e62007-02-01 22:08:25 +000092 virtual const char *getPassName() const {
Dan Gohman1e9860a2008-03-13 01:58:48 +000093 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +000094 }
95
Devang Pateleda56172006-12-12 23:34:33 +000096 // Print passes managed by this manager
97 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000098 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000099 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
100 BasicBlockPass *BP = getContainedPass(Index);
101 BP->dumpPassStructure(Offset + 1);
102 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000103 }
104 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000105
106 BasicBlockPass *getContainedPass(unsigned N) {
107 assert ( N < PassVector.size() && "Pass number out of range!");
108 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
109 return BP;
110 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000111
Devang Patel28349ab2007-02-27 15:00:39 +0000112 virtual PassManagerType getPassManagerType() const {
Devang Patel3b3f8992007-01-11 01:10:25 +0000113 return PMT_BasicBlockPassManager;
114 }
Devang Patelca58e352006-11-08 10:05:38 +0000115};
116
Devang Patel8c78a0b2007-05-03 01:11:54 +0000117char BBPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +0000118}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000119
Devang Patele7599552007-01-12 18:52:44 +0000120namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000121
Devang Patel10c2ca62006-12-12 22:47:13 +0000122//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000123// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000124//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000125/// FunctionPassManagerImpl manages FPPassManagers
126class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000127 public PMDataManager,
128 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000129public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000130 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000131 explicit FunctionPassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000132 Pass(&ID), PMDataManager(Depth),
Devang Patel09f162c2007-05-01 21:15:47 +0000133 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000134
135 /// add - Add a pass to the queue of passes to run. This passes ownership of
136 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
137 /// will be destroyed as well, so there is no need to delete the pass. This
138 /// implies that all passes MUST be allocated with 'new'.
139 void add(Pass *P) {
140 schedulePass(P);
141 }
142
143 /// run - Execute all of the passes scheduled for execution. Keep track of
144 /// whether any of the passes modifies the module, and if so, return true.
145 bool run(Function &F);
146
147 /// doInitialization - Run all of the initializers for the function passes.
148 ///
149 bool doInitialization(Module &M);
150
Dan Gohmane6656eb2007-07-30 14:51:13 +0000151 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000152 ///
153 bool doFinalization(Module &M);
154
155 /// Pass Manager itself does not invalidate any analysis info.
156 void getAnalysisUsage(AnalysisUsage &Info) const {
157 Info.setPreservesAll();
158 }
159
160 inline void addTopLevelPass(Pass *P) {
161
162 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
163
164 // P is a immutable pass and it will be managed by this
165 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000166 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000167 P->setResolver(AR);
168 initializeAnalysisImpl(P);
169 addImmutablePass(IP);
170 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000171 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000172 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000173 }
Devang Patel0f080042007-01-12 17:23:48 +0000174
Devang Patel67d6a5e2006-12-19 19:46:59 +0000175 }
176
177 FPPassManager *getContainedManager(unsigned N) {
178 assert ( N < PassManagers.size() && "Pass number out of range!");
179 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
180 return FP;
181 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000182};
183
Devang Patel8c78a0b2007-05-03 01:11:54 +0000184char FunctionPassManagerImpl::ID = 0;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000185//===----------------------------------------------------------------------===//
186// MPPassManager
187//
188/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000189/// It batches all Module passes and function pass managers together and
190/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000191class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000192
193public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000194 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000195 explicit MPPassManager(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000196 Pass(&ID), PMDataManager(Depth) { }
Devang Patel2ff44922007-04-16 20:39:59 +0000197
198 // Delete on the fly managers.
199 virtual ~MPPassManager() {
Devang Patel68f72b12007-04-26 17:50:19 +0000200 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000201 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
202 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000203 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000204 delete FPP;
205 }
206 }
207
Devang Patelca58e352006-11-08 10:05:38 +0000208 /// run - Execute all of the passes scheduled for execution. Keep track of
209 /// whether any of the passes modifies the module, and if so, return true.
210 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000211
Devang Patelf9d96b92006-12-07 19:57:52 +0000212 /// Pass Manager itself does not invalidate any analysis info.
213 void getAnalysisUsage(AnalysisUsage &Info) const {
214 Info.setPreservesAll();
215 }
216
Devang Patele64d3052007-04-16 20:12:57 +0000217 /// Add RequiredPass into list of lower level passes required by pass P.
218 /// RequiredPass is run on the fly by Pass Manager when P requests it
219 /// through getAnalysis interface.
220 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
221
Devang Patel69e9f6d2007-04-16 20:27:05 +0000222 /// Return function pass corresponding to PassInfo PI, that is
223 /// required by module pass MP. Instantiate analysis pass, by using
224 /// its runOnFunction() for function F.
225 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
226
Devang Patele3858e62007-02-01 22:08:25 +0000227 virtual const char *getPassName() const {
228 return "Module Pass Manager";
229 }
230
Devang Pateleda56172006-12-12 23:34:33 +0000231 // Print passes managed by this manager
232 void dumpPassStructure(unsigned Offset) {
233 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000234 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
235 ModulePass *MP = getContainedPass(Index);
236 MP->dumpPassStructure(Offset + 1);
Devang Patel68f72b12007-04-26 17:50:19 +0000237 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel2ff44922007-04-16 20:39:59 +0000238 FPP->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000239 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000240 }
241 }
242
Devang Patelabfbe3b2006-12-16 00:56:26 +0000243 ModulePass *getContainedPass(unsigned N) {
244 assert ( N < PassVector.size() && "Pass number out of range!");
245 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
246 return MP;
247 }
248
Devang Patel28349ab2007-02-27 15:00:39 +0000249 virtual PassManagerType getPassManagerType() const {
250 return PMT_ModulePassManager;
251 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000252
253 private:
254 /// Collection of on the fly FPPassManagers. These managers manage
255 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000256 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000257};
258
Devang Patel8c78a0b2007-05-03 01:11:54 +0000259char MPPassManager::ID = 0;
Devang Patel10c2ca62006-12-12 22:47:13 +0000260//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000261// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000262//
Devang Patel09f162c2007-05-01 21:15:47 +0000263
Devang Patel67d6a5e2006-12-19 19:46:59 +0000264/// PassManagerImpl manages MPPassManagers
265class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000266 public PMDataManager,
267 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000268
269public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000270 static char ID;
Dan Gohman13ab93e2007-10-08 15:08:41 +0000271 explicit PassManagerImpl(int Depth) :
Dan Gohmana79db302008-09-04 17:05:41 +0000272 Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000273
Devang Patel376fefa2006-11-08 10:29:57 +0000274 /// add - Add a pass to the queue of passes to run. This passes ownership of
275 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
276 /// will be destroyed as well, so there is no need to delete the pass. This
277 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000278 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000279 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000280 }
Devang Patel376fefa2006-11-08 10:29:57 +0000281
282 /// run - Execute all of the passes scheduled for execution. Keep track of
283 /// whether any of the passes modifies the module, and if so, return true.
284 bool run(Module &M);
285
Devang Patelf9d96b92006-12-07 19:57:52 +0000286 /// Pass Manager itself does not invalidate any analysis info.
287 void getAnalysisUsage(AnalysisUsage &Info) const {
288 Info.setPreservesAll();
289 }
290
Devang Patelabcd1d32006-12-07 21:27:23 +0000291 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000292
Devang Patelfa971cd2006-12-08 23:57:43 +0000293 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000294
295 // P is a immutable pass and it will be managed by this
296 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000297 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000298 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000299 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000300 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000301 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000302 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000303 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000304 }
Devang Patel0f080042007-01-12 17:23:48 +0000305
Devang Patelabcd1d32006-12-07 21:27:23 +0000306 }
307
Devang Patel67d6a5e2006-12-19 19:46:59 +0000308 MPPassManager *getContainedManager(unsigned N) {
309 assert ( N < PassManagers.size() && "Pass number out of range!");
310 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
311 return MP;
312 }
313
Devang Patel376fefa2006-11-08 10:29:57 +0000314};
315
Devang Patel8c78a0b2007-05-03 01:11:54 +0000316char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000317} // End of llvm namespace
318
319namespace {
320
321//===----------------------------------------------------------------------===//
322// TimingInfo Class - This class is used to calculate information about the
323// amount of time each pass takes to execute. This only happens when
324// -time-passes is enabled on the command line.
325//
326
327class VISIBILITY_HIDDEN TimingInfo {
328 std::map<Pass*, Timer> TimingData;
329 TimerGroup TG;
330
331public:
332 // Use 'create' member to get this.
333 TimingInfo() : TG("... Pass execution timing report ...") {}
334
335 // TimingDtor - Print out information about timing information
336 ~TimingInfo() {
337 // Delete all of the timers...
338 TimingData.clear();
339 // TimerGroup is deleted next, printing the report.
340 }
341
342 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
343 // to a non null value (if the -time-passes option is enabled) or it leaves it
344 // null. It may be called multiple times.
345 static void createTheTimeInfo();
346
347 void passStarted(Pass *P) {
348
349 if (dynamic_cast<PMDataManager *>(P))
350 return;
351
352 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
353 if (I == TimingData.end())
354 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
355 I->second.startTimer();
356 }
357 void passEnded(Pass *P) {
358
359 if (dynamic_cast<PMDataManager *>(P))
360 return;
361
362 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
363 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
364 I->second.stopTimer();
365 }
366};
367
Devang Patel1c3633e2007-01-29 23:10:37 +0000368} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000369
Dan Gohmand78c4002008-05-13 00:00:25 +0000370static TimingInfo *TheTimeInfo;
371
Devang Patela1514cb2006-12-07 19:39:39 +0000372//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000373// PMTopLevelManager implementation
374
Devang Patel4268fc02007-01-16 02:00:38 +0000375/// Initialize top level manager. Create first pass manager.
376PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
377
378 if (t == TLM_Pass) {
379 MPPassManager *MPP = new MPPassManager(1);
380 MPP->setTopLevelManager(this);
381 addPassManager(MPP);
382 activeStack.push(MPP);
383 }
384 else if (t == TLM_Function) {
385 FPPassManager *FPP = new FPPassManager(1);
386 FPP->setTopLevelManager(this);
387 addPassManager(FPP);
388 activeStack.push(FPP);
389 }
390}
391
Devang Patelafb1f3622006-12-12 22:35:25 +0000392/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000393void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000394 Pass *P) {
395
Devang Patel8adae862007-07-20 18:04:54 +0000396 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000397 E = AnalysisPasses.end(); I != E; ++I) {
398 Pass *AP = *I;
399 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000400
401 if (P == AP)
402 continue;
403
Devang Patelafb1f3622006-12-12 22:35:25 +0000404 // If AP is the last user of other passes then make P last user of
405 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000406 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000407 LUE = LastUser.end(); LUI != LUE; ++LUI) {
408 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000409 // DenseMap iterator is not invalidated here because
410 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000411 LastUser[LUI->first] = P;
412 }
413 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000414}
415
416/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000417void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000418 Pass *P) {
419 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
420 InversedLastUser.find(P);
421 if (DMI == InversedLastUser.end())
422 return;
423
424 SmallPtrSet<Pass *, 8> &LU = DMI->second;
425 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
426 E = LU.end(); I != E; ++I) {
427 LastUses.push_back(*I);
428 }
429
Devang Patelafb1f3622006-12-12 22:35:25 +0000430}
431
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000432AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
433 AnalysisUsage *AnUsage = NULL;
434 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
435 if (DMI != AnUsageMap.end())
436 AnUsage = DMI->second;
437 else {
438 AnUsage = new AnalysisUsage();
439 P->getAnalysisUsage(*AnUsage);
440 AnUsageMap[P] = AnUsage;
441 }
442 return AnUsage;
443}
444
Devang Patelafb1f3622006-12-12 22:35:25 +0000445/// Schedule pass P for execution. Make sure that passes required by
446/// P are run before P is run. Update analysis info maintained by
447/// the manager. Remove dead passes. This is a recursive function.
448void PMTopLevelManager::schedulePass(Pass *P) {
449
Devang Patel3312f752007-01-16 21:43:18 +0000450 // TODO : Allocate function manager for this pass, other wise required set
451 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000452
Devang Pateld74ede72007-03-06 01:06:16 +0000453 // Give pass a chance to prepare the stage.
454 P->preparePassManager(activeStack);
455
Devang Patel864970e2008-03-18 00:39:19 +0000456 // If P is an analysis pass and it is available then do not
457 // generate the analysis again. Stale analysis info should not be
458 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000459 if (P->getPassInfo() &&
460 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
Devang Patelaf75ab82008-03-19 00:48:41 +0000461 return;
Devang Patel864970e2008-03-18 00:39:19 +0000462
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000463 AnalysisUsage *AnUsage = findAnalysisUsage(P);
464
Devang Patelfdee7032008-08-14 23:07:48 +0000465 bool checkAnalysis = true;
466 while (checkAnalysis) {
467 checkAnalysis = false;
468
469 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
470 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
471 E = RequiredSet.end(); I != E; ++I) {
472
473 Pass *AnalysisPass = findAnalysisPass(*I);
474 if (!AnalysisPass) {
475 AnalysisPass = (*I)->createPass();
476 if (P->getPotentialPassManagerType () ==
477 AnalysisPass->getPotentialPassManagerType())
478 // Schedule analysis pass that is managed by the same pass manager.
479 schedulePass(AnalysisPass);
480 else if (P->getPotentialPassManagerType () >
481 AnalysisPass->getPotentialPassManagerType()) {
482 // Schedule analysis pass that is managed by a new manager.
483 schedulePass(AnalysisPass);
484 // Recheck analysis passes to ensure that required analysises that
485 // are already checked are still available.
486 checkAnalysis = true;
487 }
488 else
489 // Do not schedule this analysis. Lower level analsyis
490 // passes are run on the fly.
491 delete AnalysisPass;
492 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000493 }
494 }
495
496 // Now all required passes are available.
497 addTopLevelPass(P);
498}
499
500/// Find the pass that implements Analysis AID. Search immutable
501/// passes and all pass managers. If desired pass is not found
502/// then return NULL.
503Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
504
505 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000506 // Check pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000507 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000508 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000509 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000510 P = PMD->findAnalysisPass(AID, false);
511 }
512
513 // Check other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000514 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000515 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
516 P = (*I)->findAnalysisPass(AID, false);
517
Devang Patel0d29ae02008-08-12 15:44:31 +0000518 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000519 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
520 const PassInfo *PI = (*I)->getPassInfo();
521 if (PI == AID)
522 P = *I;
523
524 // If Pass not found then check the interfaces implemented by Immutable Pass
525 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000526 const std::vector<const PassInfo*> &ImmPI =
527 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000528 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
529 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000530 }
531 }
532
Devang Patelafb1f3622006-12-12 22:35:25 +0000533 return P;
534}
535
Devang Pateleda56172006-12-12 23:34:33 +0000536// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000537void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000538
Devang Patelfd4184322007-01-17 20:33:36 +0000539 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000540 return;
541
Devang Pateleda56172006-12-12 23:34:33 +0000542 // Print out the immutable passes
543 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
544 ImmutablePasses[i]->dumpPassStructure(0);
545 }
546
Dan Gohman73caf5f2008-03-13 01:48:32 +0000547 // Every class that derives from PMDataManager also derives from Pass
548 // (sometimes indirectly), but there's no inheritance relationship
549 // between PMDataManager and Pass, so we have to dynamic_cast to get
550 // from a PMDataManager* to a Pass*.
Devang Patel0d29ae02008-08-12 15:44:31 +0000551 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000552 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000553 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000554}
555
Devang Patel991aeba2006-12-15 20:13:01 +0000556void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000557
Devang Patelfd4184322007-01-17 20:33:36 +0000558 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000559 return;
560
561 cerr << "Pass Arguments: ";
Devang Patel0d29ae02008-08-12 15:44:31 +0000562 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000563 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000564 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000565 PMD->dumpPassArguments();
566 }
567 cerr << "\n";
568}
569
Devang Patele3068402006-12-21 00:16:50 +0000570void PMTopLevelManager::initializeAllAnalysisInfo() {
571
Devang Patel0d29ae02008-08-12 15:44:31 +0000572 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000573 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000574 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000575 PMD->initializeAnalysisInfo();
576 }
577
578 // Initailize other pass managers
Devang Patel0d29ae02008-08-12 15:44:31 +0000579 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000580 E = IndirectPassManagers.end(); I != E; ++I)
581 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000582
583 for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
584 DME = LastUser.end(); DMI != DME; ++DMI) {
585 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
586 InversedLastUser.find(DMI->second);
587 if (InvDMI != InversedLastUser.end()) {
588 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
589 L.insert(DMI->first);
590 } else {
591 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
592 InversedLastUser[DMI->second] = L;
593 }
594 }
Devang Patele3068402006-12-21 00:16:50 +0000595}
596
Devang Patele7599552007-01-12 18:52:44 +0000597/// Destructor
598PMTopLevelManager::~PMTopLevelManager() {
Devang Patel0d29ae02008-08-12 15:44:31 +0000599 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000600 E = PassManagers.end(); I != E; ++I)
601 delete *I;
602
Devang Patel0d29ae02008-08-12 15:44:31 +0000603 for (SmallVector<ImmutablePass *, 8>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000604 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
605 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000606
607 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
608 DME = AnUsageMap.end(); DMI != DME; ++DMI) {
609 AnalysisUsage *AU = DMI->second;
610 delete AU;
611 }
612
Devang Patele7599552007-01-12 18:52:44 +0000613}
614
Devang Patelafb1f3622006-12-12 22:35:25 +0000615//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000616// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000617
Devang Patel643676c2006-11-11 01:10:19 +0000618/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000619void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000620
Devang Patel643676c2006-11-11 01:10:19 +0000621 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000622 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000623
Devang Patele9976aa2006-12-07 19:33:53 +0000624 //This pass is the current implementation of all of the interfaces it
625 //implements as well.
626 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
627 for (unsigned i = 0, e = II.size(); i != e; ++i)
628 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000629 }
630}
631
Devang Patel9d9fc902007-03-06 17:52:53 +0000632// Return true if P preserves high level analysis used by other
633// passes managed by this manager
634bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
635
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000636 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel9d9fc902007-03-06 17:52:53 +0000637
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000638 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000639 return true;
640
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000641 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel0d29ae02008-08-12 15:44:31 +0000642 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000643 E = HigherLevelAnalysis.end(); I != E; ++I) {
644 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000645 if (!dynamic_cast<ImmutablePass*>(P1) &&
646 std::find(PreservedSet.begin(), PreservedSet.end(),
647 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000648 PreservedSet.end())
649 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000650 }
651
652 return true;
653}
654
Chris Lattner02eb94c2008-08-07 07:34:50 +0000655/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000656void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000657 // Don't do this unless assertions are enabled.
658#ifdef NDEBUG
659 return;
660#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000661 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
662 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000663
Devang Patelef432532007-07-19 05:36:09 +0000664 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000665 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000666 E = PreservedSet.end(); I != E; ++I) {
667 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000668 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000669 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000670 }
Devang Patela273d1c2007-07-19 18:02:32 +0000671}
672
Devang Patel9dbe4d12008-07-01 17:44:24 +0000673/// verifyDomInfo - Verify dominator information if it is available.
674void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
675
676 if (!VerifyDomInfo || !P.getResolver())
677 return;
678
679 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
680 if (!DT)
681 return;
682
683 DominatorTree OtherDT;
684 OtherDT.getBase().recalculate(F);
685 if (DT->compare(OtherDT)) {
686 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000687 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000688 cerr << "----- Valid -----\n";
689 OtherDT.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000690 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000691 DT->dump();
692 assert (0 && "Invalid dominator info");
693 }
694
695 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
696 if (!DF)
697 return;
698
699 DominanceFrontier OtherDF;
700 std::vector<BasicBlock*> DTRoots = DT->getRoots();
701 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
702 if (DF->compare(OtherDF)) {
703 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000704 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000705 cerr << "----- Valid -----\n";
706 OtherDF.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000707 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000708 DF->dump();
709 assert (0 && "Invalid dominator info");
710 }
711}
712
Devang Patel67c79a42008-07-01 19:50:56 +0000713/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000714void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000715 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
716 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000717 return;
718
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000719 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000720 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000721 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000722 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000723 if (!dynamic_cast<ImmutablePass*>(Info->second)
724 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000725 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000726 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000727 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000728 if (PassDebugging >= Details) {
729 Pass *S = Info->second;
Dan Gohman15269852008-07-09 00:50:40 +0000730 cerr << " -- '" << P->getPassName() << "' is not preserving '";
731 cerr << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000732 }
733 }
Devang Patel349170f2006-11-11 01:24:55 +0000734 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000735
736 // Check inherited analysis also. If P is not preserving analysis
737 // provided by parent manager then remove it here.
738 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
739
740 if (!InheritedAnalysis[Index])
741 continue;
742
743 for (std::map<AnalysisID, Pass*>::iterator
744 I = InheritedAnalysis[Index]->begin(),
745 E = InheritedAnalysis[Index]->end(); I != E; ) {
746 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000747 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
748 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000749 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000750 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000751 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000752 }
753 }
Devang Patelf68a3492006-11-07 22:35:17 +0000754}
755
Devang Patelca189262006-11-14 03:05:08 +0000756/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000757void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000758 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000759
Devang Patel8adae862007-07-20 18:04:54 +0000760 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000761
Devang Patel2ff44922007-04-16 20:39:59 +0000762 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000763 if (!TPM)
764 return;
765
Devang Patel17ad0962006-12-08 00:37:52 +0000766 TPM->collectLastUses(DeadPasses, P);
767
Devang Patel656a9172008-06-06 17:50:36 +0000768 if (PassDebugging >= Details && !DeadPasses.empty()) {
Dan Gohman15269852008-07-09 00:50:40 +0000769 cerr << " -*- '" << P->getPassName();
770 cerr << "' is the last user of following pass instances.";
Devang Patel656a9172008-06-06 17:50:36 +0000771 cerr << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000772 }
773
Devang Patel8adae862007-07-20 18:04:54 +0000774 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000775 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000776
Devang Patel003a5592007-03-05 20:01:30 +0000777 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000778
Devang Patel7ebf09d2007-03-05 18:20:51 +0000779 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000780 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000781 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000782
Devang Patel17ad0962006-12-08 00:37:52 +0000783 std::map<AnalysisID, Pass*>::iterator Pos =
784 AvailableAnalysis.find((*I)->getPassInfo());
785
Devang Patel475c4532006-12-08 00:59:05 +0000786 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000787 if (Pos != AvailableAnalysis.end())
788 AvailableAnalysis.erase(Pos);
789 }
Devang Patelca189262006-11-14 03:05:08 +0000790}
791
Devang Patel8f677ce2006-12-07 18:47:25 +0000792/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000793/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000794void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000795 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000796
Devang Pateld440cd92006-12-08 23:53:00 +0000797 // This manager is going to manage pass P. Set up analysis resolver
798 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000799 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000800 P->setResolver(AR);
801
Devang Patelec2b9a72007-03-05 22:57:49 +0000802 // If a FunctionPass F is the last user of ModulePass info M
803 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000804 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000805
Devang Patel90b05e02006-11-11 02:04:19 +0000806 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000807
808 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000809 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000810 SmallVector<Pass *, 8> RequiredPasses;
811 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
812
Devang Patelbc03f132006-12-07 23:55:10 +0000813 unsigned PDepth = this->getDepth();
814
Devang Patele64d3052007-04-16 20:12:57 +0000815 collectRequiredAnalysis(RequiredPasses,
816 ReqAnalysisNotAvailable, P);
817 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000818 E = RequiredPasses.end(); I != E; ++I) {
819 Pass *PRequired = *I;
820 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000821
Devang Patel9dbe4d12008-07-01 17:44:24 +0000822 assert (PRequired->getResolver() && "Analysis Resolver is not set");
Devang Patel64619be2006-12-09 00:07:38 +0000823 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
824 RDepth = DM.getDepth();
825
Devang Patelbc03f132006-12-07 23:55:10 +0000826 if (PDepth == RDepth)
827 LastUses.push_back(PRequired);
828 else if (PDepth > RDepth) {
829 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000830 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000831 // Keep track of higher level analysis used by this manager.
832 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000833 } else
834 assert (0 && "Unable to accomodate Required Pass");
835 }
836
Devang Patel39786a92007-01-15 20:31:54 +0000837 // Set P as P's last user until someone starts using P.
838 // However, if P is a Pass Manager then it does not need
839 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000840 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000841 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000842 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000843
Devang Patelec2b9a72007-03-05 22:57:49 +0000844 if (!TransferLastUses.empty()) {
845 Pass *My_PM = dynamic_cast<Pass *>(this);
846 TPM->setLastUser(TransferLastUses, My_PM);
847 TransferLastUses.clear();
848 }
849
Devang Patel69e9f6d2007-04-16 20:27:05 +0000850 // Now, take care of required analysises that are not available.
851 for (SmallVector<AnalysisID, 8>::iterator
852 I = ReqAnalysisNotAvailable.begin(),
853 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
854 Pass *AnalysisPass = (*I)->createPass();
855 this->addLowerLevelRequiredPass(P, AnalysisPass);
856 }
857
Devang Patel17bff0d2006-12-07 22:09:36 +0000858 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000859 // Remove the analysis not preserved by this pass
860 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000861 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000862 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000863
864 // Add pass
865 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000866}
867
Devang Patele64d3052007-04-16 20:12:57 +0000868
869/// Populate RP with analysis pass that are required by
870/// pass P and are available. Populate RP_NotAvail with analysis
871/// pass that are required by pass P but are not available.
872void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
873 SmallVector<AnalysisID, 8> &RP_NotAvail,
874 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000875 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
876 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000877 for (AnalysisUsage::VectorType::const_iterator
Devang Patel1d6267c2006-12-07 23:05:44 +0000878 I = RequiredSet.begin(), E = RequiredSet.end();
879 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000880 AnalysisID AID = *I;
881 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
882 RP.push_back(AnalysisPass);
883 else
884 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000885 }
Devang Patelf58183d2006-12-12 23:09:32 +0000886
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000887 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000888 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000889 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000890 AnalysisID AID = *I;
891 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
892 RP.push_back(AnalysisPass);
893 else
894 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000895 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000896}
897
Devang Patel07f4f582006-11-14 21:49:36 +0000898// All Required analyses should be available to the pass as it runs! Here
899// we fill in the AnalysisImpls member of the pass so that it can
900// successfully use the getAnalysis() method to retrieve the
901// implementations it needs.
902//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000903void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000904 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
905
Chris Lattnercbd160f2008-08-08 05:33:04 +0000906 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000907 I = AnUsage->getRequiredSet().begin(),
908 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000909 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000910 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000911 // This may be analysis pass that is initialized on the fly.
912 // If that is not the case then it will raise an assert when it is used.
913 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000914 AnalysisResolver *AR = P->getResolver();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000915 assert (AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000916 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000917 }
918}
919
Devang Patel640c5bb2006-12-08 22:30:11 +0000920/// Find the pass that implements Analysis AID. If desired pass is not found
921/// then return NULL.
922Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
923
924 // Check if AvailableAnalysis map has one entry.
925 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
926
927 if (I != AvailableAnalysis.end())
928 return I->second;
929
930 // Search Parents through TopLevelManager
931 if (SearchParent)
932 return TPM->findAnalysisPass(AID);
933
Devang Patel9d759b82006-12-09 00:09:12 +0000934 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000935}
936
Devang Patel991aeba2006-12-15 20:13:01 +0000937// Print list of passes that are last used by P.
938void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
939
Devang Patel8adae862007-07-20 18:04:54 +0000940 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000941
942 // If this is a on the fly manager then it does not have TPM.
943 if (!TPM)
944 return;
945
Devang Patel991aeba2006-12-15 20:13:01 +0000946 TPM->collectLastUses(LUses, P);
947
Devang Patel8adae862007-07-20 18:04:54 +0000948 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000949 E = LUses.end(); I != E; ++I) {
950 llvm::cerr << "--" << std::string(Offset*2, ' ');
951 (*I)->dumpPassStructure(0);
952 }
953}
954
955void PMDataManager::dumpPassArguments() const {
Devang Patel0d29ae02008-08-12 15:44:31 +0000956 for(SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000957 E = PassVector.end(); I != E; ++I) {
958 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
959 PMD->dumpPassArguments();
960 else
961 if (const PassInfo *PI = (*I)->getPassInfo())
962 if (!PI->isAnalysisGroup())
963 cerr << " -" << PI->getPassArgument();
964 }
965}
966
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000967void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
968 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000969 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000970 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000971 return;
972 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000973 switch (S1) {
974 case EXECUTION_MSG:
975 cerr << "Executing Pass '" << P->getPassName();
976 break;
977 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000978 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000979 break;
980 case FREEING_MSG:
981 cerr << " Freeing Pass '" << P->getPassName();
982 break;
983 default:
984 break;
985 }
986 switch (S2) {
987 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000988 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000989 break;
990 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000991 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000992 break;
993 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000994 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000995 break;
996 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000997 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000998 break;
999 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +00001000 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001001 break;
1002 default:
1003 break;
1004 }
Devang Patel991aeba2006-12-15 20:13:01 +00001005}
1006
Chris Lattner4c493d92008-08-08 15:14:09 +00001007void PMDataManager::dumpRequiredSet(const Pass *P)
1008 const {
1009 if (PassDebugging < Details)
1010 return;
1011
1012 AnalysisUsage analysisUsage;
1013 P->getAnalysisUsage(analysisUsage);
1014 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1015}
1016
1017void PMDataManager::dumpPreservedSet(const Pass *P)
1018 const {
1019 if (PassDebugging < Details)
1020 return;
1021
1022 AnalysisUsage analysisUsage;
1023 P->getAnalysisUsage(analysisUsage);
1024 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1025}
1026
1027void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
Chris Lattnercbd160f2008-08-08 05:33:04 +00001028 const AnalysisUsage::VectorType &Set)
Devang Patel991aeba2006-12-15 20:13:01 +00001029 const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001030 assert(PassDebugging >= Details);
1031 if (Set.empty())
1032 return;
1033 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1034 for (unsigned i = 0; i != Set.size(); ++i) {
1035 if (i) cerr << ",";
1036 cerr << " " << Set[i]->getPassName();
1037 }
1038 cerr << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001039}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001040
Devang Patel004937b2007-07-27 20:06:09 +00001041/// Add RequiredPass into list of lower level passes required by pass P.
1042/// RequiredPass is run on the fly by Pass Manager when P requests it
1043/// through getAnalysis interface.
1044/// This should be handled by specific pass manager.
1045void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1046 if (TPM) {
1047 TPM->dumpArguments();
1048 TPM->dumpPasses();
1049 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001050
1051 // Module Level pass may required Function Level analysis info
1052 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1053 // to provide this on demand. In that case, in Pass manager terminology,
1054 // module level pass is requiring lower level analysis info managed by
1055 // lower level pass manager.
1056
1057 // When Pass manager is not able to order required analysis info, Pass manager
1058 // checks whether any lower level manager will be able to provide this
1059 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001060#ifndef NDEBUG
Dan Gohman15269852008-07-09 00:50:40 +00001061 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1062 cerr << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001063#endif
1064 assert (0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001065}
1066
Devang Patele7599552007-01-12 18:52:44 +00001067// Destructor
1068PMDataManager::~PMDataManager() {
1069
Devang Patel0d29ae02008-08-12 15:44:31 +00001070 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001071 E = PassVector.end(); I != E; ++I)
1072 delete *I;
1073
Devang Patele7599552007-01-12 18:52:44 +00001074}
1075
Devang Patel9bdf7d42006-12-08 23:28:54 +00001076//===----------------------------------------------------------------------===//
1077// NOTE: Is this the right place to define this method ?
1078// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001079Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001080 return PM.findAnalysisPass(ID, dir);
1081}
1082
Devang Patel92942812007-04-16 20:56:24 +00001083Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1084 Function &F) {
1085 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1086}
1087
Devang Patela1514cb2006-12-07 19:39:39 +00001088//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001089// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001090
Devang Patel6e5a1132006-11-07 21:31:57 +00001091/// Execute all of the passes scheduled for execution by invoking
1092/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1093/// the function, and if so, return true.
1094bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001095BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001096
Reid Spencer5301e7c2007-01-30 20:08:39 +00001097 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001098 return false;
1099
Devang Patele9585592006-12-08 01:38:28 +00001100 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001101
Devang Patel6e5a1132006-11-07 21:31:57 +00001102 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001103 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1104 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001105
Devang Pateld305c402007-08-10 18:29:32 +00001106 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001107 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001108
Devang Patelabfbe3b2006-12-16 00:56:26 +00001109 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001110
Devang Patelabfbe3b2006-12-16 00:56:26 +00001111 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001112 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001113 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001114
Devang Patel003a5592007-03-05 20:01:30 +00001115 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001116 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1117 I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001118 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001119
Devang Patela273d1c2007-07-19 18:02:32 +00001120 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001121 removeNotPreservedAnalysis(BP);
1122 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001123 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001124 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001125
Devang Patel56d48ec2006-12-15 22:57:49 +00001126 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001127}
1128
Devang Patel475c4532006-12-08 00:59:05 +00001129// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001130inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001131 bool Changed = false;
1132
Devang Patelabfbe3b2006-12-16 00:56:26 +00001133 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1134 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001135 Changed |= BP->doInitialization(M);
1136 }
1137
1138 return Changed;
1139}
1140
Devang Patel67d6a5e2006-12-19 19:46:59 +00001141inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001142 bool Changed = false;
1143
Devang Patelabfbe3b2006-12-16 00:56:26 +00001144 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1145 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001146 Changed |= BP->doFinalization(M);
1147 }
1148
1149 return Changed;
1150}
1151
Devang Patel67d6a5e2006-12-19 19:46:59 +00001152inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001153 bool Changed = false;
1154
Devang Patelabfbe3b2006-12-16 00:56:26 +00001155 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1156 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001157 Changed |= BP->doInitialization(F);
1158 }
1159
1160 return Changed;
1161}
1162
Devang Patel67d6a5e2006-12-19 19:46:59 +00001163inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001164 bool Changed = false;
1165
Devang Patelabfbe3b2006-12-16 00:56:26 +00001166 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1167 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001168 Changed |= BP->doFinalization(F);
1169 }
1170
1171 return Changed;
1172}
1173
1174
Devang Patela1514cb2006-12-07 19:39:39 +00001175//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001176// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001177
Devang Patel4e12f862006-11-08 10:44:40 +00001178/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001179FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001180 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001181 // FPM is the top level manager.
1182 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001183
Dan Gohman565df952008-03-13 02:08:36 +00001184 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001185 FPM->setResolver(AR);
1186
Devang Patel1f653682006-12-08 18:57:16 +00001187 MP = P;
1188}
1189
Devang Patelb67904d2006-12-13 02:36:01 +00001190FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001191 delete FPM;
1192}
1193
Devang Patel4e12f862006-11-08 10:44:40 +00001194/// add - Add a pass to the queue of passes to run. This passes
1195/// ownership of the Pass to the PassManager. When the
1196/// PassManager_X is destroyed, the pass will be destroyed as well, so
1197/// there is no need to delete the pass. (TODO delete passes.)
1198/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001199void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001200 FPM->add(P);
1201}
1202
Devang Patel9f3083e2006-11-15 19:39:54 +00001203/// run - Execute all of the passes scheduled for execution. Keep
1204/// track of whether any of the passes modifies the function, and if
1205/// so, return true.
1206///
Devang Patelb67904d2006-12-13 02:36:01 +00001207bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001208 std::string errstr;
1209 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001210 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001211 abort();
1212 }
Devang Patel272908d2006-12-08 22:57:48 +00001213 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001214}
1215
1216
Devang Patelff631ae2006-11-15 01:27:05 +00001217/// doInitialization - Run all of the initializers for the function passes.
1218///
Devang Patelb67904d2006-12-13 02:36:01 +00001219bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001220 return FPM->doInitialization(*MP->getModule());
1221}
1222
Dan Gohmane6656eb2007-07-30 14:51:13 +00001223/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001224///
Devang Patelb67904d2006-12-13 02:36:01 +00001225bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001226 return FPM->doFinalization(*MP->getModule());
1227}
1228
Devang Patela1514cb2006-12-07 19:39:39 +00001229//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001230// FunctionPassManagerImpl implementation
1231//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001232inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1233 bool Changed = false;
1234
1235 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1236 FPPassManager *FP = getContainedManager(Index);
1237 Changed |= FP->doInitialization(M);
1238 }
1239
1240 return Changed;
1241}
1242
1243inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1244 bool Changed = false;
1245
1246 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1247 FPPassManager *FP = getContainedManager(Index);
1248 Changed |= FP->doFinalization(M);
1249 }
1250
1251 return Changed;
1252}
1253
1254// Execute all the passes managed by this top level manager.
1255// Return true if any function is modified by a pass.
1256bool FunctionPassManagerImpl::run(Function &F) {
1257
1258 bool Changed = false;
1259
1260 TimingInfo::createTheTimeInfo();
1261
1262 dumpArguments();
1263 dumpPasses();
1264
Devang Patele3068402006-12-21 00:16:50 +00001265 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001266 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1267 FPPassManager *FP = getContainedManager(Index);
1268 Changed |= FP->runOnFunction(F);
1269 }
1270 return Changed;
1271}
1272
1273//===----------------------------------------------------------------------===//
1274// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001275
Devang Patel8c78a0b2007-05-03 01:11:54 +00001276char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001277/// Print passes managed by this manager
1278void FPPassManager::dumpPassStructure(unsigned Offset) {
1279 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1280 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1281 FunctionPass *FP = getContainedPass(Index);
1282 FP->dumpPassStructure(Offset + 1);
1283 dumpLastUses(FP, Offset+1);
1284 }
1285}
1286
1287
Devang Patel0c2012f2006-11-07 21:49:50 +00001288/// Execute all of the passes scheduled for execution by invoking
1289/// runOnFunction method. Keep track of whether any of the passes modifies
1290/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001291bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001292
1293 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001294
Reid Spencer5301e7c2007-01-30 20:08:39 +00001295 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001296 return false;
Devang Patelcbbf2912008-03-20 01:09:53 +00001297
1298 // Collect inherited analysis from Module level pass manager.
1299 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001300
Devang Patelabfbe3b2006-12-16 00:56:26 +00001301 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1302 FunctionPass *FP = getContainedPass(Index);
1303
Devang Pateld305c402007-08-10 18:29:32 +00001304 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001305 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001306
Devang Patelabfbe3b2006-12-16 00:56:26 +00001307 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001308
Devang Patelabfbe3b2006-12-16 00:56:26 +00001309 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001310 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001311 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001312
Devang Patel003a5592007-03-05 20:01:30 +00001313 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001314 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001315 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001316
Devang Patela273d1c2007-07-19 18:02:32 +00001317 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001318 removeNotPreservedAnalysis(FP);
1319 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001320 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001321
Devang Patel67c79a42008-07-01 19:50:56 +00001322 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001323 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001324 }
1325 return Changed;
1326}
1327
Devang Patel67d6a5e2006-12-19 19:46:59 +00001328bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001329
Devang Patel67d6a5e2006-12-19 19:46:59 +00001330 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001331
1332 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1333 this->runOnFunction(*I);
1334
1335 return Changed |= doFinalization(M);
1336}
1337
1338inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001339 bool Changed = false;
1340
Devang Patelabfbe3b2006-12-16 00:56:26 +00001341 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1342 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001343 Changed |= FP->doInitialization(M);
1344 }
1345
1346 return Changed;
1347}
1348
Devang Patel67d6a5e2006-12-19 19:46:59 +00001349inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001350 bool Changed = false;
1351
Devang Patelabfbe3b2006-12-16 00:56:26 +00001352 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1353 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001354 Changed |= FP->doFinalization(M);
1355 }
1356
Devang Patelff631ae2006-11-15 01:27:05 +00001357 return Changed;
1358}
1359
Devang Patela1514cb2006-12-07 19:39:39 +00001360//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001361// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001362
Devang Patel05e1a972006-11-07 22:03:15 +00001363/// Execute all of the passes scheduled for execution by invoking
1364/// runOnModule method. Keep track of whether any of the passes modifies
1365/// the module, and if so, return true.
1366bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001367MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001368 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001369
Devang Patelabfbe3b2006-12-16 00:56:26 +00001370 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1371 ModulePass *MP = getContainedPass(Index);
1372
Dan Gohman929391a2008-01-29 12:09:55 +00001373 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1374 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001375 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001376
Devang Patelabfbe3b2006-12-16 00:56:26 +00001377 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001378
Devang Patelabfbe3b2006-12-16 00:56:26 +00001379 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001380 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001381 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001382
Devang Patel003a5592007-03-05 20:01:30 +00001383 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001384 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1385 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001386 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001387
Devang Patela273d1c2007-07-19 18:02:32 +00001388 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001389 removeNotPreservedAnalysis(MP);
1390 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001391 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001392 }
1393 return Changed;
1394}
1395
Devang Patele64d3052007-04-16 20:12:57 +00001396/// Add RequiredPass into list of lower level passes required by pass P.
1397/// RequiredPass is run on the fly by Pass Manager when P requests it
1398/// through getAnalysis interface.
1399void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1400
1401 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1402 && "Unable to handle Pass that requires lower level Analysis pass");
1403 assert ((P->getPotentialPassManagerType() <
1404 RequiredPass->getPotentialPassManagerType())
1405 && "Unable to handle Pass that requires lower level Analysis pass");
1406
Devang Patel68f72b12007-04-26 17:50:19 +00001407 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001408 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001409 FPP = new FunctionPassManagerImpl(0);
1410 // FPP is the top level manager.
1411 FPP->setTopLevelManager(FPP);
1412
Devang Patel69e9f6d2007-04-16 20:27:05 +00001413 OnTheFlyManagers[P] = FPP;
1414 }
Devang Patel68f72b12007-04-26 17:50:19 +00001415 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001416
Devang Patel68f72b12007-04-26 17:50:19 +00001417 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001418 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001419 LU.push_back(RequiredPass);
1420 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001421}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001422
1423/// Return function pass corresponding to PassInfo PI, that is
1424/// required by module pass MP. Instantiate analysis pass, by using
1425/// its runOnFunction() for function F.
1426Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1427 Function &F) {
1428 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001429 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001430 assert (FPP && "Unable to find on the fly pass");
1431
Devang Patel68f72b12007-04-26 17:50:19 +00001432 FPP->run(F);
1433 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001434}
1435
1436
Devang Patela1514cb2006-12-07 19:39:39 +00001437//===----------------------------------------------------------------------===//
1438// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001439//
Devang Patelc290c8a2006-11-07 22:23:34 +00001440/// run - Execute all of the passes scheduled for execution. Keep track of
1441/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001442bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001443
Devang Patelc290c8a2006-11-07 22:23:34 +00001444 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001445
Devang Patelb8817b92006-12-14 00:59:42 +00001446 TimingInfo::createTheTimeInfo();
1447
Devang Patelcfd70c42006-12-13 22:10:00 +00001448 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001449 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001450
Devang Patele3068402006-12-21 00:16:50 +00001451 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001452 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1453 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001454 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001455 }
1456 return Changed;
1457}
Devang Patel376fefa2006-11-08 10:29:57 +00001458
Devang Patela1514cb2006-12-07 19:39:39 +00001459//===----------------------------------------------------------------------===//
1460// PassManager implementation
1461
Devang Patel376fefa2006-11-08 10:29:57 +00001462/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001463PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001464 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001465 // PM is the top level manager
1466 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001467}
1468
Devang Patelb67904d2006-12-13 02:36:01 +00001469PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001470 delete PM;
1471}
1472
Devang Patel376fefa2006-11-08 10:29:57 +00001473/// add - Add a pass to the queue of passes to run. This passes ownership of
1474/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1475/// will be destroyed as well, so there is no need to delete the pass. This
1476/// implies that all passes MUST be allocated with 'new'.
1477void
Devang Patelb67904d2006-12-13 02:36:01 +00001478PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001479 PM->add(P);
1480}
1481
1482/// run - Execute all of the passes scheduled for execution. Keep track of
1483/// whether any of the passes modifies the module, and if so, return true.
1484bool
Devang Patelb67904d2006-12-13 02:36:01 +00001485PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001486 return PM->run(M);
1487}
1488
Devang Patelb8817b92006-12-14 00:59:42 +00001489//===----------------------------------------------------------------------===//
1490// TimingInfo Class - This class is used to calculate information about the
1491// amount of time each pass takes to execute. This only happens with
1492// -time-passes is enabled on the command line.
1493//
1494bool llvm::TimePassesIsEnabled = false;
1495static cl::opt<bool,true>
1496EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1497 cl::desc("Time each pass, printing elapsed time for each on exit"));
1498
1499// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1500// a non null value (if the -time-passes option is enabled) or it leaves it
1501// null. It may be called multiple times.
1502void TimingInfo::createTheTimeInfo() {
1503 if (!TimePassesIsEnabled || TheTimeInfo) return;
1504
1505 // Constructed the first time this is called, iff -time-passes is enabled.
1506 // This guarantees that the object will be constructed before static globals,
1507 // thus it will be destroyed before them.
1508 static ManagedStatic<TimingInfo> TTI;
1509 TheTimeInfo = &*TTI;
1510}
1511
Devang Patel1c3633e2007-01-29 23:10:37 +00001512/// If TimingInfo is enabled then start pass timer.
1513void StartPassTimer(Pass *P) {
1514 if (TheTimeInfo)
1515 TheTimeInfo->passStarted(P);
1516}
1517
1518/// If TimingInfo is enabled then stop pass timer.
1519void StopPassTimer(Pass *P) {
1520 if (TheTimeInfo)
1521 TheTimeInfo->passEnded(P);
1522}
1523
Devang Patel1c56a632007-01-08 19:29:38 +00001524//===----------------------------------------------------------------------===//
1525// PMStack implementation
1526//
Devang Patelad98d232007-01-11 22:15:30 +00001527
Devang Patel1c56a632007-01-08 19:29:38 +00001528// Pop Pass Manager from the stack and clear its analysis info.
1529void PMStack::pop() {
1530
1531 PMDataManager *Top = this->top();
1532 Top->initializeAnalysisInfo();
1533
1534 S.pop_back();
1535}
1536
1537// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001538void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001539
Devang Patel15701b52007-01-11 00:19:00 +00001540 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001541 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001542
Devang Patel15701b52007-01-11 00:19:00 +00001543 if (this->empty()) {
1544 Top = PM;
1545 }
1546 else {
1547 Top = this->top();
1548 PMTopLevelManager *TPM = Top->getTopLevelManager();
1549
1550 assert (TPM && "Unable to find top level manager");
1551 TPM->addIndirectPassManager(PM);
1552 PM->setTopLevelManager(TPM);
1553 }
1554
Devang Patel15701b52007-01-11 00:19:00 +00001555 S.push_back(PM);
1556}
1557
1558// Dump content of the pass manager stack.
1559void PMStack::dump() {
1560 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1561 E = S.end(); I != E; ++I) {
1562 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001563 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001564 }
1565 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001566 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001567}
1568
Devang Patel1c56a632007-01-08 19:29:38 +00001569/// Find appropriate Module Pass Manager in the PM Stack and
1570/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001571void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001572 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001573
Devang Patel1c56a632007-01-08 19:29:38 +00001574 // Find Module Pass Manager
1575 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001576 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1577 if (TopPMType == PreferredType)
1578 break; // We found desired pass manager
1579 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001580 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001581 else
1582 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001583 }
1584
Devang Patel23f8aa92007-01-17 21:19:23 +00001585 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001586}
1587
Devang Patel3312f752007-01-16 21:43:18 +00001588/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1589/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001590void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001591 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001592
Devang Patela3286902008-09-09 17:56:50 +00001593 // Find Module Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001594 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001595 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1596 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001597 else
Devang Patel3312f752007-01-16 21:43:18 +00001598 break;
1599 }
1600 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1601
1602 // Create new Function Pass Manager
1603 if (!FPP) {
1604 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1605 PMDataManager *PMD = PMS.top();
1606
1607 // [1] Create new Function Pass Manager
1608 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001609 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001610
1611 // [2] Set up new manager's top level manager
1612 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1613 TPM->addIndirectPassManager(FPP);
1614
1615 // [3] Assign manager to manage this new manager. This may create
1616 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001617 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001618
1619 // [4] Push new manager into PMS
1620 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001621 }
1622
Devang Patel3312f752007-01-16 21:43:18 +00001623 // Assign FPP as the manager of this pass.
1624 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001625}
1626
Devang Patel3312f752007-01-16 21:43:18 +00001627/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001628/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001629void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001630 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001631
1632 BBPassManager *BBP = NULL;
1633
Devang Patel15701b52007-01-11 00:19:00 +00001634 // Basic Pass Manager is a leaf pass manager. It does not handle
1635 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001636 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001637 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001638
Devang Patel3312f752007-01-16 21:43:18 +00001639 // If leaf manager is not Basic Block Pass manager then create new
1640 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001641
Devang Patel3312f752007-01-16 21:43:18 +00001642 if (!BBP) {
1643 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1644 PMDataManager *PMD = PMS.top();
1645
1646 // [1] Create new Basic Block Manager
1647 BBP = new BBPassManager(PMD->getDepth() + 1);
1648
1649 // [2] Set up new manager's top level manager
1650 // Basic Block Pass Manager does not live by itself
1651 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1652 TPM->addIndirectPassManager(BBP);
1653
Devang Patel15701b52007-01-11 00:19:00 +00001654 // [3] Assign manager to manage this new manager. This may create
1655 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001656 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001657
Devang Patel3312f752007-01-16 21:43:18 +00001658 // [4] Push new manager into PMS
1659 PMS.push(BBP);
1660 }
Devang Patel1c56a632007-01-08 19:29:38 +00001661
Devang Patel3312f752007-01-16 21:43:18 +00001662 // Assign BBP as the manager of this pass.
1663 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001664}
1665
Dan Gohmand3a20c92008-03-11 16:41:42 +00001666PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001667
1668/*===-- C Bindings --------------------------------------------------------===*/
1669
1670LLVMPassManagerRef LLVMCreatePassManager() {
1671 return wrap(new PassManager());
1672}
1673
1674LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1675 return wrap(new FunctionPassManager(unwrap(P)));
1676}
1677
1678int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1679 return unwrap<PassManager>(PM)->run(*unwrap(M));
1680}
1681
1682int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1683 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1684}
1685
1686int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1687 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1688}
1689
1690int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1691 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1692}
1693
1694void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1695 delete unwrap(PM);
1696}