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