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