blob: dea2d919e1c103f13c3828436b05cf5b67919ead [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(),
Devang Patel64619be2006-12-09 00:07:38 +0000246 E = PassManagers.end(); P == NULL && I != E; ++I)
247 P = (*I)->getResolver()->getAnalysisToUpdate(AID, false);
Devang Patel640c5bb2006-12-08 22:30:11 +0000248
Devang Patelaf1fca52006-12-08 23:11:43 +0000249 // Check other pass managers
250 for (std::vector<Pass *>::iterator I = OtherPassManagers.begin(),
Devang Patel64619be2006-12-09 00:07:38 +0000251 E = OtherPassManagers.end(); P == NULL && I != E; ++I)
252 P = (*I)->getResolver()->getAnalysisToUpdate(AID, false);
Devang Patelaf1fca52006-12-08 23:11:43 +0000253
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.
Devang Patelbc03f132006-12-07 23:55:10 +0000336 PMTopLevelManager *TPM;
337
Devang Patela9844592006-11-11 01:31:05 +0000338private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000339 // Set of available Analysis. This information is used while scheduling
340 // pass. If a pass requires an analysis which is not not available then
341 // equired analysis pass is scheduled to run before the pass itself is
342 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000343 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000344
345 // Collection of pass that are managed by this manager
346 std::vector<Pass *> PassVector;
Devang Patelf3827bc2006-12-07 19:54:15 +0000347
Devang Patel4c36e6b2006-12-07 23:24:58 +0000348 unsigned Depth;
Devang Patela9844592006-11-11 01:31:05 +0000349};
350
Devang Patelca58e352006-11-08 10:05:38 +0000351/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
352/// pass together and sequence them to process one basic block before
353/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000354class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000355 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000356
357public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000358 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
Devang Patelca58e352006-11-08 10:05:38 +0000359
360 /// Add a pass into a passmanager queue.
361 bool addPass(Pass *p);
362
363 /// Execute all of the passes scheduled for execution. Keep track of
364 /// whether any of the passes modifies the function, and if so, return true.
365 bool runOnFunction(Function &F);
366
Devang Patelf9d96b92006-12-07 19:57:52 +0000367 /// Pass Manager itself does not invalidate any analysis info.
368 void getAnalysisUsage(AnalysisUsage &Info) const {
369 Info.setPreservesAll();
370 }
371
Devang Patel475c4532006-12-08 00:59:05 +0000372 bool doInitialization(Module &M);
373 bool doInitialization(Function &F);
374 bool doFinalization(Module &M);
375 bool doFinalization(Function &F);
376
Devang Patelca58e352006-11-08 10:05:38 +0000377};
378
Devang Patel4e12f862006-11-08 10:44:40 +0000379/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000380/// It batches all function passes and basic block pass managers together and
381/// sequence them to process one function at a time before processing next
382/// function.
Devang Patelabcd1d32006-12-07 21:27:23 +0000383class FunctionPassManagerImpl_New : public ModulePass,
384 public PMDataManager,
385 public PMTopLevelManager {
Devang Patelca58e352006-11-08 10:05:38 +0000386public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000387 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
388 PMDataManager(D) { /* TODO */ }
389 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
Devang Patelca58e352006-11-08 10:05:38 +0000390 activeBBPassManager = NULL;
391 }
Devang Patel4e12f862006-11-08 10:44:40 +0000392 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000393
Devang Patelabcd1d32006-12-07 21:27:23 +0000394 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000395
Devang Patelfa971cd2006-12-08 23:57:43 +0000396 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000397
398 // P is a immutable pass then it will be managed by this
399 // top level manager. Set up analysis resolver to connect them.
400 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
401 P->setResolver(AR);
Devang Patelfa971cd2006-12-08 23:57:43 +0000402 addImmutablePass(IP);
403 }
404 else
405 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000406 }
407
Devang Patelca58e352006-11-08 10:05:38 +0000408 /// add - Add a pass to the queue of passes to run. This passes
409 /// ownership of the Pass to the PassManager. When the
410 /// PassManager_X is destroyed, the pass will be destroyed as well, so
411 /// there is no need to delete the pass. (TODO delete passes.)
412 /// This implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000413 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000414 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000415 }
Devang Patelca58e352006-11-08 10:05:38 +0000416
417 /// Add pass into the pass manager queue.
418 bool addPass(Pass *P);
419
420 /// Execute all of the passes scheduled for execution. Keep
421 /// track of whether any of the passes modifies the function, and if
422 /// so, return true.
423 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000424 bool runOnFunction(Function &F);
Devang Patel272908d2006-12-08 22:57:48 +0000425 bool run(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000426
Devang Patelff631ae2006-11-15 01:27:05 +0000427 /// doInitialization - Run all of the initializers for the function passes.
428 ///
429 bool doInitialization(Module &M);
430
431 /// doFinalization - Run all of the initializers for the function passes.
432 ///
433 bool doFinalization(Module &M);
Devang Patelf9d96b92006-12-07 19:57:52 +0000434
435 /// Pass Manager itself does not invalidate any analysis info.
436 void getAnalysisUsage(AnalysisUsage &Info) const {
437 Info.setPreservesAll();
438 }
439
Devang Patelca58e352006-11-08 10:05:38 +0000440private:
Devang Patelca58e352006-11-08 10:05:38 +0000441 // Active Pass Managers
442 BasicBlockPassManager_New *activeBBPassManager;
443};
444
445/// ModulePassManager_New manages ModulePasses and function pass managers.
446/// It batches all Module passes passes and function pass managers together and
447/// sequence them to process one module.
Devang Patelbc03f132006-12-07 23:55:10 +0000448class ModulePassManager_New : public Pass,
449 public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000450
451public:
Devang Patel4c36e6b2006-12-07 23:24:58 +0000452 ModulePassManager_New(int D) : PMDataManager(D) {
453 activeFunctionPassManager = NULL;
454 }
Devang Patelca58e352006-11-08 10:05:38 +0000455
456 /// Add a pass into a passmanager queue.
457 bool addPass(Pass *p);
458
459 /// run - Execute all of the passes scheduled for execution. Keep track of
460 /// whether any of the passes modifies the module, and if so, return true.
461 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000462
Devang Patelf9d96b92006-12-07 19:57:52 +0000463 /// Pass Manager itself does not invalidate any analysis info.
464 void getAnalysisUsage(AnalysisUsage &Info) const {
465 Info.setPreservesAll();
466 }
467
Devang Patelca58e352006-11-08 10:05:38 +0000468private:
Devang Patelca58e352006-11-08 10:05:38 +0000469 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000470 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000471};
472
Devang Patel376fefa2006-11-08 10:29:57 +0000473/// PassManager_New manages ModulePassManagers
Devang Patel31217af2006-12-07 21:32:57 +0000474class PassManagerImpl_New : public Pass,
475 public PMDataManager,
Devang Patelabcd1d32006-12-07 21:27:23 +0000476 public PMTopLevelManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000477
478public:
479
Devang Patel4c36e6b2006-12-07 23:24:58 +0000480 PassManagerImpl_New(int D) : PMDataManager(D) {}
481
Devang Patel376fefa2006-11-08 10:29:57 +0000482 /// add - Add a pass to the queue of passes to run. This passes ownership of
483 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
484 /// will be destroyed as well, so there is no need to delete the pass. This
485 /// implies that all passes MUST be allocated with 'new'.
Devang Patel31217af2006-12-07 21:32:57 +0000486 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000487 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000488 }
Devang Patel376fefa2006-11-08 10:29:57 +0000489
490 /// run - Execute all of the passes scheduled for execution. Keep track of
491 /// whether any of the passes modifies the module, and if so, return true.
492 bool run(Module &M);
493
Devang Patelf9d96b92006-12-07 19:57:52 +0000494 /// Pass Manager itself does not invalidate any analysis info.
495 void getAnalysisUsage(AnalysisUsage &Info) const {
496 Info.setPreservesAll();
497 }
498
Devang Patelabcd1d32006-12-07 21:27:23 +0000499 inline void addTopLevelPass(Pass *P) {
Devang Pateld440cd92006-12-08 23:53:00 +0000500
Devang Patelfa971cd2006-12-08 23:57:43 +0000501 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
Devang Pateld440cd92006-12-08 23:53:00 +0000502
503 // P is a immutable pass and it will be managed by this
504 // top level manager. Set up analysis resolver to connect them.
505 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
506 P->setResolver(AR);
Devang Patelfa971cd2006-12-08 23:57:43 +0000507 addImmutablePass(IP);
Devang Pateld440cd92006-12-08 23:53:00 +0000508 }
Devang Patelfa971cd2006-12-08 23:57:43 +0000509 else
510 addPass(P);
Devang Patelabcd1d32006-12-07 21:27:23 +0000511 }
512
Devang Patel376fefa2006-11-08 10:29:57 +0000513private:
514
Devang Patelde124182006-12-07 21:10:57 +0000515 /// Add a pass into a passmanager queue.
Devang Patel376fefa2006-11-08 10:29:57 +0000516 bool addPass(Pass *p);
517
Devang Patel376fefa2006-11-08 10:29:57 +0000518 // Active Pass Manager
519 ModulePassManager_New *activeManager;
520};
521
Devang Patelca58e352006-11-08 10:05:38 +0000522} // End of llvm namespace
523
Devang Patela1514cb2006-12-07 19:39:39 +0000524//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000525// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000526
Devang Pateld65e9e92006-11-08 01:31:28 +0000527/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000528/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000529bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000530
Devang Patel8f677ce2006-12-07 18:47:25 +0000531 // TODO
532 // If this pass is not preserving information that is required by a
533 // pass maintained by higher level pass manager then do not insert
534 // this pass into current manager. Use new manager. For example,
535 // For example, If FunctionPass F is not preserving ModulePass Info M1
536 // that is used by another ModulePass M2 then do not insert F in
537 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000538 return true;
539}
540
Devang Patel643676c2006-11-11 01:10:19 +0000541/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000542void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000543
Devang Patel643676c2006-11-11 01:10:19 +0000544 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000545 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000546
Devang Patele9976aa2006-12-07 19:33:53 +0000547 //This pass is the current implementation of all of the interfaces it
548 //implements as well.
549 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
550 for (unsigned i = 0, e = II.size(); i != e; ++i)
551 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000552 }
553}
554
Devang Patelf68a3492006-11-07 22:35:17 +0000555/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000556void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000557 AnalysisUsage AnUsage;
558 P->getAnalysisUsage(AnUsage);
Devang Patelf68a3492006-11-07 22:35:17 +0000559
Devang Patel2e169c32006-12-07 20:03:49 +0000560 if (AnUsage.getPreservesAll())
561 return;
562
563 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf60b5d92006-11-14 01:59:59 +0000564 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000565 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000566 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000567 PreservedSet.end()) {
568 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000569 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000570 AvailableAnalysis.erase(J);
571 }
572 }
Devang Patelf68a3492006-11-07 22:35:17 +0000573}
574
Devang Patelca189262006-11-14 03:05:08 +0000575/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000576void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patel17ad0962006-12-08 00:37:52 +0000577
578 std::vector<Pass *> DeadPasses;
579 TPM->collectLastUses(DeadPasses, P);
580
581 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
582 E = DeadPasses.end(); I != E; ++I) {
583 (*I)->releaseMemory();
584
585 std::map<AnalysisID, Pass*>::iterator Pos =
586 AvailableAnalysis.find((*I)->getPassInfo());
587
Devang Patel475c4532006-12-08 00:59:05 +0000588 // It is possible that pass is already removed from the AvailableAnalysis
Devang Patel17ad0962006-12-08 00:37:52 +0000589 if (Pos != AvailableAnalysis.end())
590 AvailableAnalysis.erase(Pos);
591 }
Devang Patelca189262006-11-14 03:05:08 +0000592}
593
Devang Patel8f677ce2006-12-07 18:47:25 +0000594/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000595/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Patel2e169c32006-12-07 20:03:49 +0000596void PMDataManager::addPassToManager(Pass *P,
597 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000598
Devang Pateld440cd92006-12-08 23:53:00 +0000599 // This manager is going to manage pass P. Set up analysis resolver
600 // to connect them.
601 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
602 P->setResolver(AR);
603
Devang Patel90b05e02006-11-11 02:04:19 +0000604 if (ProcessAnalysis) {
Devang Patelbc03f132006-12-07 23:55:10 +0000605
606 // At the moment, this pass is the last user of all required passes.
607 std::vector<Pass *> LastUses;
608 std::vector<Pass *> RequiredPasses;
609 unsigned PDepth = this->getDepth();
610
611 collectRequiredAnalysisPasses(RequiredPasses, P);
612 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
613 E = RequiredPasses.end(); I != E; ++I) {
614 Pass *PRequired = *I;
615 unsigned RDepth = 0;
Devang Patel64619be2006-12-09 00:07:38 +0000616
617 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
618 RDepth = DM.getDepth();
619
Devang Patelbc03f132006-12-07 23:55:10 +0000620 if (PDepth == RDepth)
621 LastUses.push_back(PRequired);
622 else if (PDepth > RDepth) {
623 // Let the parent claim responsibility of last use
624 ForcedLastUses.push_back(PRequired);
625 } else {
626 // Note : This feature is not yet implemented
627 assert (0 &&
628 "Unable to handle Pass that requires lower level Analysis pass");
629 }
630 }
631
632 if (!LastUses.empty())
633 TPM->setLastUser(LastUses, P);
634
Devang Patel17bff0d2006-12-07 22:09:36 +0000635 // Take a note of analysis required and made available by this pass.
Devang Patel90b05e02006-11-11 02:04:19 +0000636 // Remove the analysis not preserved by this pass
Devang Patel17bff0d2006-12-07 22:09:36 +0000637 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000638 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000639 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000640 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000641
642 // Add pass
643 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000644}
645
Devang Patel1d6267c2006-12-07 23:05:44 +0000646/// Populate RequiredPasses with the analysis pass that are required by
647/// pass P.
648void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
649 Pass *P) {
650 AnalysisUsage AnUsage;
651 P->getAnalysisUsage(AnUsage);
652 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
653 for (std::vector<AnalysisID>::const_iterator
654 I = RequiredSet.begin(), E = RequiredSet.end();
655 I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000656 Pass *AnalysisPass = findAnalysisPass(*I, true);
Devang Patel1d6267c2006-12-07 23:05:44 +0000657 assert (AnalysisPass && "Analysis pass is not available");
658 RP.push_back(AnalysisPass);
659 }
660}
661
Devang Patel07f4f582006-11-14 21:49:36 +0000662// All Required analyses should be available to the pass as it runs! Here
663// we fill in the AnalysisImpls member of the pass so that it can
664// successfully use the getAnalysis() method to retrieve the
665// implementations it needs.
666//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000667void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000668 AnalysisUsage AnUsage;
669 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000670
671 for (std::vector<const PassInfo *>::const_iterator
672 I = AnUsage.getRequiredSet().begin(),
673 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +0000674 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel07f4f582006-11-14 21:49:36 +0000675 if (Impl == 0)
676 assert(0 && "Analysis used but not available!");
Devang Patel984698a2006-12-09 01:11:34 +0000677 AnalysisResolver_New *AR = P->getResolver();
678 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +0000679 }
680}
681
Devang Patel640c5bb2006-12-08 22:30:11 +0000682/// Find the pass that implements Analysis AID. If desired pass is not found
683/// then return NULL.
684Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
685
686 // Check if AvailableAnalysis map has one entry.
687 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
688
689 if (I != AvailableAnalysis.end())
690 return I->second;
691
692 // Search Parents through TopLevelManager
693 if (SearchParent)
694 return TPM->findAnalysisPass(AID);
695
Devang Patel9d759b82006-12-09 00:09:12 +0000696 return NULL;
Devang Patel640c5bb2006-12-08 22:30:11 +0000697}
698
Devang Patel9bdf7d42006-12-08 23:28:54 +0000699
700//===----------------------------------------------------------------------===//
701// NOTE: Is this the right place to define this method ?
702// getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
703Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
704 return PM.findAnalysisPass(ID, dir);
705}
706
Devang Patela1514cb2006-12-07 19:39:39 +0000707//===----------------------------------------------------------------------===//
708// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000709
Devang Pateld65e9e92006-11-08 01:31:28 +0000710/// Add pass P into PassVector and return true. If this pass is not
711/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000712bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000713BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000714
715 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
716 if (!BP)
717 return false;
718
Devang Patel3c8eb622006-11-07 22:56:50 +0000719 // If this pass does not preserve anlysis that is used by other passes
720 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000721 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000722 return false;
723
Devang Patel8cad70d2006-11-11 01:51:02 +0000724 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000725
Devang Patel6e5a1132006-11-07 21:31:57 +0000726 return true;
727}
728
729/// Execute all of the passes scheduled for execution by invoking
730/// runOnBasicBlock method. Keep track of whether any of the passes modifies
731/// the function, and if so, return true.
732bool
733BasicBlockPassManager_New::runOnFunction(Function &F) {
734
Devang Patele9585592006-12-08 01:38:28 +0000735 bool Changed = doInitialization(F);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000736 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000737
Devang Patel6e5a1132006-11-07 21:31:57 +0000738 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000739 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
740 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000741 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000742
Devang Patel6e5a1132006-11-07 21:31:57 +0000743 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
744 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000745 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000746 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000747 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000748 }
Devang Patele9585592006-12-08 01:38:28 +0000749 return Changed | doFinalization(F);
Devang Patel6e5a1132006-11-07 21:31:57 +0000750}
751
Devang Patel475c4532006-12-08 00:59:05 +0000752// Implement doInitialization and doFinalization
753inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
754 bool Changed = false;
755
756 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
757 e = passVectorEnd(); itr != e; ++itr) {
758 Pass *P = *itr;
759 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
760 Changed |= BP->doInitialization(M);
761 }
762
763 return Changed;
764}
765
766inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
767 bool Changed = false;
768
769 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
770 e = passVectorEnd(); itr != e; ++itr) {
771 Pass *P = *itr;
772 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
773 Changed |= BP->doFinalization(M);
774 }
775
776 return Changed;
777}
778
779inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
780 bool Changed = false;
781
782 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
783 e = passVectorEnd(); itr != e; ++itr) {
784 Pass *P = *itr;
785 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
786 Changed |= BP->doInitialization(F);
787 }
788
789 return Changed;
790}
791
792inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
793 bool Changed = false;
794
795 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
796 e = passVectorEnd(); itr != e; ++itr) {
797 Pass *P = *itr;
798 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
799 Changed |= BP->doFinalization(F);
800 }
801
802 return Changed;
803}
804
805
Devang Patela1514cb2006-12-07 19:39:39 +0000806//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000807// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000808
Devang Patel4e12f862006-11-08 10:44:40 +0000809/// Create new Function pass manager
810FunctionPassManager_New::FunctionPassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +0000811 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel4e12f862006-11-08 10:44:40 +0000812}
813
Devang Patel1f653682006-12-08 18:57:16 +0000814FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
815 FPM = new FunctionPassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +0000816 // FPM is the top level manager.
817 FPM->setTopLevelManager(FPM);
Devang Patel1f653682006-12-08 18:57:16 +0000818 MP = P;
819}
820
Devang Patel4e12f862006-11-08 10:44:40 +0000821/// add - Add a pass to the queue of passes to run. This passes
822/// ownership of the Pass to the PassManager. When the
823/// PassManager_X is destroyed, the pass will be destroyed as well, so
824/// there is no need to delete the pass. (TODO delete passes.)
825/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000826void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000827 FPM->add(P);
828}
829
830/// Execute all of the passes scheduled for execution. Keep
831/// track of whether any of the passes modifies the function, and if
832/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000833bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000834 return FPM->runOnModule(M);
835}
836
Devang Patel9f3083e2006-11-15 19:39:54 +0000837/// run - Execute all of the passes scheduled for execution. Keep
838/// track of whether any of the passes modifies the function, and if
839/// so, return true.
840///
841bool FunctionPassManager_New::run(Function &F) {
842 std::string errstr;
843 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000844 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000845 abort();
846 }
Devang Patel272908d2006-12-08 22:57:48 +0000847 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +0000848}
849
850
Devang Patelff631ae2006-11-15 01:27:05 +0000851/// doInitialization - Run all of the initializers for the function passes.
852///
853bool FunctionPassManager_New::doInitialization() {
854 return FPM->doInitialization(*MP->getModule());
855}
856
857/// doFinalization - Run all of the initializers for the function passes.
858///
859bool FunctionPassManager_New::doFinalization() {
860 return FPM->doFinalization(*MP->getModule());
861}
862
Devang Patela1514cb2006-12-07 19:39:39 +0000863//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000864// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000865
Devang Patel0c2012f2006-11-07 21:49:50 +0000866/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
867/// either use it into active basic block pass manager or create new basic
868/// block pass manager to handle pass P.
869bool
Devang Patel4e12f862006-11-08 10:44:40 +0000870FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000871
872 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
873 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
874
Devang Patel4949fe02006-12-07 22:34:21 +0000875 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000876
Devang Patel4949fe02006-12-07 22:34:21 +0000877 // If active manager exists then clear its analysis info.
878 if (activeBBPassManager)
879 activeBBPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +0000880
Devang Patel4949fe02006-12-07 22:34:21 +0000881 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +0000882 activeBBPassManager =
883 new BasicBlockPassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +0000884 // Inherit top level manager
885 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patel90b05e02006-11-11 02:04:19 +0000886 addPassToManager(activeBBPassManager, false);
Devang Patelaf1fca52006-12-08 23:11:43 +0000887 TPM->addOtherPassManager(activeBBPassManager);
Devang Patel4949fe02006-12-07 22:34:21 +0000888
889 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +0000890 if (!activeBBPassManager->addPass(BP))
891 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000892 }
Devang Patelbc03f132006-12-07 23:55:10 +0000893
894 if (!ForcedLastUses.empty())
895 TPM->setLastUser(ForcedLastUses, this);
896
Devang Patel0c2012f2006-11-07 21:49:50 +0000897 return true;
898 }
899
900 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
901 if (!FP)
902 return false;
903
Devang Patel3c8eb622006-11-07 22:56:50 +0000904 // If this pass does not preserve anlysis that is used by other passes
905 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000906 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000907 return false;
908
Devang Patel8cad70d2006-11-11 01:51:02 +0000909 addPassToManager (FP);
Devang Patel4949fe02006-12-07 22:34:21 +0000910
911 // If active manager exists then clear its analysis info.
912 if (activeBBPassManager) {
913 activeBBPassManager->initializeAnalysisInfo();
914 activeBBPassManager = NULL;
915 }
916
Devang Patel0c2012f2006-11-07 21:49:50 +0000917 return true;
918}
919
920/// Execute all of the passes scheduled for execution by invoking
921/// runOnFunction method. Keep track of whether any of the passes modifies
922/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000923bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000924
Devang Patel0e29e292006-12-08 19:04:09 +0000925 bool Changed = doInitialization(M);
Devang Patela6b6dcb2006-12-07 18:41:09 +0000926 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000927
Devang Patel0c2012f2006-11-07 21:49:50 +0000928 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel19290892006-12-08 19:03:05 +0000929 this->runOnFunction(*I);
930
Devang Patel0e29e292006-12-08 19:04:09 +0000931 return Changed | doFinalization(M);
Devang Patel0c2012f2006-11-07 21:49:50 +0000932}
933
Devang Patel9f3083e2006-11-15 19:39:54 +0000934/// Execute all of the passes scheduled for execution by invoking
935/// runOnFunction method. Keep track of whether any of the passes modifies
936/// the function, and if so, return true.
937bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
938
939 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000940 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000941
942 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
943 e = passVectorEnd(); itr != e; ++itr) {
944 Pass *P = *itr;
945
Devang Patel9f3083e2006-11-15 19:39:54 +0000946 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
947 Changed |= FP->runOnFunction(F);
948 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +0000949 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000950 removeDeadPasses(P);
951 }
952 return Changed;
953}
954
955
Devang Patelff631ae2006-11-15 01:27:05 +0000956inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
957 bool Changed = false;
958
959 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
960 e = passVectorEnd(); itr != e; ++itr) {
961 Pass *P = *itr;
962
963 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
964 Changed |= FP->doInitialization(M);
965 }
966
967 return Changed;
968}
969
970inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
971 bool Changed = false;
972
973 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
974 e = passVectorEnd(); itr != e; ++itr) {
975 Pass *P = *itr;
976
977 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
978 Changed |= FP->doFinalization(M);
979 }
980
Devang Patelff631ae2006-11-15 01:27:05 +0000981 return Changed;
982}
983
Devang Patel272908d2006-12-08 22:57:48 +0000984// Execute all the passes managed by this top level manager.
985// Return true if any function is modified by a pass.
986bool FunctionPassManagerImpl_New::run(Function &F) {
987
988 bool Changed = false;
989 for (std::vector<Pass *>::iterator I = passManagersBegin(),
990 E = passManagersEnd(); I != E; ++I) {
991 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
992 Changed |= FP->runOnFunction(F);
993 }
994 return Changed;
995}
996
Devang Patela1514cb2006-12-07 19:39:39 +0000997//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000998// ModulePassManager implementation
999
1000/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +00001001/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +00001002/// is not manageable by this manager.
1003bool
Devang Pateld65e9e92006-11-08 01:31:28 +00001004ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +00001005
1006 // If P is FunctionPass then use function pass maanager.
1007 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1008
Devang Patel640c5bb2006-12-08 22:30:11 +00001009 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
Devang Patel05e1a972006-11-07 22:03:15 +00001010
Devang Patel4949fe02006-12-07 22:34:21 +00001011 // If active manager exists then clear its analysis info.
1012 if (activeFunctionPassManager)
1013 activeFunctionPassManager->initializeAnalysisInfo();
Devang Patel642c1432006-12-07 21:58:50 +00001014
Devang Patel4949fe02006-12-07 22:34:21 +00001015 // Create and add new manager
Devang Patel4c36e6b2006-12-07 23:24:58 +00001016 activeFunctionPassManager =
1017 new FunctionPassManagerImpl_New(getDepth() + 1);
Devang Patel90b05e02006-11-11 02:04:19 +00001018 addPassToManager(activeFunctionPassManager, false);
Devang Patel9c6290c2006-12-12 22:02:16 +00001019 // Inherit top level manager
1020 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
Devang Patelaf1fca52006-12-08 23:11:43 +00001021 TPM->addOtherPassManager(activeFunctionPassManager);
1022
Devang Patel4949fe02006-12-07 22:34:21 +00001023 // Add pass into new manager. This time it must succeed.
Devang Pateld65e9e92006-11-08 01:31:28 +00001024 if (!activeFunctionPassManager->addPass(FP))
1025 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +00001026 }
Devang Patelbc03f132006-12-07 23:55:10 +00001027
1028 if (!ForcedLastUses.empty())
1029 TPM->setLastUser(ForcedLastUses, this);
1030
Devang Patel05e1a972006-11-07 22:03:15 +00001031 return true;
1032 }
1033
1034 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1035 if (!MP)
1036 return false;
1037
Devang Patel3c8eb622006-11-07 22:56:50 +00001038 // If this pass does not preserve anlysis that is used by other passes
1039 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +00001040 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +00001041 return false;
1042
Devang Patel8cad70d2006-11-11 01:51:02 +00001043 addPassToManager(MP);
Devang Patel4949fe02006-12-07 22:34:21 +00001044 // If active manager exists then clear its analysis info.
1045 if (activeFunctionPassManager) {
1046 activeFunctionPassManager->initializeAnalysisInfo();
1047 activeFunctionPassManager = NULL;
1048 }
1049
Devang Patel05e1a972006-11-07 22:03:15 +00001050 return true;
1051}
1052
1053
1054/// Execute all of the passes scheduled for execution by invoking
1055/// runOnModule method. Keep track of whether any of the passes modifies
1056/// the module, and if so, return true.
1057bool
1058ModulePassManager_New::runOnModule(Module &M) {
1059 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +00001060 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +00001061
Devang Patel8cad70d2006-11-11 01:51:02 +00001062 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1063 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +00001064 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +00001065
Devang Patel05e1a972006-11-07 22:03:15 +00001066 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1067 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +00001068 removeNotPreservedAnalysis(P);
Devang Patel17bff0d2006-12-07 22:09:36 +00001069 recordAvailableAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +00001070 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +00001071 }
1072 return Changed;
1073}
1074
Devang Patela1514cb2006-12-07 19:39:39 +00001075//===----------------------------------------------------------------------===//
1076// PassManagerImpl implementation
1077
Devang Patelc290c8a2006-11-07 22:23:34 +00001078// PassManager_New implementation
1079/// Add P into active pass manager or use new module pass manager to
1080/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001081bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001082
Devang Patel6c9f5482006-11-11 00:42:16 +00001083 if (!activeManager || !activeManager->addPass(P)) {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001084 activeManager = new ModulePassManager_New(getDepth() + 1);
Devang Patel9c6290c2006-12-12 22:02:16 +00001085 // Inherit top level manager
1086 activeManager->setTopLevelManager(this->getTopLevelManager());
Devang Pateld440cd92006-12-08 23:53:00 +00001087
1088 // This top level manager is going to manage activeManager.
1089 // Set up analysis resolver to connect them.
1090 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1091 activeManager->setResolver(AR);
1092
Devang Patel5bbeb492006-12-08 22:47:25 +00001093 addPassManager(activeManager);
Devang Patel28bbcbe2006-12-07 21:44:12 +00001094 return activeManager->addPass(P);
Devang Patelc290c8a2006-11-07 22:23:34 +00001095 }
Devang Patel28bbcbe2006-12-07 21:44:12 +00001096 return true;
Devang Patelc290c8a2006-11-07 22:23:34 +00001097}
1098
1099/// run - Execute all of the passes scheduled for execution. Keep track of
1100/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +00001101bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001102
Devang Patelc290c8a2006-11-07 22:23:34 +00001103 bool Changed = false;
Devang Patel5bbeb492006-12-08 22:47:25 +00001104 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1105 E = passManagersEnd(); I != E; ++I) {
1106 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1107 Changed |= MP->runOnModule(M);
Devang Patelc290c8a2006-11-07 22:23:34 +00001108 }
1109 return Changed;
1110}
Devang Patel376fefa2006-11-08 10:29:57 +00001111
Devang Patela1514cb2006-12-07 19:39:39 +00001112//===----------------------------------------------------------------------===//
1113// PassManager implementation
1114
Devang Patel376fefa2006-11-08 10:29:57 +00001115/// Create new pass manager
1116PassManager_New::PassManager_New() {
Devang Patel4c36e6b2006-12-07 23:24:58 +00001117 PM = new PassManagerImpl_New(0);
Devang Patel9c6290c2006-12-12 22:02:16 +00001118 // PM is the top level manager
1119 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001120}
1121
1122/// add - Add a pass to the queue of passes to run. This passes ownership of
1123/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1124/// will be destroyed as well, so there is no need to delete the pass. This
1125/// implies that all passes MUST be allocated with 'new'.
1126void
1127PassManager_New::add(Pass *P) {
1128 PM->add(P);
1129}
1130
1131/// run - Execute all of the passes scheduled for execution. Keep track of
1132/// whether any of the passes modifies the module, and if so, return true.
1133bool
1134PassManager_New::run(Module &M) {
1135 return PM->run(M);
1136}
1137