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