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