blob: 3ab01854c4279b25359452263d57fb7fa0db9eba [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 Patelf3827bc2006-12-07 19:54:15 +0000176//===----------------------------------------------------------------------===//
177// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000178
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000179/// PMDataManager provides the common place to manage the analysis data
180/// used by pass managers.
181class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000182
183public:
184
Devang Patelf3827bc2006-12-07 19:54:15 +0000185 PMDataManager() : TPM(NULL) {
186 initializeAnalysisInfo();
187 }
188
Devang Patela9844592006-11-11 01:31:05 +0000189 /// Return true IFF pass P's required analysis set does not required new
190 /// manager.
191 bool manageablePass(Pass *P);
192
Devang Patelf60b5d92006-11-14 01:59:59 +0000193 Pass *getAnalysisPass(AnalysisID AID) const {
194
195 std::map<AnalysisID, Pass*>::const_iterator I =
196 AvailableAnalysis.find(AID);
197
198 if (I != AvailableAnalysis.end())
199 return NULL;
200 else
201 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +0000202 }
Devang Patela9844592006-11-11 01:31:05 +0000203
Devang Patela9844592006-11-11 01:31:05 +0000204 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000205 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000206
Devang Patela9844592006-11-11 01:31:05 +0000207 /// Remove Analysis that is not preserved by the pass
208 void removeNotPreservedAnalysis(Pass *P);
209
210 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000211 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000212
Devang Patel8f677ce2006-12-07 18:47:25 +0000213 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000214 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
215 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000216
Devang Patela6b6dcb2006-12-07 18:41:09 +0000217 // Initialize available analysis information.
218 void initializeAnalysisInfo() {
Devang Patel050ec722006-11-14 01:23:29 +0000219 AvailableAnalysis.clear();
Devang Patel3f0832a2006-11-14 02:54:23 +0000220 LastUser.clear();
Devang Patelb3900322006-12-07 21:02:08 +0000221
222 // Include immutable passes into AvailableAnalysis vector.
223 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
224 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
225 E = ImmutablePasses.end(); I != E; ++I)
226 recordAvailableAnalysis(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000227 }
228
Devang Patelf60b5d92006-11-14 01:59:59 +0000229 // All Required analyses should be available to the pass as it runs! Here
230 // we fill in the AnalysisImpls member of the pass so that it can
231 // successfully use the getAnalysis() method to retrieve the
232 // implementations it needs.
233 //
Devang Patel07f4f582006-11-14 21:49:36 +0000234 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000235
Devang Patel8cad70d2006-11-11 01:51:02 +0000236 inline std::vector<Pass *>::iterator passVectorBegin() {
237 return PassVector.begin();
238 }
239
240 inline std::vector<Pass *>::iterator passVectorEnd() {
241 return PassVector.end();
242 }
243
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000244 inline void setLastUser(Pass *P, Pass *LU) {
Devang Patel07f4f582006-11-14 21:49:36 +0000245 LastUser[P] = LU;
246 // TODO : Check if pass P is available.
Devang Patel07f4f582006-11-14 21:49:36 +0000247 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000248
Devang Patelf3827bc2006-12-07 19:54:15 +0000249 // Access toplevel manager
250 PMTopLevelManager *getTopLevelManager() { return TPM; }
251 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
252
Devang Patela9844592006-11-11 01:31:05 +0000253private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000254 // Set of available Analysis. This information is used while scheduling
255 // pass. If a pass requires an analysis which is not not available then
256 // equired analysis pass is scheduled to run before the pass itself is
257 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000258 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000259
Devang Patel3f0832a2006-11-14 02:54:23 +0000260 // Map to keep track of last user of the analysis pass.
261 // LastUser->second is the last user of Lastuser->first.
262 std::map<Pass *, Pass *> LastUser;
263
Devang Patel8cad70d2006-11-11 01:51:02 +0000264 // Collection of pass that are managed by this manager
265 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000266
267 // Top level manager.
268 // TODO : Make it a reference.
269 PMTopLevelManager *TPM;
Devang Patela9844592006-11-11 01:31:05 +0000270};
271
Devang Patelca58e352006-11-08 10:05:38 +0000272/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
273/// pass together and sequence them to process one basic block before
274/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000275class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000276 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000277
278public:
279 BasicBlockPassManager_New() { }
280
281 /// Add a pass into a passmanager queue.
282 bool addPass(Pass *p);
283
284 /// Execute all of the passes scheduled for execution. Keep track of
285 /// whether any of the passes modifies the function, and if so, return true.
286 bool runOnFunction(Function &F);
287
Devang Patelebba9702006-11-13 22:40:09 +0000288 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000289 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000290
Devang Patelf9d96b92006-12-07 19:57:52 +0000291 /// Pass Manager itself does not invalidate any analysis info.
292 void getAnalysisUsage(AnalysisUsage &Info) const {
293 Info.setPreservesAll();
294 }
295
Devang Patelca58e352006-11-08 10:05:38 +0000296private:
Devang Patelca58e352006-11-08 10:05:38 +0000297};
298
Devang Patel4e12f862006-11-08 10:44:40 +0000299/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000300/// It batches all function passes and basic block pass managers together and
301/// sequence them to process one function at a time before processing next
302/// function.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000303class FunctionPassManagerImpl_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000304 public ModulePass {
Devang Patelca58e352006-11-08 10:05:38 +0000305public:
Devang Patel4e12f862006-11-08 10:44:40 +0000306 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
307 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000308 activeBBPassManager = NULL;
309 }
Devang Patel4e12f862006-11-08 10:44:40 +0000310 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000311
312 /// add - Add a pass to the queue of passes to run. This passes
313 /// ownership of the Pass to the PassManager. When the
314 /// PassManager_X is destroyed, the pass will be destroyed as well, so
315 /// there is no need to delete the pass. (TODO delete passes.)
316 /// This implies that all passes MUST be allocated with 'new'.
317 void add(Pass *P) { /* TODO*/ }
318
319 /// Add pass into the pass manager queue.
320 bool addPass(Pass *P);
321
322 /// Execute all of the passes scheduled for execution. Keep
323 /// track of whether any of the passes modifies the function, and if
324 /// so, return true.
325 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000326 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000327
Devang Patelebba9702006-11-13 22:40:09 +0000328 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000329 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000330
Devang Patelff631ae2006-11-15 01:27:05 +0000331 /// doInitialization - Run all of the initializers for the function passes.
332 ///
333 bool doInitialization(Module &M);
334
335 /// doFinalization - Run all of the initializers for the function passes.
336 ///
337 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000338
339 /// Pass Manager itself does not invalidate any analysis info.
340 void getAnalysisUsage(AnalysisUsage &Info) const {
341 Info.setPreservesAll();
342 }
343
Devang Patelca58e352006-11-08 10:05:38 +0000344private:
Devang Patelca58e352006-11-08 10:05:38 +0000345 // Active Pass Managers
346 BasicBlockPassManager_New *activeBBPassManager;
347};
348
349/// ModulePassManager_New manages ModulePasses and function pass managers.
350/// It batches all Module passes passes and function pass managers together and
351/// sequence them to process one module.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000352class ModulePassManager_New : public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000353
354public:
355 ModulePassManager_New() { activeFunctionPassManager = NULL; }
356
357 /// Add a pass into a passmanager queue.
358 bool addPass(Pass *p);
359
360 /// run - Execute all of the passes scheduled for execution. Keep track of
361 /// whether any of the passes modifies the module, and if so, return true.
362 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000363
364 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000365 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelf9d96b92006-12-07 19:57:52 +0000366
367 /// Pass Manager itself does not invalidate any analysis info.
368 void getAnalysisUsage(AnalysisUsage &Info) const {
369 Info.setPreservesAll();
370 }
371
Devang Patelca58e352006-11-08 10:05:38 +0000372private:
Devang Patelca58e352006-11-08 10:05:38 +0000373 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000374 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000375};
376
Devang Patel376fefa2006-11-08 10:29:57 +0000377/// PassManager_New manages ModulePassManagers
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000378class PassManagerImpl_New : public PMDataManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000379
380public:
381
382 /// add - Add a pass to the queue of passes to run. This passes ownership of
383 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
384 /// will be destroyed as well, so there is no need to delete the pass. This
385 /// implies that all passes MUST be allocated with 'new'.
386 void add(Pass *P);
387
388 /// run - Execute all of the passes scheduled for execution. Keep track of
389 /// whether any of the passes modifies the module, and if so, return true.
390 bool run(Module &M);
391
Devang Patelebba9702006-11-13 22:40:09 +0000392 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000393 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000394
Devang Patelf9d96b92006-12-07 19:57:52 +0000395 /// Pass Manager itself does not invalidate any analysis info.
396 void getAnalysisUsage(AnalysisUsage &Info) const {
397 Info.setPreservesAll();
398 }
399
Devang Patel376fefa2006-11-08 10:29:57 +0000400private:
401
402 /// Add a pass into a passmanager queue. This is used by schedulePasses
403 bool addPass(Pass *p);
404
Devang Patel1a6eaa42006-11-11 02:22:31 +0000405 /// Schedule pass P for execution. Make sure that passes required by
406 /// P are run before P is run. Update analysis info maintained by
407 /// the manager. Remove dead passes. This is a recursive function.
408 void schedulePass(Pass *P);
409
Devang Patel376fefa2006-11-08 10:29:57 +0000410 /// Schedule all passes collected in pass queue using add(). Add all the
411 /// schedule passes into various manager's queue using addPass().
412 void schedulePasses();
413
414 // Collection of pass managers
415 std::vector<ModulePassManager_New *> PassManagers;
416
Devang Patel376fefa2006-11-08 10:29:57 +0000417 // Active Pass Manager
418 ModulePassManager_New *activeManager;
419};
420
Devang Patelca58e352006-11-08 10:05:38 +0000421} // End of llvm namespace
422
Devang Patela1514cb2006-12-07 19:39:39 +0000423//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000424// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000425
Devang Pateld65e9e92006-11-08 01:31:28 +0000426/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000427/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000428bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000429
Devang Patel8f677ce2006-12-07 18:47:25 +0000430 // TODO
431 // If this pass is not preserving information that is required by a
432 // pass maintained by higher level pass manager then do not insert
433 // this pass into current manager. Use new manager. For example,
434 // For example, If FunctionPass F is not preserving ModulePass Info M1
435 // that is used by another ModulePass M2 then do not insert F in
436 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000437 return true;
438}
439
Devang Patel643676c2006-11-11 01:10:19 +0000440/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000441void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000442
Devang Patel643676c2006-11-11 01:10:19 +0000443 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000444 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000445
Devang Patele9976aa2006-12-07 19:33:53 +0000446 //This pass is the current implementation of all of the interfaces it
447 //implements as well.
448 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
449 for (unsigned i = 0, e = II.size(); i != e; ++i)
450 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000451 }
452}
453
Devang Patelf68a3492006-11-07 22:35:17 +0000454/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000455void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000456 AnalysisUsage AnUsage;
457 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000458
Devang Patel2e169c32006-12-07 20:03:49 +0000459 if (AnUsage.getPreservesAll())
460 return;
461
462 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000463 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000464 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000465 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000466 PreservedSet.end()) {
467 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000468 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000469 AvailableAnalysis.erase(J);
470 }
471 }
Devang Patelf68a3492006-11-07 22:35:17 +0000472}
473
Devang Patelca189262006-11-14 03:05:08 +0000474/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000475void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patelca189262006-11-14 03:05:08 +0000476
477 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
478 E = LastUser.end(); I !=E; ++I) {
479 if (I->second == P) {
480 Pass *deadPass = I->first;
481 deadPass->releaseMemory();
482
483 std::map<AnalysisID, Pass*>::iterator Pos =
484 AvailableAnalysis.find(deadPass->getPassInfo());
485
486 assert (Pos != AvailableAnalysis.end() &&
487 "Pass is not available");
488 AvailableAnalysis.erase(Pos);
489 }
490 }
491}
492
Devang Patel8f677ce2006-12-07 18:47:25 +0000493/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000494/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000495void PMDataManager::addPassToManager(Pass *P,
496 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000497
Devang Patel90b05e02006-11-11 02:04:19 +0000498 if (ProcessAnalysis) {
499 // Take a note of analysis required and made available by this pass
Devang Patel8f677ce2006-12-07 18:47:25 +0000500 initializeAnalysisImpl(P);
Devang Patele9976aa2006-12-07 19:33:53 +0000501 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000502
503 // Remove the analysis not preserved by this pass
504 removeNotPreservedAnalysis(P);
505 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000506
507 // Add pass
508 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000509}
510
Devang Patel07f4f582006-11-14 21:49:36 +0000511// All Required analyses should be available to the pass as it runs! Here
512// we fill in the AnalysisImpls member of the pass so that it can
513// successfully use the getAnalysis() method to retrieve the
514// implementations it needs.
515//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000516void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000517 AnalysisUsage AnUsage;
518 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000519
520 for (std::vector<const PassInfo *>::const_iterator
521 I = AnUsage.getRequiredSet().begin(),
522 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
523 Pass *Impl = getAnalysisPass(*I);
524 if (Impl == 0)
525 assert(0 && "Analysis used but not available!");
526 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
527 }
528}
529
Devang Patela1514cb2006-12-07 19:39:39 +0000530//===----------------------------------------------------------------------===//
531// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000532
Devang Pateld65e9e92006-11-08 01:31:28 +0000533/// Add pass P into PassVector and return true. If this pass is not
534/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000535bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000536BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000537
538 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
539 if (!BP)
540 return false;
541
Devang Patel3c8eb622006-11-07 22:56:50 +0000542 // If this pass does not preserve anlysis that is used by other passes
543 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000544 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000545 return false;
546
Devang Patel8cad70d2006-11-11 01:51:02 +0000547 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000548
Devang Patel6e5a1132006-11-07 21:31:57 +0000549 return true;
550}
551
552/// Execute all of the passes scheduled for execution by invoking
553/// runOnBasicBlock method. Keep track of whether any of the passes modifies
554/// the function, and if so, return true.
555bool
556BasicBlockPassManager_New::runOnFunction(Function &F) {
557
558 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000559 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000560
Devang Patel6e5a1132006-11-07 21:31:57 +0000561 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000562 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
563 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000564 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000565
Devang Patele9976aa2006-12-07 19:33:53 +0000566 recordAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000567 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
568 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000569 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000570 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000571 }
572 return Changed;
573}
574
Devang Patelebba9702006-11-13 22:40:09 +0000575/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000576Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
577 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000578}
579
Devang Patela1514cb2006-12-07 19:39:39 +0000580//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000581// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000582
Devang Patel4e12f862006-11-08 10:44:40 +0000583/// Create new Function pass manager
584FunctionPassManager_New::FunctionPassManager_New() {
585 FPM = new FunctionPassManagerImpl_New();
586}
587
588/// add - Add a pass to the queue of passes to run. This passes
589/// ownership of the Pass to the PassManager. When the
590/// PassManager_X is destroyed, the pass will be destroyed as well, so
591/// there is no need to delete the pass. (TODO delete passes.)
592/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000593void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000594 FPM->add(P);
595}
596
597/// Execute all of the passes scheduled for execution. Keep
598/// track of whether any of the passes modifies the function, and if
599/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000600bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000601 return FPM->runOnModule(M);
602}
603
Devang Patel9f3083e2006-11-15 19:39:54 +0000604/// run - Execute all of the passes scheduled for execution. Keep
605/// track of whether any of the passes modifies the function, and if
606/// so, return true.
607///
608bool FunctionPassManager_New::run(Function &F) {
609 std::string errstr;
610 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000611 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000612 abort();
613 }
614 return FPM->runOnFunction(F);
615}
616
617
Devang Patelff631ae2006-11-15 01:27:05 +0000618/// doInitialization - Run all of the initializers for the function passes.
619///
620bool FunctionPassManager_New::doInitialization() {
621 return FPM->doInitialization(*MP->getModule());
622}
623
624/// doFinalization - Run all of the initializers for the function passes.
625///
626bool FunctionPassManager_New::doFinalization() {
627 return FPM->doFinalization(*MP->getModule());
628}
629
Devang Patela1514cb2006-12-07 19:39:39 +0000630//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000631// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000632
Devang Patel0c2012f2006-11-07 21:49:50 +0000633/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
634/// either use it into active basic block pass manager or create new basic
635/// block pass manager to handle pass P.
636bool
Devang Patel4e12f862006-11-08 10:44:40 +0000637FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000638
639 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
640 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
641
642 if (!activeBBPassManager
643 || !activeBBPassManager->addPass(BP)) {
644
645 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000646 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000647 if (!activeBBPassManager->addPass(BP))
648 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000649 }
650 return true;
651 }
652
653 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
654 if (!FP)
655 return false;
656
Devang Patel3c8eb622006-11-07 22:56:50 +0000657 // If this pass does not preserve anlysis that is used by other passes
658 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000659 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000660 return false;
661
Devang Patel8cad70d2006-11-11 01:51:02 +0000662 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000663 activeBBPassManager = NULL;
664 return true;
665}
666
667/// Execute all of the passes scheduled for execution by invoking
668/// runOnFunction method. Keep track of whether any of the passes modifies
669/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000670bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000671
672 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000673 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000674
Devang Patel0c2012f2006-11-07 21:49:50 +0000675 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000676 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
677 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000678 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000679
Devang Patele9976aa2006-12-07 19:33:53 +0000680 recordAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000681 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
682 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000683 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000684 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000685 }
686 return Changed;
687}
688
Devang Patel9f3083e2006-11-15 19:39:54 +0000689/// Execute all of the passes scheduled for execution by invoking
690/// runOnFunction method. Keep track of whether any of the passes modifies
691/// the function, and if so, return true.
692bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
693
694 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000695 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000696
697 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
698 e = passVectorEnd(); itr != e; ++itr) {
699 Pass *P = *itr;
700
Devang Patele9976aa2006-12-07 19:33:53 +0000701 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000702 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
703 Changed |= FP->runOnFunction(F);
704 removeNotPreservedAnalysis(P);
705 removeDeadPasses(P);
706 }
707 return Changed;
708}
709
710
Devang Patelebba9702006-11-13 22:40:09 +0000711/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000712Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000713
Devang Patel3f0832a2006-11-14 02:54:23 +0000714 Pass *P = getAnalysisPass(AID);
715 if (P)
716 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000717
718 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000719 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000720 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000721
722 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000723 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000724}
Devang Patel0c2012f2006-11-07 21:49:50 +0000725
Devang Patelff631ae2006-11-15 01:27:05 +0000726inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
727 bool Changed = false;
728
729 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
730 e = passVectorEnd(); itr != e; ++itr) {
731 Pass *P = *itr;
732
733 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
734 Changed |= FP->doInitialization(M);
735 }
736
737 return Changed;
738}
739
740inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
741 bool Changed = false;
742
743 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
744 e = passVectorEnd(); itr != e; ++itr) {
745 Pass *P = *itr;
746
747 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
748 Changed |= FP->doFinalization(M);
749 }
750
751
752 return Changed;
753}
754
Devang Patela1514cb2006-12-07 19:39:39 +0000755//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000756// ModulePassManager implementation
757
758/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000759/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000760/// is not manageable by this manager.
761bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000762ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000763
764 // If P is FunctionPass then use function pass maanager.
765 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
766
767 activeFunctionPassManager = NULL;
768
769 if (!activeFunctionPassManager
770 || !activeFunctionPassManager->addPass(P)) {
771
Devang Patel4e12f862006-11-08 10:44:40 +0000772 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000773 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000774 if (!activeFunctionPassManager->addPass(FP))
775 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000776 }
777 return true;
778 }
779
780 ModulePass *MP = dynamic_cast<ModulePass *>(P);
781 if (!MP)
782 return false;
783
Devang Patel3c8eb622006-11-07 22:56:50 +0000784 // If this pass does not preserve anlysis that is used by other passes
785 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000786 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000787 return false;
788
Devang Patel8cad70d2006-11-11 01:51:02 +0000789 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000790 activeFunctionPassManager = NULL;
791 return true;
792}
793
794
795/// Execute all of the passes scheduled for execution by invoking
796/// runOnModule method. Keep track of whether any of the passes modifies
797/// the module, and if so, return true.
798bool
799ModulePassManager_New::runOnModule(Module &M) {
800 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000801 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000802
Devang Patel8cad70d2006-11-11 01:51:02 +0000803 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
804 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000805 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000806
Devang Patele9976aa2006-12-07 19:33:53 +0000807 recordAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000808 ModulePass *MP = dynamic_cast<ModulePass*>(P);
809 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000810 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000811 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000812 }
813 return Changed;
814}
815
Devang Patelebba9702006-11-13 22:40:09 +0000816/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000817Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000818
Devang Patel3f0832a2006-11-14 02:54:23 +0000819
820 Pass *P = getAnalysisPass(AID);
821 if (P)
822 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000823
824 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000825 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000826 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000827
828 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000829 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000830}
831
Devang Patela1514cb2006-12-07 19:39:39 +0000832//===----------------------------------------------------------------------===//
833// PassManagerImpl implementation
834
Devang Patelebba9702006-11-13 22:40:09 +0000835/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000836Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000837
Devang Patel3f0832a2006-11-14 02:54:23 +0000838 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000839 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000840 e = PassManagers.end(); !P && itr != e; ++itr)
841 P = (*itr)->getAnalysisPassFromManager(AID);
842 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000843}
844
Devang Patel1a6eaa42006-11-11 02:22:31 +0000845/// Schedule pass P for execution. Make sure that passes required by
846/// P are run before P is run. Update analysis info maintained by
847/// the manager. Remove dead passes. This is a recursive function.
848void PassManagerImpl_New::schedulePass(Pass *P) {
849
850 AnalysisUsage AnUsage;
851 P->getAnalysisUsage(AnUsage);
852 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
853 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
854 E = RequiredSet.end(); I != E; ++I) {
855
Devang Patel3f0832a2006-11-14 02:54:23 +0000856 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
857 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000858 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000859 AnalysisPass = (*I)->createPass();
860 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000861 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000862 setLastUser (AnalysisPass, P);
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000863
864 // Prolong live range of analyses that are needed after an analysis pass
865 // is destroyed, for querying by subsequent passes
866 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
867 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
868 E = IDs.end(); I != E; ++I) {
869 Pass *AP = getAnalysisPassFromManager(*I);
870 assert (AP && "Analysis pass is not available");
871 setLastUser(AP, P);
872 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000873 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000874 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000875}
876
Devang Patelc290c8a2006-11-07 22:23:34 +0000877/// Schedule all passes from the queue by adding them in their
878/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000879void PassManagerImpl_New::schedulePasses() {
880 for (std::vector<Pass *>::iterator I = passVectorBegin(),
881 E = passVectorEnd(); I != E; ++I)
882 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000883}
884
885/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000886void PassManagerImpl_New::add(Pass *P) {
887 // Do not process Analysis now. Analysis is process while scheduling
888 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000889 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000890}
891
892// PassManager_New implementation
893/// Add P into active pass manager or use new module pass manager to
894/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000895bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000896
Devang Patel6c9f5482006-11-11 00:42:16 +0000897 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000898 activeManager = new ModulePassManager_New();
899 PassManagers.push_back(activeManager);
900 }
901
902 return activeManager->addPass(P);
903}
904
905/// run - Execute all of the passes scheduled for execution. Keep track of
906/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000907bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000908
909 schedulePasses();
910 bool Changed = false;
911 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
912 e = PassManagers.end(); itr != e; ++itr) {
913 ModulePassManager_New *pm = *itr;
914 Changed |= pm->runOnModule(M);
915 }
916 return Changed;
917}
Devang Patel376fefa2006-11-08 10:29:57 +0000918
Devang Patela1514cb2006-12-07 19:39:39 +0000919//===----------------------------------------------------------------------===//
920// PassManager implementation
921
Devang Patel376fefa2006-11-08 10:29:57 +0000922/// Create new pass manager
923PassManager_New::PassManager_New() {
924 PM = new PassManagerImpl_New();
925}
926
927/// add - Add a pass to the queue of passes to run. This passes ownership of
928/// the Pass to the PassManager. When the PassManager is destroyed, the pass
929/// will be destroyed as well, so there is no need to delete the pass. This
930/// implies that all passes MUST be allocated with 'new'.
931void
932PassManager_New::add(Pass *P) {
933 PM->add(P);
934}
935
936/// run - Execute all of the passes scheduled for execution. Keep track of
937/// whether any of the passes modifies the module, and if so, return true.
938bool
939PassManager_New::run(Module &M) {
940 return PM->run(M);
941}
942