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 | // |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 5 | // This file was developed by Devang Patel and is distributed under |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 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" |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 17 | #include "llvm/ModuleProvider.h" |
Bill Wendling | dfc9189 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Streams.h" |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 19 | #include <vector> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 20 | #include <map> |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // Overview: |
| 25 | // The Pass Manager Infrastructure manages passes. It's responsibilities are: |
| 26 | // |
| 27 | // o Manage optimization pass execution order |
| 28 | // o Make required Analysis information available before pass P is run |
| 29 | // o Release memory occupied by dead passes |
| 30 | // o If Analysis information is dirtied by a pass then regenerate Analysis |
| 31 | // information before it is consumed by another pass. |
| 32 | // |
| 33 | // Pass Manager Infrastructure uses multipe pass managers. They are PassManager, |
| 34 | // FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class |
| 35 | // hierarcy uses multiple inheritance but pass managers do not derive from |
| 36 | // another pass manager. |
| 37 | // |
| 38 | // PassManager and FunctionPassManager are two top level pass manager that |
| 39 | // represents the external interface of this entire pass manager infrastucture. |
| 40 | // |
| 41 | // Important classes : |
| 42 | // |
| 43 | // [o] class PMTopLevelManager; |
| 44 | // |
| 45 | // Two top level managers, PassManager and FunctionPassManager, derive from |
| 46 | // PMTopLevelManager. PMTopLevelManager manages information used by top level |
| 47 | // managers such as last user info. |
| 48 | // |
| 49 | // [o] class PMDataManager; |
| 50 | // |
| 51 | // PMDataManager manages information, e.g. list of available analysis info, |
| 52 | // used by a pass manager to manage execution order of passes. It also provides |
| 53 | // a place to implement common pass manager APIs. All pass managers derive from |
| 54 | // PMDataManager. |
| 55 | // |
| 56 | // [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager; |
| 57 | // |
| 58 | // BasicBlockPassManager manages BasicBlockPasses. |
| 59 | // |
| 60 | // [o] class FunctionPassManager; |
| 61 | // |
| 62 | // This is a external interface used by JIT to manage FunctionPasses. This |
| 63 | // interface relies on FunctionPassManagerImpl to do all the tasks. |
| 64 | // |
| 65 | // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager, |
| 66 | // public PMTopLevelManager; |
| 67 | // |
| 68 | // FunctionPassManagerImpl is a top level manager. It manages FunctionPasses |
| 69 | // and BasicBlockPassManagers. |
| 70 | // |
| 71 | // [o] class ModulePassManager : public Pass, public PMDataManager; |
| 72 | // |
| 73 | // ModulePassManager manages ModulePasses and FunctionPassManagerImpls. |
| 74 | // |
| 75 | // [o] class PassManager; |
| 76 | // |
| 77 | // This is a external interface used by various tools to manages passes. It |
| 78 | // relies on PassManagerImpl to do all the tasks. |
| 79 | // |
| 80 | // [o] class PassManagerImpl : public Pass, public PMDataManager, |
| 81 | // public PMDTopLevelManager |
| 82 | // |
| 83 | // PassManagerImpl is a top level pass manager responsible for managing |
| 84 | // ModulePassManagers. |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 87 | namespace llvm { |
| 88 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 89 | /// CommonPassManagerImpl helps pass manager analysis required by |
| 90 | /// the managed passes. It provides methods to add/remove analysis |
| 91 | /// available and query if certain analysis is available or not. |
Devang Patel | 42add71 | 2006-11-15 01:11:27 +0000 | [diff] [blame] | 92 | class CommonPassManagerImpl { |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 93 | |
| 94 | public: |
| 95 | |
| 96 | /// Return true IFF pass P's required analysis set does not required new |
| 97 | /// manager. |
| 98 | bool manageablePass(Pass *P); |
| 99 | |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 100 | Pass *getAnalysisPass(AnalysisID AID) const { |
| 101 | |
| 102 | std::map<AnalysisID, Pass*>::const_iterator I = |
| 103 | AvailableAnalysis.find(AID); |
| 104 | |
| 105 | if (I != AvailableAnalysis.end()) |
| 106 | return NULL; |
| 107 | else |
| 108 | return I->second; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 109 | } |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 110 | |
| 111 | /// Augment RequiredAnalysis by adding analysis required by pass P. |
| 112 | void noteDownRequiredAnalysis(Pass *P); |
| 113 | |
| 114 | /// Augment AvailableAnalysis by adding analysis made available by pass P. |
| 115 | void noteDownAvailableAnalysis(Pass *P); |
| 116 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 117 | /// Remove Analysis that is not preserved by the pass |
| 118 | void removeNotPreservedAnalysis(Pass *P); |
| 119 | |
| 120 | /// Remove dead passes |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 121 | void removeDeadPasses(Pass *P); |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 122 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 123 | /// Add pass P into the PassVector. Update RequiredAnalysis and |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 124 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
| 125 | void addPassToManager (Pass *P, bool ProcessAnalysis = true); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 126 | |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 127 | /// Clear analysis vectors RequiredAnalysis and AvailableAnalysis. |
| 128 | /// This is used before running passes managed by the manager. |
| 129 | void clearAnalysis() { |
| 130 | RequiredAnalysis.clear(); |
| 131 | AvailableAnalysis.clear(); |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 132 | LastUser.clear(); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 135 | // All Required analyses should be available to the pass as it runs! Here |
| 136 | // we fill in the AnalysisImpls member of the pass so that it can |
| 137 | // successfully use the getAnalysis() method to retrieve the |
| 138 | // implementations it needs. |
| 139 | // |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 140 | void initializeAnalysisImpl(Pass *P); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 141 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 142 | inline std::vector<Pass *>::iterator passVectorBegin() { |
| 143 | return PassVector.begin(); |
| 144 | } |
| 145 | |
| 146 | inline std::vector<Pass *>::iterator passVectorEnd() { |
| 147 | return PassVector.end(); |
| 148 | } |
| 149 | |
Devang Patel | 4a3fa4f | 2006-11-15 01:48:14 +0000 | [diff] [blame] | 150 | inline void setLastUser(Pass *P, Pass *LU) { |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 151 | LastUser[P] = LU; |
| 152 | // TODO : Check if pass P is available. |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 153 | } |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 154 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 155 | private: |
Devang Patel | dafa4dd | 2006-11-14 00:03:04 +0000 | [diff] [blame] | 156 | // Analysis required by the passes managed by this manager. This information |
| 157 | // used while selecting pass manager during addPass. If a pass does not |
| 158 | // preserve any analysis required by other passes managed by current |
| 159 | // pass manager then new pass manager is used. |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 160 | std::vector<AnalysisID> RequiredAnalysis; |
| 161 | |
Devang Patel | dafa4dd | 2006-11-14 00:03:04 +0000 | [diff] [blame] | 162 | // Set of available Analysis. This information is used while scheduling |
| 163 | // pass. If a pass requires an analysis which is not not available then |
| 164 | // equired analysis pass is scheduled to run before the pass itself is |
| 165 | // scheduled to run. |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 166 | std::map<AnalysisID, Pass*> AvailableAnalysis; |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 167 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 168 | // Map to keep track of last user of the analysis pass. |
| 169 | // LastUser->second is the last user of Lastuser->first. |
| 170 | std::map<Pass *, Pass *> LastUser; |
| 171 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 172 | // Collection of pass that are managed by this manager |
| 173 | std::vector<Pass *> PassVector; |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 176 | /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the |
| 177 | /// pass together and sequence them to process one basic block before |
| 178 | /// processing next basic block. |
Devang Patel | 42add71 | 2006-11-15 01:11:27 +0000 | [diff] [blame] | 179 | class BasicBlockPassManager_New : public CommonPassManagerImpl, |
| 180 | public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 181 | |
| 182 | public: |
| 183 | BasicBlockPassManager_New() { } |
| 184 | |
| 185 | /// Add a pass into a passmanager queue. |
| 186 | bool addPass(Pass *p); |
| 187 | |
| 188 | /// Execute all of the passes scheduled for execution. Keep track of |
| 189 | /// whether any of the passes modifies the function, and if so, return true. |
| 190 | bool runOnFunction(Function &F); |
| 191 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 192 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 193 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 194 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 195 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 198 | /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers. |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 199 | /// It batches all function passes and basic block pass managers together and |
| 200 | /// sequence them to process one function at a time before processing next |
| 201 | /// function. |
Devang Patel | 42add71 | 2006-11-15 01:11:27 +0000 | [diff] [blame] | 202 | class FunctionPassManagerImpl_New : public CommonPassManagerImpl, |
| 203 | public ModulePass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 204 | public: |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 205 | FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ } |
| 206 | FunctionPassManagerImpl_New() { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 207 | activeBBPassManager = NULL; |
| 208 | } |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 209 | ~FunctionPassManagerImpl_New() { /* TODO */ }; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 210 | |
| 211 | /// add - Add a pass to the queue of passes to run. This passes |
| 212 | /// ownership of the Pass to the PassManager. When the |
| 213 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 214 | /// there is no need to delete the pass. (TODO delete passes.) |
| 215 | /// This implies that all passes MUST be allocated with 'new'. |
| 216 | void add(Pass *P) { /* TODO*/ } |
| 217 | |
| 218 | /// Add pass into the pass manager queue. |
| 219 | bool addPass(Pass *P); |
| 220 | |
| 221 | /// Execute all of the passes scheduled for execution. Keep |
| 222 | /// track of whether any of the passes modifies the function, and if |
| 223 | /// so, return true. |
| 224 | bool runOnModule(Module &M); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 225 | bool runOnFunction(Function &F); |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 226 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 227 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 228 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 229 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 230 | /// doInitialization - Run all of the initializers for the function passes. |
| 231 | /// |
| 232 | bool doInitialization(Module &M); |
| 233 | |
| 234 | /// doFinalization - Run all of the initializers for the function passes. |
| 235 | /// |
| 236 | bool doFinalization(Module &M); |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 237 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 238 | // Active Pass Managers |
| 239 | BasicBlockPassManager_New *activeBBPassManager; |
| 240 | }; |
| 241 | |
| 242 | /// ModulePassManager_New manages ModulePasses and function pass managers. |
| 243 | /// It batches all Module passes passes and function pass managers together and |
| 244 | /// sequence them to process one module. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 245 | class ModulePassManager_New : public CommonPassManagerImpl { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 246 | |
| 247 | public: |
| 248 | ModulePassManager_New() { activeFunctionPassManager = NULL; } |
| 249 | |
| 250 | /// Add a pass into a passmanager queue. |
| 251 | bool addPass(Pass *p); |
| 252 | |
| 253 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 254 | /// whether any of the passes modifies the module, and if so, return true. |
| 255 | bool runOnModule(Module &M); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 256 | |
| 257 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 258 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 259 | |
| 260 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 261 | // Active Pass Manager |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 262 | FunctionPassManagerImpl_New *activeFunctionPassManager; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 265 | /// PassManager_New manages ModulePassManagers |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 266 | class PassManagerImpl_New : public CommonPassManagerImpl { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 267 | |
| 268 | public: |
| 269 | |
| 270 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 271 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 272 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 273 | /// implies that all passes MUST be allocated with 'new'. |
| 274 | void add(Pass *P); |
| 275 | |
| 276 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 277 | /// whether any of the passes modifies the module, and if so, return true. |
| 278 | bool run(Module &M); |
| 279 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 280 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 281 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 282 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 283 | private: |
| 284 | |
| 285 | /// Add a pass into a passmanager queue. This is used by schedulePasses |
| 286 | bool addPass(Pass *p); |
| 287 | |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 288 | /// Schedule pass P for execution. Make sure that passes required by |
| 289 | /// P are run before P is run. Update analysis info maintained by |
| 290 | /// the manager. Remove dead passes. This is a recursive function. |
| 291 | void schedulePass(Pass *P); |
| 292 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 293 | /// Schedule all passes collected in pass queue using add(). Add all the |
| 294 | /// schedule passes into various manager's queue using addPass(). |
| 295 | void schedulePasses(); |
| 296 | |
| 297 | // Collection of pass managers |
| 298 | std::vector<ModulePassManager_New *> PassManagers; |
| 299 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 300 | // Active Pass Manager |
| 301 | ModulePassManager_New *activeManager; |
| 302 | }; |
| 303 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 304 | } // End of llvm namespace |
| 305 | |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 306 | // CommonPassManagerImpl implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 307 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 308 | /// Return true IFF pass P's required analysis set does not required new |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 309 | /// manager. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 310 | bool CommonPassManagerImpl::manageablePass(Pass *P) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 311 | |
| 312 | AnalysisUsage AnUsage; |
| 313 | P->getAnalysisUsage(AnUsage); |
| 314 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 315 | // If this pass is not preserving information that is required by the other |
| 316 | // passes managed by this manager then use new manager |
| 317 | if (!AnUsage.getPreservesAll()) { |
| 318 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 319 | for (std::vector<AnalysisID>::iterator I = RequiredAnalysis.begin(), |
| 320 | E = RequiredAnalysis.end(); I != E; ++I) { |
| 321 | if (std::find(PreservedSet.begin(), PreservedSet.end(), *I) == |
| 322 | PreservedSet.end()) |
| 323 | // This analysis is not preserved. Need new manager. |
| 324 | return false; |
| 325 | } |
| 326 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 330 | /// Augment RequiredAnalysis by adding analysis required by pass P. |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 331 | void CommonPassManagerImpl::noteDownRequiredAnalysis(Pass *P) { |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 332 | AnalysisUsage AnUsage; |
| 333 | P->getAnalysisUsage(AnUsage); |
| 334 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 335 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 336 | // FIXME: What about duplicates ? |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 337 | RequiredAnalysis.insert(RequiredAnalysis.end(), RequiredSet.begin(), |
| 338 | RequiredSet.end()); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 339 | |
| 340 | initializeAnalysisImpl(P); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 343 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
| 344 | void CommonPassManagerImpl::noteDownAvailableAnalysis(Pass *P) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 345 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 346 | if (const PassInfo *PI = P->getPassInfo()) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 347 | AvailableAnalysis[PI] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 348 | |
| 349 | //TODO This pass is the current implementation of all of the interfaces it |
| 350 | //TODO implements as well. |
| 351 | //TODO |
| 352 | //TODO const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 353 | //TODO for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 354 | //TODO CurrentAnalyses[II[i]] = P; |
| 355 | } |
| 356 | } |
| 357 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 358 | /// Remove Analyss not preserved by Pass P |
Devang Patel | 0ed4779 | 2006-11-10 21:33:13 +0000 | [diff] [blame] | 359 | void CommonPassManagerImpl::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 360 | AnalysisUsage AnUsage; |
| 361 | P->getAnalysisUsage(AnUsage); |
| 362 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 363 | |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 364 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 365 | E = AvailableAnalysis.end(); I != E; ++I ) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 366 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 367 | PreservedSet.end()) { |
| 368 | // Remove this analysis |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 369 | std::map<AnalysisID, Pass*>::iterator J = I++; |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 370 | AvailableAnalysis.erase(J); |
| 371 | } |
| 372 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 375 | /// Remove analysis passes that are not used any longer |
| 376 | void CommonPassManagerImpl::removeDeadPasses(Pass *P) { |
| 377 | |
| 378 | for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(), |
| 379 | E = LastUser.end(); I !=E; ++I) { |
| 380 | if (I->second == P) { |
| 381 | Pass *deadPass = I->first; |
| 382 | deadPass->releaseMemory(); |
| 383 | |
| 384 | std::map<AnalysisID, Pass*>::iterator Pos = |
| 385 | AvailableAnalysis.find(deadPass->getPassInfo()); |
| 386 | |
| 387 | assert (Pos != AvailableAnalysis.end() && |
| 388 | "Pass is not available"); |
| 389 | AvailableAnalysis.erase(Pos); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 394 | /// Add pass P into the PassVector. Update RequiredAnalysis and |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 395 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
| 396 | void CommonPassManagerImpl::addPassToManager (Pass *P, |
| 397 | bool ProcessAnalysis) { |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 398 | |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 399 | if (ProcessAnalysis) { |
| 400 | // Take a note of analysis required and made available by this pass |
| 401 | noteDownRequiredAnalysis(P); |
| 402 | noteDownAvailableAnalysis(P); |
| 403 | |
| 404 | // Remove the analysis not preserved by this pass |
| 405 | removeNotPreservedAnalysis(P); |
| 406 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 407 | |
| 408 | // Add pass |
| 409 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 412 | // All Required analyses should be available to the pass as it runs! Here |
| 413 | // we fill in the AnalysisImpls member of the pass so that it can |
| 414 | // successfully use the getAnalysis() method to retrieve the |
| 415 | // implementations it needs. |
| 416 | // |
| 417 | void CommonPassManagerImpl::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 418 | AnalysisUsage AnUsage; |
| 419 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 420 | |
| 421 | for (std::vector<const PassInfo *>::const_iterator |
| 422 | I = AnUsage.getRequiredSet().begin(), |
| 423 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
| 424 | Pass *Impl = getAnalysisPass(*I); |
| 425 | if (Impl == 0) |
| 426 | assert(0 && "Analysis used but not available!"); |
| 427 | // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); |
| 428 | } |
| 429 | } |
| 430 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 431 | /// BasicBlockPassManager implementation |
| 432 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 433 | /// Add pass P into PassVector and return true. If this pass is not |
| 434 | /// manageable by this manager then return false. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 435 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 436 | BasicBlockPassManager_New::addPass(Pass *P) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 437 | |
| 438 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 439 | if (!BP) |
| 440 | return false; |
| 441 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 442 | // If this pass does not preserve anlysis that is used by other passes |
| 443 | // managed by this manager than it is not a suiable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 444 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 445 | return false; |
| 446 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 447 | addPassToManager (BP); |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 448 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 449 | return true; |
| 450 | } |
| 451 | |
| 452 | /// Execute all of the passes scheduled for execution by invoking |
| 453 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 454 | /// the function, and if so, return true. |
| 455 | bool |
| 456 | BasicBlockPassManager_New::runOnFunction(Function &F) { |
| 457 | |
| 458 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 459 | clearAnalysis(); |
| 460 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 461 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 462 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 463 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 464 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 465 | |
| 466 | noteDownAvailableAnalysis(P); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 467 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 468 | Changed |= BP->runOnBasicBlock(*I); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 469 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 470 | removeDeadPasses(P); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 471 | } |
| 472 | return Changed; |
| 473 | } |
| 474 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 475 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 476 | Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) { |
| 477 | return getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 480 | // FunctionPassManager_New implementation |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 481 | /// Create new Function pass manager |
| 482 | FunctionPassManager_New::FunctionPassManager_New() { |
| 483 | FPM = new FunctionPassManagerImpl_New(); |
| 484 | } |
| 485 | |
| 486 | /// add - Add a pass to the queue of passes to run. This passes |
| 487 | /// ownership of the Pass to the PassManager. When the |
| 488 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 489 | /// there is no need to delete the pass. (TODO delete passes.) |
| 490 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 491 | void FunctionPassManager_New::add(Pass *P) { |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 492 | FPM->add(P); |
| 493 | } |
| 494 | |
| 495 | /// Execute all of the passes scheduled for execution. Keep |
| 496 | /// track of whether any of the passes modifies the function, and if |
| 497 | /// so, return true. |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 498 | bool FunctionPassManager_New::runOnModule(Module &M) { |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 499 | return FPM->runOnModule(M); |
| 500 | } |
| 501 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 502 | /// run - Execute all of the passes scheduled for execution. Keep |
| 503 | /// track of whether any of the passes modifies the function, and if |
| 504 | /// so, return true. |
| 505 | /// |
| 506 | bool FunctionPassManager_New::run(Function &F) { |
| 507 | std::string errstr; |
| 508 | if (MP->materializeFunction(&F, &errstr)) { |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 509 | cerr << "Error reading bytecode file: " << errstr << "\n"; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 510 | abort(); |
| 511 | } |
| 512 | return FPM->runOnFunction(F); |
| 513 | } |
| 514 | |
| 515 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 516 | /// doInitialization - Run all of the initializers for the function passes. |
| 517 | /// |
| 518 | bool FunctionPassManager_New::doInitialization() { |
| 519 | return FPM->doInitialization(*MP->getModule()); |
| 520 | } |
| 521 | |
| 522 | /// doFinalization - Run all of the initializers for the function passes. |
| 523 | /// |
| 524 | bool FunctionPassManager_New::doFinalization() { |
| 525 | return FPM->doFinalization(*MP->getModule()); |
| 526 | } |
| 527 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 528 | // FunctionPassManagerImpl_New implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 529 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 530 | // FunctionPassManager |
| 531 | |
| 532 | /// Add pass P into the pass manager queue. If P is a BasicBlockPass then |
| 533 | /// either use it into active basic block pass manager or create new basic |
| 534 | /// block pass manager to handle pass P. |
| 535 | bool |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 536 | FunctionPassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 537 | |
| 538 | // If P is a BasicBlockPass then use BasicBlockPassManager_New. |
| 539 | if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { |
| 540 | |
| 541 | if (!activeBBPassManager |
| 542 | || !activeBBPassManager->addPass(BP)) { |
| 543 | |
| 544 | activeBBPassManager = new BasicBlockPassManager_New(); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 545 | addPassToManager(activeBBPassManager, false); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 546 | if (!activeBBPassManager->addPass(BP)) |
| 547 | assert(0 && "Unable to add Pass"); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 548 | } |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | FunctionPass *FP = dynamic_cast<FunctionPass *>(P); |
| 553 | if (!FP) |
| 554 | return false; |
| 555 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 556 | // If this pass does not preserve anlysis that is used by other passes |
| 557 | // managed by this manager than it is not a suiable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 558 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 559 | return false; |
| 560 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 561 | addPassToManager (FP); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 562 | activeBBPassManager = NULL; |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | /// Execute all of the passes scheduled for execution by invoking |
| 567 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 568 | /// the function, and if so, return true. |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 569 | bool FunctionPassManagerImpl_New::runOnModule(Module &M) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 570 | |
| 571 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 572 | clearAnalysis(); |
| 573 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 574 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 575 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 576 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 577 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 578 | |
| 579 | noteDownAvailableAnalysis(P); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 580 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 581 | Changed |= FP->runOnFunction(*I); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 582 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 583 | removeDeadPasses(P); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 584 | } |
| 585 | return Changed; |
| 586 | } |
| 587 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 588 | /// Execute all of the passes scheduled for execution by invoking |
| 589 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 590 | /// the function, and if so, return true. |
| 591 | bool FunctionPassManagerImpl_New::runOnFunction(Function &F) { |
| 592 | |
| 593 | bool Changed = false; |
| 594 | clearAnalysis(); |
| 595 | |
| 596 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 597 | e = passVectorEnd(); itr != e; ++itr) { |
| 598 | Pass *P = *itr; |
| 599 | |
| 600 | noteDownAvailableAnalysis(P); |
| 601 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 602 | Changed |= FP->runOnFunction(F); |
| 603 | removeNotPreservedAnalysis(P); |
| 604 | removeDeadPasses(P); |
| 605 | } |
| 606 | return Changed; |
| 607 | } |
| 608 | |
| 609 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 610 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 611 | Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 612 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 613 | Pass *P = getAnalysisPass(AID); |
| 614 | if (P) |
| 615 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 616 | |
| 617 | if (activeBBPassManager && |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 618 | activeBBPassManager->getAnalysisPass(AID) != 0) |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 619 | return activeBBPassManager->getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 620 | |
| 621 | // TODO : Check inactive managers |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 622 | return NULL; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 623 | } |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 624 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 625 | inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) { |
| 626 | bool Changed = false; |
| 627 | |
| 628 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 629 | e = passVectorEnd(); itr != e; ++itr) { |
| 630 | Pass *P = *itr; |
| 631 | |
| 632 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 633 | Changed |= FP->doInitialization(M); |
| 634 | } |
| 635 | |
| 636 | return Changed; |
| 637 | } |
| 638 | |
| 639 | inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) { |
| 640 | bool Changed = false; |
| 641 | |
| 642 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 643 | e = passVectorEnd(); itr != e; ++itr) { |
| 644 | Pass *P = *itr; |
| 645 | |
| 646 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 647 | Changed |= FP->doFinalization(M); |
| 648 | } |
| 649 | |
| 650 | |
| 651 | return Changed; |
| 652 | } |
| 653 | |
| 654 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 655 | // ModulePassManager implementation |
| 656 | |
| 657 | /// Add P into pass vector if it is manageble. If P is a FunctionPass |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 658 | /// then use FunctionPassManagerImpl_New to manage it. Return false if P |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 659 | /// is not manageable by this manager. |
| 660 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 661 | ModulePassManager_New::addPass(Pass *P) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 662 | |
| 663 | // If P is FunctionPass then use function pass maanager. |
| 664 | if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { |
| 665 | |
| 666 | activeFunctionPassManager = NULL; |
| 667 | |
| 668 | if (!activeFunctionPassManager |
| 669 | || !activeFunctionPassManager->addPass(P)) { |
| 670 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 671 | activeFunctionPassManager = new FunctionPassManagerImpl_New(); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 672 | addPassToManager(activeFunctionPassManager, false); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 673 | if (!activeFunctionPassManager->addPass(FP)) |
| 674 | assert(0 && "Unable to add pass"); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 675 | } |
| 676 | return true; |
| 677 | } |
| 678 | |
| 679 | ModulePass *MP = dynamic_cast<ModulePass *>(P); |
| 680 | if (!MP) |
| 681 | return false; |
| 682 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 683 | // If this pass does not preserve anlysis that is used by other passes |
| 684 | // managed by this manager than it is not a suiable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 685 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 686 | return false; |
| 687 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 688 | addPassToManager(MP); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 689 | activeFunctionPassManager = NULL; |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | |
| 694 | /// Execute all of the passes scheduled for execution by invoking |
| 695 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 696 | /// the module, and if so, return true. |
| 697 | bool |
| 698 | ModulePassManager_New::runOnModule(Module &M) { |
| 699 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 700 | clearAnalysis(); |
| 701 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 702 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 703 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 704 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 705 | |
| 706 | noteDownAvailableAnalysis(P); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 707 | ModulePass *MP = dynamic_cast<ModulePass*>(P); |
| 708 | Changed |= MP->runOnModule(M); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 709 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 710 | removeDeadPasses(P); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 711 | } |
| 712 | return Changed; |
| 713 | } |
| 714 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 715 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 716 | Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 717 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 718 | |
| 719 | Pass *P = getAnalysisPass(AID); |
| 720 | if (P) |
| 721 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 722 | |
| 723 | if (activeFunctionPassManager && |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 724 | activeFunctionPassManager->getAnalysisPass(AID) != 0) |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 725 | return activeFunctionPassManager->getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 726 | |
| 727 | // TODO : Check inactive managers |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 728 | return NULL; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 732 | Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 733 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 734 | Pass *P = NULL; |
Devang Patel | 7086844 | 2006-11-13 22:53:19 +0000 | [diff] [blame] | 735 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 736 | e = PassManagers.end(); !P && itr != e; ++itr) |
| 737 | P = (*itr)->getAnalysisPassFromManager(AID); |
| 738 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 741 | /// Schedule pass P for execution. Make sure that passes required by |
| 742 | /// P are run before P is run. Update analysis info maintained by |
| 743 | /// the manager. Remove dead passes. This is a recursive function. |
| 744 | void PassManagerImpl_New::schedulePass(Pass *P) { |
| 745 | |
| 746 | AnalysisUsage AnUsage; |
| 747 | P->getAnalysisUsage(AnUsage); |
| 748 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 749 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 750 | E = RequiredSet.end(); I != E; ++I) { |
| 751 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 752 | Pass *AnalysisPass = getAnalysisPassFromManager(*I); |
| 753 | if (!AnalysisPass) { |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 754 | // Schedule this analysis run first. |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 755 | AnalysisPass = (*I)->createPass(); |
| 756 | schedulePass(AnalysisPass); |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 757 | } |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 758 | setLastUser (AnalysisPass, P); |
Devang Patel | 4a3fa4f | 2006-11-15 01:48:14 +0000 | [diff] [blame] | 759 | |
| 760 | // Prolong live range of analyses that are needed after an analysis pass |
| 761 | // is destroyed, for querying by subsequent passes |
| 762 | const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); |
| 763 | for (std::vector<AnalysisID>::const_iterator I = IDs.begin(), |
| 764 | E = IDs.end(); I != E; ++I) { |
| 765 | Pass *AP = getAnalysisPassFromManager(*I); |
| 766 | assert (AP && "Analysis pass is not available"); |
| 767 | setLastUser(AP, P); |
| 768 | } |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 769 | } |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 770 | addPass(P); |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 773 | /// Schedule all passes from the queue by adding them in their |
| 774 | /// respective manager's queue. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 775 | void PassManagerImpl_New::schedulePasses() { |
| 776 | for (std::vector<Pass *>::iterator I = passVectorBegin(), |
| 777 | E = passVectorEnd(); I != E; ++I) |
| 778 | schedulePass (*I); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /// Add pass P to the queue of passes to run. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 782 | void PassManagerImpl_New::add(Pass *P) { |
| 783 | // Do not process Analysis now. Analysis is process while scheduling |
| 784 | // the pass vector. |
Devang Patel | db789fb | 2006-11-11 02:06:21 +0000 | [diff] [blame] | 785 | addPassToManager(P, false); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | // PassManager_New implementation |
| 789 | /// Add P into active pass manager or use new module pass manager to |
| 790 | /// manage it. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 791 | bool PassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 792 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 793 | if (!activeManager || !activeManager->addPass(P)) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 794 | activeManager = new ModulePassManager_New(); |
| 795 | PassManagers.push_back(activeManager); |
| 796 | } |
| 797 | |
| 798 | return activeManager->addPass(P); |
| 799 | } |
| 800 | |
| 801 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 802 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 803 | bool PassManagerImpl_New::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 804 | |
| 805 | schedulePasses(); |
| 806 | bool Changed = false; |
| 807 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
| 808 | e = PassManagers.end(); itr != e; ++itr) { |
| 809 | ModulePassManager_New *pm = *itr; |
| 810 | Changed |= pm->runOnModule(M); |
| 811 | } |
| 812 | return Changed; |
| 813 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 814 | |
| 815 | /// Create new pass manager |
| 816 | PassManager_New::PassManager_New() { |
| 817 | PM = new PassManagerImpl_New(); |
| 818 | } |
| 819 | |
| 820 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 821 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 822 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 823 | /// implies that all passes MUST be allocated with 'new'. |
| 824 | void |
| 825 | PassManager_New::add(Pass *P) { |
| 826 | PM->add(P); |
| 827 | } |
| 828 | |
| 829 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 830 | /// whether any of the passes modifies the module, and if so, return true. |
| 831 | bool |
| 832 | PassManager_New::run(Module &M) { |
| 833 | return PM->run(M); |
| 834 | } |
| 835 | |