blob: bf306e9d5c899140a6588b2898a05ab3bf022879 [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.
Devang Patele9976aa2006-12-07 19:33:53 +0000187 void recordAvailableAnalysis(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +0000188
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 Patela1514cb2006-12-07 19:39:39 +0000370//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000371// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000372
Devang Pateld65e9e92006-11-08 01:31:28 +0000373/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000374/// manager.
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000375bool PMDataManager::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000376
Devang Patel8f677ce2006-12-07 18:47:25 +0000377 // TODO
378 // If this pass is not preserving information that is required by a
379 // pass maintained by higher level pass manager then do not insert
380 // this pass into current manager. Use new manager. For example,
381 // For example, If FunctionPass F is not preserving ModulePass Info M1
382 // that is used by another ModulePass M2 then do not insert F in
383 // current function pass manager.
Devang Patelf68a3492006-11-07 22:35:17 +0000384 return true;
385}
386
Devang Patel643676c2006-11-11 01:10:19 +0000387/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000388void PMDataManager::recordAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000389
Devang Patel643676c2006-11-11 01:10:19 +0000390 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000391 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000392
Devang Patele9976aa2006-12-07 19:33:53 +0000393 //This pass is the current implementation of all of the interfaces it
394 //implements as well.
395 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
396 for (unsigned i = 0, e = II.size(); i != e; ++i)
397 AvailableAnalysis[II[i]] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000398 }
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 Patele9976aa2006-12-07 19:33:53 +0000445 recordAvailableAnalysis(P);
Devang Patel90b05e02006-11-11 02:04:19 +0000446
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 Patela1514cb2006-12-07 19:39:39 +0000474//===----------------------------------------------------------------------===//
475// BasicBlockPassManager_New implementation
Devang Patel6e5a1132006-11-07 21:31:57 +0000476
Devang Pateld65e9e92006-11-08 01:31:28 +0000477/// Add pass P into PassVector and return true. If this pass is not
478/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000479bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000480BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000481
482 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
483 if (!BP)
484 return false;
485
Devang Patel3c8eb622006-11-07 22:56:50 +0000486 // If this pass does not preserve anlysis that is used by other passes
487 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000488 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000489 return false;
490
Devang Patel8cad70d2006-11-11 01:51:02 +0000491 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000492
Devang Patel6e5a1132006-11-07 21:31:57 +0000493 return true;
494}
495
496/// Execute all of the passes scheduled for execution by invoking
497/// runOnBasicBlock method. Keep track of whether any of the passes modifies
498/// the function, and if so, return true.
499bool
500BasicBlockPassManager_New::runOnFunction(Function &F) {
501
502 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000503 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000504
Devang Patel6e5a1132006-11-07 21:31:57 +0000505 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000506 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
507 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000508 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000509
Devang Patele9976aa2006-12-07 19:33:53 +0000510 recordAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000511 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
512 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000513 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000514 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000515 }
516 return Changed;
517}
518
Devang Patelebba9702006-11-13 22:40:09 +0000519/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000520Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
521 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000522}
523
Devang Patela1514cb2006-12-07 19:39:39 +0000524//===----------------------------------------------------------------------===//
Devang Patel0c2012f2006-11-07 21:49:50 +0000525// FunctionPassManager_New implementation
Devang Patela1514cb2006-12-07 19:39:39 +0000526
Devang Patel4e12f862006-11-08 10:44:40 +0000527/// Create new Function pass manager
528FunctionPassManager_New::FunctionPassManager_New() {
529 FPM = new FunctionPassManagerImpl_New();
530}
531
532/// add - Add a pass to the queue of passes to run. This passes
533/// ownership of the Pass to the PassManager. When the
534/// PassManager_X is destroyed, the pass will be destroyed as well, so
535/// there is no need to delete the pass. (TODO delete passes.)
536/// This implies that all passes MUST be allocated with 'new'.
Devang Patel9f3083e2006-11-15 19:39:54 +0000537void FunctionPassManager_New::add(Pass *P) {
Devang Patel4e12f862006-11-08 10:44:40 +0000538 FPM->add(P);
539}
540
541/// Execute all of the passes scheduled for execution. Keep
542/// track of whether any of the passes modifies the function, and if
543/// so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000544bool FunctionPassManager_New::runOnModule(Module &M) {
Devang Patel4e12f862006-11-08 10:44:40 +0000545 return FPM->runOnModule(M);
546}
547
Devang Patel9f3083e2006-11-15 19:39:54 +0000548/// run - Execute all of the passes scheduled for execution. Keep
549/// track of whether any of the passes modifies the function, and if
550/// so, return true.
551///
552bool FunctionPassManager_New::run(Function &F) {
553 std::string errstr;
554 if (MP->materializeFunction(&F, &errstr)) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000555 cerr << "Error reading bytecode file: " << errstr << "\n";
Devang Patel9f3083e2006-11-15 19:39:54 +0000556 abort();
557 }
558 return FPM->runOnFunction(F);
559}
560
561
Devang Patelff631ae2006-11-15 01:27:05 +0000562/// doInitialization - Run all of the initializers for the function passes.
563///
564bool FunctionPassManager_New::doInitialization() {
565 return FPM->doInitialization(*MP->getModule());
566}
567
568/// doFinalization - Run all of the initializers for the function passes.
569///
570bool FunctionPassManager_New::doFinalization() {
571 return FPM->doFinalization(*MP->getModule());
572}
573
Devang Patela1514cb2006-12-07 19:39:39 +0000574//===----------------------------------------------------------------------===//
Devang Patel4e12f862006-11-08 10:44:40 +0000575// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000576
Devang Patel0c2012f2006-11-07 21:49:50 +0000577/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
578/// either use it into active basic block pass manager or create new basic
579/// block pass manager to handle pass P.
580bool
Devang Patel4e12f862006-11-08 10:44:40 +0000581FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000582
583 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
584 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
585
586 if (!activeBBPassManager
587 || !activeBBPassManager->addPass(BP)) {
588
589 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000590 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000591 if (!activeBBPassManager->addPass(BP))
592 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000593 }
594 return true;
595 }
596
597 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
598 if (!FP)
599 return false;
600
Devang Patel3c8eb622006-11-07 22:56:50 +0000601 // If this pass does not preserve anlysis that is used by other passes
602 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000603 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000604 return false;
605
Devang Patel8cad70d2006-11-11 01:51:02 +0000606 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000607 activeBBPassManager = NULL;
608 return true;
609}
610
611/// Execute all of the passes scheduled for execution by invoking
612/// runOnFunction method. Keep track of whether any of the passes modifies
613/// the function, and if so, return true.
Devang Patel9f3083e2006-11-15 19:39:54 +0000614bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000615
616 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000617 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000618
Devang Patel0c2012f2006-11-07 21:49:50 +0000619 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000620 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
621 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000622 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000623
Devang Patele9976aa2006-12-07 19:33:53 +0000624 recordAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000625 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
626 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000627 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000628 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000629 }
630 return Changed;
631}
632
Devang Patel9f3083e2006-11-15 19:39:54 +0000633/// Execute all of the passes scheduled for execution by invoking
634/// runOnFunction method. Keep track of whether any of the passes modifies
635/// the function, and if so, return true.
636bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
637
638 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000639 initializeAnalysisInfo();
Devang Patel9f3083e2006-11-15 19:39:54 +0000640
641 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
642 e = passVectorEnd(); itr != e; ++itr) {
643 Pass *P = *itr;
644
Devang Patele9976aa2006-12-07 19:33:53 +0000645 recordAvailableAnalysis(P);
Devang Patel9f3083e2006-11-15 19:39:54 +0000646 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
647 Changed |= FP->runOnFunction(F);
648 removeNotPreservedAnalysis(P);
649 removeDeadPasses(P);
650 }
651 return Changed;
652}
653
654
Devang Patelebba9702006-11-13 22:40:09 +0000655/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000656Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000657
Devang Patel3f0832a2006-11-14 02:54:23 +0000658 Pass *P = getAnalysisPass(AID);
659 if (P)
660 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000661
662 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000663 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000664 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000665
666 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000667 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000668}
Devang Patel0c2012f2006-11-07 21:49:50 +0000669
Devang Patelff631ae2006-11-15 01:27:05 +0000670inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
671 bool Changed = false;
672
673 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
674 e = passVectorEnd(); itr != e; ++itr) {
675 Pass *P = *itr;
676
677 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
678 Changed |= FP->doInitialization(M);
679 }
680
681 return Changed;
682}
683
684inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
685 bool Changed = false;
686
687 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
688 e = passVectorEnd(); itr != e; ++itr) {
689 Pass *P = *itr;
690
691 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
692 Changed |= FP->doFinalization(M);
693 }
694
695
696 return Changed;
697}
698
Devang Patela1514cb2006-12-07 19:39:39 +0000699//===----------------------------------------------------------------------===//
Devang Patel05e1a972006-11-07 22:03:15 +0000700// ModulePassManager implementation
701
702/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000703/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000704/// is not manageable by this manager.
705bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000706ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000707
708 // If P is FunctionPass then use function pass maanager.
709 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
710
711 activeFunctionPassManager = NULL;
712
713 if (!activeFunctionPassManager
714 || !activeFunctionPassManager->addPass(P)) {
715
Devang Patel4e12f862006-11-08 10:44:40 +0000716 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000717 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000718 if (!activeFunctionPassManager->addPass(FP))
719 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000720 }
721 return true;
722 }
723
724 ModulePass *MP = dynamic_cast<ModulePass *>(P);
725 if (!MP)
726 return false;
727
Devang Patel3c8eb622006-11-07 22:56:50 +0000728 // If this pass does not preserve anlysis that is used by other passes
729 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000730 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000731 return false;
732
Devang Patel8cad70d2006-11-11 01:51:02 +0000733 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000734 activeFunctionPassManager = NULL;
735 return true;
736}
737
738
739/// Execute all of the passes scheduled for execution by invoking
740/// runOnModule method. Keep track of whether any of the passes modifies
741/// the module, and if so, return true.
742bool
743ModulePassManager_New::runOnModule(Module &M) {
744 bool Changed = false;
Devang Patela6b6dcb2006-12-07 18:41:09 +0000745 initializeAnalysisInfo();
Devang Patel050ec722006-11-14 01:23:29 +0000746
Devang Patel8cad70d2006-11-11 01:51:02 +0000747 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
748 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000749 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000750
Devang Patele9976aa2006-12-07 19:33:53 +0000751 recordAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000752 ModulePass *MP = dynamic_cast<ModulePass*>(P);
753 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000754 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000755 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000756 }
757 return Changed;
758}
759
Devang Patelebba9702006-11-13 22:40:09 +0000760/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000761Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000762
Devang Patel3f0832a2006-11-14 02:54:23 +0000763
764 Pass *P = getAnalysisPass(AID);
765 if (P)
766 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000767
768 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000769 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000770 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000771
772 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000773 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000774}
775
Devang Patela1514cb2006-12-07 19:39:39 +0000776//===----------------------------------------------------------------------===//
777// PassManagerImpl implementation
778
Devang Patelebba9702006-11-13 22:40:09 +0000779/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000780Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000781
Devang Patel3f0832a2006-11-14 02:54:23 +0000782 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000783 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000784 e = PassManagers.end(); !P && itr != e; ++itr)
785 P = (*itr)->getAnalysisPassFromManager(AID);
786 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000787}
788
Devang Patel1a6eaa42006-11-11 02:22:31 +0000789/// Schedule pass P for execution. Make sure that passes required by
790/// P are run before P is run. Update analysis info maintained by
791/// the manager. Remove dead passes. This is a recursive function.
792void PassManagerImpl_New::schedulePass(Pass *P) {
793
794 AnalysisUsage AnUsage;
795 P->getAnalysisUsage(AnUsage);
796 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
797 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
798 E = RequiredSet.end(); I != E; ++I) {
799
Devang Patel3f0832a2006-11-14 02:54:23 +0000800 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
801 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000802 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000803 AnalysisPass = (*I)->createPass();
804 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000805 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000806 setLastUser (AnalysisPass, P);
Devang Patel4a3fa4f2006-11-15 01:48:14 +0000807
808 // Prolong live range of analyses that are needed after an analysis pass
809 // is destroyed, for querying by subsequent passes
810 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
811 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
812 E = IDs.end(); I != E; ++I) {
813 Pass *AP = getAnalysisPassFromManager(*I);
814 assert (AP && "Analysis pass is not available");
815 setLastUser(AP, P);
816 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000817 }
Devang Patel1a6eaa42006-11-11 02:22:31 +0000818 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000819}
820
Devang Patelc290c8a2006-11-07 22:23:34 +0000821/// Schedule all passes from the queue by adding them in their
822/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000823void PassManagerImpl_New::schedulePasses() {
824 for (std::vector<Pass *>::iterator I = passVectorBegin(),
825 E = passVectorEnd(); I != E; ++I)
826 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000827}
828
829/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000830void PassManagerImpl_New::add(Pass *P) {
831 // Do not process Analysis now. Analysis is process while scheduling
832 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000833 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000834}
835
836// PassManager_New implementation
837/// Add P into active pass manager or use new module pass manager to
838/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000839bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000840
Devang Patel6c9f5482006-11-11 00:42:16 +0000841 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000842 activeManager = new ModulePassManager_New();
843 PassManagers.push_back(activeManager);
844 }
845
846 return activeManager->addPass(P);
847}
848
849/// run - Execute all of the passes scheduled for execution. Keep track of
850/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000851bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000852
853 schedulePasses();
854 bool Changed = false;
855 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
856 e = PassManagers.end(); itr != e; ++itr) {
857 ModulePassManager_New *pm = *itr;
858 Changed |= pm->runOnModule(M);
859 }
860 return Changed;
861}
Devang Patel376fefa2006-11-08 10:29:57 +0000862
Devang Patela1514cb2006-12-07 19:39:39 +0000863//===----------------------------------------------------------------------===//
864// PassManager implementation
865
Devang Patel376fefa2006-11-08 10:29:57 +0000866/// Create new pass manager
867PassManager_New::PassManager_New() {
868 PM = new PassManagerImpl_New();
869}
870
871/// add - Add a pass to the queue of passes to run. This passes ownership of
872/// the Pass to the PassManager. When the PassManager is destroyed, the pass
873/// will be destroyed as well, so there is no need to delete the pass. This
874/// implies that all passes MUST be allocated with 'new'.
875void
876PassManager_New::add(Pass *P) {
877 PM->add(P);
878}
879
880/// run - Execute all of the passes scheduled for execution. Keep track of
881/// whether any of the passes modifies the module, and if so, return true.
882bool
883PassManager_New::run(Module &M) {
884 return PM->run(M);
885}
886