blob: 95941043437f3dfe8ae7b1016d645ce9775a5a65 [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
15#include "llvm/PassManager.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 Patela9844592006-11-11 01:31:05 +000020#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000021#include <map>
Devang Patel6e5a1132006-11-07 21:31:57 +000022using namespace llvm;
23
Devang Patel6fea2852006-12-07 18:23:30 +000024//===----------------------------------------------------------------------===//
25// Overview:
26// The Pass Manager Infrastructure manages passes. It's responsibilities are:
27//
28// o Manage optimization pass execution order
29// o Make required Analysis information available before pass P is run
30// o Release memory occupied by dead passes
31// o If Analysis information is dirtied by a pass then regenerate Analysis
32// information before it is consumed by another pass.
33//
34// Pass Manager Infrastructure uses multipe pass managers. They are PassManager,
35// FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class
36// hierarcy uses multiple inheritance but pass managers do not derive from
37// another pass manager.
38//
39// PassManager and FunctionPassManager are two top level pass manager that
40// represents the external interface of this entire pass manager infrastucture.
41//
42// Important classes :
43//
44// [o] class PMTopLevelManager;
45//
46// Two top level managers, PassManager and FunctionPassManager, derive from
47// PMTopLevelManager. PMTopLevelManager manages information used by top level
48// managers such as last user info.
49//
50// [o] class PMDataManager;
51//
52// PMDataManager manages information, e.g. list of available analysis info,
53// used by a pass manager to manage execution order of passes. It also provides
54// a place to implement common pass manager APIs. All pass managers derive from
55// PMDataManager.
56//
57// [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager;
58//
59// BasicBlockPassManager manages BasicBlockPasses.
60//
61// [o] class FunctionPassManager;
62//
63// This is a external interface used by JIT to manage FunctionPasses. This
64// interface relies on FunctionPassManagerImpl to do all the tasks.
65//
66// [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
67// public PMTopLevelManager;
68//
69// FunctionPassManagerImpl is a top level manager. It manages FunctionPasses
70// and BasicBlockPassManagers.
71//
72// [o] class ModulePassManager : public Pass, public PMDataManager;
73//
74// ModulePassManager manages ModulePasses and FunctionPassManagerImpls.
75//
76// [o] class PassManager;
77//
78// This is a external interface used by various tools to manages passes. It
79// relies on PassManagerImpl to do all the tasks.
80//
81// [o] class PassManagerImpl : public Pass, public PMDataManager,
82// public PMDTopLevelManager
83//
84// PassManagerImpl is a top level pass manager responsible for managing
85// ModulePassManagers.
86//===----------------------------------------------------------------------===//
87
Devang Patelf1567a52006-12-13 20:03:48 +000088namespace llvm {
89
90//===----------------------------------------------------------------------===//
91// Pass debugging information. Often it is useful to find out what pass is
92// running when a crash occurs in a utility. When this library is compiled with
93// debugging on, a command line option (--debug-pass) is enabled that causes the
94// pass name to be printed before it executes.
95//
96
Devang Patel03fb5872006-12-13 21:13:31 +000097// Different debug levels that can be enabled...
98enum PassDebugLevel {
99 None, Arguments, Structure, Executions, Details
100};
101
Devang Patelf1567a52006-12-13 20:03:48 +0000102static cl::opt<enum PassDebugLevel>
103PassDebugging_New("debug-pass", cl::Hidden,
104 cl::desc("Print PassManager debugging information"),
105 cl::values(
Devang Patel03fb5872006-12-13 21:13:31 +0000106 clEnumVal(None , "disable debug output"),
107 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
108 clEnumVal(Structure , "print pass structure before run()"),
109 clEnumVal(Executions, "print pass name before it is executed"),
110 clEnumVal(Details , "print pass details when it is executed"),
Devang Patelf1567a52006-12-13 20:03:48 +0000111 clEnumValEnd));
112} // End of llvm namespace
113
Devang Patelb67904d2006-12-13 02:36:01 +0000114#ifndef USE_OLD_PASSMANAGER
Devang Patelca58e352006-11-08 10:05:38 +0000115namespace llvm {
116
Devang Patelafb1f3622006-12-12 22:35:25 +0000117class PMDataManager;
118
Devang Patelf33f3eb2006-12-07 19:21:29 +0000119//===----------------------------------------------------------------------===//
120// PMTopLevelManager
121//
122/// PMTopLevelManager manages LastUser info and collects common APIs used by
123/// top level pass managers.
124class PMTopLevelManager {
125
126public:
127
128 inline std::vector<Pass *>::iterator passManagersBegin() {
129 return PassManagers.begin();
130 }
131
132 inline std::vector<Pass *>::iterator passManagersEnd() {
133 return PassManagers.end();
134 }
135
136 /// Schedule pass P for execution. Make sure that passes required by
137 /// P are run before P is run. Update analysis info maintained by
138 /// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000139 void schedulePass(Pass *P);
Devang Patelf33f3eb2006-12-07 19:21:29 +0000140
141 /// This is implemented by top level pass manager and used by
142 /// schedulePass() to add analysis info passes that are not available.
143 virtual void addTopLevelPass(Pass *P) = 0;
144
145 /// Set pass P as the last user of the given analysis passes.
146 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
147
148 /// Collect passes whose last user is P
149 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
150
Devang Patel640c5bb2006-12-08 22:30:11 +0000151 /// Find the pass that implements Analysis AID. Search immutable
152 /// passes and all pass managers. If desired pass is not found
153 /// then return NULL.
154 Pass *findAnalysisPass(AnalysisID AID);
155
Devang Patelb67904d2006-12-13 02:36:01 +0000156 inline void clearManagers() {
157 PassManagers.clear();
158 }
159
Devang Patelf33f3eb2006-12-07 19:21:29 +0000160 virtual ~PMTopLevelManager() {
Devang Patelab97cf42006-12-13 00:09:23 +0000161
162 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
163 E = PassManagers.end(); I != E; ++I)
164 delete *I;
165
166 for (std::vector<ImmutablePass *>::iterator
167 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
168 delete *I;
169
Devang Patelf33f3eb2006-12-07 19:21:29 +0000170 PassManagers.clear();
171 }
172
Devang Patele0eb9d82006-12-07 20:51:18 +0000173 /// Add immutable pass and initialize it.
174 inline void addImmutablePass(ImmutablePass *P) {
175 P->initializePass();
176 ImmutablePasses.push_back(P);
177 }
178
179 inline std::vector<ImmutablePass *>& getImmutablePasses() {
180 return ImmutablePasses;
181 }
182
Devang Patel5bbeb492006-12-08 22:47:25 +0000183 void addPassManager(Pass *Manager) {
184 PassManagers.push_back(Manager);
185 }
186
Devang Patelaf1fca52006-12-08 23:11:43 +0000187 // Add Manager into the list of managers that are not directly
188 // maintained by this top level pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000189 inline void addIndirectPassManager(PMDataManager *Manager) {
190 IndirectPassManagers.push_back(Manager);
Devang Patelaf1fca52006-12-08 23:11:43 +0000191 }
192
Devang Pateleda56172006-12-12 23:34:33 +0000193 // Print passes managed by this top level manager.
194 void dumpPasses();
Devang Patelcfd70c42006-12-13 22:10:00 +0000195 void dumpArguments();
Devang Pateleda56172006-12-12 23:34:33 +0000196
Devang Patelf33f3eb2006-12-07 19:21:29 +0000197private:
198
199 /// Collection of pass managers
200 std::vector<Pass *> PassManagers;
201
Devang Patelaf1fca52006-12-08 23:11:43 +0000202 /// Collection of pass managers that are not directly maintained
203 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000204 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000205
Devang Patelf33f3eb2006-12-07 19:21:29 +0000206 // Map to keep track of last user of the analysis pass.
207 // LastUser->second is the last user of Lastuser->first.
208 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000209
210 /// Immutable passes are managed by top level manager.
211 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000212};
213
Devang Patelf3827bc2006-12-07 19:54:15 +0000214//===----------------------------------------------------------------------===//
215// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000216
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000217/// PMDataManager provides the common place to manage the analysis data
218/// used by pass managers.
219class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000220
221public:
222
Devang Patel4c36e6b2006-12-07 23:24:58 +0000223 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000224 initializeAnalysisInfo();
225 }
226
Devang Patelab97cf42006-12-13 00:09:23 +0000227 virtual ~PMDataManager() {
228
229 for (std::vector<Pass *>::iterator I = PassVector.begin(),
230 E = PassVector.end(); I != E; ++I)
231 delete *I;
232
233 PassVector.clear();
234 }
235
Devang Patela9844592006-11-11 01:31:05 +0000236 /// Return true IFF pass P's required analysis set does not required new
237 /// manager.
238 bool manageablePass(Pass *P);
239
Devang Patela9844592006-11-11 01:31:05 +0000240 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000241 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000242
Devang Patela9844592006-11-11 01:31:05 +0000243 /// Remove Analysis that is not preserved by the pass
244 void removeNotPreservedAnalysis(Pass *P);
245
246 /// Remove dead passes
Devang Patel200d3052006-12-13 23:50:44 +0000247 void removeDeadPasses(Pass *P, std::string &Msg);
Devang Patela9844592006-11-11 01:31:05 +0000248
Devang Patel8f677ce2006-12-07 18:47:25 +0000249 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000250 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
251 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000252
Devang Patel1d6267c2006-12-07 23:05:44 +0000253 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000254 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000255 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000256 AvailableAnalysis.clear();
257 }
258
Devang Patel1d6267c2006-12-07 23:05:44 +0000259 /// Populate RequiredPasses with the analysis pass that are required by
260 /// pass P.
261 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
262 Pass *P);
263
264 /// All Required analyses should be available to the pass as it runs! Here
265 /// we fill in the AnalysisImpls member of the pass so that it can
266 /// successfully use the getAnalysis() method to retrieve the
267 /// implementations it needs.
268 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000269
Devang Patel640c5bb2006-12-08 22:30:11 +0000270 /// Find the pass that implements Analysis AID. If desired pass is not found
271 /// then return NULL.
272 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
273
Devang Patel8cad70d2006-11-11 01:51:02 +0000274 inline std::vector<Pass *>::iterator passVectorBegin() {
275 return PassVector.begin();
276 }
277
278 inline std::vector<Pass *>::iterator passVectorEnd() {
279 return PassVector.end();
280 }
281
Devang Patelf3827bc2006-12-07 19:54:15 +0000282 // Access toplevel manager
283 PMTopLevelManager *getTopLevelManager() { return TPM; }
284 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
285
Devang Patel4c36e6b2006-12-07 23:24:58 +0000286 unsigned getDepth() { return Depth; }
287
Devang Pateleda56172006-12-12 23:34:33 +0000288 // Print list of passes that are last used by P.
289 void dumpLastUses(Pass *P, unsigned Offset) {
290
291 std::vector<Pass *> LUses;
292
293 assert (TPM && "Top Level Manager is missing");
294 TPM->collectLastUses(LUses, P);
295
296 for (std::vector<Pass *>::iterator I = LUses.begin(),
297 E = LUses.end(); I != E; ++I) {
298 llvm::cerr << "--" << std::string(Offset*2, ' ');
299 (*I)->dumpPassStructure(0);
300 }
301 }
302
Devang Patelcfd70c42006-12-13 22:10:00 +0000303 void dumpPassArguments() {
304 for(std::vector<Pass *>::iterator I = PassVector.begin(),
305 E = PassVector.end(); I != E; ++I) {
306 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
307 PMD->dumpPassArguments();
308 else
309 if (const PassInfo *PI = (*I)->getPassInfo())
310 if (!PI->isAnalysisGroup())
311 cerr << " -" << PI->getPassArgument();
312 }
313 }
314
Devang Patel200d3052006-12-13 23:50:44 +0000315 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) {
316 if (PassDebugging_New < Executions)
317 return;
318 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
319 cerr << Msg1;
320 cerr << P->getPassName();
321 cerr << Msg2;
322 }
323
Devang Patelf6d1d212006-12-14 00:25:06 +0000324 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
325 const std::vector<AnalysisID> &Set) {
326 if (PassDebugging_New >= Details && !Set.empty()) {
327 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
328 for (unsigned i = 0; i != Set.size(); ++i) {
329 if (i) cerr << ",";
330 cerr << " " << Set[i]->getPassName();
331 }
332 cerr << "\n";
333 }
334 }
Devang Patelbc03f132006-12-07 23:55:10 +0000335protected:
336
337 // Collection of pass whose last user asked this manager to claim
338 // last use. If a FunctionPass F is the last user of ModulePass info M
339 // then the F's manager, not F, records itself as a last user of M.
340 std::vector<Pass *> ForcedLastUses;
341
342 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000343 PMTopLevelManager *TPM;
344
Devang Patela9844592006-11-11 01:31:05 +0000345private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000346 // Set of available Analysis. This information is used while scheduling
347 // pass. If a pass requires an analysis which is not not available then
348 // equired analysis pass is scheduled to run before the pass itself is
349 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000350 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000351
352 // Collection of pass that are managed by this manager
353 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000354
Devang Patel4c36e6b2006-12-07 23:24:58 +0000355 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000356};
357
Devang Patel10c2ca62006-12-12 22:47:13 +0000358//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000359// BasicBlockPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000360//
Devang Patelb67904d2006-12-13 02:36:01 +0000361/// BasicBlockPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000362/// pass together and sequence them to process one basic block before
363/// processing next basic block.
Devang Patelb67904d2006-12-13 02:36:01 +0000364class BasicBlockPassManager : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000365 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000366
367public:
Devang Patelb67904d2006-12-13 02:36:01 +0000368 BasicBlockPassManager(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000369
370 /// Add a pass into a passmanager queue.
371 bool addPass(Pass *p);
372
373 /// Execute all of the passes scheduled for execution. Keep track of
374 /// whether any of the passes modifies the function, and if so, return true.
375 bool runOnFunction(Function &F);
376
Devang Patelf9d96b92006-12-07 19:57:52 +0000377 /// Pass Manager itself does not invalidate any analysis info.
378 void getAnalysisUsage(AnalysisUsage &Info) const {
379 Info.setPreservesAll();
380 }
381
Devang Patel475c4532006-12-08 00:59:05 +0000382 bool doInitialization(Module &M);
383 bool doInitialization(Function &F);
384 bool doFinalization(Module &M);
385 bool doFinalization(Function &F);
386
Devang Pateleda56172006-12-12 23:34:33 +0000387 // Print passes managed by this manager
388 void dumpPassStructure(unsigned Offset) {
389 llvm::cerr << std::string(Offset*2, ' ') << "BasicBLockPass Manager\n";
390 for (std::vector<Pass *>::iterator I = passVectorBegin(),
391 E = passVectorEnd(); I != E; ++I) {
392 (*I)->dumpPassStructure(Offset + 1);
393 dumpLastUses(*I, Offset+1);
394 }
395 }
Devang Patelca58e352006-11-08 10:05:38 +0000396};
397
Devang Patel10c2ca62006-12-12 22:47:13 +0000398//===----------------------------------------------------------------------===//
399// FunctionPassManagerImpl_New
400//
Chris Lattnerf0f611a2006-12-13 21:56:10 +0000401/// FunctionPassManagerImpl_New manages FunctionPasses and
402/// BasicBlockPassManagers. It batches all function passes and basic block pass
403/// managers together and sequence them to process one function at a time before
404/// processing next function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000405class FunctionPassManagerImpl_New : public ModulePass,
406 public PMDataManager,
407 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000408public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000409 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000410 activeBBPassManager = NULL;
411 }
Devang Patel4e12f862006-11-08 10:44:40 +0000412 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000413
Devang Patelabcd1d32006-12-07 21:27:23 +0000414 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000415
Devang Patelfa971cd2006-12-08 23:57:43 +0000416 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000417
418 // P is a immutable pass then it will be managed by this
419 // top level manager. Set up analysis resolver to connect them.
420 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
421 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000422 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000423 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000424 recordAvailableAnalysis(IP);
Devang Patelfa971cd2006-12-08 23:57:43 +0000425 }
426 else
427 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000428 }
429
Devang Patelca58e352006-11-08 10:05:38 +0000430 /// add - Add a pass to the queue of passes to run. This passes
431 /// ownership of the Pass to the PassManager. When the
432 /// PassManager_X is destroyed, the pass will be destroyed as well, so
433 /// there is no need to delete the pass. (TODO delete passes.)
434 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000435 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000436 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000437 }
Devang Patelca58e352006-11-08 10:05:38 +0000438
439 /// Add pass into the pass manager queue.
440 bool addPass(Pass *P);
441
442 /// Execute all of the passes scheduled for execution. Keep
443 /// track of whether any of the passes modifies the function, and if
444 /// so, return true.
445 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000446 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000447 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000448
Devang Patelff631ae2006-11-15 01:27:05 +0000449 /// doInitialization - Run all of the initializers for the function passes.
450 ///
451 bool doInitialization(Module &M);
452
453 /// doFinalization - Run all of the initializers for the function passes.
454 ///
455 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000456
457 /// Pass Manager itself does not invalidate any analysis info.
458 void getAnalysisUsage(AnalysisUsage &Info) const {
459 Info.setPreservesAll();
460 }
461
Devang Pateleda56172006-12-12 23:34:33 +0000462 // Print passes managed by this manager
463 void dumpPassStructure(unsigned Offset) {
464 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
465 for (std::vector<Pass *>::iterator I = passVectorBegin(),
466 E = passVectorEnd(); I != E; ++I) {
467 (*I)->dumpPassStructure(Offset + 1);
468 dumpLastUses(*I, Offset+1);
469 }
470 }
471
Devang Patelca58e352006-11-08 10:05:38 +0000472private:
Devang Patelca58e352006-11-08 10:05:38 +0000473 // Active Pass Managers
Devang Patelb67904d2006-12-13 02:36:01 +0000474 BasicBlockPassManager *activeBBPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000475};
476
Devang Patel10c2ca62006-12-12 22:47:13 +0000477//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000478// ModulePassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000479//
Devang Patelb67904d2006-12-13 02:36:01 +0000480/// ModulePassManager manages ModulePasses and function pass managers.
Devang Patelca58e352006-11-08 10:05:38 +0000481/// It batches all Module passes passes and function pass managers together and
482/// sequence them to process one module.
Devang Patelb67904d2006-12-13 02:36:01 +0000483class ModulePassManager : public Pass,
Devang Patelbc03f132006-12-07 23:55:10 +0000484 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000485
486public:
Devang Patelb67904d2006-12-13 02:36:01 +0000487 ModulePassManager(int D) : PMDataManager(D) {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000488 activeFunctionPassManager = NULL;
489 }
Devang Patelca58e352006-11-08 10:05:38 +0000490
491 /// Add a pass into a passmanager queue.
492 bool addPass(Pass *p);
493
494 /// run - Execute all of the passes scheduled for execution. Keep track of
495 /// whether any of the passes modifies the module, and if so, return true.
496 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000497
Devang Patelf9d96b92006-12-07 19:57:52 +0000498 /// Pass Manager itself does not invalidate any analysis info.
499 void getAnalysisUsage(AnalysisUsage &Info) const {
500 Info.setPreservesAll();
501 }
502
Devang Pateleda56172006-12-12 23:34:33 +0000503 // Print passes managed by this manager
504 void dumpPassStructure(unsigned Offset) {
505 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
506 for (std::vector<Pass *>::iterator I = passVectorBegin(),
507 E = passVectorEnd(); I != E; ++I) {
508 (*I)->dumpPassStructure(Offset + 1);
509 dumpLastUses(*I, Offset+1);
510 }
511 }
512
Devang Patelca58e352006-11-08 10:05:38 +0000513private:
Devang Patelca58e352006-11-08 10:05:38 +0000514 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000515 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000516};
517
Devang Patel10c2ca62006-12-12 22:47:13 +0000518//===----------------------------------------------------------------------===//
519// PassManagerImpl_New
520//
521/// PassManagerImpl_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000522class PassManagerImpl_New : public Pass,
523 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000524 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000525
526public:
527
Devang Patelad6b7fe2006-12-12 22:57:43 +0000528 PassManagerImpl_New(int D) : PMDataManager(D) {
529 activeManager = NULL;
530 }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000531
Devang Patel376fefa2006-11-08 10:29:57 +0000532 /// add - Add a pass to the queue of passes to run. This passes ownership of
533 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
534 /// will be destroyed as well, so there is no need to delete the pass. This
535 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000536 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000537 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000538 }
Devang Patel376fefa2006-11-08 10:29:57 +0000539
540 /// run - Execute all of the passes scheduled for execution. Keep track of
541 /// whether any of the passes modifies the module, and if so, return true.
542 bool run(Module &M);
543
Devang Patelf9d96b92006-12-07 19:57:52 +0000544 /// Pass Manager itself does not invalidate any analysis info.
545 void getAnalysisUsage(AnalysisUsage &Info) const {
546 Info.setPreservesAll();
547 }
548
Devang Patelabcd1d32006-12-07 21:27:23 +0000549 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000550
Devang Patelfa971cd2006-12-08 23:57:43 +0000551 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000552
553 // P is a immutable pass and it will be managed by this
554 // top level manager. Set up analysis resolver to connect them.
555 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
556 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000557 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000558 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000559 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000560 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000561 else
562 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000563 }
564
Devang Patel376fefa2006-11-08 10:29:57 +0000565private:
566
Devang Patelde124182006-12-07 21:10:57 +0000567 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000568 bool addPass(Pass *p);
569
Devang Patel376fefa2006-11-08 10:29:57 +0000570 // Active Pass Manager
Devang Patelb67904d2006-12-13 02:36:01 +0000571 ModulePassManager *activeManager;
Devang Patel376fefa2006-11-08 10:29:57 +0000572};
573
Devang Patelca58e352006-11-08 10:05:38 +0000574} // End of llvm namespace
575
Devang Patela1514cb2006-12-07 19:39:39 +0000576//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000577// PMTopLevelManager implementation
578
579/// Set pass P as the last user of the given analysis passes.
580void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
581 Pass *P) {
582
583 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
584 E = AnalysisPasses.end(); I != E; ++I) {
585 Pass *AP = *I;
586 LastUser[AP] = P;
587 // If AP is the last user of other passes then make P last user of
588 // such passes.
589 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
590 LUE = LastUser.end(); LUI != LUE; ++LUI) {
591 if (LUI->second == AP)
592 LastUser[LUI->first] = P;
593 }
594 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000595}
596
597/// Collect passes whose last user is P
598void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
599 Pass *P) {
600 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
601 LUE = LastUser.end(); LUI != LUE; ++LUI)
602 if (LUI->second == P)
603 LastUses.push_back(LUI->first);
604}
605
606/// Schedule pass P for execution. Make sure that passes required by
607/// P are run before P is run. Update analysis info maintained by
608/// the manager. Remove dead passes. This is a recursive function.
609void PMTopLevelManager::schedulePass(Pass *P) {
610
611 // TODO : Allocate function manager for this pass, other wise required set
612 // may be inserted into previous function manager
613
614 AnalysisUsage AnUsage;
615 P->getAnalysisUsage(AnUsage);
616 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
617 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
618 E = RequiredSet.end(); I != E; ++I) {
619
620 Pass *AnalysisPass = findAnalysisPass(*I);
621 if (!AnalysisPass) {
622 // Schedule this analysis run first.
623 AnalysisPass = (*I)->createPass();
624 schedulePass(AnalysisPass);
625 }
626 }
627
628 // Now all required passes are available.
629 addTopLevelPass(P);
630}
631
632/// Find the pass that implements Analysis AID. Search immutable
633/// passes and all pass managers. If desired pass is not found
634/// then return NULL.
635Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
636
637 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000638 // Check pass managers
639 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
640 E = PassManagers.end(); P == NULL && I != E; ++I) {
641 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
642 assert(PMD && "This is not a PassManager");
643 P = PMD->findAnalysisPass(AID, false);
644 }
645
646 // Check other pass managers
647 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
648 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
649 P = (*I)->findAnalysisPass(AID, false);
650
Devang Patelafb1f3622006-12-12 22:35:25 +0000651 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
652 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
653 const PassInfo *PI = (*I)->getPassInfo();
654 if (PI == AID)
655 P = *I;
656
657 // If Pass not found then check the interfaces implemented by Immutable Pass
658 if (!P) {
659 const std::vector<const PassInfo*> &ImmPI =
660 PI->getInterfacesImplemented();
661 for (unsigned Index = 0, End = ImmPI.size();
662 P == NULL && Index != End; ++Index)
663 if (ImmPI[Index] == AID)
664 P = *I;
665 }
666 }
667
Devang Patelafb1f3622006-12-12 22:35:25 +0000668 return P;
669}
670
Devang Pateleda56172006-12-12 23:34:33 +0000671// Print passes managed by this top level manager.
672void PMTopLevelManager::dumpPasses() {
673
674 // Print out the immutable passes
675 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
676 ImmutablePasses[i]->dumpPassStructure(0);
677 }
678
679 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
680 E = PassManagers.end(); I != E; ++I)
681 (*I)->dumpPassStructure(1);
682
683}
684
Devang Patelcfd70c42006-12-13 22:10:00 +0000685void PMTopLevelManager::dumpArguments() {
686
687 if (PassDebugging_New < Arguments)
688 return;
689
690 cerr << "Pass Arguments: ";
691 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
692 E = PassManagers.end(); I != E; ++I) {
693 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
694 assert(PMD && "This is not a PassManager");
695 PMD->dumpPassArguments();
696 }
697 cerr << "\n";
698}
699
Devang Patelafb1f3622006-12-12 22:35:25 +0000700//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000701// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000702
Devang Pateld65e9e92006-11-08 01:31:28 +0000703/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000704/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000705bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000706
Devang Patel8f677ce2006-12-07 18:47:25 +0000707 // TODO
708 // If this pass is not preserving information that is required by a
709 // pass maintained by higher level pass manager then do not insert
710 // this pass into current manager. Use new manager. For example,
711 // For example, If FunctionPass F is not preserving ModulePass Info M1
712 // that is used by another ModulePass M2 then do not insert F in
713 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000714 return true;
715}
716
Devang Patel643676c2006-11-11 01:10:19 +0000717/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000718void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000719
Devang Patel643676c2006-11-11 01:10:19 +0000720 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000721 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000722
Devang Patele9976aa2006-12-07 19:33:53 +0000723 //This pass is the current implementation of all of the interfaces it
724 //implements as well.
725 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
726 for (unsigned i = 0, e = II.size(); i != e; ++i)
727 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000728 }
729}
730
Devang Patelf68a3492006-11-07 22:35:17 +0000731/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000732void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000733 AnalysisUsage AnUsage;
734 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000735
Devang Patel2e169c32006-12-07 20:03:49 +0000736 if (AnUsage.getPreservesAll())
737 return;
738
739 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000740 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000741 E = AvailableAnalysis.end(); I != E; ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000742 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000743 PreservedSet.end()) {
744 // Remove this analysis
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000745 if (!dynamic_cast<ImmutablePass*>(I->second)) {
746 std::map<AnalysisID, Pass*>::iterator J = I++;
747 AvailableAnalysis.erase(J);
748 } else
749 ++I;
750 } else
751 ++I;
Devang Patel349170f2006-11-11 01:24:55 +0000752 }
Devang Patelf68a3492006-11-07 22:35:17 +0000753}
754
Devang Patelca189262006-11-14 03:05:08 +0000755/// Remove analysis passes that are not used any longer
Devang Patel200d3052006-12-13 23:50:44 +0000756void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
Devang Patel17ad0962006-12-08 00:37:52 +0000757
758 std::vector<Pass *> DeadPasses;
759 TPM->collectLastUses(DeadPasses, P);
760
761 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
762 E = DeadPasses.end(); I != E; ++I) {
Devang Patel200d3052006-12-13 23:50:44 +0000763
764 std::string Msg1 = " Freeing Pass '";
765 dumpPassInfo(*I, Msg1, Msg);
766
Devang Patel17ad0962006-12-08 00:37:52 +0000767 (*I)->releaseMemory();
768
769 std::map<AnalysisID, Pass*>::iterator Pos =
770 AvailableAnalysis.find((*I)->getPassInfo());
771
Devang Patel475c4532006-12-08 00:59:05 +0000772 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000773 if (Pos != AvailableAnalysis.end())
774 AvailableAnalysis.erase(Pos);
775 }
Devang Patelca189262006-11-14 03:05:08 +0000776}
777
Devang Patel8f677ce2006-12-07 18:47:25 +0000778/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000779/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000780void PMDataManager::addPassToManager(Pass *P,
781 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000782
Devang Pateld440cd92006-12-08 23:53:00 +0000783 // This manager is going to manage pass P. Set up analysis resolver
784 // to connect them.
785 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
786 P->setResolver(AR);
787
Devang Patel90b05e02006-11-11 02:04:19 +0000788 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000789
790 // At the moment, this pass is the last user of all required passes.
791 std::vector<Pass *> LastUses;
792 std::vector<Pass *> RequiredPasses;
793 unsigned PDepth = this->getDepth();
794
795 collectRequiredAnalysisPasses(RequiredPasses, P);
796 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
797 E = RequiredPasses.end(); I != E; ++I) {
798 Pass *PRequired = *I;
799 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000800
801 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
802 RDepth = DM.getDepth();
803
Devang Patelbc03f132006-12-07 23:55:10 +0000804 if (PDepth == RDepth)
805 LastUses.push_back(PRequired);
806 else if (PDepth > RDepth) {
807 // Let the parent claim responsibility of last use
808 ForcedLastUses.push_back(PRequired);
809 } else {
810 // Note : This feature is not yet implemented
811 assert (0 &&
812 "Unable to handle Pass that requires lower level Analysis pass");
813 }
814 }
815
816 if (!LastUses.empty())
817 TPM->setLastUser(LastUses, P);
818
Devang Patel17bff0d2006-12-07 22:09:36 +0000819 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000820 // Remove the analysis not preserved by this pass
821 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000822 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000823 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000824
825 // Add pass
826 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000827}
828
Devang Patel1d6267c2006-12-07 23:05:44 +0000829/// Populate RequiredPasses with the analysis pass that are required by
830/// pass P.
831void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
832 Pass *P) {
833 AnalysisUsage AnUsage;
834 P->getAnalysisUsage(AnUsage);
835 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
836 for (std::vector<AnalysisID>::const_iterator
837 I = RequiredSet.begin(), E = RequiredSet.end();
838 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000839 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000840 assert (AnalysisPass && "Analysis pass is not available");
841 RP.push_back(AnalysisPass);
842 }
Devang Patelf58183d2006-12-12 23:09:32 +0000843
844 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
845 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
846 E = IDs.end(); I != E; ++I) {
847 Pass *AnalysisPass = findAnalysisPass(*I, true);
848 assert (AnalysisPass && "Analysis pass is not available");
849 RP.push_back(AnalysisPass);
850 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000851}
852
Devang Patel07f4f582006-11-14 21:49:36 +0000853// All Required analyses should be available to the pass as it runs! Here
854// we fill in the AnalysisImpls member of the pass so that it can
855// successfully use the getAnalysis() method to retrieve the
856// implementations it needs.
857//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000858void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000859 AnalysisUsage AnUsage;
860 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000861
862 for (std::vector<const PassInfo *>::const_iterator
863 I = AnUsage.getRequiredSet().begin(),
864 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000865 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000866 if (Impl == 0)
867 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000868 AnalysisResolver_New *AR = P->getResolver();
869 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000870 }
871}
872
Devang Patel640c5bb2006-12-08 22:30:11 +0000873/// Find the pass that implements Analysis AID. If desired pass is not found
874/// then return NULL.
875Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
876
877 // Check if AvailableAnalysis map has one entry.
878 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
879
880 if (I != AvailableAnalysis.end())
881 return I->second;
882
883 // Search Parents through TopLevelManager
884 if (SearchParent)
885 return TPM->findAnalysisPass(AID);
886
Devang Patel9d759b82006-12-09 00:09:12 +0000887 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000888}
889
Devang Patel9bdf7d42006-12-08 23:28:54 +0000890
891//===----------------------------------------------------------------------===//
892// NOTE: Is this the right place to define this method ?
893// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
894Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
895 return PM.findAnalysisPass(ID, dir);
896}
897
Devang Patela1514cb2006-12-07 19:39:39 +0000898//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +0000899// BasicBlockPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000900
Devang Pateld65e9e92006-11-08 01:31:28 +0000901/// Add pass P into PassVector and return true. If this pass is not
902/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000903bool
Devang Patelb67904d2006-12-13 02:36:01 +0000904BasicBlockPassManager::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000905
906 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
907 if (!BP)
908 return false;
909
Devang Patel3c8eb622006-11-07 22:56:50 +0000910 // If this pass does not preserve anlysis that is used by other passes
911 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000912 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000913 return false;
914
Devang Patel8cad70d2006-11-11 01:51:02 +0000915 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000916
Devang Patel6e5a1132006-11-07 21:31:57 +0000917 return true;
918}
919
920/// Execute all of the passes scheduled for execution by invoking
921/// runOnBasicBlock method. Keep track of whether any of the passes modifies
922/// the function, and if so, return true.
923bool
Devang Patelb67904d2006-12-13 02:36:01 +0000924BasicBlockPassManager::runOnFunction(Function &F) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000925
Devang Patel745a6962006-12-12 23:15:28 +0000926 if (F.isExternal())
927 return false;
928
Devang Patele9585592006-12-08 01:38:28 +0000929 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000930 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000931
Devang Patel93a197c2006-12-14 00:08:04 +0000932 std::string Msg1 = "Executing Pass '";
933 std::string Msg3 = "' Made Modification '";
934
Devang Patel6e5a1132006-11-07 21:31:57 +0000935 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000936 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
937 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000938 Pass *P = *itr;
Devang Patelf6d1d212006-12-14 00:25:06 +0000939 AnalysisUsage AnUsage;
940 P->getAnalysisUsage(AnUsage);
941
Devang Patel200d3052006-12-13 23:50:44 +0000942 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
943 dumpPassInfo(P, Msg1, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +0000944 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
945
Devang Patel47d7df72006-12-12 23:13:09 +0000946 initializeAnalysisImpl(P);
Devang Patel93a197c2006-12-14 00:08:04 +0000947
Devang Patel6e5a1132006-11-07 21:31:57 +0000948 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
949 Changed |= BP->runOnBasicBlock(*I);
Devang Patel93a197c2006-12-14 00:08:04 +0000950
951 if (Changed)
952 dumpPassInfo(P, Msg3, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +0000953 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +0000954
Devang Patel050ec722006-11-14 01:23:29 +0000955 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000956 recordAvailableAnalysis(P);
Devang Patel200d3052006-12-13 23:50:44 +0000957 removeDeadPasses(P, Msg2);
Devang Patel6e5a1132006-11-07 21:31:57 +0000958 }
Devang Patele9585592006-12-08 01:38:28 +0000959 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000960}
961
Devang Patel475c4532006-12-08 00:59:05 +0000962// Implement doInitialization and doFinalization
Devang Patelb67904d2006-12-13 02:36:01 +0000963inline bool BasicBlockPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000964 bool Changed = false;
965
966 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
967 e = passVectorEnd(); itr != e; ++itr) {
968 Pass *P = *itr;
969 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
970 Changed |= BP->doInitialization(M);
971 }
972
973 return Changed;
974}
975
Devang Patelb67904d2006-12-13 02:36:01 +0000976inline bool BasicBlockPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +0000977 bool Changed = false;
978
979 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
980 e = passVectorEnd(); itr != e; ++itr) {
981 Pass *P = *itr;
982 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
983 Changed |= BP->doFinalization(M);
984 }
985
986 return Changed;
987}
988
Devang Patelb67904d2006-12-13 02:36:01 +0000989inline bool BasicBlockPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +0000990 bool Changed = false;
991
992 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
993 e = passVectorEnd(); itr != e; ++itr) {
994 Pass *P = *itr;
995 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
996 Changed |= BP->doInitialization(F);
997 }
998
999 return Changed;
1000}
1001
Devang Patelb67904d2006-12-13 02:36:01 +00001002inline bool BasicBlockPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001003 bool Changed = false;
1004
1005 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1006 e = passVectorEnd(); itr != e; ++itr) {
1007 Pass *P = *itr;
1008 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1009 Changed |= BP->doFinalization(F);
1010 }
1011
1012 return Changed;
1013}
1014
1015
Devang Patela1514cb2006-12-07 19:39:39 +00001016//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001017// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001018
Devang Patel4e12f862006-11-08 10:44:40 +00001019/// Create new Function pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001020FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
Devang Patel1f653682006-12-08 18:57:16 +00001021 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001022 // FPM is the top level manager.
1023 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001024
1025 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
1026 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
1027 FPM->setResolver(AR);
1028
1029 FPM->addPassManager(FPM);
Devang Patel1f653682006-12-08 18:57:16 +00001030 MP = P;
1031}
1032
Devang Patelb67904d2006-12-13 02:36:01 +00001033FunctionPassManager::~FunctionPassManager() {
1034 // Note : FPM maintains one entry in PassManagers vector.
1035 // This one entry is FPM itself. This is not ideal. One
1036 // alternative is have one additional layer between
1037 // FunctionPassManager and FunctionPassManagerImpl.
1038 // Meanwhile, to avoid going into infinte loop, first
1039 // remove FPM from its PassMangers vector.
1040 FPM->clearManagers();
Devang Patelab97cf42006-12-13 00:09:23 +00001041 delete FPM;
1042}
1043
Devang Patel4e12f862006-11-08 10:44:40 +00001044/// add - Add a pass to the queue of passes to run. This passes
1045/// ownership of the Pass to the PassManager. When the
1046/// PassManager_X is destroyed, the pass will be destroyed as well, so
1047/// there is no need to delete the pass. (TODO delete passes.)
1048/// This implies that all passes MUST be allocated with 'new'.
Devang Patelb67904d2006-12-13 02:36:01 +00001049void FunctionPassManager::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +00001050 FPM->add(P);
1051}
1052
Devang Patel9f3083e2006-11-15 19:39:54 +00001053/// run - Execute all of the passes scheduled for execution. Keep
1054/// track of whether any of the passes modifies the function, and if
1055/// so, return true.
1056///
Devang Patelb67904d2006-12-13 02:36:01 +00001057bool FunctionPassManager::run(Function &F) {
Devang Patel9f3083e2006-11-15 19:39:54 +00001058 std::string errstr;
1059 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +00001060 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +00001061 abort();
1062 }
Devang Patel272908d2006-12-08 22:57:48 +00001063 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001064}
1065
1066
Devang Patelff631ae2006-11-15 01:27:05 +00001067/// doInitialization - Run all of the initializers for the function passes.
1068///
Devang Patelb67904d2006-12-13 02:36:01 +00001069bool FunctionPassManager::doInitialization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001070 return FPM->doInitialization(*MP->getModule());
1071}
1072
1073/// doFinalization - Run all of the initializers for the function passes.
1074///
Devang Patelb67904d2006-12-13 02:36:01 +00001075bool FunctionPassManager::doFinalization() {
Devang Patelff631ae2006-11-15 01:27:05 +00001076 return FPM->doFinalization(*MP->getModule());
1077}
1078
Devang Patela1514cb2006-12-07 19:39:39 +00001079//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +00001080// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001081
Devang Patel0c2012f2006-11-07 21:49:50 +00001082/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1083/// either use it into active basic block pass manager or create new basic
1084/// block pass manager to handle pass P.
1085bool
Devang Patel4e12f862006-11-08 10:44:40 +00001086FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001087
Devang Patelb67904d2006-12-13 02:36:01 +00001088 // If P is a BasicBlockPass then use BasicBlockPassManager.
Devang Patel0c2012f2006-11-07 21:49:50 +00001089 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1090
Devang Patel4949fe02006-12-07 22:34:21 +00001091 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001092
Devang Patel4949fe02006-12-07 22:34:21 +00001093 // If active manager exists then clear its analysis info.
1094 if (activeBBPassManager)
1095 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001096
Devang Patel4949fe02006-12-07 22:34:21 +00001097 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001098 activeBBPassManager =
Devang Patelb67904d2006-12-13 02:36:01 +00001099 new BasicBlockPassManager(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001100 // Inherit top level manager
1101 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001102
1103 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +00001104 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001105
1106 // Add new manager into top level manager's indirect passes list
1107 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1108 assert (PMD && "Manager is not Pass Manager");
1109 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +00001110
1111 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001112 if (!activeBBPassManager->addPass(BP))
1113 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +00001114 }
Devang Patelbc03f132006-12-07 23:55:10 +00001115
1116 if (!ForcedLastUses.empty())
1117 TPM->setLastUser(ForcedLastUses, this);
1118
Devang Patel0c2012f2006-11-07 21:49:50 +00001119 return true;
1120 }
1121
1122 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1123 if (!FP)
1124 return false;
1125
Devang Patel3c8eb622006-11-07 22:56:50 +00001126 // If this pass does not preserve anlysis that is used by other passes
1127 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001128 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001129 return false;
1130
Devang Patel8cad70d2006-11-11 01:51:02 +00001131 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +00001132
1133 // If active manager exists then clear its analysis info.
1134 if (activeBBPassManager) {
1135 activeBBPassManager->initializeAnalysisInfo();
1136 activeBBPassManager = NULL;
1137 }
1138
Devang Patel0c2012f2006-11-07 21:49:50 +00001139 return true;
1140}
1141
1142/// Execute all of the passes scheduled for execution by invoking
1143/// runOnFunction method. Keep track of whether any of the passes modifies
1144/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +00001145bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001146
Devang Patel0e29e292006-12-08 19:04:09 +00001147 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +00001148 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001149
Devang Patel0c2012f2006-11-07 21:49:50 +00001150 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +00001151 this->runOnFunction(*I);
1152
Devang Patel0e29e292006-12-08 19:04:09 +00001153 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +00001154}
1155
Devang Patel9f3083e2006-11-15 19:39:54 +00001156/// Execute all of the passes scheduled for execution by invoking
1157/// runOnFunction method. Keep track of whether any of the passes modifies
1158/// the function, and if so, return true.
1159bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
1160
1161 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001162
1163 if (F.isExternal())
1164 return false;
1165
Devang Patela6b6dcb2006-12-07 18:41:09 +00001166 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +00001167
Devang Patel93a197c2006-12-14 00:08:04 +00001168 std::string Msg1 = "Executing Pass '";
1169 std::string Msg3 = "' Made Modification '";
1170
Devang Patel9f3083e2006-11-15 19:39:54 +00001171 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1172 e = passVectorEnd(); itr != e; ++itr) {
1173 Pass *P = *itr;
Devang Patelf6d1d212006-12-14 00:25:06 +00001174 AnalysisUsage AnUsage;
1175 P->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001176
Devang Patel200d3052006-12-13 23:50:44 +00001177 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
1178 dumpPassInfo(P, Msg1, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001179 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001180
Devang Patel47d7df72006-12-12 23:13:09 +00001181 initializeAnalysisImpl(P);
Devang Patel9f3083e2006-11-15 19:39:54 +00001182 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1183 Changed |= FP->runOnFunction(F);
Devang Patel93a197c2006-12-14 00:08:04 +00001184
1185 if (Changed)
1186 dumpPassInfo(P, Msg3, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001187 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001188
Devang Patel9f3083e2006-11-15 19:39:54 +00001189 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001190 recordAvailableAnalysis(P);
Devang Patel200d3052006-12-13 23:50:44 +00001191 removeDeadPasses(P, Msg2);
Devang Patel9f3083e2006-11-15 19:39:54 +00001192 }
1193 return Changed;
1194}
1195
1196
Devang Patelff631ae2006-11-15 01:27:05 +00001197inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1198 bool Changed = false;
1199
1200 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1201 e = passVectorEnd(); itr != e; ++itr) {
1202 Pass *P = *itr;
1203
1204 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1205 Changed |= FP->doInitialization(M);
1206 }
1207
1208 return Changed;
1209}
1210
1211inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1212 bool Changed = false;
1213
1214 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1215 e = passVectorEnd(); itr != e; ++itr) {
1216 Pass *P = *itr;
1217
1218 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1219 Changed |= FP->doFinalization(M);
1220 }
1221
Devang Patelff631ae2006-11-15 01:27:05 +00001222 return Changed;
1223}
1224
Devang Patel272908d2006-12-08 22:57:48 +00001225// Execute all the passes managed by this top level manager.
1226// Return true if any function is modified by a pass.
1227bool FunctionPassManagerImpl_New::run(Function &F) {
1228
1229 bool Changed = false;
1230 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1231 E = passManagersEnd(); I != E; ++I) {
Devang Patelb67904d2006-12-13 02:36:01 +00001232 FunctionPassManagerImpl_New *FP =
1233 dynamic_cast<FunctionPassManagerImpl_New *>(*I);
Devang Patel272908d2006-12-08 22:57:48 +00001234 Changed |= FP->runOnFunction(F);
1235 }
1236 return Changed;
1237}
1238
Devang Patela1514cb2006-12-07 19:39:39 +00001239//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +00001240// ModulePassManager implementation
1241
1242/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +00001243/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001244/// is not manageable by this manager.
1245bool
Devang Patelb67904d2006-12-13 02:36:01 +00001246ModulePassManager::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001247
1248 // If P is FunctionPass then use function pass maanager.
1249 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1250
Devang Patel640c5bb2006-12-08 22:30:11 +00001251 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001252
Devang Patel4949fe02006-12-07 22:34:21 +00001253 // If active manager exists then clear its analysis info.
1254 if (activeFunctionPassManager)
1255 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001256
Devang Patel4949fe02006-12-07 22:34:21 +00001257 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001258 activeFunctionPassManager =
1259 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001260
1261 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001262 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001263
Devang Patel9c6290c2006-12-12 22:02:16 +00001264 // Inherit top level manager
1265 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001266
1267 // Add new manager into top level manager's indirect passes list
Chris Lattnerf0f611a2006-12-13 21:56:10 +00001268 PMDataManager *PMD =
1269 dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1270 assert(PMD && "Manager is not Pass Manager");
Devang Patelafb1f3622006-12-12 22:35:25 +00001271 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001272
Devang Patel4949fe02006-12-07 22:34:21 +00001273 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001274 if (!activeFunctionPassManager->addPass(FP))
1275 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001276 }
Devang Patelbc03f132006-12-07 23:55:10 +00001277
1278 if (!ForcedLastUses.empty())
1279 TPM->setLastUser(ForcedLastUses, this);
1280
Devang Patel05e1a972006-11-07 22:03:15 +00001281 return true;
1282 }
1283
1284 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1285 if (!MP)
1286 return false;
1287
Devang Patel3c8eb622006-11-07 22:56:50 +00001288 // If this pass does not preserve anlysis that is used by other passes
1289 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001290 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001291 return false;
1292
Devang Patel8cad70d2006-11-11 01:51:02 +00001293 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001294 // If active manager exists then clear its analysis info.
1295 if (activeFunctionPassManager) {
1296 activeFunctionPassManager->initializeAnalysisInfo();
1297 activeFunctionPassManager = NULL;
1298 }
1299
Devang Patel05e1a972006-11-07 22:03:15 +00001300 return true;
1301}
1302
1303
1304/// Execute all of the passes scheduled for execution by invoking
1305/// runOnModule method. Keep track of whether any of the passes modifies
1306/// the module, and if so, return true.
1307bool
Devang Patelb67904d2006-12-13 02:36:01 +00001308ModulePassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001309 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001310 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001311
Devang Patel93a197c2006-12-14 00:08:04 +00001312 std::string Msg1 = "Executing Pass '";
1313 std::string Msg3 = "' Made Modification '";
1314
Devang Patel8cad70d2006-11-11 01:51:02 +00001315 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1316 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001317 Pass *P = *itr;
Devang Patelf6d1d212006-12-14 00:25:06 +00001318 AnalysisUsage AnUsage;
1319 P->getAnalysisUsage(AnUsage);
Devang Patel93a197c2006-12-14 00:08:04 +00001320
Devang Patel200d3052006-12-13 23:50:44 +00001321 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
1322 dumpPassInfo(P, Msg1, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001323 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
Devang Patel93a197c2006-12-14 00:08:04 +00001324
Devang Patel47d7df72006-12-12 23:13:09 +00001325 initializeAnalysisImpl(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001326 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1327 Changed |= MP->runOnModule(M);
Devang Patel93a197c2006-12-14 00:08:04 +00001328
1329 if (Changed)
1330 dumpPassInfo(P, Msg3, Msg2);
Devang Patelf6d1d212006-12-14 00:25:06 +00001331 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
1332
Devang Patel050ec722006-11-14 01:23:29 +00001333 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001334 recordAvailableAnalysis(P);
Devang Patel200d3052006-12-13 23:50:44 +00001335 removeDeadPasses(P, Msg2);
Devang Patel05e1a972006-11-07 22:03:15 +00001336 }
1337 return Changed;
1338}
1339
Devang Patela1514cb2006-12-07 19:39:39 +00001340//===----------------------------------------------------------------------===//
1341// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001342//
Devang Patelc290c8a2006-11-07 22:23:34 +00001343/// Add P into active pass manager or use new module pass manager to
1344/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001345bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001346
Devang Patel6c9f5482006-11-11 00:42:16 +00001347 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelb67904d2006-12-13 02:36:01 +00001348 activeManager = new ModulePassManager(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001349 // Inherit top level manager
1350 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001351
1352 // This top level manager is going to manage activeManager.
1353 // Set up analysis resolver to connect them.
1354 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1355 activeManager->setResolver(AR);
1356
Devang Patel5bbeb492006-12-08 22:47:25 +00001357 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001358 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001359 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001360 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001361}
1362
1363/// run - Execute all of the passes scheduled for execution. Keep track of
1364/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001365bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001366
Devang Patelc290c8a2006-11-07 22:23:34 +00001367 bool Changed = false;
Devang Patelf1567a52006-12-13 20:03:48 +00001368
Devang Patelcfd70c42006-12-13 22:10:00 +00001369 dumpArguments();
Devang Patel03fb5872006-12-13 21:13:31 +00001370 if (PassDebugging_New >= Structure)
Devang Patelf1567a52006-12-13 20:03:48 +00001371 dumpPasses();
1372
Devang Patel5bbeb492006-12-08 22:47:25 +00001373 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1374 E = passManagersEnd(); I != E; ++I) {
Devang Patelb67904d2006-12-13 02:36:01 +00001375 ModulePassManager *MP = dynamic_cast<ModulePassManager *>(*I);
Devang Patel5bbeb492006-12-08 22:47:25 +00001376 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001377 }
1378 return Changed;
1379}
Devang Patel376fefa2006-11-08 10:29:57 +00001380
Devang Patela1514cb2006-12-07 19:39:39 +00001381//===----------------------------------------------------------------------===//
1382// PassManager implementation
1383
Devang Patel376fefa2006-11-08 10:29:57 +00001384/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001385PassManager::PassManager() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001386 PM = new PassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001387 // PM is the top level manager
1388 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001389}
1390
Devang Patelb67904d2006-12-13 02:36:01 +00001391PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001392 delete PM;
1393}
1394
Devang Patel376fefa2006-11-08 10:29:57 +00001395/// add - Add a pass to the queue of passes to run. This passes ownership of
1396/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1397/// will be destroyed as well, so there is no need to delete the pass. This
1398/// implies that all passes MUST be allocated with 'new'.
1399void
Devang Patelb67904d2006-12-13 02:36:01 +00001400PassManager::add(Pass *P) {
Devang Patel376fefa2006-11-08 10:29:57 +00001401 PM->add(P);
1402}
1403
1404/// run - Execute all of the passes scheduled for execution. Keep track of
1405/// whether any of the passes modifies the module, and if so, return true.
1406bool
Devang Patelb67904d2006-12-13 02:36:01 +00001407PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001408 return PM->run(M);
1409}
1410
Devang Patelb67904d2006-12-13 02:36:01 +00001411#endif