blob: 90f3e948a82b69de13c266bb6f864f08feecefa0 [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>
Devang Patelf60b5d92006-11-14 01:59:59 +000018#include <map>
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
Devang Patelf60b5d92006-11-14 01:59:59 +000035 Pass *getAnalysisPass(AnalysisID AID) const {
36
37 std::map<AnalysisID, Pass*>::const_iterator I =
38 AvailableAnalysis.find(AID);
39
40 if (I != AvailableAnalysis.end())
41 return NULL;
42 else
43 return I->second;
Devang Patelebba9702006-11-13 22:40:09 +000044 }
Devang Patela9844592006-11-11 01:31:05 +000045
46 /// Augment RequiredAnalysis by adding analysis required by pass P.
47 void noteDownRequiredAnalysis(Pass *P);
48
49 /// Augment AvailableAnalysis by adding analysis made available by pass P.
50 void noteDownAvailableAnalysis(Pass *P);
51
Devang Patela9844592006-11-11 01:31:05 +000052 /// Remove Analysis that is not preserved by the pass
53 void removeNotPreservedAnalysis(Pass *P);
54
55 /// Remove dead passes
56 void removeDeadPasses() { /* TODO : Implement */ }
57
Devang Patel8cad70d2006-11-11 01:51:02 +000058 /// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +000059 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
60 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
Devang Patel8cad70d2006-11-11 01:51:02 +000061
Devang Patel050ec722006-11-14 01:23:29 +000062 /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis.
63 /// This is used before running passes managed by the manager.
64 void clearAnalysis() {
65 RequiredAnalysis.clear();
66 AvailableAnalysis.clear();
67 }
68
Devang Patelf60b5d92006-11-14 01:59:59 +000069 // All Required analyses should be available to the pass as it runs! Here
70 // we fill in the AnalysisImpls member of the pass so that it can
71 // successfully use the getAnalysis() method to retrieve the
72 // implementations it needs.
73 //
74 inline void initializeAnalysisImpl(Pass *P) { /* TODO : Implement */ }
75
Devang Patel8cad70d2006-11-11 01:51:02 +000076 inline std::vector<Pass *>::iterator passVectorBegin() {
77 return PassVector.begin();
78 }
79
80 inline std::vector<Pass *>::iterator passVectorEnd() {
81 return PassVector.end();
82 }
83
Devang Patela9844592006-11-11 01:31:05 +000084private:
Devang Pateldafa4dd2006-11-14 00:03:04 +000085 // Analysis required by the passes managed by this manager. This information
86 // used while selecting pass manager during addPass. If a pass does not
87 // preserve any analysis required by other passes managed by current
88 // pass manager then new pass manager is used.
Devang Patela9844592006-11-11 01:31:05 +000089 std::vector<AnalysisID> RequiredAnalysis;
90
Devang Pateldafa4dd2006-11-14 00:03:04 +000091 // Set of available Analysis. This information is used while scheduling
92 // pass. If a pass requires an analysis which is not not available then
93 // equired analysis pass is scheduled to run before the pass itself is
94 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +000095 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +000096
97 // Collection of pass that are managed by this manager
98 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +000099};
100
Devang Patelca58e352006-11-08 10:05:38 +0000101/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
102/// pass together and sequence them to process one basic block before
103/// processing next basic block.
Devang Patel0ed47792006-11-10 21:33:13 +0000104class BasicBlockPassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000105
106public:
107 BasicBlockPassManager_New() { }
108
109 /// Add a pass into a passmanager queue.
110 bool addPass(Pass *p);
111
112 /// Execute all of the passes scheduled for execution. Keep track of
113 /// whether any of the passes modifies the function, and if so, return true.
114 bool runOnFunction(Function &F);
115
Devang Patelebba9702006-11-13 22:40:09 +0000116 /// Return true IFF AnalysisID AID is currently available.
117 bool analysisCurrentlyAvailable(AnalysisID AID);
118
Devang Patelca58e352006-11-08 10:05:38 +0000119private:
Devang Patelca58e352006-11-08 10:05:38 +0000120};
121
Devang Patel4e12f862006-11-08 10:44:40 +0000122/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000123/// It batches all function passes and basic block pass managers together and
124/// sequence them to process one function at a time before processing next
125/// function.
Devang Patel0ed47792006-11-10 21:33:13 +0000126class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000127public:
Devang Patel4e12f862006-11-08 10:44:40 +0000128 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
129 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000130 activeBBPassManager = NULL;
131 }
Devang Patel4e12f862006-11-08 10:44:40 +0000132 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000133
134 /// add - Add a pass to the queue of passes to run. This passes
135 /// ownership of the Pass to the PassManager. When the
136 /// PassManager_X is destroyed, the pass will be destroyed as well, so
137 /// there is no need to delete the pass. (TODO delete passes.)
138 /// This implies that all passes MUST be allocated with 'new'.
139 void add(Pass *P) { /* TODO*/ }
140
141 /// Add pass into the pass manager queue.
142 bool addPass(Pass *P);
143
144 /// Execute all of the passes scheduled for execution. Keep
145 /// track of whether any of the passes modifies the function, and if
146 /// so, return true.
147 bool runOnModule(Module &M);
148
Devang Patelebba9702006-11-13 22:40:09 +0000149 /// Return true IFF AnalysisID AID is currently available.
150 bool analysisCurrentlyAvailable(AnalysisID AID);
151
Devang Patelca58e352006-11-08 10:05:38 +0000152private:
Devang Patelca58e352006-11-08 10:05:38 +0000153 // Active Pass Managers
154 BasicBlockPassManager_New *activeBBPassManager;
155};
156
157/// ModulePassManager_New manages ModulePasses and function pass managers.
158/// It batches all Module passes passes and function pass managers together and
159/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000160class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000161
162public:
163 ModulePassManager_New() { activeFunctionPassManager = NULL; }
164
165 /// Add a pass into a passmanager queue.
166 bool addPass(Pass *p);
167
168 /// run - Execute all of the passes scheduled for execution. Keep track of
169 /// whether any of the passes modifies the module, and if so, return true.
170 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000171
172 /// Return true IFF AnalysisID AID is currently available.
173 bool analysisCurrentlyAvailable(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000174
175private:
Devang Patelca58e352006-11-08 10:05:38 +0000176 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000177 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000178};
179
Devang Patel376fefa2006-11-08 10:29:57 +0000180/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000181class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000182
183public:
184
185 /// add - Add a pass to the queue of passes to run. This passes ownership of
186 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
187 /// will be destroyed as well, so there is no need to delete the pass. This
188 /// implies that all passes MUST be allocated with 'new'.
189 void add(Pass *P);
190
191 /// run - Execute all of the passes scheduled for execution. Keep track of
192 /// whether any of the passes modifies the module, and if so, return true.
193 bool run(Module &M);
194
Devang Patelebba9702006-11-13 22:40:09 +0000195 /// Return true IFF AnalysisID AID is currently available.
196 bool analysisCurrentlyAvailable(AnalysisID AID);
197
Devang Patel376fefa2006-11-08 10:29:57 +0000198private:
199
200 /// Add a pass into a passmanager queue. This is used by schedulePasses
201 bool addPass(Pass *p);
202
Devang Patel1a6eaa42006-11-11 02:22:31 +0000203 /// Schedule pass P for execution. Make sure that passes required by
204 /// P are run before P is run. Update analysis info maintained by
205 /// the manager. Remove dead passes. This is a recursive function.
206 void schedulePass(Pass *P);
207
Devang Patel376fefa2006-11-08 10:29:57 +0000208 /// Schedule all passes collected in pass queue using add(). Add all the
209 /// schedule passes into various manager's queue using addPass().
210 void schedulePasses();
211
212 // Collection of pass managers
213 std::vector<ModulePassManager_New *> PassManagers;
214
Devang Patel376fefa2006-11-08 10:29:57 +0000215 // Active Pass Manager
216 ModulePassManager_New *activeManager;
217};
218
Devang Patelca58e352006-11-08 10:05:38 +0000219} // End of llvm namespace
220
Devang Patel0ed47792006-11-10 21:33:13 +0000221// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000222
Devang Pateld65e9e92006-11-08 01:31:28 +0000223/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000224/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000225bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000226
227 AnalysisUsage AnUsage;
228 P->getAnalysisUsage(AnUsage);
229
Devang Patel6c9f5482006-11-11 00:42:16 +0000230 // If this pass is not preserving information that is required by the other
231 // passes managed by this manager then use new manager
232 if (!AnUsage.getPreservesAll()) {
233 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
234 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
235 E = RequiredAnalysis.end(); I != E; ++I) {
236 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
237 PreservedSet.end())
238 // This analysis is not preserved. Need new manager.
239 return false;
240 }
241 }
Devang Patelf68a3492006-11-07 22:35:17 +0000242 return true;
243}
244
Devang Patel643676c2006-11-11 01:10:19 +0000245/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000246void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000247 AnalysisUsage AnUsage;
248 P->getAnalysisUsage(AnUsage);
249 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000250
Devang Patel6c9f5482006-11-11 00:42:16 +0000251 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000252 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
253 RequiredSet.end());
Devang Patelf68a3492006-11-07 22:35:17 +0000254}
255
Devang Patel643676c2006-11-11 01:10:19 +0000256/// Augement AvailableAnalysis by adding analysis made available by pass P.
257void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000258
Devang Patel643676c2006-11-11 01:10:19 +0000259 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000260 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000261
262 //TODO This pass is the current implementation of all of the interfaces it
263 //TODO implements as well.
264 //TODO
265 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
266 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
267 //TODO CurrentAnalyses[II[i]] = P;
268 }
269}
270
Devang Patelf68a3492006-11-07 22:35:17 +0000271/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000272void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000273 AnalysisUsage AnUsage;
274 P->getAnalysisUsage(AnUsage);
275 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000276
Devang Patelf60b5d92006-11-14 01:59:59 +0000277 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000278 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000279 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000280 PreservedSet.end()) {
281 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000282 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000283 AvailableAnalysis.erase(J);
284 }
285 }
Devang Patelf68a3492006-11-07 22:35:17 +0000286}
287
Devang Patel8cad70d2006-11-11 01:51:02 +0000288/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000289/// AvailableAnalysis appropriately if ProcessAnalysis is true.
290void CommonPassManagerImpl::addPassToManager (Pass *P,
291 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000292
Devang Patel90b05e02006-11-11 02:04:19 +0000293 if (ProcessAnalysis) {
294 // Take a note of analysis required and made available by this pass
295 noteDownRequiredAnalysis(P);
296 noteDownAvailableAnalysis(P);
297
298 // Remove the analysis not preserved by this pass
299 removeNotPreservedAnalysis(P);
300 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000301
302 // Add pass
303 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000304}
305
Devang Patel6e5a1132006-11-07 21:31:57 +0000306/// BasicBlockPassManager implementation
307
Devang Pateld65e9e92006-11-08 01:31:28 +0000308/// Add pass P into PassVector and return true. If this pass is not
309/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000310bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000311BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000312
313 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
314 if (!BP)
315 return false;
316
Devang Patel3c8eb622006-11-07 22:56:50 +0000317 // If this pass does not preserve anlysis that is used by other passes
318 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000319 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000320 return false;
321
Devang Patel8cad70d2006-11-11 01:51:02 +0000322 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000323
Devang Patel6e5a1132006-11-07 21:31:57 +0000324 return true;
325}
326
327/// Execute all of the passes scheduled for execution by invoking
328/// runOnBasicBlock method. Keep track of whether any of the passes modifies
329/// the function, and if so, return true.
330bool
331BasicBlockPassManager_New::runOnFunction(Function &F) {
332
333 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000334 clearAnalysis();
335
Devang Patel6e5a1132006-11-07 21:31:57 +0000336 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000337 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
338 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000339 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000340
341 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000342 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
343 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000344 removeNotPreservedAnalysis(P);
345 removeDeadPasses();
Devang Patel6e5a1132006-11-07 21:31:57 +0000346 }
347 return Changed;
348}
349
Devang Patelebba9702006-11-13 22:40:09 +0000350/// Return true IFF AnalysisID AID is currently available.
351bool BasicBlockPassManager_New::analysisCurrentlyAvailable(AnalysisID AID) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000352 return (getAnalysisPass(AID) != 0);
Devang Patelebba9702006-11-13 22:40:09 +0000353}
354
Devang Patel0c2012f2006-11-07 21:49:50 +0000355// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000356/// Create new Function pass manager
357FunctionPassManager_New::FunctionPassManager_New() {
358 FPM = new FunctionPassManagerImpl_New();
359}
360
361/// add - Add a pass to the queue of passes to run. This passes
362/// ownership of the Pass to the PassManager. When the
363/// PassManager_X is destroyed, the pass will be destroyed as well, so
364/// there is no need to delete the pass. (TODO delete passes.)
365/// This implies that all passes MUST be allocated with 'new'.
366void
367FunctionPassManager_New::add(Pass *P) {
368 FPM->add(P);
369}
370
371/// Execute all of the passes scheduled for execution. Keep
372/// track of whether any of the passes modifies the function, and if
373/// so, return true.
374bool
375FunctionPassManager_New::runOnModule(Module &M) {
376 return FPM->runOnModule(M);
377}
378
379// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000380
Devang Patel0c2012f2006-11-07 21:49:50 +0000381// FunctionPassManager
382
383/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
384/// either use it into active basic block pass manager or create new basic
385/// block pass manager to handle pass P.
386bool
Devang Patel4e12f862006-11-08 10:44:40 +0000387FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000388
389 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
390 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
391
392 if (!activeBBPassManager
393 || !activeBBPassManager->addPass(BP)) {
394
395 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000396 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000397 if (!activeBBPassManager->addPass(BP))
398 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000399 }
400 return true;
401 }
402
403 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
404 if (!FP)
405 return false;
406
Devang Patel3c8eb622006-11-07 22:56:50 +0000407 // If this pass does not preserve anlysis that is used by other passes
408 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000409 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000410 return false;
411
Devang Patel8cad70d2006-11-11 01:51:02 +0000412 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000413 activeBBPassManager = NULL;
414 return true;
415}
416
417/// Execute all of the passes scheduled for execution by invoking
418/// runOnFunction method. Keep track of whether any of the passes modifies
419/// the function, and if so, return true.
420bool
Devang Patel4e12f862006-11-08 10:44:40 +0000421FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000422
423 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000424 clearAnalysis();
425
Devang Patel0c2012f2006-11-07 21:49:50 +0000426 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000427 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
428 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000429 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000430
431 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000432 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
433 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000434 removeNotPreservedAnalysis(P);
435 removeDeadPasses();
Devang Patel0c2012f2006-11-07 21:49:50 +0000436 }
437 return Changed;
438}
439
Devang Patelebba9702006-11-13 22:40:09 +0000440/// Return true IFF AnalysisID AID is currently available.
441bool FunctionPassManagerImpl_New::analysisCurrentlyAvailable(AnalysisID AID) {
442
Devang Patelf60b5d92006-11-14 01:59:59 +0000443 if (getAnalysisPass(AID) != 0)
Devang Patelebba9702006-11-13 22:40:09 +0000444 return true;
445
446 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000447 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patelebba9702006-11-13 22:40:09 +0000448 return true;
449
450 // TODO : Check inactive managers
451 return false;
452}
Devang Patel0c2012f2006-11-07 21:49:50 +0000453
Devang Patel05e1a972006-11-07 22:03:15 +0000454// ModulePassManager implementation
455
456/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000457/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000458/// is not manageable by this manager.
459bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000460ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000461
462 // If P is FunctionPass then use function pass maanager.
463 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
464
465 activeFunctionPassManager = NULL;
466
467 if (!activeFunctionPassManager
468 || !activeFunctionPassManager->addPass(P)) {
469
Devang Patel4e12f862006-11-08 10:44:40 +0000470 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000471 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000472 if (!activeFunctionPassManager->addPass(FP))
473 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000474 }
475 return true;
476 }
477
478 ModulePass *MP = dynamic_cast<ModulePass *>(P);
479 if (!MP)
480 return false;
481
Devang Patel3c8eb622006-11-07 22:56:50 +0000482 // If this pass does not preserve anlysis that is used by other passes
483 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000484 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000485 return false;
486
Devang Patel8cad70d2006-11-11 01:51:02 +0000487 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000488 activeFunctionPassManager = NULL;
489 return true;
490}
491
492
493/// Execute all of the passes scheduled for execution by invoking
494/// runOnModule method. Keep track of whether any of the passes modifies
495/// the module, and if so, return true.
496bool
497ModulePassManager_New::runOnModule(Module &M) {
498 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000499 clearAnalysis();
500
Devang Patel8cad70d2006-11-11 01:51:02 +0000501 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
502 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000503 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000504
505 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000506 ModulePass *MP = dynamic_cast<ModulePass*>(P);
507 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000508 removeNotPreservedAnalysis(P);
509 removeDeadPasses();
Devang Patel05e1a972006-11-07 22:03:15 +0000510 }
511 return Changed;
512}
513
Devang Patelebba9702006-11-13 22:40:09 +0000514/// Return true IFF AnalysisID AID is currently available.
515bool ModulePassManager_New::analysisCurrentlyAvailable(AnalysisID AID) {
516
Devang Patelf60b5d92006-11-14 01:59:59 +0000517 if (getAnalysisPass(AID) != 0)
Devang Patelebba9702006-11-13 22:40:09 +0000518 return true;
519
520 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000521 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patelebba9702006-11-13 22:40:09 +0000522 return true;
523
524 // TODO : Check inactive managers
525 return false;
526}
527
528/// Return true IFF AnalysisID AID is currently available.
529bool PassManagerImpl_New::analysisCurrentlyAvailable(AnalysisID AID) {
530
Devang Patel70868442006-11-13 22:53:19 +0000531 bool available = false;
532 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
533 e = PassManagers.end(); !available && itr != e; ++itr)
534 available = (*itr)->analysisCurrentlyAvailable(AID);
535 return available;
Devang Patelebba9702006-11-13 22:40:09 +0000536}
537
Devang Patel1a6eaa42006-11-11 02:22:31 +0000538/// Schedule pass P for execution. Make sure that passes required by
539/// P are run before P is run. Update analysis info maintained by
540/// the manager. Remove dead passes. This is a recursive function.
541void PassManagerImpl_New::schedulePass(Pass *P) {
542
543 AnalysisUsage AnUsage;
544 P->getAnalysisUsage(AnUsage);
545 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
546 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
547 E = RequiredSet.end(); I != E; ++I) {
548
Devang Patelebba9702006-11-13 22:40:09 +0000549 if (!analysisCurrentlyAvailable(*I)) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000550 // Schedule this analysis run first.
551 Pass *AP = (*I)->createPass();
552 schedulePass(AP);
553 }
554 }
555
556 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000557}
558
Devang Patelc290c8a2006-11-07 22:23:34 +0000559/// Schedule all passes from the queue by adding them in their
560/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000561void PassManagerImpl_New::schedulePasses() {
562 for (std::vector<Pass *>::iterator I = passVectorBegin(),
563 E = passVectorEnd(); I != E; ++I)
564 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000565}
566
567/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000568void PassManagerImpl_New::add(Pass *P) {
569 // Do not process Analysis now. Analysis is process while scheduling
570 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000571 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000572}
573
574// PassManager_New implementation
575/// Add P into active pass manager or use new module pass manager to
576/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000577bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000578
Devang Patel6c9f5482006-11-11 00:42:16 +0000579 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000580 activeManager = new ModulePassManager_New();
581 PassManagers.push_back(activeManager);
582 }
583
584 return activeManager->addPass(P);
585}
586
587/// run - Execute all of the passes scheduled for execution. Keep track of
588/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000589bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000590
591 schedulePasses();
592 bool Changed = false;
593 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
594 e = PassManagers.end(); itr != e; ++itr) {
595 ModulePassManager_New *pm = *itr;
596 Changed |= pm->runOnModule(M);
597 }
598 return Changed;
599}
Devang Patel376fefa2006-11-08 10:29:57 +0000600
601/// Create new pass manager
602PassManager_New::PassManager_New() {
603 PM = new PassManagerImpl_New();
604}
605
606/// add - Add a pass to the queue of passes to run. This passes ownership of
607/// the Pass to the PassManager. When the PassManager is destroyed, the pass
608/// will be destroyed as well, so there is no need to delete the pass. This
609/// implies that all passes MUST be allocated with 'new'.
610void
611PassManager_New::add(Pass *P) {
612 PM->add(P);
613}
614
615/// run - Execute all of the passes scheduled for execution. Keep track of
616/// whether any of the passes modifies the module, and if so, return true.
617bool
618PassManager_New::run(Module &M) {
619 return PM->run(M);
620}
621