blob: ffa9167a4a94061fbf57573ea0c7d9527bae65cb [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 Pateleda56172006-12-12 23:34:33 +0000152 // Print passes managed by this top level manager.
153 void dumpPasses();
154
Devang Patelf33f3eb2006-12-07 19:21:29 +0000155private:
156
157 /// Collection of pass managers
158 std::vector<Pass *> PassManagers;
159
Devang Patelaf1fca52006-12-08 23:11:43 +0000160 /// Collection of pass managers that are not directly maintained
161 /// by this pass manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000162 std::vector<PMDataManager *> IndirectPassManagers;
Devang Patelaf1fca52006-12-08 23:11:43 +0000163
Devang Patelf33f3eb2006-12-07 19:21:29 +0000164 // Map to keep track of last user of the analysis pass.
165 // LastUser->second is the last user of Lastuser->first.
166 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000167
168 /// Immutable passes are managed by top level manager.
169 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000170};
171
Devang Patelf3827bc2006-12-07 19:54:15 +0000172//===----------------------------------------------------------------------===//
173// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000174
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000175/// PMDataManager provides the common place to manage the analysis data
176/// used by pass managers.
177class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000178
179public:
180
Devang Patel4c36e6b2006-12-07 23:24:58 +0000181 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000182 initializeAnalysisInfo();
183 }
184
Devang Patela9844592006-11-11 01:31:05 +0000185 /// Return true IFF pass P's required analysis set does not required new
186 /// manager.
187 bool manageablePass(Pass *P);
188
Devang Patela9844592006-11-11 01:31:05 +0000189 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000190 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000191
Devang Patela9844592006-11-11 01:31:05 +0000192 /// Remove Analysis that is not preserved by the pass
193 void removeNotPreservedAnalysis(Pass *P);
194
195 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000196 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000197
Devang Patel8f677ce2006-12-07 18:47:25 +0000198 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000199 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
200 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000201
Devang Patel1d6267c2006-12-07 23:05:44 +0000202 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000203 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000204 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000205 AvailableAnalysis.clear();
206 }
207
Devang Patel1d6267c2006-12-07 23:05:44 +0000208 /// Populate RequiredPasses with the analysis pass that are required by
209 /// pass P.
210 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
211 Pass *P);
212
213 /// All Required analyses should be available to the pass as it runs! Here
214 /// we fill in the AnalysisImpls member of the pass so that it can
215 /// successfully use the getAnalysis() method to retrieve the
216 /// implementations it needs.
217 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000218
Devang Patel640c5bb2006-12-08 22:30:11 +0000219 /// Find the pass that implements Analysis AID. If desired pass is not found
220 /// then return NULL.
221 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
222
Devang Patel8cad70d2006-11-11 01:51:02 +0000223 inline std::vector<Pass *>::iterator passVectorBegin() {
224 return PassVector.begin();
225 }
226
227 inline std::vector<Pass *>::iterator passVectorEnd() {
228 return PassVector.end();
229 }
230
Devang Patelf3827bc2006-12-07 19:54:15 +0000231 // Access toplevel manager
232 PMTopLevelManager *getTopLevelManager() { return TPM; }
233 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
234
Devang Patel4c36e6b2006-12-07 23:24:58 +0000235 unsigned getDepth() { return Depth; }
236
Devang Pateleda56172006-12-12 23:34:33 +0000237 // Print list of passes that are last used by P.
238 void dumpLastUses(Pass *P, unsigned Offset) {
239
240 std::vector<Pass *> LUses;
241
242 assert (TPM && "Top Level Manager is missing");
243 TPM->collectLastUses(LUses, P);
244
245 for (std::vector<Pass *>::iterator I = LUses.begin(),
246 E = LUses.end(); I != E; ++I) {
247 llvm::cerr << "--" << std::string(Offset*2, ' ');
248 (*I)->dumpPassStructure(0);
249 }
250 }
251
Devang Patelbc03f132006-12-07 23:55:10 +0000252protected:
253
254 // Collection of pass whose last user asked this manager to claim
255 // last use. If a FunctionPass F is the last user of ModulePass info M
256 // then the F's manager, not F, records itself as a last user of M.
257 std::vector<Pass *> ForcedLastUses;
258
259 // Top level manager.
Devang Patelbc03f132006-12-07 23:55:10 +0000260 PMTopLevelManager *TPM;
261
Devang Patela9844592006-11-11 01:31:05 +0000262private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000263 // Set of available Analysis. This information is used while scheduling
264 // pass. If a pass requires an analysis which is not not available then
265 // equired analysis pass is scheduled to run before the pass itself is
266 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000267 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000268
269 // Collection of pass that are managed by this manager
270 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000271
Devang Patel4c36e6b2006-12-07 23:24:58 +0000272 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000273};
274
Devang Patel10c2ca62006-12-12 22:47:13 +0000275//===----------------------------------------------------------------------===//
276// BasicBlockPassManager_New
277//
Devang Patelca58e352006-11-08 10:05:38 +0000278/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
279/// pass together and sequence them to process one basic block before
280/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000281class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000282 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000283
284public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000285 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000286
287 /// Add a pass into a passmanager queue.
288 bool addPass(Pass *p);
289
290 /// Execute all of the passes scheduled for execution. Keep track of
291 /// whether any of the passes modifies the function, and if so, return true.
292 bool runOnFunction(Function &F);
293
Devang Patelf9d96b92006-12-07 19:57:52 +0000294 /// Pass Manager itself does not invalidate any analysis info.
295 void getAnalysisUsage(AnalysisUsage &Info) const {
296 Info.setPreservesAll();
297 }
298
Devang Patel475c4532006-12-08 00:59:05 +0000299 bool doInitialization(Module &M);
300 bool doInitialization(Function &F);
301 bool doFinalization(Module &M);
302 bool doFinalization(Function &F);
303
Devang Pateleda56172006-12-12 23:34:33 +0000304 // Print passes managed by this manager
305 void dumpPassStructure(unsigned Offset) {
306 llvm::cerr << std::string(Offset*2, ' ') << "BasicBLockPass Manager\n";
307 for (std::vector<Pass *>::iterator I = passVectorBegin(),
308 E = passVectorEnd(); I != E; ++I) {
309 (*I)->dumpPassStructure(Offset + 1);
310 dumpLastUses(*I, Offset+1);
311 }
312 }
313
Devang Patelca58e352006-11-08 10:05:38 +0000314};
315
Devang Patel10c2ca62006-12-12 22:47:13 +0000316//===----------------------------------------------------------------------===//
317// FunctionPassManagerImpl_New
318//
Devang Patel4e12f862006-11-08 10:44:40 +0000319/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000320/// It batches all function passes and basic block pass managers together and
321/// sequence them to process one function at a time before processing next
322/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000323class FunctionPassManagerImpl_New : public ModulePass,
324 public PMDataManager,
325 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000326public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000327 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000328 activeBBPassManager = NULL;
329 }
Devang Patel4e12f862006-11-08 10:44:40 +0000330 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000331
Devang Patelabcd1d32006-12-07 21:27:23 +0000332 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000333
Devang Patelfa971cd2006-12-08 23:57:43 +0000334 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000335
336 // P is a immutable pass then it will be managed by this
337 // top level manager. Set up analysis resolver to connect them.
338 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
339 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000340 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000341 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000342 recordAvailableAnalysis(IP);
Devang Patelfa971cd2006-12-08 23:57:43 +0000343 }
344 else
345 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000346 }
347
Devang Patelca58e352006-11-08 10:05:38 +0000348 /// add - Add a pass to the queue of passes to run. This passes
349 /// ownership of the Pass to the PassManager. When the
350 /// PassManager_X is destroyed, the pass will be destroyed as well, so
351 /// there is no need to delete the pass. (TODO delete passes.)
352 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000353 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000354 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000355 }
Devang Patelca58e352006-11-08 10:05:38 +0000356
357 /// Add pass into the pass manager queue.
358 bool addPass(Pass *P);
359
360 /// Execute all of the passes scheduled for execution. Keep
361 /// track of whether any of the passes modifies the function, and if
362 /// so, return true.
363 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000364 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000365 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000366
Devang Patelff631ae2006-11-15 01:27:05 +0000367 /// doInitialization - Run all of the initializers for the function passes.
368 ///
369 bool doInitialization(Module &M);
370
371 /// doFinalization - Run all of the initializers for the function passes.
372 ///
373 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000374
375 /// Pass Manager itself does not invalidate any analysis info.
376 void getAnalysisUsage(AnalysisUsage &Info) const {
377 Info.setPreservesAll();
378 }
379
Devang Pateleda56172006-12-12 23:34:33 +0000380 // Print passes managed by this manager
381 void dumpPassStructure(unsigned Offset) {
382 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
383 for (std::vector<Pass *>::iterator I = passVectorBegin(),
384 E = passVectorEnd(); I != E; ++I) {
385 (*I)->dumpPassStructure(Offset + 1);
386 dumpLastUses(*I, Offset+1);
387 }
388 }
389
Devang Patelca58e352006-11-08 10:05:38 +0000390private:
Devang Patelca58e352006-11-08 10:05:38 +0000391 // Active Pass Managers
392 BasicBlockPassManager_New *activeBBPassManager;
393};
394
Devang Patel10c2ca62006-12-12 22:47:13 +0000395//===----------------------------------------------------------------------===//
396// ModulePassManager_New
397//
Devang Patelca58e352006-11-08 10:05:38 +0000398/// ModulePassManager_New manages ModulePasses and function pass managers.
399/// It batches all Module passes passes and function pass managers together and
400/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000401class ModulePassManager_New : public Pass,
402 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000403
404public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000405 ModulePassManager_New(int D) : PMDataManager(D) {
406 activeFunctionPassManager = NULL;
407 }
Devang Patelca58e352006-11-08 10:05:38 +0000408
409 /// Add a pass into a passmanager queue.
410 bool addPass(Pass *p);
411
412 /// run - Execute all of the passes scheduled for execution. Keep track of
413 /// whether any of the passes modifies the module, and if so, return true.
414 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000415
Devang Patelf9d96b92006-12-07 19:57:52 +0000416 /// Pass Manager itself does not invalidate any analysis info.
417 void getAnalysisUsage(AnalysisUsage &Info) const {
418 Info.setPreservesAll();
419 }
420
Devang Pateleda56172006-12-12 23:34:33 +0000421 // Print passes managed by this manager
422 void dumpPassStructure(unsigned Offset) {
423 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
424 for (std::vector<Pass *>::iterator I = passVectorBegin(),
425 E = passVectorEnd(); I != E; ++I) {
426 (*I)->dumpPassStructure(Offset + 1);
427 dumpLastUses(*I, Offset+1);
428 }
429 }
430
Devang Patelca58e352006-11-08 10:05:38 +0000431private:
Devang Patelca58e352006-11-08 10:05:38 +0000432 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000433 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000434};
435
Devang Patel10c2ca62006-12-12 22:47:13 +0000436//===----------------------------------------------------------------------===//
437// PassManagerImpl_New
438//
439/// PassManagerImpl_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000440class PassManagerImpl_New : public Pass,
441 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000442 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000443
444public:
445
Devang Patelad6b7fe2006-12-12 22:57:43 +0000446 PassManagerImpl_New(int D) : PMDataManager(D) {
447 activeManager = NULL;
448 }
Devang Patel4c36e6b2006-12-07 23:24:58 +0000449
Devang Patel376fefa2006-11-08 10:29:57 +0000450 /// add - Add a pass to the queue of passes to run. This passes ownership of
451 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
452 /// will be destroyed as well, so there is no need to delete the pass. This
453 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000454 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000455 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000456 }
Devang Patel376fefa2006-11-08 10:29:57 +0000457
458 /// run - Execute all of the passes scheduled for execution. Keep track of
459 /// whether any of the passes modifies the module, and if so, return true.
460 bool run(Module &M);
461
Devang Patelf9d96b92006-12-07 19:57:52 +0000462 /// Pass Manager itself does not invalidate any analysis info.
463 void getAnalysisUsage(AnalysisUsage &Info) const {
464 Info.setPreservesAll();
465 }
466
Devang Patelabcd1d32006-12-07 21:27:23 +0000467 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000468
Devang Patelfa971cd2006-12-08 23:57:43 +0000469 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000470
471 // P is a immutable pass and it will be managed by this
472 // top level manager. Set up analysis resolver to connect them.
473 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
474 P->setResolver(AR);
Devang Patel95257542006-12-12 22:21:37 +0000475 initializeAnalysisImpl(P);
Devang Patelfa971cd2006-12-08 23:57:43 +0000476 addImmutablePass(IP);
Devang Patel95257542006-12-12 22:21:37 +0000477 recordAvailableAnalysis(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000478 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000479 else
480 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000481 }
482
Devang Patel376fefa2006-11-08 10:29:57 +0000483private:
484
Devang Patelde124182006-12-07 21:10:57 +0000485 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000486 bool addPass(Pass *p);
487
Devang Patel376fefa2006-11-08 10:29:57 +0000488 // Active Pass Manager
489 ModulePassManager_New *activeManager;
490};
491
Devang Patelca58e352006-11-08 10:05:38 +0000492} // End of llvm namespace
493
Devang Patela1514cb2006-12-07 19:39:39 +0000494//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000495// PMTopLevelManager implementation
496
497/// Set pass P as the last user of the given analysis passes.
498void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
499 Pass *P) {
500
501 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
502 E = AnalysisPasses.end(); I != E; ++I) {
503 Pass *AP = *I;
504 LastUser[AP] = P;
505 // If AP is the last user of other passes then make P last user of
506 // such passes.
507 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
508 LUE = LastUser.end(); LUI != LUE; ++LUI) {
509 if (LUI->second == AP)
510 LastUser[LUI->first] = P;
511 }
512 }
513
514}
515
516/// Collect passes whose last user is P
517void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
518 Pass *P) {
519 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
520 LUE = LastUser.end(); LUI != LUE; ++LUI)
521 if (LUI->second == P)
522 LastUses.push_back(LUI->first);
523}
524
525/// Schedule pass P for execution. Make sure that passes required by
526/// P are run before P is run. Update analysis info maintained by
527/// the manager. Remove dead passes. This is a recursive function.
528void PMTopLevelManager::schedulePass(Pass *P) {
529
530 // TODO : Allocate function manager for this pass, other wise required set
531 // may be inserted into previous function manager
532
533 AnalysisUsage AnUsage;
534 P->getAnalysisUsage(AnUsage);
535 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
536 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
537 E = RequiredSet.end(); I != E; ++I) {
538
539 Pass *AnalysisPass = findAnalysisPass(*I);
540 if (!AnalysisPass) {
541 // Schedule this analysis run first.
542 AnalysisPass = (*I)->createPass();
543 schedulePass(AnalysisPass);
544 }
545 }
546
547 // Now all required passes are available.
548 addTopLevelPass(P);
549}
550
551/// Find the pass that implements Analysis AID. Search immutable
552/// passes and all pass managers. If desired pass is not found
553/// then return NULL.
554Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
555
556 Pass *P = NULL;
Devang Patelcd6ba152006-12-12 22:50:05 +0000557 // Check pass managers
558 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
559 E = PassManagers.end(); P == NULL && I != E; ++I) {
560 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
561 assert(PMD && "This is not a PassManager");
562 P = PMD->findAnalysisPass(AID, false);
563 }
564
565 // Check other pass managers
566 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
567 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
568 P = (*I)->findAnalysisPass(AID, false);
569
Devang Patelafb1f3622006-12-12 22:35:25 +0000570 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
571 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
572 const PassInfo *PI = (*I)->getPassInfo();
573 if (PI == AID)
574 P = *I;
575
576 // If Pass not found then check the interfaces implemented by Immutable Pass
577 if (!P) {
578 const std::vector<const PassInfo*> &ImmPI =
579 PI->getInterfacesImplemented();
580 for (unsigned Index = 0, End = ImmPI.size();
581 P == NULL && Index != End; ++Index)
582 if (ImmPI[Index] == AID)
583 P = *I;
584 }
585 }
586
Devang Patelafb1f3622006-12-12 22:35:25 +0000587 return P;
588}
589
Devang Pateleda56172006-12-12 23:34:33 +0000590// Print passes managed by this top level manager.
591void PMTopLevelManager::dumpPasses() {
592
593 // Print out the immutable passes
594 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
595 ImmutablePasses[i]->dumpPassStructure(0);
596 }
597
598 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
599 E = PassManagers.end(); I != E; ++I)
600 (*I)->dumpPassStructure(1);
601
602}
603
Devang Patelafb1f3622006-12-12 22:35:25 +0000604//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000605// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000606
Devang Pateld65e9e92006-11-08 01:31:28 +0000607/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000608/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000609bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000610
Devang Patel8f677ce2006-12-07 18:47:25 +0000611 // TODO
612 // If this pass is not preserving information that is required by a
613 // pass maintained by higher level pass manager then do not insert
614 // this pass into current manager. Use new manager. For example,
615 // For example, If FunctionPass F is not preserving ModulePass Info M1
616 // that is used by another ModulePass M2 then do not insert F in
617 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000618 return true;
619}
620
Devang Patel643676c2006-11-11 01:10:19 +0000621/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000622void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000623
Devang Patel643676c2006-11-11 01:10:19 +0000624 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000625 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000626
Devang Patele9976aa2006-12-07 19:33:53 +0000627 //This pass is the current implementation of all of the interfaces it
628 //implements as well.
629 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
630 for (unsigned i = 0, e = II.size(); i != e; ++i)
631 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000632 }
633}
634
Devang Patelf68a3492006-11-07 22:35:17 +0000635/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000636void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000637 AnalysisUsage AnUsage;
638 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000639
Devang Patel2e169c32006-12-07 20:03:49 +0000640 if (AnUsage.getPreservesAll())
641 return;
642
643 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000644 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000645 E = AvailableAnalysis.end(); I != E; ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000646 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000647 PreservedSet.end()) {
648 // Remove this analysis
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000649 if (!dynamic_cast<ImmutablePass*>(I->second)) {
650 std::map<AnalysisID, Pass*>::iterator J = I++;
651 AvailableAnalysis.erase(J);
652 } else
653 ++I;
654 } else
655 ++I;
Devang Patel349170f2006-11-11 01:24:55 +0000656 }
Devang Patelf68a3492006-11-07 22:35:17 +0000657}
658
Devang Patelca189262006-11-14 03:05:08 +0000659/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000660void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000661
662 std::vector<Pass *> DeadPasses;
663 TPM->collectLastUses(DeadPasses, P);
664
665 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
666 E = DeadPasses.end(); I != E; ++I) {
667 (*I)->releaseMemory();
668
669 std::map<AnalysisID, Pass*>::iterator Pos =
670 AvailableAnalysis.find((*I)->getPassInfo());
671
Devang Patel475c4532006-12-08 00:59:05 +0000672 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000673 if (Pos != AvailableAnalysis.end())
674 AvailableAnalysis.erase(Pos);
675 }
Devang Patelca189262006-11-14 03:05:08 +0000676}
677
Devang Patel8f677ce2006-12-07 18:47:25 +0000678/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000679/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000680void PMDataManager::addPassToManager(Pass *P,
681 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000682
Devang Pateld440cd92006-12-08 23:53:00 +0000683 // This manager is going to manage pass P. Set up analysis resolver
684 // to connect them.
685 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
686 P->setResolver(AR);
687
Devang Patel90b05e02006-11-11 02:04:19 +0000688 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000689
690 // At the moment, this pass is the last user of all required passes.
691 std::vector<Pass *> LastUses;
692 std::vector<Pass *> RequiredPasses;
693 unsigned PDepth = this->getDepth();
694
695 collectRequiredAnalysisPasses(RequiredPasses, P);
696 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
697 E = RequiredPasses.end(); I != E; ++I) {
698 Pass *PRequired = *I;
699 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000700
701 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
702 RDepth = DM.getDepth();
703
Devang Patelbc03f132006-12-07 23:55:10 +0000704 if (PDepth == RDepth)
705 LastUses.push_back(PRequired);
706 else if (PDepth > RDepth) {
707 // Let the parent claim responsibility of last use
708 ForcedLastUses.push_back(PRequired);
709 } else {
710 // Note : This feature is not yet implemented
711 assert (0 &&
712 "Unable to handle Pass that requires lower level Analysis pass");
713 }
714 }
715
716 if (!LastUses.empty())
717 TPM->setLastUser(LastUses, P);
718
Devang Patel17bff0d2006-12-07 22:09:36 +0000719 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000720 // Remove the analysis not preserved by this pass
721 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000722 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000723 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000724
725 // Add pass
726 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000727}
728
Devang Patel1d6267c2006-12-07 23:05:44 +0000729/// Populate RequiredPasses with the analysis pass that are required by
730/// pass P.
731void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
732 Pass *P) {
733 AnalysisUsage AnUsage;
734 P->getAnalysisUsage(AnUsage);
735 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
736 for (std::vector<AnalysisID>::const_iterator
737 I = RequiredSet.begin(), E = RequiredSet.end();
738 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000739 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000740 assert (AnalysisPass && "Analysis pass is not available");
741 RP.push_back(AnalysisPass);
742 }
Devang Patelf58183d2006-12-12 23:09:32 +0000743
744 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
745 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
746 E = IDs.end(); I != E; ++I) {
747 Pass *AnalysisPass = findAnalysisPass(*I, true);
748 assert (AnalysisPass && "Analysis pass is not available");
749 RP.push_back(AnalysisPass);
750 }
Devang Patel1d6267c2006-12-07 23:05:44 +0000751}
752
Devang Patel07f4f582006-11-14 21:49:36 +0000753// All Required analyses should be available to the pass as it runs! Here
754// we fill in the AnalysisImpls member of the pass so that it can
755// successfully use the getAnalysis() method to retrieve the
756// implementations it needs.
757//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000758void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000759 AnalysisUsage AnUsage;
760 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000761
762 for (std::vector<const PassInfo *>::const_iterator
763 I = AnUsage.getRequiredSet().begin(),
764 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000765 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000766 if (Impl == 0)
767 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000768 AnalysisResolver_New *AR = P->getResolver();
769 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000770 }
771}
772
Devang Patel640c5bb2006-12-08 22:30:11 +0000773/// Find the pass that implements Analysis AID. If desired pass is not found
774/// then return NULL.
775Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
776
777 // Check if AvailableAnalysis map has one entry.
778 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
779
780 if (I != AvailableAnalysis.end())
781 return I->second;
782
783 // Search Parents through TopLevelManager
784 if (SearchParent)
785 return TPM->findAnalysisPass(AID);
786
Devang Patel9d759b82006-12-09 00:09:12 +0000787 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000788}
789
Devang Patel9bdf7d42006-12-08 23:28:54 +0000790
791//===----------------------------------------------------------------------===//
792// NOTE: Is this the right place to define this method ?
793// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
794Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
795 return PM.findAnalysisPass(ID, dir);
796}
797
Devang Patela1514cb2006-12-07 19:39:39 +0000798//===----------------------------------------------------------------------===//
799// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000800
Devang Pateld65e9e92006-11-08 01:31:28 +0000801/// Add pass P into PassVector and return true. If this pass is not
802/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000803bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000804BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000805
806 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
807 if (!BP)
808 return false;
809
Devang Patel3c8eb622006-11-07 22:56:50 +0000810 // If this pass does not preserve anlysis that is used by other passes
811 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000812 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000813 return false;
814
Devang Patel8cad70d2006-11-11 01:51:02 +0000815 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000816
Devang Patel6e5a1132006-11-07 21:31:57 +0000817 return true;
818}
819
820/// Execute all of the passes scheduled for execution by invoking
821/// runOnBasicBlock method. Keep track of whether any of the passes modifies
822/// the function, and if so, return true.
823bool
824BasicBlockPassManager_New::runOnFunction(Function &F) {
825
Devang Patel745a6962006-12-12 23:15:28 +0000826 if (F.isExternal())
827 return false;
828
Devang Patele9585592006-12-08 01:38:28 +0000829 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000830 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000831
Devang Patel6e5a1132006-11-07 21:31:57 +0000832 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000833 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
834 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000835 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +0000836 initializeAnalysisImpl(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000837 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
838 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000839 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000840 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000841 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000842 }
Devang Patele9585592006-12-08 01:38:28 +0000843 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000844}
845
Devang Patel475c4532006-12-08 00:59:05 +0000846// Implement doInitialization and doFinalization
847inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
848 bool Changed = false;
849
850 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
851 e = passVectorEnd(); itr != e; ++itr) {
852 Pass *P = *itr;
853 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
854 Changed |= BP->doInitialization(M);
855 }
856
857 return Changed;
858}
859
860inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
861 bool Changed = false;
862
863 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
864 e = passVectorEnd(); itr != e; ++itr) {
865 Pass *P = *itr;
866 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
867 Changed |= BP->doFinalization(M);
868 }
869
870 return Changed;
871}
872
873inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
874 bool Changed = false;
875
876 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
877 e = passVectorEnd(); itr != e; ++itr) {
878 Pass *P = *itr;
879 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
880 Changed |= BP->doInitialization(F);
881 }
882
883 return Changed;
884}
885
886inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
887 bool Changed = false;
888
889 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
890 e = passVectorEnd(); itr != e; ++itr) {
891 Pass *P = *itr;
892 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
893 Changed |= BP->doFinalization(F);
894 }
895
896 return Changed;
897}
898
899
Devang Patela1514cb2006-12-07 19:39:39 +0000900//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000901// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000902
Devang Patel4e12f862006-11-08 10:44:40 +0000903/// Create new Function pass manager
904FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000905 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000906}
907
Devang Patel1f653682006-12-08 18:57:16 +0000908FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
909 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000910 // FPM is the top level manager.
911 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +0000912
913 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
914 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
915 FPM->setResolver(AR);
916
917 FPM->addPassManager(FPM);
Devang Patel1f653682006-12-08 18:57:16 +0000918 MP = P;
919}
920
Devang Patel4e12f862006-11-08 10:44:40 +0000921/// add - Add a pass to the queue of passes to run. This passes
922/// ownership of the Pass to the PassManager. When the
923/// PassManager_X is destroyed, the pass will be destroyed as well, so
924/// there is no need to delete the pass. (TODO delete passes.)
925/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000926void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000927 FPM->add(P);
928}
929
930/// Execute all of the passes scheduled for execution. Keep
931/// track of whether any of the passes modifies the function, and if
932/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000933bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000934 return FPM->runOnModule(M);
935}
936
Devang Patel9f3083e2006-11-15 19:39:54 +0000937/// run - Execute all of the passes scheduled for execution. Keep
938/// track of whether any of the passes modifies the function, and if
939/// so, return true.
940///
941bool FunctionPassManager_New::run(Function &F) {
942 std::string errstr;
943 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000944 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000945 abort();
946 }
Devang Patel272908d2006-12-08 22:57:48 +0000947 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000948}
949
950
Devang Patelff631ae2006-11-15 01:27:05 +0000951/// doInitialization - Run all of the initializers for the function passes.
952///
953bool FunctionPassManager_New::doInitialization() {
954 return FPM->doInitialization(*MP->getModule());
955}
956
957/// doFinalization - Run all of the initializers for the function passes.
958///
959bool FunctionPassManager_New::doFinalization() {
960 return FPM->doFinalization(*MP->getModule());
961}
962
Devang Patela1514cb2006-12-07 19:39:39 +0000963//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000964// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000965
Devang Patel0c2012f2006-11-07 21:49:50 +0000966/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
967/// either use it into active basic block pass manager or create new basic
968/// block pass manager to handle pass P.
969bool
Devang Patel4e12f862006-11-08 10:44:40 +0000970FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000971
972 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
973 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
974
Devang Patel4949fe02006-12-07 22:34:21 +0000975 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000976
Devang Patel4949fe02006-12-07 22:34:21 +0000977 // If active manager exists then clear its analysis info.
978 if (activeBBPassManager)
979 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000980
Devang Patel4949fe02006-12-07 22:34:21 +0000981 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000982 activeBBPassManager =
983 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +0000984 // Inherit top level manager
985 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +0000986
987 // Add new manager into current manager's list.
Devang Patel90b05e02006-11-11 02:04:19 +0000988 addPassToManager(activeBBPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +0000989
990 // Add new manager into top level manager's indirect passes list
991 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
992 assert (PMD && "Manager is not Pass Manager");
993 TPM->addIndirectPassManager(PMD);
Devang Patel4949fe02006-12-07 22:34:21 +0000994
995 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000996 if (!activeBBPassManager->addPass(BP))
997 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000998 }
Devang Patelbc03f132006-12-07 23:55:10 +0000999
1000 if (!ForcedLastUses.empty())
1001 TPM->setLastUser(ForcedLastUses, this);
1002
Devang Patel0c2012f2006-11-07 21:49:50 +00001003 return true;
1004 }
1005
1006 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1007 if (!FP)
1008 return false;
1009
Devang Patel3c8eb622006-11-07 22:56:50 +00001010 // If this pass does not preserve anlysis that is used by other passes
1011 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001012 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001013 return false;
1014
Devang Patel8cad70d2006-11-11 01:51:02 +00001015 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +00001016
1017 // If active manager exists then clear its analysis info.
1018 if (activeBBPassManager) {
1019 activeBBPassManager->initializeAnalysisInfo();
1020 activeBBPassManager = NULL;
1021 }
1022
Devang Patel0c2012f2006-11-07 21:49:50 +00001023 return true;
1024}
1025
1026/// Execute all of the passes scheduled for execution by invoking
1027/// runOnFunction method. Keep track of whether any of the passes modifies
1028/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +00001029bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +00001030
Devang Patel0e29e292006-12-08 19:04:09 +00001031 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +00001032 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001033
Devang Patel0c2012f2006-11-07 21:49:50 +00001034 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +00001035 this->runOnFunction(*I);
1036
Devang Patel0e29e292006-12-08 19:04:09 +00001037 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +00001038}
1039
Devang Patel9f3083e2006-11-15 19:39:54 +00001040/// Execute all of the passes scheduled for execution by invoking
1041/// runOnFunction method. Keep track of whether any of the passes modifies
1042/// the function, and if so, return true.
1043bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
1044
1045 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001046
1047 if (F.isExternal())
1048 return false;
1049
Devang Patela6b6dcb2006-12-07 18:41:09 +00001050 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +00001051
1052 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1053 e = passVectorEnd(); itr != e; ++itr) {
1054 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +00001055 initializeAnalysisImpl(P);
Devang Patel9f3083e2006-11-15 19:39:54 +00001056 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1057 Changed |= FP->runOnFunction(F);
1058 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001059 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +00001060 removeDeadPasses(P);
1061 }
1062 return Changed;
1063}
1064
1065
Devang Patelff631ae2006-11-15 01:27:05 +00001066inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1067 bool Changed = false;
1068
1069 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1070 e = passVectorEnd(); itr != e; ++itr) {
1071 Pass *P = *itr;
1072
1073 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1074 Changed |= FP->doInitialization(M);
1075 }
1076
1077 return Changed;
1078}
1079
1080inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1081 bool Changed = false;
1082
1083 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1084 e = passVectorEnd(); itr != e; ++itr) {
1085 Pass *P = *itr;
1086
1087 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1088 Changed |= FP->doFinalization(M);
1089 }
1090
Devang Patelff631ae2006-11-15 01:27:05 +00001091 return Changed;
1092}
1093
Devang Patel272908d2006-12-08 22:57:48 +00001094// Execute all the passes managed by this top level manager.
1095// Return true if any function is modified by a pass.
1096bool FunctionPassManagerImpl_New::run(Function &F) {
1097
1098 bool Changed = false;
1099 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1100 E = passManagersEnd(); I != E; ++I) {
1101 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
1102 Changed |= FP->runOnFunction(F);
1103 }
1104 return Changed;
1105}
1106
Devang Patela1514cb2006-12-07 19:39:39 +00001107//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +00001108// ModulePassManager implementation
1109
1110/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +00001111/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001112/// is not manageable by this manager.
1113bool
Devang Pateld65e9e92006-11-08 01:31:28 +00001114ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001115
1116 // If P is FunctionPass then use function pass maanager.
1117 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1118
Devang Patel640c5bb2006-12-08 22:30:11 +00001119 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001120
Devang Patel4949fe02006-12-07 22:34:21 +00001121 // If active manager exists then clear its analysis info.
1122 if (activeFunctionPassManager)
1123 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001124
Devang Patel4949fe02006-12-07 22:34:21 +00001125 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001126 activeFunctionPassManager =
1127 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patelafb1f3622006-12-12 22:35:25 +00001128
1129 // Add new manager into current manager's list
Devang Patel90b05e02006-11-11 02:04:19 +00001130 addPassToManager(activeFunctionPassManager, false);
Devang Patelafb1f3622006-12-12 22:35:25 +00001131
Devang Patel9c6290c2006-12-12 22:02:16 +00001132 // Inherit top level manager
1133 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelafb1f3622006-12-12 22:35:25 +00001134
1135 // Add new manager into top level manager's indirect passes list
1136 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1137 assert (PMD && "Manager is not Pass Manager");
1138 TPM->addIndirectPassManager(PMD);
Devang Patelaf1fca52006-12-08 23:11:43 +00001139
Devang Patel4949fe02006-12-07 22:34:21 +00001140 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001141 if (!activeFunctionPassManager->addPass(FP))
1142 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001143 }
Devang Patelbc03f132006-12-07 23:55:10 +00001144
1145 if (!ForcedLastUses.empty())
1146 TPM->setLastUser(ForcedLastUses, this);
1147
Devang Patel05e1a972006-11-07 22:03:15 +00001148 return true;
1149 }
1150
1151 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1152 if (!MP)
1153 return false;
1154
Devang Patel3c8eb622006-11-07 22:56:50 +00001155 // If this pass does not preserve anlysis that is used by other passes
1156 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001157 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001158 return false;
1159
Devang Patel8cad70d2006-11-11 01:51:02 +00001160 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001161 // If active manager exists then clear its analysis info.
1162 if (activeFunctionPassManager) {
1163 activeFunctionPassManager->initializeAnalysisInfo();
1164 activeFunctionPassManager = NULL;
1165 }
1166
Devang Patel05e1a972006-11-07 22:03:15 +00001167 return true;
1168}
1169
1170
1171/// Execute all of the passes scheduled for execution by invoking
1172/// runOnModule method. Keep track of whether any of the passes modifies
1173/// the module, and if so, return true.
1174bool
1175ModulePassManager_New::runOnModule(Module &M) {
1176 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001177 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001178
Devang Patel8cad70d2006-11-11 01:51:02 +00001179 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1180 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001181 Pass *P = *itr;
Devang Patel47d7df72006-12-12 23:13:09 +00001182 initializeAnalysisImpl(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001183 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1184 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001185 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001186 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001187 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001188 }
1189 return Changed;
1190}
1191
Devang Patela1514cb2006-12-07 19:39:39 +00001192//===----------------------------------------------------------------------===//
1193// PassManagerImpl implementation
1194
Devang Patelc290c8a2006-11-07 22:23:34 +00001195// PassManager_New implementation
1196/// Add P into active pass manager or use new module pass manager to
1197/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001198bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001199
Devang Patel6c9f5482006-11-11 00:42:16 +00001200 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001201 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001202 // Inherit top level manager
1203 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001204
1205 // This top level manager is going to manage activeManager.
1206 // Set up analysis resolver to connect them.
1207 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1208 activeManager->setResolver(AR);
1209
Devang Patel5bbeb492006-12-08 22:47:25 +00001210 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001211 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001212 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001213 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001214}
1215
1216/// run - Execute all of the passes scheduled for execution. Keep track of
1217/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001218bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001219
Devang Patelc290c8a2006-11-07 22:23:34 +00001220 bool Changed = false;
Devang Patel5bbeb492006-12-08 22:47:25 +00001221 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1222 E = passManagersEnd(); I != E; ++I) {
1223 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1224 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001225 }
1226 return Changed;
1227}
Devang Patel376fefa2006-11-08 10:29:57 +00001228
Devang Patela1514cb2006-12-07 19:39:39 +00001229//===----------------------------------------------------------------------===//
1230// PassManager implementation
1231
Devang Patel376fefa2006-11-08 10:29:57 +00001232/// Create new pass manager
1233PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001234 PM = new PassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001235 // PM is the top level manager
1236 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001237}
1238
1239/// add - Add a pass to the queue of passes to run. This passes ownership of
1240/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1241/// will be destroyed as well, so there is no need to delete the pass. This
1242/// implies that all passes MUST be allocated with 'new'.
1243void
1244PassManager_New::add(Pass *P) {
1245 PM->add(P);
1246}
1247
1248/// run - Execute all of the passes scheduled for execution. Keep track of
1249/// whether any of the passes modifies the module, and if so, return true.
1250bool
1251PassManager_New::run(Module &M) {
1252 return PM->run(M);
1253}
1254