blob: e590a6cc2a8eb8018267e84e189020bc58a1e7d2 [file] [log] [blame]
Devang Patel55fd43f2006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Devang Patelc67c9382006-11-08 10:05:38 +00005// This file was developed by Devang Patel and is distributed under
Devang Patel55fd43f2006-11-07 21:31:57 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM Pass Manager infrastructure.
11//
12//===----------------------------------------------------------------------===//
13
14
Devang Patelab7752c2007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
Devang Patel45dc02d2006-12-13 20:03:48 +000016#include "llvm/Support/CommandLine.h"
Devang Patelc874eb52007-01-29 23:10:37 +000017#include "llvm/Support/Timer.h"
Devang Patel55fd43f2006-11-07 21:31:57 +000018#include "llvm/Module.h"
Devang Patel3799f972006-11-15 01:27:05 +000019#include "llvm/ModuleProvider.h"
Bill Wendling8f487662006-11-28 02:09:03 +000020#include "llvm/Support/Streams.h"
Devang Patel8e58a1b2006-12-14 00:59:42 +000021#include "llvm/Support/ManagedStatic.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000022#include <algorithm>
Devang Patel85d344b2006-11-11 01:31:05 +000023#include <vector>
Devang Patelb899eed2006-11-14 01:59:59 +000024#include <map>
Devang Patelc2ff9622006-12-15 19:39:30 +000025
Devang Patelab7752c2007-01-12 18:52:44 +000026// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patele77242c2006-12-07 18:23:30 +000027
Devang Patel45dc02d2006-12-13 20:03:48 +000028namespace llvm {
29
30//===----------------------------------------------------------------------===//
31// Pass debugging information. Often it is useful to find out what pass is
32// running when a crash occurs in a utility. When this library is compiled with
33// debugging on, a command line option (--debug-pass) is enabled that causes the
34// pass name to be printed before it executes.
35//
36
Devang Patele8ff1ce2006-12-13 21:13:31 +000037// Different debug levels that can be enabled...
38enum PassDebugLevel {
39 None, Arguments, Structure, Executions, Details
40};
41
Devang Patel45dc02d2006-12-13 20:03:48 +000042static cl::opt<enum PassDebugLevel>
Devang Patel26426942007-01-17 20:33:36 +000043PassDebugging("debug-pass", cl::Hidden,
Devang Patel45dc02d2006-12-13 20:03:48 +000044 cl::desc("Print PassManager debugging information"),
45 cl::values(
Devang Patele8ff1ce2006-12-13 21:13:31 +000046 clEnumVal(None , "disable debug output"),
47 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
48 clEnumVal(Structure , "print pass structure before run()"),
49 clEnumVal(Executions, "print pass name before it is executed"),
50 clEnumVal(Details , "print pass details when it is executed"),
Devang Patel45dc02d2006-12-13 20:03:48 +000051 clEnumValEnd));
52} // End of llvm namespace
53
Devang Patelc2ff9622006-12-15 19:39:30 +000054namespace {
Devang Patel1b8d0152006-12-12 22:35:25 +000055
Devang Patel3f5d2b52006-12-07 19:21:29 +000056//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +000057// BBPassManager
Devang Patel7e601a72006-12-12 22:47:13 +000058//
Devang Patel5f4ddf52006-12-19 19:46:59 +000059/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelc67c9382006-11-08 10:05:38 +000060/// pass together and sequence them to process one basic block before
61/// processing next basic block.
Devang Patel5f4ddf52006-12-19 19:46:59 +000062class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
63 public FunctionPass {
Devang Patelc67c9382006-11-08 10:05:38 +000064
65public:
Devang Patel19974732007-05-03 01:11:54 +000066 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000067 BBPassManager(int Depth)
68 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
Devang Patelc67c9382006-11-08 10:05:38 +000069
Devang Patelc67c9382006-11-08 10:05:38 +000070 /// Execute all of the passes scheduled for execution. Keep track of
71 /// whether any of the passes modifies the function, and if so, return true.
72 bool runOnFunction(Function &F);
73
Devang Patel66d72e12006-12-07 19:57:52 +000074 /// Pass Manager itself does not invalidate any analysis info.
75 void getAnalysisUsage(AnalysisUsage &Info) const {
76 Info.setPreservesAll();
77 }
78
Devang Patel964e45e2006-12-08 00:59:05 +000079 bool doInitialization(Module &M);
80 bool doInitialization(Function &F);
81 bool doFinalization(Module &M);
82 bool doFinalization(Function &F);
83
Devang Patele27ae7e2007-02-01 22:08:25 +000084 virtual const char *getPassName() const {
85 return "BasicBlock Pass Manager";
86 }
87
Devang Patelebc09222006-12-12 23:34:33 +000088 // Print passes managed by this manager
89 void dumpPassStructure(unsigned Offset) {
Devang Patelc2ff9622006-12-15 19:39:30 +000090 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patel1554c852006-12-16 00:56:26 +000091 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
92 BasicBlockPass *BP = getContainedPass(Index);
93 BP->dumpPassStructure(Offset + 1);
94 dumpLastUses(BP, Offset+1);
Devang Patelebc09222006-12-12 23:34:33 +000095 }
96 }
Devang Patel1554c852006-12-16 00:56:26 +000097
98 BasicBlockPass *getContainedPass(unsigned N) {
99 assert ( N < PassVector.size() && "Pass number out of range!");
100 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
101 return BP;
102 }
Devang Patel25919cb2007-01-11 01:10:25 +0000103
Devang Patel84da80d2007-02-27 15:00:39 +0000104 virtual PassManagerType getPassManagerType() const {
Devang Patel25919cb2007-01-11 01:10:25 +0000105 return PMT_BasicBlockPassManager;
106 }
Devang Patelc67c9382006-11-08 10:05:38 +0000107};
108
Devang Patel19974732007-05-03 01:11:54 +0000109char BBPassManager::ID = 0;
Devang Patelab7752c2007-01-12 18:52:44 +0000110}
Devang Patel5f4ddf52006-12-19 19:46:59 +0000111
Devang Patelab7752c2007-01-12 18:52:44 +0000112namespace llvm {
Devang Patelc67c9382006-11-08 10:05:38 +0000113
Devang Patel7e601a72006-12-12 22:47:13 +0000114//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000115// FunctionPassManagerImpl
Devang Patel7e601a72006-12-12 22:47:13 +0000116//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000117/// FunctionPassManagerImpl manages FPPassManagers
118class FunctionPassManagerImpl : public Pass,
Devang Patel36bcb822007-01-11 22:15:30 +0000119 public PMDataManager,
120 public PMTopLevelManager {
Devang Patel5f4ddf52006-12-19 19:46:59 +0000121public:
Devang Patel19974732007-05-03 01:11:54 +0000122 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +0000123 FunctionPassManagerImpl(int Depth) :
124 Pass((intptr_t)&ID), PMDataManager(Depth),
125 PMTopLevelManager(TLM_Function) { }
Devang Patel5f4ddf52006-12-19 19:46:59 +0000126
127 /// add - Add a pass to the queue of passes to run. This passes ownership of
128 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
129 /// will be destroyed as well, so there is no need to delete the pass. This
130 /// implies that all passes MUST be allocated with 'new'.
131 void add(Pass *P) {
132 schedulePass(P);
133 }
134
135 /// run - Execute all of the passes scheduled for execution. Keep track of
136 /// whether any of the passes modifies the module, and if so, return true.
137 bool run(Function &F);
138
139 /// doInitialization - Run all of the initializers for the function passes.
140 ///
141 bool doInitialization(Module &M);
142
143 /// doFinalization - Run all of the initializers for the function passes.
144 ///
145 bool doFinalization(Module &M);
146
147 /// Pass Manager itself does not invalidate any analysis info.
148 void getAnalysisUsage(AnalysisUsage &Info) const {
149 Info.setPreservesAll();
150 }
151
152 inline void addTopLevelPass(Pass *P) {
153
154 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
155
156 // P is a immutable pass and it will be managed by this
157 // top level manager. Set up analysis resolver to connect them.
Devang Patelcde53d32007-01-05 22:47:07 +0000158 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel5f4ddf52006-12-19 19:46:59 +0000159 P->setResolver(AR);
160 initializeAnalysisImpl(P);
161 addImmutablePass(IP);
162 recordAvailableAnalysis(IP);
Devang Patela0dd9872007-01-12 17:23:48 +0000163 } else {
Devang Patela0dd9872007-01-12 17:23:48 +0000164 P->assignPassManager(activeStack);
Devang Patel5f4ddf52006-12-19 19:46:59 +0000165 }
Devang Patela0dd9872007-01-12 17:23:48 +0000166
Devang Patel5f4ddf52006-12-19 19:46:59 +0000167 }
168
169 FPPassManager *getContainedManager(unsigned N) {
170 assert ( N < PassManagers.size() && "Pass number out of range!");
171 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
172 return FP;
173 }
Devang Patel5f4ddf52006-12-19 19:46:59 +0000174};
175
Devang Patel19974732007-05-03 01:11:54 +0000176char FunctionPassManagerImpl::ID = 0;
Devang Patel5f4ddf52006-12-19 19:46:59 +0000177//===----------------------------------------------------------------------===//
178// MPPassManager
179//
180/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelc67c9382006-11-08 10:05:38 +0000181/// It batches all Module passes passes and function pass managers together and
182/// sequence them to process one module.
Devang Patel5f4ddf52006-12-19 19:46:59 +0000183class MPPassManager : public Pass, public PMDataManager {
Devang Patelc67c9382006-11-08 10:05:38 +0000184
185public:
Devang Patel19974732007-05-03 01:11:54 +0000186 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +0000187 MPPassManager(int Depth) : Pass((intptr_t)&ID), PMDataManager(Depth) { }
Devang Patel693941b2007-04-16 20:39:59 +0000188
189 // Delete on the fly managers.
190 virtual ~MPPassManager() {
Devang Pateldfa1ec32007-04-26 17:50:19 +0000191 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel693941b2007-04-16 20:39:59 +0000192 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
193 I != E; ++I) {
Devang Pateldfa1ec32007-04-26 17:50:19 +0000194 FunctionPassManagerImpl *FPP = I->second;
Devang Patel693941b2007-04-16 20:39:59 +0000195 delete FPP;
196 }
197 }
198
Devang Patelc67c9382006-11-08 10:05:38 +0000199 /// run - Execute all of the passes scheduled for execution. Keep track of
200 /// whether any of the passes modifies the module, and if so, return true.
201 bool runOnModule(Module &M);
Devang Patelbe6d5152006-11-13 22:40:09 +0000202
Devang Patel66d72e12006-12-07 19:57:52 +0000203 /// Pass Manager itself does not invalidate any analysis info.
204 void getAnalysisUsage(AnalysisUsage &Info) const {
205 Info.setPreservesAll();
206 }
207
Devang Patel569a6fd2007-04-16 20:12:57 +0000208 /// Add RequiredPass into list of lower level passes required by pass P.
209 /// RequiredPass is run on the fly by Pass Manager when P requests it
210 /// through getAnalysis interface.
211 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
212
Devang Patel0ed8df32007-04-16 20:27:05 +0000213 /// Return function pass corresponding to PassInfo PI, that is
214 /// required by module pass MP. Instantiate analysis pass, by using
215 /// its runOnFunction() for function F.
216 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
217
Devang Patele27ae7e2007-02-01 22:08:25 +0000218 virtual const char *getPassName() const {
219 return "Module Pass Manager";
220 }
221
Devang Patelebc09222006-12-12 23:34:33 +0000222 // Print passes managed by this manager
223 void dumpPassStructure(unsigned Offset) {
224 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patel1554c852006-12-16 00:56:26 +0000225 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
226 ModulePass *MP = getContainedPass(Index);
227 MP->dumpPassStructure(Offset + 1);
Devang Pateldfa1ec32007-04-26 17:50:19 +0000228 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
Devang Patel693941b2007-04-16 20:39:59 +0000229 FPP->dumpPassStructure(Offset + 2);
Devang Patel1554c852006-12-16 00:56:26 +0000230 dumpLastUses(MP, Offset+1);
Devang Patelebc09222006-12-12 23:34:33 +0000231 }
232 }
233
Devang Patel1554c852006-12-16 00:56:26 +0000234 ModulePass *getContainedPass(unsigned N) {
235 assert ( N < PassVector.size() && "Pass number out of range!");
236 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
237 return MP;
238 }
239
Devang Patel84da80d2007-02-27 15:00:39 +0000240 virtual PassManagerType getPassManagerType() const {
241 return PMT_ModulePassManager;
242 }
Devang Patel0ed8df32007-04-16 20:27:05 +0000243
244 private:
245 /// Collection of on the fly FPPassManagers. These managers manage
246 /// function passes that are required by module passes.
Devang Pateldfa1ec32007-04-26 17:50:19 +0000247 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelc67c9382006-11-08 10:05:38 +0000248};
249
Devang Patel19974732007-05-03 01:11:54 +0000250char MPPassManager::ID = 0;
Devang Patel7e601a72006-12-12 22:47:13 +0000251//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000252// PassManagerImpl
Devang Patel7e601a72006-12-12 22:47:13 +0000253//
Devang Patel794fd752007-05-01 21:15:47 +0000254
Devang Patel5f4ddf52006-12-19 19:46:59 +0000255/// PassManagerImpl manages MPPassManagers
256class PassManagerImpl : public Pass,
Devang Patel36bcb822007-01-11 22:15:30 +0000257 public PMDataManager,
258 public PMTopLevelManager {
Devang Patel5a39b2e2006-11-08 10:29:57 +0000259
260public:
Devang Patel19974732007-05-03 01:11:54 +0000261 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +0000262 PassManagerImpl(int Depth) : Pass((intptr_t)&ID), PMDataManager(Depth),
Devang Patel8f3f3d12007-01-16 02:00:38 +0000263 PMTopLevelManager(TLM_Pass) { }
Devang Patelf72d29c2006-12-07 23:24:58 +0000264
Devang Patel5a39b2e2006-11-08 10:29:57 +0000265 /// add - Add a pass to the queue of passes to run. This passes ownership of
266 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
267 /// will be destroyed as well, so there is no need to delete the pass. This
268 /// implies that all passes MUST be allocated with 'new'.
Devang Patel877bfbb2006-12-07 21:32:57 +0000269 void add(Pass *P) {
Devang Patele61b7472006-12-08 22:34:02 +0000270 schedulePass(P);
Devang Patel877bfbb2006-12-07 21:32:57 +0000271 }
Devang Patel5a39b2e2006-11-08 10:29:57 +0000272
273 /// run - Execute all of the passes scheduled for execution. Keep track of
274 /// whether any of the passes modifies the module, and if so, return true.
275 bool run(Module &M);
276
Devang Patel66d72e12006-12-07 19:57:52 +0000277 /// Pass Manager itself does not invalidate any analysis info.
278 void getAnalysisUsage(AnalysisUsage &Info) const {
279 Info.setPreservesAll();
280 }
281
Devang Pateleb0d6132006-12-07 21:27:23 +0000282 inline void addTopLevelPass(Pass *P) {
Devang Patel145e83d2006-12-08 23:53:00 +0000283
Devang Patelc9a62932006-12-08 23:57:43 +0000284 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Patel145e83d2006-12-08 23:53:00 +0000285
286 // P is a immutable pass and it will be managed by this
287 // top level manager. Set up analysis resolver to connect them.
Devang Patelcde53d32007-01-05 22:47:07 +0000288 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel145e83d2006-12-08 23:53:00 +0000289 P->setResolver(AR);
Devang Patel689c6832006-12-12 22:21:37 +0000290 initializeAnalysisImpl(P);
Devang Patelc9a62932006-12-08 23:57:43 +0000291 addImmutablePass(IP);
Devang Patel689c6832006-12-12 22:21:37 +0000292 recordAvailableAnalysis(IP);
Devang Patela0dd9872007-01-12 17:23:48 +0000293 } else {
Devang Patela0dd9872007-01-12 17:23:48 +0000294 P->assignPassManager(activeStack);
Devang Patel145e83d2006-12-08 23:53:00 +0000295 }
Devang Patela0dd9872007-01-12 17:23:48 +0000296
Devang Pateleb0d6132006-12-07 21:27:23 +0000297 }
298
Devang Patel5f4ddf52006-12-19 19:46:59 +0000299 MPPassManager *getContainedManager(unsigned N) {
300 assert ( N < PassManagers.size() && "Pass number out of range!");
301 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
302 return MP;
303 }
304
Devang Patel5a39b2e2006-11-08 10:29:57 +0000305};
306
Devang Patel19974732007-05-03 01:11:54 +0000307char PassManagerImpl::ID = 0;
Devang Patelc874eb52007-01-29 23:10:37 +0000308} // End of llvm namespace
309
310namespace {
311
312//===----------------------------------------------------------------------===//
313// TimingInfo Class - This class is used to calculate information about the
314// amount of time each pass takes to execute. This only happens when
315// -time-passes is enabled on the command line.
316//
317
318class VISIBILITY_HIDDEN TimingInfo {
319 std::map<Pass*, Timer> TimingData;
320 TimerGroup TG;
321
322public:
323 // Use 'create' member to get this.
324 TimingInfo() : TG("... Pass execution timing report ...") {}
325
326 // TimingDtor - Print out information about timing information
327 ~TimingInfo() {
328 // Delete all of the timers...
329 TimingData.clear();
330 // TimerGroup is deleted next, printing the report.
331 }
332
333 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
334 // to a non null value (if the -time-passes option is enabled) or it leaves it
335 // null. It may be called multiple times.
336 static void createTheTimeInfo();
337
338 void passStarted(Pass *P) {
339
340 if (dynamic_cast<PMDataManager *>(P))
341 return;
342
343 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
344 if (I == TimingData.end())
345 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
346 I->second.startTimer();
347 }
348 void passEnded(Pass *P) {
349
350 if (dynamic_cast<PMDataManager *>(P))
351 return;
352
353 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
354 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
355 I->second.stopTimer();
356 }
357};
358
Devang Patel8e58a1b2006-12-14 00:59:42 +0000359static TimingInfo *TheTimeInfo;
360
Devang Patelc874eb52007-01-29 23:10:37 +0000361} // End of anon namespace
Devang Patelc67c9382006-11-08 10:05:38 +0000362
Devang Patel06e86562006-12-07 19:39:39 +0000363//===----------------------------------------------------------------------===//
Devang Patel1b8d0152006-12-12 22:35:25 +0000364// PMTopLevelManager implementation
365
Devang Patel8f3f3d12007-01-16 02:00:38 +0000366/// Initialize top level manager. Create first pass manager.
367PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
368
369 if (t == TLM_Pass) {
370 MPPassManager *MPP = new MPPassManager(1);
371 MPP->setTopLevelManager(this);
372 addPassManager(MPP);
373 activeStack.push(MPP);
374 }
375 else if (t == TLM_Function) {
376 FPPassManager *FPP = new FPPassManager(1);
377 FPP->setTopLevelManager(this);
378 addPassManager(FPP);
379 activeStack.push(FPP);
380 }
381}
382
Devang Patel1b8d0152006-12-12 22:35:25 +0000383/// Set pass P as the last user of the given analysis passes.
384void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
385 Pass *P) {
386
387 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
388 E = AnalysisPasses.end(); I != E; ++I) {
389 Pass *AP = *I;
390 LastUser[AP] = P;
Devang Pateld46825c2007-03-08 19:05:01 +0000391
392 if (P == AP)
393 continue;
394
Devang Patel1b8d0152006-12-12 22:35:25 +0000395 // If AP is the last user of other passes then make P last user of
396 // such passes.
397 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
398 LUE = LastUser.end(); LUI != LUE; ++LUI) {
399 if (LUI->second == AP)
400 LastUser[LUI->first] = P;
401 }
402 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000403}
404
405/// Collect passes whose last user is P
406void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
407 Pass *P) {
Chris Lattner63925c82007-02-17 23:14:24 +0000408 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
409 LUE = LastUser.end(); LUI != LUE; ++LUI)
410 if (LUI->second == P)
411 LastUses.push_back(LUI->first);
Devang Patel1b8d0152006-12-12 22:35:25 +0000412}
413
414/// Schedule pass P for execution. Make sure that passes required by
415/// P are run before P is run. Update analysis info maintained by
416/// the manager. Remove dead passes. This is a recursive function.
417void PMTopLevelManager::schedulePass(Pass *P) {
418
Devang Patel9d133e12007-01-16 21:43:18 +0000419 // TODO : Allocate function manager for this pass, other wise required set
420 // may be inserted into previous function manager
Devang Patel1b8d0152006-12-12 22:35:25 +0000421
Devang Patel90f1ad72007-02-05 19:34:17 +0000422 // If this Analysis is already requested by one of the previous pass
423 // and it is still available then do not insert new pass in the queue again.
424 if (findAnalysisPass(P->getPassInfo()))
425 return;
426
Devang Patel22a1cf92007-03-06 01:06:16 +0000427 // Give pass a chance to prepare the stage.
428 P->preparePassManager(activeStack);
429
Devang Patel1b8d0152006-12-12 22:35:25 +0000430 AnalysisUsage AnUsage;
431 P->getAnalysisUsage(AnUsage);
432 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
433 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
434 E = RequiredSet.end(); I != E; ++I) {
435
436 Pass *AnalysisPass = findAnalysisPass(*I);
437 if (!AnalysisPass) {
Devang Patel1b8d0152006-12-12 22:35:25 +0000438 AnalysisPass = (*I)->createPass();
Devang Patel569a6fd2007-04-16 20:12:57 +0000439 // Schedule this analysis run first only if it is not a lower level
440 // analysis pass. Lower level analsyis passes are run on the fly.
441 if (P->getPotentialPassManagerType () >=
442 AnalysisPass->getPotentialPassManagerType())
443 schedulePass(AnalysisPass);
444 else
445 delete AnalysisPass;
Devang Patel1b8d0152006-12-12 22:35:25 +0000446 }
447 }
448
449 // Now all required passes are available.
450 addTopLevelPass(P);
451}
452
453/// Find the pass that implements Analysis AID. Search immutable
454/// passes and all pass managers. If desired pass is not found
455/// then return NULL.
456Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
457
458 Pass *P = NULL;
Devang Pateld0fa16c2006-12-12 22:50:05 +0000459 // Check pass managers
460 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
461 E = PassManagers.end(); P == NULL && I != E; ++I) {
462 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
463 assert(PMD && "This is not a PassManager");
464 P = PMD->findAnalysisPass(AID, false);
465 }
466
467 // Check other pass managers
468 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
469 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
470 P = (*I)->findAnalysisPass(AID, false);
471
Devang Patel1b8d0152006-12-12 22:35:25 +0000472 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
473 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
474 const PassInfo *PI = (*I)->getPassInfo();
475 if (PI == AID)
476 P = *I;
477
478 // If Pass not found then check the interfaces implemented by Immutable Pass
479 if (!P) {
Devang Patel1a803862006-12-15 22:57:49 +0000480 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
481 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
482 P = *I;
Devang Patel1b8d0152006-12-12 22:35:25 +0000483 }
484 }
485
Devang Patel1b8d0152006-12-12 22:35:25 +0000486 return P;
487}
488
Devang Patelebc09222006-12-12 23:34:33 +0000489// Print passes managed by this top level manager.
Devang Patela52035a2006-12-15 20:13:01 +0000490void PMTopLevelManager::dumpPasses() const {
Devang Patelebc09222006-12-12 23:34:33 +0000491
Devang Patel26426942007-01-17 20:33:36 +0000492 if (PassDebugging < Structure)
Devang Patel5f4ddf52006-12-19 19:46:59 +0000493 return;
494
Devang Patelebc09222006-12-12 23:34:33 +0000495 // Print out the immutable passes
496 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
497 ImmutablePasses[i]->dumpPassStructure(0);
498 }
499
Devang Patela52035a2006-12-15 20:13:01 +0000500 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelebc09222006-12-12 23:34:33 +0000501 E = PassManagers.end(); I != E; ++I)
502 (*I)->dumpPassStructure(1);
Devang Patelebc09222006-12-12 23:34:33 +0000503}
504
Devang Patela52035a2006-12-15 20:13:01 +0000505void PMTopLevelManager::dumpArguments() const {
Devang Patelc32cf542006-12-13 22:10:00 +0000506
Devang Patel26426942007-01-17 20:33:36 +0000507 if (PassDebugging < Arguments)
Devang Patelc32cf542006-12-13 22:10:00 +0000508 return;
509
510 cerr << "Pass Arguments: ";
Devang Patela52035a2006-12-15 20:13:01 +0000511 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelc32cf542006-12-13 22:10:00 +0000512 E = PassManagers.end(); I != E; ++I) {
513 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
514 assert(PMD && "This is not a PassManager");
515 PMD->dumpPassArguments();
516 }
517 cerr << "\n";
518}
519
Devang Patel1336a6b2006-12-21 00:16:50 +0000520void PMTopLevelManager::initializeAllAnalysisInfo() {
521
522 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
523 E = PassManagers.end(); I != E; ++I) {
524 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
525 assert(PMD && "This is not a PassManager");
526 PMD->initializeAnalysisInfo();
527 }
528
529 // Initailize other pass managers
530 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
531 E = IndirectPassManagers.end(); I != E; ++I)
532 (*I)->initializeAnalysisInfo();
533}
534
Devang Patelab7752c2007-01-12 18:52:44 +0000535/// Destructor
536PMTopLevelManager::~PMTopLevelManager() {
537 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
538 E = PassManagers.end(); I != E; ++I)
539 delete *I;
540
541 for (std::vector<ImmutablePass *>::iterator
542 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
543 delete *I;
544
545 PassManagers.clear();
546}
547
Devang Patel1b8d0152006-12-12 22:35:25 +0000548//===----------------------------------------------------------------------===//
Devang Patel419f0e92006-12-07 18:36:24 +0000549// PMDataManager implementation
Devang Patel889739c2006-11-07 22:35:17 +0000550
Devang Patel2c5d1852006-11-08 01:31:28 +0000551/// Return true IFF pass P's required analysis set does not required new
Devang Patel889739c2006-11-07 22:35:17 +0000552/// manager.
Devang Patel419f0e92006-12-07 18:36:24 +0000553bool PMDataManager::manageablePass(Pass *P) {
Devang Patel889739c2006-11-07 22:35:17 +0000554
Devang Patel4045af12006-12-07 18:47:25 +0000555 // TODO
556 // If this pass is not preserving information that is required by a
557 // pass maintained by higher level pass manager then do not insert
558 // this pass into current manager. Use new manager. For example,
559 // For example, If FunctionPass F is not preserving ModulePass Info M1
560 // that is used by another ModulePass M2 then do not insert F in
561 // current function pass manager.
Devang Patel889739c2006-11-07 22:35:17 +0000562 return true;
563}
564
Devang Patelb8526162006-11-11 01:10:19 +0000565/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patelf32b4dd2006-12-07 19:33:53 +0000566void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelb899eed2006-11-14 01:59:59 +0000567
Devang Patelb8526162006-11-11 01:10:19 +0000568 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelb899eed2006-11-14 01:59:59 +0000569 AvailableAnalysis[PI] = P;
Devang Patelb8526162006-11-11 01:10:19 +0000570
Devang Patelf32b4dd2006-12-07 19:33:53 +0000571 //This pass is the current implementation of all of the interfaces it
572 //implements as well.
573 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
574 for (unsigned i = 0, e = II.size(); i != e; ++i)
575 AvailableAnalysis[II[i]] = P;
Devang Patelb8526162006-11-11 01:10:19 +0000576 }
577}
578
Devang Patel7b65dd92007-03-06 17:52:53 +0000579// Return true if P preserves high level analysis used by other
580// passes managed by this manager
581bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
582
583 AnalysisUsage AnUsage;
584 P->getAnalysisUsage(AnUsage);
585
586 if (AnUsage.getPreservesAll())
587 return true;
588
589 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
590 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
591 E = HigherLevelAnalysis.end(); I != E; ++I) {
592 Pass *P1 = *I;
Devang Pateld46825c2007-03-08 19:05:01 +0000593 if (!dynamic_cast<ImmutablePass*>(P1)
594 && std::find(PreservedSet.begin(), PreservedSet.end(), P1->getPassInfo()) ==
595 PreservedSet.end())
596 return false;
Devang Patel7b65dd92007-03-06 17:52:53 +0000597 }
598
599 return true;
600}
601
Devang Patel889739c2006-11-07 22:35:17 +0000602/// Remove Analyss not preserved by Pass P
Devang Patel419f0e92006-12-07 18:36:24 +0000603void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel14d65812006-11-11 01:24:55 +0000604 AnalysisUsage AnUsage;
605 P->getAnalysisUsage(AnUsage);
Devang Patel889739c2006-11-07 22:35:17 +0000606
Devang Patel04b4e052006-12-07 20:03:49 +0000607 if (AnUsage.getPreservesAll())
608 return;
609
610 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelb899eed2006-11-14 01:59:59 +0000611 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel54e247d2006-12-12 23:07:44 +0000612 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel1a803862006-12-15 22:57:49 +0000613 std::map<AnalysisID, Pass*>::iterator Info = I++;
Devang Pateld46825c2007-03-08 19:05:01 +0000614 if (!dynamic_cast<ImmutablePass*>(Info->second)
615 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
616 PreservedSet.end())
Devang Patel14d65812006-11-11 01:24:55 +0000617 // Remove this analysis
Devang Pateld46825c2007-03-08 19:05:01 +0000618 AvailableAnalysis.erase(Info);
Devang Patel14d65812006-11-11 01:24:55 +0000619 }
Devang Patelfe613902007-03-06 01:55:46 +0000620
621 // Check inherited analysis also. If P is not preserving analysis
622 // provided by parent manager then remove it here.
623 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
624
625 if (!InheritedAnalysis[Index])
626 continue;
627
628 for (std::map<AnalysisID, Pass*>::iterator
629 I = InheritedAnalysis[Index]->begin(),
630 E = InheritedAnalysis[Index]->end(); I != E; ) {
631 std::map<AnalysisID, Pass *>::iterator Info = I++;
Devang Pateld46825c2007-03-08 19:05:01 +0000632 if (!dynamic_cast<ImmutablePass*>(Info->second)
633 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
634 PreservedSet.end())
Devang Patelfe613902007-03-06 01:55:46 +0000635 // Remove this analysis
Devang Pateld46825c2007-03-08 19:05:01 +0000636 InheritedAnalysis[Index]->erase(Info);
Devang Patelfe613902007-03-06 01:55:46 +0000637 }
638 }
639
Devang Patel889739c2006-11-07 22:35:17 +0000640}
641
Devang Pateldf1a10e2006-11-14 03:05:08 +0000642/// Remove analysis passes that are not used any longer
Devang Patel7f997612007-03-05 20:01:30 +0000643void PMDataManager::removeDeadPasses(Pass *P, std::string Msg,
644 enum PassDebuggingString DBG_STR) {
Devang Patelf9a60ae2006-12-08 00:37:52 +0000645
646 std::vector<Pass *> DeadPasses;
Devang Patel0ed8df32007-04-16 20:27:05 +0000647
Devang Patel693941b2007-04-16 20:39:59 +0000648 // If this is a on the fly manager then it does not have TPM.
Devang Patel0ed8df32007-04-16 20:27:05 +0000649 if (!TPM)
650 return;
651
Devang Patelf9a60ae2006-12-08 00:37:52 +0000652 TPM->collectLastUses(DeadPasses, P);
653
654 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
655 E = DeadPasses.end(); I != E; ++I) {
Devang Patel4eeea772006-12-13 23:50:44 +0000656
Devang Patel7f997612007-03-05 20:01:30 +0000657 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
Devang Patel4eeea772006-12-13 23:50:44 +0000658
Devang Patel55d5ac72007-03-05 18:20:51 +0000659 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
Devang Patelf9a60ae2006-12-08 00:37:52 +0000660 (*I)->releaseMemory();
Devang Patel55d5ac72007-03-05 18:20:51 +0000661 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
Devang Patel8e58a1b2006-12-14 00:59:42 +0000662
Devang Patelf9a60ae2006-12-08 00:37:52 +0000663 std::map<AnalysisID, Pass*>::iterator Pos =
664 AvailableAnalysis.find((*I)->getPassInfo());
665
Devang Patel964e45e2006-12-08 00:59:05 +0000666 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patelf9a60ae2006-12-08 00:37:52 +0000667 if (Pos != AvailableAnalysis.end())
668 AvailableAnalysis.erase(Pos);
669 }
Devang Pateldf1a10e2006-11-14 03:05:08 +0000670}
671
Devang Patel4045af12006-12-07 18:47:25 +0000672/// Add pass P into the PassVector. Update
Devang Patel893a5a62006-11-11 02:04:19 +0000673/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patele24e0e12007-01-12 20:07:16 +0000674void PMDataManager::add(Pass *P,
Devang Patel66eeb492007-01-15 23:06:56 +0000675 bool ProcessAnalysis) {
Devang Patele2533852006-11-11 01:51:02 +0000676
Devang Patel145e83d2006-12-08 23:53:00 +0000677 // This manager is going to manage pass P. Set up analysis resolver
678 // to connect them.
Devang Patelcde53d32007-01-05 22:47:07 +0000679 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel145e83d2006-12-08 23:53:00 +0000680 P->setResolver(AR);
681
Devang Patelcf5fb2b2007-03-05 22:57:49 +0000682 // If a FunctionPass F is the last user of ModulePass info M
683 // then the F's manager, not F, records itself as a last user of M.
684 std::vector<Pass *> TransferLastUses;
685
Devang Patel893a5a62006-11-11 02:04:19 +0000686 if (ProcessAnalysis) {
Devang Patele1663402006-12-07 23:55:10 +0000687
688 // At the moment, this pass is the last user of all required passes.
689 std::vector<Pass *> LastUses;
Devang Patel569a6fd2007-04-16 20:12:57 +0000690 SmallVector<Pass *, 8> RequiredPasses;
691 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
692
Devang Patele1663402006-12-07 23:55:10 +0000693 unsigned PDepth = this->getDepth();
694
Devang Patel569a6fd2007-04-16 20:12:57 +0000695 collectRequiredAnalysis(RequiredPasses,
696 ReqAnalysisNotAvailable, P);
697 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
Devang Patele1663402006-12-07 23:55:10 +0000698 E = RequiredPasses.end(); I != E; ++I) {
699 Pass *PRequired = *I;
700 unsigned RDepth = 0;
Devang Patelcd520b12006-12-09 00:07:38 +0000701
702 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
703 RDepth = DM.getDepth();
704
Devang Patele1663402006-12-07 23:55:10 +0000705 if (PDepth == RDepth)
706 LastUses.push_back(PRequired);
707 else if (PDepth > RDepth) {
708 // Let the parent claim responsibility of last use
Devang Patelef89c552006-12-15 00:08:26 +0000709 TransferLastUses.push_back(PRequired);
Devang Patel7b65dd92007-03-06 17:52:53 +0000710 // Keep track of higher level analysis used by this manager.
711 HigherLevelAnalysis.push_back(PRequired);
Devang Patel569a6fd2007-04-16 20:12:57 +0000712 } else
713 assert (0 && "Unable to accomodate Required Pass");
714 }
715
Devang Patel3f5ecd72007-01-15 20:31:54 +0000716 // Set P as P's last user until someone starts using P.
717 // However, if P is a Pass Manager then it does not need
718 // to record its last user.
Devang Patel8df87092007-01-16 22:38:10 +0000719 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel3f5ecd72007-01-15 20:31:54 +0000720 LastUses.push_back(P);
Devang Patel8df87092007-01-16 22:38:10 +0000721 TPM->setLastUser(LastUses, P);
Devang Patele1663402006-12-07 23:55:10 +0000722
Devang Patelcf5fb2b2007-03-05 22:57:49 +0000723 if (!TransferLastUses.empty()) {
724 Pass *My_PM = dynamic_cast<Pass *>(this);
725 TPM->setLastUser(TransferLastUses, My_PM);
726 TransferLastUses.clear();
727 }
728
Devang Patel0ed8df32007-04-16 20:27:05 +0000729 // Now, take care of required analysises that are not available.
730 for (SmallVector<AnalysisID, 8>::iterator
731 I = ReqAnalysisNotAvailable.begin(),
732 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
733 Pass *AnalysisPass = (*I)->createPass();
734 this->addLowerLevelRequiredPass(P, AnalysisPass);
735 }
736
Devang Patel0ac961d2006-12-07 22:09:36 +0000737 // Take a note of analysis required and made available by this pass.
Devang Patel893a5a62006-11-11 02:04:19 +0000738 // Remove the analysis not preserved by this pass
739 removeNotPreservedAnalysis(P);
Devang Patel0ac961d2006-12-07 22:09:36 +0000740 recordAvailableAnalysis(P);
Devang Patel893a5a62006-11-11 02:04:19 +0000741 }
Devang Patele2533852006-11-11 01:51:02 +0000742
743 // Add pass
744 PassVector.push_back(P);
Devang Patele2533852006-11-11 01:51:02 +0000745}
746
Devang Patel569a6fd2007-04-16 20:12:57 +0000747
748/// Populate RP with analysis pass that are required by
749/// pass P and are available. Populate RP_NotAvail with analysis
750/// pass that are required by pass P but are not available.
751void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
752 SmallVector<AnalysisID, 8> &RP_NotAvail,
753 Pass *P) {
Devang Patelc17bbb62006-12-07 23:05:44 +0000754 AnalysisUsage AnUsage;
755 P->getAnalysisUsage(AnUsage);
756 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
757 for (std::vector<AnalysisID>::const_iterator
758 I = RequiredSet.begin(), E = RequiredSet.end();
759 I != E; ++I) {
Devang Patel569a6fd2007-04-16 20:12:57 +0000760 AnalysisID AID = *I;
761 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
762 RP.push_back(AnalysisPass);
763 else
764 RP_NotAvail.push_back(AID);
Devang Patelc17bbb62006-12-07 23:05:44 +0000765 }
Devang Patel27aaab22006-12-12 23:09:32 +0000766
767 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
768 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
769 E = IDs.end(); I != E; ++I) {
Devang Patel569a6fd2007-04-16 20:12:57 +0000770 AnalysisID AID = *I;
771 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
772 RP.push_back(AnalysisPass);
773 else
774 RP_NotAvail.push_back(AID);
Devang Patel27aaab22006-12-12 23:09:32 +0000775 }
Devang Patelc17bbb62006-12-07 23:05:44 +0000776}
777
Devang Patel2f42ed62006-11-14 21:49:36 +0000778// All Required analyses should be available to the pass as it runs! Here
779// we fill in the AnalysisImpls member of the pass so that it can
780// successfully use the getAnalysis() method to retrieve the
781// implementations it needs.
782//
Devang Patel419f0e92006-12-07 18:36:24 +0000783void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patel3799f972006-11-15 01:27:05 +0000784 AnalysisUsage AnUsage;
785 P->getAnalysisUsage(AnUsage);
Devang Patel2f42ed62006-11-14 21:49:36 +0000786
787 for (std::vector<const PassInfo *>::const_iterator
788 I = AnUsage.getRequiredSet().begin(),
789 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel69867b52006-12-08 22:30:11 +0000790 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel2f42ed62006-11-14 21:49:36 +0000791 if (Impl == 0)
Devang Patelf4bd76a2007-04-16 20:44:16 +0000792 // This may be analysis pass that is initialized on the fly.
793 // If that is not the case then it will raise an assert when it is used.
794 continue;
Devang Patelcde53d32007-01-05 22:47:07 +0000795 AnalysisResolver *AR = P->getResolver();
Devang Patel298fead2006-12-09 01:11:34 +0000796 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel2f42ed62006-11-14 21:49:36 +0000797 }
798}
799
Devang Patel69867b52006-12-08 22:30:11 +0000800/// Find the pass that implements Analysis AID. If desired pass is not found
801/// then return NULL.
802Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
803
804 // Check if AvailableAnalysis map has one entry.
805 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
806
807 if (I != AvailableAnalysis.end())
808 return I->second;
809
810 // Search Parents through TopLevelManager
811 if (SearchParent)
812 return TPM->findAnalysisPass(AID);
813
Devang Patel5b640e72006-12-09 00:09:12 +0000814 return NULL;
Devang Patel69867b52006-12-08 22:30:11 +0000815}
816
Devang Patela52035a2006-12-15 20:13:01 +0000817// Print list of passes that are last used by P.
818void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
819
820 std::vector<Pass *> LUses;
Devang Patel693941b2007-04-16 20:39:59 +0000821
822 // If this is a on the fly manager then it does not have TPM.
823 if (!TPM)
824 return;
825
Devang Patela52035a2006-12-15 20:13:01 +0000826 TPM->collectLastUses(LUses, P);
827
828 for (std::vector<Pass *>::iterator I = LUses.begin(),
829 E = LUses.end(); I != E; ++I) {
830 llvm::cerr << "--" << std::string(Offset*2, ' ');
831 (*I)->dumpPassStructure(0);
832 }
833}
834
835void PMDataManager::dumpPassArguments() const {
836 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
837 E = PassVector.end(); I != E; ++I) {
838 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
839 PMD->dumpPassArguments();
840 else
841 if (const PassInfo *PI = (*I)->getPassInfo())
842 if (!PI->isAnalysisGroup())
843 cerr << " -" << PI->getPassArgument();
844 }
845}
846
Devang Patel7f997612007-03-05 20:01:30 +0000847void PMDataManager:: dumpPassInfo(Pass *P, enum PassDebuggingString S1,
848 enum PassDebuggingString S2,
849 std::string Msg) {
Devang Patel26426942007-01-17 20:33:36 +0000850 if (PassDebugging < Executions)
Devang Patela52035a2006-12-15 20:13:01 +0000851 return;
852 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel7f997612007-03-05 20:01:30 +0000853 switch (S1) {
854 case EXECUTION_MSG:
855 cerr << "Executing Pass '" << P->getPassName();
856 break;
857 case MODIFICATION_MSG:
858 cerr << "' Made Modification '" << P->getPassName();
859 break;
860 case FREEING_MSG:
861 cerr << " Freeing Pass '" << P->getPassName();
862 break;
863 default:
864 break;
865 }
866 switch (S2) {
867 case ON_BASICBLOCK_MSG:
868 cerr << "' on BasicBlock '" << Msg << "...\n";
869 break;
870 case ON_FUNCTION_MSG:
871 cerr << "' on Function '" << Msg << "...\n";
872 break;
873 case ON_MODULE_MSG:
874 cerr << "' on Module '" << Msg << "...\n";
875 break;
876 case ON_LOOP_MSG:
877 cerr << "' on Loop " << Msg << "...\n";
878 break;
879 case ON_CG_MSG:
880 cerr << "' on Call Graph " << Msg << "...\n";
881 break;
882 default:
883 break;
884 }
Devang Patela52035a2006-12-15 20:13:01 +0000885}
886
887void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
888 const std::vector<AnalysisID> &Set)
889 const {
Devang Patel26426942007-01-17 20:33:36 +0000890 if (PassDebugging >= Details && !Set.empty()) {
Devang Patela52035a2006-12-15 20:13:01 +0000891 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
892 for (unsigned i = 0; i != Set.size(); ++i) {
893 if (i) cerr << ",";
894 cerr << " " << Set[i]->getPassName();
895 }
896 cerr << "\n";
897 }
898}
Devang Patelf3dc6d92006-12-08 23:28:54 +0000899
Devang Patelab7752c2007-01-12 18:52:44 +0000900// Destructor
901PMDataManager::~PMDataManager() {
902
903 for (std::vector<Pass *>::iterator I = PassVector.begin(),
904 E = PassVector.end(); I != E; ++I)
905 delete *I;
906
907 PassVector.clear();
908}
909
Devang Patelf3dc6d92006-12-08 23:28:54 +0000910//===----------------------------------------------------------------------===//
911// NOTE: Is this the right place to define this method ?
912// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelcde53d32007-01-05 22:47:07 +0000913Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patelf3dc6d92006-12-08 23:28:54 +0000914 return PM.findAnalysisPass(ID, dir);
915}
916
Devang Patel6b1df0e2007-04-16 20:56:24 +0000917Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
918 Function &F) {
919 return PM.getOnTheFlyPass(P, AnalysisPI, F);
920}
921
Devang Patel06e86562006-12-07 19:39:39 +0000922//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000923// BBPassManager implementation
Devang Patel55fd43f2006-11-07 21:31:57 +0000924
Devang Patel55fd43f2006-11-07 21:31:57 +0000925/// Execute all of the passes scheduled for execution by invoking
926/// runOnBasicBlock method. Keep track of whether any of the passes modifies
927/// the function, and if so, return true.
928bool
Devang Patel5f4ddf52006-12-19 19:46:59 +0000929BBPassManager::runOnFunction(Function &F) {
Devang Patel55fd43f2006-11-07 21:31:57 +0000930
Reid Spencer5cbf9852007-01-30 20:08:39 +0000931 if (F.isDeclaration())
Devang Patel1fbe2c92006-12-12 23:15:28 +0000932 return false;
933
Devang Patel3b14fbe2006-12-08 01:38:28 +0000934 bool Changed = doInitialization(F);
Devang Patelc1d6e1f2006-11-14 01:23:29 +0000935
Devang Patel55fd43f2006-11-07 21:31:57 +0000936 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel1554c852006-12-16 00:56:26 +0000937 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
938 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel017b5d92006-12-14 00:25:06 +0000939 AnalysisUsage AnUsage;
Devang Patel1554c852006-12-16 00:56:26 +0000940 BP->getAnalysisUsage(AnUsage);
Devang Patel017b5d92006-12-14 00:25:06 +0000941
Devang Patel7f997612007-03-05 20:01:30 +0000942 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patel1554c852006-12-16 00:56:26 +0000943 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patel017b5d92006-12-14 00:25:06 +0000944
Devang Patel1554c852006-12-16 00:56:26 +0000945 initializeAnalysisImpl(BP);
Devang Patel693a74e2006-12-14 00:08:04 +0000946
Devang Patel1554c852006-12-16 00:56:26 +0000947 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel55fd43f2006-11-07 21:31:57 +0000948 Changed |= BP->runOnBasicBlock(*I);
Devang Patel1554c852006-12-16 00:56:26 +0000949 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel693a74e2006-12-14 00:08:04 +0000950
Devang Patel7f997612007-03-05 20:01:30 +0000951 if (Changed)
952 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
Devang Patel1554c852006-12-16 00:56:26 +0000953 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel693a74e2006-12-14 00:08:04 +0000954
Devang Patel1554c852006-12-16 00:56:26 +0000955 removeNotPreservedAnalysis(BP);
956 recordAvailableAnalysis(BP);
Devang Patel7f997612007-03-05 20:01:30 +0000957 removeDeadPasses(BP, (*I).getName(), ON_BASICBLOCK_MSG);
958
Devang Patel55fd43f2006-11-07 21:31:57 +0000959 }
Devang Patel1a803862006-12-15 22:57:49 +0000960 return Changed |= doFinalization(F);
Devang Patel55fd43f2006-11-07 21:31:57 +0000961}
962
Devang Patel964e45e2006-12-08 00:59:05 +0000963// Implement doInitialization and doFinalization
Devang Patel5f4ddf52006-12-19 19:46:59 +0000964inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel964e45e2006-12-08 00:59:05 +0000965 bool Changed = false;
966
Devang Patel1554c852006-12-16 00:56:26 +0000967 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
968 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +0000969 Changed |= BP->doInitialization(M);
970 }
971
972 return Changed;
973}
974
Devang Patel5f4ddf52006-12-19 19:46:59 +0000975inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel964e45e2006-12-08 00:59:05 +0000976 bool Changed = false;
977
Devang Patel1554c852006-12-16 00:56:26 +0000978 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
979 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +0000980 Changed |= BP->doFinalization(M);
981 }
982
983 return Changed;
984}
985
Devang Patel5f4ddf52006-12-19 19:46:59 +0000986inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel964e45e2006-12-08 00:59:05 +0000987 bool Changed = false;
988
Devang Patel1554c852006-12-16 00:56:26 +0000989 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
990 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +0000991 Changed |= BP->doInitialization(F);
992 }
993
994 return Changed;
995}
996
Devang Patel5f4ddf52006-12-19 19:46:59 +0000997inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel964e45e2006-12-08 00:59:05 +0000998 bool Changed = false;
999
Devang Patel1554c852006-12-16 00:56:26 +00001000 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1001 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +00001002 Changed |= BP->doFinalization(F);
1003 }
1004
1005 return Changed;
1006}
1007
1008
Devang Patel06e86562006-12-07 19:39:39 +00001009//===----------------------------------------------------------------------===//
Devang Patel31626912006-12-13 02:36:01 +00001010// FunctionPassManager implementation
Devang Patel06e86562006-12-07 19:39:39 +00001011
Devang Patelc63592b2006-11-08 10:44:40 +00001012/// Create new Function pass manager
Devang Patel31626912006-12-13 02:36:01 +00001013FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001014 FPM = new FunctionPassManagerImpl(0);
Devang Pateldff33ef2006-12-12 22:02:16 +00001015 // FPM is the top level manager.
1016 FPM->setTopLevelManager(FPM);
Devang Patelb920bd82006-12-12 23:27:37 +00001017
1018 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelcde53d32007-01-05 22:47:07 +00001019 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patelb920bd82006-12-12 23:27:37 +00001020 FPM->setResolver(AR);
1021
Devang Patelcc132cd2006-12-08 18:57:16 +00001022 MP = P;
1023}
1024
Devang Patel31626912006-12-13 02:36:01 +00001025FunctionPassManager::~FunctionPassManager() {
Devang Patel37a6f792006-12-13 00:09:23 +00001026 delete FPM;
1027}
1028
Devang Patelc63592b2006-11-08 10:44:40 +00001029/// add - Add a pass to the queue of passes to run. This passes
1030/// ownership of the Pass to the PassManager. When the
1031/// PassManager_X is destroyed, the pass will be destroyed as well, so
1032/// there is no need to delete the pass. (TODO delete passes.)
1033/// This implies that all passes MUST be allocated with 'new'.
Devang Patel31626912006-12-13 02:36:01 +00001034void FunctionPassManager::add(Pass *P) {
Devang Patelc63592b2006-11-08 10:44:40 +00001035 FPM->add(P);
1036}
1037
Devang Patel214ca232006-11-15 19:39:54 +00001038/// run - Execute all of the passes scheduled for execution. Keep
1039/// track of whether any of the passes modifies the function, and if
1040/// so, return true.
1041///
Devang Patel31626912006-12-13 02:36:01 +00001042bool FunctionPassManager::run(Function &F) {
Devang Patel214ca232006-11-15 19:39:54 +00001043 std::string errstr;
1044 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlinge8156192006-12-07 01:30:32 +00001045 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel214ca232006-11-15 19:39:54 +00001046 abort();
1047 }
Devang Patelc4756922006-12-08 22:57:48 +00001048 return FPM->run(F);
Devang Patel214ca232006-11-15 19:39:54 +00001049}
1050
1051
Devang Patel3799f972006-11-15 01:27:05 +00001052/// doInitialization - Run all of the initializers for the function passes.
1053///
Devang Patel31626912006-12-13 02:36:01 +00001054bool FunctionPassManager::doInitialization() {
Devang Patel3799f972006-11-15 01:27:05 +00001055 return FPM->doInitialization(*MP->getModule());
1056}
1057
1058/// doFinalization - Run all of the initializers for the function passes.
1059///
Devang Patel31626912006-12-13 02:36:01 +00001060bool FunctionPassManager::doFinalization() {
Devang Patel3799f972006-11-15 01:27:05 +00001061 return FPM->doFinalization(*MP->getModule());
1062}
1063
Devang Patel06e86562006-12-07 19:39:39 +00001064//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001065// FunctionPassManagerImpl implementation
1066//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001067inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1068 bool Changed = false;
1069
1070 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1071 FPPassManager *FP = getContainedManager(Index);
1072 Changed |= FP->doInitialization(M);
1073 }
1074
1075 return Changed;
1076}
1077
1078inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1079 bool Changed = false;
1080
1081 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1082 FPPassManager *FP = getContainedManager(Index);
1083 Changed |= FP->doFinalization(M);
1084 }
1085
1086 return Changed;
1087}
1088
1089// Execute all the passes managed by this top level manager.
1090// Return true if any function is modified by a pass.
1091bool FunctionPassManagerImpl::run(Function &F) {
1092
1093 bool Changed = false;
1094
1095 TimingInfo::createTheTimeInfo();
1096
1097 dumpArguments();
1098 dumpPasses();
1099
Devang Patel1336a6b2006-12-21 00:16:50 +00001100 initializeAllAnalysisInfo();
Devang Patel5f4ddf52006-12-19 19:46:59 +00001101 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1102 FPPassManager *FP = getContainedManager(Index);
1103 Changed |= FP->runOnFunction(F);
1104 }
1105 return Changed;
1106}
1107
1108//===----------------------------------------------------------------------===//
1109// FPPassManager implementation
Devang Patel448d27c2006-11-07 21:49:50 +00001110
Devang Patel19974732007-05-03 01:11:54 +00001111char FPPassManager::ID = 0;
Devang Patelab7752c2007-01-12 18:52:44 +00001112/// Print passes managed by this manager
1113void FPPassManager::dumpPassStructure(unsigned Offset) {
1114 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1115 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1116 FunctionPass *FP = getContainedPass(Index);
1117 FP->dumpPassStructure(Offset + 1);
1118 dumpLastUses(FP, Offset+1);
1119 }
1120}
1121
1122
Devang Patel448d27c2006-11-07 21:49:50 +00001123/// Execute all of the passes scheduled for execution by invoking
1124/// runOnFunction method. Keep track of whether any of the passes modifies
1125/// the function, and if so, return true.
Devang Patel5f4ddf52006-12-19 19:46:59 +00001126bool FPPassManager::runOnFunction(Function &F) {
Devang Patel214ca232006-11-15 19:39:54 +00001127
1128 bool Changed = false;
Devang Patel1fbe2c92006-12-12 23:15:28 +00001129
Reid Spencer5cbf9852007-01-30 20:08:39 +00001130 if (F.isDeclaration())
Devang Patel1fbe2c92006-12-12 23:15:28 +00001131 return false;
1132
Devang Patel1554c852006-12-16 00:56:26 +00001133 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1134 FunctionPass *FP = getContainedPass(Index);
1135
Devang Patel017b5d92006-12-14 00:25:06 +00001136 AnalysisUsage AnUsage;
Devang Patel1554c852006-12-16 00:56:26 +00001137 FP->getAnalysisUsage(AnUsage);
Devang Patel693a74e2006-12-14 00:08:04 +00001138
Devang Patel7f997612007-03-05 20:01:30 +00001139 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patel1554c852006-12-16 00:56:26 +00001140 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel693a74e2006-12-14 00:08:04 +00001141
Devang Patel1554c852006-12-16 00:56:26 +00001142 initializeAnalysisImpl(FP);
Devang Patel8e58a1b2006-12-14 00:59:42 +00001143
Devang Patel1554c852006-12-16 00:56:26 +00001144 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel214ca232006-11-15 19:39:54 +00001145 Changed |= FP->runOnFunction(F);
Devang Patel1554c852006-12-16 00:56:26 +00001146 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel693a74e2006-12-14 00:08:04 +00001147
Devang Patel7f997612007-03-05 20:01:30 +00001148 if (Changed)
1149 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Devang Patel1554c852006-12-16 00:56:26 +00001150 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel693a74e2006-12-14 00:08:04 +00001151
Devang Patel1554c852006-12-16 00:56:26 +00001152 removeNotPreservedAnalysis(FP);
1153 recordAvailableAnalysis(FP);
Devang Patel7f997612007-03-05 20:01:30 +00001154 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel214ca232006-11-15 19:39:54 +00001155 }
1156 return Changed;
1157}
1158
Devang Patel5f4ddf52006-12-19 19:46:59 +00001159bool FPPassManager::runOnModule(Module &M) {
Devang Patel214ca232006-11-15 19:39:54 +00001160
Devang Patel5f4ddf52006-12-19 19:46:59 +00001161 bool Changed = doInitialization(M);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001162
1163 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1164 this->runOnFunction(*I);
1165
1166 return Changed |= doFinalization(M);
1167}
1168
1169inline bool FPPassManager::doInitialization(Module &M) {
Devang Patel3799f972006-11-15 01:27:05 +00001170 bool Changed = false;
1171
Devang Patel1554c852006-12-16 00:56:26 +00001172 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1173 FunctionPass *FP = getContainedPass(Index);
Devang Patel3799f972006-11-15 01:27:05 +00001174 Changed |= FP->doInitialization(M);
1175 }
1176
1177 return Changed;
1178}
1179
Devang Patel5f4ddf52006-12-19 19:46:59 +00001180inline bool FPPassManager::doFinalization(Module &M) {
Devang Patel3799f972006-11-15 01:27:05 +00001181 bool Changed = false;
1182
Devang Patel1554c852006-12-16 00:56:26 +00001183 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1184 FunctionPass *FP = getContainedPass(Index);
Devang Patel3799f972006-11-15 01:27:05 +00001185 Changed |= FP->doFinalization(M);
1186 }
1187
Devang Patel3799f972006-11-15 01:27:05 +00001188 return Changed;
1189}
1190
Devang Patel06e86562006-12-07 19:39:39 +00001191//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001192// MPPassManager implementation
Devang Patel92c45ee2006-11-07 22:03:15 +00001193
Devang Patel92c45ee2006-11-07 22:03:15 +00001194/// Execute all of the passes scheduled for execution by invoking
1195/// runOnModule method. Keep track of whether any of the passes modifies
1196/// the module, and if so, return true.
1197bool
Devang Patel5f4ddf52006-12-19 19:46:59 +00001198MPPassManager::runOnModule(Module &M) {
Devang Patel92c45ee2006-11-07 22:03:15 +00001199 bool Changed = false;
Devang Patelc1d6e1f2006-11-14 01:23:29 +00001200
Devang Patel1554c852006-12-16 00:56:26 +00001201 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1202 ModulePass *MP = getContainedPass(Index);
1203
Devang Patel017b5d92006-12-14 00:25:06 +00001204 AnalysisUsage AnUsage;
Devang Patel1554c852006-12-16 00:56:26 +00001205 MP->getAnalysisUsage(AnUsage);
Devang Patel693a74e2006-12-14 00:08:04 +00001206
Devang Patel7f997612007-03-05 20:01:30 +00001207 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Devang Patel1554c852006-12-16 00:56:26 +00001208 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel693a74e2006-12-14 00:08:04 +00001209
Devang Patel1554c852006-12-16 00:56:26 +00001210 initializeAnalysisImpl(MP);
Devang Patel8e58a1b2006-12-14 00:59:42 +00001211
Devang Patel1554c852006-12-16 00:56:26 +00001212 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel92c45ee2006-11-07 22:03:15 +00001213 Changed |= MP->runOnModule(M);
Devang Patel1554c852006-12-16 00:56:26 +00001214 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel693a74e2006-12-14 00:08:04 +00001215
Devang Patel7f997612007-03-05 20:01:30 +00001216 if (Changed)
1217 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1218 M.getModuleIdentifier());
Devang Patel1554c852006-12-16 00:56:26 +00001219 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patel017b5d92006-12-14 00:25:06 +00001220
Devang Patel1554c852006-12-16 00:56:26 +00001221 removeNotPreservedAnalysis(MP);
1222 recordAvailableAnalysis(MP);
Devang Patel7f997612007-03-05 20:01:30 +00001223 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel92c45ee2006-11-07 22:03:15 +00001224 }
1225 return Changed;
1226}
1227
Devang Patel569a6fd2007-04-16 20:12:57 +00001228/// Add RequiredPass into list of lower level passes required by pass P.
1229/// RequiredPass is run on the fly by Pass Manager when P requests it
1230/// through getAnalysis interface.
1231void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1232
1233 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1234 && "Unable to handle Pass that requires lower level Analysis pass");
1235 assert ((P->getPotentialPassManagerType() <
1236 RequiredPass->getPotentialPassManagerType())
1237 && "Unable to handle Pass that requires lower level Analysis pass");
1238
Devang Pateldfa1ec32007-04-26 17:50:19 +00001239 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel0ed8df32007-04-16 20:27:05 +00001240 if (!FPP) {
Devang Pateldfa1ec32007-04-26 17:50:19 +00001241 FPP = new FunctionPassManagerImpl(0);
1242 // FPP is the top level manager.
1243 FPP->setTopLevelManager(FPP);
1244
Devang Patel0ed8df32007-04-16 20:27:05 +00001245 OnTheFlyManagers[P] = FPP;
1246 }
Devang Pateldfa1ec32007-04-26 17:50:19 +00001247 FPP->add(RequiredPass);
Devang Patel0ed8df32007-04-16 20:27:05 +00001248
Devang Pateldfa1ec32007-04-26 17:50:19 +00001249 // Register P as the last user of RequiredPass.
1250 std::vector<Pass *> LU;
1251 LU.push_back(RequiredPass);
1252 FPP->setLastUser(LU, P);
Devang Patel569a6fd2007-04-16 20:12:57 +00001253}
Devang Patel0ed8df32007-04-16 20:27:05 +00001254
1255/// Return function pass corresponding to PassInfo PI, that is
1256/// required by module pass MP. Instantiate analysis pass, by using
1257/// its runOnFunction() for function F.
1258Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1259 Function &F) {
1260 AnalysisID AID = PI;
Devang Pateldfa1ec32007-04-26 17:50:19 +00001261 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Devang Patel0ed8df32007-04-16 20:27:05 +00001262 assert (FPP && "Unable to find on the fly pass");
1263
Devang Pateldfa1ec32007-04-26 17:50:19 +00001264 FPP->run(F);
1265 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
Devang Patel0ed8df32007-04-16 20:27:05 +00001266}
1267
1268
Devang Patel06e86562006-12-07 19:39:39 +00001269//===----------------------------------------------------------------------===//
1270// PassManagerImpl implementation
Devang Patel37a6f792006-12-13 00:09:23 +00001271//
Devang Patelb30803b2006-11-07 22:23:34 +00001272/// run - Execute all of the passes scheduled for execution. Keep track of
1273/// whether any of the passes modifies the module, and if so, return true.
Devang Patel5f4ddf52006-12-19 19:46:59 +00001274bool PassManagerImpl::run(Module &M) {
Devang Patelb30803b2006-11-07 22:23:34 +00001275
Devang Patelb30803b2006-11-07 22:23:34 +00001276 bool Changed = false;
Devang Patel45dc02d2006-12-13 20:03:48 +00001277
Devang Patel8e58a1b2006-12-14 00:59:42 +00001278 TimingInfo::createTheTimeInfo();
1279
Devang Patelc32cf542006-12-13 22:10:00 +00001280 dumpArguments();
Devang Patel5f4ddf52006-12-19 19:46:59 +00001281 dumpPasses();
Devang Patel45dc02d2006-12-13 20:03:48 +00001282
Devang Patel1336a6b2006-12-21 00:16:50 +00001283 initializeAllAnalysisInfo();
Devang Patel5f4ddf52006-12-19 19:46:59 +00001284 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1285 MPPassManager *MP = getContainedManager(Index);
Devang Patela083e942006-12-08 22:47:25 +00001286 Changed |= MP->runOnModule(M);
Devang Patelb30803b2006-11-07 22:23:34 +00001287 }
1288 return Changed;
1289}
Devang Patel5a39b2e2006-11-08 10:29:57 +00001290
Devang Patel06e86562006-12-07 19:39:39 +00001291//===----------------------------------------------------------------------===//
1292// PassManager implementation
1293
Devang Patel5a39b2e2006-11-08 10:29:57 +00001294/// Create new pass manager
Devang Patel31626912006-12-13 02:36:01 +00001295PassManager::PassManager() {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001296 PM = new PassManagerImpl(0);
Devang Pateldff33ef2006-12-12 22:02:16 +00001297 // PM is the top level manager
1298 PM->setTopLevelManager(PM);
Devang Patel5a39b2e2006-11-08 10:29:57 +00001299}
1300
Devang Patel31626912006-12-13 02:36:01 +00001301PassManager::~PassManager() {
Devang Patel37a6f792006-12-13 00:09:23 +00001302 delete PM;
1303}
1304
Devang Patel5a39b2e2006-11-08 10:29:57 +00001305/// add - Add a pass to the queue of passes to run. This passes ownership of
1306/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1307/// will be destroyed as well, so there is no need to delete the pass. This
1308/// implies that all passes MUST be allocated with 'new'.
1309void
Devang Patel31626912006-12-13 02:36:01 +00001310PassManager::add(Pass *P) {
Devang Patel5a39b2e2006-11-08 10:29:57 +00001311 PM->add(P);
1312}
1313
1314/// run - Execute all of the passes scheduled for execution. Keep track of
1315/// whether any of the passes modifies the module, and if so, return true.
1316bool
Devang Patel31626912006-12-13 02:36:01 +00001317PassManager::run(Module &M) {
Devang Patel5a39b2e2006-11-08 10:29:57 +00001318 return PM->run(M);
1319}
1320
Devang Patel8e58a1b2006-12-14 00:59:42 +00001321//===----------------------------------------------------------------------===//
1322// TimingInfo Class - This class is used to calculate information about the
1323// amount of time each pass takes to execute. This only happens with
1324// -time-passes is enabled on the command line.
1325//
1326bool llvm::TimePassesIsEnabled = false;
1327static cl::opt<bool,true>
1328EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1329 cl::desc("Time each pass, printing elapsed time for each on exit"));
1330
1331// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1332// a non null value (if the -time-passes option is enabled) or it leaves it
1333// null. It may be called multiple times.
1334void TimingInfo::createTheTimeInfo() {
1335 if (!TimePassesIsEnabled || TheTimeInfo) return;
1336
1337 // Constructed the first time this is called, iff -time-passes is enabled.
1338 // This guarantees that the object will be constructed before static globals,
1339 // thus it will be destroyed before them.
1340 static ManagedStatic<TimingInfo> TTI;
1341 TheTimeInfo = &*TTI;
1342}
1343
Devang Patelc874eb52007-01-29 23:10:37 +00001344/// If TimingInfo is enabled then start pass timer.
1345void StartPassTimer(Pass *P) {
1346 if (TheTimeInfo)
1347 TheTimeInfo->passStarted(P);
1348}
1349
1350/// If TimingInfo is enabled then stop pass timer.
1351void StopPassTimer(Pass *P) {
1352 if (TheTimeInfo)
1353 TheTimeInfo->passEnded(P);
1354}
1355
Devang Patel09e6e432007-01-08 19:29:38 +00001356//===----------------------------------------------------------------------===//
1357// PMStack implementation
1358//
Devang Patel36bcb822007-01-11 22:15:30 +00001359
Devang Patel09e6e432007-01-08 19:29:38 +00001360// Pop Pass Manager from the stack and clear its analysis info.
1361void PMStack::pop() {
1362
1363 PMDataManager *Top = this->top();
1364 Top->initializeAnalysisInfo();
1365
1366 S.pop_back();
1367}
1368
1369// Push PM on the stack and set its top level manager.
Devang Patel97149732007-01-11 00:19:00 +00001370void PMStack::push(Pass *P) {
Devang Patel09e6e432007-01-08 19:29:38 +00001371
Devang Patel97149732007-01-11 00:19:00 +00001372 PMDataManager *Top = NULL;
1373 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1374 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel09e6e432007-01-08 19:29:38 +00001375
Devang Patel97149732007-01-11 00:19:00 +00001376 if (this->empty()) {
1377 Top = PM;
1378 }
1379 else {
1380 Top = this->top();
1381 PMTopLevelManager *TPM = Top->getTopLevelManager();
1382
1383 assert (TPM && "Unable to find top level manager");
1384 TPM->addIndirectPassManager(PM);
1385 PM->setTopLevelManager(TPM);
1386 }
1387
1388 AnalysisResolver *AR = new AnalysisResolver(*Top);
1389 P->setResolver(AR);
1390
1391 S.push_back(PM);
1392}
1393
1394// Dump content of the pass manager stack.
1395void PMStack::dump() {
1396 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1397 E = S.end(); I != E; ++I) {
1398 Pass *P = dynamic_cast<Pass *>(*I);
1399 printf ("%s ", P->getPassName());
1400 }
1401 if (!S.empty())
1402 printf ("\n");
Devang Patel09e6e432007-01-08 19:29:38 +00001403}
1404
Devang Patel09e6e432007-01-08 19:29:38 +00001405/// Find appropriate Module Pass Manager in the PM Stack and
1406/// add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001407void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001408 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001409
Devang Patel09e6e432007-01-08 19:29:38 +00001410 // Find Module Pass Manager
1411 while(!PMS.empty()) {
Devang Patel44b0d292007-01-17 21:19:23 +00001412 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1413 if (TopPMType == PreferredType)
1414 break; // We found desired pass manager
1415 else if (TopPMType > PMT_ModulePassManager)
Devang Patel09e6e432007-01-08 19:29:38 +00001416 PMS.pop(); // Pop children pass managers
Devang Patel6b9420e2007-01-11 19:59:06 +00001417 else
1418 break;
Devang Patel09e6e432007-01-08 19:29:38 +00001419 }
1420
Devang Patel44b0d292007-01-17 21:19:23 +00001421 PMS.top()->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001422}
1423
Devang Patel9d133e12007-01-16 21:43:18 +00001424/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1425/// in the PM Stack and add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001426void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001427 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001428
Devang Patel97149732007-01-11 00:19:00 +00001429 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel09e6e432007-01-08 19:29:38 +00001430 while(!PMS.empty()) {
Devang Patel6b9420e2007-01-11 19:59:06 +00001431 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1432 PMS.pop();
Devang Patel09e6e432007-01-08 19:29:38 +00001433 else
Devang Patel9d133e12007-01-16 21:43:18 +00001434 break;
1435 }
1436 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1437
1438 // Create new Function Pass Manager
1439 if (!FPP) {
1440 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1441 PMDataManager *PMD = PMS.top();
1442
1443 // [1] Create new Function Pass Manager
1444 FPP = new FPPassManager(PMD->getDepth() + 1);
1445
1446 // [2] Set up new manager's top level manager
1447 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1448 TPM->addIndirectPassManager(FPP);
1449
1450 // [3] Assign manager to manage this new manager. This may create
1451 // and push new managers into PMS
1452 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Patelbe1ffc62007-01-17 20:30:17 +00001453
1454 // If Call Graph Pass Manager is active then use it to manage
1455 // this new Function Pass manager.
1456 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1457 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1458 else
1459 P->assignPassManager(PMS);
Devang Patel9d133e12007-01-16 21:43:18 +00001460
1461 // [4] Push new manager into PMS
1462 PMS.push(FPP);
Devang Patel09e6e432007-01-08 19:29:38 +00001463 }
1464
Devang Patel9d133e12007-01-16 21:43:18 +00001465 // Assign FPP as the manager of this pass.
1466 FPP->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001467}
1468
Devang Patel9d133e12007-01-16 21:43:18 +00001469/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel09e6e432007-01-08 19:29:38 +00001470/// in the PM Stack and add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001471void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001472 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001473
1474 BBPassManager *BBP = NULL;
1475
Devang Patel97149732007-01-11 00:19:00 +00001476 // Basic Pass Manager is a leaf pass manager. It does not handle
1477 // any other pass manager.
1478 if (!PMS.empty()) {
Devang Patel09e6e432007-01-08 19:29:38 +00001479 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel09e6e432007-01-08 19:29:38 +00001480 }
1481
Devang Patel9d133e12007-01-16 21:43:18 +00001482 // If leaf manager is not Basic Block Pass manager then create new
1483 // basic Block Pass manager.
Devang Patel97149732007-01-11 00:19:00 +00001484
Devang Patel9d133e12007-01-16 21:43:18 +00001485 if (!BBP) {
1486 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1487 PMDataManager *PMD = PMS.top();
1488
1489 // [1] Create new Basic Block Manager
1490 BBP = new BBPassManager(PMD->getDepth() + 1);
1491
1492 // [2] Set up new manager's top level manager
1493 // Basic Block Pass Manager does not live by itself
1494 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1495 TPM->addIndirectPassManager(BBP);
1496
Devang Patel97149732007-01-11 00:19:00 +00001497 // [3] Assign manager to manage this new manager. This may create
1498 // and push new managers into PMS
Devang Patel9d133e12007-01-16 21:43:18 +00001499 Pass *P = dynamic_cast<Pass *>(BBP);
1500 P->assignPassManager(PMS);
Devang Patel97149732007-01-11 00:19:00 +00001501
Devang Patel9d133e12007-01-16 21:43:18 +00001502 // [4] Push new manager into PMS
1503 PMS.push(BBP);
1504 }
Devang Patel09e6e432007-01-08 19:29:38 +00001505
Devang Patel9d133e12007-01-16 21:43:18 +00001506 // Assign BBP as the manager of this pass.
1507 BBP->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001508}
1509
Devang Patelcccd80d2007-01-05 20:16:23 +00001510