blob: 8ca466cfa8647b034545fc9671e762b318d7727d [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();
Devang Patelb3900322006-12-07 21:02:08 +0000203
204 // Include immutable passes into AvailableAnalysis vector.
205 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
206 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
207 E = ImmutablePasses.end(); I != E; ++I)
208 recordAvailableAnalysis(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000209 }
210
Devang Patel1d6267c2006-12-07 23:05:44 +0000211 /// Populate RequiredPasses with the analysis pass that are required by
212 /// pass P.
213 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
214 Pass *P);
215
216 /// All Required analyses should be available to the pass as it runs! Here
217 /// we fill in the AnalysisImpls member of the pass so that it can
218 /// successfully use the getAnalysis() method to retrieve the
219 /// implementations it needs.
220 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000221
Devang Patel640c5bb2006-12-08 22:30:11 +0000222 /// Find the pass that implements Analysis AID. If desired pass is not found
223 /// then return NULL.
224 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
225
Devang Patel8cad70d2006-11-11 01:51:02 +0000226 inline std::vector<Pass *>::iterator passVectorBegin() {
227 return PassVector.begin();
228 }
229
230 inline std::vector<Pass *>::iterator passVectorEnd() {
231 return PassVector.end();
232 }
233
Devang Patelf3827bc2006-12-07 19:54:15 +0000234 // Access toplevel manager
235 PMTopLevelManager *getTopLevelManager() { return TPM; }
236 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
237
Devang Patel4c36e6b2006-12-07 23:24:58 +0000238 unsigned getDepth() { return Depth; }
239
Devang Patelbc03f132006-12-07 23:55:10 +0000240protected:
241
242 // Collection of pass whose last user asked this manager to claim
243 // last use. If a FunctionPass F is the last user of ModulePass info M
244 // then the F's manager, not F, records itself as a last user of M.
245 std::vector<Pass *> ForcedLastUses;
246
247 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000248 PMTopLevelManager *TPM;
249
Devang Patela9844592006-11-11 01:31:05 +0000250private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000251 // Set of available Analysis. This information is used while scheduling
252 // pass. If a pass requires an analysis which is not not available then
253 // equired analysis pass is scheduled to run before the pass itself is
254 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000255 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000256
257 // Collection of pass that are managed by this manager
258 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000259
Devang Patel4c36e6b2006-12-07 23:24:58 +0000260 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000261};
262
Devang Patelca58e352006-11-08 10:05:38 +0000263/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
264/// pass together and sequence them to process one basic block before
265/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000266class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000267 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000268
269public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000270 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000271
272 /// Add a pass into a passmanager queue.
273 bool addPass(Pass *p);
274
275 /// Execute all of the passes scheduled for execution. Keep track of
276 /// whether any of the passes modifies the function, and if so, return true.
277 bool runOnFunction(Function &F);
278
Devang Patelf9d96b92006-12-07 19:57:52 +0000279 /// Pass Manager itself does not invalidate any analysis info.
280 void getAnalysisUsage(AnalysisUsage &Info) const {
281 Info.setPreservesAll();
282 }
283
Devang Patel475c4532006-12-08 00:59:05 +0000284 bool doInitialization(Module &M);
285 bool doInitialization(Function &F);
286 bool doFinalization(Module &M);
287 bool doFinalization(Function &F);
288
Devang Patelca58e352006-11-08 10:05:38 +0000289};
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(ModuleProvider *P, int D) :
300 PMDataManager(D) { /* TODO */ }
301 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000302 activeBBPassManager = NULL;
303 }
Devang Patel4e12f862006-11-08 10:44:40 +0000304 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000305
Devang Patelabcd1d32006-12-07 21:27:23 +0000306 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000307
Devang Patelfa971cd2006-12-08 23:57:43 +0000308 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000309
310 // P is a immutable pass then it will be managed by this
311 // top level manager. Set up analysis resolver to connect them.
312 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
313 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000314 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000315 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000316 recordAvailableAnalysis(IP);
Devang Patelfa971cd2006-12-08 23:57:43 +0000317 }
318 else
319 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000320 }
321
Devang Patelca58e352006-11-08 10:05:38 +0000322 /// add - Add a pass to the queue of passes to run. This passes
323 /// ownership of the Pass to the PassManager. When the
324 /// PassManager_X is destroyed, the pass will be destroyed as well, so
325 /// there is no need to delete the pass. (TODO delete passes.)
326 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000327 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000328 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000329 }
Devang Patelca58e352006-11-08 10:05:38 +0000330
331 /// Add pass into the pass manager queue.
332 bool addPass(Pass *P);
333
334 /// Execute all of the passes scheduled for execution. Keep
335 /// track of whether any of the passes modifies the function, and if
336 /// so, return true.
337 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000338 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000339 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000340
Devang Patelff631ae2006-11-15 01:27:05 +0000341 /// doInitialization - Run all of the initializers for the function passes.
342 ///
343 bool doInitialization(Module &M);
344
345 /// doFinalization - Run all of the initializers for the function passes.
346 ///
347 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000348
349 /// Pass Manager itself does not invalidate any analysis info.
350 void getAnalysisUsage(AnalysisUsage &Info) const {
351 Info.setPreservesAll();
352 }
353
Devang Patelca58e352006-11-08 10:05:38 +0000354private:
Devang Patelca58e352006-11-08 10:05:38 +0000355 // Active Pass Managers
356 BasicBlockPassManager_New *activeBBPassManager;
357};
358
359/// ModulePassManager_New manages ModulePasses and function pass managers.
360/// It batches all Module passes passes and function pass managers together and
361/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000362class ModulePassManager_New : public Pass,
363 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000364
365public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000366 ModulePassManager_New(int D) : PMDataManager(D) {
367 activeFunctionPassManager = NULL;
368 }
Devang Patelca58e352006-11-08 10:05:38 +0000369
370 /// Add a pass into a passmanager queue.
371 bool addPass(Pass *p);
372
373 /// run - Execute all of the passes scheduled for execution. Keep track of
374 /// whether any of the passes modifies the module, and if so, return true.
375 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000376
Devang Patelf9d96b92006-12-07 19:57:52 +0000377 /// Pass Manager itself does not invalidate any analysis info.
378 void getAnalysisUsage(AnalysisUsage &Info) const {
379 Info.setPreservesAll();
380 }
381
Devang Patelca58e352006-11-08 10:05:38 +0000382private:
Devang Patelca58e352006-11-08 10:05:38 +0000383 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000384 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000385};
386
Devang Patel376fefa2006-11-08 10:29:57 +0000387/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000388class PassManagerImpl_New : public Pass,
389 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000390 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000391
392public:
393
Devang Patel4c36e6b2006-12-07 23:24:58 +0000394 PassManagerImpl_New(int D) : PMDataManager(D) {}
395
Devang Patel376fefa2006-11-08 10:29:57 +0000396 /// add - Add a pass to the queue of passes to run. This passes ownership of
397 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
398 /// will be destroyed as well, so there is no need to delete the pass. This
399 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000400 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000401 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000402 }
Devang Patel376fefa2006-11-08 10:29:57 +0000403
404 /// run - Execute all of the passes scheduled for execution. Keep track of
405 /// whether any of the passes modifies the module, and if so, return true.
406 bool run(Module &M);
407
Devang Patelf9d96b92006-12-07 19:57:52 +0000408 /// Pass Manager itself does not invalidate any analysis info.
409 void getAnalysisUsage(AnalysisUsage &Info) const {
410 Info.setPreservesAll();
411 }
412
Devang Patelabcd1d32006-12-07 21:27:23 +0000413 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000414
Devang Patelfa971cd2006-12-08 23:57:43 +0000415 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000416
417 // P is a immutable pass and it will be managed by this
418 // top level manager. Set up analysis resolver to connect them.
419 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
420 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000421 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000422 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000423 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000424 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000425 else
426 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000427 }
428
Devang Patel376fefa2006-11-08 10:29:57 +0000429private:
430
Devang Patelde124182006-12-07 21:10:57 +0000431 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000432 bool addPass(Pass *p);
433
Devang Patel376fefa2006-11-08 10:29:57 +0000434 // Active Pass Manager
435 ModulePassManager_New *activeManager;
436};
437
Devang Patelca58e352006-11-08 10:05:38 +0000438} // End of llvm namespace
439
Devang Patela1514cb2006-12-07 19:39:39 +0000440//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000441// PMTopLevelManager implementation
442
443/// Set pass P as the last user of the given analysis passes.
444void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
445 Pass *P) {
446
447 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
448 E = AnalysisPasses.end(); I != E; ++I) {
449 Pass *AP = *I;
450 LastUser[AP] = P;
451 // If AP is the last user of other passes then make P last user of
452 // such passes.
453 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
454 LUE = LastUser.end(); LUI != LUE; ++LUI) {
455 if (LUI->second == AP)
456 LastUser[LUI->first] = P;
457 }
458 }
459
460}
461
462/// Collect passes whose last user is P
463void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
464 Pass *P) {
465 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
466 LUE = LastUser.end(); LUI != LUE; ++LUI)
467 if (LUI->second == P)
468 LastUses.push_back(LUI->first);
469}
470
471/// Schedule pass P for execution. Make sure that passes required by
472/// P are run before P is run. Update analysis info maintained by
473/// the manager. Remove dead passes. This is a recursive function.
474void PMTopLevelManager::schedulePass(Pass *P) {
475
476 // TODO : Allocate function manager for this pass, other wise required set
477 // may be inserted into previous function manager
478
479 AnalysisUsage AnUsage;
480 P->getAnalysisUsage(AnUsage);
481 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
482 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
483 E = RequiredSet.end(); I != E; ++I) {
484
485 Pass *AnalysisPass = findAnalysisPass(*I);
486 if (!AnalysisPass) {
487 // Schedule this analysis run first.
488 AnalysisPass = (*I)->createPass();
489 schedulePass(AnalysisPass);
490 }
491 }
492
493 // Now all required passes are available.
494 addTopLevelPass(P);
495}
496
497/// Find the pass that implements Analysis AID. Search immutable
498/// passes and all pass managers. If desired pass is not found
499/// then return NULL.
500Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
501
502 Pass *P = NULL;
503 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
504 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
505 const PassInfo *PI = (*I)->getPassInfo();
506 if (PI == AID)
507 P = *I;
508
509 // If Pass not found then check the interfaces implemented by Immutable Pass
510 if (!P) {
511 const std::vector<const PassInfo*> &ImmPI =
512 PI->getInterfacesImplemented();
513 for (unsigned Index = 0, End = ImmPI.size();
514 P == NULL && Index != End; ++Index)
515 if (ImmPI[Index] == AID)
516 P = *I;
517 }
518 }
519
520 // Check pass managers
521 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
522 E = PassManagers.end(); P == NULL && I != E; ++I)
523 P = (*I)->getResolver()->getAnalysisToUpdate(AID, false);
524
525 // Check other pass managers
526 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
527 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
528 P = (*I)->findAnalysisPass(AID, false);
529
530 return P;
531}
532
533//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000534// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000535
Devang Pateld65e9e92006-11-08 01:31:28 +0000536/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000537/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000538bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000539
Devang Patel8f677ce2006-12-07 18:47:25 +0000540 // TODO
541 // If this pass is not preserving information that is required by a
542 // pass maintained by higher level pass manager then do not insert
543 // this pass into current manager. Use new manager. For example,
544 // For example, If FunctionPass F is not preserving ModulePass Info M1
545 // that is used by another ModulePass M2 then do not insert F in
546 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000547 return true;
548}
549
Devang Patel643676c2006-11-11 01:10:19 +0000550/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000551void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000552
Devang Patel643676c2006-11-11 01:10:19 +0000553 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000554 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000555
Devang Patele9976aa2006-12-07 19:33:53 +0000556 //This pass is the current implementation of all of the interfaces it
557 //implements as well.
558 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
559 for (unsigned i = 0, e = II.size(); i != e; ++i)
560 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000561 }
562}
563
Devang Patelf68a3492006-11-07 22:35:17 +0000564/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000565void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000566 AnalysisUsage AnUsage;
567 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000568
Devang Patel2e169c32006-12-07 20:03:49 +0000569 if (AnUsage.getPreservesAll())
570 return;
571
572 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000573 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000574 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000575 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000576 PreservedSet.end()) {
577 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000578 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000579 AvailableAnalysis.erase(J);
580 }
581 }
Devang Patelf68a3492006-11-07 22:35:17 +0000582}
583
Devang Patelca189262006-11-14 03:05:08 +0000584/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000585void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000586
587 std::vector<Pass *> DeadPasses;
588 TPM->collectLastUses(DeadPasses, P);
589
590 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
591 E = DeadPasses.end(); I != E; ++I) {
592 (*I)->releaseMemory();
593
594 std::map<AnalysisID, Pass*>::iterator Pos =
595 AvailableAnalysis.find((*I)->getPassInfo());
596
Devang Patel475c4532006-12-08 00:59:05 +0000597 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000598 if (Pos != AvailableAnalysis.end())
599 AvailableAnalysis.erase(Pos);
600 }
Devang Patelca189262006-11-14 03:05:08 +0000601}
602
Devang Patel8f677ce2006-12-07 18:47:25 +0000603/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000604/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000605void PMDataManager::addPassToManager(Pass *P,
606 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000607
Devang Pateld440cd92006-12-08 23:53:00 +0000608 // This manager is going to manage pass P. Set up analysis resolver
609 // to connect them.
610 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
611 P->setResolver(AR);
612
Devang Patel90b05e02006-11-11 02:04:19 +0000613 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000614
615 // At the moment, this pass is the last user of all required passes.
616 std::vector<Pass *> LastUses;
617 std::vector<Pass *> RequiredPasses;
618 unsigned PDepth = this->getDepth();
619
620 collectRequiredAnalysisPasses(RequiredPasses, P);
621 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
622 E = RequiredPasses.end(); I != E; ++I) {
623 Pass *PRequired = *I;
624 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000625
626 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
627 RDepth = DM.getDepth();
628
Devang Patelbc03f132006-12-07 23:55:10 +0000629 if (PDepth == RDepth)
630 LastUses.push_back(PRequired);
631 else if (PDepth > RDepth) {
632 // Let the parent claim responsibility of last use
633 ForcedLastUses.push_back(PRequired);
634 } else {
635 // Note : This feature is not yet implemented
636 assert (0 &&
637 "Unable to handle Pass that requires lower level Analysis pass");
638 }
639 }
640
641 if (!LastUses.empty())
642 TPM->setLastUser(LastUses, P);
643
Devang Patel17bff0d2006-12-07 22:09:36 +0000644 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000645 // Remove the analysis not preserved by this pass
646 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000647 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000648 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000649
650 // Add pass
651 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000652}
653
Devang Patel1d6267c2006-12-07 23:05:44 +0000654/// Populate RequiredPasses with the analysis pass that are required by
655/// pass P.
656void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
657 Pass *P) {
658 AnalysisUsage AnUsage;
659 P->getAnalysisUsage(AnUsage);
660 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
661 for (std::vector<AnalysisID>::const_iterator
662 I = RequiredSet.begin(), E = RequiredSet.end();
663 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000664 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000665 assert (AnalysisPass && "Analysis pass is not available");
666 RP.push_back(AnalysisPass);
667 }
668}
669
Devang Patel07f4f582006-11-14 21:49:36 +0000670// All Required analyses should be available to the pass as it runs! Here
671// we fill in the AnalysisImpls member of the pass so that it can
672// successfully use the getAnalysis() method to retrieve the
673// implementations it needs.
674//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000675void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000676 AnalysisUsage AnUsage;
677 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000678
679 for (std::vector<const PassInfo *>::const_iterator
680 I = AnUsage.getRequiredSet().begin(),
681 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000682 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000683 if (Impl == 0)
684 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000685 AnalysisResolver_New *AR = P->getResolver();
686 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000687 }
688}
689
Devang Patel640c5bb2006-12-08 22:30:11 +0000690/// Find the pass that implements Analysis AID. If desired pass is not found
691/// then return NULL.
692Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
693
694 // Check if AvailableAnalysis map has one entry.
695 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
696
697 if (I != AvailableAnalysis.end())
698 return I->second;
699
700 // Search Parents through TopLevelManager
701 if (SearchParent)
702 return TPM->findAnalysisPass(AID);
703
Devang Patel9d759b82006-12-09 00:09:12 +0000704 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000705}
706
Devang Patel9bdf7d42006-12-08 23:28:54 +0000707
708//===----------------------------------------------------------------------===//
709// NOTE: Is this the right place to define this method ?
710// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
711Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
712 return PM.findAnalysisPass(ID, dir);
713}
714
Devang Patela1514cb2006-12-07 19:39:39 +0000715//===----------------------------------------------------------------------===//
716// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000717
Devang Pateld65e9e92006-11-08 01:31:28 +0000718/// Add pass P into PassVector and return true. If this pass is not
719/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000720bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000721BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000722
723 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
724 if (!BP)
725 return false;
726
Devang Patel3c8eb622006-11-07 22:56:50 +0000727 // If this pass does not preserve anlysis that is used by other passes
728 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000729 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000730 return false;
731
Devang Patel8cad70d2006-11-11 01:51:02 +0000732 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000733
Devang Patel6e5a1132006-11-07 21:31:57 +0000734 return true;
735}
736
737/// Execute all of the passes scheduled for execution by invoking
738/// runOnBasicBlock method. Keep track of whether any of the passes modifies
739/// the function, and if so, return true.
740bool
741BasicBlockPassManager_New::runOnFunction(Function &F) {
742
Devang Patele9585592006-12-08 01:38:28 +0000743 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000744 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000745
Devang Patel6e5a1132006-11-07 21:31:57 +0000746 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000747 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
748 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000749 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000750
Devang Patel6e5a1132006-11-07 21:31:57 +0000751 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
752 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000753 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000754 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000755 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000756 }
Devang Patele9585592006-12-08 01:38:28 +0000757 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000758}
759
Devang Patel475c4532006-12-08 00:59:05 +0000760// Implement doInitialization and doFinalization
761inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
762 bool Changed = false;
763
764 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
765 e = passVectorEnd(); itr != e; ++itr) {
766 Pass *P = *itr;
767 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
768 Changed |= BP->doInitialization(M);
769 }
770
771 return Changed;
772}
773
774inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
775 bool Changed = false;
776
777 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
778 e = passVectorEnd(); itr != e; ++itr) {
779 Pass *P = *itr;
780 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
781 Changed |= BP->doFinalization(M);
782 }
783
784 return Changed;
785}
786
787inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
788 bool Changed = false;
789
790 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
791 e = passVectorEnd(); itr != e; ++itr) {
792 Pass *P = *itr;
793 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
794 Changed |= BP->doInitialization(F);
795 }
796
797 return Changed;
798}
799
800inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
801 bool Changed = false;
802
803 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
804 e = passVectorEnd(); itr != e; ++itr) {
805 Pass *P = *itr;
806 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
807 Changed |= BP->doFinalization(F);
808 }
809
810 return Changed;
811}
812
813
Devang Patela1514cb2006-12-07 19:39:39 +0000814//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000815// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000816
Devang Patel4e12f862006-11-08 10:44:40 +0000817/// Create new Function pass manager
818FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000819 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000820}
821
Devang Patel1f653682006-12-08 18:57:16 +0000822FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
823 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000824 // FPM is the top level manager.
825 FPM->setTopLevelManager(FPM);
Devang Patel1f653682006-12-08 18:57:16 +0000826 MP = P;
827}
828
Devang Patel4e12f862006-11-08 10:44:40 +0000829/// add - Add a pass to the queue of passes to run. This passes
830/// ownership of the Pass to the PassManager. When the
831/// PassManager_X is destroyed, the pass will be destroyed as well, so
832/// there is no need to delete the pass. (TODO delete passes.)
833/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000834void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000835 FPM->add(P);
836}
837
838/// Execute all of the passes scheduled for execution. Keep
839/// track of whether any of the passes modifies the function, and if
840/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000841bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000842 return FPM->runOnModule(M);
843}
844
Devang Patel9f3083e2006-11-15 19:39:54 +0000845/// run - 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.
848///
849bool FunctionPassManager_New::run(Function &F) {
850 std::string errstr;
851 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000852 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000853 abort();
854 }
Devang Patel272908d2006-12-08 22:57:48 +0000855 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000856}
857
858
Devang Patelff631ae2006-11-15 01:27:05 +0000859/// doInitialization - Run all of the initializers for the function passes.
860///
861bool FunctionPassManager_New::doInitialization() {
862 return FPM->doInitialization(*MP->getModule());
863}
864
865/// doFinalization - Run all of the initializers for the function passes.
866///
867bool FunctionPassManager_New::doFinalization() {
868 return FPM->doFinalization(*MP->getModule());
869}
870
Devang Patela1514cb2006-12-07 19:39:39 +0000871//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000872// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000873
Devang Patel0c2012f2006-11-07 21:49:50 +0000874/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
875/// either use it into active basic block pass manager or create new basic
876/// block pass manager to handle pass P.
877bool
Devang Patel4e12f862006-11-08 10:44:40 +0000878FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000879
880 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
881 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
882
Devang Patel4949fe02006-12-07 22:34:21 +0000883 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000884
Devang Patel4949fe02006-12-07 22:34:21 +0000885 // If active manager exists then clear its analysis info.
886 if (activeBBPassManager)
887 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000888
Devang Patel4949fe02006-12-07 22:34:21 +0000889 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000890 activeBBPassManager =
891 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +0000892 // Inherit top level manager
893 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +0000894
895 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +0000896 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +0000897
898 // Add new manager into top level manager's indirect passes list
899 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
900 assert (PMD && "Manager is not Pass Manager");
901 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +0000902
903 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000904 if (!activeBBPassManager->addPass(BP))
905 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000906 }
Devang Patelbc03f132006-12-07 23:55:10 +0000907
908 if (!ForcedLastUses.empty())
909 TPM->setLastUser(ForcedLastUses, this);
910
Devang Patel0c2012f2006-11-07 21:49:50 +0000911 return true;
912 }
913
914 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
915 if (!FP)
916 return false;
917
Devang Patel3c8eb622006-11-07 22:56:50 +0000918 // If this pass does not preserve anlysis that is used by other passes
919 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000920 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000921 return false;
922
Devang Patel8cad70d2006-11-11 01:51:02 +0000923 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000924
925 // If active manager exists then clear its analysis info.
926 if (activeBBPassManager) {
927 activeBBPassManager->initializeAnalysisInfo();
928 activeBBPassManager = NULL;
929 }
930
Devang Patel0c2012f2006-11-07 21:49:50 +0000931 return true;
932}
933
934/// Execute all of the passes scheduled for execution by invoking
935/// runOnFunction method. Keep track of whether any of the passes modifies
936/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000937bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000938
Devang Patel0e29e292006-12-08 19:04:09 +0000939 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000940 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000941
Devang Patel0c2012f2006-11-07 21:49:50 +0000942 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +0000943 this->runOnFunction(*I);
944
Devang Patel0e29e292006-12-08 19:04:09 +0000945 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +0000946}
947
Devang Patel9f3083e2006-11-15 19:39:54 +0000948/// Execute all of the passes scheduled for execution by invoking
949/// runOnFunction method. Keep track of whether any of the passes modifies
950/// the function, and if so, return true.
951bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
952
953 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000954 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000955
956 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
957 e = passVectorEnd(); itr != e; ++itr) {
958 Pass *P = *itr;
959
Devang Patel9f3083e2006-11-15 19:39:54 +0000960 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
961 Changed |= FP->runOnFunction(F);
962 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000963 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000964 removeDeadPasses(P);
965 }
966 return Changed;
967}
968
969
Devang Patelff631ae2006-11-15 01:27:05 +0000970inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
971 bool Changed = false;
972
973 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
974 e = passVectorEnd(); itr != e; ++itr) {
975 Pass *P = *itr;
976
977 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
978 Changed |= FP->doInitialization(M);
979 }
980
981 return Changed;
982}
983
984inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
985 bool Changed = false;
986
987 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
988 e = passVectorEnd(); itr != e; ++itr) {
989 Pass *P = *itr;
990
991 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
992 Changed |= FP->doFinalization(M);
993 }
994
Devang Patelff631ae2006-11-15 01:27:05 +0000995 return Changed;
996}
997
Devang Patel272908d2006-12-08 22:57:48 +0000998// Execute all the passes managed by this top level manager.
999// Return true if any function is modified by a pass.
1000bool FunctionPassManagerImpl_New::run(Function &F) {
1001
1002 bool Changed = false;
1003 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1004 E = passManagersEnd(); I != E; ++I) {
1005 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
1006 Changed |= FP->runOnFunction(F);
1007 }
1008 return Changed;
1009}
1010
Devang Patela1514cb2006-12-07 19:39:39 +00001011//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +00001012// ModulePassManager implementation
1013
1014/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +00001015/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001016/// is not manageable by this manager.
1017bool
Devang Pateld65e9e92006-11-08 01:31:28 +00001018ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001019
1020 // If P is FunctionPass then use function pass maanager.
1021 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1022
Devang Patel640c5bb2006-12-08 22:30:11 +00001023 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001024
Devang Patel4949fe02006-12-07 22:34:21 +00001025 // If active manager exists then clear its analysis info.
1026 if (activeFunctionPassManager)
1027 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001028
Devang Patel4949fe02006-12-07 22:34:21 +00001029 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001030 activeFunctionPassManager =
1031 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001032
1033 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001034 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001035
Devang Patel9c6290c2006-12-12 22:02:16 +00001036 // Inherit top level manager
1037 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001038
1039 // Add new manager into top level manager's indirect passes list
1040 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1041 assert (PMD && "Manager is not Pass Manager");
1042 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001043
Devang Patel4949fe02006-12-07 22:34:21 +00001044 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001045 if (!activeFunctionPassManager->addPass(FP))
1046 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001047 }
Devang Patelbc03f132006-12-07 23:55:10 +00001048
1049 if (!ForcedLastUses.empty())
1050 TPM->setLastUser(ForcedLastUses, this);
1051
Devang Patel05e1a972006-11-07 22:03:15 +00001052 return true;
1053 }
1054
1055 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1056 if (!MP)
1057 return false;
1058
Devang Patel3c8eb622006-11-07 22:56:50 +00001059 // If this pass does not preserve anlysis that is used by other passes
1060 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001061 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001062 return false;
1063
Devang Patel8cad70d2006-11-11 01:51:02 +00001064 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001065 // If active manager exists then clear its analysis info.
1066 if (activeFunctionPassManager) {
1067 activeFunctionPassManager->initializeAnalysisInfo();
1068 activeFunctionPassManager = NULL;
1069 }
1070
Devang Patel05e1a972006-11-07 22:03:15 +00001071 return true;
1072}
1073
1074
1075/// Execute all of the passes scheduled for execution by invoking
1076/// runOnModule method. Keep track of whether any of the passes modifies
1077/// the module, and if so, return true.
1078bool
1079ModulePassManager_New::runOnModule(Module &M) {
1080 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001081 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001082
Devang Patel8cad70d2006-11-11 01:51:02 +00001083 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1084 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001085 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +00001086
Devang Patel05e1a972006-11-07 22:03:15 +00001087 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1088 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001089 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001090 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001091 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001092 }
1093 return Changed;
1094}
1095
Devang Patela1514cb2006-12-07 19:39:39 +00001096//===----------------------------------------------------------------------===//
1097// PassManagerImpl implementation
1098
Devang Patelc290c8a2006-11-07 22:23:34 +00001099// PassManager_New implementation
1100/// Add P into active pass manager or use new module pass manager to
1101/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001102bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001103
Devang Patel6c9f5482006-11-11 00:42:16 +00001104 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001105 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001106 // Inherit top level manager
1107 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001108
1109 // This top level manager is going to manage activeManager.
1110 // Set up analysis resolver to connect them.
1111 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1112 activeManager->setResolver(AR);
1113
Devang Patel5bbeb492006-12-08 22:47:25 +00001114 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001115 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001116 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001117 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001118}
1119
1120/// run - Execute all of the passes scheduled for execution. Keep track of
1121/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001122bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001123
Devang Patelc290c8a2006-11-07 22:23:34 +00001124 bool Changed = false;
Devang Patel5bbeb492006-12-08 22:47:25 +00001125 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1126 E = passManagersEnd(); I != E; ++I) {
1127 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1128 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001129 }
1130 return Changed;
1131}
Devang Patel376fefa2006-11-08 10:29:57 +00001132
Devang Patela1514cb2006-12-07 19:39:39 +00001133//===----------------------------------------------------------------------===//
1134// PassManager implementation
1135
Devang Patel376fefa2006-11-08 10:29:57 +00001136/// Create new pass manager
1137PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001138 PM = new PassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001139 // PM is the top level manager
1140 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001141}
1142
1143/// add - Add a pass to the queue of passes to run. This passes ownership of
1144/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1145/// will be destroyed as well, so there is no need to delete the pass. This
1146/// implies that all passes MUST be allocated with 'new'.
1147void
1148PassManager_New::add(Pass *P) {
1149 PM->add(P);
1150}
1151
1152/// run - Execute all of the passes scheduled for execution. Keep track of
1153/// whether any of the passes modifies the module, and if so, return true.
1154bool
1155PassManager_New::run(Module &M) {
1156 return PM->run(M);
1157}
1158