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