blob: 4a5c7f47a9c97a5dd74c266151ca4c80fefcd2c8 [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 Patela9844592006-11-11 01:31:05 +000017#include <vector>
18#include <set>
Devang Patel6e5a1132006-11-07 21:31:57 +000019
20using namespace llvm;
21
Devang Patelca58e352006-11-08 10:05:38 +000022namespace llvm {
23
Devang Patela9844592006-11-11 01:31:05 +000024/// CommonPassManagerImpl helps pass manager analysis required by
25/// the managed passes. It provides methods to add/remove analysis
26/// available and query if certain analysis is available or not.
27class CommonPassManagerImpl : public Pass {
28
29public:
30
31 /// Return true IFF pass P's required analysis set does not required new
32 /// manager.
33 bool manageablePass(Pass *P);
34
35 /// Return true IFF AnalysisID AID is currently available.
Devang Patelebba9702006-11-13 22:40:09 +000036 bool isAnalysisAvailable(AnalysisID AID) {
37 return (AvailableAnalysis.count(AID) != 0);
38 }
Devang Patela9844592006-11-11 01:31:05 +000039
40 /// Augment RequiredAnalysis by adding analysis required by pass P.
41 void noteDownRequiredAnalysis(Pass *P);
42
43 /// Augment AvailableAnalysis by adding analysis made available by pass P.
44 void noteDownAvailableAnalysis(Pass *P);
45
Devang Patela9844592006-11-11 01:31:05 +000046 /// Remove Analysis that is not preserved by the pass
47 void removeNotPreservedAnalysis(Pass *P);
48
49 /// Remove dead passes
50 void removeDeadPasses() { /* TODO : Implement */ }
51
Devang Patel8cad70d2006-11-11 01:51:02 +000052 /// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +000053 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
54 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +000055
56 inline std::vector<Pass *>::iterator passVectorBegin() {
57 return PassVector.begin();
58 }
59
60 inline std::vector<Pass *>::iterator passVectorEnd() {
61 return PassVector.end();
62 }
63
Devang Patela9844592006-11-11 01:31:05 +000064private:
65 // Analysis required by the passes managed by this manager
66 std::vector<AnalysisID> RequiredAnalysis;
67
68 // set of available Analysis
69 std::set<AnalysisID> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +000070
71 // Collection of pass that are managed by this manager
72 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +000073};
74
Devang Patelca58e352006-11-08 10:05:38 +000075/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
76/// pass together and sequence them to process one basic block before
77/// processing next basic block.
Devang Patel0ed47792006-11-10 21:33:13 +000078class BasicBlockPassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +000079
80public:
81 BasicBlockPassManager_New() { }
82
83 /// Add a pass into a passmanager queue.
84 bool addPass(Pass *p);
85
86 /// Execute all of the passes scheduled for execution. Keep track of
87 /// whether any of the passes modifies the function, and if so, return true.
88 bool runOnFunction(Function &F);
89
Devang Patelebba9702006-11-13 22:40:09 +000090 /// Return true IFF AnalysisID AID is currently available.
91 bool analysisCurrentlyAvailable(AnalysisID AID);
92
Devang Patelca58e352006-11-08 10:05:38 +000093private:
Devang Patelca58e352006-11-08 10:05:38 +000094};
95
Devang Patel4e12f862006-11-08 10:44:40 +000096/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +000097/// It batches all function passes and basic block pass managers together and
98/// sequence them to process one function at a time before processing next
99/// function.
Devang Patel0ed47792006-11-10 21:33:13 +0000100class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000101public:
Devang Patel4e12f862006-11-08 10:44:40 +0000102 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
103 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000104 activeBBPassManager = NULL;
105 }
Devang Patel4e12f862006-11-08 10:44:40 +0000106 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000107
108 /// add - Add a pass to the queue of passes to run. This passes
109 /// ownership of the Pass to the PassManager. When the
110 /// PassManager_X is destroyed, the pass will be destroyed as well, so
111 /// there is no need to delete the pass. (TODO delete passes.)
112 /// This implies that all passes MUST be allocated with 'new'.
113 void add(Pass *P) { /* TODO*/ }
114
115 /// Add pass into the pass manager queue.
116 bool addPass(Pass *P);
117
118 /// Execute all of the passes scheduled for execution. Keep
119 /// track of whether any of the passes modifies the function, and if
120 /// so, return true.
121 bool runOnModule(Module &M);
122
Devang Patelebba9702006-11-13 22:40:09 +0000123 /// Return true IFF AnalysisID AID is currently available.
124 bool analysisCurrentlyAvailable(AnalysisID AID);
125
Devang Patelca58e352006-11-08 10:05:38 +0000126private:
Devang Patelca58e352006-11-08 10:05:38 +0000127 // Active Pass Managers
128 BasicBlockPassManager_New *activeBBPassManager;
129};
130
131/// ModulePassManager_New manages ModulePasses and function pass managers.
132/// It batches all Module passes passes and function pass managers together and
133/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000134class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000135
136public:
137 ModulePassManager_New() { activeFunctionPassManager = NULL; }
138
139 /// Add a pass into a passmanager queue.
140 bool addPass(Pass *p);
141
142 /// run - Execute all of the passes scheduled for execution. Keep track of
143 /// whether any of the passes modifies the module, and if so, return true.
144 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000145
146 /// Return true IFF AnalysisID AID is currently available.
147 bool analysisCurrentlyAvailable(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000148
149private:
Devang Patelca58e352006-11-08 10:05:38 +0000150 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000151 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000152};
153
Devang Patel376fefa2006-11-08 10:29:57 +0000154/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000155class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000156
157public:
158
159 /// add - Add a pass to the queue of passes to run. This passes ownership of
160 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
161 /// will be destroyed as well, so there is no need to delete the pass. This
162 /// implies that all passes MUST be allocated with 'new'.
163 void add(Pass *P);
164
165 /// run - Execute all of the passes scheduled for execution. Keep track of
166 /// whether any of the passes modifies the module, and if so, return true.
167 bool run(Module &M);
168
Devang Patelebba9702006-11-13 22:40:09 +0000169 /// Return true IFF AnalysisID AID is currently available.
170 bool analysisCurrentlyAvailable(AnalysisID AID);
171
Devang Patel376fefa2006-11-08 10:29:57 +0000172private:
173
174 /// Add a pass into a passmanager queue. This is used by schedulePasses
175 bool addPass(Pass *p);
176
Devang Patel1a6eaa42006-11-11 02:22:31 +0000177 /// Schedule pass P for execution. Make sure that passes required by
178 /// P are run before P is run. Update analysis info maintained by
179 /// the manager. Remove dead passes. This is a recursive function.
180 void schedulePass(Pass *P);
181
Devang Patel376fefa2006-11-08 10:29:57 +0000182 /// Schedule all passes collected in pass queue using add(). Add all the
183 /// schedule passes into various manager's queue using addPass().
184 void schedulePasses();
185
186 // Collection of pass managers
187 std::vector<ModulePassManager_New *> PassManagers;
188
Devang Patel376fefa2006-11-08 10:29:57 +0000189 // Active Pass Manager
190 ModulePassManager_New *activeManager;
191};
192
Devang Patelca58e352006-11-08 10:05:38 +0000193} // End of llvm namespace
194
Devang Patel0ed47792006-11-10 21:33:13 +0000195// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000196
Devang Pateld65e9e92006-11-08 01:31:28 +0000197/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000198/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000199bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000200
201 AnalysisUsage AnUsage;
202 P->getAnalysisUsage(AnUsage);
203
Devang Patel6c9f5482006-11-11 00:42:16 +0000204 // If this pass is not preserving information that is required by the other
205 // passes managed by this manager then use new manager
206 if (!AnUsage.getPreservesAll()) {
207 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
208 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
209 E = RequiredAnalysis.end(); I != E; ++I) {
210 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
211 PreservedSet.end())
212 // This analysis is not preserved. Need new manager.
213 return false;
214 }
215 }
Devang Patelf68a3492006-11-07 22:35:17 +0000216 return true;
217}
218
Devang Patel643676c2006-11-11 01:10:19 +0000219/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000220void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000221 AnalysisUsage AnUsage;
222 P->getAnalysisUsage(AnUsage);
223 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000224
Devang Patel6c9f5482006-11-11 00:42:16 +0000225 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000226 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
227 RequiredSet.end());
Devang Patelf68a3492006-11-07 22:35:17 +0000228}
229
Devang Patel643676c2006-11-11 01:10:19 +0000230/// Augement AvailableAnalysis by adding analysis made available by pass P.
231void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
232
233 if (const PassInfo *PI = P->getPassInfo()) {
234 AvailableAnalysis.insert(PI);
235
236 //TODO This pass is the current implementation of all of the interfaces it
237 //TODO implements as well.
238 //TODO
239 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
240 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
241 //TODO CurrentAnalyses[II[i]] = P;
242 }
243}
244
Devang Patelf68a3492006-11-07 22:35:17 +0000245/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000246void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000247 AnalysisUsage AnUsage;
248 P->getAnalysisUsage(AnUsage);
249 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000250
Devang Patel349170f2006-11-11 01:24:55 +0000251 for (std::set<AnalysisID>::iterator I = AvailableAnalysis.begin(),
252 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patel349170f2006-11-11 01:24:55 +0000253 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
254 PreservedSet.end()) {
255 // Remove this analysis
256 std::set<AnalysisID>::iterator J = I++;
257 AvailableAnalysis.erase(J);
258 }
259 }
Devang Patelf68a3492006-11-07 22:35:17 +0000260}
261
Devang Patel8cad70d2006-11-11 01:51:02 +0000262/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000263/// AvailableAnalysis appropriately if ProcessAnalysis is true.
264void CommonPassManagerImpl::addPassToManager (Pass *P,
265 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000266
Devang Patel90b05e02006-11-11 02:04:19 +0000267 if (ProcessAnalysis) {
268 // Take a note of analysis required and made available by this pass
269 noteDownRequiredAnalysis(P);
270 noteDownAvailableAnalysis(P);
271
272 // Remove the analysis not preserved by this pass
273 removeNotPreservedAnalysis(P);
274 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000275
276 // Add pass
277 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000278}
279
Devang Patel6e5a1132006-11-07 21:31:57 +0000280/// BasicBlockPassManager implementation
281
Devang Pateld65e9e92006-11-08 01:31:28 +0000282/// Add pass P into PassVector and return true. If this pass is not
283/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000284bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000285BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000286
287 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
288 if (!BP)
289 return false;
290
Devang Patel3c8eb622006-11-07 22:56:50 +0000291 // If this pass does not preserve anlysis that is used by other passes
292 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000293 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000294 return false;
295
Devang Patel8cad70d2006-11-11 01:51:02 +0000296 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000297
Devang Patel6e5a1132006-11-07 21:31:57 +0000298 return true;
299}
300
301/// Execute all of the passes scheduled for execution by invoking
302/// runOnBasicBlock method. Keep track of whether any of the passes modifies
303/// the function, and if so, return true.
304bool
305BasicBlockPassManager_New::runOnFunction(Function &F) {
306
307 bool Changed = false;
308 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000309 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
310 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000311 Pass *P = *itr;
312 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
313 Changed |= BP->runOnBasicBlock(*I);
314 }
315 return Changed;
316}
317
Devang Patelebba9702006-11-13 22:40:09 +0000318/// Return true IFF AnalysisID AID is currently available.
319bool BasicBlockPassManager_New::analysisCurrentlyAvailable(AnalysisID AID) {
320 return isAnalysisAvailable(AID);
321}
322
Devang Patel0c2012f2006-11-07 21:49:50 +0000323// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000324/// Create new Function pass manager
325FunctionPassManager_New::FunctionPassManager_New() {
326 FPM = new FunctionPassManagerImpl_New();
327}
328
329/// add - Add a pass to the queue of passes to run. This passes
330/// ownership of the Pass to the PassManager. When the
331/// PassManager_X is destroyed, the pass will be destroyed as well, so
332/// there is no need to delete the pass. (TODO delete passes.)
333/// This implies that all passes MUST be allocated with 'new'.
334void
335FunctionPassManager_New::add(Pass *P) {
336 FPM->add(P);
337}
338
339/// Execute all of the passes scheduled for execution. Keep
340/// track of whether any of the passes modifies the function, and if
341/// so, return true.
342bool
343FunctionPassManager_New::runOnModule(Module &M) {
344 return FPM->runOnModule(M);
345}
346
347// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000348
Devang Patel0c2012f2006-11-07 21:49:50 +0000349// FunctionPassManager
350
351/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
352/// either use it into active basic block pass manager or create new basic
353/// block pass manager to handle pass P.
354bool
Devang Patel4e12f862006-11-08 10:44:40 +0000355FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000356
357 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
358 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
359
360 if (!activeBBPassManager
361 || !activeBBPassManager->addPass(BP)) {
362
363 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000364 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000365 if (!activeBBPassManager->addPass(BP))
366 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000367 }
368 return true;
369 }
370
371 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
372 if (!FP)
373 return false;
374
Devang Patel3c8eb622006-11-07 22:56:50 +0000375 // If this pass does not preserve anlysis that is used by other passes
376 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000377 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000378 return false;
379
Devang Patel8cad70d2006-11-11 01:51:02 +0000380 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000381 activeBBPassManager = NULL;
382 return true;
383}
384
385/// Execute all of the passes scheduled for execution by invoking
386/// runOnFunction method. Keep track of whether any of the passes modifies
387/// the function, and if so, return true.
388bool
Devang Patel4e12f862006-11-08 10:44:40 +0000389FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000390
391 bool Changed = false;
392 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000393 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
394 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000395 Pass *P = *itr;
396 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
397 Changed |= FP->runOnFunction(*I);
398 }
399 return Changed;
400}
401
Devang Patelebba9702006-11-13 22:40:09 +0000402/// Return true IFF AnalysisID AID is currently available.
403bool FunctionPassManagerImpl_New::analysisCurrentlyAvailable(AnalysisID AID) {
404
405 if (isAnalysisAvailable(AID))
406 return true;
407
408 if (activeBBPassManager &&
409 activeBBPassManager->isAnalysisAvailable(AID))
410 return true;
411
412 // TODO : Check inactive managers
413 return false;
414}
Devang Patel0c2012f2006-11-07 21:49:50 +0000415
Devang Patel05e1a972006-11-07 22:03:15 +0000416// ModulePassManager implementation
417
418/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000419/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000420/// is not manageable by this manager.
421bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000422ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000423
424 // If P is FunctionPass then use function pass maanager.
425 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
426
427 activeFunctionPassManager = NULL;
428
429 if (!activeFunctionPassManager
430 || !activeFunctionPassManager->addPass(P)) {
431
Devang Patel4e12f862006-11-08 10:44:40 +0000432 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000433 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000434 if (!activeFunctionPassManager->addPass(FP))
435 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000436 }
437 return true;
438 }
439
440 ModulePass *MP = dynamic_cast<ModulePass *>(P);
441 if (!MP)
442 return false;
443
Devang Patel3c8eb622006-11-07 22:56:50 +0000444 // If this pass does not preserve anlysis that is used by other passes
445 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000446 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000447 return false;
448
Devang Patel8cad70d2006-11-11 01:51:02 +0000449 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000450 activeFunctionPassManager = NULL;
451 return true;
452}
453
454
455/// Execute all of the passes scheduled for execution by invoking
456/// runOnModule method. Keep track of whether any of the passes modifies
457/// the module, and if so, return true.
458bool
459ModulePassManager_New::runOnModule(Module &M) {
460 bool Changed = false;
Devang Patel8cad70d2006-11-11 01:51:02 +0000461 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
462 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000463 Pass *P = *itr;
464 ModulePass *MP = dynamic_cast<ModulePass*>(P);
465 Changed |= MP->runOnModule(M);
466 }
467 return Changed;
468}
469
Devang Patelebba9702006-11-13 22:40:09 +0000470/// Return true IFF AnalysisID AID is currently available.
471bool ModulePassManager_New::analysisCurrentlyAvailable(AnalysisID AID) {
472
473 if (isAnalysisAvailable(AID))
474 return true;
475
476 if (activeFunctionPassManager &&
477 activeFunctionPassManager->isAnalysisAvailable(AID))
478 return true;
479
480 // TODO : Check inactive managers
481 return false;
482}
483
484/// Return true IFF AnalysisID AID is currently available.
485bool PassManagerImpl_New::analysisCurrentlyAvailable(AnalysisID AID) {
486
487 // TODO : Check inactive managers
488 return activeManager->analysisCurrentlyAvailable(AID);
489}
490
Devang Patel1a6eaa42006-11-11 02:22:31 +0000491/// Schedule pass P for execution. Make sure that passes required by
492/// P are run before P is run. Update analysis info maintained by
493/// the manager. Remove dead passes. This is a recursive function.
494void PassManagerImpl_New::schedulePass(Pass *P) {
495
496 AnalysisUsage AnUsage;
497 P->getAnalysisUsage(AnUsage);
498 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
499 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
500 E = RequiredSet.end(); I != E; ++I) {
501
Devang Patelebba9702006-11-13 22:40:09 +0000502 if (!analysisCurrentlyAvailable(*I)) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000503 // Schedule this analysis run first.
504 Pass *AP = (*I)->createPass();
505 schedulePass(AP);
506 }
507 }
508
509 addPass(P);
510
511 // TODO : Walk through all managers and remove not preserved analysis
512 // TODO : remove dead passes
513}
514
Devang Patelc290c8a2006-11-07 22:23:34 +0000515/// Schedule all passes from the queue by adding them in their
516/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000517void PassManagerImpl_New::schedulePasses() {
518 for (std::vector<Pass *>::iterator I = passVectorBegin(),
519 E = passVectorEnd(); I != E; ++I)
520 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000521}
522
523/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000524void PassManagerImpl_New::add(Pass *P) {
525 // Do not process Analysis now. Analysis is process while scheduling
526 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000527 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000528}
529
530// PassManager_New implementation
531/// Add P into active pass manager or use new module pass manager to
532/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000533bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000534
Devang Patel6c9f5482006-11-11 00:42:16 +0000535 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000536 activeManager = new ModulePassManager_New();
537 PassManagers.push_back(activeManager);
538 }
539
540 return activeManager->addPass(P);
541}
542
543/// run - Execute all of the passes scheduled for execution. Keep track of
544/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000545bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000546
547 schedulePasses();
548 bool Changed = false;
549 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
550 e = PassManagers.end(); itr != e; ++itr) {
551 ModulePassManager_New *pm = *itr;
552 Changed |= pm->runOnModule(M);
553 }
554 return Changed;
555}
Devang Patel376fefa2006-11-08 10:29:57 +0000556
557/// Create new pass manager
558PassManager_New::PassManager_New() {
559 PM = new PassManagerImpl_New();
560}
561
562/// add - Add a pass to the queue of passes to run. This passes ownership of
563/// the Pass to the PassManager. When the PassManager is destroyed, the pass
564/// will be destroyed as well, so there is no need to delete the pass. This
565/// implies that all passes MUST be allocated with 'new'.
566void
567PassManager_New::add(Pass *P) {
568 PM->add(P);
569}
570
571/// run - Execute all of the passes scheduled for execution. Keep track of
572/// whether any of the passes modifies the module, and if so, return true.
573bool
574PassManager_New::run(Module &M) {
575 return PM->run(M);
576}
577