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