blob: 29d0ec1b7319085df25e7dd7adf8aacf35d6e983 [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 Patela6b6dcb2006-12-07 18:41:09 +0000244 // Initialize available analysis information.
245 void initializeAnalysisInfo() {
Devang Patel050ec722006-11-14 01:23:29 +0000246 AvailableAnalysis.clear();
Devang Patel3f0832a2006-11-14 02:54:23 +0000247 LastUser.clear();
Devang Patelb3900322006-12-07 21:02:08 +0000248
249 // Include immutable passes into AvailableAnalysis vector.
250 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
251 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
252 E = ImmutablePasses.end(); I != E; ++I)
253 recordAvailableAnalysis(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000254 }
255
Devang Patelf60b5d92006-11-14 01:59:59 +0000256 // All Required analyses should be available to the pass as it runs! Here
257 // we fill in the AnalysisImpls member of the pass so that it can
258 // successfully use the getAnalysis() method to retrieve the
259 // implementations it needs.
260 //
Devang Patel07f4f582006-11-14 21:49:36 +0000261 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000262
Devang Patel8cad70d2006-11-11 01:51:02 +0000263 inline std::vector<Pass *>::iterator passVectorBegin() {
264 return PassVector.begin();
265 }
266
267 inline std::vector<Pass *>::iterator passVectorEnd() {
268 return PassVector.end();
269 }
270
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000271 inline void setLastUser(Pass *P, Pass *LU) {
Devang Patel07f4f582006-11-14 21:49:36 +0000272 LastUser[P] = LU;
273 // TODO : Check if pass P is available.
Devang Patel07f4f582006-11-14 21:49:36 +0000274 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000275
Devang Patelf3827bc2006-12-07 19:54:15 +0000276 // Access toplevel manager
277 PMTopLevelManager *getTopLevelManager() { return TPM; }
278 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
279
Devang Patela9844592006-11-11 01:31:05 +0000280private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000281 // Set of available Analysis. This information is used while scheduling
282 // pass. If a pass requires an analysis which is not not available then
283 // equired analysis pass is scheduled to run before the pass itself is
284 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000285 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000286
Devang Patel3f0832a2006-11-14 02:54:23 +0000287 // Map to keep track of last user of the analysis pass.
288 // LastUser->second is the last user of Lastuser->first.
289 std::map<Pass *, Pass *> LastUser;
290
Devang Patel8cad70d2006-11-11 01:51:02 +0000291 // Collection of pass that are managed by this manager
292 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000293
294 // Top level manager.
295 // TODO : Make it a reference.
296 PMTopLevelManager *TPM;
Devang Patela9844592006-11-11 01:31:05 +0000297};
298
Devang Patelca58e352006-11-08 10:05:38 +0000299/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
300/// pass together and sequence them to process one basic block before
301/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000302class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000303 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000304
305public:
306 BasicBlockPassManager_New() { }
307
308 /// Add a pass into a passmanager queue.
309 bool addPass(Pass *p);
310
311 /// Execute all of the passes scheduled for execution. Keep track of
312 /// whether any of the passes modifies the function, and if so, return true.
313 bool runOnFunction(Function &F);
314
Devang Patelebba9702006-11-13 22:40:09 +0000315 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000316 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000317 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000318
Devang Patelf9d96b92006-12-07 19:57:52 +0000319 /// Pass Manager itself does not invalidate any analysis info.
320 void getAnalysisUsage(AnalysisUsage &Info) const {
321 Info.setPreservesAll();
322 }
323
Devang Patelca58e352006-11-08 10:05:38 +0000324private:
Devang Patelca58e352006-11-08 10:05:38 +0000325};
326
Devang Patel4e12f862006-11-08 10:44:40 +0000327/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000328/// It batches all function passes and basic block pass managers together and
329/// sequence them to process one function at a time before processing next
330/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000331class FunctionPassManagerImpl_New : public ModulePass,
332 public PMDataManager,
333 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000334public:
Devang Patel4e12f862006-11-08 10:44:40 +0000335 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
336 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000337 activeBBPassManager = NULL;
338 }
Devang Patel4e12f862006-11-08 10:44:40 +0000339 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000340
Devang Patelabcd1d32006-12-07 21:27:23 +0000341 inline void addTopLevelPass(Pass *P) {
342 addPass(P);
343 }
344
Devang Patelca58e352006-11-08 10:05:38 +0000345 /// add - Add a pass to the queue of passes to run. This passes
346 /// ownership of the Pass to the PassManager. When the
347 /// PassManager_X is destroyed, the pass will be destroyed as well, so
348 /// there is no need to delete the pass. (TODO delete passes.)
349 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000350 void add(Pass *P) {
351 schedulePass(P, this);
352 }
Devang Patelca58e352006-11-08 10:05:38 +0000353
354 /// Add pass into the pass manager queue.
355 bool addPass(Pass *P);
356
357 /// Execute all of the passes scheduled for execution. Keep
358 /// track of whether any of the passes modifies the function, and if
359 /// so, return true.
360 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000361 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000362
Devang Patelebba9702006-11-13 22:40:09 +0000363 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000364 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000365 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000366
Devang Patelff631ae2006-11-15 01:27:05 +0000367 /// doInitialization - Run all of the initializers for the function passes.
368 ///
369 bool doInitialization(Module &M);
370
371 /// doFinalization - Run all of the initializers for the function passes.
372 ///
373 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000374
375 /// Pass Manager itself does not invalidate any analysis info.
376 void getAnalysisUsage(AnalysisUsage &Info) const {
377 Info.setPreservesAll();
378 }
379
Devang Patelca58e352006-11-08 10:05:38 +0000380private:
Devang Patelca58e352006-11-08 10:05:38 +0000381 // Active Pass Managers
382 BasicBlockPassManager_New *activeBBPassManager;
383};
384
385/// ModulePassManager_New manages ModulePasses and function pass managers.
386/// It batches all Module passes passes and function pass managers together and
387/// sequence them to process one module.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000388class ModulePassManager_New : public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000389
390public:
391 ModulePassManager_New() { activeFunctionPassManager = NULL; }
392
393 /// Add a pass into a passmanager queue.
394 bool addPass(Pass *p);
395
396 /// run - Execute all of the passes scheduled for execution. Keep track of
397 /// whether any of the passes modifies the module, and if so, return true.
398 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000399
400 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000401 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000402 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelf9d96b92006-12-07 19:57:52 +0000403
404 /// Pass Manager itself does not invalidate any analysis info.
405 void getAnalysisUsage(AnalysisUsage &Info) const {
406 Info.setPreservesAll();
407 }
408
Devang Patelca58e352006-11-08 10:05:38 +0000409private:
Devang Patelca58e352006-11-08 10:05:38 +0000410 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000411 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000412};
413
Devang Patel376fefa2006-11-08 10:29:57 +0000414/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000415class PassManagerImpl_New : public Pass,
416 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000417 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000418
419public:
420
421 /// add - Add a pass to the queue of passes to run. This passes ownership of
422 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
423 /// will be destroyed as well, so there is no need to delete the pass. This
424 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000425 void add(Pass *P) {
426 schedulePass(P, this);
427 }
Devang Patel376fefa2006-11-08 10:29:57 +0000428
429 /// run - Execute all of the passes scheduled for execution. Keep track of
430 /// whether any of the passes modifies the module, and if so, return true.
431 bool run(Module &M);
432
Devang Patelebba9702006-11-13 22:40:09 +0000433 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000434 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000435 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000436
Devang Patelf9d96b92006-12-07 19:57:52 +0000437 /// Pass Manager itself does not invalidate any analysis info.
438 void getAnalysisUsage(AnalysisUsage &Info) const {
439 Info.setPreservesAll();
440 }
441
Devang Patelabcd1d32006-12-07 21:27:23 +0000442 inline void addTopLevelPass(Pass *P) {
443 addPass(P);
444 }
445
Devang Patel376fefa2006-11-08 10:29:57 +0000446private:
447
Devang Patelde124182006-12-07 21:10:57 +0000448 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000449 bool addPass(Pass *p);
450
Devang Patel376fefa2006-11-08 10:29:57 +0000451 // Collection of pass managers
452 std::vector<ModulePassManager_New *> PassManagers;
453
Devang Patel376fefa2006-11-08 10:29:57 +0000454 // Active Pass Manager
455 ModulePassManager_New *activeManager;
456};
457
Devang Patelca58e352006-11-08 10:05:38 +0000458} // End of llvm namespace
459
Devang Patela1514cb2006-12-07 19:39:39 +0000460//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000461// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000462
Devang Pateld65e9e92006-11-08 01:31:28 +0000463/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000464/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000465bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000466
Devang Patel8f677ce2006-12-07 18:47:25 +0000467 // TODO
468 // If this pass is not preserving information that is required by a
469 // pass maintained by higher level pass manager then do not insert
470 // this pass into current manager. Use new manager. For example,
471 // For example, If FunctionPass F is not preserving ModulePass Info M1
472 // that is used by another ModulePass M2 then do not insert F in
473 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000474 return true;
475}
476
Devang Patel643676c2006-11-11 01:10:19 +0000477/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000478void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000479
Devang Patel643676c2006-11-11 01:10:19 +0000480 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000481 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000482
Devang Patele9976aa2006-12-07 19:33:53 +0000483 //This pass is the current implementation of all of the interfaces it
484 //implements as well.
485 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
486 for (unsigned i = 0, e = II.size(); i != e; ++i)
487 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000488 }
489}
490
Devang Patelf68a3492006-11-07 22:35:17 +0000491/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000492void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000493 AnalysisUsage AnUsage;
494 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000495
Devang Patel2e169c32006-12-07 20:03:49 +0000496 if (AnUsage.getPreservesAll())
497 return;
498
499 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000500 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000501 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000502 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000503 PreservedSet.end()) {
504 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000505 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000506 AvailableAnalysis.erase(J);
507 }
508 }
Devang Patelf68a3492006-11-07 22:35:17 +0000509}
510
Devang Patelca189262006-11-14 03:05:08 +0000511/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000512void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patelca189262006-11-14 03:05:08 +0000513
514 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
515 E = LastUser.end(); I !=E; ++I) {
516 if (I->second == P) {
517 Pass *deadPass = I->first;
518 deadPass->releaseMemory();
519
520 std::map<AnalysisID, Pass*>::iterator Pos =
521 AvailableAnalysis.find(deadPass->getPassInfo());
522
523 assert (Pos != AvailableAnalysis.end() &&
524 "Pass is not available");
525 AvailableAnalysis.erase(Pos);
526 }
527 }
528}
529
Devang Patel8f677ce2006-12-07 18:47:25 +0000530/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000531/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000532void PMDataManager::addPassToManager(Pass *P,
533 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000534
Devang Patel90b05e02006-11-11 02:04:19 +0000535 if (ProcessAnalysis) {
Devang Patel17bff0d2006-12-07 22:09:36 +0000536 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000537 // Remove the analysis not preserved by this pass
Devang Patel17bff0d2006-12-07 22:09:36 +0000538 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000539 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000540 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000541 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000542
543 // Add pass
544 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000545}
546
Devang Patel07f4f582006-11-14 21:49:36 +0000547// All Required analyses should be available to the pass as it runs! Here
548// we fill in the AnalysisImpls member of the pass so that it can
549// successfully use the getAnalysis() method to retrieve the
550// implementations it needs.
551//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000552void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000553 AnalysisUsage AnUsage;
554 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000555
556 for (std::vector<const PassInfo *>::const_iterator
557 I = AnUsage.getRequiredSet().begin(),
558 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
559 Pass *Impl = getAnalysisPass(*I);
560 if (Impl == 0)
561 assert(0 && "Analysis used but not available!");
562 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
563 }
564}
565
Devang Patela1514cb2006-12-07 19:39:39 +0000566//===----------------------------------------------------------------------===//
567// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000568
Devang Pateld65e9e92006-11-08 01:31:28 +0000569/// Add pass P into PassVector and return true. If this pass is not
570/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000571bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000572BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000573
574 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
575 if (!BP)
576 return false;
577
Devang Patel3c8eb622006-11-07 22:56:50 +0000578 // If this pass does not preserve anlysis that is used by other passes
579 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000580 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000581 return false;
582
Devang Patel8cad70d2006-11-11 01:51:02 +0000583 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000584
Devang Patel6e5a1132006-11-07 21:31:57 +0000585 return true;
586}
587
588/// Execute all of the passes scheduled for execution by invoking
589/// runOnBasicBlock method. Keep track of whether any of the passes modifies
590/// the function, and if so, return true.
591bool
592BasicBlockPassManager_New::runOnFunction(Function &F) {
593
594 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000595 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000596
Devang Patel6e5a1132006-11-07 21:31:57 +0000597 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000598 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
599 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000600 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000601
Devang Patel6e5a1132006-11-07 21:31:57 +0000602 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
603 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000604 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000605 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000606 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000607 }
608 return Changed;
609}
610
Devang Patelebba9702006-11-13 22:40:09 +0000611/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000612/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000613Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
614 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000615}
616
Devang Patela1514cb2006-12-07 19:39:39 +0000617//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000618// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000619
Devang Patel4e12f862006-11-08 10:44:40 +0000620/// Create new Function pass manager
621FunctionPassManager_New::FunctionPassManager_New() {
622 FPM = new FunctionPassManagerImpl_New();
623}
624
625/// add - Add a pass to the queue of passes to run. This passes
626/// ownership of the Pass to the PassManager. When the
627/// PassManager_X is destroyed, the pass will be destroyed as well, so
628/// there is no need to delete the pass. (TODO delete passes.)
629/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000630void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000631 FPM->add(P);
632}
633
634/// Execute all of the passes scheduled for execution. Keep
635/// track of whether any of the passes modifies the function, and if
636/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000637bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000638 return FPM->runOnModule(M);
639}
640
Devang Patel9f3083e2006-11-15 19:39:54 +0000641/// run - Execute all of the passes scheduled for execution. Keep
642/// track of whether any of the passes modifies the function, and if
643/// so, return true.
644///
645bool FunctionPassManager_New::run(Function &F) {
646 std::string errstr;
647 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000648 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000649 abort();
650 }
651 return FPM->runOnFunction(F);
652}
653
654
Devang Patelff631ae2006-11-15 01:27:05 +0000655/// doInitialization - Run all of the initializers for the function passes.
656///
657bool FunctionPassManager_New::doInitialization() {
658 return FPM->doInitialization(*MP->getModule());
659}
660
661/// doFinalization - Run all of the initializers for the function passes.
662///
663bool FunctionPassManager_New::doFinalization() {
664 return FPM->doFinalization(*MP->getModule());
665}
666
Devang Patela1514cb2006-12-07 19:39:39 +0000667//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000668// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000669
Devang Patel0c2012f2006-11-07 21:49:50 +0000670/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
671/// either use it into active basic block pass manager or create new basic
672/// block pass manager to handle pass P.
673bool
Devang Patel4e12f862006-11-08 10:44:40 +0000674FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000675
676 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
677 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
678
679 if (!activeBBPassManager
680 || !activeBBPassManager->addPass(BP)) {
681
Devang Patel642c1432006-12-07 21:58:50 +0000682 // TODO : intialize AvailableAnalysis info
683
Devang Patel0c2012f2006-11-07 21:49:50 +0000684 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000685 addPassToManager(activeBBPassManager, false);
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 Patel642c1432006-12-07 21:58:50 +0000702 // TODO : intialize AvailableAnalysis info
Devang Patel0c2012f2006-11-07 21:49:50 +0000703 activeBBPassManager = NULL;
704 return true;
705}
706
707/// Execute all of the passes scheduled for execution by invoking
708/// runOnFunction method. Keep track of whether any of the passes modifies
709/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000710bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000711
712 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000713 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000714
Devang Patel0c2012f2006-11-07 21:49:50 +0000715 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000716 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
717 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000718 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000719
Devang Patel0c2012f2006-11-07 21:49:50 +0000720 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
721 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000722 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000723 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000724 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000725 }
726 return Changed;
727}
728
Devang Patel9f3083e2006-11-15 19:39:54 +0000729/// Execute all of the passes scheduled for execution by invoking
730/// runOnFunction method. Keep track of whether any of the passes modifies
731/// the function, and if so, return true.
732bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
733
734 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000735 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000736
737 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
738 e = passVectorEnd(); itr != e; ++itr) {
739 Pass *P = *itr;
740
Devang Patel9f3083e2006-11-15 19:39:54 +0000741 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
742 Changed |= FP->runOnFunction(F);
743 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000744 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000745 removeDeadPasses(P);
746 }
747 return Changed;
748}
749
750
Devang Patelebba9702006-11-13 22:40:09 +0000751/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000752/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000753Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000754
Devang Patel3f0832a2006-11-14 02:54:23 +0000755 Pass *P = getAnalysisPass(AID);
756 if (P)
757 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000758
759 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000760 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000761 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000762
763 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000764 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000765}
Devang Patel0c2012f2006-11-07 21:49:50 +0000766
Devang Patelff631ae2006-11-15 01:27:05 +0000767inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
768 bool Changed = false;
769
770 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
771 e = passVectorEnd(); itr != e; ++itr) {
772 Pass *P = *itr;
773
774 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
775 Changed |= FP->doInitialization(M);
776 }
777
778 return Changed;
779}
780
781inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
782 bool Changed = false;
783
784 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
785 e = passVectorEnd(); itr != e; ++itr) {
786 Pass *P = *itr;
787
788 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
789 Changed |= FP->doFinalization(M);
790 }
791
792
793 return Changed;
794}
795
Devang Patela1514cb2006-12-07 19:39:39 +0000796//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000797// ModulePassManager implementation
798
799/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000800/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000801/// is not manageable by this manager.
802bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000803ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000804
805 // If P is FunctionPass then use function pass maanager.
806 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
807
Devang Patel05e1a972006-11-07 22:03:15 +0000808 if (!activeFunctionPassManager
809 || !activeFunctionPassManager->addPass(P)) {
810
Devang Patel642c1432006-12-07 21:58:50 +0000811 // TODO : intialize AvailableAnalysis info
812 activeFunctionPassManager = NULL;
813
Devang Patel4e12f862006-11-08 10:44:40 +0000814 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000815 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000816 if (!activeFunctionPassManager->addPass(FP))
817 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000818 }
819 return true;
820 }
821
822 ModulePass *MP = dynamic_cast<ModulePass *>(P);
823 if (!MP)
824 return false;
825
Devang Patel3c8eb622006-11-07 22:56:50 +0000826 // If this pass does not preserve anlysis that is used by other passes
827 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000828 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000829 return false;
830
Devang Patel8cad70d2006-11-11 01:51:02 +0000831 addPassToManager(MP);
Devang Patel642c1432006-12-07 21:58:50 +0000832 // TODO : intialize AvailableAnalysis info
Devang Patel05e1a972006-11-07 22:03:15 +0000833 activeFunctionPassManager = NULL;
834 return true;
835}
836
837
838/// Execute all of the passes scheduled for execution by invoking
839/// runOnModule method. Keep track of whether any of the passes modifies
840/// the module, and if so, return true.
841bool
842ModulePassManager_New::runOnModule(Module &M) {
843 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000844 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000845
Devang Patel8cad70d2006-11-11 01:51:02 +0000846 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
847 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000848 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000849
Devang Patel05e1a972006-11-07 22:03:15 +0000850 ModulePass *MP = dynamic_cast<ModulePass*>(P);
851 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000852 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000853 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000854 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000855 }
856 return Changed;
857}
858
Devang Patelebba9702006-11-13 22:40:09 +0000859/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000860/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000861Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000862
Devang Patel3f0832a2006-11-14 02:54:23 +0000863 Pass *P = getAnalysisPass(AID);
864 if (P)
865 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000866
867 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000868 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000869 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000870
871 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000872 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000873}
874
Devang Patela1514cb2006-12-07 19:39:39 +0000875//===----------------------------------------------------------------------===//
876// PassManagerImpl implementation
877
Devang Patelebba9702006-11-13 22:40:09 +0000878/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000879/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000880Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000881
Devang Patel3f0832a2006-11-14 02:54:23 +0000882 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000883 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000884 e = PassManagers.end(); !P && itr != e; ++itr)
885 P = (*itr)->getAnalysisPassFromManager(AID);
886 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000887}
888
Devang Patelc290c8a2006-11-07 22:23:34 +0000889// PassManager_New implementation
890/// Add P into active pass manager or use new module pass manager to
891/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000892bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000893
Devang Patel6c9f5482006-11-11 00:42:16 +0000894 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000895 activeManager = new ModulePassManager_New();
896 PassManagers.push_back(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +0000897 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +0000898 }
Devang Patel28bbcbe2006-12-07 21:44:12 +0000899 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +0000900}
901
902/// run - Execute all of the passes scheduled for execution. Keep track of
903/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000904bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000905
Devang Patelc290c8a2006-11-07 22:23:34 +0000906 bool Changed = false;
907 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
908 e = PassManagers.end(); itr != e; ++itr) {
909 ModulePassManager_New *pm = *itr;
910 Changed |= pm->runOnModule(M);
911 }
912 return Changed;
913}
Devang Patel376fefa2006-11-08 10:29:57 +0000914
Devang Patela1514cb2006-12-07 19:39:39 +0000915//===----------------------------------------------------------------------===//
916// PassManager implementation
917
Devang Patel376fefa2006-11-08 10:29:57 +0000918/// Create new pass manager
919PassManager_New::PassManager_New() {
920 PM = new PassManagerImpl_New();
921}
922
923/// add - Add a pass to the queue of passes to run. This passes ownership of
924/// the Pass to the PassManager. When the PassManager is destroyed, the pass
925/// will be destroyed as well, so there is no need to delete the pass. This
926/// implies that all passes MUST be allocated with 'new'.
927void
928PassManager_New::add(Pass *P) {
929 PM->add(P);
930}
931
932/// run - Execute all of the passes scheduled for execution. Keep track of
933/// whether any of the passes modifies the module, and if so, return true.
934bool
935PassManager_New::run(Module &M) {
936 return PM->run(M);
937}
938