blob: e6f69c07a6a27584da56abe8ca311fd8235a343c [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() {
Devang Patelab97cf42006-12-13 00:09:23 +0000129
130 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
131 E = PassManagers.end(); I != E; ++I)
132 delete *I;
133
134 for (std::vector<ImmutablePass *>::iterator
135 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
136 delete *I;
137
Devang Patelf33f3eb2006-12-07 19:21:29 +0000138 PassManagers.clear();
139 }
140
Devang Patele0eb9d82006-12-07 20:51:18 +0000141 /// Add immutable pass and initialize it.
142 inline void addImmutablePass(ImmutablePass *P) {
143 P->initializePass();
144 ImmutablePasses.push_back(P);
145 }
146
147 inline std::vector<ImmutablePass *>& getImmutablePasses() {
148 return ImmutablePasses;
149 }
150
Devang Patel5bbeb492006-12-08 22:47:25 +0000151 void addPassManager(Pass *Manager) {
152 PassManagers.push_back(Manager);
153 }
154
Devang Patelaf1fca52006-12-08 23:11:43 +0000155 // Add Manager into the list of managers that are not directly
156 // maintained by this top level pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000157 inline void addIndirectPassManager(PMDataManager *Manager) {
158 IndirectPassManagers.push_back(Manager);
Devang Patelaf1fca52006-12-08 23:11:43 +0000159 }
160
Devang Pateleda56172006-12-12 23:34:33 +0000161 // Print passes managed by this top level manager.
162 void dumpPasses();
163
Devang Patelf33f3eb2006-12-07 19:21:29 +0000164private:
165
166 /// Collection of pass managers
167 std::vector<Pass *> PassManagers;
168
Devang Patelaf1fca52006-12-08 23:11:43 +0000169 /// Collection of pass managers that are not directly maintained
170 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000171 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000172
Devang Patelf33f3eb2006-12-07 19:21:29 +0000173 // Map to keep track of last user of the analysis pass.
174 // LastUser->second is the last user of Lastuser->first.
175 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000176
177 /// Immutable passes are managed by top level manager.
178 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000179};
180
Devang Patelf3827bc2006-12-07 19:54:15 +0000181//===----------------------------------------------------------------------===//
182// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000183
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000184/// PMDataManager provides the common place to manage the analysis data
185/// used by pass managers.
186class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000187
188public:
189
Devang Patel4c36e6b2006-12-07 23:24:58 +0000190 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000191 initializeAnalysisInfo();
192 }
193
Devang Patelab97cf42006-12-13 00:09:23 +0000194 virtual ~PMDataManager() {
195
196 for (std::vector<Pass *>::iterator I = PassVector.begin(),
197 E = PassVector.end(); I != E; ++I)
198 delete *I;
199
200 PassVector.clear();
201 }
202
Devang Patela9844592006-11-11 01:31:05 +0000203 /// Return true IFF pass P's required analysis set does not required new
204 /// manager.
205 bool manageablePass(Pass *P);
206
Devang Patela9844592006-11-11 01:31:05 +0000207 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000208 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000209
Devang Patela9844592006-11-11 01:31:05 +0000210 /// Remove Analysis that is not preserved by the pass
211 void removeNotPreservedAnalysis(Pass *P);
212
213 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000214 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000215
Devang Patel8f677ce2006-12-07 18:47:25 +0000216 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000217 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
218 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000219
Devang Patel1d6267c2006-12-07 23:05:44 +0000220 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000221 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000222 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000223 AvailableAnalysis.clear();
224 }
225
Devang Patel1d6267c2006-12-07 23:05:44 +0000226 /// Populate RequiredPasses with the analysis pass that are required by
227 /// pass P.
228 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
229 Pass *P);
230
231 /// All Required analyses should be available to the pass as it runs! Here
232 /// we fill in the AnalysisImpls member of the pass so that it can
233 /// successfully use the getAnalysis() method to retrieve the
234 /// implementations it needs.
235 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000236
Devang Patel640c5bb2006-12-08 22:30:11 +0000237 /// Find the pass that implements Analysis AID. If desired pass is not found
238 /// then return NULL.
239 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
240
Devang Patel8cad70d2006-11-11 01:51:02 +0000241 inline std::vector<Pass *>::iterator passVectorBegin() {
242 return PassVector.begin();
243 }
244
245 inline std::vector<Pass *>::iterator passVectorEnd() {
246 return PassVector.end();
247 }
248
Devang Patelf3827bc2006-12-07 19:54:15 +0000249 // Access toplevel manager
250 PMTopLevelManager *getTopLevelManager() { return TPM; }
251 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
252
Devang Patel4c36e6b2006-12-07 23:24:58 +0000253 unsigned getDepth() { return Depth; }
254
Devang Pateleda56172006-12-12 23:34:33 +0000255 // Print list of passes that are last used by P.
256 void dumpLastUses(Pass *P, unsigned Offset) {
257
258 std::vector<Pass *> LUses;
259
260 assert (TPM && "Top Level Manager is missing");
261 TPM->collectLastUses(LUses, P);
262
263 for (std::vector<Pass *>::iterator I = LUses.begin(),
264 E = LUses.end(); I != E; ++I) {
265 llvm::cerr << "--" << std::string(Offset*2, ' ');
266 (*I)->dumpPassStructure(0);
267 }
268 }
269
Devang Patelbc03f132006-12-07 23:55:10 +0000270protected:
271
272 // Collection of pass whose last user asked this manager to claim
273 // last use. If a FunctionPass F is the last user of ModulePass info M
274 // then the F's manager, not F, records itself as a last user of M.
275 std::vector<Pass *> ForcedLastUses;
276
277 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000278 PMTopLevelManager *TPM;
279
Devang Patela9844592006-11-11 01:31:05 +0000280private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000281 // Set of available Analysis. This information is used while scheduling
282 // pass. If a pass requires an analysis which is not not available then
283 // equired analysis pass is scheduled to run before the pass itself is
284 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000285 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000286
287 // Collection of pass that are managed by this manager
288 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000289
Devang Patel4c36e6b2006-12-07 23:24:58 +0000290 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000291};
292
Devang Patel10c2ca62006-12-12 22:47:13 +0000293//===----------------------------------------------------------------------===//
294// BasicBlockPassManager_New
295//
Devang Patelca58e352006-11-08 10:05:38 +0000296/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
297/// pass together and sequence them to process one basic block before
298/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000299class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000300 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000301
302public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000303 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000304
305 /// Add a pass into a passmanager queue.
306 bool addPass(Pass *p);
307
308 /// Execute all of the passes scheduled for execution. Keep track of
309 /// whether any of the passes modifies the function, and if so, return true.
310 bool runOnFunction(Function &F);
311
Devang Patelf9d96b92006-12-07 19:57:52 +0000312 /// Pass Manager itself does not invalidate any analysis info.
313 void getAnalysisUsage(AnalysisUsage &Info) const {
314 Info.setPreservesAll();
315 }
316
Devang Patel475c4532006-12-08 00:59:05 +0000317 bool doInitialization(Module &M);
318 bool doInitialization(Function &F);
319 bool doFinalization(Module &M);
320 bool doFinalization(Function &F);
321
Devang Pateleda56172006-12-12 23:34:33 +0000322 // Print passes managed by this manager
323 void dumpPassStructure(unsigned Offset) {
324 llvm::cerr << std::string(Offset*2, ' ') << "BasicBLockPass Manager\n";
325 for (std::vector<Pass *>::iterator I = passVectorBegin(),
326 E = passVectorEnd(); I != E; ++I) {
327 (*I)->dumpPassStructure(Offset + 1);
328 dumpLastUses(*I, Offset+1);
329 }
330 }
331
Devang Patelca58e352006-11-08 10:05:38 +0000332};
333
Devang Patel10c2ca62006-12-12 22:47:13 +0000334//===----------------------------------------------------------------------===//
335// FunctionPassManagerImpl_New
336//
Devang Patel4e12f862006-11-08 10:44:40 +0000337/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000338/// It batches all function passes and basic block pass managers together and
339/// sequence them to process one function at a time before processing next
340/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000341class FunctionPassManagerImpl_New : public ModulePass,
342 public PMDataManager,
343 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000344public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000345 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000346 activeBBPassManager = NULL;
347 }
Devang Patel4e12f862006-11-08 10:44:40 +0000348 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000349
Devang Patelabcd1d32006-12-07 21:27:23 +0000350 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000351
Devang Patelfa971cd2006-12-08 23:57:43 +0000352 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000353
354 // P is a immutable pass then it will be managed by this
355 // top level manager. Set up analysis resolver to connect them.
356 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
357 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000358 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000359 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000360 recordAvailableAnalysis(IP);
Devang Patelfa971cd2006-12-08 23:57:43 +0000361 }
362 else
363 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000364 }
365
Devang Patelca58e352006-11-08 10:05:38 +0000366 /// add - Add a pass to the queue of passes to run. This passes
367 /// ownership of the Pass to the PassManager. When the
368 /// PassManager_X is destroyed, the pass will be destroyed as well, so
369 /// there is no need to delete the pass. (TODO delete passes.)
370 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000371 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000372 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000373 }
Devang Patelca58e352006-11-08 10:05:38 +0000374
375 /// Add pass into the pass manager queue.
376 bool addPass(Pass *P);
377
378 /// Execute all of the passes scheduled for execution. Keep
379 /// track of whether any of the passes modifies the function, and if
380 /// so, return true.
381 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000382 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000383 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000384
Devang Patelff631ae2006-11-15 01:27:05 +0000385 /// doInitialization - Run all of the initializers for the function passes.
386 ///
387 bool doInitialization(Module &M);
388
389 /// doFinalization - Run all of the initializers for the function passes.
390 ///
391 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000392
393 /// Pass Manager itself does not invalidate any analysis info.
394 void getAnalysisUsage(AnalysisUsage &Info) const {
395 Info.setPreservesAll();
396 }
397
Devang Pateleda56172006-12-12 23:34:33 +0000398 // Print passes managed by this manager
399 void dumpPassStructure(unsigned Offset) {
400 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
401 for (std::vector<Pass *>::iterator I = passVectorBegin(),
402 E = passVectorEnd(); I != E; ++I) {
403 (*I)->dumpPassStructure(Offset + 1);
404 dumpLastUses(*I, Offset+1);
405 }
406 }
407
Devang Patelca58e352006-11-08 10:05:38 +0000408private:
Devang Patelca58e352006-11-08 10:05:38 +0000409 // Active Pass Managers
410 BasicBlockPassManager_New *activeBBPassManager;
411};
412
Devang Patel10c2ca62006-12-12 22:47:13 +0000413//===----------------------------------------------------------------------===//
414// ModulePassManager_New
415//
Devang Patelca58e352006-11-08 10:05:38 +0000416/// ModulePassManager_New manages ModulePasses and function pass managers.
417/// It batches all Module passes passes and function pass managers together and
418/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000419class ModulePassManager_New : public Pass,
420 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000421
422public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000423 ModulePassManager_New(int D) : PMDataManager(D) {
424 activeFunctionPassManager = NULL;
425 }
Devang Patelca58e352006-11-08 10:05:38 +0000426
427 /// Add a pass into a passmanager queue.
428 bool addPass(Pass *p);
429
430 /// run - Execute all of the passes scheduled for execution. Keep track of
431 /// whether any of the passes modifies the module, and if so, return true.
432 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000433
Devang Patelf9d96b92006-12-07 19:57:52 +0000434 /// Pass Manager itself does not invalidate any analysis info.
435 void getAnalysisUsage(AnalysisUsage &Info) const {
436 Info.setPreservesAll();
437 }
438
Devang Pateleda56172006-12-12 23:34:33 +0000439 // Print passes managed by this manager
440 void dumpPassStructure(unsigned Offset) {
441 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
442 for (std::vector<Pass *>::iterator I = passVectorBegin(),
443 E = passVectorEnd(); I != E; ++I) {
444 (*I)->dumpPassStructure(Offset + 1);
445 dumpLastUses(*I, Offset+1);
446 }
447 }
448
Devang Patelca58e352006-11-08 10:05:38 +0000449private:
Devang Patelca58e352006-11-08 10:05:38 +0000450 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000451 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000452};
453
Devang Patel10c2ca62006-12-12 22:47:13 +0000454//===----------------------------------------------------------------------===//
455// PassManagerImpl_New
456//
457/// PassManagerImpl_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000458class PassManagerImpl_New : public Pass,
459 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000460 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000461
462public:
463
Devang Patelad6b7fe2006-12-12 22:57:43 +0000464 PassManagerImpl_New(int D) : PMDataManager(D) {
465 activeManager = NULL;
466 }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000467
Devang Patel376fefa2006-11-08 10:29:57 +0000468 /// add - Add a pass to the queue of passes to run. This passes ownership of
469 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
470 /// will be destroyed as well, so there is no need to delete the pass. This
471 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000472 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000473 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000474 }
Devang Patel376fefa2006-11-08 10:29:57 +0000475
476 /// run - Execute all of the passes scheduled for execution. Keep track of
477 /// whether any of the passes modifies the module, and if so, return true.
478 bool run(Module &M);
479
Devang Patelf9d96b92006-12-07 19:57:52 +0000480 /// Pass Manager itself does not invalidate any analysis info.
481 void getAnalysisUsage(AnalysisUsage &Info) const {
482 Info.setPreservesAll();
483 }
484
Devang Patelabcd1d32006-12-07 21:27:23 +0000485 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000486
Devang Patelfa971cd2006-12-08 23:57:43 +0000487 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000488
489 // P is a immutable pass and it will be managed by this
490 // top level manager. Set up analysis resolver to connect them.
491 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
492 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000493 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000494 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000495 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000496 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000497 else
498 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000499 }
500
Devang Patel376fefa2006-11-08 10:29:57 +0000501private:
502
Devang Patelde124182006-12-07 21:10:57 +0000503 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000504 bool addPass(Pass *p);
505
Devang Patel376fefa2006-11-08 10:29:57 +0000506 // Active Pass Manager
507 ModulePassManager_New *activeManager;
508};
509
Devang Patelca58e352006-11-08 10:05:38 +0000510} // End of llvm namespace
511
Devang Patela1514cb2006-12-07 19:39:39 +0000512//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000513// PMTopLevelManager implementation
514
515/// Set pass P as the last user of the given analysis passes.
516void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
517 Pass *P) {
518
519 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
520 E = AnalysisPasses.end(); I != E; ++I) {
521 Pass *AP = *I;
522 LastUser[AP] = P;
523 // If AP is the last user of other passes then make P last user of
524 // such passes.
525 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
526 LUE = LastUser.end(); LUI != LUE; ++LUI) {
527 if (LUI->second == AP)
528 LastUser[LUI->first] = P;
529 }
530 }
531
532}
533
534/// Collect passes whose last user is P
535void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
536 Pass *P) {
537 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
538 LUE = LastUser.end(); LUI != LUE; ++LUI)
539 if (LUI->second == P)
540 LastUses.push_back(LUI->first);
541}
542
543/// Schedule pass P for execution. Make sure that passes required by
544/// P are run before P is run. Update analysis info maintained by
545/// the manager. Remove dead passes. This is a recursive function.
546void PMTopLevelManager::schedulePass(Pass *P) {
547
548 // TODO : Allocate function manager for this pass, other wise required set
549 // may be inserted into previous function manager
550
551 AnalysisUsage AnUsage;
552 P->getAnalysisUsage(AnUsage);
553 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
554 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
555 E = RequiredSet.end(); I != E; ++I) {
556
557 Pass *AnalysisPass = findAnalysisPass(*I);
558 if (!AnalysisPass) {
559 // Schedule this analysis run first.
560 AnalysisPass = (*I)->createPass();
561 schedulePass(AnalysisPass);
562 }
563 }
564
565 // Now all required passes are available.
566 addTopLevelPass(P);
567}
568
569/// Find the pass that implements Analysis AID. Search immutable
570/// passes and all pass managers. If desired pass is not found
571/// then return NULL.
572Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
573
574 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000575 // Check pass managers
576 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
577 E = PassManagers.end(); P == NULL && I != E; ++I) {
578 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
579 assert(PMD && "This is not a PassManager");
580 P = PMD->findAnalysisPass(AID, false);
581 }
582
583 // Check other pass managers
584 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
585 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
586 P = (*I)->findAnalysisPass(AID, false);
587
Devang Patelafb1f3622006-12-12 22:35:25 +0000588 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
589 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
590 const PassInfo *PI = (*I)->getPassInfo();
591 if (PI == AID)
592 P = *I;
593
594 // If Pass not found then check the interfaces implemented by Immutable Pass
595 if (!P) {
596 const std::vector<const PassInfo*> &ImmPI =
597 PI->getInterfacesImplemented();
598 for (unsigned Index = 0, End = ImmPI.size();
599 P == NULL && Index != End; ++Index)
600 if (ImmPI[Index] == AID)
601 P = *I;
602 }
603 }
604
Devang Patelafb1f3622006-12-12 22:35:25 +0000605 return P;
606}
607
Devang Pateleda56172006-12-12 23:34:33 +0000608// Print passes managed by this top level manager.
609void PMTopLevelManager::dumpPasses() {
610
611 // Print out the immutable passes
612 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
613 ImmutablePasses[i]->dumpPassStructure(0);
614 }
615
616 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
617 E = PassManagers.end(); I != E; ++I)
618 (*I)->dumpPassStructure(1);
619
620}
621
Devang Patelafb1f3622006-12-12 22:35:25 +0000622//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000623// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000624
Devang Pateld65e9e92006-11-08 01:31:28 +0000625/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000626/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000627bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000628
Devang Patel8f677ce2006-12-07 18:47:25 +0000629 // TODO
630 // If this pass is not preserving information that is required by a
631 // pass maintained by higher level pass manager then do not insert
632 // this pass into current manager. Use new manager. For example,
633 // For example, If FunctionPass F is not preserving ModulePass Info M1
634 // that is used by another ModulePass M2 then do not insert F in
635 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000636 return true;
637}
638
Devang Patel643676c2006-11-11 01:10:19 +0000639/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000640void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000641
Devang Patel643676c2006-11-11 01:10:19 +0000642 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000643 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000644
Devang Patele9976aa2006-12-07 19:33:53 +0000645 //This pass is the current implementation of all of the interfaces it
646 //implements as well.
647 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
648 for (unsigned i = 0, e = II.size(); i != e; ++i)
649 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000650 }
651}
652
Devang Patelf68a3492006-11-07 22:35:17 +0000653/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000654void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000655 AnalysisUsage AnUsage;
656 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000657
Devang Patel2e169c32006-12-07 20:03:49 +0000658 if (AnUsage.getPreservesAll())
659 return;
660
661 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000662 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000663 E = AvailableAnalysis.end(); I != E; ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000664 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000665 PreservedSet.end()) {
666 // Remove this analysis
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000667 if (!dynamic_cast<ImmutablePass*>(I->second)) {
668 std::map<AnalysisID, Pass*>::iterator J = I++;
669 AvailableAnalysis.erase(J);
670 } else
671 ++I;
672 } else
673 ++I;
Devang Patel349170f2006-11-11 01:24:55 +0000674 }
Devang Patelf68a3492006-11-07 22:35:17 +0000675}
676
Devang Patelca189262006-11-14 03:05:08 +0000677/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000678void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000679
680 std::vector<Pass *> DeadPasses;
681 TPM->collectLastUses(DeadPasses, P);
682
683 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
684 E = DeadPasses.end(); I != E; ++I) {
685 (*I)->releaseMemory();
686
687 std::map<AnalysisID, Pass*>::iterator Pos =
688 AvailableAnalysis.find((*I)->getPassInfo());
689
Devang Patel475c4532006-12-08 00:59:05 +0000690 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000691 if (Pos != AvailableAnalysis.end())
692 AvailableAnalysis.erase(Pos);
693 }
Devang Patelca189262006-11-14 03:05:08 +0000694}
695
Devang Patel8f677ce2006-12-07 18:47:25 +0000696/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000697/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000698void PMDataManager::addPassToManager(Pass *P,
699 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000700
Devang Pateld440cd92006-12-08 23:53:00 +0000701 // This manager is going to manage pass P. Set up analysis resolver
702 // to connect them.
703 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
704 P->setResolver(AR);
705
Devang Patel90b05e02006-11-11 02:04:19 +0000706 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000707
708 // At the moment, this pass is the last user of all required passes.
709 std::vector<Pass *> LastUses;
710 std::vector<Pass *> RequiredPasses;
711 unsigned PDepth = this->getDepth();
712
713 collectRequiredAnalysisPasses(RequiredPasses, P);
714 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
715 E = RequiredPasses.end(); I != E; ++I) {
716 Pass *PRequired = *I;
717 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000718
719 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
720 RDepth = DM.getDepth();
721
Devang Patelbc03f132006-12-07 23:55:10 +0000722 if (PDepth == RDepth)
723 LastUses.push_back(PRequired);
724 else if (PDepth > RDepth) {
725 // Let the parent claim responsibility of last use
726 ForcedLastUses.push_back(PRequired);
727 } else {
728 // Note : This feature is not yet implemented
729 assert (0 &&
730 "Unable to handle Pass that requires lower level Analysis pass");
731 }
732 }
733
734 if (!LastUses.empty())
735 TPM->setLastUser(LastUses, P);
736
Devang Patel17bff0d2006-12-07 22:09:36 +0000737 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000738 // Remove the analysis not preserved by this pass
739 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000740 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000741 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000742
743 // Add pass
744 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000745}
746
Devang Patel1d6267c2006-12-07 23:05:44 +0000747/// Populate RequiredPasses with the analysis pass that are required by
748/// pass P.
749void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
750 Pass *P) {
751 AnalysisUsage AnUsage;
752 P->getAnalysisUsage(AnUsage);
753 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
754 for (std::vector<AnalysisID>::const_iterator
755 I = RequiredSet.begin(), E = RequiredSet.end();
756 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000757 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000758 assert (AnalysisPass && "Analysis pass is not available");
759 RP.push_back(AnalysisPass);
760 }
Devang Patelf58183d2006-12-12 23:09:32 +0000761
762 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
763 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
764 E = IDs.end(); I != E; ++I) {
765 Pass *AnalysisPass = findAnalysisPass(*I, true);
766 assert (AnalysisPass && "Analysis pass is not available");
767 RP.push_back(AnalysisPass);
768 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000769}
770
Devang Patel07f4f582006-11-14 21:49:36 +0000771// All Required analyses should be available to the pass as it runs! Here
772// we fill in the AnalysisImpls member of the pass so that it can
773// successfully use the getAnalysis() method to retrieve the
774// implementations it needs.
775//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000776void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000777 AnalysisUsage AnUsage;
778 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000779
780 for (std::vector<const PassInfo *>::const_iterator
781 I = AnUsage.getRequiredSet().begin(),
782 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000783 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000784 if (Impl == 0)
785 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000786 AnalysisResolver_New *AR = P->getResolver();
787 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000788 }
789}
790
Devang Patel640c5bb2006-12-08 22:30:11 +0000791/// Find the pass that implements Analysis AID. If desired pass is not found
792/// then return NULL.
793Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
794
795 // Check if AvailableAnalysis map has one entry.
796 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
797
798 if (I != AvailableAnalysis.end())
799 return I->second;
800
801 // Search Parents through TopLevelManager
802 if (SearchParent)
803 return TPM->findAnalysisPass(AID);
804
Devang Patel9d759b82006-12-09 00:09:12 +0000805 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000806}
807
Devang Patel9bdf7d42006-12-08 23:28:54 +0000808
809//===----------------------------------------------------------------------===//
810// NOTE: Is this the right place to define this method ?
811// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
812Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
813 return PM.findAnalysisPass(ID, dir);
814}
815
Devang Patela1514cb2006-12-07 19:39:39 +0000816//===----------------------------------------------------------------------===//
817// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000818
Devang Pateld65e9e92006-11-08 01:31:28 +0000819/// Add pass P into PassVector and return true. If this pass is not
820/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000821bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000822BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000823
824 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
825 if (!BP)
826 return false;
827
Devang Patel3c8eb622006-11-07 22:56:50 +0000828 // If this pass does not preserve anlysis that is used by other passes
829 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000830 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000831 return false;
832
Devang Patel8cad70d2006-11-11 01:51:02 +0000833 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000834
Devang Patel6e5a1132006-11-07 21:31:57 +0000835 return true;
836}
837
838/// Execute all of the passes scheduled for execution by invoking
839/// runOnBasicBlock method. Keep track of whether any of the passes modifies
840/// the function, and if so, return true.
841bool
842BasicBlockPassManager_New::runOnFunction(Function &F) {
843
Devang Patel745a6962006-12-12 23:15:28 +0000844 if (F.isExternal())
845 return false;
846
Devang Patele9585592006-12-08 01:38:28 +0000847 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000848 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000849
Devang Patel6e5a1132006-11-07 21:31:57 +0000850 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000851 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
852 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000853 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +0000854 initializeAnalysisImpl(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000855 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
856 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000857 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000858 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000859 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000860 }
Devang Patele9585592006-12-08 01:38:28 +0000861 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000862}
863
Devang Patel475c4532006-12-08 00:59:05 +0000864// Implement doInitialization and doFinalization
865inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
866 bool Changed = false;
867
868 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
869 e = passVectorEnd(); itr != e; ++itr) {
870 Pass *P = *itr;
871 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
872 Changed |= BP->doInitialization(M);
873 }
874
875 return Changed;
876}
877
878inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
879 bool Changed = false;
880
881 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
882 e = passVectorEnd(); itr != e; ++itr) {
883 Pass *P = *itr;
884 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
885 Changed |= BP->doFinalization(M);
886 }
887
888 return Changed;
889}
890
891inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
892 bool Changed = false;
893
894 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
895 e = passVectorEnd(); itr != e; ++itr) {
896 Pass *P = *itr;
897 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
898 Changed |= BP->doInitialization(F);
899 }
900
901 return Changed;
902}
903
904inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
905 bool Changed = false;
906
907 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
908 e = passVectorEnd(); itr != e; ++itr) {
909 Pass *P = *itr;
910 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
911 Changed |= BP->doFinalization(F);
912 }
913
914 return Changed;
915}
916
917
Devang Patela1514cb2006-12-07 19:39:39 +0000918//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000919// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000920
Devang Patel4e12f862006-11-08 10:44:40 +0000921/// Create new Function pass manager
Devang Patel1f653682006-12-08 18:57:16 +0000922FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
923 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000924 // FPM is the top level manager.
925 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000926
927 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
928 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
929 FPM->setResolver(AR);
930
931 FPM->addPassManager(FPM);
Devang Patel1f653682006-12-08 18:57:16 +0000932 MP = P;
933}
934
Devang Patelab97cf42006-12-13 00:09:23 +0000935FunctionPassManager_New::~FunctionPassManager_New() {
936 delete FPM;
937}
938
Devang Patel4e12f862006-11-08 10:44:40 +0000939/// add - Add a pass to the queue of passes to run. This passes
940/// ownership of the Pass to the PassManager. When the
941/// PassManager_X is destroyed, the pass will be destroyed as well, so
942/// there is no need to delete the pass. (TODO delete passes.)
943/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000944void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000945 FPM->add(P);
946}
947
948/// Execute all of the passes scheduled for execution. Keep
949/// track of whether any of the passes modifies the function, and if
950/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000951bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000952 return FPM->runOnModule(M);
953}
954
Devang Patel9f3083e2006-11-15 19:39:54 +0000955/// run - Execute all of the passes scheduled for execution. Keep
956/// track of whether any of the passes modifies the function, and if
957/// so, return true.
958///
959bool FunctionPassManager_New::run(Function &F) {
960 std::string errstr;
961 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000962 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000963 abort();
964 }
Devang Patel272908d2006-12-08 22:57:48 +0000965 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000966}
967
968
Devang Patelff631ae2006-11-15 01:27:05 +0000969/// doInitialization - Run all of the initializers for the function passes.
970///
971bool FunctionPassManager_New::doInitialization() {
972 return FPM->doInitialization(*MP->getModule());
973}
974
975/// doFinalization - Run all of the initializers for the function passes.
976///
977bool FunctionPassManager_New::doFinalization() {
978 return FPM->doFinalization(*MP->getModule());
979}
980
Devang Patela1514cb2006-12-07 19:39:39 +0000981//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000982// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000983
Devang Patel0c2012f2006-11-07 21:49:50 +0000984/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
985/// either use it into active basic block pass manager or create new basic
986/// block pass manager to handle pass P.
987bool
Devang Patel4e12f862006-11-08 10:44:40 +0000988FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000989
990 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
991 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
992
Devang Patel4949fe02006-12-07 22:34:21 +0000993 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000994
Devang Patel4949fe02006-12-07 22:34:21 +0000995 // If active manager exists then clear its analysis info.
996 if (activeBBPassManager)
997 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000998
Devang Patel4949fe02006-12-07 22:34:21 +0000999 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001000 activeBBPassManager =
1001 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001002 // Inherit top level manager
1003 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001004
1005 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +00001006 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001007
1008 // Add new manager into top level manager's indirect passes list
1009 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1010 assert (PMD && "Manager is not Pass Manager");
1011 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +00001012
1013 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001014 if (!activeBBPassManager->addPass(BP))
1015 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +00001016 }
Devang Patelbc03f132006-12-07 23:55:10 +00001017
1018 if (!ForcedLastUses.empty())
1019 TPM->setLastUser(ForcedLastUses, this);
1020
Devang Patel0c2012f2006-11-07 21:49:50 +00001021 return true;
1022 }
1023
1024 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1025 if (!FP)
1026 return false;
1027
Devang Patel3c8eb622006-11-07 22:56:50 +00001028 // If this pass does not preserve anlysis that is used by other passes
1029 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001030 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001031 return false;
1032
Devang Patel8cad70d2006-11-11 01:51:02 +00001033 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +00001034
1035 // If active manager exists then clear its analysis info.
1036 if (activeBBPassManager) {
1037 activeBBPassManager->initializeAnalysisInfo();
1038 activeBBPassManager = NULL;
1039 }
1040
Devang Patel0c2012f2006-11-07 21:49:50 +00001041 return true;
1042}
1043
1044/// Execute all of the passes scheduled for execution by invoking
1045/// runOnFunction method. Keep track of whether any of the passes modifies
1046/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +00001047bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001048
Devang Patel0e29e292006-12-08 19:04:09 +00001049 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +00001050 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001051
Devang Patel0c2012f2006-11-07 21:49:50 +00001052 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +00001053 this->runOnFunction(*I);
1054
Devang Patel0e29e292006-12-08 19:04:09 +00001055 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +00001056}
1057
Devang Patel9f3083e2006-11-15 19:39:54 +00001058/// Execute all of the passes scheduled for execution by invoking
1059/// runOnFunction method. Keep track of whether any of the passes modifies
1060/// the function, and if so, return true.
1061bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
1062
1063 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001064
1065 if (F.isExternal())
1066 return false;
1067
Devang Patela6b6dcb2006-12-07 18:41:09 +00001068 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +00001069
1070 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1071 e = passVectorEnd(); itr != e; ++itr) {
1072 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +00001073 initializeAnalysisImpl(P);
Devang Patel9f3083e2006-11-15 19:39:54 +00001074 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1075 Changed |= FP->runOnFunction(F);
1076 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001077 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +00001078 removeDeadPasses(P);
1079 }
1080 return Changed;
1081}
1082
1083
Devang Patelff631ae2006-11-15 01:27:05 +00001084inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1085 bool Changed = false;
1086
1087 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1088 e = passVectorEnd(); itr != e; ++itr) {
1089 Pass *P = *itr;
1090
1091 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1092 Changed |= FP->doInitialization(M);
1093 }
1094
1095 return Changed;
1096}
1097
1098inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1099 bool Changed = false;
1100
1101 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1102 e = passVectorEnd(); itr != e; ++itr) {
1103 Pass *P = *itr;
1104
1105 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1106 Changed |= FP->doFinalization(M);
1107 }
1108
Devang Patelff631ae2006-11-15 01:27:05 +00001109 return Changed;
1110}
1111
Devang Patel272908d2006-12-08 22:57:48 +00001112// Execute all the passes managed by this top level manager.
1113// Return true if any function is modified by a pass.
1114bool FunctionPassManagerImpl_New::run(Function &F) {
1115
1116 bool Changed = false;
1117 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1118 E = passManagersEnd(); I != E; ++I) {
1119 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
1120 Changed |= FP->runOnFunction(F);
1121 }
1122 return Changed;
1123}
1124
Devang Patela1514cb2006-12-07 19:39:39 +00001125//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +00001126// ModulePassManager implementation
1127
1128/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +00001129/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001130/// is not manageable by this manager.
1131bool
Devang Pateld65e9e92006-11-08 01:31:28 +00001132ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001133
1134 // If P is FunctionPass then use function pass maanager.
1135 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1136
Devang Patel640c5bb2006-12-08 22:30:11 +00001137 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001138
Devang Patel4949fe02006-12-07 22:34:21 +00001139 // If active manager exists then clear its analysis info.
1140 if (activeFunctionPassManager)
1141 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001142
Devang Patel4949fe02006-12-07 22:34:21 +00001143 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001144 activeFunctionPassManager =
1145 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001146
1147 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001148 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001149
Devang Patel9c6290c2006-12-12 22:02:16 +00001150 // Inherit top level manager
1151 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001152
1153 // Add new manager into top level manager's indirect passes list
1154 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1155 assert (PMD && "Manager is not Pass Manager");
1156 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001157
Devang Patel4949fe02006-12-07 22:34:21 +00001158 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001159 if (!activeFunctionPassManager->addPass(FP))
1160 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001161 }
Devang Patelbc03f132006-12-07 23:55:10 +00001162
1163 if (!ForcedLastUses.empty())
1164 TPM->setLastUser(ForcedLastUses, this);
1165
Devang Patel05e1a972006-11-07 22:03:15 +00001166 return true;
1167 }
1168
1169 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1170 if (!MP)
1171 return false;
1172
Devang Patel3c8eb622006-11-07 22:56:50 +00001173 // If this pass does not preserve anlysis that is used by other passes
1174 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001175 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001176 return false;
1177
Devang Patel8cad70d2006-11-11 01:51:02 +00001178 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001179 // If active manager exists then clear its analysis info.
1180 if (activeFunctionPassManager) {
1181 activeFunctionPassManager->initializeAnalysisInfo();
1182 activeFunctionPassManager = NULL;
1183 }
1184
Devang Patel05e1a972006-11-07 22:03:15 +00001185 return true;
1186}
1187
1188
1189/// Execute all of the passes scheduled for execution by invoking
1190/// runOnModule method. Keep track of whether any of the passes modifies
1191/// the module, and if so, return true.
1192bool
1193ModulePassManager_New::runOnModule(Module &M) {
1194 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001195 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001196
Devang Patel8cad70d2006-11-11 01:51:02 +00001197 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1198 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001199 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +00001200 initializeAnalysisImpl(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001201 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1202 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001203 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001204 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001205 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001206 }
1207 return Changed;
1208}
1209
Devang Patela1514cb2006-12-07 19:39:39 +00001210//===----------------------------------------------------------------------===//
1211// PassManagerImpl implementation
Devang Patelab97cf42006-12-13 00:09:23 +00001212//
Devang Patelc290c8a2006-11-07 22:23:34 +00001213/// Add P into active pass manager or use new module pass manager to
1214/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001215bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001216
Devang Patel6c9f5482006-11-11 00:42:16 +00001217 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001218 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001219 // Inherit top level manager
1220 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001221
1222 // This top level manager is going to manage activeManager.
1223 // Set up analysis resolver to connect them.
1224 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1225 activeManager->setResolver(AR);
1226
Devang Patel5bbeb492006-12-08 22:47:25 +00001227 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001228 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001229 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001230 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001231}
1232
1233/// run - Execute all of the passes scheduled for execution. Keep track of
1234/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001235bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001236
Devang Patelc290c8a2006-11-07 22:23:34 +00001237 bool Changed = false;
Devang Patel5bbeb492006-12-08 22:47:25 +00001238 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1239 E = passManagersEnd(); I != E; ++I) {
1240 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1241 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001242 }
1243 return Changed;
1244}
Devang Patel376fefa2006-11-08 10:29:57 +00001245
Devang Patela1514cb2006-12-07 19:39:39 +00001246//===----------------------------------------------------------------------===//
1247// PassManager implementation
1248
Devang Patel376fefa2006-11-08 10:29:57 +00001249/// Create new pass manager
1250PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001251 PM = new PassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001252 // PM is the top level manager
1253 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001254}
1255
Devang Patelab97cf42006-12-13 00:09:23 +00001256PassManager_New::~PassManager_New() {
1257 delete PM;
1258}
1259
Devang Patel376fefa2006-11-08 10:29:57 +00001260/// add - Add a pass to the queue of passes to run. This passes ownership of
1261/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1262/// will be destroyed as well, so there is no need to delete the pass. This
1263/// implies that all passes MUST be allocated with 'new'.
1264void
1265PassManager_New::add(Pass *P) {
1266 PM->add(P);
1267}
1268
1269/// run - Execute all of the passes scheduled for execution. Keep track of
1270/// whether any of the passes modifies the module, and if so, return true.
1271bool
1272PassManager_New::run(Module &M) {
1273 return PM->run(M);
1274}
1275