blob: ed0dde03a187ceacde5c0f4c56d5cab07b42f84b [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)
Devang Patel09f162c2007-05-01 21:15:47 +000076 : PMDataManager(Depth), FunctionPass((intptr_t)&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) :
Devang Patel09f162c2007-05-01 21:15:47 +0000132 Pass((intptr_t)&ID), PMDataManager(Depth),
133 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) :
196 Pass((intptr_t)&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) :
272 Pass((intptr_t)&ID), PMDataManager(Depth),
273 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000274
Devang Patel376fefa2006-11-08 10:29:57 +0000275 /// add - Add a pass to the queue of passes to run. This passes ownership of
276 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
277 /// will be destroyed as well, so there is no need to delete the pass. This
278 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000279 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000280 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000281 }
Devang Patel376fefa2006-11-08 10:29:57 +0000282
283 /// run - Execute all of the passes scheduled for execution. Keep track of
284 /// whether any of the passes modifies the module, and if so, return true.
285 bool run(Module &M);
286
Devang Patelf9d96b92006-12-07 19:57:52 +0000287 /// Pass Manager itself does not invalidate any analysis info.
288 void getAnalysisUsage(AnalysisUsage &Info) const {
289 Info.setPreservesAll();
290 }
291
Devang Patelabcd1d32006-12-07 21:27:23 +0000292 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000293
Devang Patelfa971cd2006-12-08 23:57:43 +0000294 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000295
296 // P is a immutable pass and it will be managed by this
297 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000298 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000299 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000300 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000301 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000302 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000303 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000304 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000305 }
Devang Patel0f080042007-01-12 17:23:48 +0000306
Devang Patelabcd1d32006-12-07 21:27:23 +0000307 }
308
Devang Patel67d6a5e2006-12-19 19:46:59 +0000309 MPPassManager *getContainedManager(unsigned N) {
310 assert ( N < PassManagers.size() && "Pass number out of range!");
311 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
312 return MP;
313 }
314
Devang Patel376fefa2006-11-08 10:29:57 +0000315};
316
Devang Patel8c78a0b2007-05-03 01:11:54 +0000317char PassManagerImpl::ID = 0;
Devang Patel1c3633e2007-01-29 23:10:37 +0000318} // End of llvm namespace
319
320namespace {
321
322//===----------------------------------------------------------------------===//
323// TimingInfo Class - This class is used to calculate information about the
324// amount of time each pass takes to execute. This only happens when
325// -time-passes is enabled on the command line.
326//
327
328class VISIBILITY_HIDDEN TimingInfo {
329 std::map<Pass*, Timer> TimingData;
330 TimerGroup TG;
331
332public:
333 // Use 'create' member to get this.
334 TimingInfo() : TG("... Pass execution timing report ...") {}
335
336 // TimingDtor - Print out information about timing information
337 ~TimingInfo() {
338 // Delete all of the timers...
339 TimingData.clear();
340 // TimerGroup is deleted next, printing the report.
341 }
342
343 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
344 // to a non null value (if the -time-passes option is enabled) or it leaves it
345 // null. It may be called multiple times.
346 static void createTheTimeInfo();
347
348 void passStarted(Pass *P) {
349
350 if (dynamic_cast<PMDataManager *>(P))
351 return;
352
353 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
354 if (I == TimingData.end())
355 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
356 I->second.startTimer();
357 }
358 void passEnded(Pass *P) {
359
360 if (dynamic_cast<PMDataManager *>(P))
361 return;
362
363 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
364 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
365 I->second.stopTimer();
366 }
367};
368
Devang Patel1c3633e2007-01-29 23:10:37 +0000369} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000370
Dan Gohmand78c4002008-05-13 00:00:25 +0000371static TimingInfo *TheTimeInfo;
372
Devang Patela1514cb2006-12-07 19:39:39 +0000373//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000374// PMTopLevelManager implementation
375
Devang Patel4268fc02007-01-16 02:00:38 +0000376/// Initialize top level manager. Create first pass manager.
377PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
378
379 if (t == TLM_Pass) {
380 MPPassManager *MPP = new MPPassManager(1);
381 MPP->setTopLevelManager(this);
382 addPassManager(MPP);
383 activeStack.push(MPP);
384 }
385 else if (t == TLM_Function) {
386 FPPassManager *FPP = new FPPassManager(1);
387 FPP->setTopLevelManager(this);
388 addPassManager(FPP);
389 activeStack.push(FPP);
390 }
391}
392
Devang Patelafb1f3622006-12-12 22:35:25 +0000393/// Set pass P as the last user of the given analysis passes.
Devang Patel8adae862007-07-20 18:04:54 +0000394void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
Devang Patelafb1f3622006-12-12 22:35:25 +0000395 Pass *P) {
396
Devang Patel8adae862007-07-20 18:04:54 +0000397 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000398 E = AnalysisPasses.end(); I != E; ++I) {
399 Pass *AP = *I;
400 LastUser[AP] = P;
Devang Patel01919d22007-03-08 19:05:01 +0000401
402 if (P == AP)
403 continue;
404
Devang Patelafb1f3622006-12-12 22:35:25 +0000405 // If AP is the last user of other passes then make P last user of
406 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000407 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000408 LUE = LastUser.end(); LUI != LUE; ++LUI) {
409 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000410 // DenseMap iterator is not invalidated here because
411 // this is just updating exisitng entry.
Devang Patelafb1f3622006-12-12 22:35:25 +0000412 LastUser[LUI->first] = P;
413 }
414 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000415}
416
417/// Collect passes whose last user is P
Devang Patel8adae862007-07-20 18:04:54 +0000418void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000419 Pass *P) {
420 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
421 InversedLastUser.find(P);
422 if (DMI == InversedLastUser.end())
423 return;
424
425 SmallPtrSet<Pass *, 8> &LU = DMI->second;
426 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
427 E = LU.end(); I != E; ++I) {
428 LastUses.push_back(*I);
429 }
430
Devang Patelafb1f3622006-12-12 22:35:25 +0000431}
432
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000433AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
434 AnalysisUsage *AnUsage = NULL;
435 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
436 if (DMI != AnUsageMap.end())
437 AnUsage = DMI->second;
438 else {
439 AnUsage = new AnalysisUsage();
440 P->getAnalysisUsage(*AnUsage);
441 AnUsageMap[P] = AnUsage;
442 }
443 return AnUsage;
444}
445
Devang Patelafb1f3622006-12-12 22:35:25 +0000446/// Schedule pass P for execution. Make sure that passes required by
447/// P are run before P is run. Update analysis info maintained by
448/// the manager. Remove dead passes. This is a recursive function.
449void PMTopLevelManager::schedulePass(Pass *P) {
450
Devang Patel3312f752007-01-16 21:43:18 +0000451 // TODO : Allocate function manager for this pass, other wise required set
452 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000453
Devang Pateld74ede72007-03-06 01:06:16 +0000454 // Give pass a chance to prepare the stage.
455 P->preparePassManager(activeStack);
456
Devang Patel864970e2008-03-18 00:39:19 +0000457 // If P is an analysis pass and it is available then do not
458 // generate the analysis again. Stale analysis info should not be
459 // available at this point.
Devang Patel718da662008-03-19 21:56:59 +0000460 if (P->getPassInfo() &&
461 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
Devang Patelaf75ab82008-03-19 00:48:41 +0000462 return;
Devang Patel864970e2008-03-18 00:39:19 +0000463
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000464 AnalysisUsage *AnUsage = findAnalysisUsage(P);
465
466 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000467 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000468 E = RequiredSet.end(); I != E; ++I) {
469
470 Pass *AnalysisPass = findAnalysisPass(*I);
471 if (!AnalysisPass) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000472 AnalysisPass = (*I)->createPass();
Devang Patele64d3052007-04-16 20:12:57 +0000473 // Schedule this analysis run first only if it is not a lower level
474 // analysis pass. Lower level analsyis passes are run on the fly.
475 if (P->getPotentialPassManagerType () >=
476 AnalysisPass->getPotentialPassManagerType())
477 schedulePass(AnalysisPass);
478 else
479 delete AnalysisPass;
Devang Patelafb1f3622006-12-12 22:35:25 +0000480 }
481 }
482
483 // Now all required passes are available.
484 addTopLevelPass(P);
485}
486
487/// Find the pass that implements Analysis AID. Search immutable
488/// passes and all pass managers. If desired pass is not found
489/// then return NULL.
490Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
491
492 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000493 // Check pass managers
Dan Gohman73caf5f2008-03-13 01:48:32 +0000494 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patelcd6ba152006-12-12 22:50:05 +0000495 E = PassManagers.end(); P == NULL && I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000496 PMDataManager *PMD = *I;
Devang Patelcd6ba152006-12-12 22:50:05 +0000497 P = PMD->findAnalysisPass(AID, false);
498 }
499
500 // Check other pass managers
501 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
502 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
503 P = (*I)->findAnalysisPass(AID, false);
504
Devang Patelafb1f3622006-12-12 22:35:25 +0000505 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
506 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
507 const PassInfo *PI = (*I)->getPassInfo();
508 if (PI == AID)
509 P = *I;
510
511 // If Pass not found then check the interfaces implemented by Immutable Pass
512 if (!P) {
Dan Gohman929391a2008-01-29 12:09:55 +0000513 const std::vector<const PassInfo*> &ImmPI =
514 PI->getInterfacesImplemented();
Devang Patel56d48ec2006-12-15 22:57:49 +0000515 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
516 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000517 }
518 }
519
Devang Patelafb1f3622006-12-12 22:35:25 +0000520 return P;
521}
522
Devang Pateleda56172006-12-12 23:34:33 +0000523// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000524void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000525
Devang Patelfd4184322007-01-17 20:33:36 +0000526 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000527 return;
528
Devang Pateleda56172006-12-12 23:34:33 +0000529 // Print out the immutable passes
530 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
531 ImmutablePasses[i]->dumpPassStructure(0);
532 }
533
Dan Gohman73caf5f2008-03-13 01:48:32 +0000534 // Every class that derives from PMDataManager also derives from Pass
535 // (sometimes indirectly), but there's no inheritance relationship
536 // between PMDataManager and Pass, so we have to dynamic_cast to get
537 // from a PMDataManager* to a Pass*.
538 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000539 E = PassManagers.end(); I != E; ++I)
Dan Gohman73caf5f2008-03-13 01:48:32 +0000540 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000541}
542
Devang Patel991aeba2006-12-15 20:13:01 +0000543void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000544
Devang Patelfd4184322007-01-17 20:33:36 +0000545 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000546 return;
547
548 cerr << "Pass Arguments: ";
Dan Gohman73caf5f2008-03-13 01:48:32 +0000549 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000550 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000551 PMDataManager *PMD = *I;
Devang Patelcfd70c42006-12-13 22:10:00 +0000552 PMD->dumpPassArguments();
553 }
554 cerr << "\n";
555}
556
Devang Patele3068402006-12-21 00:16:50 +0000557void PMTopLevelManager::initializeAllAnalysisInfo() {
558
Dan Gohman73caf5f2008-03-13 01:48:32 +0000559 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele3068402006-12-21 00:16:50 +0000560 E = PassManagers.end(); I != E; ++I) {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000561 PMDataManager *PMD = *I;
Devang Patele3068402006-12-21 00:16:50 +0000562 PMD->initializeAnalysisInfo();
563 }
564
565 // Initailize other pass managers
566 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
567 E = IndirectPassManagers.end(); I != E; ++I)
568 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000569
570 for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
571 DME = LastUser.end(); DMI != DME; ++DMI) {
572 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
573 InversedLastUser.find(DMI->second);
574 if (InvDMI != InversedLastUser.end()) {
575 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
576 L.insert(DMI->first);
577 } else {
578 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
579 InversedLastUser[DMI->second] = L;
580 }
581 }
Devang Patele3068402006-12-21 00:16:50 +0000582}
583
Devang Patele7599552007-01-12 18:52:44 +0000584/// Destructor
585PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman73caf5f2008-03-13 01:48:32 +0000586 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000587 E = PassManagers.end(); I != E; ++I)
588 delete *I;
589
590 for (std::vector<ImmutablePass *>::iterator
591 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
592 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000593
594 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
595 DME = AnUsageMap.end(); DMI != DME; ++DMI) {
596 AnalysisUsage *AU = DMI->second;
597 delete AU;
598 }
599
Devang Patele7599552007-01-12 18:52:44 +0000600}
601
Devang Patelafb1f3622006-12-12 22:35:25 +0000602//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000603// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000604
Devang Patel643676c2006-11-11 01:10:19 +0000605/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000606void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000607
Devang Patel643676c2006-11-11 01:10:19 +0000608 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000609 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000610
Devang Patele9976aa2006-12-07 19:33:53 +0000611 //This pass is the current implementation of all of the interfaces it
612 //implements as well.
613 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
614 for (unsigned i = 0, e = II.size(); i != e; ++i)
615 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000616 }
617}
618
Devang Patel9d9fc902007-03-06 17:52:53 +0000619// Return true if P preserves high level analysis used by other
620// passes managed by this manager
621bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
622
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000623 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel9d9fc902007-03-06 17:52:53 +0000624
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000625 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000626 return true;
627
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000628 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel9d9fc902007-03-06 17:52:53 +0000629 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
630 E = HigherLevelAnalysis.end(); I != E; ++I) {
631 Pass *P1 = *I;
Dan Gohman929391a2008-01-29 12:09:55 +0000632 if (!dynamic_cast<ImmutablePass*>(P1) &&
633 std::find(PreservedSet.begin(), PreservedSet.end(),
634 P1->getPassInfo()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000635 PreservedSet.end())
636 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000637 }
638
639 return true;
640}
641
Chris Lattner02eb94c2008-08-07 07:34:50 +0000642/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000643void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000644 // Don't do this unless assertions are enabled.
645#ifdef NDEBUG
646 return;
647#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000648 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
649 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000650
Devang Patelef432532007-07-19 05:36:09 +0000651 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000652 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000653 E = PreservedSet.end(); I != E; ++I) {
654 AnalysisID AID = *I;
Chris Lattner02eb94c2008-08-07 07:34:50 +0000655 if (Pass *AP = findAnalysisPass(AID, true))
Devang Patela273d1c2007-07-19 18:02:32 +0000656 AP->verifyAnalysis();
Devang Patelef432532007-07-19 05:36:09 +0000657 }
Devang Patela273d1c2007-07-19 18:02:32 +0000658}
659
Devang Patel9dbe4d12008-07-01 17:44:24 +0000660/// verifyDomInfo - Verify dominator information if it is available.
661void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
662
663 if (!VerifyDomInfo || !P.getResolver())
664 return;
665
666 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
667 if (!DT)
668 return;
669
670 DominatorTree OtherDT;
671 OtherDT.getBase().recalculate(F);
672 if (DT->compare(OtherDT)) {
673 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000674 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000675 cerr << "----- Valid -----\n";
676 OtherDT.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000677 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000678 DT->dump();
679 assert (0 && "Invalid dominator info");
680 }
681
682 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
683 if (!DF)
684 return;
685
686 DominanceFrontier OtherDF;
687 std::vector<BasicBlock*> DTRoots = DT->getRoots();
688 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
689 if (DF->compare(OtherDF)) {
690 cerr << "Dominator Information for " << F.getNameStart() << "\n";
Dan Gohman15269852008-07-09 00:50:40 +0000691 cerr << "Pass '" << P.getPassName() << "'\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000692 cerr << "----- Valid -----\n";
693 OtherDF.dump();
Devang Patel67c79a42008-07-01 19:50:56 +0000694 cerr << "----- Invalid -----\n";
Devang Patel9dbe4d12008-07-01 17:44:24 +0000695 DF->dump();
696 assert (0 && "Invalid dominator info");
697 }
698}
699
Devang Patel67c79a42008-07-01 19:50:56 +0000700/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000701void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000702 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
703 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000704 return;
705
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000706 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000707 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000708 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000709 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Patel01919d22007-03-08 19:05:01 +0000710 if (!dynamic_cast<ImmutablePass*>(Info->second)
711 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000712 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000713 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000714 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000715 if (PassDebugging >= Details) {
716 Pass *S = Info->second;
Dan Gohman15269852008-07-09 00:50:40 +0000717 cerr << " -- '" << P->getPassName() << "' is not preserving '";
718 cerr << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000719 }
720 }
Devang Patel349170f2006-11-11 01:24:55 +0000721 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000722
723 // Check inherited analysis also. If P is not preserving analysis
724 // provided by parent manager then remove it here.
725 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
726
727 if (!InheritedAnalysis[Index])
728 continue;
729
730 for (std::map<AnalysisID, Pass*>::iterator
731 I = InheritedAnalysis[Index]->begin(),
732 E = InheritedAnalysis[Index]->end(); I != E; ) {
733 std::map<AnalysisID, Pass *>::iterator Info = I++;
Dan Gohman929391a2008-01-29 12:09:55 +0000734 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
735 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel01919d22007-03-08 19:05:01 +0000736 PreservedSet.end())
Devang Patel42dd1e92007-03-06 01:55:46 +0000737 // Remove this analysis
Devang Patel01919d22007-03-08 19:05:01 +0000738 InheritedAnalysis[Index]->erase(Info);
Devang Patel42dd1e92007-03-06 01:55:46 +0000739 }
740 }
Devang Patelf68a3492006-11-07 22:35:17 +0000741}
742
Devang Patelca189262006-11-14 03:05:08 +0000743/// Remove analysis passes that are not used any longer
Devang Pateld305c402007-08-10 18:29:32 +0000744void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000745 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000746
Devang Patel8adae862007-07-20 18:04:54 +0000747 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000748
Devang Patel2ff44922007-04-16 20:39:59 +0000749 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000750 if (!TPM)
751 return;
752
Devang Patel17ad0962006-12-08 00:37:52 +0000753 TPM->collectLastUses(DeadPasses, P);
754
Devang Patel656a9172008-06-06 17:50:36 +0000755 if (PassDebugging >= Details && !DeadPasses.empty()) {
Dan Gohman15269852008-07-09 00:50:40 +0000756 cerr << " -*- '" << P->getPassName();
757 cerr << "' is the last user of following pass instances.";
Devang Patel656a9172008-06-06 17:50:36 +0000758 cerr << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000759 }
760
Devang Patel8adae862007-07-20 18:04:54 +0000761 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
Devang Patel17ad0962006-12-08 00:37:52 +0000762 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000763
Devang Patel003a5592007-03-05 20:01:30 +0000764 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000765
Devang Patel7ebf09d2007-03-05 18:20:51 +0000766 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patel17ad0962006-12-08 00:37:52 +0000767 (*I)->releaseMemory();
Devang Patel7ebf09d2007-03-05 18:20:51 +0000768 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patelb8817b92006-12-14 00:59:42 +0000769
Devang Patel17ad0962006-12-08 00:37:52 +0000770 std::map<AnalysisID, Pass*>::iterator Pos =
771 AvailableAnalysis.find((*I)->getPassInfo());
772
Devang Patel475c4532006-12-08 00:59:05 +0000773 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000774 if (Pos != AvailableAnalysis.end())
775 AvailableAnalysis.erase(Pos);
776 }
Devang Patelca189262006-11-14 03:05:08 +0000777}
778
Devang Patel8f677ce2006-12-07 18:47:25 +0000779/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000780/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000781void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000782 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000783
Devang Pateld440cd92006-12-08 23:53:00 +0000784 // This manager is going to manage pass P. Set up analysis resolver
785 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000786 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000787 P->setResolver(AR);
788
Devang Patelec2b9a72007-03-05 22:57:49 +0000789 // If a FunctionPass F is the last user of ModulePass info M
790 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000791 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000792
Devang Patel90b05e02006-11-11 02:04:19 +0000793 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000794
795 // At the moment, this pass is the last user of all required passes.
Devang Patel8adae862007-07-20 18:04:54 +0000796 SmallVector<Pass *, 12> LastUses;
Devang Patele64d3052007-04-16 20:12:57 +0000797 SmallVector<Pass *, 8> RequiredPasses;
798 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
799
Devang Patelbc03f132006-12-07 23:55:10 +0000800 unsigned PDepth = this->getDepth();
801
Devang Patele64d3052007-04-16 20:12:57 +0000802 collectRequiredAnalysis(RequiredPasses,
803 ReqAnalysisNotAvailable, P);
804 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patelbc03f132006-12-07 23:55:10 +0000805 E = RequiredPasses.end(); I != E; ++I) {
806 Pass *PRequired = *I;
807 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000808
Devang Patel9dbe4d12008-07-01 17:44:24 +0000809 assert (PRequired->getResolver() && "Analysis Resolver is not set");
Devang Patel64619be2006-12-09 00:07:38 +0000810 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
811 RDepth = DM.getDepth();
812
Devang Patelbc03f132006-12-07 23:55:10 +0000813 if (PDepth == RDepth)
814 LastUses.push_back(PRequired);
815 else if (PDepth > RDepth) {
816 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000817 TransferLastUses.push_back(PRequired);
Devang Patel9d9fc902007-03-06 17:52:53 +0000818 // Keep track of higher level analysis used by this manager.
819 HigherLevelAnalysis.push_back(PRequired);
Devang Patele64d3052007-04-16 20:12:57 +0000820 } else
821 assert (0 && "Unable to accomodate Required Pass");
822 }
823
Devang Patel39786a92007-01-15 20:31:54 +0000824 // Set P as P's last user until someone starts using P.
825 // However, if P is a Pass Manager then it does not need
826 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000827 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000828 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000829 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000830
Devang Patelec2b9a72007-03-05 22:57:49 +0000831 if (!TransferLastUses.empty()) {
832 Pass *My_PM = dynamic_cast<Pass *>(this);
833 TPM->setLastUser(TransferLastUses, My_PM);
834 TransferLastUses.clear();
835 }
836
Devang Patel69e9f6d2007-04-16 20:27:05 +0000837 // Now, take care of required analysises that are not available.
838 for (SmallVector<AnalysisID, 8>::iterator
839 I = ReqAnalysisNotAvailable.begin(),
840 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
841 Pass *AnalysisPass = (*I)->createPass();
842 this->addLowerLevelRequiredPass(P, AnalysisPass);
843 }
844
Devang Patel17bff0d2006-12-07 22:09:36 +0000845 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000846 // Remove the analysis not preserved by this pass
847 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000848 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000849 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000850
851 // Add pass
852 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000853}
854
Devang Patele64d3052007-04-16 20:12:57 +0000855
856/// Populate RP with analysis pass that are required by
857/// pass P and are available. Populate RP_NotAvail with analysis
858/// pass that are required by pass P but are not available.
859void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
860 SmallVector<AnalysisID, 8> &RP_NotAvail,
861 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000862 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
863 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000864 for (AnalysisUsage::VectorType::const_iterator
Devang Patel1d6267c2006-12-07 23:05:44 +0000865 I = RequiredSet.begin(), E = RequiredSet.end();
866 I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000867 AnalysisID AID = *I;
868 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
869 RP.push_back(AnalysisPass);
870 else
871 RP_NotAvail.push_back(AID);
Devang Patel1d6267c2006-12-07 23:05:44 +0000872 }
Devang Patelf58183d2006-12-12 23:09:32 +0000873
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000874 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +0000875 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +0000876 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +0000877 AnalysisID AID = *I;
878 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
879 RP.push_back(AnalysisPass);
880 else
881 RP_NotAvail.push_back(AID);
Devang Patelf58183d2006-12-12 23:09:32 +0000882 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000883}
884
Devang Patel07f4f582006-11-14 21:49:36 +0000885// All Required analyses should be available to the pass as it runs! Here
886// we fill in the AnalysisImpls member of the pass so that it can
887// successfully use the getAnalysis() method to retrieve the
888// implementations it needs.
889//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000890void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000891 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
892
Chris Lattnercbd160f2008-08-08 05:33:04 +0000893 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000894 I = AnUsage->getRequiredSet().begin(),
895 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000896 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000897 if (Impl == 0)
Devang Patel56a5c622007-04-16 20:44:16 +0000898 // This may be analysis pass that is initialized on the fly.
899 // If that is not the case then it will raise an assert when it is used.
900 continue;
Devang Patelb66334b2007-01-05 22:47:07 +0000901 AnalysisResolver *AR = P->getResolver();
Devang Patel9dbe4d12008-07-01 17:44:24 +0000902 assert (AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +0000903 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000904 }
905}
906
Devang Patel640c5bb2006-12-08 22:30:11 +0000907/// Find the pass that implements Analysis AID. If desired pass is not found
908/// then return NULL.
909Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
910
911 // Check if AvailableAnalysis map has one entry.
912 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
913
914 if (I != AvailableAnalysis.end())
915 return I->second;
916
917 // Search Parents through TopLevelManager
918 if (SearchParent)
919 return TPM->findAnalysisPass(AID);
920
Devang Patel9d759b82006-12-09 00:09:12 +0000921 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000922}
923
Devang Patel991aeba2006-12-15 20:13:01 +0000924// Print list of passes that are last used by P.
925void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
926
Devang Patel8adae862007-07-20 18:04:54 +0000927 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +0000928
929 // If this is a on the fly manager then it does not have TPM.
930 if (!TPM)
931 return;
932
Devang Patel991aeba2006-12-15 20:13:01 +0000933 TPM->collectLastUses(LUses, P);
934
Devang Patel8adae862007-07-20 18:04:54 +0000935 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +0000936 E = LUses.end(); I != E; ++I) {
937 llvm::cerr << "--" << std::string(Offset*2, ' ');
938 (*I)->dumpPassStructure(0);
939 }
940}
941
942void PMDataManager::dumpPassArguments() const {
943 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
944 E = PassVector.end(); I != E; ++I) {
945 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
946 PMD->dumpPassArguments();
947 else
948 if (const PassInfo *PI = (*I)->getPassInfo())
949 if (!PI->isAnalysisGroup())
950 cerr << " -" << PI->getPassArgument();
951 }
952}
953
Chris Lattnerdd6304f2007-08-10 06:17:04 +0000954void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
955 enum PassDebuggingString S2,
Devang Pateld305c402007-08-10 18:29:32 +0000956 const char *Msg) {
Devang Patelfd4184322007-01-17 20:33:36 +0000957 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000958 return;
959 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +0000960 switch (S1) {
961 case EXECUTION_MSG:
962 cerr << "Executing Pass '" << P->getPassName();
963 break;
964 case MODIFICATION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000965 cerr << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +0000966 break;
967 case FREEING_MSG:
968 cerr << " Freeing Pass '" << P->getPassName();
969 break;
970 default:
971 break;
972 }
973 switch (S2) {
974 case ON_BASICBLOCK_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000975 cerr << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000976 break;
977 case ON_FUNCTION_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000978 cerr << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000979 break;
980 case ON_MODULE_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000981 cerr << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000982 break;
983 case ON_LOOP_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000984 cerr << "' on Loop " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000985 break;
986 case ON_CG_MSG:
Devang Pateld56e4912007-06-18 21:32:29 +0000987 cerr << "' on Call Graph " << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +0000988 break;
989 default:
990 break;
991 }
Devang Patel991aeba2006-12-15 20:13:01 +0000992}
993
Chris Lattner4c493d92008-08-08 15:14:09 +0000994void PMDataManager::dumpRequiredSet(const Pass *P)
995 const {
996 if (PassDebugging < Details)
997 return;
998
999 AnalysisUsage analysisUsage;
1000 P->getAnalysisUsage(analysisUsage);
1001 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1002}
1003
1004void PMDataManager::dumpPreservedSet(const Pass *P)
1005 const {
1006 if (PassDebugging < Details)
1007 return;
1008
1009 AnalysisUsage analysisUsage;
1010 P->getAnalysisUsage(analysisUsage);
1011 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1012}
1013
1014void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
Chris Lattnercbd160f2008-08-08 05:33:04 +00001015 const AnalysisUsage::VectorType &Set)
Devang Patel991aeba2006-12-15 20:13:01 +00001016 const {
Chris Lattner4c493d92008-08-08 15:14:09 +00001017 assert(PassDebugging >= Details);
1018 if (Set.empty())
1019 return;
1020 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1021 for (unsigned i = 0; i != Set.size(); ++i) {
1022 if (i) cerr << ",";
1023 cerr << " " << Set[i]->getPassName();
1024 }
1025 cerr << "\n";
Devang Patel991aeba2006-12-15 20:13:01 +00001026}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001027
Devang Patel004937b2007-07-27 20:06:09 +00001028/// Add RequiredPass into list of lower level passes required by pass P.
1029/// RequiredPass is run on the fly by Pass Manager when P requests it
1030/// through getAnalysis interface.
1031/// This should be handled by specific pass manager.
1032void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1033 if (TPM) {
1034 TPM->dumpArguments();
1035 TPM->dumpPasses();
1036 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001037
1038 // Module Level pass may required Function Level analysis info
1039 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1040 // to provide this on demand. In that case, in Pass manager terminology,
1041 // module level pass is requiring lower level analysis info managed by
1042 // lower level pass manager.
1043
1044 // When Pass manager is not able to order required analysis info, Pass manager
1045 // checks whether any lower level manager will be able to provide this
1046 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001047#ifndef NDEBUG
Dan Gohman15269852008-07-09 00:50:40 +00001048 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1049 cerr << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001050#endif
1051 assert (0 && "Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001052}
1053
Devang Patele7599552007-01-12 18:52:44 +00001054// Destructor
1055PMDataManager::~PMDataManager() {
1056
1057 for (std::vector<Pass *>::iterator I = PassVector.begin(),
1058 E = PassVector.end(); I != E; ++I)
1059 delete *I;
1060
Devang Patele7599552007-01-12 18:52:44 +00001061}
1062
Devang Patel9bdf7d42006-12-08 23:28:54 +00001063//===----------------------------------------------------------------------===//
1064// NOTE: Is this the right place to define this method ?
1065// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +00001066Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001067 return PM.findAnalysisPass(ID, dir);
1068}
1069
Devang Patel92942812007-04-16 20:56:24 +00001070Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1071 Function &F) {
1072 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1073}
1074
Devang Patela1514cb2006-12-07 19:39:39 +00001075//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001076// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001077
Devang Patel6e5a1132006-11-07 21:31:57 +00001078/// Execute all of the passes scheduled for execution by invoking
1079/// runOnBasicBlock method. Keep track of whether any of the passes modifies
1080/// the function, and if so, return true.
1081bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001082BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +00001083
Reid Spencer5301e7c2007-01-30 20:08:39 +00001084 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001085 return false;
1086
Devang Patele9585592006-12-08 01:38:28 +00001087 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001088
Devang Patel6e5a1132006-11-07 21:31:57 +00001089 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001090 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1091 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +00001092
Devang Pateld305c402007-08-10 18:29:32 +00001093 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001094 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001095
Devang Patelabfbe3b2006-12-16 00:56:26 +00001096 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001097
Devang Patelabfbe3b2006-12-16 00:56:26 +00001098 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +00001099 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001100 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001101
Devang Patel003a5592007-03-05 20:01:30 +00001102 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001103 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1104 I->getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001105 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001106
Devang Patela273d1c2007-07-19 18:02:32 +00001107 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001108 removeNotPreservedAnalysis(BP);
1109 recordAvailableAnalysis(BP);
Devang Pateld305c402007-08-10 18:29:32 +00001110 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001111 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001112
Devang Patel56d48ec2006-12-15 22:57:49 +00001113 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +00001114}
1115
Devang Patel475c4532006-12-08 00:59:05 +00001116// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +00001117inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001118 bool Changed = false;
1119
Devang Patelabfbe3b2006-12-16 00:56:26 +00001120 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1121 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001122 Changed |= BP->doInitialization(M);
1123 }
1124
1125 return Changed;
1126}
1127
Devang Patel67d6a5e2006-12-19 19:46:59 +00001128inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001129 bool Changed = false;
1130
Devang Patelabfbe3b2006-12-16 00:56:26 +00001131 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1132 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001133 Changed |= BP->doFinalization(M);
1134 }
1135
1136 return Changed;
1137}
1138
Devang Patel67d6a5e2006-12-19 19:46:59 +00001139inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001140 bool Changed = false;
1141
Devang Patelabfbe3b2006-12-16 00:56:26 +00001142 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1143 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001144 Changed |= BP->doInitialization(F);
1145 }
1146
1147 return Changed;
1148}
1149
Devang Patel67d6a5e2006-12-19 19:46:59 +00001150inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001151 bool Changed = false;
1152
Devang Patelabfbe3b2006-12-16 00:56:26 +00001153 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1154 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001155 Changed |= BP->doFinalization(F);
1156 }
1157
1158 return Changed;
1159}
1160
1161
Devang Patela1514cb2006-12-07 19:39:39 +00001162//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001163// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001164
Devang Patel4e12f862006-11-08 10:44:40 +00001165/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001166FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001167 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001168 // FPM is the top level manager.
1169 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001170
Dan Gohman565df952008-03-13 02:08:36 +00001171 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001172 FPM->setResolver(AR);
1173
Devang Patel1f653682006-12-08 18:57:16 +00001174 MP = P;
1175}
1176
Devang Patelb67904d2006-12-13 02:36:01 +00001177FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001178 delete FPM;
1179}
1180
Devang Patel4e12f862006-11-08 10:44:40 +00001181/// add - Add a pass to the queue of passes to run. This passes
1182/// ownership of the Pass to the PassManager. When the
1183/// PassManager_X is destroyed, the pass will be destroyed as well, so
1184/// there is no need to delete the pass. (TODO delete passes.)
1185/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001186void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001187 FPM->add(P);
1188}
1189
Devang Patel9f3083e2006-11-15 19:39:54 +00001190/// run - Execute all of the passes scheduled for execution. Keep
1191/// track of whether any of the passes modifies the function, and if
1192/// so, return true.
1193///
Devang Patelb67904d2006-12-13 02:36:01 +00001194bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001195 std::string errstr;
1196 if (MP->materializeFunction(&F, &errstr)) {
Gabor Greife16561c2007-07-05 17:07:56 +00001197 cerr << "Error reading bitcode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001198 abort();
1199 }
Devang Patel272908d2006-12-08 22:57:48 +00001200 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001201}
1202
1203
Devang Patelff631ae2006-11-15 01:27:05 +00001204/// doInitialization - Run all of the initializers for the function passes.
1205///
Devang Patelb67904d2006-12-13 02:36:01 +00001206bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001207 return FPM->doInitialization(*MP->getModule());
1208}
1209
Dan Gohmane6656eb2007-07-30 14:51:13 +00001210/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001211///
Devang Patelb67904d2006-12-13 02:36:01 +00001212bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001213 return FPM->doFinalization(*MP->getModule());
1214}
1215
Devang Patela1514cb2006-12-07 19:39:39 +00001216//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001217// FunctionPassManagerImpl implementation
1218//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001219inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1220 bool Changed = false;
1221
1222 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1223 FPPassManager *FP = getContainedManager(Index);
1224 Changed |= FP->doInitialization(M);
1225 }
1226
1227 return Changed;
1228}
1229
1230inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1231 bool Changed = false;
1232
1233 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1234 FPPassManager *FP = getContainedManager(Index);
1235 Changed |= FP->doFinalization(M);
1236 }
1237
1238 return Changed;
1239}
1240
1241// Execute all the passes managed by this top level manager.
1242// Return true if any function is modified by a pass.
1243bool FunctionPassManagerImpl::run(Function &F) {
1244
1245 bool Changed = false;
1246
1247 TimingInfo::createTheTimeInfo();
1248
1249 dumpArguments();
1250 dumpPasses();
1251
Devang Patele3068402006-12-21 00:16:50 +00001252 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001253 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1254 FPPassManager *FP = getContainedManager(Index);
1255 Changed |= FP->runOnFunction(F);
1256 }
1257 return Changed;
1258}
1259
1260//===----------------------------------------------------------------------===//
1261// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001262
Devang Patel8c78a0b2007-05-03 01:11:54 +00001263char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001264/// Print passes managed by this manager
1265void FPPassManager::dumpPassStructure(unsigned Offset) {
1266 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1267 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1268 FunctionPass *FP = getContainedPass(Index);
1269 FP->dumpPassStructure(Offset + 1);
1270 dumpLastUses(FP, Offset+1);
1271 }
1272}
1273
1274
Devang Patel0c2012f2006-11-07 21:49:50 +00001275/// Execute all of the passes scheduled for execution by invoking
1276/// runOnFunction method. Keep track of whether any of the passes modifies
1277/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001278bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001279
1280 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001281
Reid Spencer5301e7c2007-01-30 20:08:39 +00001282 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001283 return false;
Devang Patelcbbf2912008-03-20 01:09:53 +00001284
1285 // Collect inherited analysis from Module level pass manager.
1286 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001287
Devang Patelabfbe3b2006-12-16 00:56:26 +00001288 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1289 FunctionPass *FP = getContainedPass(Index);
1290
Devang Pateld305c402007-08-10 18:29:32 +00001291 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001292 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001293
Devang Patelabfbe3b2006-12-16 00:56:26 +00001294 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +00001295
Devang Patelabfbe3b2006-12-16 00:56:26 +00001296 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +00001297 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001298 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001299
Devang Patel003a5592007-03-05 20:01:30 +00001300 if (Changed)
Devang Pateld305c402007-08-10 18:29:32 +00001301 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
Chris Lattner4c493d92008-08-08 15:14:09 +00001302 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001303
Devang Patela273d1c2007-07-19 18:02:32 +00001304 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001305 removeNotPreservedAnalysis(FP);
1306 recordAvailableAnalysis(FP);
Devang Pateld305c402007-08-10 18:29:32 +00001307 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
Devang Patel9dbe4d12008-07-01 17:44:24 +00001308
Devang Patel67c79a42008-07-01 19:50:56 +00001309 // If dominator information is available then verify the info if requested.
Devang Patel9dbe4d12008-07-01 17:44:24 +00001310 verifyDomInfo(*FP, F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001311 }
1312 return Changed;
1313}
1314
Devang Patel67d6a5e2006-12-19 19:46:59 +00001315bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001316
Devang Patel67d6a5e2006-12-19 19:46:59 +00001317 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001318
1319 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1320 this->runOnFunction(*I);
1321
1322 return Changed |= doFinalization(M);
1323}
1324
1325inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001326 bool Changed = false;
1327
Devang Patelabfbe3b2006-12-16 00:56:26 +00001328 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1329 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001330 Changed |= FP->doInitialization(M);
1331 }
1332
1333 return Changed;
1334}
1335
Devang Patel67d6a5e2006-12-19 19:46:59 +00001336inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001337 bool Changed = false;
1338
Devang Patelabfbe3b2006-12-16 00:56:26 +00001339 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1340 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +00001341 Changed |= FP->doFinalization(M);
1342 }
1343
Devang Patelff631ae2006-11-15 01:27:05 +00001344 return Changed;
1345}
1346
Devang Patela1514cb2006-12-07 19:39:39 +00001347//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001348// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001349
Devang Patel05e1a972006-11-07 22:03:15 +00001350/// Execute all of the passes scheduled for execution by invoking
1351/// runOnModule method. Keep track of whether any of the passes modifies
1352/// the module, and if so, return true.
1353bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001354MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001355 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001356
Devang Patelabfbe3b2006-12-16 00:56:26 +00001357 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1358 ModulePass *MP = getContainedPass(Index);
1359
Dan Gohman929391a2008-01-29 12:09:55 +00001360 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1361 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001362 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001363
Devang Patelabfbe3b2006-12-16 00:56:26 +00001364 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001365
Devang Patelabfbe3b2006-12-16 00:56:26 +00001366 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +00001367 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001368 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001369
Devang Patel003a5592007-03-05 20:01:30 +00001370 if (Changed)
Dan Gohman929391a2008-01-29 12:09:55 +00001371 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1372 M.getModuleIdentifier().c_str());
Chris Lattner4c493d92008-08-08 15:14:09 +00001373 dumpPreservedSet(MP);
Chris Lattner02eb94c2008-08-07 07:34:50 +00001374
Devang Patela273d1c2007-07-19 18:02:32 +00001375 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001376 removeNotPreservedAnalysis(MP);
1377 recordAvailableAnalysis(MP);
Devang Pateld305c402007-08-10 18:29:32 +00001378 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001379 }
1380 return Changed;
1381}
1382
Devang Patele64d3052007-04-16 20:12:57 +00001383/// Add RequiredPass into list of lower level passes required by pass P.
1384/// RequiredPass is run on the fly by Pass Manager when P requests it
1385/// through getAnalysis interface.
1386void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1387
1388 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1389 && "Unable to handle Pass that requires lower level Analysis pass");
1390 assert ((P->getPotentialPassManagerType() <
1391 RequiredPass->getPotentialPassManagerType())
1392 && "Unable to handle Pass that requires lower level Analysis pass");
1393
Devang Patel68f72b12007-04-26 17:50:19 +00001394 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001395 if (!FPP) {
Devang Patel68f72b12007-04-26 17:50:19 +00001396 FPP = new FunctionPassManagerImpl(0);
1397 // FPP is the top level manager.
1398 FPP->setTopLevelManager(FPP);
1399
Devang Patel69e9f6d2007-04-16 20:27:05 +00001400 OnTheFlyManagers[P] = FPP;
1401 }
Devang Patel68f72b12007-04-26 17:50:19 +00001402 FPP->add(RequiredPass);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001403
Devang Patel68f72b12007-04-26 17:50:19 +00001404 // Register P as the last user of RequiredPass.
Devang Patel8adae862007-07-20 18:04:54 +00001405 SmallVector<Pass *, 12> LU;
Devang Patel68f72b12007-04-26 17:50:19 +00001406 LU.push_back(RequiredPass);
1407 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001408}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001409
1410/// Return function pass corresponding to PassInfo PI, that is
1411/// required by module pass MP. Instantiate analysis pass, by using
1412/// its runOnFunction() for function F.
1413Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1414 Function &F) {
1415 AnalysisID AID = PI;
Devang Patel68f72b12007-04-26 17:50:19 +00001416 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001417 assert (FPP && "Unable to find on the fly pass");
1418
Devang Patel68f72b12007-04-26 17:50:19 +00001419 FPP->run(F);
1420 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001421}
1422
1423
Devang Patela1514cb2006-12-07 19:39:39 +00001424//===----------------------------------------------------------------------===//
1425// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001426//
Devang Patelc290c8a2006-11-07 22:23:34 +00001427/// run - Execute all of the passes scheduled for execution. Keep track of
1428/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001429bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001430
Devang Patelc290c8a2006-11-07 22:23:34 +00001431 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001432
Devang Patelb8817b92006-12-14 00:59:42 +00001433 TimingInfo::createTheTimeInfo();
1434
Devang Patelcfd70c42006-12-13 22:10:00 +00001435 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001436 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001437
Devang Patele3068402006-12-21 00:16:50 +00001438 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001439 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1440 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001441 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001442 }
1443 return Changed;
1444}
Devang Patel376fefa2006-11-08 10:29:57 +00001445
Devang Patela1514cb2006-12-07 19:39:39 +00001446//===----------------------------------------------------------------------===//
1447// PassManager implementation
1448
Devang Patel376fefa2006-11-08 10:29:57 +00001449/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001450PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001451 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001452 // PM is the top level manager
1453 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001454}
1455
Devang Patelb67904d2006-12-13 02:36:01 +00001456PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001457 delete PM;
1458}
1459
Devang Patel376fefa2006-11-08 10:29:57 +00001460/// add - Add a pass to the queue of passes to run. This passes ownership of
1461/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1462/// will be destroyed as well, so there is no need to delete the pass. This
1463/// implies that all passes MUST be allocated with 'new'.
1464void
Devang Patelb67904d2006-12-13 02:36:01 +00001465PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001466 PM->add(P);
1467}
1468
1469/// run - Execute all of the passes scheduled for execution. Keep track of
1470/// whether any of the passes modifies the module, and if so, return true.
1471bool
Devang Patelb67904d2006-12-13 02:36:01 +00001472PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001473 return PM->run(M);
1474}
1475
Devang Patelb8817b92006-12-14 00:59:42 +00001476//===----------------------------------------------------------------------===//
1477// TimingInfo Class - This class is used to calculate information about the
1478// amount of time each pass takes to execute. This only happens with
1479// -time-passes is enabled on the command line.
1480//
1481bool llvm::TimePassesIsEnabled = false;
1482static cl::opt<bool,true>
1483EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1484 cl::desc("Time each pass, printing elapsed time for each on exit"));
1485
1486// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1487// a non null value (if the -time-passes option is enabled) or it leaves it
1488// null. It may be called multiple times.
1489void TimingInfo::createTheTimeInfo() {
1490 if (!TimePassesIsEnabled || TheTimeInfo) return;
1491
1492 // Constructed the first time this is called, iff -time-passes is enabled.
1493 // This guarantees that the object will be constructed before static globals,
1494 // thus it will be destroyed before them.
1495 static ManagedStatic<TimingInfo> TTI;
1496 TheTimeInfo = &*TTI;
1497}
1498
Devang Patel1c3633e2007-01-29 23:10:37 +00001499/// If TimingInfo is enabled then start pass timer.
1500void StartPassTimer(Pass *P) {
1501 if (TheTimeInfo)
1502 TheTimeInfo->passStarted(P);
1503}
1504
1505/// If TimingInfo is enabled then stop pass timer.
1506void StopPassTimer(Pass *P) {
1507 if (TheTimeInfo)
1508 TheTimeInfo->passEnded(P);
1509}
1510
Devang Patel1c56a632007-01-08 19:29:38 +00001511//===----------------------------------------------------------------------===//
1512// PMStack implementation
1513//
Devang Patelad98d232007-01-11 22:15:30 +00001514
Devang Patel1c56a632007-01-08 19:29:38 +00001515// Pop Pass Manager from the stack and clear its analysis info.
1516void PMStack::pop() {
1517
1518 PMDataManager *Top = this->top();
1519 Top->initializeAnalysisInfo();
1520
1521 S.pop_back();
1522}
1523
1524// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001525void PMStack::push(PMDataManager *PM) {
Devang Patel1c56a632007-01-08 19:29:38 +00001526
Devang Patel15701b52007-01-11 00:19:00 +00001527 PMDataManager *Top = NULL;
Devang Patel15701b52007-01-11 00:19:00 +00001528 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001529
Devang Patel15701b52007-01-11 00:19:00 +00001530 if (this->empty()) {
1531 Top = PM;
1532 }
1533 else {
1534 Top = this->top();
1535 PMTopLevelManager *TPM = Top->getTopLevelManager();
1536
1537 assert (TPM && "Unable to find top level manager");
1538 TPM->addIndirectPassManager(PM);
1539 PM->setTopLevelManager(TPM);
1540 }
1541
Devang Patel15701b52007-01-11 00:19:00 +00001542 S.push_back(PM);
1543}
1544
1545// Dump content of the pass manager stack.
1546void PMStack::dump() {
1547 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1548 E = S.end(); I != E; ++I) {
1549 Pass *P = dynamic_cast<Pass *>(*I);
Chris Lattnerde2aa652007-08-10 06:22:25 +00001550 printf("%s ", P->getPassName());
Devang Patel15701b52007-01-11 00:19:00 +00001551 }
1552 if (!S.empty())
Chris Lattnerde2aa652007-08-10 06:22:25 +00001553 printf("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001554}
1555
Devang Patel1c56a632007-01-08 19:29:38 +00001556/// Find appropriate Module Pass Manager in the PM Stack and
1557/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001558void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001559 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001560
Devang Patel1c56a632007-01-08 19:29:38 +00001561 // Find Module Pass Manager
1562 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001563 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1564 if (TopPMType == PreferredType)
1565 break; // We found desired pass manager
1566 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001567 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001568 else
1569 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001570 }
1571
Devang Patel23f8aa92007-01-17 21:19:23 +00001572 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001573}
1574
Devang Patel3312f752007-01-16 21:43:18 +00001575/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1576/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001577void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001578 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001579
Devang Patel15701b52007-01-11 00:19:00 +00001580 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001581 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001582 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1583 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001584 else
Devang Patel3312f752007-01-16 21:43:18 +00001585 break;
1586 }
1587 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1588
1589 // Create new Function Pass Manager
1590 if (!FPP) {
1591 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1592 PMDataManager *PMD = PMS.top();
1593
1594 // [1] Create new Function Pass Manager
1595 FPP = new FPPassManager(PMD->getDepth() + 1);
Devang Patelcbbf2912008-03-20 01:09:53 +00001596 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001597
1598 // [2] Set up new manager's top level manager
1599 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1600 TPM->addIndirectPassManager(FPP);
1601
1602 // [3] Assign manager to manage this new manager. This may create
1603 // and push new managers into PMS
Devang Pateldffca632007-01-17 20:30:17 +00001604
1605 // If Call Graph Pass Manager is active then use it to manage
1606 // this new Function Pass manager.
1607 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
Dan Gohman565df952008-03-13 02:08:36 +00001608 FPP->assignPassManager(PMS, PMT_CallGraphPassManager);
Devang Pateldffca632007-01-17 20:30:17 +00001609 else
Dan Gohman565df952008-03-13 02:08:36 +00001610 FPP->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001611
1612 // [4] Push new manager into PMS
1613 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001614 }
1615
Devang Patel3312f752007-01-16 21:43:18 +00001616 // Assign FPP as the manager of this pass.
1617 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001618}
1619
Devang Patel3312f752007-01-16 21:43:18 +00001620/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001621/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001622void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001623 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001624
1625 BBPassManager *BBP = NULL;
1626
Devang Patel15701b52007-01-11 00:19:00 +00001627 // Basic Pass Manager is a leaf pass manager. It does not handle
1628 // any other pass manager.
Chris Lattnerde2aa652007-08-10 06:22:25 +00001629 if (!PMS.empty())
Devang Patel1c56a632007-01-08 19:29:38 +00001630 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001631
Devang Patel3312f752007-01-16 21:43:18 +00001632 // If leaf manager is not Basic Block Pass manager then create new
1633 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001634
Devang Patel3312f752007-01-16 21:43:18 +00001635 if (!BBP) {
1636 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1637 PMDataManager *PMD = PMS.top();
1638
1639 // [1] Create new Basic Block Manager
1640 BBP = new BBPassManager(PMD->getDepth() + 1);
1641
1642 // [2] Set up new manager's top level manager
1643 // Basic Block Pass Manager does not live by itself
1644 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1645 TPM->addIndirectPassManager(BBP);
1646
Devang Patel15701b52007-01-11 00:19:00 +00001647 // [3] Assign manager to manage this new manager. This may create
1648 // and push new managers into PMS
Dan Gohman565df952008-03-13 02:08:36 +00001649 BBP->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001650
Devang Patel3312f752007-01-16 21:43:18 +00001651 // [4] Push new manager into PMS
1652 PMS.push(BBP);
1653 }
Devang Patel1c56a632007-01-08 19:29:38 +00001654
Devang Patel3312f752007-01-16 21:43:18 +00001655 // Assign BBP as the manager of this pass.
1656 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001657}
1658
Dan Gohmand3a20c92008-03-11 16:41:42 +00001659PassManagerBase::~PassManagerBase() {}
Gordon Henriksen878114b2008-03-16 04:20:44 +00001660
1661/*===-- C Bindings --------------------------------------------------------===*/
1662
1663LLVMPassManagerRef LLVMCreatePassManager() {
1664 return wrap(new PassManager());
1665}
1666
1667LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1668 return wrap(new FunctionPassManager(unwrap(P)));
1669}
1670
1671int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1672 return unwrap<PassManager>(PM)->run(*unwrap(M));
1673}
1674
1675int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1676 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1677}
1678
1679int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1680 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1681}
1682
1683int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1684 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1685}
1686
1687void LLVMDisposePassManager(LLVMPassManagerRef PM) {
1688 delete unwrap(PM);
1689}