blob: 99e7bc58728380daab9e8835c36d5b0067c078da [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 Patel6e5a1132006-11-07 21:31:57 +000016#include "llvm/Module.h"
Devang Patelff631ae2006-11-15 01:27:05 +000017#include "llvm/ModuleProvider.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000018#include "llvm/Support/Streams.h"
Devang Patela9844592006-11-11 01:31:05 +000019#include <vector>
Devang Patelf60b5d92006-11-14 01:59:59 +000020#include <map>
Devang Patel6e5a1132006-11-07 21:31:57 +000021using namespace llvm;
22
Devang Patel6fea2852006-12-07 18:23:30 +000023//===----------------------------------------------------------------------===//
24// Overview:
25// The Pass Manager Infrastructure manages passes. It's responsibilities are:
26//
27// o Manage optimization pass execution order
28// o Make required Analysis information available before pass P is run
29// o Release memory occupied by dead passes
30// o If Analysis information is dirtied by a pass then regenerate Analysis
31// information before it is consumed by another pass.
32//
33// Pass Manager Infrastructure uses multipe pass managers. They are PassManager,
34// FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class
35// hierarcy uses multiple inheritance but pass managers do not derive from
36// another pass manager.
37//
38// PassManager and FunctionPassManager are two top level pass manager that
39// represents the external interface of this entire pass manager infrastucture.
40//
41// Important classes :
42//
43// [o] class PMTopLevelManager;
44//
45// Two top level managers, PassManager and FunctionPassManager, derive from
46// PMTopLevelManager. PMTopLevelManager manages information used by top level
47// managers such as last user info.
48//
49// [o] class PMDataManager;
50//
51// PMDataManager manages information, e.g. list of available analysis info,
52// used by a pass manager to manage execution order of passes. It also provides
53// a place to implement common pass manager APIs. All pass managers derive from
54// PMDataManager.
55//
56// [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager;
57//
58// BasicBlockPassManager manages BasicBlockPasses.
59//
60// [o] class FunctionPassManager;
61//
62// This is a external interface used by JIT to manage FunctionPasses. This
63// interface relies on FunctionPassManagerImpl to do all the tasks.
64//
65// [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
66// public PMTopLevelManager;
67//
68// FunctionPassManagerImpl is a top level manager. It manages FunctionPasses
69// and BasicBlockPassManagers.
70//
71// [o] class ModulePassManager : public Pass, public PMDataManager;
72//
73// ModulePassManager manages ModulePasses and FunctionPassManagerImpls.
74//
75// [o] class PassManager;
76//
77// This is a external interface used by various tools to manages passes. It
78// relies on PassManagerImpl to do all the tasks.
79//
80// [o] class PassManagerImpl : public Pass, public PMDataManager,
81// public PMDTopLevelManager
82//
83// PassManagerImpl is a top level pass manager responsible for managing
84// ModulePassManagers.
85//===----------------------------------------------------------------------===//
86
Devang Patelca58e352006-11-08 10:05:38 +000087namespace llvm {
88
Devang Patelf33f3eb2006-12-07 19:21:29 +000089//===----------------------------------------------------------------------===//
90// PMTopLevelManager
91//
92/// PMTopLevelManager manages LastUser info and collects common APIs used by
93/// top level pass managers.
94class PMTopLevelManager {
95
96public:
97
98 inline std::vector<Pass *>::iterator passManagersBegin() {
99 return PassManagers.begin();
100 }
101
102 inline std::vector<Pass *>::iterator passManagersEnd() {
103 return PassManagers.end();
104 }
105
106 /// Schedule pass P for execution. Make sure that passes required by
107 /// P are run before P is run. Update analysis info maintained by
108 /// the manager. Remove dead passes. This is a recursive function.
109 void schedulePass(Pass *P, Pass *PM);
110
111 /// This is implemented by top level pass manager and used by
112 /// schedulePass() to add analysis info passes that are not available.
113 virtual void addTopLevelPass(Pass *P) = 0;
114
115 /// Set pass P as the last user of the given analysis passes.
116 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
117
118 /// Collect passes whose last user is P
119 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
120
121 virtual ~PMTopLevelManager() {
122 PassManagers.clear();
123 }
124
Devang Patele0eb9d82006-12-07 20:51:18 +0000125 /// Add immutable pass and initialize it.
126 inline void addImmutablePass(ImmutablePass *P) {
127 P->initializePass();
128 ImmutablePasses.push_back(P);
129 }
130
131 inline std::vector<ImmutablePass *>& getImmutablePasses() {
132 return ImmutablePasses;
133 }
134
Devang Patelf33f3eb2006-12-07 19:21:29 +0000135private:
136
137 /// Collection of pass managers
138 std::vector<Pass *> PassManagers;
139
140 // Map to keep track of last user of the analysis pass.
141 // LastUser->second is the last user of Lastuser->first.
142 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000143
144 /// Immutable passes are managed by top level manager.
145 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000146};
147
148/// Set pass P as the last user of the given analysis passes.
149void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
150 Pass *P) {
151
152 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
153 E = AnalysisPasses.end(); I != E; ++I) {
154 Pass *AP = *I;
155 LastUser[AP] = P;
156 // If AP is the last user of other passes then make P last user of
157 // such passes.
158 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
159 LUE = LastUser.end(); LUI != LUE; ++LUI) {
160 if (LUI->second == AP)
161 LastUser[LUI->first] = P;
162 }
163 }
164
165}
166
167/// Collect passes whose last user is P
168void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
169 Pass *P) {
170 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
171 LUE = LastUser.end(); LUI != LUE; ++LUI)
172 if (LUI->second == P)
173 LastUses.push_back(LUI->first);
174}
175
Devang Patelde124182006-12-07 21:10:57 +0000176/// Schedule pass P for execution. Make sure that passes required by
177/// P are run before P is run. Update analysis info maintained by
178/// the manager. Remove dead passes. This is a recursive function.
179void PMTopLevelManager::schedulePass(Pass *P, Pass *PM) {
180
181 // TODO : Allocate function manager for this pass, other wise required set
182 // may be inserted into previous function manager
183
184 AnalysisUsage AnUsage;
185 P->getAnalysisUsage(AnUsage);
186 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
187 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
188 E = RequiredSet.end(); I != E; ++I) {
189
190 Pass *AnalysisPass = NULL; // FIXME PM->getAnalysisPass(*I, true);
191 if (!AnalysisPass) {
192 // Schedule this analysis run first.
193 AnalysisPass = (*I)->createPass();
194 schedulePass(AnalysisPass, PM);
195 }
196 }
197
198 // Now all required passes are available.
199 addTopLevelPass(P);
200}
201
202
Devang Patelf3827bc2006-12-07 19:54:15 +0000203//===----------------------------------------------------------------------===//
204// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000205
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000206/// PMDataManager provides the common place to manage the analysis data
207/// used by pass managers.
208class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000209
210public:
211
Devang Patelf3827bc2006-12-07 19:54:15 +0000212 PMDataManager() : TPM(NULL) {
213 initializeAnalysisInfo();
214 }
215
Devang Patela9844592006-11-11 01:31:05 +0000216 /// Return true IFF pass P's required analysis set does not required new
217 /// manager.
218 bool manageablePass(Pass *P);
219
Devang Patelf60b5d92006-11-14 01:59:59 +0000220 Pass *getAnalysisPass(AnalysisID AID) const {
221
222 std::map<AnalysisID, Pass*>::const_iterator I =
223 AvailableAnalysis.find(AID);
224
225 if (I != AvailableAnalysis.end())
226 return NULL;
227 else
228 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +0000229 }
Devang Patela9844592006-11-11 01:31:05 +0000230
Devang Patela9844592006-11-11 01:31:05 +0000231 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000232 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000233
Devang Patela9844592006-11-11 01:31:05 +0000234 /// Remove Analysis that is not preserved by the pass
235 void removeNotPreservedAnalysis(Pass *P);
236
237 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000238 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000239
Devang Patel8f677ce2006-12-07 18:47:25 +0000240 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000241 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
242 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000243
Devang Patel1d6267c2006-12-07 23:05:44 +0000244 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000245 void initializeAnalysisInfo() {
Devang Patel050ec722006-11-14 01:23:29 +0000246 AvailableAnalysis.clear();
Devang Patelb3900322006-12-07 21:02:08 +0000247
248 // Include immutable passes into AvailableAnalysis vector.
249 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
250 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
251 E = ImmutablePasses.end(); I != E; ++I)
252 recordAvailableAnalysis(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000253 }
254
Devang Patel1d6267c2006-12-07 23:05:44 +0000255 /// Populate RequiredPasses with the analysis pass that are required by
256 /// pass P.
257 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
258 Pass *P);
259
260 /// All Required analyses should be available to the pass as it runs! Here
261 /// we fill in the AnalysisImpls member of the pass so that it can
262 /// successfully use the getAnalysis() method to retrieve the
263 /// implementations it needs.
264 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000265
Devang Patel8cad70d2006-11-11 01:51:02 +0000266 inline std::vector<Pass *>::iterator passVectorBegin() {
267 return PassVector.begin();
268 }
269
270 inline std::vector<Pass *>::iterator passVectorEnd() {
271 return PassVector.end();
272 }
273
Devang Patelf3827bc2006-12-07 19:54:15 +0000274 // Access toplevel manager
275 PMTopLevelManager *getTopLevelManager() { return TPM; }
276 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
277
Devang Patela9844592006-11-11 01:31:05 +0000278private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000279 // Set of available Analysis. This information is used while scheduling
280 // pass. If a pass requires an analysis which is not not available then
281 // equired analysis pass is scheduled to run before the pass itself is
282 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000283 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000284
285 // Collection of pass that are managed by this manager
286 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000287
288 // Top level manager.
289 // TODO : Make it a reference.
290 PMTopLevelManager *TPM;
Devang Patela9844592006-11-11 01:31:05 +0000291};
292
Devang Patelca58e352006-11-08 10:05:38 +0000293/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
294/// pass together and sequence them to process one basic block before
295/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000296class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000297 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000298
299public:
300 BasicBlockPassManager_New() { }
301
302 /// Add a pass into a passmanager queue.
303 bool addPass(Pass *p);
304
305 /// Execute all of the passes scheduled for execution. Keep track of
306 /// whether any of the passes modifies the function, and if so, return true.
307 bool runOnFunction(Function &F);
308
Devang Patelebba9702006-11-13 22:40:09 +0000309 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000310 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000311 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000312
Devang Patelf9d96b92006-12-07 19:57:52 +0000313 /// Pass Manager itself does not invalidate any analysis info.
314 void getAnalysisUsage(AnalysisUsage &Info) const {
315 Info.setPreservesAll();
316 }
317
Devang Patelca58e352006-11-08 10:05:38 +0000318private:
Devang Patelca58e352006-11-08 10:05:38 +0000319};
320
Devang Patel4e12f862006-11-08 10:44:40 +0000321/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000322/// It batches all function passes and basic block pass managers together and
323/// sequence them to process one function at a time before processing next
324/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000325class FunctionPassManagerImpl_New : public ModulePass,
326 public PMDataManager,
327 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000328public:
Devang Patel4e12f862006-11-08 10:44:40 +0000329 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
330 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000331 activeBBPassManager = NULL;
332 }
Devang Patel4e12f862006-11-08 10:44:40 +0000333 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000334
Devang Patelabcd1d32006-12-07 21:27:23 +0000335 inline void addTopLevelPass(Pass *P) {
336 addPass(P);
337 }
338
Devang Patelca58e352006-11-08 10:05:38 +0000339 /// add - Add a pass to the queue of passes to run. This passes
340 /// ownership of the Pass to the PassManager. When the
341 /// PassManager_X is destroyed, the pass will be destroyed as well, so
342 /// there is no need to delete the pass. (TODO delete passes.)
343 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000344 void add(Pass *P) {
345 schedulePass(P, this);
346 }
Devang Patelca58e352006-11-08 10:05:38 +0000347
348 /// Add pass into the pass manager queue.
349 bool addPass(Pass *P);
350
351 /// Execute all of the passes scheduled for execution. Keep
352 /// track of whether any of the passes modifies the function, and if
353 /// so, return true.
354 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000355 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000356
Devang Patelebba9702006-11-13 22:40:09 +0000357 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000358 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000359 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000360
Devang Patelff631ae2006-11-15 01:27:05 +0000361 /// doInitialization - Run all of the initializers for the function passes.
362 ///
363 bool doInitialization(Module &M);
364
365 /// doFinalization - Run all of the initializers for the function passes.
366 ///
367 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000368
369 /// Pass Manager itself does not invalidate any analysis info.
370 void getAnalysisUsage(AnalysisUsage &Info) const {
371 Info.setPreservesAll();
372 }
373
Devang Patelca58e352006-11-08 10:05:38 +0000374private:
Devang Patelca58e352006-11-08 10:05:38 +0000375 // Active Pass Managers
376 BasicBlockPassManager_New *activeBBPassManager;
377};
378
379/// ModulePassManager_New manages ModulePasses and function pass managers.
380/// It batches all Module passes passes and function pass managers together and
381/// sequence them to process one module.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000382class ModulePassManager_New : public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000383
384public:
385 ModulePassManager_New() { activeFunctionPassManager = NULL; }
386
387 /// Add a pass into a passmanager queue.
388 bool addPass(Pass *p);
389
390 /// run - Execute all of the passes scheduled for execution. Keep track of
391 /// whether any of the passes modifies the module, and if so, return true.
392 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000393
394 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000395 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000396 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelf9d96b92006-12-07 19:57:52 +0000397
398 /// Pass Manager itself does not invalidate any analysis info.
399 void getAnalysisUsage(AnalysisUsage &Info) const {
400 Info.setPreservesAll();
401 }
402
Devang Patelca58e352006-11-08 10:05:38 +0000403private:
Devang Patelca58e352006-11-08 10:05:38 +0000404 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000405 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000406};
407
Devang Patel376fefa2006-11-08 10:29:57 +0000408/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000409class PassManagerImpl_New : public Pass,
410 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000411 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000412
413public:
414
415 /// add - Add a pass to the queue of passes to run. This passes ownership of
416 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
417 /// will be destroyed as well, so there is no need to delete the pass. This
418 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000419 void add(Pass *P) {
420 schedulePass(P, this);
421 }
Devang Patel376fefa2006-11-08 10:29:57 +0000422
423 /// run - Execute all of the passes scheduled for execution. Keep track of
424 /// whether any of the passes modifies the module, and if so, return true.
425 bool run(Module &M);
426
Devang Patelebba9702006-11-13 22:40:09 +0000427 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000428 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000429 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000430
Devang Patelf9d96b92006-12-07 19:57:52 +0000431 /// Pass Manager itself does not invalidate any analysis info.
432 void getAnalysisUsage(AnalysisUsage &Info) const {
433 Info.setPreservesAll();
434 }
435
Devang Patelabcd1d32006-12-07 21:27:23 +0000436 inline void addTopLevelPass(Pass *P) {
437 addPass(P);
438 }
439
Devang Patel376fefa2006-11-08 10:29:57 +0000440private:
441
Devang Patelde124182006-12-07 21:10:57 +0000442 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000443 bool addPass(Pass *p);
444
Devang Patel376fefa2006-11-08 10:29:57 +0000445 // Collection of pass managers
446 std::vector<ModulePassManager_New *> PassManagers;
447
Devang Patel376fefa2006-11-08 10:29:57 +0000448 // Active Pass Manager
449 ModulePassManager_New *activeManager;
450};
451
Devang Patelca58e352006-11-08 10:05:38 +0000452} // End of llvm namespace
453
Devang Patela1514cb2006-12-07 19:39:39 +0000454//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000455// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000456
Devang Pateld65e9e92006-11-08 01:31:28 +0000457/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000458/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000459bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000460
Devang Patel8f677ce2006-12-07 18:47:25 +0000461 // TODO
462 // If this pass is not preserving information that is required by a
463 // pass maintained by higher level pass manager then do not insert
464 // this pass into current manager. Use new manager. For example,
465 // For example, If FunctionPass F is not preserving ModulePass Info M1
466 // that is used by another ModulePass M2 then do not insert F in
467 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000468 return true;
469}
470
Devang Patel643676c2006-11-11 01:10:19 +0000471/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000472void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000473
Devang Patel643676c2006-11-11 01:10:19 +0000474 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000475 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000476
Devang Patele9976aa2006-12-07 19:33:53 +0000477 //This pass is the current implementation of all of the interfaces it
478 //implements as well.
479 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
480 for (unsigned i = 0, e = II.size(); i != e; ++i)
481 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000482 }
483}
484
Devang Patelf68a3492006-11-07 22:35:17 +0000485/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000486void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000487 AnalysisUsage AnUsage;
488 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000489
Devang Patel2e169c32006-12-07 20:03:49 +0000490 if (AnUsage.getPreservesAll())
491 return;
492
493 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000494 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000495 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000496 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000497 PreservedSet.end()) {
498 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000499 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000500 AvailableAnalysis.erase(J);
501 }
502 }
Devang Patelf68a3492006-11-07 22:35:17 +0000503}
504
Devang Patelca189262006-11-14 03:05:08 +0000505/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000506void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patelf4805ce2006-12-07 22:17:09 +0000507 // TODO : reimplement
Devang Patelca189262006-11-14 03:05:08 +0000508}
509
Devang Patel8f677ce2006-12-07 18:47:25 +0000510/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000511/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000512void PMDataManager::addPassToManager(Pass *P,
513 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000514
Devang Patel90b05e02006-11-11 02:04:19 +0000515 if (ProcessAnalysis) {
Devang Patel17bff0d2006-12-07 22:09:36 +0000516 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000517 // Remove the analysis not preserved by this pass
Devang Patel17bff0d2006-12-07 22:09:36 +0000518 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000519 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000520 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000521 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000522
523 // Add pass
524 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000525}
526
Devang Patel1d6267c2006-12-07 23:05:44 +0000527/// Populate RequiredPasses with the analysis pass that are required by
528/// pass P.
529void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
530 Pass *P) {
531 AnalysisUsage AnUsage;
532 P->getAnalysisUsage(AnUsage);
533 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
534 for (std::vector<AnalysisID>::const_iterator
535 I = RequiredSet.begin(), E = RequiredSet.end();
536 I != E; ++I) {
537 Pass *AnalysisPass = NULL; //FIXME findAnalysisPass(*I,true);
538 assert (AnalysisPass && "Analysis pass is not available");
539 RP.push_back(AnalysisPass);
540 }
541}
542
Devang Patel07f4f582006-11-14 21:49:36 +0000543// All Required analyses should be available to the pass as it runs! Here
544// we fill in the AnalysisImpls member of the pass so that it can
545// successfully use the getAnalysis() method to retrieve the
546// implementations it needs.
547//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000548void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000549 AnalysisUsage AnUsage;
550 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000551
552 for (std::vector<const PassInfo *>::const_iterator
553 I = AnUsage.getRequiredSet().begin(),
554 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
555 Pass *Impl = getAnalysisPass(*I);
556 if (Impl == 0)
557 assert(0 && "Analysis used but not available!");
558 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
559 }
560}
561
Devang Patela1514cb2006-12-07 19:39:39 +0000562//===----------------------------------------------------------------------===//
563// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000564
Devang Pateld65e9e92006-11-08 01:31:28 +0000565/// Add pass P into PassVector and return true. If this pass is not
566/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000567bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000568BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000569
570 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
571 if (!BP)
572 return false;
573
Devang Patel3c8eb622006-11-07 22:56:50 +0000574 // If this pass does not preserve anlysis that is used by other passes
575 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000576 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000577 return false;
578
Devang Patel8cad70d2006-11-11 01:51:02 +0000579 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000580
Devang Patel6e5a1132006-11-07 21:31:57 +0000581 return true;
582}
583
584/// Execute all of the passes scheduled for execution by invoking
585/// runOnBasicBlock method. Keep track of whether any of the passes modifies
586/// the function, and if so, return true.
587bool
588BasicBlockPassManager_New::runOnFunction(Function &F) {
589
590 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000591 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000592
Devang Patel6e5a1132006-11-07 21:31:57 +0000593 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000594 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
595 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000596 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000597
Devang Patel6e5a1132006-11-07 21:31:57 +0000598 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
599 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000600 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000601 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000602 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000603 }
604 return Changed;
605}
606
Devang Patelebba9702006-11-13 22:40:09 +0000607/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000608/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000609Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
610 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000611}
612
Devang Patela1514cb2006-12-07 19:39:39 +0000613//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000614// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000615
Devang Patel4e12f862006-11-08 10:44:40 +0000616/// Create new Function pass manager
617FunctionPassManager_New::FunctionPassManager_New() {
618 FPM = new FunctionPassManagerImpl_New();
619}
620
621/// add - Add a pass to the queue of passes to run. This passes
622/// ownership of the Pass to the PassManager. When the
623/// PassManager_X is destroyed, the pass will be destroyed as well, so
624/// there is no need to delete the pass. (TODO delete passes.)
625/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000626void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000627 FPM->add(P);
628}
629
630/// Execute all of the passes scheduled for execution. Keep
631/// track of whether any of the passes modifies the function, and if
632/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000633bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000634 return FPM->runOnModule(M);
635}
636
Devang Patel9f3083e2006-11-15 19:39:54 +0000637/// run - Execute all of the passes scheduled for execution. Keep
638/// track of whether any of the passes modifies the function, and if
639/// so, return true.
640///
641bool FunctionPassManager_New::run(Function &F) {
642 std::string errstr;
643 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000644 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000645 abort();
646 }
647 return FPM->runOnFunction(F);
648}
649
650
Devang Patelff631ae2006-11-15 01:27:05 +0000651/// doInitialization - Run all of the initializers for the function passes.
652///
653bool FunctionPassManager_New::doInitialization() {
654 return FPM->doInitialization(*MP->getModule());
655}
656
657/// doFinalization - Run all of the initializers for the function passes.
658///
659bool FunctionPassManager_New::doFinalization() {
660 return FPM->doFinalization(*MP->getModule());
661}
662
Devang Patela1514cb2006-12-07 19:39:39 +0000663//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000664// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000665
Devang Patel0c2012f2006-11-07 21:49:50 +0000666/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
667/// either use it into active basic block pass manager or create new basic
668/// block pass manager to handle pass P.
669bool
Devang Patel4e12f862006-11-08 10:44:40 +0000670FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000671
672 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
673 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
674
Devang Patel4949fe02006-12-07 22:34:21 +0000675 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000676
Devang Patel4949fe02006-12-07 22:34:21 +0000677 // If active manager exists then clear its analysis info.
678 if (activeBBPassManager)
679 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000680
Devang Patel4949fe02006-12-07 22:34:21 +0000681 // Create and add new manager
Devang Patel0c2012f2006-11-07 21:49:50 +0000682 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000683 addPassToManager(activeBBPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000684
685 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000686 if (!activeBBPassManager->addPass(BP))
687 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000688 }
689 return true;
690 }
691
692 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
693 if (!FP)
694 return false;
695
Devang Patel3c8eb622006-11-07 22:56:50 +0000696 // If this pass does not preserve anlysis that is used by other passes
697 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000698 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000699 return false;
700
Devang Patel8cad70d2006-11-11 01:51:02 +0000701 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000702
703 // If active manager exists then clear its analysis info.
704 if (activeBBPassManager) {
705 activeBBPassManager->initializeAnalysisInfo();
706 activeBBPassManager = NULL;
707 }
708
Devang Patel0c2012f2006-11-07 21:49:50 +0000709 return true;
710}
711
712/// Execute all of the passes scheduled for execution by invoking
713/// runOnFunction method. Keep track of whether any of the passes modifies
714/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000715bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000716
717 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000718 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000719
Devang Patel0c2012f2006-11-07 21:49:50 +0000720 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000721 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
722 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000723 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000724
Devang Patel0c2012f2006-11-07 21:49:50 +0000725 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
726 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000727 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000728 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000729 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000730 }
731 return Changed;
732}
733
Devang Patel9f3083e2006-11-15 19:39:54 +0000734/// Execute all of the passes scheduled for execution by invoking
735/// runOnFunction method. Keep track of whether any of the passes modifies
736/// the function, and if so, return true.
737bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
738
739 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000740 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000741
742 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
743 e = passVectorEnd(); itr != e; ++itr) {
744 Pass *P = *itr;
745
Devang Patel9f3083e2006-11-15 19:39:54 +0000746 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
747 Changed |= FP->runOnFunction(F);
748 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000749 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000750 removeDeadPasses(P);
751 }
752 return Changed;
753}
754
755
Devang Patelebba9702006-11-13 22:40:09 +0000756/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000757/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000758Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000759
Devang Patel3f0832a2006-11-14 02:54:23 +0000760 Pass *P = getAnalysisPass(AID);
761 if (P)
762 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000763
764 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000765 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000766 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000767
768 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000769 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000770}
Devang Patel0c2012f2006-11-07 21:49:50 +0000771
Devang Patelff631ae2006-11-15 01:27:05 +0000772inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
773 bool Changed = false;
774
775 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
776 e = passVectorEnd(); itr != e; ++itr) {
777 Pass *P = *itr;
778
779 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
780 Changed |= FP->doInitialization(M);
781 }
782
783 return Changed;
784}
785
786inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
787 bool Changed = false;
788
789 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
790 e = passVectorEnd(); itr != e; ++itr) {
791 Pass *P = *itr;
792
793 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
794 Changed |= FP->doFinalization(M);
795 }
796
797
798 return Changed;
799}
800
Devang Patela1514cb2006-12-07 19:39:39 +0000801//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000802// ModulePassManager implementation
803
804/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000805/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000806/// is not manageable by this manager.
807bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000808ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000809
810 // If P is FunctionPass then use function pass maanager.
811 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
812
Devang Patel05e1a972006-11-07 22:03:15 +0000813 if (!activeFunctionPassManager
814 || !activeFunctionPassManager->addPass(P)) {
815
Devang Patel4949fe02006-12-07 22:34:21 +0000816 // If active manager exists then clear its analysis info.
817 if (activeFunctionPassManager)
818 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000819
Devang Patel4949fe02006-12-07 22:34:21 +0000820 // Create and add new manager
Devang Patel4e12f862006-11-08 10:44:40 +0000821 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000822 addPassToManager(activeFunctionPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000823
824 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000825 if (!activeFunctionPassManager->addPass(FP))
826 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000827 }
828 return true;
829 }
830
831 ModulePass *MP = dynamic_cast<ModulePass *>(P);
832 if (!MP)
833 return false;
834
Devang Patel3c8eb622006-11-07 22:56:50 +0000835 // If this pass does not preserve anlysis that is used by other passes
836 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000837 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000838 return false;
839
Devang Patel8cad70d2006-11-11 01:51:02 +0000840 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +0000841 // If active manager exists then clear its analysis info.
842 if (activeFunctionPassManager) {
843 activeFunctionPassManager->initializeAnalysisInfo();
844 activeFunctionPassManager = NULL;
845 }
846
Devang Patel05e1a972006-11-07 22:03:15 +0000847 return true;
848}
849
850
851/// Execute all of the passes scheduled for execution by invoking
852/// runOnModule method. Keep track of whether any of the passes modifies
853/// the module, and if so, return true.
854bool
855ModulePassManager_New::runOnModule(Module &M) {
856 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000857 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000858
Devang Patel8cad70d2006-11-11 01:51:02 +0000859 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
860 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000861 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000862
Devang Patel05e1a972006-11-07 22:03:15 +0000863 ModulePass *MP = dynamic_cast<ModulePass*>(P);
864 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000865 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000866 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000867 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000868 }
869 return Changed;
870}
871
Devang Patelebba9702006-11-13 22:40:09 +0000872/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000873/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000874Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000875
Devang Patel3f0832a2006-11-14 02:54:23 +0000876 Pass *P = getAnalysisPass(AID);
877 if (P)
878 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000879
880 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000881 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000882 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000883
884 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000885 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000886}
887
Devang Patela1514cb2006-12-07 19:39:39 +0000888//===----------------------------------------------------------------------===//
889// PassManagerImpl implementation
890
Devang Patelebba9702006-11-13 22:40:09 +0000891/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000892/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000893Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000894
Devang Patel3f0832a2006-11-14 02:54:23 +0000895 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000896 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000897 e = PassManagers.end(); !P && itr != e; ++itr)
898 P = (*itr)->getAnalysisPassFromManager(AID);
899 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000900}
901
Devang Patelc290c8a2006-11-07 22:23:34 +0000902// PassManager_New implementation
903/// Add P into active pass manager or use new module pass manager to
904/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000905bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000906
Devang Patel6c9f5482006-11-11 00:42:16 +0000907 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000908 activeManager = new ModulePassManager_New();
909 PassManagers.push_back(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +0000910 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +0000911 }
Devang Patel28bbcbe2006-12-07 21:44:12 +0000912 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +0000913}
914
915/// run - Execute all of the passes scheduled for execution. Keep track of
916/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000917bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000918
Devang Patelc290c8a2006-11-07 22:23:34 +0000919 bool Changed = false;
920 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
921 e = PassManagers.end(); itr != e; ++itr) {
922 ModulePassManager_New *pm = *itr;
923 Changed |= pm->runOnModule(M);
924 }
925 return Changed;
926}
Devang Patel376fefa2006-11-08 10:29:57 +0000927
Devang Patela1514cb2006-12-07 19:39:39 +0000928//===----------------------------------------------------------------------===//
929// PassManager implementation
930
Devang Patel376fefa2006-11-08 10:29:57 +0000931/// Create new pass manager
932PassManager_New::PassManager_New() {
933 PM = new PassManagerImpl_New();
934}
935
936/// add - Add a pass to the queue of passes to run. This passes ownership of
937/// the Pass to the PassManager. When the PassManager is destroyed, the pass
938/// will be destroyed as well, so there is no need to delete the pass. This
939/// implies that all passes MUST be allocated with 'new'.
940void
941PassManager_New::add(Pass *P) {
942 PM->add(P);
943}
944
945/// run - Execute all of the passes scheduled for execution. Keep track of
946/// whether any of the passes modifies the module, and if so, return true.
947bool
948PassManager_New::run(Module &M) {
949 return PM->run(M);
950}
951