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