blob: bc469d33115d6d4589f19b1826d727852f5ebe4e [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 Patelad6b7fe2006-12-12 22:57:43 +0000398 PassManagerImpl_New(int D) : PMDataManager(D) {
399 activeManager = NULL;
400 }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000401
Devang Patel376fefa2006-11-08 10:29:57 +0000402 /// add - Add a pass to the queue of passes to run. This passes ownership of
403 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
404 /// will be destroyed as well, so there is no need to delete the pass. This
405 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000406 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000407 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000408 }
Devang Patel376fefa2006-11-08 10:29:57 +0000409
410 /// run - Execute all of the passes scheduled for execution. Keep track of
411 /// whether any of the passes modifies the module, and if so, return true.
412 bool run(Module &M);
413
Devang Patelf9d96b92006-12-07 19:57:52 +0000414 /// Pass Manager itself does not invalidate any analysis info.
415 void getAnalysisUsage(AnalysisUsage &Info) const {
416 Info.setPreservesAll();
417 }
418
Devang Patelabcd1d32006-12-07 21:27:23 +0000419 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000420
Devang Patelfa971cd2006-12-08 23:57:43 +0000421 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000422
423 // P is a immutable pass and it will be managed by this
424 // top level manager. Set up analysis resolver to connect them.
425 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
426 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000427 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000428 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000429 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000430 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000431 else
432 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000433 }
434
Devang Patel376fefa2006-11-08 10:29:57 +0000435private:
436
Devang Patelde124182006-12-07 21:10:57 +0000437 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000438 bool addPass(Pass *p);
439
Devang Patel376fefa2006-11-08 10:29:57 +0000440 // Active Pass Manager
441 ModulePassManager_New *activeManager;
442};
443
Devang Patelca58e352006-11-08 10:05:38 +0000444} // End of llvm namespace
445
Devang Patela1514cb2006-12-07 19:39:39 +0000446//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000447// PMTopLevelManager implementation
448
449/// Set pass P as the last user of the given analysis passes.
450void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
451 Pass *P) {
452
453 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
454 E = AnalysisPasses.end(); I != E; ++I) {
455 Pass *AP = *I;
456 LastUser[AP] = P;
457 // If AP is the last user of other passes then make P last user of
458 // such passes.
459 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
460 LUE = LastUser.end(); LUI != LUE; ++LUI) {
461 if (LUI->second == AP)
462 LastUser[LUI->first] = P;
463 }
464 }
465
466}
467
468/// Collect passes whose last user is P
469void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
470 Pass *P) {
471 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
472 LUE = LastUser.end(); LUI != LUE; ++LUI)
473 if (LUI->second == P)
474 LastUses.push_back(LUI->first);
475}
476
477/// Schedule pass P for execution. Make sure that passes required by
478/// P are run before P is run. Update analysis info maintained by
479/// the manager. Remove dead passes. This is a recursive function.
480void PMTopLevelManager::schedulePass(Pass *P) {
481
482 // TODO : Allocate function manager for this pass, other wise required set
483 // may be inserted into previous function manager
484
485 AnalysisUsage AnUsage;
486 P->getAnalysisUsage(AnUsage);
487 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
488 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
489 E = RequiredSet.end(); I != E; ++I) {
490
491 Pass *AnalysisPass = findAnalysisPass(*I);
492 if (!AnalysisPass) {
493 // Schedule this analysis run first.
494 AnalysisPass = (*I)->createPass();
495 schedulePass(AnalysisPass);
496 }
497 }
498
499 // Now all required passes are available.
500 addTopLevelPass(P);
501}
502
503/// Find the pass that implements Analysis AID. Search immutable
504/// passes and all pass managers. If desired pass is not found
505/// then return NULL.
506Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
507
508 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000509 // Check pass managers
510 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
511 E = PassManagers.end(); P == NULL && I != E; ++I) {
512 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
513 assert(PMD && "This is not a PassManager");
514 P = PMD->findAnalysisPass(AID, false);
515 }
516
517 // Check other pass managers
518 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
519 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
520 P = (*I)->findAnalysisPass(AID, false);
521
Devang Patelafb1f3622006-12-12 22:35:25 +0000522 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
523 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
524 const PassInfo *PI = (*I)->getPassInfo();
525 if (PI == AID)
526 P = *I;
527
528 // If Pass not found then check the interfaces implemented by Immutable Pass
529 if (!P) {
530 const std::vector<const PassInfo*> &ImmPI =
531 PI->getInterfacesImplemented();
532 for (unsigned Index = 0, End = ImmPI.size();
533 P == NULL && Index != End; ++Index)
534 if (ImmPI[Index] == AID)
535 P = *I;
536 }
537 }
538
Devang Patelafb1f3622006-12-12 22:35:25 +0000539 return P;
540}
541
542//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000543// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000544
Devang Pateld65e9e92006-11-08 01:31:28 +0000545/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000546/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000547bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000548
Devang Patel8f677ce2006-12-07 18:47:25 +0000549 // TODO
550 // If this pass is not preserving information that is required by a
551 // pass maintained by higher level pass manager then do not insert
552 // this pass into current manager. Use new manager. For example,
553 // For example, If FunctionPass F is not preserving ModulePass Info M1
554 // that is used by another ModulePass M2 then do not insert F in
555 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000556 return true;
557}
558
Devang Patel643676c2006-11-11 01:10:19 +0000559/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000560void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000561
Devang Patel643676c2006-11-11 01:10:19 +0000562 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000563 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000564
Devang Patele9976aa2006-12-07 19:33:53 +0000565 //This pass is the current implementation of all of the interfaces it
566 //implements as well.
567 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
568 for (unsigned i = 0, e = II.size(); i != e; ++i)
569 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000570 }
571}
572
Devang Patelf68a3492006-11-07 22:35:17 +0000573/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000574void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000575 AnalysisUsage AnUsage;
576 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000577
Devang Patel2e169c32006-12-07 20:03:49 +0000578 if (AnUsage.getPreservesAll())
579 return;
580
581 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000582 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000583 E = AvailableAnalysis.end(); I != E; ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000584 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000585 PreservedSet.end()) {
586 // Remove this analysis
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000587 if (!dynamic_cast<ImmutablePass*>(I->second)) {
588 std::map<AnalysisID, Pass*>::iterator J = I++;
589 AvailableAnalysis.erase(J);
590 } else
591 ++I;
592 } else
593 ++I;
Devang Patel349170f2006-11-11 01:24:55 +0000594 }
Devang Patelf68a3492006-11-07 22:35:17 +0000595}
596
Devang Patelca189262006-11-14 03:05:08 +0000597/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000598void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000599
600 std::vector<Pass *> DeadPasses;
601 TPM->collectLastUses(DeadPasses, P);
602
603 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
604 E = DeadPasses.end(); I != E; ++I) {
605 (*I)->releaseMemory();
606
607 std::map<AnalysisID, Pass*>::iterator Pos =
608 AvailableAnalysis.find((*I)->getPassInfo());
609
Devang Patel475c4532006-12-08 00:59:05 +0000610 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000611 if (Pos != AvailableAnalysis.end())
612 AvailableAnalysis.erase(Pos);
613 }
Devang Patelca189262006-11-14 03:05:08 +0000614}
615
Devang Patel8f677ce2006-12-07 18:47:25 +0000616/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000617/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000618void PMDataManager::addPassToManager(Pass *P,
619 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000620
Devang Pateld440cd92006-12-08 23:53:00 +0000621 // This manager is going to manage pass P. Set up analysis resolver
622 // to connect them.
623 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
624 P->setResolver(AR);
625
Devang Patel90b05e02006-11-11 02:04:19 +0000626 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000627
628 // At the moment, this pass is the last user of all required passes.
629 std::vector<Pass *> LastUses;
630 std::vector<Pass *> RequiredPasses;
631 unsigned PDepth = this->getDepth();
632
633 collectRequiredAnalysisPasses(RequiredPasses, P);
634 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
635 E = RequiredPasses.end(); I != E; ++I) {
636 Pass *PRequired = *I;
637 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000638
639 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
640 RDepth = DM.getDepth();
641
Devang Patelbc03f132006-12-07 23:55:10 +0000642 if (PDepth == RDepth)
643 LastUses.push_back(PRequired);
644 else if (PDepth > RDepth) {
645 // Let the parent claim responsibility of last use
646 ForcedLastUses.push_back(PRequired);
647 } else {
648 // Note : This feature is not yet implemented
649 assert (0 &&
650 "Unable to handle Pass that requires lower level Analysis pass");
651 }
652 }
653
654 if (!LastUses.empty())
655 TPM->setLastUser(LastUses, P);
656
Devang Patel17bff0d2006-12-07 22:09:36 +0000657 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000658 // Remove the analysis not preserved by this pass
659 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000660 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000661 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000662
663 // Add pass
664 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000665}
666
Devang Patel1d6267c2006-12-07 23:05:44 +0000667/// Populate RequiredPasses with the analysis pass that are required by
668/// pass P.
669void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
670 Pass *P) {
671 AnalysisUsage AnUsage;
672 P->getAnalysisUsage(AnUsage);
673 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
674 for (std::vector<AnalysisID>::const_iterator
675 I = RequiredSet.begin(), E = RequiredSet.end();
676 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000677 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000678 assert (AnalysisPass && "Analysis pass is not available");
679 RP.push_back(AnalysisPass);
680 }
Devang Patelf58183d2006-12-12 23:09:32 +0000681
682 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
683 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
684 E = IDs.end(); I != E; ++I) {
685 Pass *AnalysisPass = findAnalysisPass(*I, true);
686 assert (AnalysisPass && "Analysis pass is not available");
687 RP.push_back(AnalysisPass);
688 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000689}
690
Devang Patel07f4f582006-11-14 21:49:36 +0000691// All Required analyses should be available to the pass as it runs! Here
692// we fill in the AnalysisImpls member of the pass so that it can
693// successfully use the getAnalysis() method to retrieve the
694// implementations it needs.
695//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000696void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000697 AnalysisUsage AnUsage;
698 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000699
700 for (std::vector<const PassInfo *>::const_iterator
701 I = AnUsage.getRequiredSet().begin(),
702 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000703 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000704 if (Impl == 0)
705 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000706 AnalysisResolver_New *AR = P->getResolver();
707 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000708 }
709}
710
Devang Patel640c5bb2006-12-08 22:30:11 +0000711/// Find the pass that implements Analysis AID. If desired pass is not found
712/// then return NULL.
713Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
714
715 // Check if AvailableAnalysis map has one entry.
716 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
717
718 if (I != AvailableAnalysis.end())
719 return I->second;
720
721 // Search Parents through TopLevelManager
722 if (SearchParent)
723 return TPM->findAnalysisPass(AID);
724
Devang Patel9d759b82006-12-09 00:09:12 +0000725 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000726}
727
Devang Patel9bdf7d42006-12-08 23:28:54 +0000728
729//===----------------------------------------------------------------------===//
730// NOTE: Is this the right place to define this method ?
731// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
732Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
733 return PM.findAnalysisPass(ID, dir);
734}
735
Devang Patela1514cb2006-12-07 19:39:39 +0000736//===----------------------------------------------------------------------===//
737// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000738
Devang Pateld65e9e92006-11-08 01:31:28 +0000739/// Add pass P into PassVector and return true. If this pass is not
740/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000741bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000742BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000743
744 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
745 if (!BP)
746 return false;
747
Devang Patel3c8eb622006-11-07 22:56:50 +0000748 // If this pass does not preserve anlysis that is used by other passes
749 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000750 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000751 return false;
752
Devang Patel8cad70d2006-11-11 01:51:02 +0000753 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000754
Devang Patel6e5a1132006-11-07 21:31:57 +0000755 return true;
756}
757
758/// Execute all of the passes scheduled for execution by invoking
759/// runOnBasicBlock method. Keep track of whether any of the passes modifies
760/// the function, and if so, return true.
761bool
762BasicBlockPassManager_New::runOnFunction(Function &F) {
763
Devang Patel745a6962006-12-12 23:15:28 +0000764 if (F.isExternal())
765 return false;
766
Devang Patele9585592006-12-08 01:38:28 +0000767 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000768 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000769
Devang Patel6e5a1132006-11-07 21:31:57 +0000770 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000771 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
772 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000773 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +0000774 initializeAnalysisImpl(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000775 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
776 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000777 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000778 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000779 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000780 }
Devang Patele9585592006-12-08 01:38:28 +0000781 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000782}
783
Devang Patel475c4532006-12-08 00:59:05 +0000784// Implement doInitialization and doFinalization
785inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
786 bool Changed = false;
787
788 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
789 e = passVectorEnd(); itr != e; ++itr) {
790 Pass *P = *itr;
791 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
792 Changed |= BP->doInitialization(M);
793 }
794
795 return Changed;
796}
797
798inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
799 bool Changed = false;
800
801 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
802 e = passVectorEnd(); itr != e; ++itr) {
803 Pass *P = *itr;
804 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
805 Changed |= BP->doFinalization(M);
806 }
807
808 return Changed;
809}
810
811inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
812 bool Changed = false;
813
814 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
815 e = passVectorEnd(); itr != e; ++itr) {
816 Pass *P = *itr;
817 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
818 Changed |= BP->doInitialization(F);
819 }
820
821 return Changed;
822}
823
824inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
825 bool Changed = false;
826
827 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
828 e = passVectorEnd(); itr != e; ++itr) {
829 Pass *P = *itr;
830 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
831 Changed |= BP->doFinalization(F);
832 }
833
834 return Changed;
835}
836
837
Devang Patela1514cb2006-12-07 19:39:39 +0000838//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000839// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000840
Devang Patel4e12f862006-11-08 10:44:40 +0000841/// Create new Function pass manager
842FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000843 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000844}
845
Devang Patel1f653682006-12-08 18:57:16 +0000846FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
847 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000848 // FPM is the top level manager.
849 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000850
851 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
852 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
853 FPM->setResolver(AR);
854
855 FPM->addPassManager(FPM);
Devang Patel1f653682006-12-08 18:57:16 +0000856 MP = P;
857}
858
Devang Patel4e12f862006-11-08 10:44:40 +0000859/// add - Add a pass to the queue of passes to run. This passes
860/// ownership of the Pass to the PassManager. When the
861/// PassManager_X is destroyed, the pass will be destroyed as well, so
862/// there is no need to delete the pass. (TODO delete passes.)
863/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000864void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000865 FPM->add(P);
866}
867
868/// Execute all of the passes scheduled for execution. Keep
869/// track of whether any of the passes modifies the function, and if
870/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000871bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000872 return FPM->runOnModule(M);
873}
874
Devang Patel9f3083e2006-11-15 19:39:54 +0000875/// run - Execute all of the passes scheduled for execution. Keep
876/// track of whether any of the passes modifies the function, and if
877/// so, return true.
878///
879bool FunctionPassManager_New::run(Function &F) {
880 std::string errstr;
881 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000882 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000883 abort();
884 }
Devang Patel272908d2006-12-08 22:57:48 +0000885 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000886}
887
888
Devang Patelff631ae2006-11-15 01:27:05 +0000889/// doInitialization - Run all of the initializers for the function passes.
890///
891bool FunctionPassManager_New::doInitialization() {
892 return FPM->doInitialization(*MP->getModule());
893}
894
895/// doFinalization - Run all of the initializers for the function passes.
896///
897bool FunctionPassManager_New::doFinalization() {
898 return FPM->doFinalization(*MP->getModule());
899}
900
Devang Patela1514cb2006-12-07 19:39:39 +0000901//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000902// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000903
Devang Patel0c2012f2006-11-07 21:49:50 +0000904/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
905/// either use it into active basic block pass manager or create new basic
906/// block pass manager to handle pass P.
907bool
Devang Patel4e12f862006-11-08 10:44:40 +0000908FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000909
910 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
911 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
912
Devang Patel4949fe02006-12-07 22:34:21 +0000913 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000914
Devang Patel4949fe02006-12-07 22:34:21 +0000915 // If active manager exists then clear its analysis info.
916 if (activeBBPassManager)
917 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000918
Devang Patel4949fe02006-12-07 22:34:21 +0000919 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000920 activeBBPassManager =
921 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +0000922 // Inherit top level manager
923 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +0000924
925 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +0000926 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +0000927
928 // Add new manager into top level manager's indirect passes list
929 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
930 assert (PMD && "Manager is not Pass Manager");
931 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +0000932
933 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000934 if (!activeBBPassManager->addPass(BP))
935 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000936 }
Devang Patelbc03f132006-12-07 23:55:10 +0000937
938 if (!ForcedLastUses.empty())
939 TPM->setLastUser(ForcedLastUses, this);
940
Devang Patel0c2012f2006-11-07 21:49:50 +0000941 return true;
942 }
943
944 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
945 if (!FP)
946 return false;
947
Devang Patel3c8eb622006-11-07 22:56:50 +0000948 // If this pass does not preserve anlysis that is used by other passes
949 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000950 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000951 return false;
952
Devang Patel8cad70d2006-11-11 01:51:02 +0000953 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000954
955 // If active manager exists then clear its analysis info.
956 if (activeBBPassManager) {
957 activeBBPassManager->initializeAnalysisInfo();
958 activeBBPassManager = NULL;
959 }
960
Devang Patel0c2012f2006-11-07 21:49:50 +0000961 return true;
962}
963
964/// Execute all of the passes scheduled for execution by invoking
965/// runOnFunction method. Keep track of whether any of the passes modifies
966/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000967bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000968
Devang Patel0e29e292006-12-08 19:04:09 +0000969 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000970 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000971
Devang Patel0c2012f2006-11-07 21:49:50 +0000972 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +0000973 this->runOnFunction(*I);
974
Devang Patel0e29e292006-12-08 19:04:09 +0000975 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +0000976}
977
Devang Patel9f3083e2006-11-15 19:39:54 +0000978/// Execute all of the passes scheduled for execution by invoking
979/// runOnFunction method. Keep track of whether any of the passes modifies
980/// the function, and if so, return true.
981bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
982
983 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +0000984
985 if (F.isExternal())
986 return false;
987
Devang Patela6b6dcb2006-12-07 18:41:09 +0000988 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000989
990 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
991 e = passVectorEnd(); itr != e; ++itr) {
992 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +0000993 initializeAnalysisImpl(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000994 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
995 Changed |= FP->runOnFunction(F);
996 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000997 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000998 removeDeadPasses(P);
999 }
1000 return Changed;
1001}
1002
1003
Devang Patelff631ae2006-11-15 01:27:05 +00001004inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1005 bool Changed = false;
1006
1007 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1008 e = passVectorEnd(); itr != e; ++itr) {
1009 Pass *P = *itr;
1010
1011 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1012 Changed |= FP->doInitialization(M);
1013 }
1014
1015 return Changed;
1016}
1017
1018inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1019 bool Changed = false;
1020
1021 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1022 e = passVectorEnd(); itr != e; ++itr) {
1023 Pass *P = *itr;
1024
1025 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1026 Changed |= FP->doFinalization(M);
1027 }
1028
Devang Patelff631ae2006-11-15 01:27:05 +00001029 return Changed;
1030}
1031
Devang Patel272908d2006-12-08 22:57:48 +00001032// Execute all the passes managed by this top level manager.
1033// Return true if any function is modified by a pass.
1034bool FunctionPassManagerImpl_New::run(Function &F) {
1035
1036 bool Changed = false;
1037 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1038 E = passManagersEnd(); I != E; ++I) {
1039 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
1040 Changed |= FP->runOnFunction(F);
1041 }
1042 return Changed;
1043}
1044
Devang Patela1514cb2006-12-07 19:39:39 +00001045//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +00001046// ModulePassManager implementation
1047
1048/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +00001049/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001050/// is not manageable by this manager.
1051bool
Devang Pateld65e9e92006-11-08 01:31:28 +00001052ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001053
1054 // If P is FunctionPass then use function pass maanager.
1055 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1056
Devang Patel640c5bb2006-12-08 22:30:11 +00001057 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001058
Devang Patel4949fe02006-12-07 22:34:21 +00001059 // If active manager exists then clear its analysis info.
1060 if (activeFunctionPassManager)
1061 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001062
Devang Patel4949fe02006-12-07 22:34:21 +00001063 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001064 activeFunctionPassManager =
1065 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001066
1067 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001068 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001069
Devang Patel9c6290c2006-12-12 22:02:16 +00001070 // Inherit top level manager
1071 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001072
1073 // Add new manager into top level manager's indirect passes list
1074 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1075 assert (PMD && "Manager is not Pass Manager");
1076 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001077
Devang Patel4949fe02006-12-07 22:34:21 +00001078 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001079 if (!activeFunctionPassManager->addPass(FP))
1080 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001081 }
Devang Patelbc03f132006-12-07 23:55:10 +00001082
1083 if (!ForcedLastUses.empty())
1084 TPM->setLastUser(ForcedLastUses, this);
1085
Devang Patel05e1a972006-11-07 22:03:15 +00001086 return true;
1087 }
1088
1089 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1090 if (!MP)
1091 return false;
1092
Devang Patel3c8eb622006-11-07 22:56:50 +00001093 // If this pass does not preserve anlysis that is used by other passes
1094 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001095 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001096 return false;
1097
Devang Patel8cad70d2006-11-11 01:51:02 +00001098 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001099 // If active manager exists then clear its analysis info.
1100 if (activeFunctionPassManager) {
1101 activeFunctionPassManager->initializeAnalysisInfo();
1102 activeFunctionPassManager = NULL;
1103 }
1104
Devang Patel05e1a972006-11-07 22:03:15 +00001105 return true;
1106}
1107
1108
1109/// Execute all of the passes scheduled for execution by invoking
1110/// runOnModule method. Keep track of whether any of the passes modifies
1111/// the module, and if so, return true.
1112bool
1113ModulePassManager_New::runOnModule(Module &M) {
1114 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001115 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001116
Devang Patel8cad70d2006-11-11 01:51:02 +00001117 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1118 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001119 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +00001120 initializeAnalysisImpl(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001121 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1122 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001123 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001124 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001125 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001126 }
1127 return Changed;
1128}
1129
Devang Patela1514cb2006-12-07 19:39:39 +00001130//===----------------------------------------------------------------------===//
1131// PassManagerImpl implementation
1132
Devang Patelc290c8a2006-11-07 22:23:34 +00001133// PassManager_New implementation
1134/// Add P into active pass manager or use new module pass manager to
1135/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001136bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001137
Devang Patel6c9f5482006-11-11 00:42:16 +00001138 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001139 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001140 // Inherit top level manager
1141 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001142
1143 // This top level manager is going to manage activeManager.
1144 // Set up analysis resolver to connect them.
1145 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1146 activeManager->setResolver(AR);
1147
Devang Patel5bbeb492006-12-08 22:47:25 +00001148 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001149 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001150 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001151 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001152}
1153
1154/// run - Execute all of the passes scheduled for execution. Keep track of
1155/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001156bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001157
Devang Patelc290c8a2006-11-07 22:23:34 +00001158 bool Changed = false;
Devang Patel5bbeb492006-12-08 22:47:25 +00001159 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1160 E = passManagersEnd(); I != E; ++I) {
1161 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1162 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001163 }
1164 return Changed;
1165}
Devang Patel376fefa2006-11-08 10:29:57 +00001166
Devang Patela1514cb2006-12-07 19:39:39 +00001167//===----------------------------------------------------------------------===//
1168// PassManager implementation
1169
Devang Patel376fefa2006-11-08 10:29:57 +00001170/// Create new pass manager
1171PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001172 PM = new PassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001173 // PM is the top level manager
1174 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001175}
1176
1177/// add - Add a pass to the queue of passes to run. This passes ownership of
1178/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1179/// will be destroyed as well, so there is no need to delete the pass. This
1180/// implies that all passes MUST be allocated with 'new'.
1181void
1182PassManager_New::add(Pass *P) {
1183 PM->add(P);
1184}
1185
1186/// run - Execute all of the passes scheduled for execution. Keep track of
1187/// whether any of the passes modifies the module, and if so, return true.
1188bool
1189PassManager_New::run(Module &M) {
1190 return PM->run(M);
1191}
1192