blob: 1e7089745ee073c66291779662bb089950b4efd3 [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
Devang Patelca189262006-11-14 03:05:08 +000056 void removeDeadPasses(Pass *P);
Devang Patela9844592006-11-11 01:31:05 +000057
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();
Devang Patel3f0832a2006-11-14 02:54:23 +000067 LastUser.clear();
Devang Patel050ec722006-11-14 01:23:29 +000068 }
69
Devang Patelf60b5d92006-11-14 01:59:59 +000070 // All Required analyses should be available to the pass as it runs! Here
71 // we fill in the AnalysisImpls member of the pass so that it can
72 // successfully use the getAnalysis() method to retrieve the
73 // implementations it needs.
74 //
Devang Patel07f4f582006-11-14 21:49:36 +000075 void initializeAnalysisImpl(Pass *P);
Devang Patelf60b5d92006-11-14 01:59:59 +000076
Devang Patel8cad70d2006-11-11 01:51:02 +000077 inline std::vector<Pass *>::iterator passVectorBegin() {
78 return PassVector.begin();
79 }
80
81 inline std::vector<Pass *>::iterator passVectorEnd() {
82 return PassVector.end();
83 }
84
Devang Patel07f4f582006-11-14 21:49:36 +000085 inline void setLastUser(Pass *P, Pass *LU) {
86 LastUser[P] = LU;
87 // TODO : Check if pass P is available.
88
89 // Prolong live range of analyses that are needed after an analysis pass
90 // is destroyed, for querying by subsequent passes
91 AnalysisUsage AnUsage;
92 P->getAnalysisUsage(AnUsage);
93 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
94 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
95 E = IDs.end(); I != E; ++I) {
96 Pass *AnalysisPass = getAnalysisPass(*I); // getAnalysisPassFromManager(*I);
97 assert (AnalysisPass && "Analysis pass is not available");
98 setLastUser(AnalysisPass, LU);
99 }
100
101 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000102
Devang Patela9844592006-11-11 01:31:05 +0000103private:
Devang Pateldafa4dd2006-11-14 00:03:04 +0000104 // Analysis required by the passes managed by this manager. This information
105 // used while selecting pass manager during addPass. If a pass does not
106 // preserve any analysis required by other passes managed by current
107 // pass manager then new pass manager is used.
Devang Patela9844592006-11-11 01:31:05 +0000108 std::vector<AnalysisID> RequiredAnalysis;
109
Devang Pateldafa4dd2006-11-14 00:03:04 +0000110 // Set of available Analysis. This information is used while scheduling
111 // pass. If a pass requires an analysis which is not not available then
112 // equired analysis pass is scheduled to run before the pass itself is
113 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +0000114 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +0000115
Devang Patel3f0832a2006-11-14 02:54:23 +0000116 // Map to keep track of last user of the analysis pass.
117 // LastUser->second is the last user of Lastuser->first.
118 std::map<Pass *, Pass *> LastUser;
119
Devang Patel8cad70d2006-11-11 01:51:02 +0000120 // Collection of pass that are managed by this manager
121 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +0000122};
123
Devang Patelca58e352006-11-08 10:05:38 +0000124/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
125/// pass together and sequence them to process one basic block before
126/// processing next basic block.
Devang Patel0ed47792006-11-10 21:33:13 +0000127class BasicBlockPassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000128
129public:
130 BasicBlockPassManager_New() { }
131
132 /// Add a pass into a passmanager queue.
133 bool addPass(Pass *p);
134
135 /// Execute all of the passes scheduled for execution. Keep track of
136 /// whether any of the passes modifies the function, and if so, return true.
137 bool runOnFunction(Function &F);
138
Devang Patelebba9702006-11-13 22:40:09 +0000139 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000140 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000141
Devang Patelca58e352006-11-08 10:05:38 +0000142private:
Devang Patelca58e352006-11-08 10:05:38 +0000143};
144
Devang Patel4e12f862006-11-08 10:44:40 +0000145/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000146/// It batches all function passes and basic block pass managers together and
147/// sequence them to process one function at a time before processing next
148/// function.
Devang Patel0ed47792006-11-10 21:33:13 +0000149class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000150public:
Devang Patel4e12f862006-11-08 10:44:40 +0000151 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
152 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000153 activeBBPassManager = NULL;
154 }
Devang Patel4e12f862006-11-08 10:44:40 +0000155 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000156
157 /// add - Add a pass to the queue of passes to run. This passes
158 /// ownership of the Pass to the PassManager. When the
159 /// PassManager_X is destroyed, the pass will be destroyed as well, so
160 /// there is no need to delete the pass. (TODO delete passes.)
161 /// This implies that all passes MUST be allocated with 'new'.
162 void add(Pass *P) { /* TODO*/ }
163
164 /// Add pass into the pass manager queue.
165 bool addPass(Pass *P);
166
167 /// Execute all of the passes scheduled for execution. Keep
168 /// track of whether any of the passes modifies the function, and if
169 /// so, return true.
170 bool runOnModule(Module &M);
171
Devang Patelebba9702006-11-13 22:40:09 +0000172 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000173 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000174
Devang Patelca58e352006-11-08 10:05:38 +0000175private:
Devang Patelca58e352006-11-08 10:05:38 +0000176 // Active Pass Managers
177 BasicBlockPassManager_New *activeBBPassManager;
178};
179
180/// ModulePassManager_New manages ModulePasses and function pass managers.
181/// It batches all Module passes passes and function pass managers together and
182/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000183class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000184
185public:
186 ModulePassManager_New() { activeFunctionPassManager = NULL; }
187
188 /// Add a pass into a passmanager queue.
189 bool addPass(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 runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000194
195 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000196 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000197
198private:
Devang Patelca58e352006-11-08 10:05:38 +0000199 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000200 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000201};
202
Devang Patel376fefa2006-11-08 10:29:57 +0000203/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000204class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000205
206public:
207
208 /// add - Add a pass to the queue of passes to run. This passes ownership of
209 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
210 /// will be destroyed as well, so there is no need to delete the pass. This
211 /// implies that all passes MUST be allocated with 'new'.
212 void add(Pass *P);
213
214 /// run - Execute all of the passes scheduled for execution. Keep track of
215 /// whether any of the passes modifies the module, and if so, return true.
216 bool run(Module &M);
217
Devang Patelebba9702006-11-13 22:40:09 +0000218 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000219 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000220
Devang Patel376fefa2006-11-08 10:29:57 +0000221private:
222
223 /// Add a pass into a passmanager queue. This is used by schedulePasses
224 bool addPass(Pass *p);
225
Devang Patel1a6eaa42006-11-11 02:22:31 +0000226 /// Schedule pass P for execution. Make sure that passes required by
227 /// P are run before P is run. Update analysis info maintained by
228 /// the manager. Remove dead passes. This is a recursive function.
229 void schedulePass(Pass *P);
230
Devang Patel376fefa2006-11-08 10:29:57 +0000231 /// Schedule all passes collected in pass queue using add(). Add all the
232 /// schedule passes into various manager's queue using addPass().
233 void schedulePasses();
234
235 // Collection of pass managers
236 std::vector<ModulePassManager_New *> PassManagers;
237
Devang Patel376fefa2006-11-08 10:29:57 +0000238 // Active Pass Manager
239 ModulePassManager_New *activeManager;
240};
241
Devang Patelca58e352006-11-08 10:05:38 +0000242} // End of llvm namespace
243
Devang Patel0ed47792006-11-10 21:33:13 +0000244// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000245
Devang Pateld65e9e92006-11-08 01:31:28 +0000246/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000247/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000248bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000249
250 AnalysisUsage AnUsage;
251 P->getAnalysisUsage(AnUsage);
252
Devang Patel6c9f5482006-11-11 00:42:16 +0000253 // If this pass is not preserving information that is required by the other
254 // passes managed by this manager then use new manager
255 if (!AnUsage.getPreservesAll()) {
256 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
257 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
258 E = RequiredAnalysis.end(); I != E; ++I) {
259 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
260 PreservedSet.end())
261 // This analysis is not preserved. Need new manager.
262 return false;
263 }
264 }
Devang Patelf68a3492006-11-07 22:35:17 +0000265 return true;
266}
267
Devang Patel643676c2006-11-11 01:10:19 +0000268/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000269void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000270 AnalysisUsage AnUsage;
271 P->getAnalysisUsage(AnUsage);
272 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000273
Devang Patel6c9f5482006-11-11 00:42:16 +0000274 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000275 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
276 RequiredSet.end());
Devang Patel07f4f582006-11-14 21:49:36 +0000277
278 initializeAnalysisImpl(P);
Devang Patelf68a3492006-11-07 22:35:17 +0000279}
280
Devang Patel643676c2006-11-11 01:10:19 +0000281/// Augement AvailableAnalysis by adding analysis made available by pass P.
282void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000283
Devang Patel643676c2006-11-11 01:10:19 +0000284 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000285 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000286
287 //TODO This pass is the current implementation of all of the interfaces it
288 //TODO implements as well.
289 //TODO
290 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
291 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
292 //TODO CurrentAnalyses[II[i]] = P;
293 }
294}
295
Devang Patelf68a3492006-11-07 22:35:17 +0000296/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000297void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000298 AnalysisUsage AnUsage;
299 P->getAnalysisUsage(AnUsage);
300 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000301
Devang Patelf60b5d92006-11-14 01:59:59 +0000302 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000303 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000304 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000305 PreservedSet.end()) {
306 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000307 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000308 AvailableAnalysis.erase(J);
309 }
310 }
Devang Patelf68a3492006-11-07 22:35:17 +0000311}
312
Devang Patelca189262006-11-14 03:05:08 +0000313/// Remove analysis passes that are not used any longer
314void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
315
316 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
317 E = LastUser.end(); I !=E; ++I) {
318 if (I->second == P) {
319 Pass *deadPass = I->first;
320 deadPass->releaseMemory();
321
322 std::map<AnalysisID, Pass*>::iterator Pos =
323 AvailableAnalysis.find(deadPass->getPassInfo());
324
325 assert (Pos != AvailableAnalysis.end() &&
326 "Pass is not available");
327 AvailableAnalysis.erase(Pos);
328 }
329 }
330}
331
Devang Patel8cad70d2006-11-11 01:51:02 +0000332/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000333/// AvailableAnalysis appropriately if ProcessAnalysis is true.
334void CommonPassManagerImpl::addPassToManager (Pass *P,
335 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000336
Devang Patel90b05e02006-11-11 02:04:19 +0000337 if (ProcessAnalysis) {
338 // Take a note of analysis required and made available by this pass
339 noteDownRequiredAnalysis(P);
340 noteDownAvailableAnalysis(P);
341
342 // Remove the analysis not preserved by this pass
343 removeNotPreservedAnalysis(P);
344 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000345
346 // Add pass
347 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000348}
349
Devang Patel07f4f582006-11-14 21:49:36 +0000350// All Required analyses should be available to the pass as it runs! Here
351// we fill in the AnalysisImpls member of the pass so that it can
352// successfully use the getAnalysis() method to retrieve the
353// implementations it needs.
354//
355void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) {
356 AnalysisUsage AnUsage;
357 P->getAnalysisUsage(AnUsage);
358
359 for (std::vector<const PassInfo *>::const_iterator
360 I = AnUsage.getRequiredSet().begin(),
361 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
362 Pass *Impl = getAnalysisPass(*I);
363 if (Impl == 0)
364 assert(0 && "Analysis used but not available!");
365 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
366 }
367}
368
Devang Patel6e5a1132006-11-07 21:31:57 +0000369/// BasicBlockPassManager implementation
370
Devang Pateld65e9e92006-11-08 01:31:28 +0000371/// Add pass P into PassVector and return true. If this pass is not
372/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000373bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000374BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000375
376 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
377 if (!BP)
378 return false;
379
Devang Patel3c8eb622006-11-07 22:56:50 +0000380 // If this pass does not preserve anlysis that is used by other passes
381 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000382 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000383 return false;
384
Devang Patel8cad70d2006-11-11 01:51:02 +0000385 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000386
Devang Patel6e5a1132006-11-07 21:31:57 +0000387 return true;
388}
389
390/// Execute all of the passes scheduled for execution by invoking
391/// runOnBasicBlock method. Keep track of whether any of the passes modifies
392/// the function, and if so, return true.
393bool
394BasicBlockPassManager_New::runOnFunction(Function &F) {
395
396 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000397 clearAnalysis();
398
Devang Patel6e5a1132006-11-07 21:31:57 +0000399 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000400 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
401 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000402 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000403
404 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000405 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
406 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000407 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000408 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000409 }
410 return Changed;
411}
412
Devang Patelebba9702006-11-13 22:40:09 +0000413/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000414Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
415 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000416}
417
Devang Patel0c2012f2006-11-07 21:49:50 +0000418// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000419/// Create new Function pass manager
420FunctionPassManager_New::FunctionPassManager_New() {
421 FPM = new FunctionPassManagerImpl_New();
422}
423
424/// add - Add a pass to the queue of passes to run. This passes
425/// ownership of the Pass to the PassManager. When the
426/// PassManager_X is destroyed, the pass will be destroyed as well, so
427/// there is no need to delete the pass. (TODO delete passes.)
428/// This implies that all passes MUST be allocated with 'new'.
429void
430FunctionPassManager_New::add(Pass *P) {
431 FPM->add(P);
432}
433
434/// Execute all of the passes scheduled for execution. Keep
435/// track of whether any of the passes modifies the function, and if
436/// so, return true.
437bool
438FunctionPassManager_New::runOnModule(Module &M) {
439 return FPM->runOnModule(M);
440}
441
442// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000443
Devang Patel0c2012f2006-11-07 21:49:50 +0000444// FunctionPassManager
445
446/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
447/// either use it into active basic block pass manager or create new basic
448/// block pass manager to handle pass P.
449bool
Devang Patel4e12f862006-11-08 10:44:40 +0000450FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000451
452 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
453 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
454
455 if (!activeBBPassManager
456 || !activeBBPassManager->addPass(BP)) {
457
458 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000459 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000460 if (!activeBBPassManager->addPass(BP))
461 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000462 }
463 return true;
464 }
465
466 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
467 if (!FP)
468 return false;
469
Devang Patel3c8eb622006-11-07 22:56:50 +0000470 // If this pass does not preserve anlysis that is used by other passes
471 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000472 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000473 return false;
474
Devang Patel8cad70d2006-11-11 01:51:02 +0000475 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000476 activeBBPassManager = NULL;
477 return true;
478}
479
480/// Execute all of the passes scheduled for execution by invoking
481/// runOnFunction method. Keep track of whether any of the passes modifies
482/// the function, and if so, return true.
483bool
Devang Patel4e12f862006-11-08 10:44:40 +0000484FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000485
486 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000487 clearAnalysis();
488
Devang Patel0c2012f2006-11-07 21:49:50 +0000489 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000490 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
491 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000492 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000493
494 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000495 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
496 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000497 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000498 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000499 }
500 return Changed;
501}
502
Devang Patelebba9702006-11-13 22:40:09 +0000503/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000504Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000505
Devang Patel3f0832a2006-11-14 02:54:23 +0000506 Pass *P = getAnalysisPass(AID);
507 if (P)
508 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000509
510 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000511 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000512 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000513
514 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000515 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000516}
Devang Patel0c2012f2006-11-07 21:49:50 +0000517
Devang Patel05e1a972006-11-07 22:03:15 +0000518// ModulePassManager implementation
519
520/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000521/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000522/// is not manageable by this manager.
523bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000524ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000525
526 // If P is FunctionPass then use function pass maanager.
527 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
528
529 activeFunctionPassManager = NULL;
530
531 if (!activeFunctionPassManager
532 || !activeFunctionPassManager->addPass(P)) {
533
Devang Patel4e12f862006-11-08 10:44:40 +0000534 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000535 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000536 if (!activeFunctionPassManager->addPass(FP))
537 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000538 }
539 return true;
540 }
541
542 ModulePass *MP = dynamic_cast<ModulePass *>(P);
543 if (!MP)
544 return false;
545
Devang Patel3c8eb622006-11-07 22:56:50 +0000546 // If this pass does not preserve anlysis that is used by other passes
547 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000548 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000549 return false;
550
Devang Patel8cad70d2006-11-11 01:51:02 +0000551 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000552 activeFunctionPassManager = NULL;
553 return true;
554}
555
556
557/// Execute all of the passes scheduled for execution by invoking
558/// runOnModule method. Keep track of whether any of the passes modifies
559/// the module, and if so, return true.
560bool
561ModulePassManager_New::runOnModule(Module &M) {
562 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000563 clearAnalysis();
564
Devang Patel8cad70d2006-11-11 01:51:02 +0000565 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
566 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000567 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000568
569 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000570 ModulePass *MP = dynamic_cast<ModulePass*>(P);
571 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000572 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000573 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000574 }
575 return Changed;
576}
577
Devang Patelebba9702006-11-13 22:40:09 +0000578/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000579Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000580
Devang Patel3f0832a2006-11-14 02:54:23 +0000581
582 Pass *P = getAnalysisPass(AID);
583 if (P)
584 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000585
586 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000587 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000588 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000589
590 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000591 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000592}
593
594/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000595Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000596
Devang Patel3f0832a2006-11-14 02:54:23 +0000597 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000598 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000599 e = PassManagers.end(); !P && itr != e; ++itr)
600 P = (*itr)->getAnalysisPassFromManager(AID);
601 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000602}
603
Devang Patel1a6eaa42006-11-11 02:22:31 +0000604/// Schedule pass P for execution. Make sure that passes required by
605/// P are run before P is run. Update analysis info maintained by
606/// the manager. Remove dead passes. This is a recursive function.
607void PassManagerImpl_New::schedulePass(Pass *P) {
608
609 AnalysisUsage AnUsage;
610 P->getAnalysisUsage(AnUsage);
611 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
612 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
613 E = RequiredSet.end(); I != E; ++I) {
614
Devang Patel3f0832a2006-11-14 02:54:23 +0000615 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
616 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000617 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000618 AnalysisPass = (*I)->createPass();
619 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000620 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000621 setLastUser (AnalysisPass, P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000622 }
623
624 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000625}
626
Devang Patelc290c8a2006-11-07 22:23:34 +0000627/// Schedule all passes from the queue by adding them in their
628/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000629void PassManagerImpl_New::schedulePasses() {
630 for (std::vector<Pass *>::iterator I = passVectorBegin(),
631 E = passVectorEnd(); I != E; ++I)
632 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000633}
634
635/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000636void PassManagerImpl_New::add(Pass *P) {
637 // Do not process Analysis now. Analysis is process while scheduling
638 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000639 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000640}
641
642// PassManager_New implementation
643/// Add P into active pass manager or use new module pass manager to
644/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000645bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000646
Devang Patel6c9f5482006-11-11 00:42:16 +0000647 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000648 activeManager = new ModulePassManager_New();
649 PassManagers.push_back(activeManager);
650 }
651
652 return activeManager->addPass(P);
653}
654
655/// run - Execute all of the passes scheduled for execution. Keep track of
656/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000657bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000658
659 schedulePasses();
660 bool Changed = false;
661 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
662 e = PassManagers.end(); itr != e; ++itr) {
663 ModulePassManager_New *pm = *itr;
664 Changed |= pm->runOnModule(M);
665 }
666 return Changed;
667}
Devang Patel376fefa2006-11-08 10:29:57 +0000668
669/// Create new pass manager
670PassManager_New::PassManager_New() {
671 PM = new PassManagerImpl_New();
672}
673
674/// add - Add a pass to the queue of passes to run. This passes ownership of
675/// the Pass to the PassManager. When the PassManager is destroyed, the pass
676/// will be destroyed as well, so there is no need to delete the pass. This
677/// implies that all passes MUST be allocated with 'new'.
678void
679PassManager_New::add(Pass *P) {
680 PM->add(P);
681}
682
683/// run - Execute all of the passes scheduled for execution. Keep track of
684/// whether any of the passes modifies the module, and if so, return true.
685bool
686PassManager_New::run(Module &M) {
687 return PM->run(M);
688}
689