blob: 25e8cbff421fcd9b96bcab87efd17a6936108ff8 [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 Patelf33f3eb2006-12-07 19:21:29 +000089//===----------------------------------------------------------------------===//
90// PMTopLevelManager
91//
92/// PMTopLevelManager manages LastUser info and collects common APIs used by
93/// top level pass managers.
94class PMTopLevelManager {
95
96public:
97
98 inline std::vector<Pass *>::iterator passManagersBegin() {
99 return PassManagers.begin();
100 }
101
102 inline std::vector<Pass *>::iterator passManagersEnd() {
103 return PassManagers.end();
104 }
105
106 /// Schedule pass P for execution. Make sure that passes required by
107 /// P are run before P is run. Update analysis info maintained by
108 /// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000109 void schedulePass(Pass *P);
Devang Patelf33f3eb2006-12-07 19:21:29 +0000110
111 /// This is implemented by top level pass manager and used by
112 /// schedulePass() to add analysis info passes that are not available.
113 virtual void addTopLevelPass(Pass *P) = 0;
114
115 /// Set pass P as the last user of the given analysis passes.
116 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
117
118 /// Collect passes whose last user is P
119 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
120
Devang Patel640c5bb2006-12-08 22:30:11 +0000121 /// Find the pass that implements Analysis AID. Search immutable
122 /// passes and all pass managers. If desired pass is not found
123 /// then return NULL.
124 Pass *findAnalysisPass(AnalysisID AID);
125
Devang Patelf33f3eb2006-12-07 19:21:29 +0000126 virtual ~PMTopLevelManager() {
127 PassManagers.clear();
128 }
129
Devang Patele0eb9d82006-12-07 20:51:18 +0000130 /// Add immutable pass and initialize it.
131 inline void addImmutablePass(ImmutablePass *P) {
132 P->initializePass();
133 ImmutablePasses.push_back(P);
134 }
135
136 inline std::vector<ImmutablePass *>& getImmutablePasses() {
137 return ImmutablePasses;
138 }
139
Devang Patel5bbeb492006-12-08 22:47:25 +0000140 void addPassManager(Pass *Manager) {
141 PassManagers.push_back(Manager);
142 }
143
Devang Patelaf1fca52006-12-08 23:11:43 +0000144 // Add Manager into the list of managers that are not directly
145 // maintained by this top level pass manager
146 void addOtherPassManager(Pass *Manager) {
147 OtherPassManagers.push_back(Manager);
148 }
149
Devang Patelf33f3eb2006-12-07 19:21:29 +0000150private:
151
152 /// Collection of pass managers
153 std::vector<Pass *> PassManagers;
154
Devang Patelaf1fca52006-12-08 23:11:43 +0000155 /// Collection of pass managers that are not directly maintained
156 /// by this pass manager
157 std::vector<Pass *> OtherPassManagers;
158
Devang Patelf33f3eb2006-12-07 19:21:29 +0000159 // Map to keep track of last user of the analysis pass.
160 // LastUser->second is the last user of Lastuser->first.
161 std::map<Pass *, Pass *> LastUser;
Devang Patele0eb9d82006-12-07 20:51:18 +0000162
163 /// Immutable passes are managed by top level manager.
164 std::vector<ImmutablePass *> ImmutablePasses;
Devang Patelf33f3eb2006-12-07 19:21:29 +0000165};
166
167/// Set pass P as the last user of the given analysis passes.
168void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
169 Pass *P) {
170
171 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
172 E = AnalysisPasses.end(); I != E; ++I) {
173 Pass *AP = *I;
174 LastUser[AP] = P;
175 // If AP is the last user of other passes then make P last user of
176 // such passes.
177 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
178 LUE = LastUser.end(); LUI != LUE; ++LUI) {
179 if (LUI->second == AP)
180 LastUser[LUI->first] = P;
181 }
182 }
183
184}
185
186/// Collect passes whose last user is P
187void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
188 Pass *P) {
189 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
190 LUE = LastUser.end(); LUI != LUE; ++LUI)
191 if (LUI->second == P)
192 LastUses.push_back(LUI->first);
193}
194
Devang Patelde124182006-12-07 21:10:57 +0000195/// Schedule pass P for execution. Make sure that passes required by
196/// P are run before P is run. Update analysis info maintained by
197/// the manager. Remove dead passes. This is a recursive function.
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000198void PMTopLevelManager::schedulePass(Pass *P) {
Devang Patelde124182006-12-07 21:10:57 +0000199
200 // TODO : Allocate function manager for this pass, other wise required set
201 // may be inserted into previous function manager
202
203 AnalysisUsage AnUsage;
204 P->getAnalysisUsage(AnUsage);
205 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
206 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
207 E = RequiredSet.end(); I != E; ++I) {
208
Devang Patel640c5bb2006-12-08 22:30:11 +0000209 Pass *AnalysisPass = findAnalysisPass(*I);
Devang Patelde124182006-12-07 21:10:57 +0000210 if (!AnalysisPass) {
211 // Schedule this analysis run first.
212 AnalysisPass = (*I)->createPass();
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000213 schedulePass(AnalysisPass);
Devang Patelde124182006-12-07 21:10:57 +0000214 }
215 }
216
217 // Now all required passes are available.
218 addTopLevelPass(P);
219}
220
Devang Patel640c5bb2006-12-08 22:30:11 +0000221/// Find the pass that implements Analysis AID. Search immutable
222/// passes and all pass managers. If desired pass is not found
223/// then return NULL.
224Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
225
226 Pass *P = NULL;
227 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
228 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
229 const PassInfo *PI = (*I)->getPassInfo();
230 if (PI == AID)
231 P = *I;
232
233 // If Pass not found then check the interfaces implemented by Immutable Pass
234 if (!P) {
235 const std::vector<const PassInfo*> &ImmPI =
236 PI->getInterfacesImplemented();
237 for (unsigned Index = 0, End = ImmPI.size();
238 P == NULL && Index != End; ++Index)
239 if (ImmPI[Index] == AID)
240 P = *I;
241 }
242 }
243
Devang Patelaf1fca52006-12-08 23:11:43 +0000244 // Check pass managers
Devang Patel640c5bb2006-12-08 22:30:11 +0000245 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
246 E = PassManagers.end(); P == NULL && I != E; ++I)
247 P = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
248
Devang Patelaf1fca52006-12-08 23:11:43 +0000249 // Check other pass managers
250 for (std::vector<Pass *>::iterator I = OtherPassManagers.begin(),
251 E = OtherPassManagers.end(); P == NULL && I != E; ++I)
252 P = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
253
Devang Patel640c5bb2006-12-08 22:30:11 +0000254 return P;
255}
256
Devang Patelf3827bc2006-12-07 19:54:15 +0000257//===----------------------------------------------------------------------===//
258// PMDataManager
Devang Patelf33f3eb2006-12-07 19:21:29 +0000259
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000260/// PMDataManager provides the common place to manage the analysis data
261/// used by pass managers.
262class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000263
264public:
265
Devang Patel4c36e6b2006-12-07 23:24:58 +0000266 PMDataManager(int D) : TPM(NULL), Depth(D) {
Devang Patelf3827bc2006-12-07 19:54:15 +0000267 initializeAnalysisInfo();
268 }
269
Devang Patela9844592006-11-11 01:31:05 +0000270 /// Return true IFF pass P's required analysis set does not required new
271 /// manager.
272 bool manageablePass(Pass *P);
273
Devang Patela9844592006-11-11 01:31:05 +0000274 /// Augment AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000275 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000276
Devang Patela9844592006-11-11 01:31:05 +0000277 /// Remove Analysis that is not preserved by the pass
278 void removeNotPreservedAnalysis(Pass *P);
279
280 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000281 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000282
Devang Patel8f677ce2006-12-07 18:47:25 +0000283 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000284 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
285 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000286
Devang Patel1d6267c2006-12-07 23:05:44 +0000287 /// Initialize available analysis information.
Devang Patela6b6dcb2006-12-07 18:41:09 +0000288 void initializeAnalysisInfo() {
Devang Patelbc03f132006-12-07 23:55:10 +0000289 ForcedLastUses.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000290 AvailableAnalysis.clear();
Devang Patelb3900322006-12-07 21:02:08 +0000291
292 // Include immutable passes into AvailableAnalysis vector.
293 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
294 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
295 E = ImmutablePasses.end(); I != E; ++I)
296 recordAvailableAnalysis(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000297 }
298
Devang Patel1d6267c2006-12-07 23:05:44 +0000299 /// Populate RequiredPasses with the analysis pass that are required by
300 /// pass P.
301 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
302 Pass *P);
303
304 /// All Required analyses should be available to the pass as it runs! Here
305 /// we fill in the AnalysisImpls member of the pass so that it can
306 /// successfully use the getAnalysis() method to retrieve the
307 /// implementations it needs.
308 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000309
Devang Patel640c5bb2006-12-08 22:30:11 +0000310 /// Find the pass that implements Analysis AID. If desired pass is not found
311 /// then return NULL.
312 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
313
Devang Patel8cad70d2006-11-11 01:51:02 +0000314 inline std::vector<Pass *>::iterator passVectorBegin() {
315 return PassVector.begin();
316 }
317
318 inline std::vector<Pass *>::iterator passVectorEnd() {
319 return PassVector.end();
320 }
321
Devang Patelf3827bc2006-12-07 19:54:15 +0000322 // Access toplevel manager
323 PMTopLevelManager *getTopLevelManager() { return TPM; }
324 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
325
Devang Patel4c36e6b2006-12-07 23:24:58 +0000326 unsigned getDepth() { return Depth; }
327
Devang Patelbc03f132006-12-07 23:55:10 +0000328protected:
329
330 // Collection of pass whose last user asked this manager to claim
331 // last use. If a FunctionPass F is the last user of ModulePass info M
332 // then the F's manager, not F, records itself as a last user of M.
333 std::vector<Pass *> ForcedLastUses;
334
335 // Top level manager.
336 // TODO : Make it a reference.
337 PMTopLevelManager *TPM;
338
Devang Patela9844592006-11-11 01:31:05 +0000339private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000340 // Set of available Analysis. This information is used while scheduling
341 // pass. If a pass requires an analysis which is not not available then
342 // equired analysis pass is scheduled to run before the pass itself is
343 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000344 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000345
346 // Collection of pass that are managed by this manager
347 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000348
Devang Patel4c36e6b2006-12-07 23:24:58 +0000349 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000350};
351
Devang Patelca58e352006-11-08 10:05:38 +0000352/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
353/// pass together and sequence them to process one basic block before
354/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000355class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000356 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000357
358public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000359 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000360
361 /// Add a pass into a passmanager queue.
362 bool addPass(Pass *p);
363
364 /// Execute all of the passes scheduled for execution. Keep track of
365 /// whether any of the passes modifies the function, and if so, return true.
366 bool runOnFunction(Function &F);
367
Devang Patelf9d96b92006-12-07 19:57:52 +0000368 /// Pass Manager itself does not invalidate any analysis info.
369 void getAnalysisUsage(AnalysisUsage &Info) const {
370 Info.setPreservesAll();
371 }
372
Devang Patel475c4532006-12-08 00:59:05 +0000373 bool doInitialization(Module &M);
374 bool doInitialization(Function &F);
375 bool doFinalization(Module &M);
376 bool doFinalization(Function &F);
377
Devang Patelca58e352006-11-08 10:05:38 +0000378};
379
Devang Patel4e12f862006-11-08 10:44:40 +0000380/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000381/// It batches all function passes and basic block pass managers together and
382/// sequence them to process one function at a time before processing next
383/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000384class FunctionPassManagerImpl_New : public ModulePass,
385 public PMDataManager,
386 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000387public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000388 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
389 PMDataManager(D) { /* TODO */ }
390 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000391 activeBBPassManager = NULL;
392 }
Devang Patel4e12f862006-11-08 10:44:40 +0000393 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000394
Devang Patelabcd1d32006-12-07 21:27:23 +0000395 inline void addTopLevelPass(Pass *P) {
396 addPass(P);
397 }
398
Devang Patelca58e352006-11-08 10:05:38 +0000399 /// add - Add a pass to the queue of passes to run. This passes
400 /// ownership of the Pass to the PassManager. When the
401 /// PassManager_X is destroyed, the pass will be destroyed as well, so
402 /// there is no need to delete the pass. (TODO delete passes.)
403 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000404 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000405 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000406 }
Devang Patelca58e352006-11-08 10:05:38 +0000407
408 /// Add pass into the pass manager queue.
409 bool addPass(Pass *P);
410
411 /// Execute all of the passes scheduled for execution. Keep
412 /// track of whether any of the passes modifies the function, and if
413 /// so, return true.
414 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000415 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000416 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000417
Devang Patelff631ae2006-11-15 01:27:05 +0000418 /// doInitialization - Run all of the initializers for the function passes.
419 ///
420 bool doInitialization(Module &M);
421
422 /// doFinalization - Run all of the initializers for the function passes.
423 ///
424 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000425
426 /// Pass Manager itself does not invalidate any analysis info.
427 void getAnalysisUsage(AnalysisUsage &Info) const {
428 Info.setPreservesAll();
429 }
430
Devang Patelca58e352006-11-08 10:05:38 +0000431private:
Devang Patelca58e352006-11-08 10:05:38 +0000432 // Active Pass Managers
433 BasicBlockPassManager_New *activeBBPassManager;
434};
435
436/// ModulePassManager_New manages ModulePasses and function pass managers.
437/// It batches all Module passes passes and function pass managers together and
438/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000439class ModulePassManager_New : public Pass,
440 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000441
442public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000443 ModulePassManager_New(int D) : PMDataManager(D) {
444 activeFunctionPassManager = NULL;
445 }
Devang Patelca58e352006-11-08 10:05:38 +0000446
447 /// Add a pass into a passmanager queue.
448 bool addPass(Pass *p);
449
450 /// run - Execute all of the passes scheduled for execution. Keep track of
451 /// whether any of the passes modifies the module, and if so, return true.
452 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000453
Devang Patelf9d96b92006-12-07 19:57:52 +0000454 /// Pass Manager itself does not invalidate any analysis info.
455 void getAnalysisUsage(AnalysisUsage &Info) const {
456 Info.setPreservesAll();
457 }
458
Devang Patelca58e352006-11-08 10:05:38 +0000459private:
Devang Patelca58e352006-11-08 10:05:38 +0000460 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000461 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000462};
463
Devang Patel376fefa2006-11-08 10:29:57 +0000464/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000465class PassManagerImpl_New : public Pass,
466 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000467 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000468
469public:
470
Devang Patel4c36e6b2006-12-07 23:24:58 +0000471 PassManagerImpl_New(int D) : PMDataManager(D) {}
472
Devang Patel376fefa2006-11-08 10:29:57 +0000473 /// add - Add a pass to the queue of passes to run. This passes ownership of
474 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
475 /// will be destroyed as well, so there is no need to delete the pass. This
476 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000477 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000478 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000479 }
Devang Patel376fefa2006-11-08 10:29:57 +0000480
481 /// run - Execute all of the passes scheduled for execution. Keep track of
482 /// whether any of the passes modifies the module, and if so, return true.
483 bool run(Module &M);
484
Devang Patelf9d96b92006-12-07 19:57:52 +0000485 /// Pass Manager itself does not invalidate any analysis info.
486 void getAnalysisUsage(AnalysisUsage &Info) const {
487 Info.setPreservesAll();
488 }
489
Devang Patelabcd1d32006-12-07 21:27:23 +0000490 inline void addTopLevelPass(Pass *P) {
491 addPass(P);
492 }
493
Devang Patel376fefa2006-11-08 10:29:57 +0000494private:
495
Devang Patelde124182006-12-07 21:10:57 +0000496 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000497 bool addPass(Pass *p);
498
Devang Patel376fefa2006-11-08 10:29:57 +0000499 // Active Pass Manager
500 ModulePassManager_New *activeManager;
501};
502
Devang Patelca58e352006-11-08 10:05:38 +0000503} // End of llvm namespace
504
Devang Patela1514cb2006-12-07 19:39:39 +0000505//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000506// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000507
Devang Pateld65e9e92006-11-08 01:31:28 +0000508/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000509/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000510bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000511
Devang Patel8f677ce2006-12-07 18:47:25 +0000512 // TODO
513 // If this pass is not preserving information that is required by a
514 // pass maintained by higher level pass manager then do not insert
515 // this pass into current manager. Use new manager. For example,
516 // For example, If FunctionPass F is not preserving ModulePass Info M1
517 // that is used by another ModulePass M2 then do not insert F in
518 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000519 return true;
520}
521
Devang Patel643676c2006-11-11 01:10:19 +0000522/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000523void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000524
Devang Patel643676c2006-11-11 01:10:19 +0000525 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000526 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000527
Devang Patele9976aa2006-12-07 19:33:53 +0000528 //This pass is the current implementation of all of the interfaces it
529 //implements as well.
530 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
531 for (unsigned i = 0, e = II.size(); i != e; ++i)
532 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000533 }
534}
535
Devang Patelf68a3492006-11-07 22:35:17 +0000536/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000537void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000538 AnalysisUsage AnUsage;
539 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000540
Devang Patel2e169c32006-12-07 20:03:49 +0000541 if (AnUsage.getPreservesAll())
542 return;
543
544 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000545 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000546 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000547 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000548 PreservedSet.end()) {
549 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000550 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000551 AvailableAnalysis.erase(J);
552 }
553 }
Devang Patelf68a3492006-11-07 22:35:17 +0000554}
555
Devang Patelca189262006-11-14 03:05:08 +0000556/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000557void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000558
559 std::vector<Pass *> DeadPasses;
560 TPM->collectLastUses(DeadPasses, P);
561
562 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
563 E = DeadPasses.end(); I != E; ++I) {
564 (*I)->releaseMemory();
565
566 std::map<AnalysisID, Pass*>::iterator Pos =
567 AvailableAnalysis.find((*I)->getPassInfo());
568
Devang Patel475c4532006-12-08 00:59:05 +0000569 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000570 if (Pos != AvailableAnalysis.end())
571 AvailableAnalysis.erase(Pos);
572 }
Devang Patelca189262006-11-14 03:05:08 +0000573}
574
Devang Patel8f677ce2006-12-07 18:47:25 +0000575/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000576/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000577void PMDataManager::addPassToManager(Pass *P,
578 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000579
Devang Patel90b05e02006-11-11 02:04:19 +0000580 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000581
582 // At the moment, this pass is the last user of all required passes.
583 std::vector<Pass *> LastUses;
584 std::vector<Pass *> RequiredPasses;
585 unsigned PDepth = this->getDepth();
586
587 collectRequiredAnalysisPasses(RequiredPasses, P);
588 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
589 E = RequiredPasses.end(); I != E; ++I) {
590 Pass *PRequired = *I;
591 unsigned RDepth = 0;
592 //FIXME: RDepth = PRequired->getResolver()->getDepth();
593 if (PDepth == RDepth)
594 LastUses.push_back(PRequired);
595 else if (PDepth > RDepth) {
596 // Let the parent claim responsibility of last use
597 ForcedLastUses.push_back(PRequired);
598 } else {
599 // Note : This feature is not yet implemented
600 assert (0 &&
601 "Unable to handle Pass that requires lower level Analysis pass");
602 }
603 }
604
605 if (!LastUses.empty())
606 TPM->setLastUser(LastUses, P);
607
Devang Patel17bff0d2006-12-07 22:09:36 +0000608 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000609 // Remove the analysis not preserved by this pass
Devang Patel17bff0d2006-12-07 22:09:36 +0000610 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000611 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000612 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000613 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000614
615 // Add pass
616 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000617}
618
Devang Patel1d6267c2006-12-07 23:05:44 +0000619/// Populate RequiredPasses with the analysis pass that are required by
620/// pass P.
621void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
622 Pass *P) {
623 AnalysisUsage AnUsage;
624 P->getAnalysisUsage(AnUsage);
625 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
626 for (std::vector<AnalysisID>::const_iterator
627 I = RequiredSet.begin(), E = RequiredSet.end();
628 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000629 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000630 assert (AnalysisPass && "Analysis pass is not available");
631 RP.push_back(AnalysisPass);
632 }
633}
634
Devang Patel07f4f582006-11-14 21:49:36 +0000635// All Required analyses should be available to the pass as it runs! Here
636// we fill in the AnalysisImpls member of the pass so that it can
637// successfully use the getAnalysis() method to retrieve the
638// implementations it needs.
639//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000640void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000641 AnalysisUsage AnUsage;
642 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000643
644 for (std::vector<const PassInfo *>::const_iterator
645 I = AnUsage.getRequiredSet().begin(),
646 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000647 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000648 if (Impl == 0)
649 assert(0 && "Analysis used but not available!");
650 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
651 }
652}
653
Devang Patel640c5bb2006-12-08 22:30:11 +0000654/// Find the pass that implements Analysis AID. If desired pass is not found
655/// then return NULL.
656Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
657
658 // Check if AvailableAnalysis map has one entry.
659 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
660
661 if (I != AvailableAnalysis.end())
662 return I->second;
663
664 // Search Parents through TopLevelManager
665 if (SearchParent)
666 return TPM->findAnalysisPass(AID);
667
668 // FIXME : This is expensive and requires. Need to check only managers not all passes.
669 // One solution is to collect managers in advance at TPM level.
670 Pass *P = NULL;
671 for(std::vector<Pass *>::iterator I = passVectorBegin(),
672 E = passVectorEnd(); P == NULL && I!= E; ++I )
673 P = NULL; // FIXME : P = (*I)->getResolver()->getAnalysisToUpdate(AID, false /* Do not search parents again */);
674
675 return P;
676}
677
Devang Patela1514cb2006-12-07 19:39:39 +0000678//===----------------------------------------------------------------------===//
679// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000680
Devang Pateld65e9e92006-11-08 01:31:28 +0000681/// Add pass P into PassVector and return true. If this pass is not
682/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000683bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000684BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000685
686 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
687 if (!BP)
688 return false;
689
Devang Patel3c8eb622006-11-07 22:56:50 +0000690 // If this pass does not preserve anlysis that is used by other passes
691 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000692 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000693 return false;
694
Devang Patel8cad70d2006-11-11 01:51:02 +0000695 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000696
Devang Patel6e5a1132006-11-07 21:31:57 +0000697 return true;
698}
699
700/// Execute all of the passes scheduled for execution by invoking
701/// runOnBasicBlock method. Keep track of whether any of the passes modifies
702/// the function, and if so, return true.
703bool
704BasicBlockPassManager_New::runOnFunction(Function &F) {
705
Devang Patele9585592006-12-08 01:38:28 +0000706 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000707 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000708
Devang Patel6e5a1132006-11-07 21:31:57 +0000709 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000710 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
711 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000712 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000713
Devang Patel6e5a1132006-11-07 21:31:57 +0000714 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
715 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000716 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000717 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000718 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000719 }
Devang Patele9585592006-12-08 01:38:28 +0000720 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000721}
722
Devang Patel475c4532006-12-08 00:59:05 +0000723// Implement doInitialization and doFinalization
724inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
725 bool Changed = false;
726
727 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
728 e = passVectorEnd(); itr != e; ++itr) {
729 Pass *P = *itr;
730 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
731 Changed |= BP->doInitialization(M);
732 }
733
734 return Changed;
735}
736
737inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
738 bool Changed = false;
739
740 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
741 e = passVectorEnd(); itr != e; ++itr) {
742 Pass *P = *itr;
743 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
744 Changed |= BP->doFinalization(M);
745 }
746
747 return Changed;
748}
749
750inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
751 bool Changed = false;
752
753 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
754 e = passVectorEnd(); itr != e; ++itr) {
755 Pass *P = *itr;
756 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
757 Changed |= BP->doInitialization(F);
758 }
759
760 return Changed;
761}
762
763inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
764 bool Changed = false;
765
766 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
767 e = passVectorEnd(); itr != e; ++itr) {
768 Pass *P = *itr;
769 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
770 Changed |= BP->doFinalization(F);
771 }
772
773 return Changed;
774}
775
776
Devang Patela1514cb2006-12-07 19:39:39 +0000777//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000778// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000779
Devang Patel4e12f862006-11-08 10:44:40 +0000780/// Create new Function pass manager
781FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000782 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000783}
784
Devang Patel1f653682006-12-08 18:57:16 +0000785FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
786 FPM = new FunctionPassManagerImpl_New(0);
787 MP = P;
788}
789
Devang Patel4e12f862006-11-08 10:44:40 +0000790/// add - Add a pass to the queue of passes to run. This passes
791/// ownership of the Pass to the PassManager. When the
792/// PassManager_X is destroyed, the pass will be destroyed as well, so
793/// there is no need to delete the pass. (TODO delete passes.)
794/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000795void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000796 FPM->add(P);
797}
798
799/// Execute all of the passes scheduled for execution. Keep
800/// track of whether any of the passes modifies the function, and if
801/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000802bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000803 return FPM->runOnModule(M);
804}
805
Devang Patel9f3083e2006-11-15 19:39:54 +0000806/// run - Execute all of the passes scheduled for execution. Keep
807/// track of whether any of the passes modifies the function, and if
808/// so, return true.
809///
810bool FunctionPassManager_New::run(Function &F) {
811 std::string errstr;
812 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000813 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000814 abort();
815 }
Devang Patel272908d2006-12-08 22:57:48 +0000816 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000817}
818
819
Devang Patelff631ae2006-11-15 01:27:05 +0000820/// doInitialization - Run all of the initializers for the function passes.
821///
822bool FunctionPassManager_New::doInitialization() {
823 return FPM->doInitialization(*MP->getModule());
824}
825
826/// doFinalization - Run all of the initializers for the function passes.
827///
828bool FunctionPassManager_New::doFinalization() {
829 return FPM->doFinalization(*MP->getModule());
830}
831
Devang Patela1514cb2006-12-07 19:39:39 +0000832//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000833// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000834
Devang Patel0c2012f2006-11-07 21:49:50 +0000835/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
836/// either use it into active basic block pass manager or create new basic
837/// block pass manager to handle pass P.
838bool
Devang Patel4e12f862006-11-08 10:44:40 +0000839FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000840
841 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
842 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
843
Devang Patel4949fe02006-12-07 22:34:21 +0000844 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000845
Devang Patel4949fe02006-12-07 22:34:21 +0000846 // If active manager exists then clear its analysis info.
847 if (activeBBPassManager)
848 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000849
Devang Patel4949fe02006-12-07 22:34:21 +0000850 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000851 activeBBPassManager =
852 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000853 addPassToManager(activeBBPassManager, false);
Devang Patelaf1fca52006-12-08 23:11:43 +0000854 TPM->addOtherPassManager(activeBBPassManager);
Devang Patel4949fe02006-12-07 22:34:21 +0000855
856 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000857 if (!activeBBPassManager->addPass(BP))
858 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000859 }
Devang Patelbc03f132006-12-07 23:55:10 +0000860
861 if (!ForcedLastUses.empty())
862 TPM->setLastUser(ForcedLastUses, this);
863
Devang Patel0c2012f2006-11-07 21:49:50 +0000864 return true;
865 }
866
867 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
868 if (!FP)
869 return false;
870
Devang Patel3c8eb622006-11-07 22:56:50 +0000871 // If this pass does not preserve anlysis that is used by other passes
872 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000873 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000874 return false;
875
Devang Patel8cad70d2006-11-11 01:51:02 +0000876 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000877
878 // If active manager exists then clear its analysis info.
879 if (activeBBPassManager) {
880 activeBBPassManager->initializeAnalysisInfo();
881 activeBBPassManager = NULL;
882 }
883
Devang Patel0c2012f2006-11-07 21:49:50 +0000884 return true;
885}
886
887/// Execute all of the passes scheduled for execution by invoking
888/// runOnFunction method. Keep track of whether any of the passes modifies
889/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000890bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000891
Devang Patel0e29e292006-12-08 19:04:09 +0000892 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000893 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000894
Devang Patel0c2012f2006-11-07 21:49:50 +0000895 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +0000896 this->runOnFunction(*I);
897
Devang Patel0e29e292006-12-08 19:04:09 +0000898 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +0000899}
900
Devang Patel9f3083e2006-11-15 19:39:54 +0000901/// Execute all of the passes scheduled for execution by invoking
902/// runOnFunction method. Keep track of whether any of the passes modifies
903/// the function, and if so, return true.
904bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
905
906 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000907 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000908
909 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
910 e = passVectorEnd(); itr != e; ++itr) {
911 Pass *P = *itr;
912
Devang Patel9f3083e2006-11-15 19:39:54 +0000913 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
914 Changed |= FP->runOnFunction(F);
915 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000916 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000917 removeDeadPasses(P);
918 }
919 return Changed;
920}
921
922
Devang Patelff631ae2006-11-15 01:27:05 +0000923inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
924 bool Changed = false;
925
926 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
927 e = passVectorEnd(); itr != e; ++itr) {
928 Pass *P = *itr;
929
930 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
931 Changed |= FP->doInitialization(M);
932 }
933
934 return Changed;
935}
936
937inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
938 bool Changed = false;
939
940 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
941 e = passVectorEnd(); itr != e; ++itr) {
942 Pass *P = *itr;
943
944 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
945 Changed |= FP->doFinalization(M);
946 }
947
Devang Patelff631ae2006-11-15 01:27:05 +0000948 return Changed;
949}
950
Devang Patel272908d2006-12-08 22:57:48 +0000951// Execute all the passes managed by this top level manager.
952// Return true if any function is modified by a pass.
953bool FunctionPassManagerImpl_New::run(Function &F) {
954
955 bool Changed = false;
956 for (std::vector<Pass *>::iterator I = passManagersBegin(),
957 E = passManagersEnd(); I != E; ++I) {
958 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
959 Changed |= FP->runOnFunction(F);
960 }
961 return Changed;
962}
963
Devang Patela1514cb2006-12-07 19:39:39 +0000964//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000965// ModulePassManager implementation
966
967/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000968/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000969/// is not manageable by this manager.
970bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000971ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000972
973 // If P is FunctionPass then use function pass maanager.
974 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
975
Devang Patel640c5bb2006-12-08 22:30:11 +0000976 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +0000977
Devang Patel4949fe02006-12-07 22:34:21 +0000978 // If active manager exists then clear its analysis info.
979 if (activeFunctionPassManager)
980 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000981
Devang Patel4949fe02006-12-07 22:34:21 +0000982 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000983 activeFunctionPassManager =
984 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +0000985 addPassToManager(activeFunctionPassManager, false);
Devang Patelaf1fca52006-12-08 23:11:43 +0000986 TPM->addOtherPassManager(activeFunctionPassManager);
987
Devang Patel4949fe02006-12-07 22:34:21 +0000988 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000989 if (!activeFunctionPassManager->addPass(FP))
990 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000991 }
Devang Patelbc03f132006-12-07 23:55:10 +0000992
993 if (!ForcedLastUses.empty())
994 TPM->setLastUser(ForcedLastUses, this);
995
Devang Patel05e1a972006-11-07 22:03:15 +0000996 return true;
997 }
998
999 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1000 if (!MP)
1001 return false;
1002
Devang Patel3c8eb622006-11-07 22:56:50 +00001003 // If this pass does not preserve anlysis that is used by other passes
1004 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001005 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001006 return false;
1007
Devang Patel8cad70d2006-11-11 01:51:02 +00001008 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001009 // If active manager exists then clear its analysis info.
1010 if (activeFunctionPassManager) {
1011 activeFunctionPassManager->initializeAnalysisInfo();
1012 activeFunctionPassManager = NULL;
1013 }
1014
Devang Patel05e1a972006-11-07 22:03:15 +00001015 return true;
1016}
1017
1018
1019/// Execute all of the passes scheduled for execution by invoking
1020/// runOnModule method. Keep track of whether any of the passes modifies
1021/// the module, and if so, return true.
1022bool
1023ModulePassManager_New::runOnModule(Module &M) {
1024 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001025 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001026
Devang Patel8cad70d2006-11-11 01:51:02 +00001027 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1028 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001029 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +00001030
Devang Patel05e1a972006-11-07 22:03:15 +00001031 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1032 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001033 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001034 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001035 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001036 }
1037 return Changed;
1038}
1039
Devang Patela1514cb2006-12-07 19:39:39 +00001040//===----------------------------------------------------------------------===//
1041// PassManagerImpl implementation
1042
Devang Patelc290c8a2006-11-07 22:23:34 +00001043// PassManager_New implementation
1044/// Add P into active pass manager or use new module pass manager to
1045/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001046bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001047
Devang Patel6c9f5482006-11-11 00:42:16 +00001048 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001049 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patel5bbeb492006-12-08 22:47:25 +00001050 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001051 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001052 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001053 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001054}
1055
1056/// run - Execute all of the passes scheduled for execution. Keep track of
1057/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001058bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001059
Devang Patelc290c8a2006-11-07 22:23:34 +00001060 bool Changed = false;
Devang Patel5bbeb492006-12-08 22:47:25 +00001061 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1062 E = passManagersEnd(); I != E; ++I) {
1063 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1064 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001065 }
1066 return Changed;
1067}
Devang Patel376fefa2006-11-08 10:29:57 +00001068
Devang Patela1514cb2006-12-07 19:39:39 +00001069//===----------------------------------------------------------------------===//
1070// PassManager implementation
1071
Devang Patel376fefa2006-11-08 10:29:57 +00001072/// Create new pass manager
1073PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001074 PM = new PassManagerImpl_New(0);
Devang Patel376fefa2006-11-08 10:29:57 +00001075}
1076
1077/// add - Add a pass to the queue of passes to run. This passes ownership of
1078/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1079/// will be destroyed as well, so there is no need to delete the pass. This
1080/// implies that all passes MUST be allocated with 'new'.
1081void
1082PassManager_New::add(Pass *P) {
1083 PM->add(P);
1084}
1085
1086/// run - Execute all of the passes scheduled for execution. Keep track of
1087/// whether any of the passes modifies the module, and if so, return true.
1088bool
1089PassManager_New::run(Module &M) {
1090 return PM->run(M);
1091}
1092