blob: 5d5b67baa53938f9a13a90a65423eb4547a6f84f [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
43/// FunctionPassManager_New manages FunctionPasses and BasicBlockPassManagers.
44/// 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.
47class FunctionPassManager_New : public Pass,
48 public PassManagerAnalysisHelper {
49public:
50 FunctionPassManager_New(ModuleProvider *P) { /* TODO */ }
51 FunctionPassManager_New() {
52 activeBBPassManager = NULL;
53 }
54 ~FunctionPassManager_New() { /* TODO */ };
55
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
100 FunctionPassManager_New *activeFunctionPassManager;
101};
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
222
Devang Patel0c2012f2006-11-07 21:49:50 +0000223// FunctionPassManager
224
225/// Add pass P into the pass manager queue. If P is a BasicBlockPass then
226/// either use it into active basic block pass manager or create new basic
227/// block pass manager to handle pass P.
228bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000229FunctionPassManager_New::addPass(Pass *P) {
Devang Patel0c2012f2006-11-07 21:49:50 +0000230
231 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
232 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
233
234 if (!activeBBPassManager
235 || !activeBBPassManager->addPass(BP)) {
236
237 activeBBPassManager = new BasicBlockPassManager_New();
238
239 PassVector.push_back(activeBBPassManager);
Devang Pateld65e9e92006-11-08 01:31:28 +0000240 if (!activeBBPassManager->addPass(BP))
241 assert(0 && "Unable to add Pass");
Devang Patel0c2012f2006-11-07 21:49:50 +0000242 }
243 return true;
244 }
245
246 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
247 if (!FP)
248 return false;
249
Devang Patel3c8eb622006-11-07 22:56:50 +0000250 // If this pass does not preserve anlysis that is used by other passes
251 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000252 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000253 return false;
254
255 // Take a note of analysis required by this pass.
256 noteDownRequiredAnalysis(P);
Devang Patel0c2012f2006-11-07 21:49:50 +0000257
258 PassVector.push_back(FP);
259 activeBBPassManager = NULL;
260 return true;
261}
262
263/// Execute all of the passes scheduled for execution by invoking
264/// runOnFunction method. Keep track of whether any of the passes modifies
265/// the function, and if so, return true.
266bool
267FunctionPassManager_New::runOnModule(Module &M) {
268
269 bool Changed = false;
270 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
271 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
272 e = PassVector.end(); itr != e; ++itr) {
273 Pass *P = *itr;
274 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
275 Changed |= FP->runOnFunction(*I);
276 }
277 return Changed;
278}
279
280
Devang Patel05e1a972006-11-07 22:03:15 +0000281// ModulePassManager implementation
282
283/// Add P into pass vector if it is manageble. If P is a FunctionPass
Devang Pateld65e9e92006-11-08 01:31:28 +0000284/// then use FunctionPassManager_New to manage it. Return false if P
Devang Patel05e1a972006-11-07 22:03:15 +0000285/// is not manageable by this manager.
286bool
Devang Pateld65e9e92006-11-08 01:31:28 +0000287ModulePassManager_New::addPass(Pass *P) {
Devang Patel05e1a972006-11-07 22:03:15 +0000288
289 // If P is FunctionPass then use function pass maanager.
290 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
291
292 activeFunctionPassManager = NULL;
293
294 if (!activeFunctionPassManager
295 || !activeFunctionPassManager->addPass(P)) {
296
297 activeFunctionPassManager = new FunctionPassManager_New();
298
299 PassVector.push_back(activeFunctionPassManager);
Devang Pateld65e9e92006-11-08 01:31:28 +0000300 if (!activeFunctionPassManager->addPass(FP))
301 assert(0 && "Unable to add pass");
Devang Patel05e1a972006-11-07 22:03:15 +0000302 }
303 return true;
304 }
305
306 ModulePass *MP = dynamic_cast<ModulePass *>(P);
307 if (!MP)
308 return false;
309
Devang Patel3c8eb622006-11-07 22:56:50 +0000310 // If this pass does not preserve anlysis that is used by other passes
311 // managed by this manager than it is not a suiable pass for this manager.
Devang Pateld65e9e92006-11-08 01:31:28 +0000312 if (!manageablePass(P))
Devang Patel3c8eb622006-11-07 22:56:50 +0000313 return false;
314
315 // Take a note of analysis required by this pass.
316 noteDownRequiredAnalysis(P);
Devang Patel05e1a972006-11-07 22:03:15 +0000317
318 PassVector.push_back(MP);
319 activeFunctionPassManager = NULL;
320 return true;
321}
322
323
324/// Execute all of the passes scheduled for execution by invoking
325/// runOnModule method. Keep track of whether any of the passes modifies
326/// the module, and if so, return true.
327bool
328ModulePassManager_New::runOnModule(Module &M) {
329 bool Changed = false;
330 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
331 e = PassVector.end(); itr != e; ++itr) {
332 Pass *P = *itr;
333 ModulePass *MP = dynamic_cast<ModulePass*>(P);
334 Changed |= MP->runOnModule(M);
335 }
336 return Changed;
337}
338
Devang Patelc290c8a2006-11-07 22:23:34 +0000339/// Schedule all passes from the queue by adding them in their
340/// respective manager's queue.
341void
Devang Patel376fefa2006-11-08 10:29:57 +0000342PassManagerImpl_New::schedulePasses() {
Devang Patelc290c8a2006-11-07 22:23:34 +0000343 /* TODO */
344}
345
346/// Add pass P to the queue of passes to run.
347void
Devang Patel376fefa2006-11-08 10:29:57 +0000348PassManagerImpl_New::add(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000349 /* TODO */
350}
351
352// PassManager_New implementation
353/// Add P into active pass manager or use new module pass manager to
354/// manage it.
355bool
Devang Patel376fefa2006-11-08 10:29:57 +0000356PassManagerImpl_New::addPass(Pass *P) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000357
358 if (!activeManager) {
359 activeManager = new ModulePassManager_New();
360 PassManagers.push_back(activeManager);
361 }
362
363 return activeManager->addPass(P);
364}
365
366/// run - Execute all of the passes scheduled for execution. Keep track of
367/// whether any of the passes modifies the module, and if so, return true.
368bool
Devang Patel376fefa2006-11-08 10:29:57 +0000369PassManagerImpl_New::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +0000370
371 schedulePasses();
372 bool Changed = false;
373 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
374 e = PassManagers.end(); itr != e; ++itr) {
375 ModulePassManager_New *pm = *itr;
376 Changed |= pm->runOnModule(M);
377 }
378 return Changed;
379}
Devang Patel376fefa2006-11-08 10:29:57 +0000380
381/// Create new pass manager
382PassManager_New::PassManager_New() {
383 PM = new PassManagerImpl_New();
384}
385
386/// add - Add a pass to the queue of passes to run. This passes ownership of
387/// the Pass to the PassManager. When the PassManager is destroyed, the pass
388/// will be destroyed as well, so there is no need to delete the pass. This
389/// implies that all passes MUST be allocated with 'new'.
390void
391PassManager_New::add(Pass *P) {
392 PM->add(P);
393}
394
395/// run - Execute all of the passes scheduled for execution. Keep track of
396/// whether any of the passes modifies the module, and if so, return true.
397bool
398PassManager_New::run(Module &M) {
399 return PM->run(M);
400}
401