blob: 4d047205a36cd32f7459cd38971755d2888c815b [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 Patelafb1f3622006-12-12 22:35:25 +000089class PMDataManager;
90
Devang Patelf33f3eb2006-12-07 19:21:29 +000091//===----------------------------------------------------------------------===//
92// PMTopLevelManager
93//
94/// PMTopLevelManager manages LastUser info and collects common APIs used by
95/// top level pass managers.
96class PMTopLevelManager {
97
98public:
99
100 inline std::vector<Pass *>::iterator passManagersBegin() {
101 return PassManagers.begin();
102 }
103
104 inline std::vector<Pass *>::iterator passManagersEnd() {
105 return PassManagers.end();
106 }
107
108 /// Schedule pass P for execution. Make sure that passes required by
109 /// P are run before P is run. Update analysis info maintained by
110 /// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000111 void schedulePass(Pass *P);
Devang Patelf33f3eb2006-12-07 19:21:29 +0000112
113 /// This is implemented by top level pass manager and used by
114 /// schedulePass() to add analysis info passes that are not available.
115 virtual void addTopLevelPass(Pass *P) = 0;
116
117 /// Set pass P as the last user of the given analysis passes.
118 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
119
120 /// Collect passes whose last user is P
121 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
122
Devang Patel640c5bb2006-12-08 22:30:11 +0000123 /// Find the pass that implements Analysis AID. Search immutable
124 /// passes and all pass managers. If desired pass is not found
125 /// then return NULL.
126 Pass *findAnalysisPass(AnalysisID AID);
127
Devang Patelf33f3eb2006-12-07 19:21:29 +0000128 virtual ~PMTopLevelManager() {
129 PassManagers.clear();
130 }
131
Devang Patele0eb9d82006-12-07 20:51:18 +0000132 /// Add immutable pass and initialize it.
133 inline void addImmutablePass(ImmutablePass *P) {
134 P->initializePass();
135 ImmutablePasses.push_back(P);
136 }
137
138 inline std::vector<ImmutablePass *>& getImmutablePasses() {
139 return ImmutablePasses;
140 }
141
Devang Patel5bbeb492006-12-08 22:47:25 +0000142 void addPassManager(Pass *Manager) {
143 PassManagers.push_back(Manager);
144 }
145
Devang Patelaf1fca52006-12-08 23:11:43 +0000146 // Add Manager into the list of managers that are not directly
147 // maintained by this top level pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000148 inline void addIndirectPassManager(PMDataManager *Manager) {
149 IndirectPassManagers.push_back(Manager);
Devang Patelaf1fca52006-12-08 23:11:43 +0000150 }
151
Devang Patelf33f3eb2006-12-07 19:21:29 +0000152private:
153
154 /// Collection of pass managers
155 std::vector<Pass *> PassManagers;
156
Devang Patelaf1fca52006-12-08 23:11:43 +0000157 /// Collection of pass managers that are not directly maintained
158 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000159 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000160
Devang Patelf33f3eb2006-12-07 19:21:29 +0000161 // Map to keep track of last user of the analysis pass.
162 // LastUser->second is the last user of Lastuser->first.
163 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000164
165 /// Immutable passes are managed by top level manager.
166 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000167};
168
Devang Patelf3827bc2006-12-07 19:54:15 +0000169//===----------------------------------------------------------------------===//
170// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000171
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000172/// PMDataManager provides the common place to manage the analysis data
173/// used by pass managers.
174class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000175
176public:
177
Devang Patel4c36e6b2006-12-07 23:24:58 +0000178 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000179 initializeAnalysisInfo();
180 }
181
Devang Patela9844592006-11-11 01:31:05 +0000182 /// Return true IFF pass P's required analysis set does not required new
183 /// manager.
184 bool manageablePass(Pass *P);
185
Devang Patela9844592006-11-11 01:31:05 +0000186 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000187 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000188
Devang Patela9844592006-11-11 01:31:05 +0000189 /// Remove Analysis that is not preserved by the pass
190 void removeNotPreservedAnalysis(Pass *P);
191
192 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000193 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000194
Devang Patel8f677ce2006-12-07 18:47:25 +0000195 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000196 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
197 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000198
Devang Patel1d6267c2006-12-07 23:05:44 +0000199 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000200 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000201 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000202 AvailableAnalysis.clear();
203 }
204
Devang Patel1d6267c2006-12-07 23:05:44 +0000205 /// Populate RequiredPasses with the analysis pass that are required by
206 /// pass P.
207 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
208 Pass *P);
209
210 /// All Required analyses should be available to the pass as it runs! Here
211 /// we fill in the AnalysisImpls member of the pass so that it can
212 /// successfully use the getAnalysis() method to retrieve the
213 /// implementations it needs.
214 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000215
Devang Patel640c5bb2006-12-08 22:30:11 +0000216 /// Find the pass that implements Analysis AID. If desired pass is not found
217 /// then return NULL.
218 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
219
Devang Patel8cad70d2006-11-11 01:51:02 +0000220 inline std::vector<Pass *>::iterator passVectorBegin() {
221 return PassVector.begin();
222 }
223
224 inline std::vector<Pass *>::iterator passVectorEnd() {
225 return PassVector.end();
226 }
227
Devang Patelf3827bc2006-12-07 19:54:15 +0000228 // Access toplevel manager
229 PMTopLevelManager *getTopLevelManager() { return TPM; }
230 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
231
Devang Patel4c36e6b2006-12-07 23:24:58 +0000232 unsigned getDepth() { return Depth; }
233
Devang Patelbc03f132006-12-07 23:55:10 +0000234protected:
235
236 // Collection of pass whose last user asked this manager to claim
237 // last use. If a FunctionPass F is the last user of ModulePass info M
238 // then the F's manager, not F, records itself as a last user of M.
239 std::vector<Pass *> ForcedLastUses;
240
241 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000242 PMTopLevelManager *TPM;
243
Devang Patela9844592006-11-11 01:31:05 +0000244private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000245 // Set of available Analysis. This information is used while scheduling
246 // pass. If a pass requires an analysis which is not not available then
247 // equired analysis pass is scheduled to run before the pass itself is
248 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000249 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000250
251 // Collection of pass that are managed by this manager
252 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000253
Devang Patel4c36e6b2006-12-07 23:24:58 +0000254 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000255};
256
Devang Patel10c2ca62006-12-12 22:47:13 +0000257//===----------------------------------------------------------------------===//
258// BasicBlockPassManager_New
259//
Devang Patelca58e352006-11-08 10:05:38 +0000260/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
261/// pass together and sequence them to process one basic block before
262/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000263class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000264 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000265
266public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000267 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000268
269 /// Add a pass into a passmanager queue.
270 bool addPass(Pass *p);
271
272 /// Execute all of the passes scheduled for execution. Keep track of
273 /// whether any of the passes modifies the function, and if so, return true.
274 bool runOnFunction(Function &F);
275
Devang Patelf9d96b92006-12-07 19:57:52 +0000276 /// Pass Manager itself does not invalidate any analysis info.
277 void getAnalysisUsage(AnalysisUsage &Info) const {
278 Info.setPreservesAll();
279 }
280
Devang Patel475c4532006-12-08 00:59:05 +0000281 bool doInitialization(Module &M);
282 bool doInitialization(Function &F);
283 bool doFinalization(Module &M);
284 bool doFinalization(Function &F);
285
Devang Patelca58e352006-11-08 10:05:38 +0000286};
287
Devang Patel10c2ca62006-12-12 22:47:13 +0000288//===----------------------------------------------------------------------===//
289// FunctionPassManagerImpl_New
290//
Devang Patel4e12f862006-11-08 10:44:40 +0000291/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000292/// It batches all function passes and basic block pass managers together and
293/// sequence them to process one function at a time before processing next
294/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000295class FunctionPassManagerImpl_New : public ModulePass,
296 public PMDataManager,
297 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000298public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000299 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000300 activeBBPassManager = NULL;
301 }
Devang Patel4e12f862006-11-08 10:44:40 +0000302 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000303
Devang Patelabcd1d32006-12-07 21:27:23 +0000304 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000305
Devang Patelfa971cd2006-12-08 23:57:43 +0000306 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000307
308 // P is a immutable pass then it will be managed by this
309 // top level manager. Set up analysis resolver to connect them.
310 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
311 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000312 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000313 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000314 recordAvailableAnalysis(IP);
Devang Patelfa971cd2006-12-08 23:57:43 +0000315 }
316 else
317 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000318 }
319
Devang Patelca58e352006-11-08 10:05:38 +0000320 /// add - Add a pass to the queue of passes to run. This passes
321 /// ownership of the Pass to the PassManager. When the
322 /// PassManager_X is destroyed, the pass will be destroyed as well, so
323 /// there is no need to delete the pass. (TODO delete passes.)
324 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000325 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000326 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000327 }
Devang Patelca58e352006-11-08 10:05:38 +0000328
329 /// Add pass into the pass manager queue.
330 bool addPass(Pass *P);
331
332 /// Execute all of the passes scheduled for execution. Keep
333 /// track of whether any of the passes modifies the function, and if
334 /// so, return true.
335 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000336 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000337 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000338
Devang Patelff631ae2006-11-15 01:27:05 +0000339 /// doInitialization - Run all of the initializers for the function passes.
340 ///
341 bool doInitialization(Module &M);
342
343 /// doFinalization - Run all of the initializers for the function passes.
344 ///
345 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000346
347 /// Pass Manager itself does not invalidate any analysis info.
348 void getAnalysisUsage(AnalysisUsage &Info) const {
349 Info.setPreservesAll();
350 }
351
Devang Patelca58e352006-11-08 10:05:38 +0000352private:
Devang Patelca58e352006-11-08 10:05:38 +0000353 // Active Pass Managers
354 BasicBlockPassManager_New *activeBBPassManager;
355};
356
Devang Patel10c2ca62006-12-12 22:47:13 +0000357//===----------------------------------------------------------------------===//
358// ModulePassManager_New
359//
Devang Patelca58e352006-11-08 10:05:38 +0000360/// ModulePassManager_New manages ModulePasses and function pass managers.
361/// It batches all Module passes passes and function pass managers together and
362/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000363class ModulePassManager_New : public Pass,
364 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000365
366public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000367 ModulePassManager_New(int D) : PMDataManager(D) {
368 activeFunctionPassManager = NULL;
369 }
Devang Patelca58e352006-11-08 10:05:38 +0000370
371 /// Add a pass into a passmanager queue.
372 bool addPass(Pass *p);
373
374 /// run - Execute all of the passes scheduled for execution. Keep track of
375 /// whether any of the passes modifies the module, and if so, return true.
376 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000377
Devang Patelf9d96b92006-12-07 19:57:52 +0000378 /// Pass Manager itself does not invalidate any analysis info.
379 void getAnalysisUsage(AnalysisUsage &Info) const {
380 Info.setPreservesAll();
381 }
382
Devang Patelca58e352006-11-08 10:05:38 +0000383private:
Devang Patelca58e352006-11-08 10:05:38 +0000384 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000385 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000386};
387
Devang Patel10c2ca62006-12-12 22:47:13 +0000388//===----------------------------------------------------------------------===//
389// PassManagerImpl_New
390//
391/// PassManagerImpl_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000392class PassManagerImpl_New : public Pass,
393 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000394 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000395
396public:
397
Devang Patel4c36e6b2006-12-07 23:24:58 +0000398 PassManagerImpl_New(int D) : PMDataManager(D) {}
399
Devang Patel376fefa2006-11-08 10:29:57 +0000400 /// add - Add a pass to the queue of passes to run. This passes ownership of
401 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
402 /// will be destroyed as well, so there is no need to delete the pass. This
403 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000404 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000405 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000406 }
Devang Patel376fefa2006-11-08 10:29:57 +0000407
408 /// run - Execute all of the passes scheduled for execution. Keep track of
409 /// whether any of the passes modifies the module, and if so, return true.
410 bool run(Module &M);
411
Devang Patelf9d96b92006-12-07 19:57:52 +0000412 /// Pass Manager itself does not invalidate any analysis info.
413 void getAnalysisUsage(AnalysisUsage &Info) const {
414 Info.setPreservesAll();
415 }
416
Devang Patelabcd1d32006-12-07 21:27:23 +0000417 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000418
Devang Patelfa971cd2006-12-08 23:57:43 +0000419 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000420
421 // P is a immutable pass and it will be managed by this
422 // top level manager. Set up analysis resolver to connect them.
423 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
424 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000425 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000426 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000427 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000428 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000429 else
430 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000431 }
432
Devang Patel376fefa2006-11-08 10:29:57 +0000433private:
434
Devang Patelde124182006-12-07 21:10:57 +0000435 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000436 bool addPass(Pass *p);
437
Devang Patel376fefa2006-11-08 10:29:57 +0000438 // Active Pass Manager
439 ModulePassManager_New *activeManager;
440};
441
Devang Patelca58e352006-11-08 10:05:38 +0000442} // End of llvm namespace
443
Devang Patela1514cb2006-12-07 19:39:39 +0000444//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000445// PMTopLevelManager implementation
446
447/// Set pass P as the last user of the given analysis passes.
448void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
449 Pass *P) {
450
451 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
452 E = AnalysisPasses.end(); I != E; ++I) {
453 Pass *AP = *I;
454 LastUser[AP] = P;
455 // If AP is the last user of other passes then make P last user of
456 // such passes.
457 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
458 LUE = LastUser.end(); LUI != LUE; ++LUI) {
459 if (LUI->second == AP)
460 LastUser[LUI->first] = P;
461 }
462 }
463
464}
465
466/// Collect passes whose last user is P
467void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
468 Pass *P) {
469 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
470 LUE = LastUser.end(); LUI != LUE; ++LUI)
471 if (LUI->second == P)
472 LastUses.push_back(LUI->first);
473}
474
475/// Schedule pass P for execution. Make sure that passes required by
476/// P are run before P is run. Update analysis info maintained by
477/// the manager. Remove dead passes. This is a recursive function.
478void PMTopLevelManager::schedulePass(Pass *P) {
479
480 // TODO : Allocate function manager for this pass, other wise required set
481 // may be inserted into previous function manager
482
483 AnalysisUsage AnUsage;
484 P->getAnalysisUsage(AnUsage);
485 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
486 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
487 E = RequiredSet.end(); I != E; ++I) {
488
489 Pass *AnalysisPass = findAnalysisPass(*I);
490 if (!AnalysisPass) {
491 // Schedule this analysis run first.
492 AnalysisPass = (*I)->createPass();
493 schedulePass(AnalysisPass);
494 }
495 }
496
497 // Now all required passes are available.
498 addTopLevelPass(P);
499}
500
501/// Find the pass that implements Analysis AID. Search immutable
502/// passes and all pass managers. If desired pass is not found
503/// then return NULL.
504Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
505
506 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000507 // Check pass managers
508 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
509 E = PassManagers.end(); P == NULL && I != E; ++I) {
510 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
511 assert(PMD && "This is not a PassManager");
512 P = PMD->findAnalysisPass(AID, false);
513 }
514
515 // Check other pass managers
516 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
517 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
518 P = (*I)->findAnalysisPass(AID, false);
519
Devang Patelafb1f3622006-12-12 22:35:25 +0000520 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
521 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
522 const PassInfo *PI = (*I)->getPassInfo();
523 if (PI == AID)
524 P = *I;
525
526 // If Pass not found then check the interfaces implemented by Immutable Pass
527 if (!P) {
528 const std::vector<const PassInfo*> &ImmPI =
529 PI->getInterfacesImplemented();
530 for (unsigned Index = 0, End = ImmPI.size();
531 P == NULL && Index != End; ++Index)
532 if (ImmPI[Index] == AID)
533 P = *I;
534 }
535 }
536
Devang Patelafb1f3622006-12-12 22:35:25 +0000537 return P;
538}
539
540//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000541// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000542
Devang Pateld65e9e92006-11-08 01:31:28 +0000543/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000544/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000545bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000546
Devang Patel8f677ce2006-12-07 18:47:25 +0000547 // TODO
548 // If this pass is not preserving information that is required by a
549 // pass maintained by higher level pass manager then do not insert
550 // this pass into current manager. Use new manager. For example,
551 // For example, If FunctionPass F is not preserving ModulePass Info M1
552 // that is used by another ModulePass M2 then do not insert F in
553 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000554 return true;
555}
556
Devang Patel643676c2006-11-11 01:10:19 +0000557/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000558void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000559
Devang Patel643676c2006-11-11 01:10:19 +0000560 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000561 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000562
Devang Patele9976aa2006-12-07 19:33:53 +0000563 //This pass is the current implementation of all of the interfaces it
564 //implements as well.
565 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
566 for (unsigned i = 0, e = II.size(); i != e; ++i)
567 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000568 }
569}
570
Devang Patelf68a3492006-11-07 22:35:17 +0000571/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000572void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000573 AnalysisUsage AnUsage;
574 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000575
Devang Patel2e169c32006-12-07 20:03:49 +0000576 if (AnUsage.getPreservesAll())
577 return;
578
579 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000580 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000581 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000582 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000583 PreservedSet.end()) {
584 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000585 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000586 AvailableAnalysis.erase(J);
587 }
588 }
Devang Patelf68a3492006-11-07 22:35:17 +0000589}
590
Devang Patelca189262006-11-14 03:05:08 +0000591/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000592void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000593
594 std::vector<Pass *> DeadPasses;
595 TPM->collectLastUses(DeadPasses, P);
596
597 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
598 E = DeadPasses.end(); I != E; ++I) {
599 (*I)->releaseMemory();
600
601 std::map<AnalysisID, Pass*>::iterator Pos =
602 AvailableAnalysis.find((*I)->getPassInfo());
603
Devang Patel475c4532006-12-08 00:59:05 +0000604 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000605 if (Pos != AvailableAnalysis.end())
606 AvailableAnalysis.erase(Pos);
607 }
Devang Patelca189262006-11-14 03:05:08 +0000608}
609
Devang Patel8f677ce2006-12-07 18:47:25 +0000610/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000611/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000612void PMDataManager::addPassToManager(Pass *P,
613 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000614
Devang Pateld440cd92006-12-08 23:53:00 +0000615 // This manager is going to manage pass P. Set up analysis resolver
616 // to connect them.
617 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
618 P->setResolver(AR);
619
Devang Patel90b05e02006-11-11 02:04:19 +0000620 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000621
622 // At the moment, this pass is the last user of all required passes.
623 std::vector<Pass *> LastUses;
624 std::vector<Pass *> RequiredPasses;
625 unsigned PDepth = this->getDepth();
626
627 collectRequiredAnalysisPasses(RequiredPasses, P);
628 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
629 E = RequiredPasses.end(); I != E; ++I) {
630 Pass *PRequired = *I;
631 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000632
633 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
634 RDepth = DM.getDepth();
635
Devang Patelbc03f132006-12-07 23:55:10 +0000636 if (PDepth == RDepth)
637 LastUses.push_back(PRequired);
638 else if (PDepth > RDepth) {
639 // Let the parent claim responsibility of last use
640 ForcedLastUses.push_back(PRequired);
641 } else {
642 // Note : This feature is not yet implemented
643 assert (0 &&
644 "Unable to handle Pass that requires lower level Analysis pass");
645 }
646 }
647
648 if (!LastUses.empty())
649 TPM->setLastUser(LastUses, P);
650
Devang Patel17bff0d2006-12-07 22:09:36 +0000651 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000652 // Remove the analysis not preserved by this pass
653 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000654 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000655 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000656
657 // Add pass
658 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000659}
660
Devang Patel1d6267c2006-12-07 23:05:44 +0000661/// Populate RequiredPasses with the analysis pass that are required by
662/// pass P.
663void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
664 Pass *P) {
665 AnalysisUsage AnUsage;
666 P->getAnalysisUsage(AnUsage);
667 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
668 for (std::vector<AnalysisID>::const_iterator
669 I = RequiredSet.begin(), E = RequiredSet.end();
670 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000671 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000672 assert (AnalysisPass && "Analysis pass is not available");
673 RP.push_back(AnalysisPass);
674 }
675}
676
Devang Patel07f4f582006-11-14 21:49:36 +0000677// All Required analyses should be available to the pass as it runs! Here
678// we fill in the AnalysisImpls member of the pass so that it can
679// successfully use the getAnalysis() method to retrieve the
680// implementations it needs.
681//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000682void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000683 AnalysisUsage AnUsage;
684 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000685
686 for (std::vector<const PassInfo *>::const_iterator
687 I = AnUsage.getRequiredSet().begin(),
688 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000689 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000690 if (Impl == 0)
691 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000692 AnalysisResolver_New *AR = P->getResolver();
693 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000694 }
695}
696
Devang Patel640c5bb2006-12-08 22:30:11 +0000697/// Find the pass that implements Analysis AID. If desired pass is not found
698/// then return NULL.
699Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
700
701 // Check if AvailableAnalysis map has one entry.
702 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
703
704 if (I != AvailableAnalysis.end())
705 return I->second;
706
707 // Search Parents through TopLevelManager
708 if (SearchParent)
709 return TPM->findAnalysisPass(AID);
710
Devang Patel9d759b82006-12-09 00:09:12 +0000711 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000712}
713
Devang Patel9bdf7d42006-12-08 23:28:54 +0000714
715//===----------------------------------------------------------------------===//
716// NOTE: Is this the right place to define this method ?
717// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
718Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
719 return PM.findAnalysisPass(ID, dir);
720}
721
Devang Patela1514cb2006-12-07 19:39:39 +0000722//===----------------------------------------------------------------------===//
723// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000724
Devang Pateld65e9e92006-11-08 01:31:28 +0000725/// Add pass P into PassVector and return true. If this pass is not
726/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000727bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000728BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000729
730 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
731 if (!BP)
732 return false;
733
Devang Patel3c8eb622006-11-07 22:56:50 +0000734 // If this pass does not preserve anlysis that is used by other passes
735 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000736 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000737 return false;
738
Devang Patel8cad70d2006-11-11 01:51:02 +0000739 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000740
Devang Patel6e5a1132006-11-07 21:31:57 +0000741 return true;
742}
743
744/// Execute all of the passes scheduled for execution by invoking
745/// runOnBasicBlock method. Keep track of whether any of the passes modifies
746/// the function, and if so, return true.
747bool
748BasicBlockPassManager_New::runOnFunction(Function &F) {
749
Devang Patele9585592006-12-08 01:38:28 +0000750 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000751 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000752
Devang Patel6e5a1132006-11-07 21:31:57 +0000753 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000754 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
755 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000756 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000757
Devang Patel6e5a1132006-11-07 21:31:57 +0000758 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
759 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000760 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000761 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000762 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000763 }
Devang Patele9585592006-12-08 01:38:28 +0000764 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000765}
766
Devang Patel475c4532006-12-08 00:59:05 +0000767// Implement doInitialization and doFinalization
768inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
769 bool Changed = false;
770
771 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
772 e = passVectorEnd(); itr != e; ++itr) {
773 Pass *P = *itr;
774 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
775 Changed |= BP->doInitialization(M);
776 }
777
778 return Changed;
779}
780
781inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
782 bool Changed = false;
783
784 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
785 e = passVectorEnd(); itr != e; ++itr) {
786 Pass *P = *itr;
787 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
788 Changed |= BP->doFinalization(M);
789 }
790
791 return Changed;
792}
793
794inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
795 bool Changed = false;
796
797 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
798 e = passVectorEnd(); itr != e; ++itr) {
799 Pass *P = *itr;
800 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
801 Changed |= BP->doInitialization(F);
802 }
803
804 return Changed;
805}
806
807inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
808 bool Changed = false;
809
810 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
811 e = passVectorEnd(); itr != e; ++itr) {
812 Pass *P = *itr;
813 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
814 Changed |= BP->doFinalization(F);
815 }
816
817 return Changed;
818}
819
820
Devang Patela1514cb2006-12-07 19:39:39 +0000821//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000822// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000823
Devang Patel4e12f862006-11-08 10:44:40 +0000824/// Create new Function pass manager
825FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000826 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000827}
828
Devang Patel1f653682006-12-08 18:57:16 +0000829FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
830 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000831 // FPM is the top level manager.
832 FPM->setTopLevelManager(FPM);
Devang Patel1f653682006-12-08 18:57:16 +0000833 MP = P;
834}
835
Devang Patel4e12f862006-11-08 10:44:40 +0000836/// add - Add a pass to the queue of passes to run. This passes
837/// ownership of the Pass to the PassManager. When the
838/// PassManager_X is destroyed, the pass will be destroyed as well, so
839/// there is no need to delete the pass. (TODO delete passes.)
840/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000841void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000842 FPM->add(P);
843}
844
845/// Execute all of the passes scheduled for execution. Keep
846/// track of whether any of the passes modifies the function, and if
847/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000848bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000849 return FPM->runOnModule(M);
850}
851
Devang Patel9f3083e2006-11-15 19:39:54 +0000852/// run - Execute all of the passes scheduled for execution. Keep
853/// track of whether any of the passes modifies the function, and if
854/// so, return true.
855///
856bool FunctionPassManager_New::run(Function &F) {
857 std::string errstr;
858 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000859 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000860 abort();
861 }
Devang Patel272908d2006-12-08 22:57:48 +0000862 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000863}
864
865
Devang Patelff631ae2006-11-15 01:27:05 +0000866/// doInitialization - Run all of the initializers for the function passes.
867///
868bool FunctionPassManager_New::doInitialization() {
869 return FPM->doInitialization(*MP->getModule());
870}
871
872/// doFinalization - Run all of the initializers for the function passes.
873///
874bool FunctionPassManager_New::doFinalization() {
875 return FPM->doFinalization(*MP->getModule());
876}
877
Devang Patela1514cb2006-12-07 19:39:39 +0000878//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000879// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000880
Devang Patel0c2012f2006-11-07 21:49:50 +0000881/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
882/// either use it into active basic block pass manager or create new basic
883/// block pass manager to handle pass P.
884bool
Devang Patel4e12f862006-11-08 10:44:40 +0000885FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000886
887 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
888 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
889
Devang Patel4949fe02006-12-07 22:34:21 +0000890 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000891
Devang Patel4949fe02006-12-07 22:34:21 +0000892 // If active manager exists then clear its analysis info.
893 if (activeBBPassManager)
894 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000895
Devang Patel4949fe02006-12-07 22:34:21 +0000896 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000897 activeBBPassManager =
898 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +0000899 // Inherit top level manager
900 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +0000901
902 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +0000903 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +0000904
905 // Add new manager into top level manager's indirect passes list
906 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
907 assert (PMD && "Manager is not Pass Manager");
908 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +0000909
910 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000911 if (!activeBBPassManager->addPass(BP))
912 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000913 }
Devang Patelbc03f132006-12-07 23:55:10 +0000914
915 if (!ForcedLastUses.empty())
916 TPM->setLastUser(ForcedLastUses, this);
917
Devang Patel0c2012f2006-11-07 21:49:50 +0000918 return true;
919 }
920
921 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
922 if (!FP)
923 return false;
924
Devang Patel3c8eb622006-11-07 22:56:50 +0000925 // If this pass does not preserve anlysis that is used by other passes
926 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000927 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000928 return false;
929
Devang Patel8cad70d2006-11-11 01:51:02 +0000930 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000931
932 // If active manager exists then clear its analysis info.
933 if (activeBBPassManager) {
934 activeBBPassManager->initializeAnalysisInfo();
935 activeBBPassManager = NULL;
936 }
937
Devang Patel0c2012f2006-11-07 21:49:50 +0000938 return true;
939}
940
941/// Execute all of the passes scheduled for execution by invoking
942/// runOnFunction method. Keep track of whether any of the passes modifies
943/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000944bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000945
Devang Patel0e29e292006-12-08 19:04:09 +0000946 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000947 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000948
Devang Patel0c2012f2006-11-07 21:49:50 +0000949 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +0000950 this->runOnFunction(*I);
951
Devang Patel0e29e292006-12-08 19:04:09 +0000952 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +0000953}
954
Devang Patel9f3083e2006-11-15 19:39:54 +0000955/// Execute all of the passes scheduled for execution by invoking
956/// runOnFunction method. Keep track of whether any of the passes modifies
957/// the function, and if so, return true.
958bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
959
960 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000961 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000962
963 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
964 e = passVectorEnd(); itr != e; ++itr) {
965 Pass *P = *itr;
966
Devang Patel9f3083e2006-11-15 19:39:54 +0000967 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
968 Changed |= FP->runOnFunction(F);
969 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000970 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000971 removeDeadPasses(P);
972 }
973 return Changed;
974}
975
976
Devang Patelff631ae2006-11-15 01:27:05 +0000977inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
978 bool Changed = false;
979
980 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
981 e = passVectorEnd(); itr != e; ++itr) {
982 Pass *P = *itr;
983
984 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
985 Changed |= FP->doInitialization(M);
986 }
987
988 return Changed;
989}
990
991inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
992 bool Changed = false;
993
994 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
995 e = passVectorEnd(); itr != e; ++itr) {
996 Pass *P = *itr;
997
998 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
999 Changed |= FP->doFinalization(M);
1000 }
1001
Devang Patelff631ae2006-11-15 01:27:05 +00001002 return Changed;
1003}
1004
Devang Patel272908d2006-12-08 22:57:48 +00001005// Execute all the passes managed by this top level manager.
1006// Return true if any function is modified by a pass.
1007bool FunctionPassManagerImpl_New::run(Function &F) {
1008
1009 bool Changed = false;
1010 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1011 E = passManagersEnd(); I != E; ++I) {
1012 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
1013 Changed |= FP->runOnFunction(F);
1014 }
1015 return Changed;
1016}
1017
Devang Patela1514cb2006-12-07 19:39:39 +00001018//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +00001019// ModulePassManager implementation
1020
1021/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +00001022/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001023/// is not manageable by this manager.
1024bool
Devang Pateld65e9e92006-11-08 01:31:28 +00001025ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001026
1027 // If P is FunctionPass then use function pass maanager.
1028 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1029
Devang Patel640c5bb2006-12-08 22:30:11 +00001030 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001031
Devang Patel4949fe02006-12-07 22:34:21 +00001032 // If active manager exists then clear its analysis info.
1033 if (activeFunctionPassManager)
1034 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001035
Devang Patel4949fe02006-12-07 22:34:21 +00001036 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001037 activeFunctionPassManager =
1038 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001039
1040 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001041 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001042
Devang Patel9c6290c2006-12-12 22:02:16 +00001043 // Inherit top level manager
1044 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001045
1046 // Add new manager into top level manager's indirect passes list
1047 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1048 assert (PMD && "Manager is not Pass Manager");
1049 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001050
Devang Patel4949fe02006-12-07 22:34:21 +00001051 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001052 if (!activeFunctionPassManager->addPass(FP))
1053 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001054 }
Devang Patelbc03f132006-12-07 23:55:10 +00001055
1056 if (!ForcedLastUses.empty())
1057 TPM->setLastUser(ForcedLastUses, this);
1058
Devang Patel05e1a972006-11-07 22:03:15 +00001059 return true;
1060 }
1061
1062 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1063 if (!MP)
1064 return false;
1065
Devang Patel3c8eb622006-11-07 22:56:50 +00001066 // If this pass does not preserve anlysis that is used by other passes
1067 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001068 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001069 return false;
1070
Devang Patel8cad70d2006-11-11 01:51:02 +00001071 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001072 // If active manager exists then clear its analysis info.
1073 if (activeFunctionPassManager) {
1074 activeFunctionPassManager->initializeAnalysisInfo();
1075 activeFunctionPassManager = NULL;
1076 }
1077
Devang Patel05e1a972006-11-07 22:03:15 +00001078 return true;
1079}
1080
1081
1082/// Execute all of the passes scheduled for execution by invoking
1083/// runOnModule method. Keep track of whether any of the passes modifies
1084/// the module, and if so, return true.
1085bool
1086ModulePassManager_New::runOnModule(Module &M) {
1087 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001088 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001089
Devang Patel8cad70d2006-11-11 01:51:02 +00001090 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1091 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001092 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +00001093
Devang Patel05e1a972006-11-07 22:03:15 +00001094 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1095 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001096 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001097 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001098 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001099 }
1100 return Changed;
1101}
1102
Devang Patela1514cb2006-12-07 19:39:39 +00001103//===----------------------------------------------------------------------===//
1104// PassManagerImpl implementation
1105
Devang Patelc290c8a2006-11-07 22:23:34 +00001106// PassManager_New implementation
1107/// Add P into active pass manager or use new module pass manager to
1108/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001109bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001110
Devang Patel6c9f5482006-11-11 00:42:16 +00001111 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001112 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001113 // Inherit top level manager
1114 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001115
1116 // This top level manager is going to manage activeManager.
1117 // Set up analysis resolver to connect them.
1118 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1119 activeManager->setResolver(AR);
1120
Devang Patel5bbeb492006-12-08 22:47:25 +00001121 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001122 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001123 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001124 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001125}
1126
1127/// run - Execute all of the passes scheduled for execution. Keep track of
1128/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001129bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001130
Devang Patelc290c8a2006-11-07 22:23:34 +00001131 bool Changed = false;
Devang Patel5bbeb492006-12-08 22:47:25 +00001132 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1133 E = passManagersEnd(); I != E; ++I) {
1134 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1135 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001136 }
1137 return Changed;
1138}
Devang Patel376fefa2006-11-08 10:29:57 +00001139
Devang Patela1514cb2006-12-07 19:39:39 +00001140//===----------------------------------------------------------------------===//
1141// PassManager implementation
1142
Devang Patel376fefa2006-11-08 10:29:57 +00001143/// Create new pass manager
1144PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001145 PM = new PassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001146 // PM is the top level manager
1147 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001148}
1149
1150/// add - Add a pass to the queue of passes to run. This passes ownership of
1151/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1152/// will be destroyed as well, so there is no need to delete the pass. This
1153/// implies that all passes MUST be allocated with 'new'.
1154void
1155PassManager_New::add(Pass *P) {
1156 PM->add(P);
1157}
1158
1159/// run - Execute all of the passes scheduled for execution. Keep track of
1160/// whether any of the passes modifies the module, and if so, return true.
1161bool
1162PassManager_New::run(Module &M) {
1163 return PM->run(M);
1164}
1165