blob: 8cf4a159642ee1561e47dcf2aff599a0751e1b17 [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 Patel4c36e6b2006-12-07 23:24:58 +0000212 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000213 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 Patelbc03f132006-12-07 23:55:10 +0000246 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000247 AvailableAnalysis.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 Patel1d6267c2006-12-07 23:05:44 +0000256 /// Populate RequiredPasses with the analysis pass that are required by
257 /// pass P.
258 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
259 Pass *P);
260
261 /// All Required analyses should be available to the pass as it runs! Here
262 /// we fill in the AnalysisImpls member of the pass so that it can
263 /// successfully use the getAnalysis() method to retrieve the
264 /// implementations it needs.
265 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000266
Devang Patel8cad70d2006-11-11 01:51:02 +0000267 inline std::vector<Pass *>::iterator passVectorBegin() {
268 return PassVector.begin();
269 }
270
271 inline std::vector<Pass *>::iterator passVectorEnd() {
272 return PassVector.end();
273 }
274
Devang Patelf3827bc2006-12-07 19:54:15 +0000275 // Access toplevel manager
276 PMTopLevelManager *getTopLevelManager() { return TPM; }
277 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
278
Devang Patel4c36e6b2006-12-07 23:24:58 +0000279 unsigned getDepth() { return Depth; }
280
Devang Patelbc03f132006-12-07 23:55:10 +0000281protected:
282
283 // Collection of pass whose last user asked this manager to claim
284 // last use. If a FunctionPass F is the last user of ModulePass info M
285 // then the F's manager, not F, records itself as a last user of M.
286 std::vector<Pass *> ForcedLastUses;
287
288 // Top level manager.
289 // TODO : Make it a reference.
290 PMTopLevelManager *TPM;
291
Devang Patela9844592006-11-11 01:31:05 +0000292private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000293 // Set of available Analysis. This information is used while scheduling
294 // pass. If a pass requires an analysis which is not not available then
295 // equired analysis pass is scheduled to run before the pass itself is
296 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000297 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000298
299 // Collection of pass that are managed by this manager
300 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000301
Devang Patel4c36e6b2006-12-07 23:24:58 +0000302 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000303};
304
Devang Patelca58e352006-11-08 10:05:38 +0000305/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
306/// pass together and sequence them to process one basic block before
307/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000308class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000309 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000310
311public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000312 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000313
314 /// Add a pass into a passmanager queue.
315 bool addPass(Pass *p);
316
317 /// Execute all of the passes scheduled for execution. Keep track of
318 /// whether any of the passes modifies the function, and if so, return true.
319 bool runOnFunction(Function &F);
320
Devang Patelebba9702006-11-13 22:40:09 +0000321 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000322 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000323 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000324
Devang Patelf9d96b92006-12-07 19:57:52 +0000325 /// Pass Manager itself does not invalidate any analysis info.
326 void getAnalysisUsage(AnalysisUsage &Info) const {
327 Info.setPreservesAll();
328 }
329
Devang Patelca58e352006-11-08 10:05:38 +0000330private:
Devang Patelca58e352006-11-08 10:05:38 +0000331};
332
Devang Patel4e12f862006-11-08 10:44:40 +0000333/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000334/// It batches all function passes and basic block pass managers together and
335/// sequence them to process one function at a time before processing next
336/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000337class FunctionPassManagerImpl_New : public ModulePass,
338 public PMDataManager,
339 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000340public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000341 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
342 PMDataManager(D) { /* TODO */ }
343 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000344 activeBBPassManager = NULL;
345 }
Devang Patel4e12f862006-11-08 10:44:40 +0000346 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000347
Devang Patelabcd1d32006-12-07 21:27:23 +0000348 inline void addTopLevelPass(Pass *P) {
349 addPass(P);
350 }
351
Devang Patelca58e352006-11-08 10:05:38 +0000352 /// add - Add a pass to the queue of passes to run. This passes
353 /// ownership of the Pass to the PassManager. When the
354 /// PassManager_X is destroyed, the pass will be destroyed as well, so
355 /// there is no need to delete the pass. (TODO delete passes.)
356 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000357 void add(Pass *P) {
358 schedulePass(P, this);
359 }
Devang Patelca58e352006-11-08 10:05:38 +0000360
361 /// Add pass into the pass manager queue.
362 bool addPass(Pass *P);
363
364 /// Execute all of the passes scheduled for execution. Keep
365 /// track of whether any of the passes modifies the function, and if
366 /// so, return true.
367 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000368 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000369
Devang Patelebba9702006-11-13 22:40:09 +0000370 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000371 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000372 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000373
Devang Patelff631ae2006-11-15 01:27:05 +0000374 /// doInitialization - Run all of the initializers for the function passes.
375 ///
376 bool doInitialization(Module &M);
377
378 /// doFinalization - Run all of the initializers for the function passes.
379 ///
380 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000381
382 /// Pass Manager itself does not invalidate any analysis info.
383 void getAnalysisUsage(AnalysisUsage &Info) const {
384 Info.setPreservesAll();
385 }
386
Devang Patelca58e352006-11-08 10:05:38 +0000387private:
Devang Patelca58e352006-11-08 10:05:38 +0000388 // Active Pass Managers
389 BasicBlockPassManager_New *activeBBPassManager;
390};
391
392/// ModulePassManager_New manages ModulePasses and function pass managers.
393/// It batches all Module passes passes and function pass managers together and
394/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000395class ModulePassManager_New : public Pass,
396 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000397
398public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000399 ModulePassManager_New(int D) : PMDataManager(D) {
400 activeFunctionPassManager = NULL;
401 }
Devang Patelca58e352006-11-08 10:05:38 +0000402
403 /// Add a pass into a passmanager queue.
404 bool addPass(Pass *p);
405
406 /// run - Execute all of the passes scheduled for execution. Keep track of
407 /// whether any of the passes modifies the module, and if so, return true.
408 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000409
410 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000411 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000412 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelf9d96b92006-12-07 19:57:52 +0000413
414 /// Pass Manager itself does not invalidate any analysis info.
415 void getAnalysisUsage(AnalysisUsage &Info) const {
416 Info.setPreservesAll();
417 }
418
Devang Patelca58e352006-11-08 10:05:38 +0000419private:
Devang Patelca58e352006-11-08 10:05:38 +0000420 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000421 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000422};
423
Devang Patel376fefa2006-11-08 10:29:57 +0000424/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000425class PassManagerImpl_New : public Pass,
426 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000427 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000428
429public:
430
Devang Patel4c36e6b2006-12-07 23:24:58 +0000431 PassManagerImpl_New(int D) : PMDataManager(D) {}
432
Devang Patel376fefa2006-11-08 10:29:57 +0000433 /// add - Add a pass to the queue of passes to run. This passes ownership of
434 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
435 /// will be destroyed as well, so there is no need to delete the pass. This
436 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000437 void add(Pass *P) {
438 schedulePass(P, this);
439 }
Devang Patel376fefa2006-11-08 10:29:57 +0000440
441 /// run - Execute all of the passes scheduled for execution. Keep track of
442 /// whether any of the passes modifies the module, and if so, return true.
443 bool run(Module &M);
444
Devang Patelebba9702006-11-13 22:40:09 +0000445 /// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000446 /// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000447 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000448
Devang Patelf9d96b92006-12-07 19:57:52 +0000449 /// Pass Manager itself does not invalidate any analysis info.
450 void getAnalysisUsage(AnalysisUsage &Info) const {
451 Info.setPreservesAll();
452 }
453
Devang Patelabcd1d32006-12-07 21:27:23 +0000454 inline void addTopLevelPass(Pass *P) {
455 addPass(P);
456 }
457
Devang Patel376fefa2006-11-08 10:29:57 +0000458private:
459
Devang Patelde124182006-12-07 21:10:57 +0000460 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000461 bool addPass(Pass *p);
462
Devang Patel376fefa2006-11-08 10:29:57 +0000463 // Collection of pass managers
464 std::vector<ModulePassManager_New *> PassManagers;
465
Devang Patel376fefa2006-11-08 10:29:57 +0000466 // Active Pass Manager
467 ModulePassManager_New *activeManager;
468};
469
Devang Patelca58e352006-11-08 10:05:38 +0000470} // End of llvm namespace
471
Devang Patela1514cb2006-12-07 19:39:39 +0000472//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000473// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000474
Devang Pateld65e9e92006-11-08 01:31:28 +0000475/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000476/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000477bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000478
Devang Patel8f677ce2006-12-07 18:47:25 +0000479 // TODO
480 // If this pass is not preserving information that is required by a
481 // pass maintained by higher level pass manager then do not insert
482 // this pass into current manager. Use new manager. For example,
483 // For example, If FunctionPass F is not preserving ModulePass Info M1
484 // that is used by another ModulePass M2 then do not insert F in
485 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000486 return true;
487}
488
Devang Patel643676c2006-11-11 01:10:19 +0000489/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000490void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000491
Devang Patel643676c2006-11-11 01:10:19 +0000492 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000493 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000494
Devang Patele9976aa2006-12-07 19:33:53 +0000495 //This pass is the current implementation of all of the interfaces it
496 //implements as well.
497 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
498 for (unsigned i = 0, e = II.size(); i != e; ++i)
499 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000500 }
501}
502
Devang Patelf68a3492006-11-07 22:35:17 +0000503/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000504void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000505 AnalysisUsage AnUsage;
506 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000507
Devang Patel2e169c32006-12-07 20:03:49 +0000508 if (AnUsage.getPreservesAll())
509 return;
510
511 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000512 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000513 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000514 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000515 PreservedSet.end()) {
516 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000517 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000518 AvailableAnalysis.erase(J);
519 }
520 }
Devang Patelf68a3492006-11-07 22:35:17 +0000521}
522
Devang Patelca189262006-11-14 03:05:08 +0000523/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000524void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000525
526 std::vector<Pass *> DeadPasses;
527 TPM->collectLastUses(DeadPasses, P);
528
529 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
530 E = DeadPasses.end(); I != E; ++I) {
531 (*I)->releaseMemory();
532
533 std::map<AnalysisID, Pass*>::iterator Pos =
534 AvailableAnalysis.find((*I)->getPassInfo());
535
536 // It is possible that deadPass is already removed from the AvailableAnalysis
537 if (Pos != AvailableAnalysis.end())
538 AvailableAnalysis.erase(Pos);
539 }
Devang Patelca189262006-11-14 03:05:08 +0000540}
541
Devang Patel8f677ce2006-12-07 18:47:25 +0000542/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000543/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000544void PMDataManager::addPassToManager(Pass *P,
545 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000546
Devang Patel90b05e02006-11-11 02:04:19 +0000547 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000548
549 // At the moment, this pass is the last user of all required passes.
550 std::vector<Pass *> LastUses;
551 std::vector<Pass *> RequiredPasses;
552 unsigned PDepth = this->getDepth();
553
554 collectRequiredAnalysisPasses(RequiredPasses, P);
555 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
556 E = RequiredPasses.end(); I != E; ++I) {
557 Pass *PRequired = *I;
558 unsigned RDepth = 0;
559 //FIXME: RDepth = PRequired->getResolver()->getDepth();
560 if (PDepth == RDepth)
561 LastUses.push_back(PRequired);
562 else if (PDepth > RDepth) {
563 // Let the parent claim responsibility of last use
564 ForcedLastUses.push_back(PRequired);
565 } else {
566 // Note : This feature is not yet implemented
567 assert (0 &&
568 "Unable to handle Pass that requires lower level Analysis pass");
569 }
570 }
571
572 if (!LastUses.empty())
573 TPM->setLastUser(LastUses, P);
574
Devang Patel17bff0d2006-12-07 22:09:36 +0000575 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000576 // Remove the analysis not preserved by this pass
Devang Patel17bff0d2006-12-07 22:09:36 +0000577 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000578 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000579 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000580 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000581
582 // Add pass
583 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000584}
585
Devang Patel1d6267c2006-12-07 23:05:44 +0000586/// Populate RequiredPasses with the analysis pass that are required by
587/// pass P.
588void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
589 Pass *P) {
590 AnalysisUsage AnUsage;
591 P->getAnalysisUsage(AnUsage);
592 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
593 for (std::vector<AnalysisID>::const_iterator
594 I = RequiredSet.begin(), E = RequiredSet.end();
595 I != E; ++I) {
596 Pass *AnalysisPass = NULL; //FIXME findAnalysisPass(*I,true);
597 assert (AnalysisPass && "Analysis pass is not available");
598 RP.push_back(AnalysisPass);
599 }
600}
601
Devang Patel07f4f582006-11-14 21:49:36 +0000602// All Required analyses should be available to the pass as it runs! Here
603// we fill in the AnalysisImpls member of the pass so that it can
604// successfully use the getAnalysis() method to retrieve the
605// implementations it needs.
606//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000607void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000608 AnalysisUsage AnUsage;
609 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000610
611 for (std::vector<const PassInfo *>::const_iterator
612 I = AnUsage.getRequiredSet().begin(),
613 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
614 Pass *Impl = getAnalysisPass(*I);
615 if (Impl == 0)
616 assert(0 && "Analysis used but not available!");
617 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
618 }
619}
620
Devang Patela1514cb2006-12-07 19:39:39 +0000621//===----------------------------------------------------------------------===//
622// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000623
Devang Pateld65e9e92006-11-08 01:31:28 +0000624/// Add pass P into PassVector and return true. If this pass is not
625/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000626bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000627BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000628
629 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
630 if (!BP)
631 return false;
632
Devang Patel3c8eb622006-11-07 22:56:50 +0000633 // If this pass does not preserve anlysis that is used by other passes
634 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000635 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000636 return false;
637
Devang Patel8cad70d2006-11-11 01:51:02 +0000638 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000639
Devang Patel6e5a1132006-11-07 21:31:57 +0000640 return true;
641}
642
643/// Execute all of the passes scheduled for execution by invoking
644/// runOnBasicBlock method. Keep track of whether any of the passes modifies
645/// the function, and if so, return true.
646bool
647BasicBlockPassManager_New::runOnFunction(Function &F) {
648
649 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000650 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000651
Devang Patel6e5a1132006-11-07 21:31:57 +0000652 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000653 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
654 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000655 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000656
Devang Patel6e5a1132006-11-07 21:31:57 +0000657 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
658 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000659 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000660 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000661 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000662 }
663 return Changed;
664}
665
Devang Patelebba9702006-11-13 22:40:09 +0000666/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000667/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000668Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
669 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000670}
671
Devang Patela1514cb2006-12-07 19:39:39 +0000672//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000673// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000674
Devang Patel4e12f862006-11-08 10:44:40 +0000675/// Create new Function pass manager
676FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000677 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000678}
679
680/// add - Add a pass to the queue of passes to run. This passes
681/// ownership of the Pass to the PassManager. When the
682/// PassManager_X is destroyed, the pass will be destroyed as well, so
683/// there is no need to delete the pass. (TODO delete passes.)
684/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000685void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000686 FPM->add(P);
687}
688
689/// Execute all of the passes scheduled for execution. Keep
690/// track of whether any of the passes modifies the function, and if
691/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000692bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000693 return FPM->runOnModule(M);
694}
695
Devang Patel9f3083e2006-11-15 19:39:54 +0000696/// run - Execute all of the passes scheduled for execution. Keep
697/// track of whether any of the passes modifies the function, and if
698/// so, return true.
699///
700bool FunctionPassManager_New::run(Function &F) {
701 std::string errstr;
702 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000703 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000704 abort();
705 }
706 return FPM->runOnFunction(F);
707}
708
709
Devang Patelff631ae2006-11-15 01:27:05 +0000710/// doInitialization - Run all of the initializers for the function passes.
711///
712bool FunctionPassManager_New::doInitialization() {
713 return FPM->doInitialization(*MP->getModule());
714}
715
716/// doFinalization - Run all of the initializers for the function passes.
717///
718bool FunctionPassManager_New::doFinalization() {
719 return FPM->doFinalization(*MP->getModule());
720}
721
Devang Patela1514cb2006-12-07 19:39:39 +0000722//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000723// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000724
Devang Patel0c2012f2006-11-07 21:49:50 +0000725/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
726/// either use it into active basic block pass manager or create new basic
727/// block pass manager to handle pass P.
728bool
Devang Patel4e12f862006-11-08 10:44:40 +0000729FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000730
731 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
732 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
733
Devang Patel4949fe02006-12-07 22:34:21 +0000734 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000735
Devang Patel4949fe02006-12-07 22:34:21 +0000736 // If active manager exists then clear its analysis info.
737 if (activeBBPassManager)
738 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000739
Devang Patel4949fe02006-12-07 22:34:21 +0000740 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000741 activeBBPassManager =
742 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000743 addPassToManager(activeBBPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000744
745 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000746 if (!activeBBPassManager->addPass(BP))
747 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000748 }
Devang Patelbc03f132006-12-07 23:55:10 +0000749
750 if (!ForcedLastUses.empty())
751 TPM->setLastUser(ForcedLastUses, this);
752
Devang Patel0c2012f2006-11-07 21:49:50 +0000753 return true;
754 }
755
756 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
757 if (!FP)
758 return false;
759
Devang Patel3c8eb622006-11-07 22:56:50 +0000760 // If this pass does not preserve anlysis that is used by other passes
761 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000762 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000763 return false;
764
Devang Patel8cad70d2006-11-11 01:51:02 +0000765 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000766
767 // If active manager exists then clear its analysis info.
768 if (activeBBPassManager) {
769 activeBBPassManager->initializeAnalysisInfo();
770 activeBBPassManager = NULL;
771 }
772
Devang Patel0c2012f2006-11-07 21:49:50 +0000773 return true;
774}
775
776/// Execute all of the passes scheduled for execution by invoking
777/// runOnFunction method. Keep track of whether any of the passes modifies
778/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000779bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000780
781 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000782 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000783
Devang Patel0c2012f2006-11-07 21:49:50 +0000784 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000785 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
786 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000787 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000788
Devang Patel0c2012f2006-11-07 21:49:50 +0000789 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
790 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000791 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000792 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000793 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000794 }
795 return Changed;
796}
797
Devang Patel9f3083e2006-11-15 19:39:54 +0000798/// Execute all of the passes scheduled for execution by invoking
799/// runOnFunction method. Keep track of whether any of the passes modifies
800/// the function, and if so, return true.
801bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
802
803 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000804 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000805
806 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
807 e = passVectorEnd(); itr != e; ++itr) {
808 Pass *P = *itr;
809
Devang Patel9f3083e2006-11-15 19:39:54 +0000810 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
811 Changed |= FP->runOnFunction(F);
812 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000813 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000814 removeDeadPasses(P);
815 }
816 return Changed;
817}
818
819
Devang Patelebba9702006-11-13 22:40:09 +0000820/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000821/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000822Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000823
Devang Patel3f0832a2006-11-14 02:54:23 +0000824 Pass *P = getAnalysisPass(AID);
825 if (P)
826 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000827
828 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000829 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000830 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000831
832 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000833 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000834}
Devang Patel0c2012f2006-11-07 21:49:50 +0000835
Devang Patelff631ae2006-11-15 01:27:05 +0000836inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
837 bool Changed = false;
838
839 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
840 e = passVectorEnd(); itr != e; ++itr) {
841 Pass *P = *itr;
842
843 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
844 Changed |= FP->doInitialization(M);
845 }
846
847 return Changed;
848}
849
850inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
851 bool Changed = false;
852
853 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
854 e = passVectorEnd(); itr != e; ++itr) {
855 Pass *P = *itr;
856
857 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
858 Changed |= FP->doFinalization(M);
859 }
860
861
862 return Changed;
863}
864
Devang Patela1514cb2006-12-07 19:39:39 +0000865//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000866// ModulePassManager implementation
867
868/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000869/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000870/// is not manageable by this manager.
871bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000872ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000873
874 // If P is FunctionPass then use function pass maanager.
875 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
876
Devang Patel05e1a972006-11-07 22:03:15 +0000877 if (!activeFunctionPassManager
878 || !activeFunctionPassManager->addPass(P)) {
879
Devang Patel4949fe02006-12-07 22:34:21 +0000880 // If active manager exists then clear its analysis info.
881 if (activeFunctionPassManager)
882 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000883
Devang Patel4949fe02006-12-07 22:34:21 +0000884 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000885 activeFunctionPassManager =
886 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000887 addPassToManager(activeFunctionPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000888
889 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000890 if (!activeFunctionPassManager->addPass(FP))
891 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000892 }
Devang Patelbc03f132006-12-07 23:55:10 +0000893
894 if (!ForcedLastUses.empty())
895 TPM->setLastUser(ForcedLastUses, this);
896
Devang Patel05e1a972006-11-07 22:03:15 +0000897 return true;
898 }
899
900 ModulePass *MP = dynamic_cast<ModulePass *>(P);
901 if (!MP)
902 return false;
903
Devang Patel3c8eb622006-11-07 22:56:50 +0000904 // If this pass does not preserve anlysis that is used by other passes
905 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000906 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000907 return false;
908
Devang Patel8cad70d2006-11-11 01:51:02 +0000909 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +0000910 // If active manager exists then clear its analysis info.
911 if (activeFunctionPassManager) {
912 activeFunctionPassManager->initializeAnalysisInfo();
913 activeFunctionPassManager = NULL;
914 }
915
Devang Patel05e1a972006-11-07 22:03:15 +0000916 return true;
917}
918
919
920/// Execute all of the passes scheduled for execution by invoking
921/// runOnModule method. Keep track of whether any of the passes modifies
922/// the module, and if so, return true.
923bool
924ModulePassManager_New::runOnModule(Module &M) {
925 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000926 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000927
Devang Patel8cad70d2006-11-11 01:51:02 +0000928 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
929 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000930 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000931
Devang Patel05e1a972006-11-07 22:03:15 +0000932 ModulePass *MP = dynamic_cast<ModulePass*>(P);
933 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000934 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000935 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000936 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000937 }
938 return Changed;
939}
940
Devang Patelebba9702006-11-13 22:40:09 +0000941/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000942/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000943Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000944
Devang Patel3f0832a2006-11-14 02:54:23 +0000945 Pass *P = getAnalysisPass(AID);
946 if (P)
947 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000948
949 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000950 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000951 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000952
953 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000954 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000955}
956
Devang Patela1514cb2006-12-07 19:39:39 +0000957//===----------------------------------------------------------------------===//
958// PassManagerImpl implementation
959
Devang Patelebba9702006-11-13 22:40:09 +0000960/// Return true IFF AnalysisID AID is currently available.
Devang Patel642c1432006-12-07 21:58:50 +0000961/// TODO : Replace this method with getAnalysisPass()
Devang Patel3f0832a2006-11-14 02:54:23 +0000962Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000963
Devang Patel3f0832a2006-11-14 02:54:23 +0000964 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000965 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000966 e = PassManagers.end(); !P && itr != e; ++itr)
967 P = (*itr)->getAnalysisPassFromManager(AID);
968 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000969}
970
Devang Patelc290c8a2006-11-07 22:23:34 +0000971// PassManager_New implementation
972/// Add P into active pass manager or use new module pass manager to
973/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000974bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000975
Devang Patel6c9f5482006-11-11 00:42:16 +0000976 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000977 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patelc290c8a2006-11-07 22:23:34 +0000978 PassManagers.push_back(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +0000979 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +0000980 }
Devang Patel28bbcbe2006-12-07 21:44:12 +0000981 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +0000982}
983
984/// run - Execute all of the passes scheduled for execution. Keep track of
985/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000986bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000987
Devang Patelc290c8a2006-11-07 22:23:34 +0000988 bool Changed = false;
989 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
990 e = PassManagers.end(); itr != e; ++itr) {
991 ModulePassManager_New *pm = *itr;
992 Changed |= pm->runOnModule(M);
993 }
994 return Changed;
995}
Devang Patel376fefa2006-11-08 10:29:57 +0000996
Devang Patela1514cb2006-12-07 19:39:39 +0000997//===----------------------------------------------------------------------===//
998// PassManager implementation
999
Devang Patel376fefa2006-11-08 10:29:57 +00001000/// Create new pass manager
1001PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001002 PM = new PassManagerImpl_New(0);
Devang Patel376fefa2006-11-08 10:29:57 +00001003}
1004
1005/// add - Add a pass to the queue of passes to run. This passes ownership of
1006/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1007/// will be destroyed as well, so there is no need to delete the pass. This
1008/// implies that all passes MUST be allocated with 'new'.
1009void
1010PassManager_New::add(Pass *P) {
1011 PM->add(P);
1012}
1013
1014/// run - Execute all of the passes scheduled for execution. Keep track of
1015/// whether any of the passes modifies the module, and if so, return true.
1016bool
1017PassManager_New::run(Module &M) {
1018 return PM->run(M);
1019}
1020