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