blob: 25742f1be1adcf6a46cc4fca2a6852a874f8c8f0 [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
Devang Patel050ec722006-11-14 01:23:29 +000056 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
57 /// This is used before running passes managed by the manager.
58 void clearAnalysis() {
59 RequiredAnalysis.clear();
60 AvailableAnalysis.clear();
61 }
62
Devang Patel8cad70d2006-11-11 01:51:02 +000063 inline std::vector<Pass *>::iterator passVectorBegin() {
64 return PassVector.begin();
65 }
66
67 inline std::vector<Pass *>::iterator passVectorEnd() {
68 return PassVector.end();
69 }
70
Devang Patela9844592006-11-11 01:31:05 +000071private:
Devang Pateldafa4dd2006-11-14 00:03:04 +000072 // Analysis required by the passes managed by this manager. This information
73 // used while selecting pass manager during addPass. If a pass does not
74 // preserve any analysis required by other passes managed by current
75 // pass manager then new pass manager is used.
Devang Patela9844592006-11-11 01:31:05 +000076 std::vector<AnalysisID> RequiredAnalysis;
77
Devang Pateldafa4dd2006-11-14 00:03:04 +000078 // Set of available Analysis. This information is used while scheduling
79 // pass. If a pass requires an analysis which is not not available then
80 // equired analysis pass is scheduled to run before the pass itself is
81 // scheduled to run.
Devang Patela9844592006-11-11 01:31:05 +000082 std::set<AnalysisID> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +000083
84 // Collection of pass that are managed by this manager
85 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +000086};
87
Devang Patelca58e352006-11-08 10:05:38 +000088/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
89/// pass together and sequence them to process one basic block before
90/// processing next basic block.
Devang Patel0ed47792006-11-10 21:33:13 +000091class BasicBlockPassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +000092
93public:
94 BasicBlockPassManager_New() { }
95
96 /// Add a pass into a passmanager queue.
97 bool addPass(Pass *p);
98
99 /// Execute all of the passes scheduled for execution. Keep track of
100 /// whether any of the passes modifies the function, and if so, return true.
101 bool runOnFunction(Function &F);
102
Devang Patelebba9702006-11-13 22:40:09 +0000103 /// Return true IFF AnalysisID AID is currently available.
104 bool analysisCurrentlyAvailable(AnalysisID AID);
105
Devang Patelca58e352006-11-08 10:05:38 +0000106private:
Devang Patelca58e352006-11-08 10:05:38 +0000107};
108
Devang Patel4e12f862006-11-08 10:44:40 +0000109/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000110/// It batches all function passes and basic block pass managers together and
111/// sequence them to process one function at a time before processing next
112/// function.
Devang Patel0ed47792006-11-10 21:33:13 +0000113class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000114public:
Devang Patel4e12f862006-11-08 10:44:40 +0000115 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
116 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000117 activeBBPassManager = NULL;
118 }
Devang Patel4e12f862006-11-08 10:44:40 +0000119 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000120
121 /// add - Add a pass to the queue of passes to run. This passes
122 /// ownership of the Pass to the PassManager. When the
123 /// PassManager_X is destroyed, the pass will be destroyed as well, so
124 /// there is no need to delete the pass. (TODO delete passes.)
125 /// This implies that all passes MUST be allocated with 'new'.
126 void add(Pass *P) { /* TODO*/ }
127
128 /// Add pass into the pass manager queue.
129 bool addPass(Pass *P);
130
131 /// Execute all of the passes scheduled for execution. Keep
132 /// track of whether any of the passes modifies the function, and if
133 /// so, return true.
134 bool runOnModule(Module &M);
135
Devang Patelebba9702006-11-13 22:40:09 +0000136 /// Return true IFF AnalysisID AID is currently available.
137 bool analysisCurrentlyAvailable(AnalysisID AID);
138
Devang Patelca58e352006-11-08 10:05:38 +0000139private:
Devang Patelca58e352006-11-08 10:05:38 +0000140 // Active Pass Managers
141 BasicBlockPassManager_New *activeBBPassManager;
142};
143
144/// ModulePassManager_New manages ModulePasses and function pass managers.
145/// It batches all Module passes passes and function pass managers together and
146/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000147class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000148
149public:
150 ModulePassManager_New() { activeFunctionPassManager = NULL; }
151
152 /// Add a pass into a passmanager queue.
153 bool addPass(Pass *p);
154
155 /// run - Execute all of the passes scheduled for execution. Keep track of
156 /// whether any of the passes modifies the module, and if so, return true.
157 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000158
159 /// Return true IFF AnalysisID AID is currently available.
160 bool analysisCurrentlyAvailable(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000161
162private:
Devang Patelca58e352006-11-08 10:05:38 +0000163 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000164 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000165};
166
Devang Patel376fefa2006-11-08 10:29:57 +0000167/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000168class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000169
170public:
171
172 /// add - Add a pass to the queue of passes to run. This passes ownership of
173 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
174 /// will be destroyed as well, so there is no need to delete the pass. This
175 /// implies that all passes MUST be allocated with 'new'.
176 void add(Pass *P);
177
178 /// run - Execute all of the passes scheduled for execution. Keep track of
179 /// whether any of the passes modifies the module, and if so, return true.
180 bool run(Module &M);
181
Devang Patelebba9702006-11-13 22:40:09 +0000182 /// Return true IFF AnalysisID AID is currently available.
183 bool analysisCurrentlyAvailable(AnalysisID AID);
184
Devang Patel376fefa2006-11-08 10:29:57 +0000185private:
186
187 /// Add a pass into a passmanager queue. This is used by schedulePasses
188 bool addPass(Pass *p);
189
Devang Patel1a6eaa42006-11-11 02:22:31 +0000190 /// Schedule pass P for execution. Make sure that passes required by
191 /// P are run before P is run. Update analysis info maintained by
192 /// the manager. Remove dead passes. This is a recursive function.
193 void schedulePass(Pass *P);
194
Devang Patel376fefa2006-11-08 10:29:57 +0000195 /// Schedule all passes collected in pass queue using add(). Add all the
196 /// schedule passes into various manager's queue using addPass().
197 void schedulePasses();
198
199 // Collection of pass managers
200 std::vector<ModulePassManager_New *> PassManagers;
201
Devang Patel376fefa2006-11-08 10:29:57 +0000202 // Active Pass Manager
203 ModulePassManager_New *activeManager;
204};
205
Devang Patelca58e352006-11-08 10:05:38 +0000206} // End of llvm namespace
207
Devang Patel0ed47792006-11-10 21:33:13 +0000208// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000209
Devang Pateld65e9e92006-11-08 01:31:28 +0000210/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000211/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000212bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000213
214 AnalysisUsage AnUsage;
215 P->getAnalysisUsage(AnUsage);
216
Devang Patel6c9f5482006-11-11 00:42:16 +0000217 // If this pass is not preserving information that is required by the other
218 // passes managed by this manager then use new manager
219 if (!AnUsage.getPreservesAll()) {
220 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
221 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
222 E = RequiredAnalysis.end(); I != E; ++I) {
223 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
224 PreservedSet.end())
225 // This analysis is not preserved. Need new manager.
226 return false;
227 }
228 }
Devang Patelf68a3492006-11-07 22:35:17 +0000229 return true;
230}
231
Devang Patel643676c2006-11-11 01:10:19 +0000232/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000233void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000234 AnalysisUsage AnUsage;
235 P->getAnalysisUsage(AnUsage);
236 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000237
Devang Patel6c9f5482006-11-11 00:42:16 +0000238 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000239 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
240 RequiredSet.end());
Devang Patelf68a3492006-11-07 22:35:17 +0000241}
242
Devang Patel643676c2006-11-11 01:10:19 +0000243/// Augement AvailableAnalysis by adding analysis made available by pass P.
244void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
245
246 if (const PassInfo *PI = P->getPassInfo()) {
247 AvailableAnalysis.insert(PI);
248
249 //TODO This pass is the current implementation of all of the interfaces it
250 //TODO implements as well.
251 //TODO
252 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
253 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
254 //TODO CurrentAnalyses[II[i]] = P;
255 }
256}
257
Devang Patelf68a3492006-11-07 22:35:17 +0000258/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000259void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000260 AnalysisUsage AnUsage;
261 P->getAnalysisUsage(AnUsage);
262 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000263
Devang Patel349170f2006-11-11 01:24:55 +0000264 for (std::set<AnalysisID>::iterator I = AvailableAnalysis.begin(),
265 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patel349170f2006-11-11 01:24:55 +0000266 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
267 PreservedSet.end()) {
268 // Remove this analysis
269 std::set<AnalysisID>::iterator J = I++;
270 AvailableAnalysis.erase(J);
271 }
272 }
Devang Patelf68a3492006-11-07 22:35:17 +0000273}
274
Devang Patel8cad70d2006-11-11 01:51:02 +0000275/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000276/// AvailableAnalysis appropriately if ProcessAnalysis is true.
277void CommonPassManagerImpl::addPassToManager (Pass *P,
278 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000279
Devang Patel90b05e02006-11-11 02:04:19 +0000280 if (ProcessAnalysis) {
281 // Take a note of analysis required and made available by this pass
282 noteDownRequiredAnalysis(P);
283 noteDownAvailableAnalysis(P);
284
285 // Remove the analysis not preserved by this pass
286 removeNotPreservedAnalysis(P);
287 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000288
289 // Add pass
290 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000291}
292
Devang Patel6e5a1132006-11-07 21:31:57 +0000293/// BasicBlockPassManager implementation
294
Devang Pateld65e9e92006-11-08 01:31:28 +0000295/// Add pass P into PassVector and return true. If this pass is not
296/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000297bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000298BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000299
300 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
301 if (!BP)
302 return false;
303
Devang Patel3c8eb622006-11-07 22:56:50 +0000304 // If this pass does not preserve anlysis that is used by other passes
305 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000306 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000307 return false;
308
Devang Patel8cad70d2006-11-11 01:51:02 +0000309 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000310
Devang Patel6e5a1132006-11-07 21:31:57 +0000311 return true;
312}
313
314/// Execute all of the passes scheduled for execution by invoking
315/// runOnBasicBlock method. Keep track of whether any of the passes modifies
316/// the function, and if so, return true.
317bool
318BasicBlockPassManager_New::runOnFunction(Function &F) {
319
320 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000321 clearAnalysis();
322
Devang Patel6e5a1132006-11-07 21:31:57 +0000323 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000324 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
325 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000326 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000327
328 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000329 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
330 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000331 removeNotPreservedAnalysis(P);
332 removeDeadPasses();
Devang Patel6e5a1132006-11-07 21:31:57 +0000333 }
334 return Changed;
335}
336
Devang Patelebba9702006-11-13 22:40:09 +0000337/// Return true IFF AnalysisID AID is currently available.
338bool BasicBlockPassManager_New::analysisCurrentlyAvailable(AnalysisID AID) {
339 return isAnalysisAvailable(AID);
340}
341
Devang Patel0c2012f2006-11-07 21:49:50 +0000342// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000343/// Create new Function pass manager
344FunctionPassManager_New::FunctionPassManager_New() {
345 FPM = new FunctionPassManagerImpl_New();
346}
347
348/// add - Add a pass to the queue of passes to run. This passes
349/// ownership of the Pass to the PassManager. When the
350/// PassManager_X is destroyed, the pass will be destroyed as well, so
351/// there is no need to delete the pass. (TODO delete passes.)
352/// This implies that all passes MUST be allocated with 'new'.
353void
354FunctionPassManager_New::add(Pass *P) {
355 FPM->add(P);
356}
357
358/// Execute all of the passes scheduled for execution. Keep
359/// track of whether any of the passes modifies the function, and if
360/// so, return true.
361bool
362FunctionPassManager_New::runOnModule(Module &M) {
363 return FPM->runOnModule(M);
364}
365
366// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000367
Devang Patel0c2012f2006-11-07 21:49:50 +0000368// FunctionPassManager
369
370/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
371/// either use it into active basic block pass manager or create new basic
372/// block pass manager to handle pass P.
373bool
Devang Patel4e12f862006-11-08 10:44:40 +0000374FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000375
376 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
377 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
378
379 if (!activeBBPassManager
380 || !activeBBPassManager->addPass(BP)) {
381
382 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000383 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000384 if (!activeBBPassManager->addPass(BP))
385 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000386 }
387 return true;
388 }
389
390 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
391 if (!FP)
392 return false;
393
Devang Patel3c8eb622006-11-07 22:56:50 +0000394 // If this pass does not preserve anlysis that is used by other passes
395 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000396 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000397 return false;
398
Devang Patel8cad70d2006-11-11 01:51:02 +0000399 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000400 activeBBPassManager = NULL;
401 return true;
402}
403
404/// Execute all of the passes scheduled for execution by invoking
405/// runOnFunction method. Keep track of whether any of the passes modifies
406/// the function, and if so, return true.
407bool
Devang Patel4e12f862006-11-08 10:44:40 +0000408FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000409
410 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000411 clearAnalysis();
412
Devang Patel0c2012f2006-11-07 21:49:50 +0000413 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000414 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
415 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000416 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000417
418 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000419 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
420 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000421 removeNotPreservedAnalysis(P);
422 removeDeadPasses();
Devang Patel0c2012f2006-11-07 21:49:50 +0000423 }
424 return Changed;
425}
426
Devang Patelebba9702006-11-13 22:40:09 +0000427/// Return true IFF AnalysisID AID is currently available.
428bool FunctionPassManagerImpl_New::analysisCurrentlyAvailable(AnalysisID AID) {
429
430 if (isAnalysisAvailable(AID))
431 return true;
432
433 if (activeBBPassManager &&
434 activeBBPassManager->isAnalysisAvailable(AID))
435 return true;
436
437 // TODO : Check inactive managers
438 return false;
439}
Devang Patel0c2012f2006-11-07 21:49:50 +0000440
Devang Patel05e1a972006-11-07 22:03:15 +0000441// ModulePassManager implementation
442
443/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000444/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000445/// is not manageable by this manager.
446bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000447ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000448
449 // If P is FunctionPass then use function pass maanager.
450 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
451
452 activeFunctionPassManager = NULL;
453
454 if (!activeFunctionPassManager
455 || !activeFunctionPassManager->addPass(P)) {
456
Devang Patel4e12f862006-11-08 10:44:40 +0000457 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000458 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000459 if (!activeFunctionPassManager->addPass(FP))
460 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000461 }
462 return true;
463 }
464
465 ModulePass *MP = dynamic_cast<ModulePass *>(P);
466 if (!MP)
467 return false;
468
Devang Patel3c8eb622006-11-07 22:56:50 +0000469 // If this pass does not preserve anlysis that is used by other passes
470 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000471 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000472 return false;
473
Devang Patel8cad70d2006-11-11 01:51:02 +0000474 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000475 activeFunctionPassManager = NULL;
476 return true;
477}
478
479
480/// Execute all of the passes scheduled for execution by invoking
481/// runOnModule method. Keep track of whether any of the passes modifies
482/// the module, and if so, return true.
483bool
484ModulePassManager_New::runOnModule(Module &M) {
485 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000486 clearAnalysis();
487
Devang Patel8cad70d2006-11-11 01:51:02 +0000488 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
489 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000490 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000491
492 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000493 ModulePass *MP = dynamic_cast<ModulePass*>(P);
494 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000495 removeNotPreservedAnalysis(P);
496 removeDeadPasses();
Devang Patel05e1a972006-11-07 22:03:15 +0000497 }
498 return Changed;
499}
500
Devang Patelebba9702006-11-13 22:40:09 +0000501/// Return true IFF AnalysisID AID is currently available.
502bool ModulePassManager_New::analysisCurrentlyAvailable(AnalysisID AID) {
503
504 if (isAnalysisAvailable(AID))
505 return true;
506
507 if (activeFunctionPassManager &&
508 activeFunctionPassManager->isAnalysisAvailable(AID))
509 return true;
510
511 // TODO : Check inactive managers
512 return false;
513}
514
515/// Return true IFF AnalysisID AID is currently available.
516bool PassManagerImpl_New::analysisCurrentlyAvailable(AnalysisID AID) {
517
Devang Patel70868442006-11-13 22:53:19 +0000518 bool available = false;
519 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
520 e = PassManagers.end(); !available && itr != e; ++itr)
521 available = (*itr)->analysisCurrentlyAvailable(AID);
522 return available;
Devang Patelebba9702006-11-13 22:40:09 +0000523}
524
Devang Patel1a6eaa42006-11-11 02:22:31 +0000525/// Schedule pass P for execution. Make sure that passes required by
526/// P are run before P is run. Update analysis info maintained by
527/// the manager. Remove dead passes. This is a recursive function.
528void PassManagerImpl_New::schedulePass(Pass *P) {
529
530 AnalysisUsage AnUsage;
531 P->getAnalysisUsage(AnUsage);
532 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
533 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
534 E = RequiredSet.end(); I != E; ++I) {
535
Devang Patelebba9702006-11-13 22:40:09 +0000536 if (!analysisCurrentlyAvailable(*I)) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000537 // Schedule this analysis run first.
538 Pass *AP = (*I)->createPass();
539 schedulePass(AP);
540 }
541 }
542
543 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000544}
545
Devang Patelc290c8a2006-11-07 22:23:34 +0000546/// Schedule all passes from the queue by adding them in their
547/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000548void PassManagerImpl_New::schedulePasses() {
549 for (std::vector<Pass *>::iterator I = passVectorBegin(),
550 E = passVectorEnd(); I != E; ++I)
551 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000552}
553
554/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000555void PassManagerImpl_New::add(Pass *P) {
556 // Do not process Analysis now. Analysis is process while scheduling
557 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000558 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000559}
560
561// PassManager_New implementation
562/// Add P into active pass manager or use new module pass manager to
563/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000564bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000565
Devang Patel6c9f5482006-11-11 00:42:16 +0000566 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000567 activeManager = new ModulePassManager_New();
568 PassManagers.push_back(activeManager);
569 }
570
571 return activeManager->addPass(P);
572}
573
574/// run - Execute all of the passes scheduled for execution. Keep track of
575/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000576bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000577
578 schedulePasses();
579 bool Changed = false;
580 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
581 e = PassManagers.end(); itr != e; ++itr) {
582 ModulePassManager_New *pm = *itr;
583 Changed |= pm->runOnModule(M);
584 }
585 return Changed;
586}
Devang Patel376fefa2006-11-08 10:29:57 +0000587
588/// Create new pass manager
589PassManager_New::PassManager_New() {
590 PM = new PassManagerImpl_New();
591}
592
593/// add - Add a pass to the queue of passes to run. This passes ownership of
594/// the Pass to the PassManager. When the PassManager is destroyed, the pass
595/// will be destroyed as well, so there is no need to delete the pass. This
596/// implies that all passes MUST be allocated with 'new'.
597void
598PassManager_New::add(Pass *P) {
599 PM->add(P);
600}
601
602/// run - Execute all of the passes scheduled for execution. Keep track of
603/// whether any of the passes modifies the module, and if so, return true.
604bool
605PassManager_New::run(Module &M) {
606 return PM->run(M);
607}
608