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