blob: 0f52b2d674ea9d278cb1e032f409167f8a3feb16 [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 //
75 inline void initializeAnalysisImpl(Pass *P) { /* TODO : Implement */ }
76
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 Patel3f0832a2006-11-14 02:54:23 +000085 inline void setLastUser(Pass *P, Pass *LU) { LastUser[P] = LU; }
86
Devang Patela9844592006-11-11 01:31:05 +000087private:
Devang Pateldafa4dd2006-11-14 00:03:04 +000088 // Analysis required by the passes managed by this manager. This information
89 // used while selecting pass manager during addPass. If a pass does not
90 // preserve any analysis required by other passes managed by current
91 // pass manager then new pass manager is used.
Devang Patela9844592006-11-11 01:31:05 +000092 std::vector<AnalysisID> RequiredAnalysis;
93
Devang Pateldafa4dd2006-11-14 00:03:04 +000094 // Set of available Analysis. This information is used while scheduling
95 // pass. If a pass requires an analysis which is not not available then
96 // equired analysis pass is scheduled to run before the pass itself is
97 // scheduled to run.
Devang Patelf60b5d92006-11-14 01:59:59 +000098 std::map<AnalysisID, Pass*> AvailableAnalysis;
Devang Patel8cad70d2006-11-11 01:51:02 +000099
Devang Patel3f0832a2006-11-14 02:54:23 +0000100 // Map to keep track of last user of the analysis pass.
101 // LastUser->second is the last user of Lastuser->first.
102 std::map<Pass *, Pass *> LastUser;
103
Devang Patel8cad70d2006-11-11 01:51:02 +0000104 // Collection of pass that are managed by this manager
105 std::vector<Pass *> PassVector;
Devang Patela9844592006-11-11 01:31:05 +0000106};
107
Devang Patelca58e352006-11-08 10:05:38 +0000108/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
109/// pass together and sequence them to process one basic block before
110/// processing next basic block.
Devang Patel0ed47792006-11-10 21:33:13 +0000111class BasicBlockPassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000112
113public:
114 BasicBlockPassManager_New() { }
115
116 /// Add a pass into a passmanager queue.
117 bool addPass(Pass *p);
118
119 /// Execute all of the passes scheduled for execution. Keep track of
120 /// whether any of the passes modifies the function, and if so, return true.
121 bool runOnFunction(Function &F);
122
Devang Patelebba9702006-11-13 22:40:09 +0000123 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000124 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000125
Devang Patelca58e352006-11-08 10:05:38 +0000126private:
Devang Patelca58e352006-11-08 10:05:38 +0000127};
128
Devang Patel4e12f862006-11-08 10:44:40 +0000129/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +0000130/// It batches all function passes and basic block pass managers together and
131/// sequence them to process one function at a time before processing next
132/// function.
Devang Patel0ed47792006-11-10 21:33:13 +0000133class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000134public:
Devang Patel4e12f862006-11-08 10:44:40 +0000135 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
136 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +0000137 activeBBPassManager = NULL;
138 }
Devang Patel4e12f862006-11-08 10:44:40 +0000139 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +0000140
141 /// add - Add a pass to the queue of passes to run. This passes
142 /// ownership of the Pass to the PassManager. When the
143 /// PassManager_X is destroyed, the pass will be destroyed as well, so
144 /// there is no need to delete the pass. (TODO delete passes.)
145 /// This implies that all passes MUST be allocated with 'new'.
146 void add(Pass *P) { /* TODO*/ }
147
148 /// Add pass into the pass manager queue.
149 bool addPass(Pass *P);
150
151 /// Execute all of the passes scheduled for execution. Keep
152 /// track of whether any of the passes modifies the function, and if
153 /// so, return true.
154 bool runOnModule(Module &M);
155
Devang Patelebba9702006-11-13 22:40:09 +0000156 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000157 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000158
Devang Patelca58e352006-11-08 10:05:38 +0000159private:
Devang Patelca58e352006-11-08 10:05:38 +0000160 // Active Pass Managers
161 BasicBlockPassManager_New *activeBBPassManager;
162};
163
164/// ModulePassManager_New manages ModulePasses and function pass managers.
165/// It batches all Module passes passes and function pass managers together and
166/// sequence them to process one module.
Devang Patel0ed47792006-11-10 21:33:13 +0000167class ModulePassManager_New : public CommonPassManagerImpl {
Devang Patelca58e352006-11-08 10:05:38 +0000168
169public:
170 ModulePassManager_New() { activeFunctionPassManager = NULL; }
171
172 /// Add a pass into a passmanager queue.
173 bool addPass(Pass *p);
174
175 /// run - Execute all of the passes scheduled for execution. Keep track of
176 /// whether any of the passes modifies the module, and if so, return true.
177 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000178
179 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000180 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelca58e352006-11-08 10:05:38 +0000181
182private:
Devang Patelca58e352006-11-08 10:05:38 +0000183 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000184 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000185};
186
Devang Patel376fefa2006-11-08 10:29:57 +0000187/// PassManager_New manages ModulePassManagers
Devang Patel0ed47792006-11-10 21:33:13 +0000188class PassManagerImpl_New : public CommonPassManagerImpl {
Devang Patel376fefa2006-11-08 10:29:57 +0000189
190public:
191
192 /// add - Add a pass to the queue of passes to run. This passes ownership of
193 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
194 /// will be destroyed as well, so there is no need to delete the pass. This
195 /// implies that all passes MUST be allocated with 'new'.
196 void add(Pass *P);
197
198 /// run - Execute all of the passes scheduled for execution. Keep track of
199 /// whether any of the passes modifies the module, and if so, return true.
200 bool run(Module &M);
201
Devang Patelebba9702006-11-13 22:40:09 +0000202 /// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000203 Pass *getAnalysisPassFromManager(AnalysisID AID);
Devang Patelebba9702006-11-13 22:40:09 +0000204
Devang Patel376fefa2006-11-08 10:29:57 +0000205private:
206
207 /// Add a pass into a passmanager queue. This is used by schedulePasses
208 bool addPass(Pass *p);
209
Devang Patel1a6eaa42006-11-11 02:22:31 +0000210 /// Schedule pass P for execution. Make sure that passes required by
211 /// P are run before P is run. Update analysis info maintained by
212 /// the manager. Remove dead passes. This is a recursive function.
213 void schedulePass(Pass *P);
214
Devang Patel376fefa2006-11-08 10:29:57 +0000215 /// Schedule all passes collected in pass queue using add(). Add all the
216 /// schedule passes into various manager's queue using addPass().
217 void schedulePasses();
218
219 // Collection of pass managers
220 std::vector<ModulePassManager_New *> PassManagers;
221
Devang Patel376fefa2006-11-08 10:29:57 +0000222 // Active Pass Manager
223 ModulePassManager_New *activeManager;
224};
225
Devang Patelca58e352006-11-08 10:05:38 +0000226} // End of llvm namespace
227
Devang Patel0ed47792006-11-10 21:33:13 +0000228// CommonPassManagerImpl implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000229
Devang Pateld65e9e92006-11-08 01:31:28 +0000230/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000231/// manager.
Devang Patel0ed47792006-11-10 21:33:13 +0000232bool CommonPassManagerImpl::manageablePass(Pass *P) {
Devang Patelf68a3492006-11-07 22:35:17 +0000233
234 AnalysisUsage AnUsage;
235 P->getAnalysisUsage(AnUsage);
236
Devang Patel6c9f5482006-11-11 00:42:16 +0000237 // If this pass is not preserving information that is required by the other
238 // passes managed by this manager then use new manager
239 if (!AnUsage.getPreservesAll()) {
240 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
241 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
242 E = RequiredAnalysis.end(); I != E; ++I) {
243 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
244 PreservedSet.end())
245 // This analysis is not preserved. Need new manager.
246 return false;
247 }
248 }
Devang Patelf68a3492006-11-07 22:35:17 +0000249 return true;
250}
251
Devang Patel643676c2006-11-11 01:10:19 +0000252/// Augment RequiredAnalysis by adding analysis required by pass P.
Devang Patel0ed47792006-11-10 21:33:13 +0000253void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
Devang Patel6c9f5482006-11-11 00:42:16 +0000254 AnalysisUsage AnUsage;
255 P->getAnalysisUsage(AnUsage);
256 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000257
Devang Patel6c9f5482006-11-11 00:42:16 +0000258 // FIXME: What about duplicates ?
Devang Patel349170f2006-11-11 01:24:55 +0000259 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(),
260 RequiredSet.end());
Devang Patelf68a3492006-11-07 22:35:17 +0000261}
262
Devang Patel643676c2006-11-11 01:10:19 +0000263/// Augement AvailableAnalysis by adding analysis made available by pass P.
264void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000265
Devang Patel643676c2006-11-11 01:10:19 +0000266 if (const PassInfo *PI = P->getPassInfo()) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000267 AvailableAnalysis[PI] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000268
269 //TODO This pass is the current implementation of all of the interfaces it
270 //TODO implements as well.
271 //TODO
272 //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
273 //TODO for (unsigned i = 0, e = II.size(); i != e; ++i)
274 //TODO CurrentAnalyses[II[i]] = P;
275 }
276}
277
Devang Patelf68a3492006-11-07 22:35:17 +0000278/// Remove Analyss not preserved by Pass P
Devang Patel0ed47792006-11-10 21:33:13 +0000279void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
Devang Patel349170f2006-11-11 01:24:55 +0000280 AnalysisUsage AnUsage;
281 P->getAnalysisUsage(AnUsage);
282 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000283
Devang Patelf60b5d92006-11-14 01:59:59 +0000284 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel349170f2006-11-11 01:24:55 +0000285 E = AvailableAnalysis.end(); I != E; ++I ) {
Devang Patelf60b5d92006-11-14 01:59:59 +0000286 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
Devang Patel349170f2006-11-11 01:24:55 +0000287 PreservedSet.end()) {
288 // Remove this analysis
Devang Patelf60b5d92006-11-14 01:59:59 +0000289 std::map<AnalysisID, Pass*>::iterator J = I++;
Devang Patel349170f2006-11-11 01:24:55 +0000290 AvailableAnalysis.erase(J);
291 }
292 }
Devang Patelf68a3492006-11-07 22:35:17 +0000293}
294
Devang Patelca189262006-11-14 03:05:08 +0000295/// Remove analysis passes that are not used any longer
296void CommonPassManagerImpl::removeDeadPasses(Pass *P) {
297
298 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
299 E = LastUser.end(); I !=E; ++I) {
300 if (I->second == P) {
301 Pass *deadPass = I->first;
302 deadPass->releaseMemory();
303
304 std::map<AnalysisID, Pass*>::iterator Pos =
305 AvailableAnalysis.find(deadPass->getPassInfo());
306
307 assert (Pos != AvailableAnalysis.end() &&
308 "Pass is not available");
309 AvailableAnalysis.erase(Pos);
310 }
311 }
312}
313
Devang Patel8cad70d2006-11-11 01:51:02 +0000314/// Add pass P into the PassVector. Update RequiredAnalysis and
Devang Patel90b05e02006-11-11 02:04:19 +0000315/// AvailableAnalysis appropriately if ProcessAnalysis is true.
316void CommonPassManagerImpl::addPassToManager (Pass *P,
317 bool ProcessAnalysis) {
Devang Patel8cad70d2006-11-11 01:51:02 +0000318
Devang Patel90b05e02006-11-11 02:04:19 +0000319 if (ProcessAnalysis) {
320 // Take a note of analysis required and made available by this pass
321 noteDownRequiredAnalysis(P);
322 noteDownAvailableAnalysis(P);
323
324 // Remove the analysis not preserved by this pass
325 removeNotPreservedAnalysis(P);
326 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000327
328 // Add pass
329 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +0000330}
331
Devang Patel6e5a1132006-11-07 21:31:57 +0000332/// BasicBlockPassManager implementation
333
Devang Pateld65e9e92006-11-08 01:31:28 +0000334/// Add pass P into PassVector and return true. If this pass is not
335/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000336bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000337BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000338
339 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
340 if (!BP)
341 return false;
342
Devang Patel3c8eb622006-11-07 22:56:50 +0000343 // If this pass does not preserve anlysis that is used by other passes
344 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000345 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000346 return false;
347
Devang Patel8cad70d2006-11-11 01:51:02 +0000348 addPassToManager (BP);
Devang Patel349170f2006-11-11 01:24:55 +0000349
Devang Patel6e5a1132006-11-07 21:31:57 +0000350 return true;
351}
352
353/// Execute all of the passes scheduled for execution by invoking
354/// runOnBasicBlock method. Keep track of whether any of the passes modifies
355/// the function, and if so, return true.
356bool
357BasicBlockPassManager_New::runOnFunction(Function &F) {
358
359 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000360 clearAnalysis();
361
Devang Patel6e5a1132006-11-07 21:31:57 +0000362 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000363 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
364 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000365 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000366
367 noteDownAvailableAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000368 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
369 Changed |= BP->runOnBasicBlock(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000370 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000371 removeDeadPasses(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000372 }
373 return Changed;
374}
375
Devang Patelebba9702006-11-13 22:40:09 +0000376/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000377Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
378 return getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000379}
380
Devang Patel0c2012f2006-11-07 21:49:50 +0000381// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000382/// Create new Function pass manager
383FunctionPassManager_New::FunctionPassManager_New() {
384 FPM = new FunctionPassManagerImpl_New();
385}
386
387/// add - Add a pass to the queue of passes to run. This passes
388/// ownership of the Pass to the PassManager. When the
389/// PassManager_X is destroyed, the pass will be destroyed as well, so
390/// there is no need to delete the pass. (TODO delete passes.)
391/// This implies that all passes MUST be allocated with 'new'.
392void
393FunctionPassManager_New::add(Pass *P) {
394 FPM->add(P);
395}
396
397/// Execute all of the passes scheduled for execution. Keep
398/// track of whether any of the passes modifies the function, and if
399/// so, return true.
400bool
401FunctionPassManager_New::runOnModule(Module &M) {
402 return FPM->runOnModule(M);
403}
404
405// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000406
Devang Patel0c2012f2006-11-07 21:49:50 +0000407// FunctionPassManager
408
409/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
410/// either use it into active basic block pass manager or create new basic
411/// block pass manager to handle pass P.
412bool
Devang Patel4e12f862006-11-08 10:44:40 +0000413FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000414
415 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
416 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
417
418 if (!activeBBPassManager
419 || !activeBBPassManager->addPass(BP)) {
420
421 activeBBPassManager = new BasicBlockPassManager_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000422 addPassToManager(activeBBPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000423 if (!activeBBPassManager->addPass(BP))
424 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000425 }
426 return true;
427 }
428
429 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
430 if (!FP)
431 return false;
432
Devang Patel3c8eb622006-11-07 22:56:50 +0000433 // If this pass does not preserve anlysis that is used by other passes
434 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000435 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000436 return false;
437
Devang Patel8cad70d2006-11-11 01:51:02 +0000438 addPassToManager (FP);
Devang Patel0c2012f2006-11-07 21:49:50 +0000439 activeBBPassManager = NULL;
440 return true;
441}
442
443/// Execute all of the passes scheduled for execution by invoking
444/// runOnFunction method. Keep track of whether any of the passes modifies
445/// the function, and if so, return true.
446bool
Devang Patel4e12f862006-11-08 10:44:40 +0000447FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000448
449 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000450 clearAnalysis();
451
Devang Patel0c2012f2006-11-07 21:49:50 +0000452 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Devang Patel8cad70d2006-11-11 01:51:02 +0000453 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
454 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000455 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000456
457 noteDownAvailableAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000458 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
459 Changed |= FP->runOnFunction(*I);
Devang Patel050ec722006-11-14 01:23:29 +0000460 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000461 removeDeadPasses(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000462 }
463 return Changed;
464}
465
Devang Patelebba9702006-11-13 22:40:09 +0000466/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000467Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000468
Devang Patel3f0832a2006-11-14 02:54:23 +0000469 Pass *P = getAnalysisPass(AID);
470 if (P)
471 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000472
473 if (activeBBPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000474 activeBBPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000475 return activeBBPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000476
477 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000478 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000479}
Devang Patel0c2012f2006-11-07 21:49:50 +0000480
Devang Patel05e1a972006-11-07 22:03:15 +0000481// ModulePassManager implementation
482
483/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000484/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000485/// is not manageable by this manager.
486bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000487ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000488
489 // If P is FunctionPass then use function pass maanager.
490 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
491
492 activeFunctionPassManager = NULL;
493
494 if (!activeFunctionPassManager
495 || !activeFunctionPassManager->addPass(P)) {
496
Devang Patel4e12f862006-11-08 10:44:40 +0000497 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel90b05e02006-11-11 02:04:19 +0000498 addPassToManager(activeFunctionPassManager, false);
Devang Pateld65e9e92006-11-08 01:31:28 +0000499 if (!activeFunctionPassManager->addPass(FP))
500 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000501 }
502 return true;
503 }
504
505 ModulePass *MP = dynamic_cast<ModulePass *>(P);
506 if (!MP)
507 return false;
508
Devang Patel3c8eb622006-11-07 22:56:50 +0000509 // If this pass does not preserve anlysis that is used by other passes
510 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000511 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000512 return false;
513
Devang Patel8cad70d2006-11-11 01:51:02 +0000514 addPassToManager(MP);
Devang Patel05e1a972006-11-07 22:03:15 +0000515 activeFunctionPassManager = NULL;
516 return true;
517}
518
519
520/// Execute all of the passes scheduled for execution by invoking
521/// runOnModule method. Keep track of whether any of the passes modifies
522/// the module, and if so, return true.
523bool
524ModulePassManager_New::runOnModule(Module &M) {
525 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +0000526 clearAnalysis();
527
Devang Patel8cad70d2006-11-11 01:51:02 +0000528 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
529 e = passVectorEnd(); itr != e; ++itr) {
Devang Patel05e1a972006-11-07 22:03:15 +0000530 Pass *P = *itr;
Devang Patel050ec722006-11-14 01:23:29 +0000531
532 noteDownAvailableAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000533 ModulePass *MP = dynamic_cast<ModulePass*>(P);
534 Changed |= MP->runOnModule(M);
Devang Patel050ec722006-11-14 01:23:29 +0000535 removeNotPreservedAnalysis(P);
Devang Patelca189262006-11-14 03:05:08 +0000536 removeDeadPasses(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000537 }
538 return Changed;
539}
540
Devang Patelebba9702006-11-13 22:40:09 +0000541/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000542Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000543
Devang Patel3f0832a2006-11-14 02:54:23 +0000544
545 Pass *P = getAnalysisPass(AID);
546 if (P)
547 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000548
549 if (activeFunctionPassManager &&
Devang Patelf60b5d92006-11-14 01:59:59 +0000550 activeFunctionPassManager->getAnalysisPass(AID) != 0)
Devang Patel3f0832a2006-11-14 02:54:23 +0000551 return activeFunctionPassManager->getAnalysisPass(AID);
Devang Patelebba9702006-11-13 22:40:09 +0000552
553 // TODO : Check inactive managers
Devang Patel3f0832a2006-11-14 02:54:23 +0000554 return NULL;
Devang Patelebba9702006-11-13 22:40:09 +0000555}
556
557/// Return true IFF AnalysisID AID is currently available.
Devang Patel3f0832a2006-11-14 02:54:23 +0000558Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
Devang Patelebba9702006-11-13 22:40:09 +0000559
Devang Patel3f0832a2006-11-14 02:54:23 +0000560 Pass *P = NULL;
Devang Patel70868442006-11-13 22:53:19 +0000561 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
Devang Patel3f0832a2006-11-14 02:54:23 +0000562 e = PassManagers.end(); !P && itr != e; ++itr)
563 P = (*itr)->getAnalysisPassFromManager(AID);
564 return P;
Devang Patelebba9702006-11-13 22:40:09 +0000565}
566
Devang Patel1a6eaa42006-11-11 02:22:31 +0000567/// Schedule pass P for execution. Make sure that passes required by
568/// P are run before P is run. Update analysis info maintained by
569/// the manager. Remove dead passes. This is a recursive function.
570void PassManagerImpl_New::schedulePass(Pass *P) {
571
572 AnalysisUsage AnUsage;
573 P->getAnalysisUsage(AnUsage);
574 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
575 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
576 E = RequiredSet.end(); I != E; ++I) {
577
Devang Patel3f0832a2006-11-14 02:54:23 +0000578 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
579 if (!AnalysisPass) {
Devang Patel1a6eaa42006-11-11 02:22:31 +0000580 // Schedule this analysis run first.
Devang Patel3f0832a2006-11-14 02:54:23 +0000581 AnalysisPass = (*I)->createPass();
582 schedulePass(AnalysisPass);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000583 }
Devang Patel3f0832a2006-11-14 02:54:23 +0000584 setLastUser (AnalysisPass, P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000585 }
586
587 addPass(P);
Devang Patel1a6eaa42006-11-11 02:22:31 +0000588}
589
Devang Patelc290c8a2006-11-07 22:23:34 +0000590/// Schedule all passes from the queue by adding them in their
591/// respective manager's queue.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000592void PassManagerImpl_New::schedulePasses() {
593 for (std::vector<Pass *>::iterator I = passVectorBegin(),
594 E = passVectorEnd(); I != E; ++I)
595 schedulePass (*I);
Devang Patelc290c8a2006-11-07 22:23:34 +0000596}
597
598/// Add pass P to the queue of passes to run.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000599void PassManagerImpl_New::add(Pass *P) {
600 // Do not process Analysis now. Analysis is process while scheduling
601 // the pass vector.
Devang Pateldb789fb2006-11-11 02:06:21 +0000602 addPassToManager(P, false);
Devang Patelc290c8a2006-11-07 22:23:34 +0000603}
604
605// PassManager_New implementation
606/// Add P into active pass manager or use new module pass manager to
607/// manage it.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000608bool PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000609
Devang Patel6c9f5482006-11-11 00:42:16 +0000610 if (!activeManager || !activeManager->addPass(P)) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000611 activeManager = new ModulePassManager_New();
612 PassManagers.push_back(activeManager);
613 }
614
615 return activeManager->addPass(P);
616}
617
618/// run - Execute all of the passes scheduled for execution. Keep track of
619/// whether any of the passes modifies the module, and if so, return true.
Devang Patel1a6eaa42006-11-11 02:22:31 +0000620bool PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000621
622 schedulePasses();
623 bool Changed = false;
624 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
625 e = PassManagers.end(); itr != e; ++itr) {
626 ModulePassManager_New *pm = *itr;
627 Changed |= pm->runOnModule(M);
628 }
629 return Changed;
630}
Devang Patel376fefa2006-11-08 10:29:57 +0000631
632/// Create new pass manager
633PassManager_New::PassManager_New() {
634 PM = new PassManagerImpl_New();
635}
636
637/// add - Add a pass to the queue of passes to run. This passes ownership of
638/// the Pass to the PassManager. When the PassManager is destroyed, the pass
639/// will be destroyed as well, so there is no need to delete the pass. This
640/// implies that all passes MUST be allocated with 'new'.
641void
642PassManager_New::add(Pass *P) {
643 PM->add(P);
644}
645
646/// run - Execute all of the passes scheduled for execution. Keep track of
647/// whether any of the passes modifies the module, and if so, return true.
648bool
649PassManager_New::run(Module &M) {
650 return PM->run(M);
651}
652