blob: 3207ef2a3f5be4dd78691dd6476d172be470d3a4 [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.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000109 void schedulePass(Pass *P);
Devang Patelf33f3eb2006-12-07 19:21:29 +0000110
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
Devang Patel640c5bb2006-12-08 22:30:11 +0000121 /// Find the pass that implements Analysis AID. Search immutable
122 /// passes and all pass managers. If desired pass is not found
123 /// then return NULL.
124 Pass *findAnalysisPass(AnalysisID AID);
125
Devang Patelf33f3eb2006-12-07 19:21:29 +0000126 virtual ~PMTopLevelManager() {
127 PassManagers.clear();
128 }
129
Devang Patele0eb9d82006-12-07 20:51:18 +0000130 /// Add immutable pass and initialize it.
131 inline void addImmutablePass(ImmutablePass *P) {
132 P->initializePass();
133 ImmutablePasses.push_back(P);
134 }
135
136 inline std::vector<ImmutablePass *>& getImmutablePasses() {
137 return ImmutablePasses;
138 }
139
Devang Patel5bbeb492006-12-08 22:47:25 +0000140 void addPassManager(Pass *Manager) {
141 PassManagers.push_back(Manager);
142 }
143
Devang Patelf33f3eb2006-12-07 19:21:29 +0000144private:
145
146 /// Collection of pass managers
147 std::vector<Pass *> PassManagers;
148
149 // Map to keep track of last user of the analysis pass.
150 // LastUser->second is the last user of Lastuser->first.
151 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000152
153 /// Immutable passes are managed by top level manager.
154 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000155};
156
157/// Set pass P as the last user of the given analysis passes.
158void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
159 Pass *P) {
160
161 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
162 E = AnalysisPasses.end(); I != E; ++I) {
163 Pass *AP = *I;
164 LastUser[AP] = P;
165 // If AP is the last user of other passes then make P last user of
166 // such passes.
167 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
168 LUE = LastUser.end(); LUI != LUE; ++LUI) {
169 if (LUI->second == AP)
170 LastUser[LUI->first] = P;
171 }
172 }
173
174}
175
176/// Collect passes whose last user is P
177void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
178 Pass *P) {
179 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
180 LUE = LastUser.end(); LUI != LUE; ++LUI)
181 if (LUI->second == P)
182 LastUses.push_back(LUI->first);
183}
184
Devang Patelde124182006-12-07 21:10:57 +0000185/// Schedule pass P for execution. Make sure that passes required by
186/// P are run before P is run. Update analysis info maintained by
187/// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000188void PMTopLevelManager::schedulePass(Pass *P) {
Devang Patelde124182006-12-07 21:10:57 +0000189
190 // TODO : Allocate function manager for this pass, other wise required set
191 // may be inserted into previous function manager
192
193 AnalysisUsage AnUsage;
194 P->getAnalysisUsage(AnUsage);
195 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
196 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
197 E = RequiredSet.end(); I != E; ++I) {
198
Devang Patel640c5bb2006-12-08 22:30:11 +0000199 Pass *AnalysisPass = findAnalysisPass(*I);
Devang Patelde124182006-12-07 21:10:57 +0000200 if (!AnalysisPass) {
201 // Schedule this analysis run first.
202 AnalysisPass = (*I)->createPass();
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000203 schedulePass(AnalysisPass);
Devang Patelde124182006-12-07 21:10:57 +0000204 }
205 }
206
207 // Now all required passes are available.
208 addTopLevelPass(P);
209}
210
Devang Patel640c5bb2006-12-08 22:30:11 +0000211/// Find the pass that implements Analysis AID. Search immutable
212/// passes and all pass managers. If desired pass is not found
213/// then return NULL.
214Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
215
216 Pass *P = NULL;
217 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
218 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
219 const PassInfo *PI = (*I)->getPassInfo();
220 if (PI == AID)
221 P = *I;
222
223 // If Pass not found then check the interfaces implemented by Immutable Pass
224 if (!P) {
225 const std::vector<const PassInfo*> &ImmPI =
226 PI->getInterfacesImplemented();
227 for (unsigned Index = 0, End = ImmPI.size();
228 P == NULL && Index != End; ++Index)
229 if (ImmPI[Index] == AID)
230 P = *I;
231 }
232 }
233
234 if (P)
235 return P;
236
237 // Check pass managers;
238 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
239 E = PassManagers.end(); P == NULL && I != E; ++I)
240 P = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
241
242 return P;
243}
244
Devang Patelf3827bc2006-12-07 19:54:15 +0000245//===----------------------------------------------------------------------===//
246// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000247
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000248/// PMDataManager provides the common place to manage the analysis data
249/// used by pass managers.
250class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000251
252public:
253
Devang Patel4c36e6b2006-12-07 23:24:58 +0000254 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000255 initializeAnalysisInfo();
256 }
257
Devang Patela9844592006-11-11 01:31:05 +0000258 /// Return true IFF pass P's required analysis set does not required new
259 /// manager.
260 bool manageablePass(Pass *P);
261
Devang Patela9844592006-11-11 01:31:05 +0000262 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000263 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000264
Devang Patela9844592006-11-11 01:31:05 +0000265 /// Remove Analysis that is not preserved by the pass
266 void removeNotPreservedAnalysis(Pass *P);
267
268 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000269 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000270
Devang Patel8f677ce2006-12-07 18:47:25 +0000271 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000272 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
273 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000274
Devang Patel1d6267c2006-12-07 23:05:44 +0000275 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000276 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000277 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000278 AvailableAnalysis.clear();
Devang Patelb3900322006-12-07 21:02:08 +0000279
280 // Include immutable passes into AvailableAnalysis vector.
281 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
282 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
283 E = ImmutablePasses.end(); I != E; ++I)
284 recordAvailableAnalysis(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000285 }
286
Devang Patel1d6267c2006-12-07 23:05:44 +0000287 /// Populate RequiredPasses with the analysis pass that are required by
288 /// pass P.
289 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
290 Pass *P);
291
292 /// All Required analyses should be available to the pass as it runs! Here
293 /// we fill in the AnalysisImpls member of the pass so that it can
294 /// successfully use the getAnalysis() method to retrieve the
295 /// implementations it needs.
296 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000297
Devang Patel640c5bb2006-12-08 22:30:11 +0000298 /// Find the pass that implements Analysis AID. If desired pass is not found
299 /// then return NULL.
300 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
301
Devang Patel8cad70d2006-11-11 01:51:02 +0000302 inline std::vector<Pass *>::iterator passVectorBegin() {
303 return PassVector.begin();
304 }
305
306 inline std::vector<Pass *>::iterator passVectorEnd() {
307 return PassVector.end();
308 }
309
Devang Patelf3827bc2006-12-07 19:54:15 +0000310 // Access toplevel manager
311 PMTopLevelManager *getTopLevelManager() { return TPM; }
312 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
313
Devang Patel4c36e6b2006-12-07 23:24:58 +0000314 unsigned getDepth() { return Depth; }
315
Devang Patelbc03f132006-12-07 23:55:10 +0000316protected:
317
318 // Collection of pass whose last user asked this manager to claim
319 // last use. If a FunctionPass F is the last user of ModulePass info M
320 // then the F's manager, not F, records itself as a last user of M.
321 std::vector<Pass *> ForcedLastUses;
322
323 // Top level manager.
324 // TODO : Make it a reference.
325 PMTopLevelManager *TPM;
326
Devang Patela9844592006-11-11 01:31:05 +0000327private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000328 // Set of available Analysis. This information is used while scheduling
329 // pass. If a pass requires an analysis which is not not available then
330 // equired analysis pass is scheduled to run before the pass itself is
331 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000332 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000333
334 // Collection of pass that are managed by this manager
335 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000336
Devang Patel4c36e6b2006-12-07 23:24:58 +0000337 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000338};
339
Devang Patelca58e352006-11-08 10:05:38 +0000340/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
341/// pass together and sequence them to process one basic block before
342/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000343class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000344 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000345
346public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000347 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000348
349 /// Add a pass into a passmanager queue.
350 bool addPass(Pass *p);
351
352 /// Execute all of the passes scheduled for execution. Keep track of
353 /// whether any of the passes modifies the function, and if so, return true.
354 bool runOnFunction(Function &F);
355
Devang Patelf9d96b92006-12-07 19:57:52 +0000356 /// Pass Manager itself does not invalidate any analysis info.
357 void getAnalysisUsage(AnalysisUsage &Info) const {
358 Info.setPreservesAll();
359 }
360
Devang Patel475c4532006-12-08 00:59:05 +0000361 bool doInitialization(Module &M);
362 bool doInitialization(Function &F);
363 bool doFinalization(Module &M);
364 bool doFinalization(Function &F);
365
Devang Patelca58e352006-11-08 10:05:38 +0000366};
367
Devang Patel4e12f862006-11-08 10:44:40 +0000368/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000369/// It batches all function passes and basic block pass managers together and
370/// sequence them to process one function at a time before processing next
371/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000372class FunctionPassManagerImpl_New : public ModulePass,
373 public PMDataManager,
374 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000375public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000376 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
377 PMDataManager(D) { /* TODO */ }
378 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000379 activeBBPassManager = NULL;
380 }
Devang Patel4e12f862006-11-08 10:44:40 +0000381 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000382
Devang Patelabcd1d32006-12-07 21:27:23 +0000383 inline void addTopLevelPass(Pass *P) {
384 addPass(P);
385 }
386
Devang Patelca58e352006-11-08 10:05:38 +0000387 /// add - Add a pass to the queue of passes to run. This passes
388 /// ownership of the Pass to the PassManager. When the
389 /// PassManager_X is destroyed, the pass will be destroyed as well, so
390 /// there is no need to delete the pass. (TODO delete passes.)
391 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000392 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000393 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000394 }
Devang Patelca58e352006-11-08 10:05:38 +0000395
396 /// Add pass into the pass manager queue.
397 bool addPass(Pass *P);
398
399 /// Execute all of the passes scheduled for execution. Keep
400 /// track of whether any of the passes modifies the function, and if
401 /// so, return true.
402 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000403 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000404 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000405
Devang Patelff631ae2006-11-15 01:27:05 +0000406 /// doInitialization - Run all of the initializers for the function passes.
407 ///
408 bool doInitialization(Module &M);
409
410 /// doFinalization - Run all of the initializers for the function passes.
411 ///
412 bool doFinalization(Module &M);
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 Managers
421 BasicBlockPassManager_New *activeBBPassManager;
422};
423
424/// ModulePassManager_New manages ModulePasses and function pass managers.
425/// It batches all Module passes passes and function pass managers together and
426/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000427class ModulePassManager_New : public Pass,
428 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000429
430public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000431 ModulePassManager_New(int D) : PMDataManager(D) {
432 activeFunctionPassManager = NULL;
433 }
Devang Patelca58e352006-11-08 10:05:38 +0000434
435 /// Add a pass into a passmanager queue.
436 bool addPass(Pass *p);
437
438 /// run - Execute all of the passes scheduled for execution. Keep track of
439 /// whether any of the passes modifies the module, and if so, return true.
440 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000441
Devang Patelf9d96b92006-12-07 19:57:52 +0000442 /// Pass Manager itself does not invalidate any analysis info.
443 void getAnalysisUsage(AnalysisUsage &Info) const {
444 Info.setPreservesAll();
445 }
446
Devang Patelca58e352006-11-08 10:05:38 +0000447private:
Devang Patelca58e352006-11-08 10:05:38 +0000448 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000449 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000450};
451
Devang Patel376fefa2006-11-08 10:29:57 +0000452/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000453class PassManagerImpl_New : public Pass,
454 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000455 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000456
457public:
458
Devang Patel4c36e6b2006-12-07 23:24:58 +0000459 PassManagerImpl_New(int D) : PMDataManager(D) {}
460
Devang Patel376fefa2006-11-08 10:29:57 +0000461 /// add - Add a pass to the queue of passes to run. This passes ownership of
462 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
463 /// will be destroyed as well, so there is no need to delete the pass. This
464 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000465 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000466 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000467 }
Devang Patel376fefa2006-11-08 10:29:57 +0000468
469 /// run - Execute all of the passes scheduled for execution. Keep track of
470 /// whether any of the passes modifies the module, and if so, return true.
471 bool run(Module &M);
472
Devang Patelf9d96b92006-12-07 19:57:52 +0000473 /// Pass Manager itself does not invalidate any analysis info.
474 void getAnalysisUsage(AnalysisUsage &Info) const {
475 Info.setPreservesAll();
476 }
477
Devang Patelabcd1d32006-12-07 21:27:23 +0000478 inline void addTopLevelPass(Pass *P) {
479 addPass(P);
480 }
481
Devang Patel376fefa2006-11-08 10:29:57 +0000482private:
483
Devang Patelde124182006-12-07 21:10:57 +0000484 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000485 bool addPass(Pass *p);
486
Devang Patel376fefa2006-11-08 10:29:57 +0000487 // Active Pass Manager
488 ModulePassManager_New *activeManager;
489};
490
Devang Patelca58e352006-11-08 10:05:38 +0000491} // End of llvm namespace
492
Devang Patela1514cb2006-12-07 19:39:39 +0000493//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000494// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000495
Devang Pateld65e9e92006-11-08 01:31:28 +0000496/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000497/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000498bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000499
Devang Patel8f677ce2006-12-07 18:47:25 +0000500 // TODO
501 // If this pass is not preserving information that is required by a
502 // pass maintained by higher level pass manager then do not insert
503 // this pass into current manager. Use new manager. For example,
504 // For example, If FunctionPass F is not preserving ModulePass Info M1
505 // that is used by another ModulePass M2 then do not insert F in
506 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000507 return true;
508}
509
Devang Patel643676c2006-11-11 01:10:19 +0000510/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000511void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000512
Devang Patel643676c2006-11-11 01:10:19 +0000513 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000514 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000515
Devang Patele9976aa2006-12-07 19:33:53 +0000516 //This pass is the current implementation of all of the interfaces it
517 //implements as well.
518 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
519 for (unsigned i = 0, e = II.size(); i != e; ++i)
520 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000521 }
522}
523
Devang Patelf68a3492006-11-07 22:35:17 +0000524/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000525void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000526 AnalysisUsage AnUsage;
527 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000528
Devang Patel2e169c32006-12-07 20:03:49 +0000529 if (AnUsage.getPreservesAll())
530 return;
531
532 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000533 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000534 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000535 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000536 PreservedSet.end()) {
537 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000538 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000539 AvailableAnalysis.erase(J);
540 }
541 }
Devang Patelf68a3492006-11-07 22:35:17 +0000542}
543
Devang Patelca189262006-11-14 03:05:08 +0000544/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000545void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000546
547 std::vector<Pass *> DeadPasses;
548 TPM->collectLastUses(DeadPasses, P);
549
550 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
551 E = DeadPasses.end(); I != E; ++I) {
552 (*I)->releaseMemory();
553
554 std::map<AnalysisID, Pass*>::iterator Pos =
555 AvailableAnalysis.find((*I)->getPassInfo());
556
Devang Patel475c4532006-12-08 00:59:05 +0000557 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000558 if (Pos != AvailableAnalysis.end())
559 AvailableAnalysis.erase(Pos);
560 }
Devang Patelca189262006-11-14 03:05:08 +0000561}
562
Devang Patel8f677ce2006-12-07 18:47:25 +0000563/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000564/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000565void PMDataManager::addPassToManager(Pass *P,
566 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000567
Devang Patel90b05e02006-11-11 02:04:19 +0000568 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000569
570 // At the moment, this pass is the last user of all required passes.
571 std::vector<Pass *> LastUses;
572 std::vector<Pass *> RequiredPasses;
573 unsigned PDepth = this->getDepth();
574
575 collectRequiredAnalysisPasses(RequiredPasses, P);
576 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
577 E = RequiredPasses.end(); I != E; ++I) {
578 Pass *PRequired = *I;
579 unsigned RDepth = 0;
580 //FIXME: RDepth = PRequired->getResolver()->getDepth();
581 if (PDepth == RDepth)
582 LastUses.push_back(PRequired);
583 else if (PDepth > RDepth) {
584 // Let the parent claim responsibility of last use
585 ForcedLastUses.push_back(PRequired);
586 } else {
587 // Note : This feature is not yet implemented
588 assert (0 &&
589 "Unable to handle Pass that requires lower level Analysis pass");
590 }
591 }
592
593 if (!LastUses.empty())
594 TPM->setLastUser(LastUses, P);
595
Devang Patel17bff0d2006-12-07 22:09:36 +0000596 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000597 // Remove the analysis not preserved by this pass
Devang Patel17bff0d2006-12-07 22:09:36 +0000598 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000599 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000600 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000601 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000602
603 // Add pass
604 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000605}
606
Devang Patel1d6267c2006-12-07 23:05:44 +0000607/// Populate RequiredPasses with the analysis pass that are required by
608/// pass P.
609void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
610 Pass *P) {
611 AnalysisUsage AnUsage;
612 P->getAnalysisUsage(AnUsage);
613 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
614 for (std::vector<AnalysisID>::const_iterator
615 I = RequiredSet.begin(), E = RequiredSet.end();
616 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000617 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000618 assert (AnalysisPass && "Analysis pass is not available");
619 RP.push_back(AnalysisPass);
620 }
621}
622
Devang Patel07f4f582006-11-14 21:49:36 +0000623// All Required analyses should be available to the pass as it runs! Here
624// we fill in the AnalysisImpls member of the pass so that it can
625// successfully use the getAnalysis() method to retrieve the
626// implementations it needs.
627//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000628void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000629 AnalysisUsage AnUsage;
630 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000631
632 for (std::vector<const PassInfo *>::const_iterator
633 I = AnUsage.getRequiredSet().begin(),
634 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000635 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000636 if (Impl == 0)
637 assert(0 && "Analysis used but not available!");
638 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
639 }
640}
641
Devang Patel640c5bb2006-12-08 22:30:11 +0000642/// Find the pass that implements Analysis AID. If desired pass is not found
643/// then return NULL.
644Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
645
646 // Check if AvailableAnalysis map has one entry.
647 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
648
649 if (I != AvailableAnalysis.end())
650 return I->second;
651
652 // Search Parents through TopLevelManager
653 if (SearchParent)
654 return TPM->findAnalysisPass(AID);
655
656 // FIXME : This is expensive and requires. Need to check only managers not all passes.
657 // One solution is to collect managers in advance at TPM level.
658 Pass *P = NULL;
659 for(std::vector<Pass *>::iterator I = passVectorBegin(),
660 E = passVectorEnd(); P == NULL && I!= E; ++I )
661 P = NULL; // FIXME : P = (*I)->getResolver()->getAnalysisToUpdate(AID, false /* Do not search parents again */);
662
663 return P;
664}
665
Devang Patela1514cb2006-12-07 19:39:39 +0000666//===----------------------------------------------------------------------===//
667// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000668
Devang Pateld65e9e92006-11-08 01:31:28 +0000669/// Add pass P into PassVector and return true. If this pass is not
670/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000671bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000672BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000673
674 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
675 if (!BP)
676 return false;
677
Devang Patel3c8eb622006-11-07 22:56:50 +0000678 // If this pass does not preserve anlysis that is used by other passes
679 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000680 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000681 return false;
682
Devang Patel8cad70d2006-11-11 01:51:02 +0000683 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000684
Devang Patel6e5a1132006-11-07 21:31:57 +0000685 return true;
686}
687
688/// Execute all of the passes scheduled for execution by invoking
689/// runOnBasicBlock method. Keep track of whether any of the passes modifies
690/// the function, and if so, return true.
691bool
692BasicBlockPassManager_New::runOnFunction(Function &F) {
693
Devang Patele9585592006-12-08 01:38:28 +0000694 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000695 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000696
Devang Patel6e5a1132006-11-07 21:31:57 +0000697 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000698 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
699 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000700 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000701
Devang Patel6e5a1132006-11-07 21:31:57 +0000702 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
703 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000704 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000705 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000706 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000707 }
Devang Patele9585592006-12-08 01:38:28 +0000708 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000709}
710
Devang Patel475c4532006-12-08 00:59:05 +0000711// Implement doInitialization and doFinalization
712inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
713 bool Changed = false;
714
715 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
716 e = passVectorEnd(); itr != e; ++itr) {
717 Pass *P = *itr;
718 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
719 Changed |= BP->doInitialization(M);
720 }
721
722 return Changed;
723}
724
725inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
726 bool Changed = false;
727
728 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
729 e = passVectorEnd(); itr != e; ++itr) {
730 Pass *P = *itr;
731 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
732 Changed |= BP->doFinalization(M);
733 }
734
735 return Changed;
736}
737
738inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
739 bool Changed = false;
740
741 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
742 e = passVectorEnd(); itr != e; ++itr) {
743 Pass *P = *itr;
744 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
745 Changed |= BP->doInitialization(F);
746 }
747
748 return Changed;
749}
750
751inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
752 bool Changed = false;
753
754 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
755 e = passVectorEnd(); itr != e; ++itr) {
756 Pass *P = *itr;
757 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
758 Changed |= BP->doFinalization(F);
759 }
760
761 return Changed;
762}
763
764
Devang Patela1514cb2006-12-07 19:39:39 +0000765//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000766// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000767
Devang Patel4e12f862006-11-08 10:44:40 +0000768/// Create new Function pass manager
769FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000770 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000771}
772
Devang Patel1f653682006-12-08 18:57:16 +0000773FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
774 FPM = new FunctionPassManagerImpl_New(0);
775 MP = P;
776}
777
Devang Patel4e12f862006-11-08 10:44:40 +0000778/// add - Add a pass to the queue of passes to run. This passes
779/// ownership of the Pass to the PassManager. When the
780/// PassManager_X is destroyed, the pass will be destroyed as well, so
781/// there is no need to delete the pass. (TODO delete passes.)
782/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000783void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000784 FPM->add(P);
785}
786
787/// Execute all of the passes scheduled for execution. Keep
788/// track of whether any of the passes modifies the function, and if
789/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000790bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000791 return FPM->runOnModule(M);
792}
793
Devang Patel9f3083e2006-11-15 19:39:54 +0000794/// run - Execute all of the passes scheduled for execution. Keep
795/// track of whether any of the passes modifies the function, and if
796/// so, return true.
797///
798bool FunctionPassManager_New::run(Function &F) {
799 std::string errstr;
800 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000801 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000802 abort();
803 }
Devang Patel272908d2006-12-08 22:57:48 +0000804 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000805}
806
807
Devang Patelff631ae2006-11-15 01:27:05 +0000808/// doInitialization - Run all of the initializers for the function passes.
809///
810bool FunctionPassManager_New::doInitialization() {
811 return FPM->doInitialization(*MP->getModule());
812}
813
814/// doFinalization - Run all of the initializers for the function passes.
815///
816bool FunctionPassManager_New::doFinalization() {
817 return FPM->doFinalization(*MP->getModule());
818}
819
Devang Patela1514cb2006-12-07 19:39:39 +0000820//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000821// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000822
Devang Patel0c2012f2006-11-07 21:49:50 +0000823/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
824/// either use it into active basic block pass manager or create new basic
825/// block pass manager to handle pass P.
826bool
Devang Patel4e12f862006-11-08 10:44:40 +0000827FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000828
829 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
830 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
831
Devang Patel4949fe02006-12-07 22:34:21 +0000832 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000833
Devang Patel4949fe02006-12-07 22:34:21 +0000834 // If active manager exists then clear its analysis info.
835 if (activeBBPassManager)
836 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000837
Devang Patel4949fe02006-12-07 22:34:21 +0000838 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000839 activeBBPassManager =
840 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000841 addPassToManager(activeBBPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000842
843 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000844 if (!activeBBPassManager->addPass(BP))
845 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000846 }
Devang Patelbc03f132006-12-07 23:55:10 +0000847
848 if (!ForcedLastUses.empty())
849 TPM->setLastUser(ForcedLastUses, this);
850
Devang Patel0c2012f2006-11-07 21:49:50 +0000851 return true;
852 }
853
854 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
855 if (!FP)
856 return false;
857
Devang Patel3c8eb622006-11-07 22:56:50 +0000858 // If this pass does not preserve anlysis that is used by other passes
859 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000860 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000861 return false;
862
Devang Patel8cad70d2006-11-11 01:51:02 +0000863 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000864
865 // If active manager exists then clear its analysis info.
866 if (activeBBPassManager) {
867 activeBBPassManager->initializeAnalysisInfo();
868 activeBBPassManager = NULL;
869 }
870
Devang Patel0c2012f2006-11-07 21:49:50 +0000871 return true;
872}
873
874/// Execute all of the passes scheduled for execution by invoking
875/// runOnFunction method. Keep track of whether any of the passes modifies
876/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000877bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000878
Devang Patel0e29e292006-12-08 19:04:09 +0000879 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000880 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000881
Devang Patel0c2012f2006-11-07 21:49:50 +0000882 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +0000883 this->runOnFunction(*I);
884
Devang Patel0e29e292006-12-08 19:04:09 +0000885 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +0000886}
887
Devang Patel9f3083e2006-11-15 19:39:54 +0000888/// Execute all of the passes scheduled for execution by invoking
889/// runOnFunction method. Keep track of whether any of the passes modifies
890/// the function, and if so, return true.
891bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
892
893 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000894 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000895
896 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
897 e = passVectorEnd(); itr != e; ++itr) {
898 Pass *P = *itr;
899
Devang Patel9f3083e2006-11-15 19:39:54 +0000900 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
901 Changed |= FP->runOnFunction(F);
902 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000903 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000904 removeDeadPasses(P);
905 }
906 return Changed;
907}
908
909
Devang Patelff631ae2006-11-15 01:27:05 +0000910inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
911 bool Changed = false;
912
913 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
914 e = passVectorEnd(); itr != e; ++itr) {
915 Pass *P = *itr;
916
917 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
918 Changed |= FP->doInitialization(M);
919 }
920
921 return Changed;
922}
923
924inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
925 bool Changed = false;
926
927 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
928 e = passVectorEnd(); itr != e; ++itr) {
929 Pass *P = *itr;
930
931 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
932 Changed |= FP->doFinalization(M);
933 }
934
Devang Patelff631ae2006-11-15 01:27:05 +0000935 return Changed;
936}
937
Devang Patel272908d2006-12-08 22:57:48 +0000938// Execute all the passes managed by this top level manager.
939// Return true if any function is modified by a pass.
940bool FunctionPassManagerImpl_New::run(Function &F) {
941
942 bool Changed = false;
943 for (std::vector<Pass *>::iterator I = passManagersBegin(),
944 E = passManagersEnd(); I != E; ++I) {
945 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
946 Changed |= FP->runOnFunction(F);
947 }
948 return Changed;
949}
950
Devang Patela1514cb2006-12-07 19:39:39 +0000951//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000952// ModulePassManager implementation
953
954/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000955/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000956/// is not manageable by this manager.
957bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000958ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000959
960 // If P is FunctionPass then use function pass maanager.
961 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
962
Devang Patel640c5bb2006-12-08 22:30:11 +0000963 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +0000964
Devang Patel4949fe02006-12-07 22:34:21 +0000965 // If active manager exists then clear its analysis info.
966 if (activeFunctionPassManager)
967 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000968
Devang Patel4949fe02006-12-07 22:34:21 +0000969 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000970 activeFunctionPassManager =
971 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000972 addPassToManager(activeFunctionPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000973
974 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000975 if (!activeFunctionPassManager->addPass(FP))
976 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000977 }
Devang Patelbc03f132006-12-07 23:55:10 +0000978
979 if (!ForcedLastUses.empty())
980 TPM->setLastUser(ForcedLastUses, this);
981
Devang Patel05e1a972006-11-07 22:03:15 +0000982 return true;
983 }
984
985 ModulePass *MP = dynamic_cast<ModulePass *>(P);
986 if (!MP)
987 return false;
988
Devang Patel3c8eb622006-11-07 22:56:50 +0000989 // If this pass does not preserve anlysis that is used by other passes
990 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000991 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000992 return false;
993
Devang Patel8cad70d2006-11-11 01:51:02 +0000994 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +0000995 // If active manager exists then clear its analysis info.
996 if (activeFunctionPassManager) {
997 activeFunctionPassManager->initializeAnalysisInfo();
998 activeFunctionPassManager = NULL;
999 }
1000
Devang Patel05e1a972006-11-07 22:03:15 +00001001 return true;
1002}
1003
1004
1005/// Execute all of the passes scheduled for execution by invoking
1006/// runOnModule method. Keep track of whether any of the passes modifies
1007/// the module, and if so, return true.
1008bool
1009ModulePassManager_New::runOnModule(Module &M) {
1010 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001011 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001012
Devang Patel8cad70d2006-11-11 01:51:02 +00001013 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1014 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001015 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +00001016
Devang Patel05e1a972006-11-07 22:03:15 +00001017 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1018 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001019 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001020 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001021 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001022 }
1023 return Changed;
1024}
1025
Devang Patela1514cb2006-12-07 19:39:39 +00001026//===----------------------------------------------------------------------===//
1027// PassManagerImpl implementation
1028
Devang Patelc290c8a2006-11-07 22:23:34 +00001029// PassManager_New implementation
1030/// Add P into active pass manager or use new module pass manager to
1031/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001032bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001033
Devang Patel6c9f5482006-11-11 00:42:16 +00001034 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001035 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patel5bbeb492006-12-08 22:47:25 +00001036 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001037 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001038 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001039 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001040}
1041
1042/// run - Execute all of the passes scheduled for execution. Keep track of
1043/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001044bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001045
Devang Patelc290c8a2006-11-07 22:23:34 +00001046 bool Changed = false;
Devang Patel5bbeb492006-12-08 22:47:25 +00001047 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1048 E = passManagersEnd(); I != E; ++I) {
1049 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1050 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001051 }
1052 return Changed;
1053}
Devang Patel376fefa2006-11-08 10:29:57 +00001054
Devang Patela1514cb2006-12-07 19:39:39 +00001055//===----------------------------------------------------------------------===//
1056// PassManager implementation
1057
Devang Patel376fefa2006-11-08 10:29:57 +00001058/// Create new pass manager
1059PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001060 PM = new PassManagerImpl_New(0);
Devang Patel376fefa2006-11-08 10:29:57 +00001061}
1062
1063/// add - Add a pass to the queue of passes to run. This passes ownership of
1064/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1065/// will be destroyed as well, so there is no need to delete the pass. This
1066/// implies that all passes MUST be allocated with 'new'.
1067void
1068PassManager_New::add(Pass *P) {
1069 PM->add(P);
1070}
1071
1072/// run - Execute all of the passes scheduled for execution. Keep track of
1073/// whether any of the passes modifies the module, and if so, return true.
1074bool
1075PassManager_New::run(Module &M) {
1076 return PM->run(M);
1077}
1078