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