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