Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1 | //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 5 | // This file was developed by Devang Patel and is distributed under |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM Pass Manager infrastructure. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | |
| 15 | #include "llvm/PassManager.h" |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 17 | #include "llvm/ModuleProvider.h" |
Bill Wendling | dfc9189 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Streams.h" |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 19 | #include <vector> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 20 | #include <map> |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // Overview: |
| 25 | // The Pass Manager Infrastructure manages passes. It's responsibilities are: |
| 26 | // |
| 27 | // o Manage optimization pass execution order |
| 28 | // o Make required Analysis information available before pass P is run |
| 29 | // o Release memory occupied by dead passes |
| 30 | // o If Analysis information is dirtied by a pass then regenerate Analysis |
| 31 | // information before it is consumed by another pass. |
| 32 | // |
| 33 | // Pass Manager Infrastructure uses multipe pass managers. They are PassManager, |
| 34 | // FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class |
| 35 | // hierarcy uses multiple inheritance but pass managers do not derive from |
| 36 | // another pass manager. |
| 37 | // |
| 38 | // PassManager and FunctionPassManager are two top level pass manager that |
| 39 | // represents the external interface of this entire pass manager infrastucture. |
| 40 | // |
| 41 | // Important classes : |
| 42 | // |
| 43 | // [o] class PMTopLevelManager; |
| 44 | // |
| 45 | // Two top level managers, PassManager and FunctionPassManager, derive from |
| 46 | // PMTopLevelManager. PMTopLevelManager manages information used by top level |
| 47 | // managers such as last user info. |
| 48 | // |
| 49 | // [o] class PMDataManager; |
| 50 | // |
| 51 | // PMDataManager manages information, e.g. list of available analysis info, |
| 52 | // used by a pass manager to manage execution order of passes. It also provides |
| 53 | // a place to implement common pass manager APIs. All pass managers derive from |
| 54 | // PMDataManager. |
| 55 | // |
| 56 | // [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager; |
| 57 | // |
| 58 | // BasicBlockPassManager manages BasicBlockPasses. |
| 59 | // |
| 60 | // [o] class FunctionPassManager; |
| 61 | // |
| 62 | // This is a external interface used by JIT to manage FunctionPasses. This |
| 63 | // interface relies on FunctionPassManagerImpl to do all the tasks. |
| 64 | // |
| 65 | // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager, |
| 66 | // public PMTopLevelManager; |
| 67 | // |
| 68 | // FunctionPassManagerImpl is a top level manager. It manages FunctionPasses |
| 69 | // and BasicBlockPassManagers. |
| 70 | // |
| 71 | // [o] class ModulePassManager : public Pass, public PMDataManager; |
| 72 | // |
| 73 | // ModulePassManager manages ModulePasses and FunctionPassManagerImpls. |
| 74 | // |
| 75 | // [o] class PassManager; |
| 76 | // |
| 77 | // This is a external interface used by various tools to manages passes. It |
| 78 | // relies on PassManagerImpl to do all the tasks. |
| 79 | // |
| 80 | // [o] class PassManagerImpl : public Pass, public PMDataManager, |
| 81 | // public PMDTopLevelManager |
| 82 | // |
| 83 | // PassManagerImpl is a top level pass manager responsible for managing |
| 84 | // ModulePassManagers. |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 87 | namespace llvm { |
| 88 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // PMTopLevelManager |
| 91 | // |
| 92 | /// PMTopLevelManager manages LastUser info and collects common APIs used by |
| 93 | /// top level pass managers. |
| 94 | class PMTopLevelManager { |
| 95 | |
| 96 | public: |
| 97 | |
| 98 | inline std::vector<Pass *>::iterator passManagersBegin() { |
| 99 | return PassManagers.begin(); |
| 100 | } |
| 101 | |
| 102 | inline std::vector<Pass *>::iterator passManagersEnd() { |
| 103 | return PassManagers.end(); |
| 104 | } |
| 105 | |
| 106 | /// Schedule pass P for execution. Make sure that passes required by |
| 107 | /// P are run before P is run. Update analysis info maintained by |
| 108 | /// the manager. Remove dead passes. This is a recursive function. |
| 109 | void schedulePass(Pass *P, Pass *PM); |
| 110 | |
| 111 | /// This is implemented by top level pass manager and used by |
| 112 | /// schedulePass() to add analysis info passes that are not available. |
| 113 | virtual void addTopLevelPass(Pass *P) = 0; |
| 114 | |
| 115 | /// Set pass P as the last user of the given analysis passes. |
| 116 | void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P); |
| 117 | |
| 118 | /// Collect passes whose last user is P |
| 119 | void collectLastUses(std::vector<Pass *> &LastUses, Pass *P); |
| 120 | |
| 121 | virtual ~PMTopLevelManager() { |
| 122 | PassManagers.clear(); |
| 123 | } |
| 124 | |
Devang Patel | e0eb9d8 | 2006-12-07 20:51:18 +0000 | [diff] [blame] | 125 | /// Add immutable pass and initialize it. |
| 126 | inline void addImmutablePass(ImmutablePass *P) { |
| 127 | P->initializePass(); |
| 128 | ImmutablePasses.push_back(P); |
| 129 | } |
| 130 | |
| 131 | inline std::vector<ImmutablePass *>& getImmutablePasses() { |
| 132 | return ImmutablePasses; |
| 133 | } |
| 134 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 135 | private: |
| 136 | |
| 137 | /// Collection of pass managers |
| 138 | std::vector<Pass *> PassManagers; |
| 139 | |
| 140 | // Map to keep track of last user of the analysis pass. |
| 141 | // LastUser->second is the last user of Lastuser->first. |
| 142 | std::map<Pass *, Pass *> LastUser; |
Devang Patel | e0eb9d8 | 2006-12-07 20:51:18 +0000 | [diff] [blame] | 143 | |
| 144 | /// Immutable passes are managed by top level manager. |
| 145 | std::vector<ImmutablePass *> ImmutablePasses; |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /// Set pass P as the last user of the given analysis passes. |
| 149 | void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses, |
| 150 | Pass *P) { |
| 151 | |
| 152 | for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(), |
| 153 | E = AnalysisPasses.end(); I != E; ++I) { |
| 154 | Pass *AP = *I; |
| 155 | LastUser[AP] = P; |
| 156 | // If AP is the last user of other passes then make P last user of |
| 157 | // such passes. |
| 158 | for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
| 159 | LUE = LastUser.end(); LUI != LUE; ++LUI) { |
| 160 | if (LUI->second == AP) |
| 161 | LastUser[LUI->first] = P; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | /// Collect passes whose last user is P |
| 168 | void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses, |
| 169 | Pass *P) { |
| 170 | for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
| 171 | LUE = LastUser.end(); LUI != LUE; ++LUI) |
| 172 | if (LUI->second == P) |
| 173 | LastUses.push_back(LUI->first); |
| 174 | } |
| 175 | |
Devang Patel | de12418 | 2006-12-07 21:10:57 +0000 | [diff] [blame] | 176 | /// Schedule pass P for execution. Make sure that passes required by |
| 177 | /// P are run before P is run. Update analysis info maintained by |
| 178 | /// the manager. Remove dead passes. This is a recursive function. |
| 179 | void PMTopLevelManager::schedulePass(Pass *P, Pass *PM) { |
| 180 | |
| 181 | // TODO : Allocate function manager for this pass, other wise required set |
| 182 | // may be inserted into previous function manager |
| 183 | |
| 184 | AnalysisUsage AnUsage; |
| 185 | P->getAnalysisUsage(AnUsage); |
| 186 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 187 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 188 | E = RequiredSet.end(); I != E; ++I) { |
| 189 | |
| 190 | Pass *AnalysisPass = NULL; // FIXME PM->getAnalysisPass(*I, true); |
| 191 | if (!AnalysisPass) { |
| 192 | // Schedule this analysis run first. |
| 193 | AnalysisPass = (*I)->createPass(); |
| 194 | schedulePass(AnalysisPass, PM); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Now all required passes are available. |
| 199 | addTopLevelPass(P); |
| 200 | } |
| 201 | |
| 202 | |
Devang Patel | f3827bc | 2006-12-07 19:54:15 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
| 204 | // PMDataManager |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 205 | |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 206 | /// PMDataManager provides the common place to manage the analysis data |
| 207 | /// used by pass managers. |
| 208 | class PMDataManager { |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 209 | |
| 210 | public: |
| 211 | |
Devang Patel | f3827bc | 2006-12-07 19:54:15 +0000 | [diff] [blame] | 212 | PMDataManager() : TPM(NULL) { |
| 213 | initializeAnalysisInfo(); |
| 214 | } |
| 215 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 216 | /// Return true IFF pass P's required analysis set does not required new |
| 217 | /// manager. |
| 218 | bool manageablePass(Pass *P); |
| 219 | |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 220 | Pass *getAnalysisPass(AnalysisID AID) const { |
| 221 | |
| 222 | std::map<AnalysisID, Pass*>::const_iterator I = |
| 223 | AvailableAnalysis.find(AID); |
| 224 | |
| 225 | if (I != AvailableAnalysis.end()) |
| 226 | return NULL; |
| 227 | else |
| 228 | return I->second; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 229 | } |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 230 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 231 | /// Augment AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 232 | void recordAvailableAnalysis(Pass *P); |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 233 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 234 | /// Remove Analysis that is not preserved by the pass |
| 235 | void removeNotPreservedAnalysis(Pass *P); |
| 236 | |
| 237 | /// Remove dead passes |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 238 | void removeDeadPasses(Pass *P); |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 239 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 240 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 241 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
| 242 | void addPassToManager (Pass *P, bool ProcessAnalysis = true); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 243 | |
Devang Patel | a6b6dcb | 2006-12-07 18:41:09 +0000 | [diff] [blame] | 244 | // Initialize available analysis information. |
| 245 | void initializeAnalysisInfo() { |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 246 | AvailableAnalysis.clear(); |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 247 | LastUser.clear(); |
Devang Patel | b390032 | 2006-12-07 21:02:08 +0000 | [diff] [blame] | 248 | |
| 249 | // Include immutable passes into AvailableAnalysis vector. |
| 250 | std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses(); |
| 251 | for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(), |
| 252 | E = ImmutablePasses.end(); I != E; ++I) |
| 253 | recordAvailableAnalysis(*I); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 256 | // All Required analyses should be available to the pass as it runs! Here |
| 257 | // we fill in the AnalysisImpls member of the pass so that it can |
| 258 | // successfully use the getAnalysis() method to retrieve the |
| 259 | // implementations it needs. |
| 260 | // |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 261 | void initializeAnalysisImpl(Pass *P); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 262 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 263 | inline std::vector<Pass *>::iterator passVectorBegin() { |
| 264 | return PassVector.begin(); |
| 265 | } |
| 266 | |
| 267 | inline std::vector<Pass *>::iterator passVectorEnd() { |
| 268 | return PassVector.end(); |
| 269 | } |
| 270 | |
Devang Patel | 4a3fa4f | 2006-11-15 01:48:14 +0000 | [diff] [blame] | 271 | inline void setLastUser(Pass *P, Pass *LU) { |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 272 | LastUser[P] = LU; |
| 273 | // TODO : Check if pass P is available. |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 274 | } |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 275 | |
Devang Patel | f3827bc | 2006-12-07 19:54:15 +0000 | [diff] [blame] | 276 | // Access toplevel manager |
| 277 | PMTopLevelManager *getTopLevelManager() { return TPM; } |
| 278 | void setTopLevelManager(PMTopLevelManager *T) { TPM = T; } |
| 279 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 280 | private: |
Devang Patel | dafa4dd | 2006-11-14 00:03:04 +0000 | [diff] [blame] | 281 | // Set of available Analysis. This information is used while scheduling |
| 282 | // pass. If a pass requires an analysis which is not not available then |
| 283 | // equired analysis pass is scheduled to run before the pass itself is |
| 284 | // scheduled to run. |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 285 | std::map<AnalysisID, Pass*> AvailableAnalysis; |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 286 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 287 | // Map to keep track of last user of the analysis pass. |
| 288 | // LastUser->second is the last user of Lastuser->first. |
| 289 | std::map<Pass *, Pass *> LastUser; |
| 290 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 291 | // Collection of pass that are managed by this manager |
| 292 | std::vector<Pass *> PassVector; |
Devang Patel | f3827bc | 2006-12-07 19:54:15 +0000 | [diff] [blame] | 293 | |
| 294 | // Top level manager. |
| 295 | // TODO : Make it a reference. |
| 296 | PMTopLevelManager *TPM; |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 299 | /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the |
| 300 | /// pass together and sequence them to process one basic block before |
| 301 | /// processing next basic block. |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 302 | class BasicBlockPassManager_New : public PMDataManager, |
Devang Patel | 42add71 | 2006-11-15 01:11:27 +0000 | [diff] [blame] | 303 | public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 304 | |
| 305 | public: |
| 306 | BasicBlockPassManager_New() { } |
| 307 | |
| 308 | /// Add a pass into a passmanager queue. |
| 309 | bool addPass(Pass *p); |
| 310 | |
| 311 | /// Execute all of the passes scheduled for execution. Keep track of |
| 312 | /// whether any of the passes modifies the function, and if so, return true. |
| 313 | bool runOnFunction(Function &F); |
| 314 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 315 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 316 | /// TODO : Replace this method with getAnalysisPass() |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 317 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 318 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 319 | /// Pass Manager itself does not invalidate any analysis info. |
| 320 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 321 | Info.setPreservesAll(); |
| 322 | } |
| 323 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 324 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 327 | /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers. |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 328 | /// It batches all function passes and basic block pass managers together and |
| 329 | /// sequence them to process one function at a time before processing next |
| 330 | /// function. |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 331 | class FunctionPassManagerImpl_New : public ModulePass, |
| 332 | public PMDataManager, |
| 333 | public PMTopLevelManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 334 | public: |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 335 | FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ } |
| 336 | FunctionPassManagerImpl_New() { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 337 | activeBBPassManager = NULL; |
| 338 | } |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 339 | ~FunctionPassManagerImpl_New() { /* TODO */ }; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 340 | |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 341 | inline void addTopLevelPass(Pass *P) { |
| 342 | addPass(P); |
| 343 | } |
| 344 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 345 | /// add - Add a pass to the queue of passes to run. This passes |
| 346 | /// ownership of the Pass to the PassManager. When the |
| 347 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 348 | /// there is no need to delete the pass. (TODO delete passes.) |
| 349 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 350 | void add(Pass *P) { |
| 351 | schedulePass(P, this); |
| 352 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 353 | |
| 354 | /// Add pass into the pass manager queue. |
| 355 | bool addPass(Pass *P); |
| 356 | |
| 357 | /// Execute all of the passes scheduled for execution. Keep |
| 358 | /// track of whether any of the passes modifies the function, and if |
| 359 | /// so, return true. |
| 360 | bool runOnModule(Module &M); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 361 | bool runOnFunction(Function &F); |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 362 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 363 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 364 | /// TODO : Replace this method with getAnalysisPass() |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 365 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 366 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 367 | /// doInitialization - Run all of the initializers for the function passes. |
| 368 | /// |
| 369 | bool doInitialization(Module &M); |
| 370 | |
| 371 | /// doFinalization - Run all of the initializers for the function passes. |
| 372 | /// |
| 373 | bool doFinalization(Module &M); |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 374 | |
| 375 | /// Pass Manager itself does not invalidate any analysis info. |
| 376 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 377 | Info.setPreservesAll(); |
| 378 | } |
| 379 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 380 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 381 | // Active Pass Managers |
| 382 | BasicBlockPassManager_New *activeBBPassManager; |
| 383 | }; |
| 384 | |
| 385 | /// ModulePassManager_New manages ModulePasses and function pass managers. |
| 386 | /// It batches all Module passes passes and function pass managers together and |
| 387 | /// sequence them to process one module. |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 388 | class ModulePassManager_New : public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 389 | |
| 390 | public: |
| 391 | ModulePassManager_New() { activeFunctionPassManager = NULL; } |
| 392 | |
| 393 | /// Add a pass into a passmanager queue. |
| 394 | bool addPass(Pass *p); |
| 395 | |
| 396 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 397 | /// whether any of the passes modifies the module, and if so, return true. |
| 398 | bool runOnModule(Module &M); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 399 | |
| 400 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 401 | /// TODO : Replace this method with getAnalysisPass() |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 402 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 403 | |
| 404 | /// Pass Manager itself does not invalidate any analysis info. |
| 405 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 406 | Info.setPreservesAll(); |
| 407 | } |
| 408 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 409 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 410 | // Active Pass Manager |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 411 | FunctionPassManagerImpl_New *activeFunctionPassManager; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 412 | }; |
| 413 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 414 | /// PassManager_New manages ModulePassManagers |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 415 | class PassManagerImpl_New : public Pass, |
| 416 | public PMDataManager, |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 417 | public PMTopLevelManager { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 418 | |
| 419 | public: |
| 420 | |
| 421 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 422 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 423 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 424 | /// implies that all passes MUST be allocated with 'new'. |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 425 | void add(Pass *P) { |
| 426 | schedulePass(P, this); |
| 427 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 428 | |
| 429 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 430 | /// whether any of the passes modifies the module, and if so, return true. |
| 431 | bool run(Module &M); |
| 432 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 433 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 434 | /// TODO : Replace this method with getAnalysisPass() |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 435 | Pass *getAnalysisPassFromManager(AnalysisID AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 436 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 437 | /// Pass Manager itself does not invalidate any analysis info. |
| 438 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 439 | Info.setPreservesAll(); |
| 440 | } |
| 441 | |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 442 | inline void addTopLevelPass(Pass *P) { |
| 443 | addPass(P); |
| 444 | } |
| 445 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 446 | private: |
| 447 | |
Devang Patel | de12418 | 2006-12-07 21:10:57 +0000 | [diff] [blame] | 448 | /// Add a pass into a passmanager queue. |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 449 | bool addPass(Pass *p); |
| 450 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 451 | // Collection of pass managers |
| 452 | std::vector<ModulePassManager_New *> PassManagers; |
| 453 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 454 | // Active Pass Manager |
| 455 | ModulePassManager_New *activeManager; |
| 456 | }; |
| 457 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 458 | } // End of llvm namespace |
| 459 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 460 | //===----------------------------------------------------------------------===// |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 461 | // PMDataManager implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 462 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 463 | /// 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] | 464 | /// manager. |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 465 | bool PMDataManager::manageablePass(Pass *P) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 466 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 467 | // TODO |
| 468 | // If this pass is not preserving information that is required by a |
| 469 | // pass maintained by higher level pass manager then do not insert |
| 470 | // this pass into current manager. Use new manager. For example, |
| 471 | // For example, If FunctionPass F is not preserving ModulePass Info M1 |
| 472 | // that is used by another ModulePass M2 then do not insert F in |
| 473 | // current function pass manager. |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 474 | return true; |
| 475 | } |
| 476 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 477 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 478 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 479 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 480 | if (const PassInfo *PI = P->getPassInfo()) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 481 | AvailableAnalysis[PI] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 482 | |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 483 | //This pass is the current implementation of all of the interfaces it |
| 484 | //implements as well. |
| 485 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 486 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 487 | AvailableAnalysis[II[i]] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 491 | /// Remove Analyss not preserved by Pass P |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 492 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 493 | AnalysisUsage AnUsage; |
| 494 | P->getAnalysisUsage(AnUsage); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 495 | |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 496 | if (AnUsage.getPreservesAll()) |
| 497 | return; |
| 498 | |
| 499 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 500 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 501 | E = AvailableAnalysis.end(); I != E; ++I ) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 502 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 503 | PreservedSet.end()) { |
| 504 | // Remove this analysis |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 505 | std::map<AnalysisID, Pass*>::iterator J = I++; |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 506 | AvailableAnalysis.erase(J); |
| 507 | } |
| 508 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 511 | /// Remove analysis passes that are not used any longer |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 512 | void PMDataManager::removeDeadPasses(Pass *P) { |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 513 | |
| 514 | for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(), |
| 515 | E = LastUser.end(); I !=E; ++I) { |
| 516 | if (I->second == P) { |
| 517 | Pass *deadPass = I->first; |
| 518 | deadPass->releaseMemory(); |
| 519 | |
| 520 | std::map<AnalysisID, Pass*>::iterator Pos = |
| 521 | AvailableAnalysis.find(deadPass->getPassInfo()); |
| 522 | |
| 523 | assert (Pos != AvailableAnalysis.end() && |
| 524 | "Pass is not available"); |
| 525 | AvailableAnalysis.erase(Pos); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 530 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 531 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 532 | void PMDataManager::addPassToManager(Pass *P, |
| 533 | bool ProcessAnalysis) { |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 534 | |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 535 | if (ProcessAnalysis) { |
| 536 | // Take a note of analysis required and made available by this pass |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 537 | initializeAnalysisImpl(P); |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 538 | recordAvailableAnalysis(P); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 539 | |
| 540 | // Remove the analysis not preserved by this pass |
| 541 | removeNotPreservedAnalysis(P); |
| 542 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 543 | |
| 544 | // Add pass |
| 545 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 548 | // All Required analyses should be available to the pass as it runs! Here |
| 549 | // we fill in the AnalysisImpls member of the pass so that it can |
| 550 | // successfully use the getAnalysis() method to retrieve the |
| 551 | // implementations it needs. |
| 552 | // |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 553 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 554 | AnalysisUsage AnUsage; |
| 555 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 556 | |
| 557 | for (std::vector<const PassInfo *>::const_iterator |
| 558 | I = AnUsage.getRequiredSet().begin(), |
| 559 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
| 560 | Pass *Impl = getAnalysisPass(*I); |
| 561 | if (Impl == 0) |
| 562 | assert(0 && "Analysis used but not available!"); |
| 563 | // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); |
| 564 | } |
| 565 | } |
| 566 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 567 | //===----------------------------------------------------------------------===// |
| 568 | // BasicBlockPassManager_New implementation |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 569 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 570 | /// Add pass P into PassVector and return true. If this pass is not |
| 571 | /// manageable by this manager then return false. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 572 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 573 | BasicBlockPassManager_New::addPass(Pass *P) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 574 | |
| 575 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 576 | if (!BP) |
| 577 | return false; |
| 578 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 579 | // If this pass does not preserve anlysis that is used by other passes |
| 580 | // 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] | 581 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 582 | return false; |
| 583 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 584 | addPassToManager (BP); |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 585 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | |
| 589 | /// Execute all of the passes scheduled for execution by invoking |
| 590 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 591 | /// the function, and if so, return true. |
| 592 | bool |
| 593 | BasicBlockPassManager_New::runOnFunction(Function &F) { |
| 594 | |
| 595 | bool Changed = false; |
Devang Patel | a6b6dcb | 2006-12-07 18:41:09 +0000 | [diff] [blame] | 596 | initializeAnalysisInfo(); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 597 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 598 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 599 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 600 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 601 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 602 | |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 603 | recordAvailableAnalysis(P); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 604 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 605 | Changed |= BP->runOnBasicBlock(*I); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 606 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 607 | removeDeadPasses(P); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 608 | } |
| 609 | return Changed; |
| 610 | } |
| 611 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 612 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 613 | /// TODO : Replace this method with getAnalysisPass() |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 614 | Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) { |
| 615 | return getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 618 | //===----------------------------------------------------------------------===// |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 619 | // FunctionPassManager_New implementation |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 620 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 621 | /// Create new Function pass manager |
| 622 | FunctionPassManager_New::FunctionPassManager_New() { |
| 623 | FPM = new FunctionPassManagerImpl_New(); |
| 624 | } |
| 625 | |
| 626 | /// add - Add a pass to the queue of passes to run. This passes |
| 627 | /// ownership of the Pass to the PassManager. When the |
| 628 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 629 | /// there is no need to delete the pass. (TODO delete passes.) |
| 630 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 631 | void FunctionPassManager_New::add(Pass *P) { |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 632 | FPM->add(P); |
| 633 | } |
| 634 | |
| 635 | /// Execute all of the passes scheduled for execution. Keep |
| 636 | /// track of whether any of the passes modifies the function, and if |
| 637 | /// so, return true. |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 638 | bool FunctionPassManager_New::runOnModule(Module &M) { |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 639 | return FPM->runOnModule(M); |
| 640 | } |
| 641 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 642 | /// run - Execute all of the passes scheduled for execution. Keep |
| 643 | /// track of whether any of the passes modifies the function, and if |
| 644 | /// so, return true. |
| 645 | /// |
| 646 | bool FunctionPassManager_New::run(Function &F) { |
| 647 | std::string errstr; |
| 648 | if (MP->materializeFunction(&F, &errstr)) { |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 649 | cerr << "Error reading bytecode file: " << errstr << "\n"; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 650 | abort(); |
| 651 | } |
| 652 | return FPM->runOnFunction(F); |
| 653 | } |
| 654 | |
| 655 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 656 | /// doInitialization - Run all of the initializers for the function passes. |
| 657 | /// |
| 658 | bool FunctionPassManager_New::doInitialization() { |
| 659 | return FPM->doInitialization(*MP->getModule()); |
| 660 | } |
| 661 | |
| 662 | /// doFinalization - Run all of the initializers for the function passes. |
| 663 | /// |
| 664 | bool FunctionPassManager_New::doFinalization() { |
| 665 | return FPM->doFinalization(*MP->getModule()); |
| 666 | } |
| 667 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 668 | //===----------------------------------------------------------------------===// |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 669 | // FunctionPassManagerImpl_New implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 670 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 671 | /// Add pass P into the pass manager queue. If P is a BasicBlockPass then |
| 672 | /// either use it into active basic block pass manager or create new basic |
| 673 | /// block pass manager to handle pass P. |
| 674 | bool |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 675 | FunctionPassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 676 | |
| 677 | // If P is a BasicBlockPass then use BasicBlockPassManager_New. |
| 678 | if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { |
| 679 | |
| 680 | if (!activeBBPassManager |
| 681 | || !activeBBPassManager->addPass(BP)) { |
| 682 | |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 683 | // TODO : intialize AvailableAnalysis info |
| 684 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 685 | activeBBPassManager = new BasicBlockPassManager_New(); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 686 | addPassToManager(activeBBPassManager, false); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 687 | if (!activeBBPassManager->addPass(BP)) |
| 688 | assert(0 && "Unable to add Pass"); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 689 | } |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | FunctionPass *FP = dynamic_cast<FunctionPass *>(P); |
| 694 | if (!FP) |
| 695 | return false; |
| 696 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 697 | // If this pass does not preserve anlysis that is used by other passes |
| 698 | // 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] | 699 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 700 | return false; |
| 701 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 702 | addPassToManager (FP); |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 703 | // TODO : intialize AvailableAnalysis info |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 704 | activeBBPassManager = NULL; |
| 705 | return true; |
| 706 | } |
| 707 | |
| 708 | /// Execute all of the passes scheduled for execution by invoking |
| 709 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 710 | /// the function, and if so, return true. |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 711 | bool FunctionPassManagerImpl_New::runOnModule(Module &M) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 712 | |
| 713 | bool Changed = false; |
Devang Patel | a6b6dcb | 2006-12-07 18:41:09 +0000 | [diff] [blame] | 714 | initializeAnalysisInfo(); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 715 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 716 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 717 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 718 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 719 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 720 | |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 721 | recordAvailableAnalysis(P); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 722 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 723 | Changed |= FP->runOnFunction(*I); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 724 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 725 | removeDeadPasses(P); |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 726 | } |
| 727 | return Changed; |
| 728 | } |
| 729 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 730 | /// Execute all of the passes scheduled for execution by invoking |
| 731 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 732 | /// the function, and if so, return true. |
| 733 | bool FunctionPassManagerImpl_New::runOnFunction(Function &F) { |
| 734 | |
| 735 | bool Changed = false; |
Devang Patel | a6b6dcb | 2006-12-07 18:41:09 +0000 | [diff] [blame] | 736 | initializeAnalysisInfo(); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 737 | |
| 738 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 739 | e = passVectorEnd(); itr != e; ++itr) { |
| 740 | Pass *P = *itr; |
| 741 | |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 742 | recordAvailableAnalysis(P); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 743 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 744 | Changed |= FP->runOnFunction(F); |
| 745 | removeNotPreservedAnalysis(P); |
| 746 | removeDeadPasses(P); |
| 747 | } |
| 748 | return Changed; |
| 749 | } |
| 750 | |
| 751 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 752 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 753 | /// TODO : Replace this method with getAnalysisPass() |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 754 | Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 755 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 756 | Pass *P = getAnalysisPass(AID); |
| 757 | if (P) |
| 758 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 759 | |
| 760 | if (activeBBPassManager && |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 761 | activeBBPassManager->getAnalysisPass(AID) != 0) |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 762 | return activeBBPassManager->getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 763 | |
| 764 | // TODO : Check inactive managers |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 765 | return NULL; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 766 | } |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 767 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 768 | inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) { |
| 769 | bool Changed = false; |
| 770 | |
| 771 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 772 | e = passVectorEnd(); itr != e; ++itr) { |
| 773 | Pass *P = *itr; |
| 774 | |
| 775 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 776 | Changed |= FP->doInitialization(M); |
| 777 | } |
| 778 | |
| 779 | return Changed; |
| 780 | } |
| 781 | |
| 782 | inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) { |
| 783 | bool Changed = false; |
| 784 | |
| 785 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 786 | e = passVectorEnd(); itr != e; ++itr) { |
| 787 | Pass *P = *itr; |
| 788 | |
| 789 | FunctionPass *FP = dynamic_cast<FunctionPass*>(P); |
| 790 | Changed |= FP->doFinalization(M); |
| 791 | } |
| 792 | |
| 793 | |
| 794 | return Changed; |
| 795 | } |
| 796 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 797 | //===----------------------------------------------------------------------===// |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 798 | // ModulePassManager implementation |
| 799 | |
| 800 | /// 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] | 801 | /// then use FunctionPassManagerImpl_New to manage it. Return false if P |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 802 | /// is not manageable by this manager. |
| 803 | bool |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 804 | ModulePassManager_New::addPass(Pass *P) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 805 | |
| 806 | // If P is FunctionPass then use function pass maanager. |
| 807 | if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { |
| 808 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 809 | if (!activeFunctionPassManager |
| 810 | || !activeFunctionPassManager->addPass(P)) { |
| 811 | |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 812 | // TODO : intialize AvailableAnalysis info |
| 813 | activeFunctionPassManager = NULL; |
| 814 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 815 | activeFunctionPassManager = new FunctionPassManagerImpl_New(); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 816 | addPassToManager(activeFunctionPassManager, false); |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 817 | if (!activeFunctionPassManager->addPass(FP)) |
| 818 | assert(0 && "Unable to add pass"); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | |
| 823 | ModulePass *MP = dynamic_cast<ModulePass *>(P); |
| 824 | if (!MP) |
| 825 | return false; |
| 826 | |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 827 | // If this pass does not preserve anlysis that is used by other passes |
| 828 | // 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] | 829 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 830 | return false; |
| 831 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 832 | addPassToManager(MP); |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 833 | // TODO : intialize AvailableAnalysis info |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 834 | activeFunctionPassManager = NULL; |
| 835 | return true; |
| 836 | } |
| 837 | |
| 838 | |
| 839 | /// Execute all of the passes scheduled for execution by invoking |
| 840 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 841 | /// the module, and if so, return true. |
| 842 | bool |
| 843 | ModulePassManager_New::runOnModule(Module &M) { |
| 844 | bool Changed = false; |
Devang Patel | a6b6dcb | 2006-12-07 18:41:09 +0000 | [diff] [blame] | 845 | initializeAnalysisInfo(); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 846 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 847 | for (std::vector<Pass *>::iterator itr = passVectorBegin(), |
| 848 | e = passVectorEnd(); itr != e; ++itr) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 849 | Pass *P = *itr; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 850 | |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 851 | recordAvailableAnalysis(P); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 852 | ModulePass *MP = dynamic_cast<ModulePass*>(P); |
| 853 | Changed |= MP->runOnModule(M); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 854 | removeNotPreservedAnalysis(P); |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 855 | removeDeadPasses(P); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 856 | } |
| 857 | return Changed; |
| 858 | } |
| 859 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 860 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 861 | /// TODO : Replace this method with getAnalysisPass() |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 862 | Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 863 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 864 | Pass *P = getAnalysisPass(AID); |
| 865 | if (P) |
| 866 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 867 | |
| 868 | if (activeFunctionPassManager && |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 869 | activeFunctionPassManager->getAnalysisPass(AID) != 0) |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 870 | return activeFunctionPassManager->getAnalysisPass(AID); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 871 | |
| 872 | // TODO : Check inactive managers |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 873 | return NULL; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 876 | //===----------------------------------------------------------------------===// |
| 877 | // PassManagerImpl implementation |
| 878 | |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 879 | /// Return true IFF AnalysisID AID is currently available. |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame^] | 880 | /// TODO : Replace this method with getAnalysisPass() |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 881 | Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) { |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 882 | |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 883 | Pass *P = NULL; |
Devang Patel | 7086844 | 2006-11-13 22:53:19 +0000 | [diff] [blame] | 884 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
Devang Patel | 3f0832a | 2006-11-14 02:54:23 +0000 | [diff] [blame] | 885 | e = PassManagers.end(); !P && itr != e; ++itr) |
| 886 | P = (*itr)->getAnalysisPassFromManager(AID); |
| 887 | return P; |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 890 | // PassManager_New implementation |
| 891 | /// Add P into active pass manager or use new module pass manager to |
| 892 | /// manage it. |
Devang Patel | 1a6eaa4 | 2006-11-11 02:22:31 +0000 | [diff] [blame] | 893 | bool PassManagerImpl_New::addPass(Pass *P) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 894 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 895 | if (!activeManager || !activeManager->addPass(P)) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 896 | activeManager = new ModulePassManager_New(); |
| 897 | PassManagers.push_back(activeManager); |
Devang Patel | 28bbcbe | 2006-12-07 21:44:12 +0000 | [diff] [blame] | 898 | return activeManager->addPass(P); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 899 | } |
Devang Patel | 28bbcbe | 2006-12-07 21:44:12 +0000 | [diff] [blame] | 900 | return true; |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 904 | /// 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] | 905 | bool PassManagerImpl_New::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 906 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 907 | bool Changed = false; |
| 908 | for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(), |
| 909 | e = PassManagers.end(); itr != e; ++itr) { |
| 910 | ModulePassManager_New *pm = *itr; |
| 911 | Changed |= pm->runOnModule(M); |
| 912 | } |
| 913 | return Changed; |
| 914 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 915 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 916 | //===----------------------------------------------------------------------===// |
| 917 | // PassManager implementation |
| 918 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 919 | /// Create new pass manager |
| 920 | PassManager_New::PassManager_New() { |
| 921 | PM = new PassManagerImpl_New(); |
| 922 | } |
| 923 | |
| 924 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 925 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 926 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 927 | /// implies that all passes MUST be allocated with 'new'. |
| 928 | void |
| 929 | PassManager_New::add(Pass *P) { |
| 930 | PM->add(P); |
| 931 | } |
| 932 | |
| 933 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 934 | /// whether any of the passes modifies the module, and if so, return true. |
| 935 | bool |
| 936 | PassManager_New::run(Module &M) { |
| 937 | return PM->run(M); |
| 938 | } |
| 939 | |