1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/PassManager.h"
16 #include "llvm/Module.h"
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.
25 class BasicBlockPassManager_New : public CommonPassManagerImpl {
28 BasicBlockPassManager_New() { }
30 /// Add a pass into a passmanager queue.
31 bool addPass(Pass *p);
33 /// Execute all of the passes scheduled for execution. Keep track of
34 /// whether any of the passes modifies the function, and if so, return true.
35 bool runOnFunction(Function &F);
38 // Collection of pass that are managed by this manager
39 std::vector<Pass *> PassVector;
42 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
43 /// It batches all function passes and basic block pass managers together and
44 /// sequence them to process one function at a time before processing next
46 class FunctionPassManagerImpl_New : public CommonPassManagerImpl {
48 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
49 FunctionPassManagerImpl_New() {
50 activeBBPassManager = NULL;
52 ~FunctionPassManagerImpl_New() { /* TODO */ };
54 /// add - Add a pass to the queue of passes to run. This passes
55 /// ownership of the Pass to the PassManager. When the
56 /// PassManager_X is destroyed, the pass will be destroyed as well, so
57 /// there is no need to delete the pass. (TODO delete passes.)
58 /// This implies that all passes MUST be allocated with 'new'.
59 void add(Pass *P) { /* TODO*/ }
61 /// Add pass into the pass manager queue.
62 bool addPass(Pass *P);
64 /// Execute all of the passes scheduled for execution. Keep
65 /// track of whether any of the passes modifies the function, and if
67 bool runOnModule(Module &M);
70 // Collection of pass that are manged by this manager
71 std::vector<Pass *> PassVector;
73 // Active Pass Managers
74 BasicBlockPassManager_New *activeBBPassManager;
77 /// ModulePassManager_New manages ModulePasses and function pass managers.
78 /// It batches all Module passes passes and function pass managers together and
79 /// sequence them to process one module.
80 class ModulePassManager_New : public CommonPassManagerImpl {
83 ModulePassManager_New() { activeFunctionPassManager = NULL; }
85 /// Add a pass into a passmanager queue.
86 bool addPass(Pass *p);
88 /// run - Execute all of the passes scheduled for execution. Keep track of
89 /// whether any of the passes modifies the module, and if so, return true.
90 bool runOnModule(Module &M);
93 // Collection of pass that are managed by this manager
94 std::vector<Pass *> PassVector;
96 // Active Pass Manager
97 FunctionPassManagerImpl_New *activeFunctionPassManager;
100 /// PassManager_New manages ModulePassManagers
101 class PassManagerImpl_New : public CommonPassManagerImpl {
105 /// add - Add a pass to the queue of passes to run. This passes ownership of
106 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
107 /// will be destroyed as well, so there is no need to delete the pass. This
108 /// implies that all passes MUST be allocated with 'new'.
111 /// run - Execute all of the passes scheduled for execution. Keep track of
112 /// whether any of the passes modifies the module, and if so, return true.
117 /// Add a pass into a passmanager queue. This is used by schedulePasses
118 bool addPass(Pass *p);
120 /// Schedule all passes collected in pass queue using add(). Add all the
121 /// schedule passes into various manager's queue using addPass().
122 void schedulePasses();
124 // Collection of pass managers
125 std::vector<ModulePassManager_New *> PassManagers;
127 // Collection of pass that are not yet scheduled
128 std::vector<Pass *> PassVector;
130 // Active Pass Manager
131 ModulePassManager_New *activeManager;
134 } // End of llvm namespace
136 // CommonPassManagerImpl implementation
138 /// Return true IFF pass P's required analysis set does not required new
140 bool CommonPassManagerImpl::manageablePass(Pass *P) {
142 AnalysisUsage AnUsage;
143 P->getAnalysisUsage(AnUsage);
145 // If this pass is not preserving information that is required by the other
146 // passes managed by this manager then use new manager
147 if (!AnUsage.getPreservesAll()) {
148 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
149 for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(),
150 E = RequiredAnalysis.end(); I != E; ++I) {
151 if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) ==
153 // This analysis is not preserved. Need new manager.
160 /// Return true IFF AnalysisID AID is currently available.
161 bool CommonPassManagerImpl::analysisCurrentlyAvailable(AnalysisID AID) {
167 /// Augment RequiredSet by adding analysis required by pass P.
168 void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) {
169 AnalysisUsage AnUsage;
170 P->getAnalysisUsage(AnUsage);
171 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
173 // FIXME: What about duplicates ?
174 RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(), RequiredSet.end());
177 /// Remove AnalysisID from the RequiredSet
178 void CommonPassManagerImpl::removeAnalysis(AnalysisID AID) {
183 /// Remove Analyss not preserved by Pass P
184 void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) {
189 /// BasicBlockPassManager implementation
191 /// Add pass P into PassVector and return true. If this pass is not
192 /// manageable by this manager then return false.
194 BasicBlockPassManager_New::addPass(Pass *P) {
196 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
200 // If this pass does not preserve anlysis that is used by other passes
201 // managed by this manager than it is not a suiable pass for this manager.
202 if (!manageablePass(P))
205 // Take a note of analysis required by this pass.
206 noteDownRequiredAnalysis(P);
209 PassVector.push_back(BP);
213 /// Execute all of the passes scheduled for execution by invoking
214 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
215 /// the function, and if so, return true.
217 BasicBlockPassManager_New::runOnFunction(Function &F) {
219 bool Changed = false;
220 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
221 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
222 e = PassVector.end(); itr != e; ++itr) {
224 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
225 Changed |= BP->runOnBasicBlock(*I);
230 // FunctionPassManager_New implementation
231 /// Create new Function pass manager
232 FunctionPassManager_New::FunctionPassManager_New() {
233 FPM = new FunctionPassManagerImpl_New();
236 /// add - Add a pass to the queue of passes to run. This passes
237 /// ownership of the Pass to the PassManager. When the
238 /// PassManager_X is destroyed, the pass will be destroyed as well, so
239 /// there is no need to delete the pass. (TODO delete passes.)
240 /// This implies that all passes MUST be allocated with 'new'.
242 FunctionPassManager_New::add(Pass *P) {
246 /// Execute all of the passes scheduled for execution. Keep
247 /// track of whether any of the passes modifies the function, and if
250 FunctionPassManager_New::runOnModule(Module &M) {
251 return FPM->runOnModule(M);
254 // FunctionPassManagerImpl_New implementation
256 // FunctionPassManager
258 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
259 /// either use it into active basic block pass manager or create new basic
260 /// block pass manager to handle pass P.
262 FunctionPassManagerImpl_New::addPass(Pass *P) {
264 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
265 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
267 if (!activeBBPassManager
268 || !activeBBPassManager->addPass(BP)) {
270 activeBBPassManager = new BasicBlockPassManager_New();
272 PassVector.push_back(activeBBPassManager);
273 if (!activeBBPassManager->addPass(BP))
274 assert(0 && "Unable to add Pass");
279 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
283 // If this pass does not preserve anlysis that is used by other passes
284 // managed by this manager than it is not a suiable pass for this manager.
285 if (!manageablePass(P))
288 // Take a note of analysis required by this pass.
289 noteDownRequiredAnalysis(P);
291 PassVector.push_back(FP);
292 activeBBPassManager = NULL;
296 /// Execute all of the passes scheduled for execution by invoking
297 /// runOnFunction method. Keep track of whether any of the passes modifies
298 /// the function, and if so, return true.
300 FunctionPassManagerImpl_New::runOnModule(Module &M) {
302 bool Changed = false;
303 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
304 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
305 e = PassVector.end(); itr != e; ++itr) {
307 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
308 Changed |= FP->runOnFunction(*I);
314 // ModulePassManager implementation
316 /// Add P into pass vector if it is manageble. If P is a FunctionPass
317 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
318 /// is not manageable by this manager.
320 ModulePassManager_New::addPass(Pass *P) {
322 // If P is FunctionPass then use function pass maanager.
323 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
325 activeFunctionPassManager = NULL;
327 if (!activeFunctionPassManager
328 || !activeFunctionPassManager->addPass(P)) {
330 activeFunctionPassManager = new FunctionPassManagerImpl_New();
332 PassVector.push_back(activeFunctionPassManager);
333 if (!activeFunctionPassManager->addPass(FP))
334 assert(0 && "Unable to add pass");
339 ModulePass *MP = dynamic_cast<ModulePass *>(P);
343 // 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.
345 if (!manageablePass(P))
348 // Take a note of analysis required by this pass.
349 noteDownRequiredAnalysis(P);
351 PassVector.push_back(MP);
352 activeFunctionPassManager = NULL;
357 /// Execute all of the passes scheduled for execution by invoking
358 /// runOnModule method. Keep track of whether any of the passes modifies
359 /// the module, and if so, return true.
361 ModulePassManager_New::runOnModule(Module &M) {
362 bool Changed = false;
363 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
364 e = PassVector.end(); itr != e; ++itr) {
366 ModulePass *MP = dynamic_cast<ModulePass*>(P);
367 Changed |= MP->runOnModule(M);
372 /// Schedule all passes from the queue by adding them in their
373 /// respective manager's queue.
375 PassManagerImpl_New::schedulePasses() {
379 /// Add pass P to the queue of passes to run.
381 PassManagerImpl_New::add(Pass *P) {
385 // PassManager_New implementation
386 /// Add P into active pass manager or use new module pass manager to
389 PassManagerImpl_New::addPass(Pass *P) {
391 if (!activeManager || !activeManager->addPass(P)) {
392 activeManager = new ModulePassManager_New();
393 PassManagers.push_back(activeManager);
396 return activeManager->addPass(P);
399 /// run - Execute all of the passes scheduled for execution. Keep track of
400 /// whether any of the passes modifies the module, and if so, return true.
402 PassManagerImpl_New::run(Module &M) {
405 bool Changed = false;
406 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
407 e = PassManagers.end(); itr != e; ++itr) {
408 ModulePassManager_New *pm = *itr;
409 Changed |= pm->runOnModule(M);
414 /// Create new pass manager
415 PassManager_New::PassManager_New() {
416 PM = new PassManagerImpl_New();
419 /// add - Add a pass to the queue of passes to run. This passes ownership of
420 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
421 /// will be destroyed as well, so there is no need to delete the pass. This
422 /// implies that all passes MUST be allocated with 'new'.
424 PassManager_New::add(Pass *P) {
428 /// run - Execute all of the passes scheduled for execution. Keep track of
429 /// whether any of the passes modifies the module, and if so, return true.
431 PassManager_New::run(Module &M) {