blob: b1d262fe32ade08471598e2909ee17ccff2eb76b [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"
17
18using namespace llvm;
19
Devang Patelca58e352006-11-08 10:05:38 +000020namespace llvm {
21
22/// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
23/// pass together and sequence them to process one basic block before
24/// processing next basic block.
25class BasicBlockPassManager_New : public Pass,
26 public PassManagerAnalysisHelper {
27
28public:
29 BasicBlockPassManager_New() { }
30
31 /// Add a pass into a passmanager queue.
32 bool addPass(Pass *p);
33
34 /// Execute all of the passes scheduled for execution. Keep track of
35 /// whether any of the passes modifies the function, and if so, return true.
36 bool runOnFunction(Function &F);
37
38private:
39 // Collection of pass that are managed by this manager
40 std::vector<Pass *> PassVector;
41};
42
Devang Patel4e12f862006-11-08 10:44:40 +000043/// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
Devang Patelca58e352006-11-08 10:05:38 +000044/// It batches all function passes and basic block pass managers together and
45/// sequence them to process one function at a time before processing next
46/// function.
Devang Patel4e12f862006-11-08 10:44:40 +000047class FunctionPassManagerImpl_New : public Pass,
Devang Patelca58e352006-11-08 10:05:38 +000048 public PassManagerAnalysisHelper {
49public:
Devang Patel4e12f862006-11-08 10:44:40 +000050 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
51 FunctionPassManagerImpl_New() {
Devang Patelca58e352006-11-08 10:05:38 +000052 activeBBPassManager = NULL;
53 }
Devang Patel4e12f862006-11-08 10:44:40 +000054 ~FunctionPassManagerImpl_New() { /* TODO */ };
Devang Patelca58e352006-11-08 10:05:38 +000055
56 /// add - Add a pass to the queue of passes to run. This passes
57 /// ownership of the Pass to the PassManager. When the
58 /// PassManager_X is destroyed, the pass will be destroyed as well, so
59 /// there is no need to delete the pass. (TODO delete passes.)
60 /// This implies that all passes MUST be allocated with 'new'.
61 void add(Pass *P) { /* TODO*/ }
62
63 /// Add pass into the pass manager queue.
64 bool addPass(Pass *P);
65
66 /// Execute all of the passes scheduled for execution. Keep
67 /// track of whether any of the passes modifies the function, and if
68 /// so, return true.
69 bool runOnModule(Module &M);
70
71private:
72 // Collection of pass that are manged by this manager
73 std::vector<Pass *> PassVector;
74
75 // Active Pass Managers
76 BasicBlockPassManager_New *activeBBPassManager;
77};
78
79/// ModulePassManager_New manages ModulePasses and function pass managers.
80/// It batches all Module passes passes and function pass managers together and
81/// sequence them to process one module.
82class ModulePassManager_New : public Pass,
83 public PassManagerAnalysisHelper {
84
85public:
86 ModulePassManager_New() { activeFunctionPassManager = NULL; }
87
88 /// Add a pass into a passmanager queue.
89 bool addPass(Pass *p);
90
91 /// run - Execute all of the passes scheduled for execution. Keep track of
92 /// whether any of the passes modifies the module, and if so, return true.
93 bool runOnModule(Module &M);
94
95private:
96 // Collection of pass that are managed by this manager
97 std::vector<Pass *> PassVector;
98
99 // Active Pass Manager
Devang Patel4e12f862006-11-08 10:44:40 +0000100 FunctionPassManagerImpl_New *activeFunctionPassManager;
Devang Patelca58e352006-11-08 10:05:38 +0000101};
102
Devang Patel376fefa2006-11-08 10:29:57 +0000103/// PassManager_New manages ModulePassManagers
104class PassManagerImpl_New : public Pass,
105 public PassManagerAnalysisHelper {
106
107public:
108
109 /// add - Add a pass to the queue of passes to run. This passes ownership of
110 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
111 /// will be destroyed as well, so there is no need to delete the pass. This
112 /// implies that all passes MUST be allocated with 'new'.
113 void add(Pass *P);
114
115 /// run - Execute all of the passes scheduled for execution. Keep track of
116 /// whether any of the passes modifies the module, and if so, return true.
117 bool run(Module &M);
118
119private:
120
121 /// Add a pass into a passmanager queue. This is used by schedulePasses
122 bool addPass(Pass *p);
123
124 /// Schedule all passes collected in pass queue using add(). Add all the
125 /// schedule passes into various manager's queue using addPass().
126 void schedulePasses();
127
128 // Collection of pass managers
129 std::vector<ModulePassManager_New *> PassManagers;
130
131 // Collection of pass that are not yet scheduled
132 std::vector<Pass *> PassVector;
133
134 // Active Pass Manager
135 ModulePassManager_New *activeManager;
136};
137
Devang Patelca58e352006-11-08 10:05:38 +0000138} // End of llvm namespace
139
Devang Patelf68a3492006-11-07 22:35:17 +0000140// PassManagerAnalysisHelper implementation
141
Devang Pateld65e9e92006-11-08 01:31:28 +0000142/// Return true IFF pass P's required analysis set does not required new
Devang Patelf68a3492006-11-07 22:35:17 +0000143/// manager.
144bool PassManagerAnalysisHelper::manageablePass(Pass *P) {
145
146 AnalysisUsage AnUsage;
147 P->getAnalysisUsage(AnUsage);
148
149 // If this pass is not preserving information that is required by the other passes
150 // managed by this manager then use new manager
151 // TODO
152 return true;
153}
154
Devang Pateld65e9e92006-11-08 01:31:28 +0000155/// Return true IFF AnalysisID AID is currently available.
Devang Patelf68a3492006-11-07 22:35:17 +0000156bool PassManagerAnalysisHelper::analysisCurrentlyAvailable(AnalysisID AID) {
157
158 // TODO
159 return false;
160}
161
162/// Augment RequiredSet by adding analysis required by pass P.
163void PassManagerAnalysisHelper::noteDownRequiredAnalysis(Pass *P) {
164
165 // TODO
166}
167
168/// Remove AnalysisID from the RequiredSet
169void PassManagerAnalysisHelper::removeAnalysis(AnalysisID AID) {
170
171 // TODO
172}
173
174/// Remove Analyss not preserved by Pass P
175void PassManagerAnalysisHelper::removeNotPreservedAnalysis(Pass *P) {
176
177 // TODO
178}
179
Devang Patel6e5a1132006-11-07 21:31:57 +0000180/// BasicBlockPassManager implementation
181
Devang Pateld65e9e92006-11-08 01:31:28 +0000182/// Add pass P into PassVector and return true. If this pass is not
183/// manageable by this manager then return false.
Devang Patel6e5a1132006-11-07 21:31:57 +0000184bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000185BasicBlockPassManager_New::addPass(Pass *P) {
Devang Patel6e5a1132006-11-07 21:31:57 +0000186
187 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
188 if (!BP)
189 return false;
190
Devang Patel3c8eb622006-11-07 22:56:50 +0000191 // If this pass does not preserve anlysis that is used by other passes
192 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000193 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000194 return false;
195
196 // Take a note of analysis required by this pass.
197 noteDownRequiredAnalysis(P);
Devang Patel6e5a1132006-11-07 21:31:57 +0000198
199 // Add pass
200 PassVector.push_back(BP);
201 return true;
202}
203
204/// Execute all of the passes scheduled for execution by invoking
205/// runOnBasicBlock method. Keep track of whether any of the passes modifies
206/// the function, and if so, return true.
207bool
208BasicBlockPassManager_New::runOnFunction(Function &F) {
209
210 bool Changed = false;
211 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
212 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
213 e = PassVector.end(); itr != e; ++itr) {
214 Pass *P = *itr;
215 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
216 Changed |= BP->runOnBasicBlock(*I);
217 }
218 return Changed;
219}
220
Devang Patel0c2012f2006-11-07 21:49:50 +0000221// FunctionPassManager_New implementation
Devang Patel4e12f862006-11-08 10:44:40 +0000222/// Create new Function pass manager
223FunctionPassManager_New::FunctionPassManager_New() {
224 FPM = new FunctionPassManagerImpl_New();
225}
226
227/// add - Add a pass to the queue of passes to run. This passes
228/// ownership of the Pass to the PassManager. When the
229/// PassManager_X is destroyed, the pass will be destroyed as well, so
230/// there is no need to delete the pass. (TODO delete passes.)
231/// This implies that all passes MUST be allocated with 'new'.
232void
233FunctionPassManager_New::add(Pass *P) {
234 FPM->add(P);
235}
236
237/// Execute all of the passes scheduled for execution. Keep
238/// track of whether any of the passes modifies the function, and if
239/// so, return true.
240bool
241FunctionPassManager_New::runOnModule(Module &M) {
242 return FPM->runOnModule(M);
243}
244
245// FunctionPassManagerImpl_New implementation
Devang Patel0c2012f2006-11-07 21:49:50 +0000246
Devang Patel0c2012f2006-11-07 21:49:50 +0000247// FunctionPassManager
248
249/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
250/// either use it into active basic block pass manager or create new basic
251/// block pass manager to handle pass P.
252bool
Devang Patel4e12f862006-11-08 10:44:40 +0000253FunctionPassManagerImpl_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000254
255 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
256 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
257
258 if (!activeBBPassManager
259 || !activeBBPassManager->addPass(BP)) {
260
261 activeBBPassManager = new BasicBlockPassManager_New();
262
263 PassVector.push_back(activeBBPassManager);
Devang Pateld65e9e92006-11-08 01:31:28 +0000264 if (!activeBBPassManager->addPass(BP))
265 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000266 }
267 return true;
268 }
269
270 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
271 if (!FP)
272 return false;
273
Devang Patel3c8eb622006-11-07 22:56:50 +0000274 // If this pass does not preserve anlysis that is used by other passes
275 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000276 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000277 return false;
278
279 // Take a note of analysis required by this pass.
280 noteDownRequiredAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000281
282 PassVector.push_back(FP);
283 activeBBPassManager = NULL;
284 return true;
285}
286
287/// Execute all of the passes scheduled for execution by invoking
288/// runOnFunction method. Keep track of whether any of the passes modifies
289/// the function, and if so, return true.
290bool
Devang Patel4e12f862006-11-08 10:44:40 +0000291FunctionPassManagerImpl_New::runOnModule(Module &M) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000292
293 bool Changed = false;
294 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
295 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
296 e = PassVector.end(); itr != e; ++itr) {
297 Pass *P = *itr;
298 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
299 Changed |= FP->runOnFunction(*I);
300 }
301 return Changed;
302}
303
304
Devang Patel05e1a972006-11-07 22:03:15 +0000305// ModulePassManager implementation
306
307/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Patel4e12f862006-11-08 10:44:40 +0000308/// then use FunctionPassManagerImpl_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000309/// is not manageable by this manager.
310bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000311ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000312
313 // If P is FunctionPass then use function pass maanager.
314 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
315
316 activeFunctionPassManager = NULL;
317
318 if (!activeFunctionPassManager
319 || !activeFunctionPassManager->addPass(P)) {
320
Devang Patel4e12f862006-11-08 10:44:40 +0000321 activeFunctionPassManager = new FunctionPassManagerImpl_New();
Devang Patel05e1a972006-11-07 22:03:15 +0000322
323 PassVector.push_back(activeFunctionPassManager);
Devang Pateld65e9e92006-11-08 01:31:28 +0000324 if (!activeFunctionPassManager->addPass(FP))
325 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000326 }
327 return true;
328 }
329
330 ModulePass *MP = dynamic_cast<ModulePass *>(P);
331 if (!MP)
332 return false;
333
Devang Patel3c8eb622006-11-07 22:56:50 +0000334 // If this pass does not preserve anlysis that is used by other passes
335 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000336 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000337 return false;
338
339 // Take a note of analysis required by this pass.
340 noteDownRequiredAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000341
342 PassVector.push_back(MP);
343 activeFunctionPassManager = NULL;
344 return true;
345}
346
347
348/// Execute all of the passes scheduled for execution by invoking
349/// runOnModule method. Keep track of whether any of the passes modifies
350/// the module, and if so, return true.
351bool
352ModulePassManager_New::runOnModule(Module &M) {
353 bool Changed = false;
354 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
355 e = PassVector.end(); itr != e; ++itr) {
356 Pass *P = *itr;
357 ModulePass *MP = dynamic_cast<ModulePass*>(P);
358 Changed |= MP->runOnModule(M);
359 }
360 return Changed;
361}
362
Devang Patelc290c8a2006-11-07 22:23:34 +0000363/// Schedule all passes from the queue by adding them in their
364/// respective manager's queue.
365void
Devang Patel376fefa2006-11-08 10:29:57 +0000366PassManagerImpl_New::schedulePasses() {
Devang Patelc290c8a2006-11-07 22:23:34 +0000367 /* TODO */
368}
369
370/// Add pass P to the queue of passes to run.
371void
Devang Patel376fefa2006-11-08 10:29:57 +0000372PassManagerImpl_New::add(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000373 /* TODO */
374}
375
376// PassManager_New implementation
377/// Add P into active pass manager or use new module pass manager to
378/// manage it.
379bool
Devang Patel376fefa2006-11-08 10:29:57 +0000380PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000381
382 if (!activeManager) {
383 activeManager = new ModulePassManager_New();
384 PassManagers.push_back(activeManager);
385 }
386
387 return activeManager->addPass(P);
388}
389
390/// run - Execute all of the passes scheduled for execution. Keep track of
391/// whether any of the passes modifies the module, and if so, return true.
392bool
Devang Patel376fefa2006-11-08 10:29:57 +0000393PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000394
395 schedulePasses();
396 bool Changed = false;
397 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
398 e = PassManagers.end(); itr != e; ++itr) {
399 ModulePassManager_New *pm = *itr;
400 Changed |= pm->runOnModule(M);
401 }
402 return Changed;
403}
Devang Patel376fefa2006-11-08 10:29:57 +0000404
405/// Create new pass manager
406PassManager_New::PassManager_New() {
407 PM = new PassManagerImpl_New();
408}
409
410/// add - Add a pass to the queue of passes to run. This passes ownership of
411/// the Pass to the PassManager. When the PassManager is destroyed, the pass
412/// will be destroyed as well, so there is no need to delete the pass. This
413/// implies that all passes MUST be allocated with 'new'.
414void
415PassManager_New::add(Pass *P) {
416 PM->add(P);
417}
418
419/// run - Execute all of the passes scheduled for execution. Keep track of
420/// whether any of the passes modifies the module, and if so, return true.
421bool
422PassManager_New::run(Module &M) {
423 return PM->run(M);
424}
425