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