blob: 28bd4b5439b3dbd835f675bc3dc7a84f4dd78d3f [file] [log] [blame]
Devang Patel6e5a1132006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Devang Patelca58e352006-11-08 10:05:38 +00005// This file was developed by Devang Patel and is distributed under
Devang Patel6e5a1132006-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 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 Patel6e5a1132006-11-07 21:31:57 +000017#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000018#include "llvm/ModuleProvider.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000019#include "llvm/Support/Streams.h"
Devang Patelb8817b92006-12-14 00:59:42 +000020#include "llvm/Support/ManagedStatic.h"
Devang Patela9844592006-11-11 01:31:05 +000021#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000022#include <map>
Devang Patelffca9102006-12-15 19:39:30 +000023
Devang Patele7599552007-01-12 18:52:44 +000024// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000025
Devang Patelf1567a52006-12-13 20:03:48 +000026namespace llvm {
27
28//===----------------------------------------------------------------------===//
29// Pass debugging information. Often it is useful to find out what pass is
30// running when a crash occurs in a utility. When this library is compiled with
31// debugging on, a command line option (--debug-pass) is enabled that causes the
32// pass name to be printed before it executes.
33//
34
Devang Patel03fb5872006-12-13 21:13:31 +000035// Different debug levels that can be enabled...
36enum PassDebugLevel {
37 None, Arguments, Structure, Executions, Details
38};
39
Devang Patelf1567a52006-12-13 20:03:48 +000040static cl::opt<enum PassDebugLevel>
Devang Patelfd4184322007-01-17 20:33:36 +000041PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000042 cl::desc("Print PassManager debugging information"),
43 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +000044 clEnumVal(None , "disable debug output"),
45 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
46 clEnumVal(Structure , "print pass structure before run()"),
47 clEnumVal(Executions, "print pass name before it is executed"),
48 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +000049 clEnumValEnd));
50} // End of llvm namespace
51
Devang Patelffca9102006-12-15 19:39:30 +000052namespace {
Devang Patelafb1f3622006-12-12 22:35:25 +000053
Devang Patelf33f3eb2006-12-07 19:21:29 +000054//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +000055// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +000056//
Devang Patel67d6a5e2006-12-19 19:46:59 +000057/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +000058/// pass together and sequence them to process one basic block before
59/// processing next basic block.
Devang Patel67d6a5e2006-12-19 19:46:59 +000060class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
61 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +000062
63public:
Devang Patel67d6a5e2006-12-19 19:46:59 +000064 BBPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +000065
Devang Patelca58e352006-11-08 10:05:38 +000066 /// Execute all of the passes scheduled for execution. Keep track of
67 /// whether any of the passes modifies the function, and if so, return true.
68 bool runOnFunction(Function &F);
69
Devang Patelf9d96b92006-12-07 19:57:52 +000070 /// Pass Manager itself does not invalidate any analysis info.
71 void getAnalysisUsage(AnalysisUsage &Info) const {
72 Info.setPreservesAll();
73 }
74
Devang Patel475c4532006-12-08 00:59:05 +000075 bool doInitialization(Module &M);
76 bool doInitialization(Function &F);
77 bool doFinalization(Module &M);
78 bool doFinalization(Function &F);
79
Devang Pateleda56172006-12-12 23:34:33 +000080 // Print passes managed by this manager
81 void dumpPassStructure(unsigned Offset) {
Devang Patelffca9102006-12-15 19:39:30 +000082 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +000083 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
84 BasicBlockPass *BP = getContainedPass(Index);
85 BP->dumpPassStructure(Offset + 1);
86 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +000087 }
88 }
Devang Patelabfbe3b2006-12-16 00:56:26 +000089
90 BasicBlockPass *getContainedPass(unsigned N) {
91 assert ( N < PassVector.size() && "Pass number out of range!");
92 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
93 return BP;
94 }
Devang Patel3b3f8992007-01-11 01:10:25 +000095
96 virtual PassManagerType getPassManagerType() {
97 return PMT_BasicBlockPassManager;
98 }
Devang Patelca58e352006-11-08 10:05:38 +000099};
100
Devang Patele7599552007-01-12 18:52:44 +0000101}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000102
Devang Patele7599552007-01-12 18:52:44 +0000103namespace llvm {
Devang Patelca58e352006-11-08 10:05:38 +0000104
Devang Patel10c2ca62006-12-12 22:47:13 +0000105//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000106// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000107//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000108/// FunctionPassManagerImpl manages FPPassManagers
109class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000110 public PMDataManager,
111 public PMTopLevelManager {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000112public:
113
Devang Patel4268fc02007-01-16 02:00:38 +0000114 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth),
115 PMTopLevelManager(TLM_Function) { }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000116
117 /// add - Add a pass to the queue of passes to run. This passes ownership of
118 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
119 /// will be destroyed as well, so there is no need to delete the pass. This
120 /// implies that all passes MUST be allocated with 'new'.
121 void add(Pass *P) {
122 schedulePass(P);
123 }
124
125 /// run - Execute all of the passes scheduled for execution. Keep track of
126 /// whether any of the passes modifies the module, and if so, return true.
127 bool run(Function &F);
128
129 /// doInitialization - Run all of the initializers for the function passes.
130 ///
131 bool doInitialization(Module &M);
132
133 /// doFinalization - Run all of the initializers for the function passes.
134 ///
135 bool doFinalization(Module &M);
136
137 /// Pass Manager itself does not invalidate any analysis info.
138 void getAnalysisUsage(AnalysisUsage &Info) const {
139 Info.setPreservesAll();
140 }
141
142 inline void addTopLevelPass(Pass *P) {
143
144 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
145
146 // P is a immutable pass and it will be managed by this
147 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000148 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000149 P->setResolver(AR);
150 initializeAnalysisImpl(P);
151 addImmutablePass(IP);
152 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000153 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000154 P->assignPassManager(activeStack);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000155 }
Devang Patel0f080042007-01-12 17:23:48 +0000156
Devang Patel67d6a5e2006-12-19 19:46:59 +0000157 }
158
159 FPPassManager *getContainedManager(unsigned N) {
160 assert ( N < PassManagers.size() && "Pass number out of range!");
161 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
162 return FP;
163 }
164
Devang Patel67d6a5e2006-12-19 19:46:59 +0000165};
166
167//===----------------------------------------------------------------------===//
168// MPPassManager
169//
170/// MPPassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000171/// It batches all Module passes passes and function pass managers together and
172/// sequence them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000173class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000174
175public:
Devang Patel0f080042007-01-12 17:23:48 +0000176 MPPassManager(int Depth) : PMDataManager(Depth) { }
Devang Patelca58e352006-11-08 10:05:38 +0000177
178 /// run - Execute all of the passes scheduled for execution. Keep track of
179 /// whether any of the passes modifies the module, and if so, return true.
180 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000181
Devang Patelf9d96b92006-12-07 19:57:52 +0000182 /// Pass Manager itself does not invalidate any analysis info.
183 void getAnalysisUsage(AnalysisUsage &Info) const {
184 Info.setPreservesAll();
185 }
186
Devang Pateleda56172006-12-12 23:34:33 +0000187 // Print passes managed by this manager
188 void dumpPassStructure(unsigned Offset) {
189 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000190 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
191 ModulePass *MP = getContainedPass(Index);
192 MP->dumpPassStructure(Offset + 1);
193 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000194 }
195 }
196
Devang Patelabfbe3b2006-12-16 00:56:26 +0000197 ModulePass *getContainedPass(unsigned N) {
198 assert ( N < PassVector.size() && "Pass number out of range!");
199 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
200 return MP;
201 }
202
Devang Patel3b3f8992007-01-11 01:10:25 +0000203 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
Devang Patelca58e352006-11-08 10:05:38 +0000204};
205
Devang Patel10c2ca62006-12-12 22:47:13 +0000206//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000207// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000208//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000209/// PassManagerImpl manages MPPassManagers
210class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000211 public PMDataManager,
212 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000213
214public:
215
Devang Patel4268fc02007-01-16 02:00:38 +0000216 PassManagerImpl(int Depth) : PMDataManager(Depth),
217 PMTopLevelManager(TLM_Pass) { }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000218
Devang Patel376fefa2006-11-08 10:29:57 +0000219 /// add - Add a pass to the queue of passes to run. This passes ownership of
220 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
221 /// will be destroyed as well, so there is no need to delete the pass. This
222 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000223 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000224 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000225 }
Devang Patel376fefa2006-11-08 10:29:57 +0000226
227 /// run - Execute all of the passes scheduled for execution. Keep track of
228 /// whether any of the passes modifies the module, and if so, return true.
229 bool run(Module &M);
230
Devang Patelf9d96b92006-12-07 19:57:52 +0000231 /// Pass Manager itself does not invalidate any analysis info.
232 void getAnalysisUsage(AnalysisUsage &Info) const {
233 Info.setPreservesAll();
234 }
235
Devang Patelabcd1d32006-12-07 21:27:23 +0000236 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000237
Devang Patelfa971cd2006-12-08 23:57:43 +0000238 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000239
240 // P is a immutable pass and it will be managed by this
241 // top level manager. Set up analysis resolver to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000242 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000243 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000244 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000245 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000246 recordAvailableAnalysis(IP);
Devang Patel0f080042007-01-12 17:23:48 +0000247 } else {
Devang Patel0f080042007-01-12 17:23:48 +0000248 P->assignPassManager(activeStack);
Devang Pateld440cd92006-12-08 23:53:00 +0000249 }
Devang Patel0f080042007-01-12 17:23:48 +0000250
Devang Patelabcd1d32006-12-07 21:27:23 +0000251 }
252
Devang Patel67d6a5e2006-12-19 19:46:59 +0000253 MPPassManager *getContainedManager(unsigned N) {
254 assert ( N < PassManagers.size() && "Pass number out of range!");
255 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
256 return MP;
257 }
258
Devang Patel376fefa2006-11-08 10:29:57 +0000259};
260
Devang Patelb8817b92006-12-14 00:59:42 +0000261static TimingInfo *TheTimeInfo;
262
Devang Patel55bcb502007-01-29 20:06:26 +0000263TimingInfo *getTheTimeInfo() {
264 return TheTimeInfo;
265}
266
267} // End of llvm namespace
Devang Patelca58e352006-11-08 10:05:38 +0000268
Devang Patela1514cb2006-12-07 19:39:39 +0000269//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000270// PMTopLevelManager implementation
271
Devang Patel4268fc02007-01-16 02:00:38 +0000272/// Initialize top level manager. Create first pass manager.
273PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
274
275 if (t == TLM_Pass) {
276 MPPassManager *MPP = new MPPassManager(1);
277 MPP->setTopLevelManager(this);
278 addPassManager(MPP);
279 activeStack.push(MPP);
280 }
281 else if (t == TLM_Function) {
282 FPPassManager *FPP = new FPPassManager(1);
283 FPP->setTopLevelManager(this);
284 addPassManager(FPP);
285 activeStack.push(FPP);
286 }
287}
288
Devang Patelafb1f3622006-12-12 22:35:25 +0000289/// Set pass P as the last user of the given analysis passes.
290void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
291 Pass *P) {
292
293 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
294 E = AnalysisPasses.end(); I != E; ++I) {
295 Pass *AP = *I;
296 LastUser[AP] = P;
297 // If AP is the last user of other passes then make P last user of
298 // such passes.
299 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
300 LUE = LastUser.end(); LUI != LUE; ++LUI) {
301 if (LUI->second == AP)
302 LastUser[LUI->first] = P;
303 }
304 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000305}
306
307/// Collect passes whose last user is P
308void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
309 Pass *P) {
310 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
311 LUE = LastUser.end(); LUI != LUE; ++LUI)
312 if (LUI->second == P)
313 LastUses.push_back(LUI->first);
314}
315
316/// Schedule pass P for execution. Make sure that passes required by
317/// P are run before P is run. Update analysis info maintained by
318/// the manager. Remove dead passes. This is a recursive function.
319void PMTopLevelManager::schedulePass(Pass *P) {
320
Devang Patel3312f752007-01-16 21:43:18 +0000321 // TODO : Allocate function manager for this pass, other wise required set
322 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000323
324 AnalysisUsage AnUsage;
325 P->getAnalysisUsage(AnUsage);
326 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
327 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
328 E = RequiredSet.end(); I != E; ++I) {
329
330 Pass *AnalysisPass = findAnalysisPass(*I);
331 if (!AnalysisPass) {
332 // Schedule this analysis run first.
333 AnalysisPass = (*I)->createPass();
334 schedulePass(AnalysisPass);
335 }
336 }
337
338 // Now all required passes are available.
339 addTopLevelPass(P);
340}
341
342/// Find the pass that implements Analysis AID. Search immutable
343/// passes and all pass managers. If desired pass is not found
344/// then return NULL.
345Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
346
347 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000348 // Check pass managers
349 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
350 E = PassManagers.end(); P == NULL && I != E; ++I) {
351 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
352 assert(PMD && "This is not a PassManager");
353 P = PMD->findAnalysisPass(AID, false);
354 }
355
356 // Check other pass managers
357 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
358 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
359 P = (*I)->findAnalysisPass(AID, false);
360
Devang Patelafb1f3622006-12-12 22:35:25 +0000361 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
362 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
363 const PassInfo *PI = (*I)->getPassInfo();
364 if (PI == AID)
365 P = *I;
366
367 // If Pass not found then check the interfaces implemented by Immutable Pass
368 if (!P) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000369 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
370 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
371 P = *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000372 }
373 }
374
Devang Patelafb1f3622006-12-12 22:35:25 +0000375 return P;
376}
377
Devang Pateleda56172006-12-12 23:34:33 +0000378// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000379void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000380
Devang Patelfd4184322007-01-17 20:33:36 +0000381 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000382 return;
383
Devang Pateleda56172006-12-12 23:34:33 +0000384 // Print out the immutable passes
385 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
386 ImmutablePasses[i]->dumpPassStructure(0);
387 }
388
Devang Patel991aeba2006-12-15 20:13:01 +0000389 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Pateleda56172006-12-12 23:34:33 +0000390 E = PassManagers.end(); I != E; ++I)
391 (*I)->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000392}
393
Devang Patel991aeba2006-12-15 20:13:01 +0000394void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000395
Devang Patelfd4184322007-01-17 20:33:36 +0000396 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000397 return;
398
399 cerr << "Pass Arguments: ";
Devang Patel991aeba2006-12-15 20:13:01 +0000400 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
Devang Patelcfd70c42006-12-13 22:10:00 +0000401 E = PassManagers.end(); I != E; ++I) {
402 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
403 assert(PMD && "This is not a PassManager");
404 PMD->dumpPassArguments();
405 }
406 cerr << "\n";
407}
408
Devang Patele3068402006-12-21 00:16:50 +0000409void PMTopLevelManager::initializeAllAnalysisInfo() {
410
411 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
412 E = PassManagers.end(); I != E; ++I) {
413 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
414 assert(PMD && "This is not a PassManager");
415 PMD->initializeAnalysisInfo();
416 }
417
418 // Initailize other pass managers
419 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
420 E = IndirectPassManagers.end(); I != E; ++I)
421 (*I)->initializeAnalysisInfo();
422}
423
Devang Patele7599552007-01-12 18:52:44 +0000424/// Destructor
425PMTopLevelManager::~PMTopLevelManager() {
426 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
427 E = PassManagers.end(); I != E; ++I)
428 delete *I;
429
430 for (std::vector<ImmutablePass *>::iterator
431 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
432 delete *I;
433
434 PassManagers.clear();
435}
436
Devang Patelafb1f3622006-12-12 22:35:25 +0000437//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000438// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000439
Devang Pateld65e9e92006-11-08 01:31:28 +0000440/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000441/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000442bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000443
Devang Patel8f677ce2006-12-07 18:47:25 +0000444 // TODO
445 // If this pass is not preserving information that is required by a
446 // pass maintained by higher level pass manager then do not insert
447 // this pass into current manager. Use new manager. For example,
448 // For example, If FunctionPass F is not preserving ModulePass Info M1
449 // that is used by another ModulePass M2 then do not insert F in
450 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000451 return true;
452}
453
Devang Patel643676c2006-11-11 01:10:19 +0000454/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000455void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000456
Devang Patel643676c2006-11-11 01:10:19 +0000457 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000458 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000459
Devang Patele9976aa2006-12-07 19:33:53 +0000460 //This pass is the current implementation of all of the interfaces it
461 //implements as well.
462 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
463 for (unsigned i = 0, e = II.size(); i != e; ++i)
464 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000465 }
466}
467
Devang Patelf68a3492006-11-07 22:35:17 +0000468/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000469void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000470 AnalysisUsage AnUsage;
471 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000472
Devang Patel2e169c32006-12-07 20:03:49 +0000473 if (AnUsage.getPreservesAll())
474 return;
475
476 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000477 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000478 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel56d48ec2006-12-15 22:57:49 +0000479 std::map<AnalysisID, Pass*>::iterator Info = I++;
480 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000481 PreservedSet.end()) {
482 // Remove this analysis
Devang Patel56d48ec2006-12-15 22:57:49 +0000483 if (!dynamic_cast<ImmutablePass*>(Info->second))
484 AvailableAnalysis.erase(Info);
485 }
Devang Patel349170f2006-11-11 01:24:55 +0000486 }
Devang Patelf68a3492006-11-07 22:35:17 +0000487}
488
Devang Patelca189262006-11-14 03:05:08 +0000489/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000490void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000491
492 std::vector<Pass *> DeadPasses;
493 TPM->collectLastUses(DeadPasses, P);
494
495 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
496 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000497
498 std::string Msg1 = " Freeing Pass '";
499 dumpPassInfo(*I, Msg1, Msg);
500
Devang Patelb8817b92006-12-14 00:59:42 +0000501 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Devang Patel17ad0962006-12-08 00:37:52 +0000502 (*I)->releaseMemory();
Devang Patelb8817b92006-12-14 00:59:42 +0000503 if (TheTimeInfo) TheTimeInfo->passEnded(P);
504
Devang Patel17ad0962006-12-08 00:37:52 +0000505 std::map<AnalysisID, Pass*>::iterator Pos =
506 AvailableAnalysis.find((*I)->getPassInfo());
507
Devang Patel475c4532006-12-08 00:59:05 +0000508 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000509 if (Pos != AvailableAnalysis.end())
510 AvailableAnalysis.erase(Pos);
511 }
Devang Patelca189262006-11-14 03:05:08 +0000512}
513
Devang Patel8f677ce2006-12-07 18:47:25 +0000514/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000515/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patelf85793d2007-01-12 20:07:16 +0000516void PMDataManager::add(Pass *P,
Devang Patel6975b6e2007-01-15 23:06:56 +0000517 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000518
Devang Pateld440cd92006-12-08 23:53:00 +0000519 // This manager is going to manage pass P. Set up analysis resolver
520 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000521 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000522 P->setResolver(AR);
523
Devang Patel90b05e02006-11-11 02:04:19 +0000524 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000525
526 // At the moment, this pass is the last user of all required passes.
527 std::vector<Pass *> LastUses;
528 std::vector<Pass *> RequiredPasses;
529 unsigned PDepth = this->getDepth();
530
531 collectRequiredAnalysisPasses(RequiredPasses, P);
532 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
533 E = RequiredPasses.end(); I != E; ++I) {
534 Pass *PRequired = *I;
535 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000536
537 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
538 RDepth = DM.getDepth();
539
Devang Patelbc03f132006-12-07 23:55:10 +0000540 if (PDepth == RDepth)
541 LastUses.push_back(PRequired);
542 else if (PDepth > RDepth) {
543 // Let the parent claim responsibility of last use
Devang Patel832bc072006-12-15 00:08:26 +0000544 TransferLastUses.push_back(PRequired);
Devang Patelbc03f132006-12-07 23:55:10 +0000545 } else {
546 // Note : This feature is not yet implemented
547 assert (0 &&
548 "Unable to handle Pass that requires lower level Analysis pass");
549 }
550 }
551
Devang Patel39786a92007-01-15 20:31:54 +0000552 // Set P as P's last user until someone starts using P.
553 // However, if P is a Pass Manager then it does not need
554 // to record its last user.
Devang Pateld85662f2007-01-16 22:38:10 +0000555 if (!dynamic_cast<PMDataManager *>(P))
Devang Patel39786a92007-01-15 20:31:54 +0000556 LastUses.push_back(P);
Devang Pateld85662f2007-01-16 22:38:10 +0000557 TPM->setLastUser(LastUses, P);
Devang Patelbc03f132006-12-07 23:55:10 +0000558
Devang Patel17bff0d2006-12-07 22:09:36 +0000559 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000560 // Remove the analysis not preserved by this pass
561 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000562 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000563 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000564
565 // Add pass
566 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000567}
568
Devang Patel1d6267c2006-12-07 23:05:44 +0000569/// Populate RequiredPasses with the analysis pass that are required by
570/// pass P.
571void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
572 Pass *P) {
573 AnalysisUsage AnUsage;
574 P->getAnalysisUsage(AnUsage);
575 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
576 for (std::vector<AnalysisID>::const_iterator
577 I = RequiredSet.begin(), E = RequiredSet.end();
578 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000579 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000580 assert (AnalysisPass && "Analysis pass is not available");
581 RP.push_back(AnalysisPass);
582 }
Devang Patelf58183d2006-12-12 23:09:32 +0000583
584 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
585 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
586 E = IDs.end(); I != E; ++I) {
587 Pass *AnalysisPass = findAnalysisPass(*I, true);
588 assert (AnalysisPass && "Analysis pass is not available");
589 RP.push_back(AnalysisPass);
590 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000591}
592
Devang Patel07f4f582006-11-14 21:49:36 +0000593// All Required analyses should be available to the pass as it runs! Here
594// we fill in the AnalysisImpls member of the pass so that it can
595// successfully use the getAnalysis() method to retrieve the
596// implementations it needs.
597//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000598void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000599 AnalysisUsage AnUsage;
600 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000601
602 for (std::vector<const PassInfo *>::const_iterator
603 I = AnUsage.getRequiredSet().begin(),
604 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000605 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000606 if (Impl == 0)
607 assert(0 && "Analysis used but not available!");
Devang Patelb66334b2007-01-05 22:47:07 +0000608 AnalysisResolver *AR = P->getResolver();
Devang Patel984698a2006-12-09 01:11:34 +0000609 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000610 }
611}
612
Devang Patel640c5bb2006-12-08 22:30:11 +0000613/// Find the pass that implements Analysis AID. If desired pass is not found
614/// then return NULL.
615Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
616
617 // Check if AvailableAnalysis map has one entry.
618 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
619
620 if (I != AvailableAnalysis.end())
621 return I->second;
622
623 // Search Parents through TopLevelManager
624 if (SearchParent)
625 return TPM->findAnalysisPass(AID);
626
Devang Patel9d759b82006-12-09 00:09:12 +0000627 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000628}
629
Devang Patel991aeba2006-12-15 20:13:01 +0000630// Print list of passes that are last used by P.
631void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
632
633 std::vector<Pass *> LUses;
634
635 assert (TPM && "Top Level Manager is missing");
636 TPM->collectLastUses(LUses, P);
637
638 for (std::vector<Pass *>::iterator I = LUses.begin(),
639 E = LUses.end(); I != E; ++I) {
640 llvm::cerr << "--" << std::string(Offset*2, ' ');
641 (*I)->dumpPassStructure(0);
642 }
643}
644
645void PMDataManager::dumpPassArguments() const {
646 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
647 E = PassVector.end(); I != E; ++I) {
648 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
649 PMD->dumpPassArguments();
650 else
651 if (const PassInfo *PI = (*I)->getPassInfo())
652 if (!PI->isAnalysisGroup())
653 cerr << " -" << PI->getPassArgument();
654 }
655}
656
657void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
658 std::string &Msg2) const {
Devang Patelfd4184322007-01-17 20:33:36 +0000659 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +0000660 return;
661 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
662 cerr << Msg1;
663 cerr << P->getPassName();
664 cerr << Msg2;
665}
666
667void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
668 const std::vector<AnalysisID> &Set)
669 const {
Devang Patelfd4184322007-01-17 20:33:36 +0000670 if (PassDebugging >= Details && !Set.empty()) {
Devang Patel991aeba2006-12-15 20:13:01 +0000671 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
672 for (unsigned i = 0; i != Set.size(); ++i) {
673 if (i) cerr << ",";
674 cerr << " " << Set[i]->getPassName();
675 }
676 cerr << "\n";
677 }
678}
Devang Patel9bdf7d42006-12-08 23:28:54 +0000679
Devang Patele7599552007-01-12 18:52:44 +0000680// Destructor
681PMDataManager::~PMDataManager() {
682
683 for (std::vector<Pass *>::iterator I = PassVector.begin(),
684 E = PassVector.end(); I != E; ++I)
685 delete *I;
686
687 PassVector.clear();
688}
689
Devang Patel9bdf7d42006-12-08 23:28:54 +0000690//===----------------------------------------------------------------------===//
691// NOTE: Is this the right place to define this method ?
692// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
Devang Patelb66334b2007-01-05 22:47:07 +0000693Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +0000694 return PM.findAnalysisPass(ID, dir);
695}
696
Devang Patela1514cb2006-12-07 19:39:39 +0000697//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000698// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000699
Devang Patel6e5a1132006-11-07 21:31:57 +0000700/// Execute all of the passes scheduled for execution by invoking
701/// runOnBasicBlock method. Keep track of whether any of the passes modifies
702/// the function, and if so, return true.
703bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000704BBPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000705
Devang Patel745a6962006-12-12 23:15:28 +0000706 if (F.isExternal())
707 return false;
708
Devang Patele9585592006-12-08 01:38:28 +0000709 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +0000710
Devang Patel93a197c2006-12-14 00:08:04 +0000711 std::string Msg1 = "Executing Pass '";
712 std::string Msg3 = "' Made Modification '";
713
Devang Patel6e5a1132006-11-07 21:31:57 +0000714 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000715 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
716 BasicBlockPass *BP = getContainedPass(Index);
Devang Patelf6d1d212006-12-14 00:25:06 +0000717 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000718 BP->getAnalysisUsage(AnUsage);
Devang Patelf6d1d212006-12-14 00:25:06 +0000719
Devang Patel200d3052006-12-13 23:50:44 +0000720 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000721 dumpPassInfo(BP, Msg1, Msg2);
722 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
Devang Patelf6d1d212006-12-14 00:25:06 +0000723
Devang Patelabfbe3b2006-12-16 00:56:26 +0000724 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000725
Devang Patelabfbe3b2006-12-16 00:56:26 +0000726 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
Devang Patel6e5a1132006-11-07 21:31:57 +0000727 Changed |= BP->runOnBasicBlock(*I);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000728 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
Devang Patel93a197c2006-12-14 00:08:04 +0000729
730 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000731 dumpPassInfo(BP, Msg3, Msg2);
732 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000733
Devang Patelabfbe3b2006-12-16 00:56:26 +0000734 removeNotPreservedAnalysis(BP);
735 recordAvailableAnalysis(BP);
736 removeDeadPasses(BP, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +0000737 }
Devang Patel56d48ec2006-12-15 22:57:49 +0000738 return Changed |= doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000739}
740
Devang Patel475c4532006-12-08 00:59:05 +0000741// Implement doInitialization and doFinalization
Devang Patel67d6a5e2006-12-19 19:46:59 +0000742inline bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000743 bool Changed = false;
744
Devang Patelabfbe3b2006-12-16 00:56:26 +0000745 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
746 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000747 Changed |= BP->doInitialization(M);
748 }
749
750 return Changed;
751}
752
Devang Patel67d6a5e2006-12-19 19:46:59 +0000753inline bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000754 bool Changed = false;
755
Devang Patelabfbe3b2006-12-16 00:56:26 +0000756 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
757 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000758 Changed |= BP->doFinalization(M);
759 }
760
761 return Changed;
762}
763
Devang Patel67d6a5e2006-12-19 19:46:59 +0000764inline bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000765 bool Changed = false;
766
Devang Patelabfbe3b2006-12-16 00:56:26 +0000767 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
768 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000769 Changed |= BP->doInitialization(F);
770 }
771
772 return Changed;
773}
774
Devang Patel67d6a5e2006-12-19 19:46:59 +0000775inline bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000776 bool Changed = false;
777
Devang Patelabfbe3b2006-12-16 00:56:26 +0000778 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
779 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +0000780 Changed |= BP->doFinalization(F);
781 }
782
783 return Changed;
784}
785
786
Devang Patela1514cb2006-12-07 19:39:39 +0000787//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000788// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000789
Devang Patel4e12f862006-11-08 10:44:40 +0000790/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +0000791FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000792 FPM = new FunctionPassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000793 // FPM is the top level manager.
794 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000795
796 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
Devang Patelb66334b2007-01-05 22:47:07 +0000797 AnalysisResolver *AR = new AnalysisResolver(*PMD);
Devang Patel1036b652006-12-12 23:27:37 +0000798 FPM->setResolver(AR);
799
Devang Patel1f653682006-12-08 18:57:16 +0000800 MP = P;
801}
802
Devang Patelb67904d2006-12-13 02:36:01 +0000803FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000804 delete FPM;
805}
806
Devang Patel4e12f862006-11-08 10:44:40 +0000807/// add - Add a pass to the queue of passes to run. This passes
808/// ownership of the Pass to the PassManager. When the
809/// PassManager_X is destroyed, the pass will be destroyed as well, so
810/// there is no need to delete the pass. (TODO delete passes.)
811/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +0000812void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000813 FPM->add(P);
814}
815
Devang Patel9f3083e2006-11-15 19:39:54 +0000816/// run - Execute all of the passes scheduled for execution. Keep
817/// track of whether any of the passes modifies the function, and if
818/// so, return true.
819///
Devang Patelb67904d2006-12-13 02:36:01 +0000820bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000821 std::string errstr;
822 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000823 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000824 abort();
825 }
Devang Patel272908d2006-12-08 22:57:48 +0000826 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000827}
828
829
Devang Patelff631ae2006-11-15 01:27:05 +0000830/// doInitialization - Run all of the initializers for the function passes.
831///
Devang Patelb67904d2006-12-13 02:36:01 +0000832bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000833 return FPM->doInitialization(*MP->getModule());
834}
835
836/// doFinalization - Run all of the initializers for the function passes.
837///
Devang Patelb67904d2006-12-13 02:36:01 +0000838bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +0000839 return FPM->doFinalization(*MP->getModule());
840}
841
Devang Patela1514cb2006-12-07 19:39:39 +0000842//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000843// FunctionPassManagerImpl implementation
844//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000845inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
846 bool Changed = false;
847
848 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
849 FPPassManager *FP = getContainedManager(Index);
850 Changed |= FP->doInitialization(M);
851 }
852
853 return Changed;
854}
855
856inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
857 bool Changed = false;
858
859 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
860 FPPassManager *FP = getContainedManager(Index);
861 Changed |= FP->doFinalization(M);
862 }
863
864 return Changed;
865}
866
867// Execute all the passes managed by this top level manager.
868// Return true if any function is modified by a pass.
869bool FunctionPassManagerImpl::run(Function &F) {
870
871 bool Changed = false;
872
873 TimingInfo::createTheTimeInfo();
874
875 dumpArguments();
876 dumpPasses();
877
Devang Patele3068402006-12-21 00:16:50 +0000878 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +0000879 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
880 FPPassManager *FP = getContainedManager(Index);
881 Changed |= FP->runOnFunction(F);
882 }
883 return Changed;
884}
885
886//===----------------------------------------------------------------------===//
887// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000888
Devang Patele7599552007-01-12 18:52:44 +0000889/// Print passes managed by this manager
890void FPPassManager::dumpPassStructure(unsigned Offset) {
891 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
892 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
893 FunctionPass *FP = getContainedPass(Index);
894 FP->dumpPassStructure(Offset + 1);
895 dumpLastUses(FP, Offset+1);
896 }
897}
898
899
Devang Patel0c2012f2006-11-07 21:49:50 +0000900/// Execute all of the passes scheduled for execution by invoking
901/// runOnFunction method. Keep track of whether any of the passes modifies
902/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000903bool FPPassManager::runOnFunction(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000904
905 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +0000906
907 if (F.isExternal())
908 return false;
909
Devang Patel93a197c2006-12-14 00:08:04 +0000910 std::string Msg1 = "Executing Pass '";
911 std::string Msg3 = "' Made Modification '";
912
Devang Patelabfbe3b2006-12-16 00:56:26 +0000913 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
914 FunctionPass *FP = getContainedPass(Index);
915
Devang Patelf6d1d212006-12-14 00:25:06 +0000916 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000917 FP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +0000918
Devang Patel200d3052006-12-13 23:50:44 +0000919 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000920 dumpPassInfo(FP, Msg1, Msg2);
921 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000922
Devang Patelabfbe3b2006-12-16 00:56:26 +0000923 initializeAnalysisImpl(FP);
Devang Patelb8817b92006-12-14 00:59:42 +0000924
Devang Patelabfbe3b2006-12-16 00:56:26 +0000925 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
Devang Patel9f3083e2006-11-15 19:39:54 +0000926 Changed |= FP->runOnFunction(F);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000927 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
Devang Patel93a197c2006-12-14 00:08:04 +0000928
929 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +0000930 dumpPassInfo(FP, Msg3, Msg2);
931 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000932
Devang Patelabfbe3b2006-12-16 00:56:26 +0000933 removeNotPreservedAnalysis(FP);
934 recordAvailableAnalysis(FP);
935 removeDeadPasses(FP, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +0000936 }
937 return Changed;
938}
939
Devang Patel67d6a5e2006-12-19 19:46:59 +0000940bool FPPassManager::runOnModule(Module &M) {
Devang Patel9f3083e2006-11-15 19:39:54 +0000941
Devang Patel67d6a5e2006-12-19 19:46:59 +0000942 bool Changed = doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +0000943
944 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
945 this->runOnFunction(*I);
946
947 return Changed |= doFinalization(M);
948}
949
950inline bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +0000951 bool Changed = false;
952
Devang Patelabfbe3b2006-12-16 00:56:26 +0000953 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
954 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +0000955 Changed |= FP->doInitialization(M);
956 }
957
958 return Changed;
959}
960
Devang Patel67d6a5e2006-12-19 19:46:59 +0000961inline bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +0000962 bool Changed = false;
963
Devang Patelabfbe3b2006-12-16 00:56:26 +0000964 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
965 FunctionPass *FP = getContainedPass(Index);
Devang Patelff631ae2006-11-15 01:27:05 +0000966 Changed |= FP->doFinalization(M);
967 }
968
Devang Patelff631ae2006-11-15 01:27:05 +0000969 return Changed;
970}
971
Devang Patela1514cb2006-12-07 19:39:39 +0000972//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000973// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +0000974
Devang Patel05e1a972006-11-07 22:03:15 +0000975/// Execute all of the passes scheduled for execution by invoking
976/// runOnModule method. Keep track of whether any of the passes modifies
977/// the module, and if so, return true.
978bool
Devang Patel67d6a5e2006-12-19 19:46:59 +0000979MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +0000980 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000981
Devang Patel93a197c2006-12-14 00:08:04 +0000982 std::string Msg1 = "Executing Pass '";
983 std::string Msg3 = "' Made Modification '";
984
Devang Patelabfbe3b2006-12-16 00:56:26 +0000985 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
986 ModulePass *MP = getContainedPass(Index);
987
Devang Patelf6d1d212006-12-14 00:25:06 +0000988 AnalysisUsage AnUsage;
Devang Patelabfbe3b2006-12-16 00:56:26 +0000989 MP->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +0000990
Devang Patel200d3052006-12-13 23:50:44 +0000991 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000992 dumpPassInfo(MP, Msg1, Msg2);
993 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000994
Devang Patelabfbe3b2006-12-16 00:56:26 +0000995 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +0000996
Devang Patelabfbe3b2006-12-16 00:56:26 +0000997 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000998 Changed |= MP->runOnModule(M);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000999 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001000
1001 if (Changed)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001002 dumpPassInfo(MP, Msg3, Msg2);
1003 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
Devang Patelf6d1d212006-12-14 00:25:06 +00001004
Devang Patelabfbe3b2006-12-16 00:56:26 +00001005 removeNotPreservedAnalysis(MP);
1006 recordAvailableAnalysis(MP);
1007 removeDeadPasses(MP, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001008 }
1009 return Changed;
1010}
1011
Devang Patela1514cb2006-12-07 19:39:39 +00001012//===----------------------------------------------------------------------===//
1013// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001014//
Devang Patelc290c8a2006-11-07 22:23:34 +00001015/// run - Execute all of the passes scheduled for execution. Keep track of
1016/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001017bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001018
Devang Patelc290c8a2006-11-07 22:23:34 +00001019 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001020
Devang Patelb8817b92006-12-14 00:59:42 +00001021 TimingInfo::createTheTimeInfo();
1022
Devang Patelcfd70c42006-12-13 22:10:00 +00001023 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001024 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001025
Devang Patele3068402006-12-21 00:16:50 +00001026 initializeAllAnalysisInfo();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001027 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1028 MPPassManager *MP = getContainedManager(Index);
Devang Patel5bbeb492006-12-08 22:47:25 +00001029 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001030 }
1031 return Changed;
1032}
Devang Patel376fefa2006-11-08 10:29:57 +00001033
Devang Patela1514cb2006-12-07 19:39:39 +00001034//===----------------------------------------------------------------------===//
1035// PassManager implementation
1036
Devang Patel376fefa2006-11-08 10:29:57 +00001037/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001038PassManager::PassManager() {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001039 PM = new PassManagerImpl(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001040 // PM is the top level manager
1041 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001042}
1043
Devang Patelb67904d2006-12-13 02:36:01 +00001044PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001045 delete PM;
1046}
1047
Devang Patel376fefa2006-11-08 10:29:57 +00001048/// add - Add a pass to the queue of passes to run. This passes ownership of
1049/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1050/// will be destroyed as well, so there is no need to delete the pass. This
1051/// implies that all passes MUST be allocated with 'new'.
1052void
Devang Patelb67904d2006-12-13 02:36:01 +00001053PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001054 PM->add(P);
1055}
1056
1057/// run - Execute all of the passes scheduled for execution. Keep track of
1058/// whether any of the passes modifies the module, and if so, return true.
1059bool
Devang Patelb67904d2006-12-13 02:36:01 +00001060PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001061 return PM->run(M);
1062}
1063
Devang Patelb8817b92006-12-14 00:59:42 +00001064//===----------------------------------------------------------------------===//
1065// TimingInfo Class - This class is used to calculate information about the
1066// amount of time each pass takes to execute. This only happens with
1067// -time-passes is enabled on the command line.
1068//
1069bool llvm::TimePassesIsEnabled = false;
1070static cl::opt<bool,true>
1071EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1072 cl::desc("Time each pass, printing elapsed time for each on exit"));
1073
1074// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1075// a non null value (if the -time-passes option is enabled) or it leaves it
1076// null. It may be called multiple times.
1077void TimingInfo::createTheTimeInfo() {
1078 if (!TimePassesIsEnabled || TheTimeInfo) return;
1079
1080 // Constructed the first time this is called, iff -time-passes is enabled.
1081 // This guarantees that the object will be constructed before static globals,
1082 // thus it will be destroyed before them.
1083 static ManagedStatic<TimingInfo> TTI;
1084 TheTimeInfo = &*TTI;
1085}
1086
Devang Patel1c56a632007-01-08 19:29:38 +00001087//===----------------------------------------------------------------------===//
1088// PMStack implementation
1089//
Devang Patelad98d232007-01-11 22:15:30 +00001090
Devang Patel1c56a632007-01-08 19:29:38 +00001091// Pop Pass Manager from the stack and clear its analysis info.
1092void PMStack::pop() {
1093
1094 PMDataManager *Top = this->top();
1095 Top->initializeAnalysisInfo();
1096
1097 S.pop_back();
1098}
1099
1100// Push PM on the stack and set its top level manager.
Devang Patel15701b52007-01-11 00:19:00 +00001101void PMStack::push(Pass *P) {
Devang Patel1c56a632007-01-08 19:29:38 +00001102
Devang Patel15701b52007-01-11 00:19:00 +00001103 PMDataManager *Top = NULL;
1104 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1105 assert (PM && "Unable to push. Pass Manager expected");
Devang Patel1c56a632007-01-08 19:29:38 +00001106
Devang Patel15701b52007-01-11 00:19:00 +00001107 if (this->empty()) {
1108 Top = PM;
1109 }
1110 else {
1111 Top = this->top();
1112 PMTopLevelManager *TPM = Top->getTopLevelManager();
1113
1114 assert (TPM && "Unable to find top level manager");
1115 TPM->addIndirectPassManager(PM);
1116 PM->setTopLevelManager(TPM);
1117 }
1118
1119 AnalysisResolver *AR = new AnalysisResolver(*Top);
1120 P->setResolver(AR);
1121
1122 S.push_back(PM);
1123}
1124
1125// Dump content of the pass manager stack.
1126void PMStack::dump() {
1127 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1128 E = S.end(); I != E; ++I) {
1129 Pass *P = dynamic_cast<Pass *>(*I);
1130 printf ("%s ", P->getPassName());
1131 }
1132 if (!S.empty())
1133 printf ("\n");
Devang Patel1c56a632007-01-08 19:29:38 +00001134}
1135
1136// Walk Pass Manager stack and set LastUse markers if any
1137// manager is transfering this priviledge to its parent manager
1138void PMStack::handleLastUserOverflow() {
1139
1140 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1141
1142 PMDataManager *Child = *I++;
1143 if (I != E) {
1144 PMDataManager *Parent = *I++;
1145 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1146 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1147 if (!TLU.empty()) {
1148 Pass *P = dynamic_cast<Pass *>(Parent);
1149 TPM->setLastUser(TLU, P);
1150 }
1151 }
1152 }
1153}
1154
1155/// Find appropriate Module Pass Manager in the PM Stack and
1156/// add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001157void ModulePass::assignPassManager(PMStack &PMS,
1158 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001159
Devang Patel1c56a632007-01-08 19:29:38 +00001160 // Find Module Pass Manager
1161 while(!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001162 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1163 if (TopPMType == PreferredType)
1164 break; // We found desired pass manager
1165 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001166 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001167 else
1168 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001169 }
1170
Devang Patel23f8aa92007-01-17 21:19:23 +00001171 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001172}
1173
Devang Patel3312f752007-01-16 21:43:18 +00001174/// Find appropriate Function Pass Manager or Call Graph Pass Manager
1175/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001176void FunctionPass::assignPassManager(PMStack &PMS,
1177 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001178
Devang Patel15701b52007-01-11 00:19:00 +00001179 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
Devang Patel1c56a632007-01-08 19:29:38 +00001180 while(!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001181 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1182 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001183 else
Devang Patel3312f752007-01-16 21:43:18 +00001184 break;
1185 }
1186 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1187
1188 // Create new Function Pass Manager
1189 if (!FPP) {
1190 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1191 PMDataManager *PMD = PMS.top();
1192
1193 // [1] Create new Function Pass Manager
1194 FPP = new FPPassManager(PMD->getDepth() + 1);
1195
1196 // [2] Set up new manager's top level manager
1197 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1198 TPM->addIndirectPassManager(FPP);
1199
1200 // [3] Assign manager to manage this new manager. This may create
1201 // and push new managers into PMS
1202 Pass *P = dynamic_cast<Pass *>(FPP);
Devang Pateldffca632007-01-17 20:30:17 +00001203
1204 // If Call Graph Pass Manager is active then use it to manage
1205 // this new Function Pass manager.
1206 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1207 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1208 else
1209 P->assignPassManager(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001210
1211 // [4] Push new manager into PMS
1212 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001213 }
1214
Devang Patel3312f752007-01-16 21:43:18 +00001215 // Assign FPP as the manager of this pass.
1216 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001217}
1218
Devang Patel3312f752007-01-16 21:43:18 +00001219/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Devang Patel1c56a632007-01-08 19:29:38 +00001220/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001221void BasicBlockPass::assignPassManager(PMStack &PMS,
1222 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001223
1224 BBPassManager *BBP = NULL;
1225
Devang Patel15701b52007-01-11 00:19:00 +00001226 // Basic Pass Manager is a leaf pass manager. It does not handle
1227 // any other pass manager.
1228 if (!PMS.empty()) {
Devang Patel1c56a632007-01-08 19:29:38 +00001229 BBP = dynamic_cast<BBPassManager *>(PMS.top());
Devang Patel1c56a632007-01-08 19:29:38 +00001230 }
1231
Devang Patel3312f752007-01-16 21:43:18 +00001232 // If leaf manager is not Basic Block Pass manager then create new
1233 // basic Block Pass manager.
Devang Patel15701b52007-01-11 00:19:00 +00001234
Devang Patel3312f752007-01-16 21:43:18 +00001235 if (!BBP) {
1236 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1237 PMDataManager *PMD = PMS.top();
1238
1239 // [1] Create new Basic Block Manager
1240 BBP = new BBPassManager(PMD->getDepth() + 1);
1241
1242 // [2] Set up new manager's top level manager
1243 // Basic Block Pass Manager does not live by itself
1244 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1245 TPM->addIndirectPassManager(BBP);
1246
Devang Patel15701b52007-01-11 00:19:00 +00001247 // [3] Assign manager to manage this new manager. This may create
1248 // and push new managers into PMS
Devang Patel3312f752007-01-16 21:43:18 +00001249 Pass *P = dynamic_cast<Pass *>(BBP);
1250 P->assignPassManager(PMS);
Devang Patel15701b52007-01-11 00:19:00 +00001251
Devang Patel3312f752007-01-16 21:43:18 +00001252 // [4] Push new manager into PMS
1253 PMS.push(BBP);
1254 }
Devang Patel1c56a632007-01-08 19:29:38 +00001255
Devang Patel3312f752007-01-16 21:43:18 +00001256 // Assign BBP as the manager of this pass.
1257 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001258}
1259
Devang Patelc6b5a552007-01-05 20:16:23 +00001260