Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1 | //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM Pass Manager infrastructure. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | |
| 15 | #include "llvm/PassManager.h" |
| 16 | #include "llvm/Function.h" |
| 17 | #include "llvm/Module.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 21 | // PassManagerAnalysisHelper implementation |
| 22 | |
| 23 | /// Return TRUE IFF pass P's required analysis set does not required new |
| 24 | /// manager. |
| 25 | bool PassManagerAnalysisHelper::manageablePass(Pass *P) { |
| 26 | |
| 27 | AnalysisUsage AnUsage; |
| 28 | P->getAnalysisUsage(AnUsage); |
| 29 | |
| 30 | // If this pass is not preserving information that is required by the other passes |
| 31 | // managed by this manager then use new manager |
| 32 | // TODO |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | /// Return TRUE iff AnalysisID AID is currently available. |
| 37 | bool PassManagerAnalysisHelper::analysisCurrentlyAvailable(AnalysisID AID) { |
| 38 | |
| 39 | // TODO |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | /// Augment RequiredSet by adding analysis required by pass P. |
| 44 | void PassManagerAnalysisHelper::noteDownRequiredAnalysis(Pass *P) { |
| 45 | |
| 46 | // TODO |
| 47 | } |
| 48 | |
| 49 | /// Remove AnalysisID from the RequiredSet |
| 50 | void PassManagerAnalysisHelper::removeAnalysis(AnalysisID AID) { |
| 51 | |
| 52 | // TODO |
| 53 | } |
| 54 | |
| 55 | /// Remove Analyss not preserved by Pass P |
| 56 | void PassManagerAnalysisHelper::removeNotPreservedAnalysis(Pass *P) { |
| 57 | |
| 58 | // TODO |
| 59 | } |
| 60 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 61 | /// BasicBlockPassManager implementation |
| 62 | |
| 63 | /// Add pass P into PassVector and return TRUE. If this pass is not |
| 64 | /// manageable by this manager then return FALSE. |
| 65 | bool |
| 66 | BasicBlockPassManager_New::addPass (Pass *P) { |
| 67 | |
| 68 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 69 | if (!BP) |
| 70 | return false; |
| 71 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame^] | 72 | // If this pass does not preserve anlysis that is used by other passes |
| 73 | // managed by this manager than it is not a suiable pass for this manager. |
| 74 | if (!manageablePass (P)) |
| 75 | return false; |
| 76 | |
| 77 | // Take a note of analysis required by this pass. |
| 78 | noteDownRequiredAnalysis(P); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 79 | |
| 80 | // Add pass |
| 81 | PassVector.push_back(BP); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | /// Execute all of the passes scheduled for execution by invoking |
| 86 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 87 | /// the function, and if so, return true. |
| 88 | bool |
| 89 | BasicBlockPassManager_New::runOnFunction(Function &F) { |
| 90 | |
| 91 | bool Changed = false; |
| 92 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 93 | for (std::vector<Pass *>::iterator itr = PassVector.begin(), |
| 94 | e = PassVector.end(); itr != e; ++itr) { |
| 95 | Pass *P = *itr; |
| 96 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 97 | Changed |= BP->runOnBasicBlock(*I); |
| 98 | } |
| 99 | return Changed; |
| 100 | } |
| 101 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 102 | // FunctionPassManager_New implementation |
| 103 | |
| 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | // FunctionPassManager |
| 106 | |
| 107 | /// Add pass P into the pass manager queue. If P is a BasicBlockPass then |
| 108 | /// either use it into active basic block pass manager or create new basic |
| 109 | /// block pass manager to handle pass P. |
| 110 | bool |
| 111 | FunctionPassManager_New::addPass (Pass *P) { |
| 112 | |
| 113 | // If P is a BasicBlockPass then use BasicBlockPassManager_New. |
| 114 | if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { |
| 115 | |
| 116 | if (!activeBBPassManager |
| 117 | || !activeBBPassManager->addPass(BP)) { |
| 118 | |
| 119 | activeBBPassManager = new BasicBlockPassManager_New(); |
| 120 | |
| 121 | PassVector.push_back(activeBBPassManager); |
| 122 | assert (!activeBBPassManager->addPass(BP) && |
| 123 | "Unable to add Pass"); |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | FunctionPass *FP = dynamic_cast<FunctionPass *>(P); |
| 129 | if (!FP) |
| 130 | return false; |
| 131 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame^] | 132 | // If this pass does not preserve anlysis that is used by other passes |
| 133 | // managed by this manager than it is not a suiable pass for this manager. |
| 134 | if (!manageablePass (P)) |
| 135 | return false; |
| 136 | |
| 137 | // Take a note of analysis required by this pass. |
| 138 | noteDownRequiredAnalysis(P); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 139 | |
| 140 | PassVector.push_back(FP); |
| 141 | activeBBPassManager = NULL; |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | /// Execute all of the passes scheduled for execution by invoking |
| 146 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 147 | /// the function, and if so, return true. |
| 148 | bool |
| 149 | FunctionPassManager_New::runOnModule(Module &M) { |
| 150 | |
| 151 | bool Changed = false; |
| 152 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 153 | for (std::vector<Pass *>::iterator itr = PassVector.begin(), |
| 154 | e = PassVector.end(); itr != e; ++itr) { |
| 155 | Pass *P = *itr; |
| 156 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 157 | Changed |= FP->runOnFunction(*I); |
| 158 | } |
| 159 | return Changed; |
| 160 | } |
| 161 | |
| 162 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 163 | // ModulePassManager implementation |
| 164 | |
| 165 | /// Add P into pass vector if it is manageble. If P is a FunctionPass |
| 166 | /// then use FunctionPassManager_New to manage it. Return FALSE if P |
| 167 | /// is not manageable by this manager. |
| 168 | bool |
| 169 | ModulePassManager_New::addPass (Pass *P) { |
| 170 | |
| 171 | // If P is FunctionPass then use function pass maanager. |
| 172 | if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { |
| 173 | |
| 174 | activeFunctionPassManager = NULL; |
| 175 | |
| 176 | if (!activeFunctionPassManager |
| 177 | || !activeFunctionPassManager->addPass(P)) { |
| 178 | |
| 179 | activeFunctionPassManager = new FunctionPassManager_New(); |
| 180 | |
| 181 | PassVector.push_back(activeFunctionPassManager); |
| 182 | assert (!activeFunctionPassManager->addPass(FP) && |
| 183 | "Unable to add Pass"); |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | ModulePass *MP = dynamic_cast<ModulePass *>(P); |
| 189 | if (!MP) |
| 190 | return false; |
| 191 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame^] | 192 | // If this pass does not preserve anlysis that is used by other passes |
| 193 | // managed by this manager than it is not a suiable pass for this manager. |
| 194 | if (!manageablePass (P)) |
| 195 | return false; |
| 196 | |
| 197 | // Take a note of analysis required by this pass. |
| 198 | noteDownRequiredAnalysis(P); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 199 | |
| 200 | PassVector.push_back(MP); |
| 201 | activeFunctionPassManager = NULL; |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /// Execute all of the passes scheduled for execution by invoking |
| 207 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 208 | /// the module, and if so, return true. |
| 209 | bool |
| 210 | ModulePassManager_New::runOnModule(Module &M) { |
| 211 | bool Changed = false; |
| 212 | for (std::vector<Pass *>::iterator itr = PassVector.begin(), |
| 213 | e = PassVector.end(); itr != e; ++itr) { |
| 214 | Pass *P = *itr; |
| 215 | ModulePass *MP = dynamic_cast<ModulePass*>(P); |
| 216 | Changed |= MP->runOnModule(M); |
| 217 | } |
| 218 | return Changed; |
| 219 | } |
| 220 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 221 | /// Schedule all passes from the queue by adding them in their |
| 222 | /// respective manager's queue. |
| 223 | void |
| 224 | PassManager_New::schedulePasses() { |
| 225 | /* TODO */ |
| 226 | } |
| 227 | |
| 228 | /// Add pass P to the queue of passes to run. |
| 229 | void |
| 230 | PassManager_New::add(Pass *P) { |
| 231 | /* TODO */ |
| 232 | } |
| 233 | |
| 234 | // PassManager_New implementation |
| 235 | /// Add P into active pass manager or use new module pass manager to |
| 236 | /// manage it. |
| 237 | bool |
| 238 | PassManager_New::addPass (Pass *P) { |
| 239 | |
| 240 | if (!activeManager) { |
| 241 | activeManager = new ModulePassManager_New(); |
| 242 | PassManagers.push_back(activeManager); |
| 243 | } |
| 244 | |
| 245 | return activeManager->addPass(P); |
| 246 | } |
| 247 | |
| 248 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 249 | /// whether any of the passes modifies the module, and if so, return true. |
| 250 | bool |
| 251 | PassManager_New::run(Module &M) { |
| 252 | |
| 253 | schedulePasses(); |
| 254 | bool Changed = false; |
| 255 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
| 256 | e = PassManagers.end(); itr != e; ++itr) { |
| 257 | ModulePassManager_New *pm = *itr; |
| 258 | Changed |= pm->runOnModule(M); |
| 259 | } |
| 260 | return Changed; |
| 261 | } |