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