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