1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group 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/Function.h"
17 #include "llvm/Module.h"
21 /// BasicBlockPassManager implementation
23 /// Add pass P into PassVector and return TRUE. If this pass is not
24 /// manageable by this manager then return FALSE.
26 BasicBlockPassManager_New::addPass (Pass *P) {
28 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
32 // TODO: Check if it suitable to manage P using this BasicBlockPassManager
33 // or we need another instance of BasicBlockPassManager
36 PassVector.push_back(BP);
40 /// Execute all of the passes scheduled for execution by invoking
41 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
42 /// the function, and if so, return true.
44 BasicBlockPassManager_New::runOnFunction(Function &F) {
47 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
48 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
49 e = PassVector.end(); itr != e; ++itr) {
51 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
52 Changed |= BP->runOnBasicBlock(*I);
57 // FunctionPassManager_New implementation
59 ///////////////////////////////////////////////////////////////////////////////
60 // FunctionPassManager
62 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
63 /// either use it into active basic block pass manager or create new basic
64 /// block pass manager to handle pass P.
66 FunctionPassManager_New::addPass (Pass *P) {
68 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
69 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
71 if (!activeBBPassManager
72 || !activeBBPassManager->addPass(BP)) {
74 activeBBPassManager = new BasicBlockPassManager_New();
76 PassVector.push_back(activeBBPassManager);
77 assert (!activeBBPassManager->addPass(BP) &&
78 "Unable to add Pass");
83 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
87 // TODO: Check if it suitable to manage P using this FunctionPassManager
88 // or we need another instance of FunctionPassManager
90 PassVector.push_back(FP);
91 activeBBPassManager = NULL;
95 /// Execute all of the passes scheduled for execution by invoking
96 /// runOnFunction method. Keep track of whether any of the passes modifies
97 /// the function, and if so, return true.
99 FunctionPassManager_New::runOnModule(Module &M) {
101 bool Changed = false;
102 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
103 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
104 e = PassVector.end(); itr != e; ++itr) {
106 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
107 Changed |= FP->runOnFunction(*I);
113 // ModulePassManager implementation
115 /// Add P into pass vector if it is manageble. If P is a FunctionPass
116 /// then use FunctionPassManager_New to manage it. Return FALSE if P
117 /// is not manageable by this manager.
119 ModulePassManager_New::addPass (Pass *P) {
121 // If P is FunctionPass then use function pass maanager.
122 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
124 activeFunctionPassManager = NULL;
126 if (!activeFunctionPassManager
127 || !activeFunctionPassManager->addPass(P)) {
129 activeFunctionPassManager = new FunctionPassManager_New();
131 PassVector.push_back(activeFunctionPassManager);
132 assert (!activeFunctionPassManager->addPass(FP) &&
133 "Unable to add Pass");
138 ModulePass *MP = dynamic_cast<ModulePass *>(P);
142 // TODO: Check if it suitable to manage P using this ModulePassManager
143 // or we need another instance of ModulePassManager
145 PassVector.push_back(MP);
146 activeFunctionPassManager = NULL;
151 /// Execute all of the passes scheduled for execution by invoking
152 /// runOnModule method. Keep track of whether any of the passes modifies
153 /// the module, and if so, return true.
155 ModulePassManager_New::runOnModule(Module &M) {
156 bool Changed = false;
157 for (std::vector<Pass *>::iterator itr = PassVector.begin(),
158 e = PassVector.end(); itr != e; ++itr) {
160 ModulePass *MP = dynamic_cast<ModulePass*>(P);
161 Changed |= MP->runOnModule(M);
166 /// Schedule all passes from the queue by adding them in their
167 /// respective manager's queue.
169 PassManager_New::schedulePasses() {
173 /// Add pass P to the queue of passes to run.
175 PassManager_New::add(Pass *P) {
179 // PassManager_New implementation
180 /// Add P into active pass manager or use new module pass manager to
183 PassManager_New::addPass (Pass *P) {
185 if (!activeManager) {
186 activeManager = new ModulePassManager_New();
187 PassManagers.push_back(activeManager);
190 return activeManager->addPass(P);
193 /// run - Execute all of the passes scheduled for execution. Keep track of
194 /// whether any of the passes modifies the module, and if so, return true.
196 PassManager_New::run(Module &M) {
199 bool Changed = false;
200 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
201 e = PassManagers.end(); itr != e; ++itr) {
202 ModulePassManager_New *pm = *itr;
203 Changed |= pm->runOnModule(M);