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 | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Timer.h" |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 19 | #include "llvm/ModuleProvider.h" |
Bill Wendling | dfc9189 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Streams.h" |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 22 | #include <vector> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 23 | #include <map> |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 24 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 26 | class llvm::PMDataManager; |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 27 | |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Overview: |
| 30 | // The Pass Manager Infrastructure manages passes. It's responsibilities are: |
| 31 | // |
| 32 | // o Manage optimization pass execution order |
| 33 | // o Make required Analysis information available before pass P is run |
| 34 | // o Release memory occupied by dead passes |
| 35 | // o If Analysis information is dirtied by a pass then regenerate Analysis |
| 36 | // information before it is consumed by another pass. |
| 37 | // |
Chris Lattner | ce22ca3 | 2006-12-14 18:22:14 +0000 | [diff] [blame] | 38 | // Pass Manager Infrastructure uses multiple pass managers. They are |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 39 | // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager. |
Chris Lattner | ce22ca3 | 2006-12-14 18:22:14 +0000 | [diff] [blame] | 40 | // This class hierarcy uses multiple inheritance but pass managers do not derive |
| 41 | // from another pass manager. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 42 | // |
Chris Lattner | ce22ca3 | 2006-12-14 18:22:14 +0000 | [diff] [blame] | 43 | // PassManager and FunctionPassManager are two top-level pass manager that |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 44 | // represents the external interface of this entire pass manager infrastucture. |
| 45 | // |
| 46 | // Important classes : |
| 47 | // |
| 48 | // [o] class PMTopLevelManager; |
| 49 | // |
| 50 | // Two top level managers, PassManager and FunctionPassManager, derive from |
| 51 | // PMTopLevelManager. PMTopLevelManager manages information used by top level |
| 52 | // managers such as last user info. |
| 53 | // |
| 54 | // [o] class PMDataManager; |
| 55 | // |
| 56 | // PMDataManager manages information, e.g. list of available analysis info, |
| 57 | // used by a pass manager to manage execution order of passes. It also provides |
| 58 | // a place to implement common pass manager APIs. All pass managers derive from |
| 59 | // PMDataManager. |
| 60 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 61 | // [o] class BBPassManager : public FunctionPass, public PMDataManager; |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 62 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 63 | // BBPassManager manages BasicBlockPasses. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 64 | // |
| 65 | // [o] class FunctionPassManager; |
| 66 | // |
| 67 | // This is a external interface used by JIT to manage FunctionPasses. This |
| 68 | // interface relies on FunctionPassManagerImpl to do all the tasks. |
| 69 | // |
| 70 | // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager, |
| 71 | // public PMTopLevelManager; |
| 72 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 73 | // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 74 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 75 | // [o] class FPPassManager : public ModulePass, public PMDataManager; |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 76 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 77 | // FPPassManager manages FunctionPasses and BBPassManagers |
| 78 | // |
| 79 | // [o] class MPPassManager : public Pass, public PMDataManager; |
| 80 | // |
| 81 | // MPPassManager manages ModulePasses and FPPassManagers |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 82 | // |
| 83 | // [o] class PassManager; |
| 84 | // |
| 85 | // This is a external interface used by various tools to manages passes. It |
| 86 | // relies on PassManagerImpl to do all the tasks. |
| 87 | // |
| 88 | // [o] class PassManagerImpl : public Pass, public PMDataManager, |
| 89 | // public PMDTopLevelManager |
| 90 | // |
| 91 | // PassManagerImpl is a top level pass manager responsible for managing |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 92 | // MPPassManagers. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 93 | //===----------------------------------------------------------------------===// |
| 94 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 95 | namespace llvm { |
| 96 | |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | // Pass debugging information. Often it is useful to find out what pass is |
| 99 | // running when a crash occurs in a utility. When this library is compiled with |
| 100 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 101 | // pass name to be printed before it executes. |
| 102 | // |
| 103 | |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 104 | // Different debug levels that can be enabled... |
| 105 | enum PassDebugLevel { |
| 106 | None, Arguments, Structure, Executions, Details |
| 107 | }; |
| 108 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 109 | static cl::opt<enum PassDebugLevel> |
| 110 | PassDebugging_New("debug-pass", cl::Hidden, |
| 111 | cl::desc("Print PassManager debugging information"), |
| 112 | cl::values( |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 113 | clEnumVal(None , "disable debug output"), |
| 114 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
| 115 | clEnumVal(Structure , "print pass structure before run()"), |
| 116 | clEnumVal(Executions, "print pass name before it is executed"), |
| 117 | clEnumVal(Details , "print pass details when it is executed"), |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 118 | clEnumValEnd)); |
| 119 | } // End of llvm namespace |
| 120 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 121 | #ifndef USE_OLD_PASSMANAGER |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 122 | namespace { |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 123 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 124 | //===----------------------------------------------------------------------===// |
| 125 | // PMTopLevelManager |
| 126 | // |
| 127 | /// PMTopLevelManager manages LastUser info and collects common APIs used by |
| 128 | /// top level pass managers. |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 129 | class VISIBILITY_HIDDEN PMTopLevelManager { |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 130 | public: |
| 131 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 132 | virtual unsigned getNumContainedManagers() { |
| 133 | return PassManagers.size(); |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 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 | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 156 | virtual ~PMTopLevelManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 157 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 158 | E = PassManagers.end(); I != E; ++I) |
| 159 | delete *I; |
| 160 | |
| 161 | for (std::vector<ImmutablePass *>::iterator |
| 162 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 163 | delete *I; |
| 164 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 165 | PassManagers.clear(); |
| 166 | } |
| 167 | |
Devang Patel | e0eb9d8 | 2006-12-07 20:51:18 +0000 | [diff] [blame] | 168 | /// Add immutable pass and initialize it. |
| 169 | inline void addImmutablePass(ImmutablePass *P) { |
| 170 | P->initializePass(); |
| 171 | ImmutablePasses.push_back(P); |
| 172 | } |
| 173 | |
| 174 | inline std::vector<ImmutablePass *>& getImmutablePasses() { |
| 175 | return ImmutablePasses; |
| 176 | } |
| 177 | |
Devang Patel | 5bbeb49 | 2006-12-08 22:47:25 +0000 | [diff] [blame] | 178 | void addPassManager(Pass *Manager) { |
| 179 | PassManagers.push_back(Manager); |
| 180 | } |
| 181 | |
Devang Patel | af1fca5 | 2006-12-08 23:11:43 +0000 | [diff] [blame] | 182 | // Add Manager into the list of managers that are not directly |
| 183 | // maintained by this top level pass manager |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 184 | inline void addIndirectPassManager(PMDataManager *Manager) { |
| 185 | IndirectPassManagers.push_back(Manager); |
Devang Patel | af1fca5 | 2006-12-08 23:11:43 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 188 | // Print passes managed by this top level manager. |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 189 | void dumpPasses() const; |
| 190 | void dumpArguments() const; |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 191 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame^] | 192 | void initializeAllAnalysisInfo(); |
| 193 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 194 | protected: |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 195 | |
| 196 | /// Collection of pass managers |
| 197 | std::vector<Pass *> PassManagers; |
| 198 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 199 | private: |
| 200 | |
Devang Patel | af1fca5 | 2006-12-08 23:11:43 +0000 | [diff] [blame] | 201 | /// Collection of pass managers that are not directly maintained |
| 202 | /// by this pass manager |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 203 | std::vector<PMDataManager *> IndirectPassManagers; |
Devang Patel | af1fca5 | 2006-12-08 23:11:43 +0000 | [diff] [blame] | 204 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 205 | // Map to keep track of last user of the analysis pass. |
| 206 | // LastUser->second is the last user of Lastuser->first. |
| 207 | std::map<Pass *, Pass *> LastUser; |
Devang Patel | e0eb9d8 | 2006-12-07 20:51:18 +0000 | [diff] [blame] | 208 | |
| 209 | /// Immutable passes are managed by top level manager. |
| 210 | std::vector<ImmutablePass *> ImmutablePasses; |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 211 | }; |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 212 | |
| 213 | } // End of anon namespace |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 214 | |
Devang Patel | f3827bc | 2006-12-07 19:54:15 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 216 | // PMDataManager |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 217 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 218 | namespace llvm { |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 219 | /// PMDataManager provides the common place to manage the analysis data |
| 220 | /// used by pass managers. |
| 221 | class PMDataManager { |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 222 | public: |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 223 | PMDataManager(int Depth) : TPM(NULL), Depth(Depth) { |
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 | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 247 | void removeDeadPasses(Pass *P, std::string &Msg); |
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. |
Chris Lattner | ce22ca3 | 2006-12-14 18:22:14 +0000 | [diff] [blame] | 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 | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 255 | TransferLastUses.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 | f3827bc | 2006-12-07 19:54:15 +0000 | [diff] [blame] | 274 | // Access toplevel manager |
| 275 | PMTopLevelManager *getTopLevelManager() { return TPM; } |
| 276 | void setTopLevelManager(PMTopLevelManager *T) { TPM = T; } |
| 277 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 278 | unsigned getDepth() const { return Depth; } |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 279 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 280 | // Print routines used by debug-pass |
| 281 | void dumpLastUses(Pass *P, unsigned Offset) const; |
| 282 | void dumpPassArguments() const; |
| 283 | void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const; |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 284 | void dumpAnalysisSetInfo(const char *Msg, Pass *P, |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 285 | const std::vector<AnalysisID> &Set) const; |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 286 | |
| 287 | std::vector<Pass *>& getTransferredLastUses() { |
| 288 | return TransferLastUses; |
| 289 | } |
| 290 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 291 | virtual unsigned getNumContainedPasses() { |
| 292 | return PassVector.size(); |
| 293 | } |
| 294 | |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 295 | protected: |
| 296 | |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 297 | // If a FunctionPass F is the last user of ModulePass info M |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 298 | // then the F's manager, not F, records itself as a last user of M. |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 299 | // Current pass manage is requesting parent manager to record parent |
| 300 | // manager as the last user of these TrransferLastUses passes. |
| 301 | std::vector<Pass *> TransferLastUses; |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 302 | |
| 303 | // Top level manager. |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 304 | PMTopLevelManager *TPM; |
| 305 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 306 | // Collection of pass that are managed by this manager |
| 307 | std::vector<Pass *> PassVector; |
| 308 | |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 309 | private: |
Devang Patel | dafa4dd | 2006-11-14 00:03:04 +0000 | [diff] [blame] | 310 | // Set of available Analysis. This information is used while scheduling |
| 311 | // pass. If a pass requires an analysis which is not not available then |
| 312 | // equired analysis pass is scheduled to run before the pass itself is |
| 313 | // scheduled to run. |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 314 | std::map<AnalysisID, Pass*> AvailableAnalysis; |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 315 | |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 316 | unsigned Depth; |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 319 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 320 | // BBPassManager |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 321 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 322 | /// BBPassManager manages BasicBlockPass. It batches all the |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 323 | /// pass together and sequence them to process one basic block before |
| 324 | /// processing next basic block. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 325 | class VISIBILITY_HIDDEN BBPassManager : public PMDataManager, |
| 326 | public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 327 | |
| 328 | public: |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 329 | BBPassManager(int Depth) : PMDataManager(Depth) { } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 330 | |
| 331 | /// Add a pass into a passmanager queue. |
| 332 | bool addPass(Pass *p); |
| 333 | |
| 334 | /// Execute all of the passes scheduled for execution. Keep track of |
| 335 | /// whether any of the passes modifies the function, and if so, return true. |
| 336 | bool runOnFunction(Function &F); |
| 337 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 338 | /// Pass Manager itself does not invalidate any analysis info. |
| 339 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 340 | Info.setPreservesAll(); |
| 341 | } |
| 342 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 343 | bool doInitialization(Module &M); |
| 344 | bool doInitialization(Function &F); |
| 345 | bool doFinalization(Module &M); |
| 346 | bool doFinalization(Function &F); |
| 347 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 348 | // Print passes managed by this manager |
| 349 | void dumpPassStructure(unsigned Offset) { |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 350 | llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 351 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 352 | BasicBlockPass *BP = getContainedPass(Index); |
| 353 | BP->dumpPassStructure(Offset + 1); |
| 354 | dumpLastUses(BP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 357 | |
| 358 | BasicBlockPass *getContainedPass(unsigned N) { |
| 359 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 360 | BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); |
| 361 | return BP; |
| 362 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 365 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 366 | // FPPassManager |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 367 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 368 | /// FPPassManager manages BBPassManagers and FunctionPasses. |
| 369 | /// It batches all function passes and basic block pass managers together and |
| 370 | /// sequence them to process one function at a time before processing next |
| 371 | /// function. |
| 372 | |
| 373 | class FPPassManager : public ModulePass, public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 374 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 375 | public: |
| 376 | FPPassManager(int Depth) : PMDataManager(Depth) { |
| 377 | activeBBPassManager = NULL; |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 378 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 379 | |
| 380 | /// Add a pass into a passmanager queue. |
| 381 | bool addPass(Pass *p); |
| 382 | |
| 383 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 384 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 385 | bool runOnFunction(Function &F); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 386 | bool runOnModule(Module &M); |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 387 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 388 | /// doInitialization - Run all of the initializers for the function passes. |
| 389 | /// |
| 390 | bool doInitialization(Module &M); |
| 391 | |
| 392 | /// doFinalization - Run all of the initializers for the function passes. |
| 393 | /// |
| 394 | bool doFinalization(Module &M); |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 395 | |
| 396 | /// Pass Manager itself does not invalidate any analysis info. |
| 397 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 398 | Info.setPreservesAll(); |
| 399 | } |
| 400 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 401 | // Print passes managed by this manager |
| 402 | void dumpPassStructure(unsigned Offset) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 403 | llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 404 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 405 | FunctionPass *FP = getContainedPass(Index); |
| 406 | FP->dumpPassStructure(Offset + 1); |
| 407 | dumpLastUses(FP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 411 | FunctionPass *getContainedPass(unsigned N) { |
| 412 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 413 | FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]); |
| 414 | return FP; |
| 415 | } |
| 416 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 417 | private: |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 418 | // Active Pass Manager |
| 419 | BBPassManager *activeBBPassManager; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 420 | }; |
| 421 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 422 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 423 | // FunctionPassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 424 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 425 | /// FunctionPassManagerImpl manages FPPassManagers |
| 426 | class FunctionPassManagerImpl : public Pass, |
| 427 | public PMDataManager, |
| 428 | public PMTopLevelManager { |
| 429 | |
| 430 | public: |
| 431 | |
| 432 | FunctionPassManagerImpl(int Depth) : PMDataManager(Depth) { |
| 433 | activeManager = NULL; |
| 434 | } |
| 435 | |
| 436 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 437 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 438 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 439 | /// implies that all passes MUST be allocated with 'new'. |
| 440 | void add(Pass *P) { |
| 441 | schedulePass(P); |
| 442 | } |
| 443 | |
| 444 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 445 | /// whether any of the passes modifies the module, and if so, return true. |
| 446 | bool run(Function &F); |
| 447 | |
| 448 | /// doInitialization - Run all of the initializers for the function passes. |
| 449 | /// |
| 450 | bool doInitialization(Module &M); |
| 451 | |
| 452 | /// doFinalization - Run all of the initializers for the function passes. |
| 453 | /// |
| 454 | bool doFinalization(Module &M); |
| 455 | |
| 456 | /// Pass Manager itself does not invalidate any analysis info. |
| 457 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 458 | Info.setPreservesAll(); |
| 459 | } |
| 460 | |
| 461 | inline void addTopLevelPass(Pass *P) { |
| 462 | |
| 463 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
| 464 | |
| 465 | // P is a immutable pass and it will be managed by this |
| 466 | // top level manager. Set up analysis resolver to connect them. |
| 467 | AnalysisResolver_New *AR = new AnalysisResolver_New(*this); |
| 468 | P->setResolver(AR); |
| 469 | initializeAnalysisImpl(P); |
| 470 | addImmutablePass(IP); |
| 471 | recordAvailableAnalysis(IP); |
| 472 | } |
| 473 | else |
| 474 | addPass(P); |
| 475 | } |
| 476 | |
| 477 | FPPassManager *getContainedManager(unsigned N) { |
| 478 | assert ( N < PassManagers.size() && "Pass number out of range!"); |
| 479 | FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); |
| 480 | return FP; |
| 481 | } |
| 482 | |
| 483 | /// Add a pass into a passmanager queue. |
| 484 | bool addPass(Pass *p); |
| 485 | |
| 486 | private: |
| 487 | |
| 488 | // Active Pass Manager |
| 489 | FPPassManager *activeManager; |
| 490 | }; |
| 491 | |
| 492 | //===----------------------------------------------------------------------===// |
| 493 | // MPPassManager |
| 494 | // |
| 495 | /// MPPassManager manages ModulePasses and function pass managers. |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 496 | /// It batches all Module passes passes and function pass managers together and |
| 497 | /// sequence them to process one module. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 498 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 499 | |
| 500 | public: |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 501 | MPPassManager(int Depth) : PMDataManager(Depth) { |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 502 | activeFunctionPassManager = NULL; |
| 503 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 504 | |
| 505 | /// Add a pass into a passmanager queue. |
| 506 | bool addPass(Pass *p); |
| 507 | |
| 508 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 509 | /// whether any of the passes modifies the module, and if so, return true. |
| 510 | bool runOnModule(Module &M); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 511 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 512 | /// Pass Manager itself does not invalidate any analysis info. |
| 513 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 514 | Info.setPreservesAll(); |
| 515 | } |
| 516 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 517 | // Print passes managed by this manager |
| 518 | void dumpPassStructure(unsigned Offset) { |
| 519 | llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 520 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 521 | ModulePass *MP = getContainedPass(Index); |
| 522 | MP->dumpPassStructure(Offset + 1); |
| 523 | dumpLastUses(MP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 527 | ModulePass *getContainedPass(unsigned N) { |
| 528 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 529 | ModulePass *MP = static_cast<ModulePass *>(PassVector[N]); |
| 530 | return MP; |
| 531 | } |
| 532 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 533 | private: |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 534 | // Active Pass Manager |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 535 | FPPassManager *activeFunctionPassManager; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 538 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 539 | // PassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 540 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 541 | /// PassManagerImpl manages MPPassManagers |
| 542 | class PassManagerImpl : public Pass, |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 543 | public PMDataManager, |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 544 | public PMTopLevelManager { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 545 | |
| 546 | public: |
| 547 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 548 | PassManagerImpl(int Depth) : PMDataManager(Depth) { |
Devang Patel | ad6b7fe | 2006-12-12 22:57:43 +0000 | [diff] [blame] | 549 | activeManager = NULL; |
| 550 | } |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 551 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 552 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 553 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 554 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 555 | /// implies that all passes MUST be allocated with 'new'. |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 556 | void add(Pass *P) { |
Devang Patel | df6c9ae | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 557 | schedulePass(P); |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 558 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 559 | |
| 560 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 561 | /// whether any of the passes modifies the module, and if so, return true. |
| 562 | bool run(Module &M); |
| 563 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 564 | /// Pass Manager itself does not invalidate any analysis info. |
| 565 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 566 | Info.setPreservesAll(); |
| 567 | } |
| 568 | |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 569 | inline void addTopLevelPass(Pass *P) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 570 | |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 571 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 572 | |
| 573 | // P is a immutable pass and it will be managed by this |
| 574 | // top level manager. Set up analysis resolver to connect them. |
| 575 | AnalysisResolver_New *AR = new AnalysisResolver_New(*this); |
| 576 | P->setResolver(AR); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 577 | initializeAnalysisImpl(P); |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 578 | addImmutablePass(IP); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 579 | recordAvailableAnalysis(IP); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 580 | } |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 581 | else |
| 582 | addPass(P); |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 585 | MPPassManager *getContainedManager(unsigned N) { |
| 586 | assert ( N < PassManagers.size() && "Pass number out of range!"); |
| 587 | MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); |
| 588 | return MP; |
| 589 | } |
| 590 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 591 | private: |
| 592 | |
Devang Patel | de12418 | 2006-12-07 21:10:57 +0000 | [diff] [blame] | 593 | /// Add a pass into a passmanager queue. |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 594 | bool addPass(Pass *p); |
| 595 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 596 | // Active Pass Manager |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 597 | MPPassManager *activeManager; |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 598 | }; |
| 599 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 600 | } // End of llvm namespace |
| 601 | |
| 602 | namespace { |
| 603 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 604 | //===----------------------------------------------------------------------===// |
| 605 | // TimingInfo Class - This class is used to calculate information about the |
| 606 | // amount of time each pass takes to execute. This only happens when |
| 607 | // -time-passes is enabled on the command line. |
| 608 | // |
| 609 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 610 | class VISIBILITY_HIDDEN TimingInfo { |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 611 | std::map<Pass*, Timer> TimingData; |
| 612 | TimerGroup TG; |
| 613 | |
| 614 | public: |
| 615 | // Use 'create' member to get this. |
| 616 | TimingInfo() : TG("... Pass execution timing report ...") {} |
| 617 | |
| 618 | // TimingDtor - Print out information about timing information |
| 619 | ~TimingInfo() { |
| 620 | // Delete all of the timers... |
| 621 | TimingData.clear(); |
| 622 | // TimerGroup is deleted next, printing the report. |
| 623 | } |
| 624 | |
| 625 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer |
| 626 | // to a non null value (if the -time-passes option is enabled) or it leaves it |
| 627 | // null. It may be called multiple times. |
| 628 | static void createTheTimeInfo(); |
| 629 | |
| 630 | void passStarted(Pass *P) { |
| 631 | |
| 632 | if (dynamic_cast<PMDataManager *>(P)) |
| 633 | return; |
| 634 | |
| 635 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 636 | if (I == TimingData.end()) |
| 637 | I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first; |
| 638 | I->second.startTimer(); |
| 639 | } |
| 640 | void passEnded(Pass *P) { |
| 641 | |
| 642 | if (dynamic_cast<PMDataManager *>(P)) |
| 643 | return; |
| 644 | |
| 645 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 646 | assert (I != TimingData.end() && "passStarted/passEnded not nested right!"); |
| 647 | I->second.stopTimer(); |
| 648 | } |
| 649 | }; |
| 650 | |
| 651 | static TimingInfo *TheTimeInfo; |
| 652 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 653 | } // End of anon namespace |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 654 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 655 | //===----------------------------------------------------------------------===// |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 656 | // PMTopLevelManager implementation |
| 657 | |
| 658 | /// Set pass P as the last user of the given analysis passes. |
| 659 | void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses, |
| 660 | Pass *P) { |
| 661 | |
| 662 | for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(), |
| 663 | E = AnalysisPasses.end(); I != E; ++I) { |
| 664 | Pass *AP = *I; |
| 665 | LastUser[AP] = P; |
| 666 | // If AP is the last user of other passes then make P last user of |
| 667 | // such passes. |
| 668 | for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
| 669 | LUE = LastUser.end(); LUI != LUE; ++LUI) { |
| 670 | if (LUI->second == AP) |
| 671 | LastUser[LUI->first] = P; |
| 672 | } |
| 673 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /// Collect passes whose last user is P |
| 677 | void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses, |
| 678 | Pass *P) { |
| 679 | for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
| 680 | LUE = LastUser.end(); LUI != LUE; ++LUI) |
| 681 | if (LUI->second == P) |
| 682 | LastUses.push_back(LUI->first); |
| 683 | } |
| 684 | |
| 685 | /// Schedule pass P for execution. Make sure that passes required by |
| 686 | /// P are run before P is run. Update analysis info maintained by |
| 687 | /// the manager. Remove dead passes. This is a recursive function. |
| 688 | void PMTopLevelManager::schedulePass(Pass *P) { |
| 689 | |
| 690 | // TODO : Allocate function manager for this pass, other wise required set |
| 691 | // may be inserted into previous function manager |
| 692 | |
| 693 | AnalysisUsage AnUsage; |
| 694 | P->getAnalysisUsage(AnUsage); |
| 695 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 696 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 697 | E = RequiredSet.end(); I != E; ++I) { |
| 698 | |
| 699 | Pass *AnalysisPass = findAnalysisPass(*I); |
| 700 | if (!AnalysisPass) { |
| 701 | // Schedule this analysis run first. |
| 702 | AnalysisPass = (*I)->createPass(); |
| 703 | schedulePass(AnalysisPass); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | // Now all required passes are available. |
| 708 | addTopLevelPass(P); |
| 709 | } |
| 710 | |
| 711 | /// Find the pass that implements Analysis AID. Search immutable |
| 712 | /// passes and all pass managers. If desired pass is not found |
| 713 | /// then return NULL. |
| 714 | Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { |
| 715 | |
| 716 | Pass *P = NULL; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 717 | // Check pass managers |
| 718 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 719 | E = PassManagers.end(); P == NULL && I != E; ++I) { |
| 720 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 721 | assert(PMD && "This is not a PassManager"); |
| 722 | P = PMD->findAnalysisPass(AID, false); |
| 723 | } |
| 724 | |
| 725 | // Check other pass managers |
| 726 | for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), |
| 727 | E = IndirectPassManagers.end(); P == NULL && I != E; ++I) |
| 728 | P = (*I)->findAnalysisPass(AID, false); |
| 729 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 730 | for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(), |
| 731 | E = ImmutablePasses.end(); P == NULL && I != E; ++I) { |
| 732 | const PassInfo *PI = (*I)->getPassInfo(); |
| 733 | if (PI == AID) |
| 734 | P = *I; |
| 735 | |
| 736 | // If Pass not found then check the interfaces implemented by Immutable Pass |
| 737 | if (!P) { |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 738 | const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented(); |
| 739 | if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end()) |
| 740 | P = *I; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 744 | return P; |
| 745 | } |
| 746 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 747 | // Print passes managed by this top level manager. |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 748 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 749 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 750 | if (PassDebugging_New < Structure) |
| 751 | return; |
| 752 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 753 | // Print out the immutable passes |
| 754 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 755 | ImmutablePasses[i]->dumpPassStructure(0); |
| 756 | } |
| 757 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 758 | for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 759 | E = PassManagers.end(); I != E; ++I) |
| 760 | (*I)->dumpPassStructure(1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 763 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 764 | |
| 765 | if (PassDebugging_New < Arguments) |
| 766 | return; |
| 767 | |
| 768 | cerr << "Pass Arguments: "; |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 769 | for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 770 | E = PassManagers.end(); I != E; ++I) { |
| 771 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 772 | assert(PMD && "This is not a PassManager"); |
| 773 | PMD->dumpPassArguments(); |
| 774 | } |
| 775 | cerr << "\n"; |
| 776 | } |
| 777 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame^] | 778 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
| 779 | |
| 780 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 781 | E = PassManagers.end(); I != E; ++I) { |
| 782 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 783 | assert(PMD && "This is not a PassManager"); |
| 784 | PMD->initializeAnalysisInfo(); |
| 785 | } |
| 786 | |
| 787 | // Initailize other pass managers |
| 788 | for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), |
| 789 | E = IndirectPassManagers.end(); I != E; ++I) |
| 790 | (*I)->initializeAnalysisInfo(); |
| 791 | } |
| 792 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 793 | //===----------------------------------------------------------------------===// |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 794 | // PMDataManager implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 795 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 796 | /// 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] | 797 | /// manager. |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 798 | bool PMDataManager::manageablePass(Pass *P) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 799 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 800 | // TODO |
| 801 | // If this pass is not preserving information that is required by a |
| 802 | // pass maintained by higher level pass manager then do not insert |
| 803 | // this pass into current manager. Use new manager. For example, |
| 804 | // For example, If FunctionPass F is not preserving ModulePass Info M1 |
| 805 | // that is used by another ModulePass M2 then do not insert F in |
| 806 | // current function pass manager. |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 807 | return true; |
| 808 | } |
| 809 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 810 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 811 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 812 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 813 | if (const PassInfo *PI = P->getPassInfo()) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 814 | AvailableAnalysis[PI] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 815 | |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 816 | //This pass is the current implementation of all of the interfaces it |
| 817 | //implements as well. |
| 818 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 819 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 820 | AvailableAnalysis[II[i]] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 824 | /// Remove Analyss not preserved by Pass P |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 825 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 826 | AnalysisUsage AnUsage; |
| 827 | P->getAnalysisUsage(AnUsage); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 828 | |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 829 | if (AnUsage.getPreservesAll()) |
| 830 | return; |
| 831 | |
| 832 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 833 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | be6bd55e | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 834 | E = AvailableAnalysis.end(); I != E; ) { |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 835 | std::map<AnalysisID, Pass*>::iterator Info = I++; |
| 836 | if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 837 | PreservedSet.end()) { |
| 838 | // Remove this analysis |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 839 | if (!dynamic_cast<ImmutablePass*>(Info->second)) |
| 840 | AvailableAnalysis.erase(Info); |
| 841 | } |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 842 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 845 | /// Remove analysis passes that are not used any longer |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 846 | void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) { |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 847 | |
| 848 | std::vector<Pass *> DeadPasses; |
| 849 | TPM->collectLastUses(DeadPasses, P); |
| 850 | |
| 851 | for (std::vector<Pass *>::iterator I = DeadPasses.begin(), |
| 852 | E = DeadPasses.end(); I != E; ++I) { |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 853 | |
| 854 | std::string Msg1 = " Freeing Pass '"; |
| 855 | dumpPassInfo(*I, Msg1, Msg); |
| 856 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 857 | if (TheTimeInfo) TheTimeInfo->passStarted(P); |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 858 | (*I)->releaseMemory(); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 859 | if (TheTimeInfo) TheTimeInfo->passEnded(P); |
| 860 | |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 861 | std::map<AnalysisID, Pass*>::iterator Pos = |
| 862 | AvailableAnalysis.find((*I)->getPassInfo()); |
| 863 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 864 | // It is possible that pass is already removed from the AvailableAnalysis |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 865 | if (Pos != AvailableAnalysis.end()) |
| 866 | AvailableAnalysis.erase(Pos); |
| 867 | } |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 870 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 871 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 872 | void PMDataManager::addPassToManager(Pass *P, |
| 873 | bool ProcessAnalysis) { |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 874 | |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 875 | // This manager is going to manage pass P. Set up analysis resolver |
| 876 | // to connect them. |
| 877 | AnalysisResolver_New *AR = new AnalysisResolver_New(*this); |
| 878 | P->setResolver(AR); |
| 879 | |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 880 | if (ProcessAnalysis) { |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 881 | |
| 882 | // At the moment, this pass is the last user of all required passes. |
| 883 | std::vector<Pass *> LastUses; |
| 884 | std::vector<Pass *> RequiredPasses; |
| 885 | unsigned PDepth = this->getDepth(); |
| 886 | |
| 887 | collectRequiredAnalysisPasses(RequiredPasses, P); |
| 888 | for (std::vector<Pass *>::iterator I = RequiredPasses.begin(), |
| 889 | E = RequiredPasses.end(); I != E; ++I) { |
| 890 | Pass *PRequired = *I; |
| 891 | unsigned RDepth = 0; |
Devang Patel | 64619be | 2006-12-09 00:07:38 +0000 | [diff] [blame] | 892 | |
| 893 | PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); |
| 894 | RDepth = DM.getDepth(); |
| 895 | |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 896 | if (PDepth == RDepth) |
| 897 | LastUses.push_back(PRequired); |
| 898 | else if (PDepth > RDepth) { |
| 899 | // Let the parent claim responsibility of last use |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 900 | TransferLastUses.push_back(PRequired); |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 901 | } else { |
| 902 | // Note : This feature is not yet implemented |
| 903 | assert (0 && |
| 904 | "Unable to handle Pass that requires lower level Analysis pass"); |
| 905 | } |
| 906 | } |
| 907 | |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 908 | LastUses.push_back(P); |
| 909 | TPM->setLastUser(LastUses, P); |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 910 | |
Devang Patel | 17bff0d | 2006-12-07 22:09:36 +0000 | [diff] [blame] | 911 | // Take a note of analysis required and made available by this pass. |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 912 | // Remove the analysis not preserved by this pass |
| 913 | removeNotPreservedAnalysis(P); |
Devang Patel | 17bff0d | 2006-12-07 22:09:36 +0000 | [diff] [blame] | 914 | recordAvailableAnalysis(P); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 915 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 916 | |
| 917 | // Add pass |
| 918 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 921 | /// Populate RequiredPasses with the analysis pass that are required by |
| 922 | /// pass P. |
| 923 | void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP, |
| 924 | Pass *P) { |
| 925 | AnalysisUsage AnUsage; |
| 926 | P->getAnalysisUsage(AnUsage); |
| 927 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 928 | for (std::vector<AnalysisID>::const_iterator |
| 929 | I = RequiredSet.begin(), E = RequiredSet.end(); |
| 930 | I != E; ++I) { |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 931 | Pass *AnalysisPass = findAnalysisPass(*I, true); |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 932 | assert (AnalysisPass && "Analysis pass is not available"); |
| 933 | RP.push_back(AnalysisPass); |
| 934 | } |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 935 | |
| 936 | const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); |
| 937 | for (std::vector<AnalysisID>::const_iterator I = IDs.begin(), |
| 938 | E = IDs.end(); I != E; ++I) { |
| 939 | Pass *AnalysisPass = findAnalysisPass(*I, true); |
| 940 | assert (AnalysisPass && "Analysis pass is not available"); |
| 941 | RP.push_back(AnalysisPass); |
| 942 | } |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 945 | // All Required analyses should be available to the pass as it runs! Here |
| 946 | // we fill in the AnalysisImpls member of the pass so that it can |
| 947 | // successfully use the getAnalysis() method to retrieve the |
| 948 | // implementations it needs. |
| 949 | // |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 950 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 951 | AnalysisUsage AnUsage; |
| 952 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 953 | |
| 954 | for (std::vector<const PassInfo *>::const_iterator |
| 955 | I = AnUsage.getRequiredSet().begin(), |
| 956 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 957 | Pass *Impl = findAnalysisPass(*I, true); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 958 | if (Impl == 0) |
| 959 | assert(0 && "Analysis used but not available!"); |
Devang Patel | 984698a | 2006-12-09 01:11:34 +0000 | [diff] [blame] | 960 | AnalysisResolver_New *AR = P->getResolver(); |
| 961 | AR->addAnalysisImplsPair(*I, Impl); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 965 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 966 | /// then return NULL. |
| 967 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 968 | |
| 969 | // Check if AvailableAnalysis map has one entry. |
| 970 | std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
| 971 | |
| 972 | if (I != AvailableAnalysis.end()) |
| 973 | return I->second; |
| 974 | |
| 975 | // Search Parents through TopLevelManager |
| 976 | if (SearchParent) |
| 977 | return TPM->findAnalysisPass(AID); |
| 978 | |
Devang Patel | 9d759b8 | 2006-12-09 00:09:12 +0000 | [diff] [blame] | 979 | return NULL; |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 982 | // Print list of passes that are last used by P. |
| 983 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
| 984 | |
| 985 | std::vector<Pass *> LUses; |
| 986 | |
| 987 | assert (TPM && "Top Level Manager is missing"); |
| 988 | TPM->collectLastUses(LUses, P); |
| 989 | |
| 990 | for (std::vector<Pass *>::iterator I = LUses.begin(), |
| 991 | E = LUses.end(); I != E; ++I) { |
| 992 | llvm::cerr << "--" << std::string(Offset*2, ' '); |
| 993 | (*I)->dumpPassStructure(0); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | void PMDataManager::dumpPassArguments() const { |
| 998 | for(std::vector<Pass *>::const_iterator I = PassVector.begin(), |
| 999 | E = PassVector.end(); I != E; ++I) { |
| 1000 | if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I)) |
| 1001 | PMD->dumpPassArguments(); |
| 1002 | else |
| 1003 | if (const PassInfo *PI = (*I)->getPassInfo()) |
| 1004 | if (!PI->isAnalysisGroup()) |
| 1005 | cerr << " -" << PI->getPassArgument(); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1, |
| 1010 | std::string &Msg2) const { |
| 1011 | if (PassDebugging_New < Executions) |
| 1012 | return; |
| 1013 | cerr << (void*)this << std::string(getDepth()*2+1, ' '); |
| 1014 | cerr << Msg1; |
| 1015 | cerr << P->getPassName(); |
| 1016 | cerr << Msg2; |
| 1017 | } |
| 1018 | |
| 1019 | void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P, |
| 1020 | const std::vector<AnalysisID> &Set) |
| 1021 | const { |
| 1022 | if (PassDebugging_New >= Details && !Set.empty()) { |
| 1023 | cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
| 1024 | for (unsigned i = 0; i != Set.size(); ++i) { |
| 1025 | if (i) cerr << ","; |
| 1026 | cerr << " " << Set[i]->getPassName(); |
| 1027 | } |
| 1028 | cerr << "\n"; |
| 1029 | } |
| 1030 | } |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1031 | |
| 1032 | //===----------------------------------------------------------------------===// |
| 1033 | // NOTE: Is this the right place to define this method ? |
| 1034 | // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist |
| 1035 | Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const { |
| 1036 | return PM.findAnalysisPass(ID, dir); |
| 1037 | } |
| 1038 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1039 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1040 | // BBPassManager implementation |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1041 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 1042 | /// Add pass P into PassVector and return true. If this pass is not |
| 1043 | /// manageable by this manager then return false. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1044 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1045 | BBPassManager::addPass(Pass *P) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1046 | |
| 1047 | BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P); |
| 1048 | if (!BP) |
| 1049 | return false; |
| 1050 | |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 1051 | // If this pass does not preserve analysis that is used by other passes |
| 1052 | // managed by this manager than it is not a suitable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 1053 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 1054 | return false; |
| 1055 | |
Chris Lattner | ce22ca3 | 2006-12-14 18:22:14 +0000 | [diff] [blame] | 1056 | addPassToManager(BP); |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 1057 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1058 | return true; |
| 1059 | } |
| 1060 | |
| 1061 | /// Execute all of the passes scheduled for execution by invoking |
| 1062 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 1063 | /// the function, and if so, return true. |
| 1064 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1065 | BBPassManager::runOnFunction(Function &F) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1066 | |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1067 | if (F.isExternal()) |
| 1068 | return false; |
| 1069 | |
Devang Patel | e958559 | 2006-12-08 01:38:28 +0000 | [diff] [blame] | 1070 | bool Changed = doInitialization(F); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1071 | |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1072 | std::string Msg1 = "Executing Pass '"; |
| 1073 | std::string Msg3 = "' Made Modification '"; |
| 1074 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1075 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1076 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1077 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1078 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1079 | BP->getAnalysisUsage(AnUsage); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1080 | |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 1081 | std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1082 | dumpPassInfo(BP, Msg1, Msg2); |
| 1083 | dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet()); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1084 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1085 | initializeAnalysisImpl(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1086 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1087 | if (TheTimeInfo) TheTimeInfo->passStarted(BP); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1088 | Changed |= BP->runOnBasicBlock(*I); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1089 | if (TheTimeInfo) TheTimeInfo->passEnded(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1090 | |
| 1091 | if (Changed) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1092 | dumpPassInfo(BP, Msg3, Msg2); |
| 1093 | dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1094 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1095 | removeNotPreservedAnalysis(BP); |
| 1096 | recordAvailableAnalysis(BP); |
| 1097 | removeDeadPasses(BP, Msg2); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1098 | } |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 1099 | return Changed |= doFinalization(F); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1102 | // Implement doInitialization and doFinalization |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1103 | inline bool BBPassManager::doInitialization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1104 | bool Changed = false; |
| 1105 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1106 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1107 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1108 | Changed |= BP->doInitialization(M); |
| 1109 | } |
| 1110 | |
| 1111 | return Changed; |
| 1112 | } |
| 1113 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1114 | inline bool BBPassManager::doFinalization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1115 | bool Changed = false; |
| 1116 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1117 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1118 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1119 | Changed |= BP->doFinalization(M); |
| 1120 | } |
| 1121 | |
| 1122 | return Changed; |
| 1123 | } |
| 1124 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1125 | inline bool BBPassManager::doInitialization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1126 | bool Changed = false; |
| 1127 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1128 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1129 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1130 | Changed |= BP->doInitialization(F); |
| 1131 | } |
| 1132 | |
| 1133 | return Changed; |
| 1134 | } |
| 1135 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1136 | inline bool BBPassManager::doFinalization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1137 | bool Changed = false; |
| 1138 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1139 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1140 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1141 | Changed |= BP->doFinalization(F); |
| 1142 | } |
| 1143 | |
| 1144 | return Changed; |
| 1145 | } |
| 1146 | |
| 1147 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1148 | //===----------------------------------------------------------------------===// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1149 | // FunctionPassManager implementation |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1150 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1151 | /// Create new Function pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1152 | FunctionPassManager::FunctionPassManager(ModuleProvider *P) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1153 | FPM = new FunctionPassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1154 | // FPM is the top level manager. |
| 1155 | FPM->setTopLevelManager(FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1156 | |
| 1157 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM); |
| 1158 | AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD); |
| 1159 | FPM->setResolver(AR); |
| 1160 | |
Devang Patel | 1f65368 | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 1161 | MP = P; |
| 1162 | } |
| 1163 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1164 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1165 | delete FPM; |
| 1166 | } |
| 1167 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1168 | /// add - Add a pass to the queue of passes to run. This passes |
| 1169 | /// ownership of the Pass to the PassManager. When the |
| 1170 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 1171 | /// there is no need to delete the pass. (TODO delete passes.) |
| 1172 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1173 | void FunctionPassManager::add(Pass *P) { |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1174 | FPM->add(P); |
| 1175 | } |
| 1176 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1177 | /// run - Execute all of the passes scheduled for execution. Keep |
| 1178 | /// track of whether any of the passes modifies the function, and if |
| 1179 | /// so, return true. |
| 1180 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1181 | bool FunctionPassManager::run(Function &F) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1182 | std::string errstr; |
| 1183 | if (MP->materializeFunction(&F, &errstr)) { |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1184 | cerr << "Error reading bytecode file: " << errstr << "\n"; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1185 | abort(); |
| 1186 | } |
Devang Patel | 272908d | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 1187 | return FPM->run(F); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1191 | /// doInitialization - Run all of the initializers for the function passes. |
| 1192 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1193 | bool FunctionPassManager::doInitialization() { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1194 | return FPM->doInitialization(*MP->getModule()); |
| 1195 | } |
| 1196 | |
| 1197 | /// doFinalization - Run all of the initializers for the function passes. |
| 1198 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1199 | bool FunctionPassManager::doFinalization() { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1200 | return FPM->doFinalization(*MP->getModule()); |
| 1201 | } |
| 1202 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1203 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1204 | // FunctionPassManagerImpl implementation |
| 1205 | // |
| 1206 | /// Add P into active pass manager or use new module pass manager to |
| 1207 | /// manage it. |
| 1208 | bool FunctionPassManagerImpl::addPass(Pass *P) { |
| 1209 | |
| 1210 | if (!activeManager || !activeManager->addPass(P)) { |
| 1211 | activeManager = new FPPassManager(getDepth() + 1); |
| 1212 | // Inherit top level manager |
| 1213 | activeManager->setTopLevelManager(this->getTopLevelManager()); |
| 1214 | |
| 1215 | // This top level manager is going to manage activeManager. |
| 1216 | // Set up analysis resolver to connect them. |
| 1217 | AnalysisResolver_New *AR = new AnalysisResolver_New(*this); |
| 1218 | activeManager->setResolver(AR); |
| 1219 | |
| 1220 | addPassManager(activeManager); |
| 1221 | return activeManager->addPass(P); |
| 1222 | } |
| 1223 | return true; |
| 1224 | } |
| 1225 | |
| 1226 | inline bool FunctionPassManagerImpl::doInitialization(Module &M) { |
| 1227 | bool Changed = false; |
| 1228 | |
| 1229 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1230 | FPPassManager *FP = getContainedManager(Index); |
| 1231 | Changed |= FP->doInitialization(M); |
| 1232 | } |
| 1233 | |
| 1234 | return Changed; |
| 1235 | } |
| 1236 | |
| 1237 | inline bool FunctionPassManagerImpl::doFinalization(Module &M) { |
| 1238 | bool Changed = false; |
| 1239 | |
| 1240 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1241 | FPPassManager *FP = getContainedManager(Index); |
| 1242 | Changed |= FP->doFinalization(M); |
| 1243 | } |
| 1244 | |
| 1245 | return Changed; |
| 1246 | } |
| 1247 | |
| 1248 | // Execute all the passes managed by this top level manager. |
| 1249 | // Return true if any function is modified by a pass. |
| 1250 | bool FunctionPassManagerImpl::run(Function &F) { |
| 1251 | |
| 1252 | bool Changed = false; |
| 1253 | |
| 1254 | TimingInfo::createTheTimeInfo(); |
| 1255 | |
| 1256 | dumpArguments(); |
| 1257 | dumpPasses(); |
| 1258 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame^] | 1259 | initializeAllAnalysisInfo(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1260 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1261 | FPPassManager *FP = getContainedManager(Index); |
| 1262 | Changed |= FP->runOnFunction(F); |
| 1263 | } |
| 1264 | return Changed; |
| 1265 | } |
| 1266 | |
| 1267 | //===----------------------------------------------------------------------===// |
| 1268 | // FPPassManager implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1269 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1270 | /// Add pass P into the pass manager queue. If P is a BasicBlockPass then |
| 1271 | /// either use it into active basic block pass manager or create new basic |
| 1272 | /// block pass manager to handle pass P. |
| 1273 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1274 | FPPassManager::addPass(Pass *P) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1275 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1276 | // If P is a BasicBlockPass then use BBPassManager. |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1277 | if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) { |
| 1278 | |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1279 | if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) { |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1280 | |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1281 | // If active manager exists then clear its analysis info. |
| 1282 | if (activeBBPassManager) |
| 1283 | activeBBPassManager->initializeAnalysisInfo(); |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame] | 1284 | |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1285 | // Create and add new manager |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1286 | activeBBPassManager = new BBPassManager(getDepth() + 1); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1287 | // Inherit top level manager |
| 1288 | activeBBPassManager->setTopLevelManager(this->getTopLevelManager()); |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 1289 | |
| 1290 | // Add new manager into current manager's list. |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 1291 | addPassToManager(activeBBPassManager, false); |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Add new manager into top level manager's indirect passes list |
| 1294 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager); |
| 1295 | assert (PMD && "Manager is not Pass Manager"); |
| 1296 | TPM->addIndirectPassManager(PMD); |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1297 | |
| 1298 | // Add pass into new manager. This time it must succeed. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 1299 | if (!activeBBPassManager->addPass(BP)) |
| 1300 | assert(0 && "Unable to add Pass"); |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 1301 | |
| 1302 | // If activeBBPassManager transfered any Last Uses then handle them here. |
| 1303 | std::vector<Pass *> &TLU = activeBBPassManager->getTransferredLastUses(); |
| 1304 | if (!TLU.empty()) |
| 1305 | TPM->setLastUser(TLU, this); |
| 1306 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1307 | } |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 1308 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1309 | return true; |
| 1310 | } |
| 1311 | |
| 1312 | FunctionPass *FP = dynamic_cast<FunctionPass *>(P); |
| 1313 | if (!FP) |
| 1314 | return false; |
| 1315 | |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 1316 | // If this pass does not preserve analysis that is used by other passes |
| 1317 | // managed by this manager than it is not a suitable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 1318 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 1319 | return false; |
| 1320 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1321 | addPassToManager (FP); |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1322 | |
| 1323 | // If active manager exists then clear its analysis info. |
| 1324 | if (activeBBPassManager) { |
| 1325 | activeBBPassManager->initializeAnalysisInfo(); |
| 1326 | activeBBPassManager = NULL; |
| 1327 | } |
| 1328 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1329 | return true; |
| 1330 | } |
| 1331 | |
| 1332 | /// Execute all of the passes scheduled for execution by invoking |
| 1333 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 1334 | /// the function, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1335 | bool FPPassManager::runOnFunction(Function &F) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1336 | |
| 1337 | bool Changed = false; |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1338 | |
| 1339 | if (F.isExternal()) |
| 1340 | return false; |
| 1341 | |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1342 | std::string Msg1 = "Executing Pass '"; |
| 1343 | std::string Msg3 = "' Made Modification '"; |
| 1344 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1345 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1346 | FunctionPass *FP = getContainedPass(Index); |
| 1347 | |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1348 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1349 | FP->getAnalysisUsage(AnUsage); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1350 | |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 1351 | std::string Msg2 = "' on Function '" + F.getName() + "'...\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1352 | dumpPassInfo(FP, Msg1, Msg2); |
| 1353 | dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1354 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1355 | initializeAnalysisImpl(FP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1356 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1357 | if (TheTimeInfo) TheTimeInfo->passStarted(FP); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1358 | Changed |= FP->runOnFunction(F); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1359 | if (TheTimeInfo) TheTimeInfo->passEnded(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (Changed) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1362 | dumpPassInfo(FP, Msg3, Msg2); |
| 1363 | dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1364 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1365 | removeNotPreservedAnalysis(FP); |
| 1366 | recordAvailableAnalysis(FP); |
| 1367 | removeDeadPasses(FP, Msg2); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1368 | } |
| 1369 | return Changed; |
| 1370 | } |
| 1371 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1372 | bool FPPassManager::runOnModule(Module &M) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1373 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1374 | bool Changed = doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1375 | |
| 1376 | for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 1377 | this->runOnFunction(*I); |
| 1378 | |
| 1379 | return Changed |= doFinalization(M); |
| 1380 | } |
| 1381 | |
| 1382 | inline bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1383 | bool Changed = false; |
| 1384 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1385 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1386 | FunctionPass *FP = getContainedPass(Index); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1387 | Changed |= FP->doInitialization(M); |
| 1388 | } |
| 1389 | |
| 1390 | return Changed; |
| 1391 | } |
| 1392 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1393 | inline bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1394 | bool Changed = false; |
| 1395 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1396 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1397 | FunctionPass *FP = getContainedPass(Index); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1398 | Changed |= FP->doFinalization(M); |
| 1399 | } |
| 1400 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1401 | return Changed; |
| 1402 | } |
| 1403 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1404 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1405 | // MPPassManager implementation |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1406 | |
| 1407 | /// Add P into pass vector if it is manageble. If P is a FunctionPass |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1408 | /// then use FPPassManager to manage it. Return false if P |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1409 | /// is not manageable by this manager. |
| 1410 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1411 | MPPassManager::addPass(Pass *P) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1412 | |
| 1413 | // If P is FunctionPass then use function pass maanager. |
| 1414 | if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) { |
| 1415 | |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1416 | if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1417 | |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1418 | // If active manager exists then clear its analysis info. |
| 1419 | if (activeFunctionPassManager) |
| 1420 | activeFunctionPassManager->initializeAnalysisInfo(); |
Devang Patel | 642c143 | 2006-12-07 21:58:50 +0000 | [diff] [blame] | 1421 | |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1422 | // Create and add new manager |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 1423 | activeFunctionPassManager = |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1424 | new FPPassManager(getDepth() + 1); |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 1425 | |
| 1426 | // Add new manager into current manager's list |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 1427 | addPassToManager(activeFunctionPassManager, false); |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 1428 | |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1429 | // Inherit top level manager |
| 1430 | activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager()); |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Add new manager into top level manager's indirect passes list |
Chris Lattner | f0f611a | 2006-12-13 21:56:10 +0000 | [diff] [blame] | 1433 | PMDataManager *PMD = |
| 1434 | dynamic_cast<PMDataManager *>(activeFunctionPassManager); |
| 1435 | assert(PMD && "Manager is not Pass Manager"); |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 1436 | TPM->addIndirectPassManager(PMD); |
Devang Patel | af1fca5 | 2006-12-08 23:11:43 +0000 | [diff] [blame] | 1437 | |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1438 | // Add pass into new manager. This time it must succeed. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 1439 | if (!activeFunctionPassManager->addPass(FP)) |
| 1440 | assert(0 && "Unable to add pass"); |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 1441 | |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 1442 | // If activeFunctionPassManager transfered any Last Uses then |
| 1443 | // handle them here. |
| 1444 | std::vector<Pass *> &TLU = |
| 1445 | activeFunctionPassManager->getTransferredLastUses(); |
| 1446 | if (!TLU.empty()) |
| 1447 | TPM->setLastUser(TLU, this); |
| 1448 | } |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 1449 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1450 | return true; |
| 1451 | } |
| 1452 | |
| 1453 | ModulePass *MP = dynamic_cast<ModulePass *>(P); |
| 1454 | if (!MP) |
| 1455 | return false; |
| 1456 | |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 1457 | // If this pass does not preserve analysis that is used by other passes |
| 1458 | // managed by this manager than it is not a suitable pass for this manager. |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 1459 | if (!manageablePass(P)) |
Devang Patel | 3c8eb62 | 2006-11-07 22:56:50 +0000 | [diff] [blame] | 1460 | return false; |
| 1461 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1462 | addPassToManager(MP); |
Devang Patel | 4949fe0 | 2006-12-07 22:34:21 +0000 | [diff] [blame] | 1463 | // If active manager exists then clear its analysis info. |
| 1464 | if (activeFunctionPassManager) { |
| 1465 | activeFunctionPassManager->initializeAnalysisInfo(); |
| 1466 | activeFunctionPassManager = NULL; |
| 1467 | } |
| 1468 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1469 | return true; |
| 1470 | } |
| 1471 | |
| 1472 | |
| 1473 | /// Execute all of the passes scheduled for execution by invoking |
| 1474 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 1475 | /// the module, and if so, return true. |
| 1476 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1477 | MPPassManager::runOnModule(Module &M) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1478 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1479 | |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1480 | std::string Msg1 = "Executing Pass '"; |
| 1481 | std::string Msg3 = "' Made Modification '"; |
| 1482 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1483 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1484 | ModulePass *MP = getContainedPass(Index); |
| 1485 | |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1486 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1487 | MP->getAnalysisUsage(AnUsage); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1488 | |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 1489 | std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1490 | dumpPassInfo(MP, Msg1, Msg2); |
| 1491 | dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1492 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1493 | initializeAnalysisImpl(MP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1494 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1495 | if (TheTimeInfo) TheTimeInfo->passStarted(MP); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1496 | Changed |= MP->runOnModule(M); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1497 | if (TheTimeInfo) TheTimeInfo->passEnded(MP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1498 | |
| 1499 | if (Changed) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1500 | dumpPassInfo(MP, Msg3, Msg2); |
| 1501 | dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet()); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1502 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1503 | removeNotPreservedAnalysis(MP); |
| 1504 | recordAvailableAnalysis(MP); |
| 1505 | removeDeadPasses(MP, Msg2); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1506 | } |
| 1507 | return Changed; |
| 1508 | } |
| 1509 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1510 | //===----------------------------------------------------------------------===// |
| 1511 | // PassManagerImpl implementation |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1512 | // |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1513 | /// Add P into active pass manager or use new module pass manager to |
| 1514 | /// manage it. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1515 | bool PassManagerImpl::addPass(Pass *P) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1516 | |
Devang Patel | 6c9f548 | 2006-11-11 00:42:16 +0000 | [diff] [blame] | 1517 | if (!activeManager || !activeManager->addPass(P)) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1518 | activeManager = new MPPassManager(getDepth() + 1); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1519 | // Inherit top level manager |
| 1520 | activeManager->setTopLevelManager(this->getTopLevelManager()); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 1521 | |
| 1522 | // This top level manager is going to manage activeManager. |
| 1523 | // Set up analysis resolver to connect them. |
| 1524 | AnalysisResolver_New *AR = new AnalysisResolver_New(*this); |
| 1525 | activeManager->setResolver(AR); |
| 1526 | |
Devang Patel | 5bbeb49 | 2006-12-08 22:47:25 +0000 | [diff] [blame] | 1527 | addPassManager(activeManager); |
Devang Patel | 28bbcbe | 2006-12-07 21:44:12 +0000 | [diff] [blame] | 1528 | return activeManager->addPass(P); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1529 | } |
Devang Patel | 28bbcbe | 2006-12-07 21:44:12 +0000 | [diff] [blame] | 1530 | return true; |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1534 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1535 | bool PassManagerImpl::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1536 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1537 | bool Changed = false; |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1538 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1539 | TimingInfo::createTheTimeInfo(); |
| 1540 | |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 1541 | dumpArguments(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1542 | dumpPasses(); |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1543 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame^] | 1544 | initializeAllAnalysisInfo(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1545 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1546 | MPPassManager *MP = getContainedManager(Index); |
Devang Patel | 5bbeb49 | 2006-12-08 22:47:25 +0000 | [diff] [blame] | 1547 | Changed |= MP->runOnModule(M); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1548 | } |
| 1549 | return Changed; |
| 1550 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1551 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1552 | //===----------------------------------------------------------------------===// |
| 1553 | // PassManager implementation |
| 1554 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1555 | /// Create new pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1556 | PassManager::PassManager() { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1557 | PM = new PassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1558 | // PM is the top level manager |
| 1559 | PM->setTopLevelManager(PM); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1562 | PassManager::~PassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1563 | delete PM; |
| 1564 | } |
| 1565 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1566 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 1567 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 1568 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 1569 | /// implies that all passes MUST be allocated with 'new'. |
| 1570 | void |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1571 | PassManager::add(Pass *P) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1572 | PM->add(P); |
| 1573 | } |
| 1574 | |
| 1575 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1576 | /// whether any of the passes modifies the module, and if so, return true. |
| 1577 | bool |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1578 | PassManager::run(Module &M) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1579 | return PM->run(M); |
| 1580 | } |
| 1581 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1582 | //===----------------------------------------------------------------------===// |
| 1583 | // TimingInfo Class - This class is used to calculate information about the |
| 1584 | // amount of time each pass takes to execute. This only happens with |
| 1585 | // -time-passes is enabled on the command line. |
| 1586 | // |
| 1587 | bool llvm::TimePassesIsEnabled = false; |
| 1588 | static cl::opt<bool,true> |
| 1589 | EnableTiming("time-passes", cl::location(TimePassesIsEnabled), |
| 1590 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
| 1591 | |
| 1592 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 1593 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 1594 | // null. It may be called multiple times. |
| 1595 | void TimingInfo::createTheTimeInfo() { |
| 1596 | if (!TimePassesIsEnabled || TheTimeInfo) return; |
| 1597 | |
| 1598 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 1599 | // This guarantees that the object will be constructed before static globals, |
| 1600 | // thus it will be destroyed before them. |
| 1601 | static ManagedStatic<TimingInfo> TTI; |
| 1602 | TheTimeInfo = &*TTI; |
| 1603 | } |
| 1604 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1605 | #endif |