blob: 1141c6a32918198d8051ffd004a6bb1f8ab98ad1 [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 Patelf33f3eb2006-12-07 19:21:29 +0000140private:
141
142 /// Collection of pass managers
143 std::vector<Pass *> PassManagers;
144
145 // Map to keep track of last user of the analysis pass.
146 // LastUser->second is the last user of Lastuser->first.
147 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000148
149 /// Immutable passes are managed by top level manager.
150 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000151};
152
153/// Set pass P as the last user of the given analysis passes.
154void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
155 Pass *P) {
156
157 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
158 E = AnalysisPasses.end(); I != E; ++I) {
159 Pass *AP = *I;
160 LastUser[AP] = P;
161 // If AP is the last user of other passes then make P last user of
162 // such passes.
163 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
164 LUE = LastUser.end(); LUI != LUE; ++LUI) {
165 if (LUI->second == AP)
166 LastUser[LUI->first] = P;
167 }
168 }
169
170}
171
172/// Collect passes whose last user is P
173void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
174 Pass *P) {
175 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
176 LUE = LastUser.end(); LUI != LUE; ++LUI)
177 if (LUI->second == P)
178 LastUses.push_back(LUI->first);
179}
180
Devang Patelde124182006-12-07 21:10:57 +0000181/// Schedule pass P for execution. Make sure that passes required by
182/// P are run before P is run. Update analysis info maintained by
183/// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000184void PMTopLevelManager::schedulePass(Pass *P) {
Devang Patelde124182006-12-07 21:10:57 +0000185
186 // TODO : Allocate function manager for this pass, other wise required set
187 // may be inserted into previous function manager
188
189 AnalysisUsage AnUsage;
190 P->getAnalysisUsage(AnUsage);
191 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
192 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
193 E = RequiredSet.end(); I != E; ++I) {
194
Devang Patel640c5bb2006-12-08 22:30:11 +0000195 Pass *AnalysisPass = findAnalysisPass(*I);
Devang Patelde124182006-12-07 21:10:57 +0000196 if (!AnalysisPass) {
197 // Schedule this analysis run first.
198 AnalysisPass = (*I)->createPass();
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000199 schedulePass(AnalysisPass);
Devang Patelde124182006-12-07 21:10:57 +0000200 }
201 }
202
203 // Now all required passes are available.
204 addTopLevelPass(P);
205}
206
Devang Patel640c5bb2006-12-08 22:30:11 +0000207/// Find the pass that implements Analysis AID. Search immutable
208/// passes and all pass managers. If desired pass is not found
209/// then return NULL.
210Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
211
212 Pass *P = NULL;
213 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
214 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
215 const PassInfo *PI = (*I)->getPassInfo();
216 if (PI == AID)
217 P = *I;
218
219 // If Pass not found then check the interfaces implemented by Immutable Pass
220 if (!P) {
221 const std::vector<const PassInfo*> &ImmPI =
222 PI->getInterfacesImplemented();
223 for (unsigned Index = 0, End = ImmPI.size();
224 P == NULL && Index != End; ++Index)
225 if (ImmPI[Index] == AID)
226 P = *I;
227 }
228 }
229
230 if (P)
231 return P;
232
233 // Check pass managers;
234 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
235 E = PassManagers.end(); P == NULL && I != E; ++I)
236 P = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
237
238 return P;
239}
240
Devang Patelf3827bc2006-12-07 19:54:15 +0000241//===----------------------------------------------------------------------===//
242// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000243
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000244/// PMDataManager provides the common place to manage the analysis data
245/// used by pass managers.
246class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000247
248public:
249
Devang Patel4c36e6b2006-12-07 23:24:58 +0000250 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000251 initializeAnalysisInfo();
252 }
253
Devang Patela9844592006-11-11 01:31:05 +0000254 /// Return true IFF pass P's required analysis set does not required new
255 /// manager.
256 bool manageablePass(Pass *P);
257
Devang Patela9844592006-11-11 01:31:05 +0000258 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000259 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000260
Devang Patela9844592006-11-11 01:31:05 +0000261 /// Remove Analysis that is not preserved by the pass
262 void removeNotPreservedAnalysis(Pass *P);
263
264 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000265 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000266
Devang Patel8f677ce2006-12-07 18:47:25 +0000267 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000268 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
269 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000270
Devang Patel1d6267c2006-12-07 23:05:44 +0000271 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000272 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000273 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000274 AvailableAnalysis.clear();
Devang Patelb3900322006-12-07 21:02:08 +0000275
276 // Include immutable passes into AvailableAnalysis vector.
277 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
278 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
279 E = ImmutablePasses.end(); I != E; ++I)
280 recordAvailableAnalysis(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000281 }
282
Devang Patel1d6267c2006-12-07 23:05:44 +0000283 /// Populate RequiredPasses with the analysis pass that are required by
284 /// pass P.
285 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
286 Pass *P);
287
288 /// All Required analyses should be available to the pass as it runs! Here
289 /// we fill in the AnalysisImpls member of the pass so that it can
290 /// successfully use the getAnalysis() method to retrieve the
291 /// implementations it needs.
292 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000293
Devang Patel640c5bb2006-12-08 22:30:11 +0000294 /// Find the pass that implements Analysis AID. If desired pass is not found
295 /// then return NULL.
296 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
297
Devang Patel8cad70d2006-11-11 01:51:02 +0000298 inline std::vector<Pass *>::iterator passVectorBegin() {
299 return PassVector.begin();
300 }
301
302 inline std::vector<Pass *>::iterator passVectorEnd() {
303 return PassVector.end();
304 }
305
Devang Patelf3827bc2006-12-07 19:54:15 +0000306 // Access toplevel manager
307 PMTopLevelManager *getTopLevelManager() { return TPM; }
308 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
309
Devang Patel4c36e6b2006-12-07 23:24:58 +0000310 unsigned getDepth() { return Depth; }
311
Devang Patelbc03f132006-12-07 23:55:10 +0000312protected:
313
314 // Collection of pass whose last user asked this manager to claim
315 // last use. If a FunctionPass F is the last user of ModulePass info M
316 // then the F's manager, not F, records itself as a last user of M.
317 std::vector<Pass *> ForcedLastUses;
318
319 // Top level manager.
320 // TODO : Make it a reference.
321 PMTopLevelManager *TPM;
322
Devang Patela9844592006-11-11 01:31:05 +0000323private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000324 // Set of available Analysis. This information is used while scheduling
325 // pass. If a pass requires an analysis which is not not available then
326 // equired analysis pass is scheduled to run before the pass itself is
327 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000328 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000329
330 // Collection of pass that are managed by this manager
331 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000332
Devang Patel4c36e6b2006-12-07 23:24:58 +0000333 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000334};
335
Devang Patelca58e352006-11-08 10:05:38 +0000336/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
337/// pass together and sequence them to process one basic block before
338/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000339class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000340 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000341
342public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000343 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000344
345 /// Add a pass into a passmanager queue.
346 bool addPass(Pass *p);
347
348 /// Execute all of the passes scheduled for execution. Keep track of
349 /// whether any of the passes modifies the function, and if so, return true.
350 bool runOnFunction(Function &F);
351
Devang Patelf9d96b92006-12-07 19:57:52 +0000352 /// Pass Manager itself does not invalidate any analysis info.
353 void getAnalysisUsage(AnalysisUsage &Info) const {
354 Info.setPreservesAll();
355 }
356
Devang Patel475c4532006-12-08 00:59:05 +0000357 bool doInitialization(Module &M);
358 bool doInitialization(Function &F);
359 bool doFinalization(Module &M);
360 bool doFinalization(Function &F);
361
Devang Patelca58e352006-11-08 10:05:38 +0000362};
363
Devang Patel4e12f862006-11-08 10:44:40 +0000364/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000365/// It batches all function passes and basic block pass managers together and
366/// sequence them to process one function at a time before processing next
367/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000368class FunctionPassManagerImpl_New : public ModulePass,
369 public PMDataManager,
370 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000371public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000372 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
373 PMDataManager(D) { /* TODO */ }
374 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000375 activeBBPassManager = NULL;
376 }
Devang Patel4e12f862006-11-08 10:44:40 +0000377 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000378
Devang Patelabcd1d32006-12-07 21:27:23 +0000379 inline void addTopLevelPass(Pass *P) {
380 addPass(P);
381 }
382
Devang Patelca58e352006-11-08 10:05:38 +0000383 /// add - Add a pass to the queue of passes to run. This passes
384 /// ownership of the Pass to the PassManager. When the
385 /// PassManager_X is destroyed, the pass will be destroyed as well, so
386 /// there is no need to delete the pass. (TODO delete passes.)
387 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000388 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000389 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000390 }
Devang Patelca58e352006-11-08 10:05:38 +0000391
392 /// Add pass into the pass manager queue.
393 bool addPass(Pass *P);
394
395 /// Execute all of the passes scheduled for execution. Keep
396 /// track of whether any of the passes modifies the function, and if
397 /// so, return true.
398 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000399 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000400
Devang Patelff631ae2006-11-15 01:27:05 +0000401 /// doInitialization - Run all of the initializers for the function passes.
402 ///
403 bool doInitialization(Module &M);
404
405 /// doFinalization - Run all of the initializers for the function passes.
406 ///
407 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000408
409 /// Pass Manager itself does not invalidate any analysis info.
410 void getAnalysisUsage(AnalysisUsage &Info) const {
411 Info.setPreservesAll();
412 }
413
Devang Patelca58e352006-11-08 10:05:38 +0000414private:
Devang Patelca58e352006-11-08 10:05:38 +0000415 // Active Pass Managers
416 BasicBlockPassManager_New *activeBBPassManager;
417};
418
419/// ModulePassManager_New manages ModulePasses and function pass managers.
420/// It batches all Module passes passes and function pass managers together and
421/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000422class ModulePassManager_New : public Pass,
423 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000424
425public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000426 ModulePassManager_New(int D) : PMDataManager(D) {
427 activeFunctionPassManager = NULL;
428 }
Devang Patelca58e352006-11-08 10:05:38 +0000429
430 /// Add a pass into a passmanager queue.
431 bool addPass(Pass *p);
432
433 /// run - Execute all of the passes scheduled for execution. Keep track of
434 /// whether any of the passes modifies the module, and if so, return true.
435 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000436
Devang Patelf9d96b92006-12-07 19:57:52 +0000437 /// Pass Manager itself does not invalidate any analysis info.
438 void getAnalysisUsage(AnalysisUsage &Info) const {
439 Info.setPreservesAll();
440 }
441
Devang Patelca58e352006-11-08 10:05:38 +0000442private:
Devang Patelca58e352006-11-08 10:05:38 +0000443 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000444 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000445};
446
Devang Patel376fefa2006-11-08 10:29:57 +0000447/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000448class PassManagerImpl_New : public Pass,
449 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000450 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000451
452public:
453
Devang Patel4c36e6b2006-12-07 23:24:58 +0000454 PassManagerImpl_New(int D) : PMDataManager(D) {}
455
Devang Patel376fefa2006-11-08 10:29:57 +0000456 /// add - Add a pass to the queue of passes to run. This passes ownership of
457 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
458 /// will be destroyed as well, so there is no need to delete the pass. This
459 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000460 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000461 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000462 }
Devang Patel376fefa2006-11-08 10:29:57 +0000463
464 /// run - Execute all of the passes scheduled for execution. Keep track of
465 /// whether any of the passes modifies the module, and if so, return true.
466 bool run(Module &M);
467
Devang Patelf9d96b92006-12-07 19:57:52 +0000468 /// Pass Manager itself does not invalidate any analysis info.
469 void getAnalysisUsage(AnalysisUsage &Info) const {
470 Info.setPreservesAll();
471 }
472
Devang Patelabcd1d32006-12-07 21:27:23 +0000473 inline void addTopLevelPass(Pass *P) {
474 addPass(P);
475 }
476
Devang Patel376fefa2006-11-08 10:29:57 +0000477private:
478
Devang Patelde124182006-12-07 21:10:57 +0000479 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000480 bool addPass(Pass *p);
481
Devang Patel376fefa2006-11-08 10:29:57 +0000482 // Collection of pass managers
483 std::vector<ModulePassManager_New *> PassManagers;
484
Devang Patel376fefa2006-11-08 10:29:57 +0000485 // Active Pass Manager
486 ModulePassManager_New *activeManager;
487};
488
Devang Patelca58e352006-11-08 10:05:38 +0000489} // End of llvm namespace
490
Devang Patela1514cb2006-12-07 19:39:39 +0000491//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000492// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000493
Devang Pateld65e9e92006-11-08 01:31:28 +0000494/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000495/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000496bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000497
Devang Patel8f677ce2006-12-07 18:47:25 +0000498 // TODO
499 // If this pass is not preserving information that is required by a
500 // pass maintained by higher level pass manager then do not insert
501 // this pass into current manager. Use new manager. For example,
502 // For example, If FunctionPass F is not preserving ModulePass Info M1
503 // that is used by another ModulePass M2 then do not insert F in
504 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000505 return true;
506}
507
Devang Patel643676c2006-11-11 01:10:19 +0000508/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000509void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000510
Devang Patel643676c2006-11-11 01:10:19 +0000511 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000512 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000513
Devang Patele9976aa2006-12-07 19:33:53 +0000514 //This pass is the current implementation of all of the interfaces it
515 //implements as well.
516 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
517 for (unsigned i = 0, e = II.size(); i != e; ++i)
518 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000519 }
520}
521
Devang Patelf68a3492006-11-07 22:35:17 +0000522/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000523void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000524 AnalysisUsage AnUsage;
525 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000526
Devang Patel2e169c32006-12-07 20:03:49 +0000527 if (AnUsage.getPreservesAll())
528 return;
529
530 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000531 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000532 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000533 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000534 PreservedSet.end()) {
535 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000536 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000537 AvailableAnalysis.erase(J);
538 }
539 }
Devang Patelf68a3492006-11-07 22:35:17 +0000540}
541
Devang Patelca189262006-11-14 03:05:08 +0000542/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000543void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000544
545 std::vector<Pass *> DeadPasses;
546 TPM->collectLastUses(DeadPasses, P);
547
548 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
549 E = DeadPasses.end(); I != E; ++I) {
550 (*I)->releaseMemory();
551
552 std::map<AnalysisID, Pass*>::iterator Pos =
553 AvailableAnalysis.find((*I)->getPassInfo());
554
Devang Patel475c4532006-12-08 00:59:05 +0000555 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000556 if (Pos != AvailableAnalysis.end())
557 AvailableAnalysis.erase(Pos);
558 }
Devang Patelca189262006-11-14 03:05:08 +0000559}
560
Devang Patel8f677ce2006-12-07 18:47:25 +0000561/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000562/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000563void PMDataManager::addPassToManager(Pass *P,
564 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000565
Devang Patel90b05e02006-11-11 02:04:19 +0000566 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000567
568 // At the moment, this pass is the last user of all required passes.
569 std::vector<Pass *> LastUses;
570 std::vector<Pass *> RequiredPasses;
571 unsigned PDepth = this->getDepth();
572
573 collectRequiredAnalysisPasses(RequiredPasses, P);
574 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
575 E = RequiredPasses.end(); I != E; ++I) {
576 Pass *PRequired = *I;
577 unsigned RDepth = 0;
578 //FIXME: RDepth = PRequired->getResolver()->getDepth();
579 if (PDepth == RDepth)
580 LastUses.push_back(PRequired);
581 else if (PDepth > RDepth) {
582 // Let the parent claim responsibility of last use
583 ForcedLastUses.push_back(PRequired);
584 } else {
585 // Note : This feature is not yet implemented
586 assert (0 &&
587 "Unable to handle Pass that requires lower level Analysis pass");
588 }
589 }
590
591 if (!LastUses.empty())
592 TPM->setLastUser(LastUses, P);
593
Devang Patel17bff0d2006-12-07 22:09:36 +0000594 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000595 // Remove the analysis not preserved by this pass
Devang Patel17bff0d2006-12-07 22:09:36 +0000596 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000597 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000598 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000599 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000600
601 // Add pass
602 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000603}
604
Devang Patel1d6267c2006-12-07 23:05:44 +0000605/// Populate RequiredPasses with the analysis pass that are required by
606/// pass P.
607void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
608 Pass *P) {
609 AnalysisUsage AnUsage;
610 P->getAnalysisUsage(AnUsage);
611 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
612 for (std::vector<AnalysisID>::const_iterator
613 I = RequiredSet.begin(), E = RequiredSet.end();
614 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000615 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000616 assert (AnalysisPass && "Analysis pass is not available");
617 RP.push_back(AnalysisPass);
618 }
619}
620
Devang Patel07f4f582006-11-14 21:49:36 +0000621// All Required analyses should be available to the pass as it runs! Here
622// we fill in the AnalysisImpls member of the pass so that it can
623// successfully use the getAnalysis() method to retrieve the
624// implementations it needs.
625//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000626void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000627 AnalysisUsage AnUsage;
628 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000629
630 for (std::vector<const PassInfo *>::const_iterator
631 I = AnUsage.getRequiredSet().begin(),
632 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000633 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000634 if (Impl == 0)
635 assert(0 && "Analysis used but not available!");
636 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
637 }
638}
639
Devang Patel640c5bb2006-12-08 22:30:11 +0000640/// Find the pass that implements Analysis AID. If desired pass is not found
641/// then return NULL.
642Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
643
644 // Check if AvailableAnalysis map has one entry.
645 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
646
647 if (I != AvailableAnalysis.end())
648 return I->second;
649
650 // Search Parents through TopLevelManager
651 if (SearchParent)
652 return TPM->findAnalysisPass(AID);
653
654 // FIXME : This is expensive and requires. Need to check only managers not all passes.
655 // One solution is to collect managers in advance at TPM level.
656 Pass *P = NULL;
657 for(std::vector<Pass *>::iterator I = passVectorBegin(),
658 E = passVectorEnd(); P == NULL && I!= E; ++I )
659 P = NULL; // FIXME : P = (*I)->getResolver()->getAnalysisToUpdate(AID, false /* Do not search parents again */);
660
661 return P;
662}
663
Devang Patela1514cb2006-12-07 19:39:39 +0000664//===----------------------------------------------------------------------===//
665// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000666
Devang Pateld65e9e92006-11-08 01:31:28 +0000667/// Add pass P into PassVector and return true. If this pass is not
668/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000669bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000670BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000671
672 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
673 if (!BP)
674 return false;
675
Devang Patel3c8eb622006-11-07 22:56:50 +0000676 // If this pass does not preserve anlysis that is used by other passes
677 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000678 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000679 return false;
680
Devang Patel8cad70d2006-11-11 01:51:02 +0000681 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000682
Devang Patel6e5a1132006-11-07 21:31:57 +0000683 return true;
684}
685
686/// Execute all of the passes scheduled for execution by invoking
687/// runOnBasicBlock method. Keep track of whether any of the passes modifies
688/// the function, and if so, return true.
689bool
690BasicBlockPassManager_New::runOnFunction(Function &F) {
691
Devang Patele9585592006-12-08 01:38:28 +0000692 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000693 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000694
Devang Patel6e5a1132006-11-07 21:31:57 +0000695 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000696 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
697 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000698 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000699
Devang Patel6e5a1132006-11-07 21:31:57 +0000700 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
701 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000702 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000703 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000704 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000705 }
Devang Patele9585592006-12-08 01:38:28 +0000706 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000707}
708
Devang Patel475c4532006-12-08 00:59:05 +0000709// Implement doInitialization and doFinalization
710inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
711 bool Changed = false;
712
713 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
714 e = passVectorEnd(); itr != e; ++itr) {
715 Pass *P = *itr;
716 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
717 Changed |= BP->doInitialization(M);
718 }
719
720 return Changed;
721}
722
723inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
724 bool Changed = false;
725
726 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
727 e = passVectorEnd(); itr != e; ++itr) {
728 Pass *P = *itr;
729 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
730 Changed |= BP->doFinalization(M);
731 }
732
733 return Changed;
734}
735
736inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
737 bool Changed = false;
738
739 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
740 e = passVectorEnd(); itr != e; ++itr) {
741 Pass *P = *itr;
742 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
743 Changed |= BP->doInitialization(F);
744 }
745
746 return Changed;
747}
748
749inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
750 bool Changed = false;
751
752 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
753 e = passVectorEnd(); itr != e; ++itr) {
754 Pass *P = *itr;
755 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
756 Changed |= BP->doFinalization(F);
757 }
758
759 return Changed;
760}
761
762
Devang Patela1514cb2006-12-07 19:39:39 +0000763//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000764// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000765
Devang Patel4e12f862006-11-08 10:44:40 +0000766/// Create new Function pass manager
767FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000768 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000769}
770
Devang Patel1f653682006-12-08 18:57:16 +0000771FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
772 FPM = new FunctionPassManagerImpl_New(0);
773 MP = P;
774}
775
Devang Patel4e12f862006-11-08 10:44:40 +0000776/// add - Add a pass to the queue of passes to run. This passes
777/// ownership of the Pass to the PassManager. When the
778/// PassManager_X is destroyed, the pass will be destroyed as well, so
779/// there is no need to delete the pass. (TODO delete passes.)
780/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000781void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000782 FPM->add(P);
783}
784
785/// Execute all of the passes scheduled for execution. Keep
786/// track of whether any of the passes modifies the function, and if
787/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000788bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000789 return FPM->runOnModule(M);
790}
791
Devang Patel9f3083e2006-11-15 19:39:54 +0000792/// run - Execute all of the passes scheduled for execution. Keep
793/// track of whether any of the passes modifies the function, and if
794/// so, return true.
795///
796bool FunctionPassManager_New::run(Function &F) {
797 std::string errstr;
798 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000799 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000800 abort();
801 }
802 return FPM->runOnFunction(F);
803}
804
805
Devang Patelff631ae2006-11-15 01:27:05 +0000806/// doInitialization - Run all of the initializers for the function passes.
807///
808bool FunctionPassManager_New::doInitialization() {
809 return FPM->doInitialization(*MP->getModule());
810}
811
812/// doFinalization - Run all of the initializers for the function passes.
813///
814bool FunctionPassManager_New::doFinalization() {
815 return FPM->doFinalization(*MP->getModule());
816}
817
Devang Patela1514cb2006-12-07 19:39:39 +0000818//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000819// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000820
Devang Patel0c2012f2006-11-07 21:49:50 +0000821/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
822/// either use it into active basic block pass manager or create new basic
823/// block pass manager to handle pass P.
824bool
Devang Patel4e12f862006-11-08 10:44:40 +0000825FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000826
827 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
828 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
829
Devang Patel4949fe02006-12-07 22:34:21 +0000830 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000831
Devang Patel4949fe02006-12-07 22:34:21 +0000832 // If active manager exists then clear its analysis info.
833 if (activeBBPassManager)
834 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000835
Devang Patel4949fe02006-12-07 22:34:21 +0000836 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000837 activeBBPassManager =
838 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000839 addPassToManager(activeBBPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000840
841 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000842 if (!activeBBPassManager->addPass(BP))
843 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000844 }
Devang Patelbc03f132006-12-07 23:55:10 +0000845
846 if (!ForcedLastUses.empty())
847 TPM->setLastUser(ForcedLastUses, this);
848
Devang Patel0c2012f2006-11-07 21:49:50 +0000849 return true;
850 }
851
852 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
853 if (!FP)
854 return false;
855
Devang Patel3c8eb622006-11-07 22:56:50 +0000856 // If this pass does not preserve anlysis that is used by other passes
857 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000858 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000859 return false;
860
Devang Patel8cad70d2006-11-11 01:51:02 +0000861 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000862
863 // If active manager exists then clear its analysis info.
864 if (activeBBPassManager) {
865 activeBBPassManager->initializeAnalysisInfo();
866 activeBBPassManager = NULL;
867 }
868
Devang Patel0c2012f2006-11-07 21:49:50 +0000869 return true;
870}
871
872/// Execute all of the passes scheduled for execution by invoking
873/// runOnFunction method. Keep track of whether any of the passes modifies
874/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000875bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000876
Devang Patel0e29e292006-12-08 19:04:09 +0000877 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000878 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000879
Devang Patel0c2012f2006-11-07 21:49:50 +0000880 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +0000881 this->runOnFunction(*I);
882
Devang Patel0e29e292006-12-08 19:04:09 +0000883 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +0000884}
885
Devang Patel9f3083e2006-11-15 19:39:54 +0000886/// Execute all of the passes scheduled for execution by invoking
887/// runOnFunction method. Keep track of whether any of the passes modifies
888/// the function, and if so, return true.
889bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
890
891 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000892 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000893
894 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
895 e = passVectorEnd(); itr != e; ++itr) {
896 Pass *P = *itr;
897
Devang Patel9f3083e2006-11-15 19:39:54 +0000898 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
899 Changed |= FP->runOnFunction(F);
900 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000901 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000902 removeDeadPasses(P);
903 }
904 return Changed;
905}
906
907
Devang Patelff631ae2006-11-15 01:27:05 +0000908inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
909 bool Changed = false;
910
911 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
912 e = passVectorEnd(); itr != e; ++itr) {
913 Pass *P = *itr;
914
915 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
916 Changed |= FP->doInitialization(M);
917 }
918
919 return Changed;
920}
921
922inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
923 bool Changed = false;
924
925 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
926 e = passVectorEnd(); itr != e; ++itr) {
927 Pass *P = *itr;
928
929 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
930 Changed |= FP->doFinalization(M);
931 }
932
Devang Patelff631ae2006-11-15 01:27:05 +0000933 return Changed;
934}
935
Devang Patela1514cb2006-12-07 19:39:39 +0000936//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000937// ModulePassManager implementation
938
939/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000940/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000941/// is not manageable by this manager.
942bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000943ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000944
945 // If P is FunctionPass then use function pass maanager.
946 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
947
Devang Patel640c5bb2006-12-08 22:30:11 +0000948 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +0000949
Devang Patel4949fe02006-12-07 22:34:21 +0000950 // If active manager exists then clear its analysis info.
951 if (activeFunctionPassManager)
952 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000953
Devang Patel4949fe02006-12-07 22:34:21 +0000954 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000955 activeFunctionPassManager =
956 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000957 addPassToManager(activeFunctionPassManager, false);
Devang Patel4949fe02006-12-07 22:34:21 +0000958
959 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000960 if (!activeFunctionPassManager->addPass(FP))
961 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000962 }
Devang Patelbc03f132006-12-07 23:55:10 +0000963
964 if (!ForcedLastUses.empty())
965 TPM->setLastUser(ForcedLastUses, this);
966
Devang Patel05e1a972006-11-07 22:03:15 +0000967 return true;
968 }
969
970 ModulePass *MP = dynamic_cast<ModulePass *>(P);
971 if (!MP)
972 return false;
973
Devang Patel3c8eb622006-11-07 22:56:50 +0000974 // If this pass does not preserve anlysis that is used by other passes
975 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000976 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000977 return false;
978
Devang Patel8cad70d2006-11-11 01:51:02 +0000979 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +0000980 // If active manager exists then clear its analysis info.
981 if (activeFunctionPassManager) {
982 activeFunctionPassManager->initializeAnalysisInfo();
983 activeFunctionPassManager = NULL;
984 }
985
Devang Patel05e1a972006-11-07 22:03:15 +0000986 return true;
987}
988
989
990/// Execute all of the passes scheduled for execution by invoking
991/// runOnModule method. Keep track of whether any of the passes modifies
992/// the module, and if so, return true.
993bool
994ModulePassManager_New::runOnModule(Module &M) {
995 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000996 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000997
Devang Patel8cad70d2006-11-11 01:51:02 +0000998 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
999 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001000 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +00001001
Devang Patel05e1a972006-11-07 22:03:15 +00001002 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1003 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001004 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001005 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001006 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001007 }
1008 return Changed;
1009}
1010
Devang Patela1514cb2006-12-07 19:39:39 +00001011//===----------------------------------------------------------------------===//
1012// PassManagerImpl implementation
1013
Devang Patelc290c8a2006-11-07 22:23:34 +00001014// PassManager_New implementation
1015/// Add P into active pass manager or use new module pass manager to
1016/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001017bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001018
Devang Patel6c9f5482006-11-11 00:42:16 +00001019 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001020 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patelc290c8a2006-11-07 22:23:34 +00001021 PassManagers.push_back(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001022 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001023 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001024 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001025}
1026
1027/// run - Execute all of the passes scheduled for execution. Keep track of
1028/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001029bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001030
Devang Patelc290c8a2006-11-07 22:23:34 +00001031 bool Changed = false;
1032 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
1033 e = PassManagers.end(); itr != e; ++itr) {
1034 ModulePassManager_New *pm = *itr;
1035 Changed |= pm->runOnModule(M);
1036 }
1037 return Changed;
1038}
Devang Patel376fefa2006-11-08 10:29:57 +00001039
Devang Patela1514cb2006-12-07 19:39:39 +00001040//===----------------------------------------------------------------------===//
1041// PassManager implementation
1042
Devang Patel376fefa2006-11-08 10:29:57 +00001043/// Create new pass manager
1044PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001045 PM = new PassManagerImpl_New(0);
Devang Patel376fefa2006-11-08 10:29:57 +00001046}
1047
1048/// add - Add a pass to the queue of passes to run. This passes ownership of
1049/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1050/// will be destroyed as well, so there is no need to delete the pass. This
1051/// implies that all passes MUST be allocated with 'new'.
1052void
1053PassManager_New::add(Pass *P) {
1054 PM->add(P);
1055}
1056
1057/// run - Execute all of the passes scheduled for execution. Keep track of
1058/// whether any of the passes modifies the module, and if so, return true.
1059bool
1060PassManager_New::run(Module &M) {
1061 return PM->run(M);
1062}
1063