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