blob: d4db1d14537f37dd00b6440d776bff50bfe36d95 [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.
109 void schedulePass(Pass *P, Pass *PM);
110
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
121 virtual ~PMTopLevelManager() {
122 PassManagers.clear();
123 }
124
125private:
126
127 /// Collection of pass managers
128 std::vector<Pass *> PassManagers;
129
130 // Map to keep track of last user of the analysis pass.
131 // LastUser->second is the last user of Lastuser->first.
132 std::map<Pass *, Pass *> LastUser;
133};
134
135/// Set pass P as the last user of the given analysis passes.
136void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
137 Pass *P) {
138
139 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
140 E = AnalysisPasses.end(); I != E; ++I) {
141 Pass *AP = *I;
142 LastUser[AP] = P;
143 // If AP is the last user of other passes then make P last user of
144 // such passes.
145 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
146 LUE = LastUser.end(); LUI != LUE; ++LUI) {
147 if (LUI->second == AP)
148 LastUser[LUI->first] = P;
149 }
150 }
151
152}
153
154/// Collect passes whose last user is P
155void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
156 Pass *P) {
157 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
158 LUE = LastUser.end(); LUI != LUE; ++LUI)
159 if (LUI->second == P)
160 LastUses.push_back(LUI->first);
161}
162
163
164
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000165/// PMDataManager provides the common place to manage the analysis data
166/// used by pass managers.
167class PMDataManager {
Devang Patela9844592006-11-11 01:31:05 +0000168
169public:
170
171 /// Return true IFF pass P's required analysis set does not required new
172 /// manager.
173 bool manageablePass(Pass *P);
174
Devang Patelf60b5d92006-11-14 01:59:59 +0000175 Pass *getAnalysisPass(AnalysisID AID) const {
176
177 std::map<AnalysisID, Pass*>::const_iterator I =
178 AvailableAnalysis.find(AID);
179
180 if (I != AvailableAnalysis.end())
181 return NULL;
182 else
183 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +0000184 }
Devang Patela9844592006-11-11 01:31:05 +0000185
Devang Patela9844592006-11-11 01:31:05 +0000186 /// Augment AvailableAnalysis by adding analysis made available by pass P.
187 void noteDownAvailableAnalysis(Pass *P);
188
Devang Patela9844592006-11-11 01:31:05 +0000189 /// Remove Analysis that is not preserved by the pass
190 void removeNotPreservedAnalysis(Pass *P);
191
192 /// Remove dead passes
Devang Patelca189262006-11-14 03:05:08 +0000193 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000194
Devang Patel8f677ce2006-12-07 18:47:25 +0000195 /// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000196 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
197 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +0000198
Devang Patela6b6dcb2006-12-07 18:41:09 +0000199 // Initialize available analysis information.
200 void initializeAnalysisInfo() {
Devang Patel050ec722006-11-14 01:23:29 +0000201 AvailableAnalysis.clear();
Devang Patel3f0832a2006-11-14 02:54:23 +0000202 LastUser.clear();
Devang Patel050ec722006-11-14 01:23:29 +0000203 }
204
Devang Patelf60b5d92006-11-14 01:59:59 +0000205 // All Required analyses should be available to the pass as it runs! Here
206 // we fill in the AnalysisImpls member of the pass so that it can
207 // successfully use the getAnalysis() method to retrieve the
208 // implementations it needs.
209 //
Devang Patel07f4f582006-11-14 21:49:36 +0000210 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +0000211
Devang Patel8cad70d2006-11-11 01:51:02 +0000212 inline std::vector<Pass *>::iterator passVectorBegin() {
213 return PassVector.begin();
214 }
215
216 inline std::vector<Pass *>::iterator passVectorEnd() {
217 return PassVector.end();
218 }
219
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000220 inline void setLastUser(Pass *P, Pass *LU) {
Devang Patel07f4f582006-11-14 21:49:36 +0000221 LastUser[P] = LU;
222 // TODO : Check if pass P is available.
Devang Patel07f4f582006-11-14 21:49:36 +0000223 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000224
Devang Patela9844592006-11-11 01:31:05 +0000225private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000226 // Set of available Analysis. This information is used while scheduling
227 // pass. If a pass requires an analysis which is not not available then
228 // equired analysis pass is scheduled to run before the pass itself is
229 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000230 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000231
Devang Patel3f0832a2006-11-14 02:54:23 +0000232 // Map to keep track of last user of the analysis pass.
233 // LastUser->second is the last user of Lastuser->first.
234 std::map<Pass *, Pass *> LastUser;
235
Devang Patel8cad70d2006-11-11 01:51:02 +0000236 // Collection of pass that are managed by this manager
237 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +0000238};
239
Devang Patelca58e352006-11-08 10:05:38 +0000240/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
241/// pass together and sequence them to process one basic block before
242/// processing next basic block.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000243class BasicBlockPassManager_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000244 public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000245
246public:
247 BasicBlockPassManager_New() { }
248
249 /// Add a pass into a passmanager queue.
250 bool addPass(Pass *p);
251
252 /// Execute all of the passes scheduled for execution. Keep track of
253 /// whether any of the passes modifies the function, and if so, return true.
254 bool runOnFunction(Function &F);
255
Devang Patelebba9702006-11-13 22:40:09 +0000256 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000257 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000258
Devang Patelca58e352006-11-08 10:05:38 +0000259private:
Devang Patelca58e352006-11-08 10:05:38 +0000260};
261
Devang Patel4e12f862006-11-08 10:44:40 +0000262/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000263/// It batches all function passes and basic block pass managers together and
264/// sequence them to process one function at a time before processing next
265/// function.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000266class FunctionPassManagerImpl_New : public PMDataManager,
Devang Patel42add712006-11-15 01:11:27 +0000267 public ModulePass {
Devang Patelca58e352006-11-08 10:05:38 +0000268public:
Devang Patel4e12f862006-11-08 10:44:40 +0000269 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
270 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000271 activeBBPassManager = NULL;
272 }
Devang Patel4e12f862006-11-08 10:44:40 +0000273 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000274
275 /// add - Add a pass to the queue of passes to run. This passes
276 /// ownership of the Pass to the PassManager. When the
277 /// PassManager_X is destroyed, the pass will be destroyed as well, so
278 /// there is no need to delete the pass. (TODO delete passes.)
279 /// This implies that all passes MUST be allocated with 'new'.
280 void add(Pass *P) { /* TODO*/ }
281
282 /// Add pass into the pass manager queue.
283 bool addPass(Pass *P);
284
285 /// Execute all of the passes scheduled for execution. Keep
286 /// track of whether any of the passes modifies the function, and if
287 /// so, return true.
288 bool runOnModule(Module &M);
Devang Patel9f3083e2006-11-15 19:39:54 +0000289 bool runOnFunction(Function &F);
Devang Patelca58e352006-11-08 10:05:38 +0000290
Devang Patelebba9702006-11-13 22:40:09 +0000291 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000292 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000293
Devang Patelff631ae2006-11-15 01:27:05 +0000294 /// doInitialization - Run all of the initializers for the function passes.
295 ///
296 bool doInitialization(Module &M);
297
298 /// doFinalization - Run all of the initializers for the function passes.
299 ///
300 bool doFinalization(Module &M);
Devang Patelca58e352006-11-08 10:05:38 +0000301private:
Devang Patelca58e352006-11-08 10:05:38 +0000302 // Active Pass Managers
303 BasicBlockPassManager_New *activeBBPassManager;
304};
305
306/// ModulePassManager_New manages ModulePasses and function pass managers.
307/// It batches all Module passes passes and function pass managers together and
308/// sequence them to process one module.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000309class ModulePassManager_New : public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000310
311public:
312 ModulePassManager_New() { activeFunctionPassManager = NULL; }
313
314 /// Add a pass into a passmanager queue.
315 bool addPass(Pass *p);
316
317 /// run - Execute all of the passes scheduled for execution. Keep track of
318 /// whether any of the passes modifies the module, and if so, return true.
319 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000320
321 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000322 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000323
324private:
Devang Patelca58e352006-11-08 10:05:38 +0000325 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000326 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000327};
328
Devang Patel376fefa2006-11-08 10:29:57 +0000329/// PassManager_New manages ModulePassManagers
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000330class PassManagerImpl_New : public PMDataManager {
Devang Patel376fefa2006-11-08 10:29:57 +0000331
332public:
333
334 /// add - Add a pass to the queue of passes to run. This passes ownership of
335 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
336 /// will be destroyed as well, so there is no need to delete the pass. This
337 /// implies that all passes MUST be allocated with 'new'.
338 void add(Pass *P);
339
340 /// run - Execute all of the passes scheduled for execution. Keep track of
341 /// whether any of the passes modifies the module, and if so, return true.
342 bool run(Module &M);
343
Devang Patelebba9702006-11-13 22:40:09 +0000344 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000345 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000346
Devang Patel376fefa2006-11-08 10:29:57 +0000347private:
348
349 /// Add a pass into a passmanager queue. This is used by schedulePasses
350 bool addPass(Pass *p);
351
Devang Patel1a6eaa42006-11-11 02:22:31 +0000352 /// Schedule pass P for execution. Make sure that passes required by
353 /// P are run before P is run. Update analysis info maintained by
354 /// the manager. Remove dead passes. This is a recursive function.
355 void schedulePass(Pass *P);
356
Devang Patel376fefa2006-11-08 10:29:57 +0000357 /// Schedule all passes collected in pass queue using add(). Add all the
358 /// schedule passes into various manager's queue using addPass().
359 void schedulePasses();
360
361 // Collection of pass managers
362 std::vector<ModulePassManager_New *> PassManagers;
363
Devang Patel376fefa2006-11-08 10:29:57 +0000364 // Active Pass Manager
365 ModulePassManager_New *activeManager;
366};
367
Devang Patelca58e352006-11-08 10:05:38 +0000368} // End of llvm namespace
369
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000370// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000371
Devang Pateld65e9e92006-11-08 01:31:28 +0000372/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000373/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000374bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000375
Devang Patel8f677ce2006-12-07 18:47:25 +0000376 // TODO
377 // If this pass is not preserving information that is required by a
378 // pass maintained by higher level pass manager then do not insert
379 // this pass into current manager. Use new manager. For example,
380 // For example, If FunctionPass F is not preserving ModulePass Info M1
381 // that is used by another ModulePass M2 then do not insert F in
382 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000383 return true;
384}
385
Devang Patel643676c2006-11-11 01:10:19 +0000386/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000387void PMDataManager::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000388
Devang Patel643676c2006-11-11 01:10:19 +0000389 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000390 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000391
392 //TODO This pass is the current implementation of all of the interfaces it
393 //TODO implements as well.
394 //TODO
395 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
396 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
397 //TODO CurrentAnalyses[II[i]] = P;
398 }
399}
400
Devang Patelf68a3492006-11-07 22:35:17 +0000401/// Remove Analyss not preserved by Pass P
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000402void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000403 AnalysisUsage AnUsage;
404 P->getAnalysisUsage(AnUsage);
405 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000406
Devang Patelf60b5d92006-11-14 01:59:59 +0000407 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000408 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000409 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000410 PreservedSet.end()) {
411 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000412 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000413 AvailableAnalysis.erase(J);
414 }
415 }
Devang Patelf68a3492006-11-07 22:35:17 +0000416}
417
Devang Patelca189262006-11-14 03:05:08 +0000418/// Remove analysis passes that are not used any longer
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000419void PMDataManager::removeDeadPasses(Pass *P) {
Devang Patelca189262006-11-14 03:05:08 +0000420
421 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
422 E = LastUser.end(); I !=E; ++I) {
423 if (I->second == P) {
424 Pass *deadPass = I->first;
425 deadPass->releaseMemory();
426
427 std::map<AnalysisID, Pass*>::iterator Pos =
428 AvailableAnalysis.find(deadPass->getPassInfo());
429
430 assert (Pos != AvailableAnalysis.end() &&
431 "Pass is not available");
432 AvailableAnalysis.erase(Pos);
433 }
434 }
435}
436
Devang Patel8f677ce2006-12-07 18:47:25 +0000437/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000438/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000439void PMDataManager::addPassToManager (Pass *P,
Devang Patel90b05e02006-11-11 02:04:19 +0000440 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000441
Devang Patel90b05e02006-11-11 02:04:19 +0000442 if (ProcessAnalysis) {
443 // Take a note of analysis required and made available by this pass
Devang Patel8f677ce2006-12-07 18:47:25 +0000444 initializeAnalysisImpl(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000445 noteDownAvailableAnalysis(P);
446
447 // Remove the analysis not preserved by this pass
448 removeNotPreservedAnalysis(P);
449 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000450
451 // Add pass
452 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000453}
454
Devang Patel07f4f582006-11-14 21:49:36 +0000455// All Required analyses should be available to the pass as it runs! Here
456// we fill in the AnalysisImpls member of the pass so that it can
457// successfully use the getAnalysis() method to retrieve the
458// implementations it needs.
459//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000460void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelff631ae2006-11-15 01:27:05 +0000461 AnalysisUsage AnUsage;
462 P->getAnalysisUsage(AnUsage);
Devang Patel07f4f582006-11-14 21:49:36 +0000463
464 for (std::vector<const PassInfo *>::const_iterator
465 I = AnUsage.getRequiredSet().begin(),
466 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
467 Pass *Impl = getAnalysisPass(*I);
468 if (Impl == 0)
469 assert(0 && "Analysis used but not available!");
470 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
471 }
472}
473
Devang Patel6e5a1132006-11-07 21:31:57 +0000474/// BasicBlockPassManager implementation
475
Devang Pateld65e9e92006-11-08 01:31:28 +0000476/// Add pass P into PassVector and return true. If this pass is not
477/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000478bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000479BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000480
481 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
482 if (!BP)
483 return false;
484
Devang Patel3c8eb622006-11-07 22:56:50 +0000485 // If this pass does not preserve anlysis that is used by other passes
486 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000487 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000488 return false;
489
Devang Patel8cad70d2006-11-11 01:51:02 +0000490 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000491
Devang Patel6e5a1132006-11-07 21:31:57 +0000492 return true;
493}
494
495/// Execute all of the passes scheduled for execution by invoking
496/// runOnBasicBlock method. Keep track of whether any of the passes modifies
497/// the function, and if so, return true.
498bool
499BasicBlockPassManager_New::runOnFunction(Function &F) {
500
501 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000502 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000503
Devang Patel6e5a1132006-11-07 21:31:57 +0000504 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000505 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
506 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000507 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000508
509 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000510 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
511 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000512 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000513 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000514 }
515 return Changed;
516}
517
Devang Patelebba9702006-11-13 22:40:09 +0000518/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000519Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
520 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000521}
522
Devang Patel0c2012f2006-11-07 21:49:50 +0000523// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000524/// Create new Function pass manager
525FunctionPassManager_New::FunctionPassManager_New() {
526 FPM = new FunctionPassManagerImpl_New();
527}
528
529/// add - Add a pass to the queue of passes to run. This passes
530/// ownership of the Pass to the PassManager. When the
531/// PassManager_X is destroyed, the pass will be destroyed as well, so
532/// there is no need to delete the pass. (TODO delete passes.)
533/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000534void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000535 FPM->add(P);
536}
537
538/// Execute all of the passes scheduled for execution. Keep
539/// track of whether any of the passes modifies the function, and if
540/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000541bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000542 return FPM->runOnModule(M);
543}
544
Devang Patel9f3083e2006-11-15 19:39:54 +0000545/// run - Execute all of the passes scheduled for execution. Keep
546/// track of whether any of the passes modifies the function, and if
547/// so, return true.
548///
549bool FunctionPassManager_New::run(Function &F) {
550 std::string errstr;
551 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000552 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000553 abort();
554 }
555 return FPM->runOnFunction(F);
556}
557
558
Devang Patelff631ae2006-11-15 01:27:05 +0000559/// doInitialization - Run all of the initializers for the function passes.
560///
561bool FunctionPassManager_New::doInitialization() {
562 return FPM->doInitialization(*MP->getModule());
563}
564
565/// doFinalization - Run all of the initializers for the function passes.
566///
567bool FunctionPassManager_New::doFinalization() {
568 return FPM->doFinalization(*MP->getModule());
569}
570
Devang Patel4e12f862006-11-08 10:44:40 +0000571// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000572
Devang Patel0c2012f2006-11-07 21:49:50 +0000573// FunctionPassManager
574
575/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
576/// either use it into active basic block pass manager or create new basic
577/// block pass manager to handle pass P.
578bool
Devang Patel4e12f862006-11-08 10:44:40 +0000579FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000580
581 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
582 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
583
584 if (!activeBBPassManager
585 || !activeBBPassManager->addPass(BP)) {
586
587 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000588 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000589 if (!activeBBPassManager->addPass(BP))
590 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000591 }
592 return true;
593 }
594
595 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
596 if (!FP)
597 return false;
598
Devang Patel3c8eb622006-11-07 22:56:50 +0000599 // If this pass does not preserve anlysis that is used by other passes
600 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000601 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000602 return false;
603
Devang Patel8cad70d2006-11-11 01:51:02 +0000604 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000605 activeBBPassManager = NULL;
606 return true;
607}
608
609/// Execute all of the passes scheduled for execution by invoking
610/// runOnFunction method. Keep track of whether any of the passes modifies
611/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000612bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000613
614 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000615 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000616
Devang Patel0c2012f2006-11-07 21:49:50 +0000617 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000618 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
619 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000620 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000621
622 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000623 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
624 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000625 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000626 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000627 }
628 return Changed;
629}
630
Devang Patel9f3083e2006-11-15 19:39:54 +0000631/// Execute all of the passes scheduled for execution by invoking
632/// runOnFunction method. Keep track of whether any of the passes modifies
633/// the function, and if so, return true.
634bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
635
636 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000637 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000638
639 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
640 e = passVectorEnd(); itr != e; ++itr) {
641 Pass *P = *itr;
642
643 noteDownAvailableAnalysis(P);
644 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
645 Changed |= FP->runOnFunction(F);
646 removeNotPreservedAnalysis(P);
647 removeDeadPasses(P);
648 }
649 return Changed;
650}
651
652
Devang Patelebba9702006-11-13 22:40:09 +0000653/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000654Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000655
Devang Patel3f0832a2006-11-14 02:54:23 +0000656 Pass *P = getAnalysisPass(AID);
657 if (P)
658 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000659
660 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000661 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000662 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000663
664 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000665 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000666}
Devang Patel0c2012f2006-11-07 21:49:50 +0000667
Devang Patelff631ae2006-11-15 01:27:05 +0000668inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
669 bool Changed = false;
670
671 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
672 e = passVectorEnd(); itr != e; ++itr) {
673 Pass *P = *itr;
674
675 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
676 Changed |= FP->doInitialization(M);
677 }
678
679 return Changed;
680}
681
682inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
683 bool Changed = false;
684
685 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
686 e = passVectorEnd(); itr != e; ++itr) {
687 Pass *P = *itr;
688
689 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
690 Changed |= FP->doFinalization(M);
691 }
692
693
694 return Changed;
695}
696
697
Devang Patel05e1a972006-11-07 22:03:15 +0000698// ModulePassManager implementation
699
700/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000701/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000702/// is not manageable by this manager.
703bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000704ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000705
706 // If P is FunctionPass then use function pass maanager.
707 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
708
709 activeFunctionPassManager = NULL;
710
711 if (!activeFunctionPassManager
712 || !activeFunctionPassManager->addPass(P)) {
713
Devang Patel4e12f862006-11-08 10:44:40 +0000714 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000715 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000716 if (!activeFunctionPassManager->addPass(FP))
717 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000718 }
719 return true;
720 }
721
722 ModulePass *MP = dynamic_cast<ModulePass *>(P);
723 if (!MP)
724 return false;
725
Devang Patel3c8eb622006-11-07 22:56:50 +0000726 // If this pass does not preserve anlysis that is used by other passes
727 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000728 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000729 return false;
730
Devang Patel8cad70d2006-11-11 01:51:02 +0000731 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000732 activeFunctionPassManager = NULL;
733 return true;
734}
735
736
737/// Execute all of the passes scheduled for execution by invoking
738/// runOnModule method. Keep track of whether any of the passes modifies
739/// the module, and if so, return true.
740bool
741ModulePassManager_New::runOnModule(Module &M) {
742 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000743 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000744
Devang Patel8cad70d2006-11-11 01:51:02 +0000745 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
746 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000747 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000748
749 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000750 ModulePass *MP = dynamic_cast<ModulePass*>(P);
751 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000752 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000753 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000754 }
755 return Changed;
756}
757
Devang Patelebba9702006-11-13 22:40:09 +0000758/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000759Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000760
Devang Patel3f0832a2006-11-14 02:54:23 +0000761
762 Pass *P = getAnalysisPass(AID);
763 if (P)
764 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000765
766 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000767 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000768 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000769
770 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000771 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000772}
773
774/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000775Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000776
Devang Patel3f0832a2006-11-14 02:54:23 +0000777 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000778 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000779 e = PassManagers.end(); !P && itr != e; ++itr)
780 P = (*itr)->getAnalysisPassFromManager(AID);
781 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000782}
783
Devang Patel1a6eaa42006-11-11 02:22:31 +0000784/// Schedule pass P for execution. Make sure that passes required by
785/// P are run before P is run. Update analysis info maintained by
786/// the manager. Remove dead passes. This is a recursive function.
787void PassManagerImpl_New::schedulePass(Pass *P) {
788
789 AnalysisUsage AnUsage;
790 P->getAnalysisUsage(AnUsage);
791 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
792 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
793 E = RequiredSet.end(); I != E; ++I) {
794
Devang Patel3f0832a2006-11-14 02:54:23 +0000795 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
796 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000797 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000798 AnalysisPass = (*I)->createPass();
799 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000800 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000801 setLastUser (AnalysisPass, P);
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000802
803 // Prolong live range of analyses that are needed after an analysis pass
804 // is destroyed, for querying by subsequent passes
805 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
806 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
807 E = IDs.end(); I != E; ++I) {
808 Pass *AP = getAnalysisPassFromManager(*I);
809 assert (AP && "Analysis pass is not available");
810 setLastUser(AP, P);
811 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000812 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000813 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000814}
815
Devang Patelc290c8a2006-11-07 22:23:34 +0000816/// Schedule all passes from the queue by adding them in their
817/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000818void PassManagerImpl_New::schedulePasses() {
819 for (std::vector<Pass *>::iterator I = passVectorBegin(),
820 E = passVectorEnd(); I != E; ++I)
821 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000822}
823
824/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000825void PassManagerImpl_New::add(Pass *P) {
826 // Do not process Analysis now. Analysis is process while scheduling
827 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000828 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000829}
830
831// PassManager_New implementation
832/// Add P into active pass manager or use new module pass manager to
833/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000834bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000835
Devang Patel6c9f5482006-11-11 00:42:16 +0000836 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000837 activeManager = new ModulePassManager_New();
838 PassManagers.push_back(activeManager);
839 }
840
841 return activeManager->addPass(P);
842}
843
844/// run - Execute all of the passes scheduled for execution. Keep track of
845/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000846bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000847
848 schedulePasses();
849 bool Changed = false;
850 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
851 e = PassManagers.end(); itr != e; ++itr) {
852 ModulePassManager_New *pm = *itr;
853 Changed |= pm->runOnModule(M);
854 }
855 return Changed;
856}
Devang Patel376fefa2006-11-08 10:29:57 +0000857
858/// Create new pass manager
859PassManager_New::PassManager_New() {
860 PM = new PassManagerImpl_New();
861}
862
863/// add - Add a pass to the queue of passes to run. This passes ownership of
864/// the Pass to the PassManager. When the PassManager is destroyed, the pass
865/// will be destroyed as well, so there is no need to delete the pass. This
866/// implies that all passes MUST be allocated with 'new'.
867void
868PassManager_New::add(Pass *P) {
869 PM->add(P);
870}
871
872/// run - Execute all of the passes scheduled for execution. Keep track of
873/// whether any of the passes modifies the module, and if so, return true.
874bool
875PassManager_New::run(Module &M) {
876 return PM->run(M);
877}
878