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 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM Pass Manager infrastructure. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 15 | #include "llvm/PassManagers.h" |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +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" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | 0dd39fd | 2009-06-17 21:28:54 +0000 | [diff] [blame] | 23 | #include "llvm/System/Mutex.h" |
Owen Anderson | 7d42b95 | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 24 | #include "llvm/System/Threading.h" |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/Dominators.h" |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 26 | #include "llvm-c/Core.h" |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Duncan Sands | 26ff6f9 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 28 | #include <cstdio> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 29 | #include <map> |
Dan Gohman | 8c43e41 | 2007-10-03 19:04:09 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 31 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 32 | // See PassManagers.h for Pass Manager infrastructure overview. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 33 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 34 | namespace llvm { |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Pass debugging information. Often it is useful to find out what pass is |
| 38 | // running when a crash occurs in a utility. When this library is compiled with |
| 39 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 40 | // pass name to be printed before it executes. |
| 41 | // |
| 42 | |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 43 | // Different debug levels that can be enabled... |
| 44 | enum PassDebugLevel { |
| 45 | None, Arguments, Structure, Executions, Details |
| 46 | }; |
| 47 | |
Duncan Sands | e5e9f09 | 2009-05-22 08:52:53 +0000 | [diff] [blame] | 48 | // Always verify dominfo if expensive checking is enabled. |
| 49 | #ifdef XDEBUG |
| 50 | bool VerifyDomInfo = true; |
| 51 | #else |
Devang Patel | 99ad4ba | 2008-07-01 21:36:11 +0000 | [diff] [blame] | 52 | bool VerifyDomInfo = false; |
Duncan Sands | e5e9f09 | 2009-05-22 08:52:53 +0000 | [diff] [blame] | 53 | #endif |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 54 | static cl::opt<bool,true> |
| 55 | VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo), |
| 56 | cl::desc("Verify dominator info (time consuming)")); |
| 57 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 58 | static cl::opt<enum PassDebugLevel> |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 59 | PassDebugging("debug-pass", cl::Hidden, |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 60 | cl::desc("Print PassManager debugging information"), |
| 61 | cl::values( |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 62 | clEnumVal(None , "disable debug output"), |
| 63 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
| 64 | clEnumVal(Structure , "print pass structure before run()"), |
| 65 | clEnumVal(Executions, "print pass name before it is executed"), |
| 66 | clEnumVal(Details , "print pass details when it is executed"), |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 67 | clEnumValEnd)); |
| 68 | } // End of llvm namespace |
| 69 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 70 | void PassManagerPrettyStackEntry::print(raw_ostream &OS) const { |
| 71 | if (V == 0 && M == 0) |
| 72 | OS << "Releasing pass '"; |
| 73 | else |
| 74 | OS << "Running pass '"; |
| 75 | |
| 76 | OS << P->getPassName() << "'"; |
| 77 | |
| 78 | if (M) { |
| 79 | OS << " on module '" << M->getModuleIdentifier() << "'.\n"; |
| 80 | return; |
| 81 | } |
| 82 | if (V == 0) { |
| 83 | OS << '\n'; |
| 84 | return; |
| 85 | } |
| 86 | |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 87 | OS << " on "; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 88 | if (isa<Function>(V)) |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 89 | OS << "function"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 90 | else if (isa<BasicBlock>(V)) |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 91 | OS << "basic block"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 92 | else |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 93 | OS << "value"; |
| 94 | |
| 95 | OS << " '"; |
| 96 | WriteAsOperand(OS, V, /*PrintTy=*/false, M); |
| 97 | OS << "'\n"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 101 | namespace { |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 102 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 103 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 104 | // BBPassManager |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 105 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 106 | /// BBPassManager manages BasicBlockPass. It batches all the |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 107 | /// pass together and sequence them to process one basic block before |
| 108 | /// processing next basic block. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 109 | class VISIBILITY_HIDDEN BBPassManager : public PMDataManager, |
| 110 | public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 111 | |
| 112 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 113 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 114 | explicit BBPassManager(int Depth) |
Dan Gohman | a79db30 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 115 | : PMDataManager(Depth), FunctionPass(&ID) {} |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 116 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 117 | /// Execute all of the passes scheduled for execution. Keep track of |
| 118 | /// whether any of the passes modifies the function, and if so, return true. |
| 119 | bool runOnFunction(Function &F); |
| 120 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 121 | /// Pass Manager itself does not invalidate any analysis info. |
| 122 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 123 | Info.setPreservesAll(); |
| 124 | } |
| 125 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 126 | bool doInitialization(Module &M); |
| 127 | bool doInitialization(Function &F); |
| 128 | bool doFinalization(Module &M); |
| 129 | bool doFinalization(Function &F); |
| 130 | |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 131 | virtual const char *getPassName() const { |
Dan Gohman | 1e9860a | 2008-03-13 01:58:48 +0000 | [diff] [blame] | 132 | return "BasicBlock Pass Manager"; |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 135 | // Print passes managed by this manager |
| 136 | void dumpPassStructure(unsigned Offset) { |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 137 | llvm::errs() << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 138 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 139 | BasicBlockPass *BP = getContainedPass(Index); |
| 140 | BP->dumpPassStructure(Offset + 1); |
| 141 | dumpLastUses(BP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 144 | |
| 145 | BasicBlockPass *getContainedPass(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 146 | assert(N < PassVector.size() && "Pass number out of range!"); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 147 | BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); |
| 148 | return BP; |
| 149 | } |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 150 | |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 151 | virtual PassManagerType getPassManagerType() const { |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 152 | return PMT_BasicBlockPassManager; |
| 153 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 156 | char BBPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 157 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 158 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 159 | namespace llvm { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 160 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 161 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 162 | // FunctionPassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 163 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 164 | /// FunctionPassManagerImpl manages FPPassManagers |
| 165 | class FunctionPassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 166 | public PMDataManager, |
| 167 | public PMTopLevelManager { |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 168 | private: |
| 169 | bool wasRun; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 170 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 171 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 172 | explicit FunctionPassManagerImpl(int Depth) : |
Dan Gohman | a79db30 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 173 | Pass(&ID), PMDataManager(Depth), |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 174 | PMTopLevelManager(TLM_Function), wasRun(false) { } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 175 | |
| 176 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 177 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 178 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 179 | /// implies that all passes MUST be allocated with 'new'. |
| 180 | void add(Pass *P) { |
| 181 | schedulePass(P); |
| 182 | } |
| 183 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 184 | // Prepare for running an on the fly pass, freeing memory if needed |
| 185 | // from a previous run. |
| 186 | void releaseMemoryOnTheFly(); |
| 187 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 188 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 189 | /// whether any of the passes modifies the module, and if so, return true. |
| 190 | bool run(Function &F); |
| 191 | |
| 192 | /// doInitialization - Run all of the initializers for the function passes. |
| 193 | /// |
| 194 | bool doInitialization(Module &M); |
| 195 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 196 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 197 | /// |
| 198 | bool doFinalization(Module &M); |
| 199 | |
| 200 | /// Pass Manager itself does not invalidate any analysis info. |
| 201 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 202 | Info.setPreservesAll(); |
| 203 | } |
| 204 | |
| 205 | inline void addTopLevelPass(Pass *P) { |
| 206 | |
| 207 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
| 208 | |
| 209 | // P is a immutable pass and it will be managed by this |
| 210 | // top level manager. Set up analysis resolver to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 211 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 212 | P->setResolver(AR); |
| 213 | initializeAnalysisImpl(P); |
| 214 | addImmutablePass(IP); |
| 215 | recordAvailableAnalysis(IP); |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 216 | } else { |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 217 | P->assignPassManager(activeStack); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 218 | } |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 219 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | FPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 223 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 224 | FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); |
| 225 | return FP; |
| 226 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 229 | char FunctionPassManagerImpl::ID = 0; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 230 | //===----------------------------------------------------------------------===// |
| 231 | // MPPassManager |
| 232 | // |
| 233 | /// MPPassManager manages ModulePasses and function pass managers. |
Dan Gohman | dfdf2c0 | 2008-03-11 16:18:48 +0000 | [diff] [blame] | 234 | /// It batches all Module passes and function pass managers together and |
| 235 | /// sequences them to process one module. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 236 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 237 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 238 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 239 | explicit MPPassManager(int Depth) : |
Dan Gohman | a79db30 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 240 | Pass(&ID), PMDataManager(Depth) { } |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 241 | |
| 242 | // Delete on the fly managers. |
| 243 | virtual ~MPPassManager() { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 244 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 245 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 246 | I != E; ++I) { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 247 | FunctionPassManagerImpl *FPP = I->second; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 248 | delete FPP; |
| 249 | } |
| 250 | } |
| 251 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 252 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 253 | /// whether any of the passes modifies the module, and if so, return true. |
| 254 | bool runOnModule(Module &M); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 255 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 256 | /// Pass Manager itself does not invalidate any analysis info. |
| 257 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 258 | Info.setPreservesAll(); |
| 259 | } |
| 260 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 261 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 262 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 263 | /// through getAnalysis interface. |
| 264 | virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); |
| 265 | |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 266 | /// Return function pass corresponding to PassInfo PI, that is |
| 267 | /// required by module pass MP. Instantiate analysis pass, by using |
| 268 | /// its runOnFunction() for function F. |
| 269 | virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F); |
| 270 | |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 271 | virtual const char *getPassName() const { |
| 272 | return "Module Pass Manager"; |
| 273 | } |
| 274 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 275 | // Print passes managed by this manager |
| 276 | void dumpPassStructure(unsigned Offset) { |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 277 | llvm::errs() << std::string(Offset*2, ' ') << "ModulePass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 278 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 279 | ModulePass *MP = getContainedPass(Index); |
| 280 | MP->dumpPassStructure(Offset + 1); |
Dan Gohman | 83ff184 | 2009-07-01 23:12:33 +0000 | [diff] [blame] | 281 | std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I = |
| 282 | OnTheFlyManagers.find(MP); |
| 283 | if (I != OnTheFlyManagers.end()) |
| 284 | I->second->dumpPassStructure(Offset + 2); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 285 | dumpLastUses(MP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 289 | ModulePass *getContainedPass(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 290 | assert(N < PassVector.size() && "Pass number out of range!"); |
| 291 | return static_cast<ModulePass *>(PassVector[N]); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 294 | virtual PassManagerType getPassManagerType() const { |
| 295 | return PMT_ModulePassManager; |
| 296 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 297 | |
| 298 | private: |
| 299 | /// Collection of on the fly FPPassManagers. These managers manage |
| 300 | /// function passes that are required by module passes. |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 301 | std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 304 | char MPPassManager::ID = 0; |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 306 | // PassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 307 | // |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 308 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 309 | /// PassManagerImpl manages MPPassManagers |
| 310 | class PassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 311 | public PMDataManager, |
| 312 | public PMTopLevelManager { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 313 | |
| 314 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 315 | static char ID; |
Dan Gohman | 13ab93e | 2007-10-08 15:08:41 +0000 | [diff] [blame] | 316 | explicit PassManagerImpl(int Depth) : |
Dan Gohman | a79db30 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 317 | Pass(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { } |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 318 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 319 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 320 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 321 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 322 | /// implies that all passes MUST be allocated with 'new'. |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 323 | void add(Pass *P) { |
Devang Patel | df6c9ae | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 324 | schedulePass(P); |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 325 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 326 | |
| 327 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 328 | /// whether any of the passes modifies the module, and if so, return true. |
| 329 | bool run(Module &M); |
| 330 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 331 | /// Pass Manager itself does not invalidate any analysis info. |
| 332 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 333 | Info.setPreservesAll(); |
| 334 | } |
| 335 | |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 336 | inline void addTopLevelPass(Pass *P) { |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 337 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 338 | |
| 339 | // P is a immutable pass and it will be managed by this |
| 340 | // top level manager. Set up analysis resolver to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 341 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 342 | P->setResolver(AR); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 343 | initializeAnalysisImpl(P); |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 344 | addImmutablePass(IP); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 345 | recordAvailableAnalysis(IP); |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 346 | } else { |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 347 | P->assignPassManager(activeStack); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 348 | } |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 351 | MPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 352 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 353 | MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); |
| 354 | return MP; |
| 355 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 356 | }; |
| 357 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 358 | char PassManagerImpl::ID = 0; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 359 | } // End of llvm namespace |
| 360 | |
| 361 | namespace { |
| 362 | |
| 363 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 364 | /// TimingInfo Class - This class is used to calculate information about the |
| 365 | /// amount of time each pass takes to execute. This only happens when |
| 366 | /// -time-passes is enabled on the command line. |
| 367 | /// |
Owen Anderson | 0dd39fd | 2009-06-17 21:28:54 +0000 | [diff] [blame] | 368 | |
Owen Anderson | 5a6960f | 2009-06-18 20:51:00 +0000 | [diff] [blame] | 369 | static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex; |
Owen Anderson | 0dd39fd | 2009-06-17 21:28:54 +0000 | [diff] [blame] | 370 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 371 | class VISIBILITY_HIDDEN TimingInfo { |
| 372 | std::map<Pass*, Timer> TimingData; |
| 373 | TimerGroup TG; |
| 374 | |
| 375 | public: |
| 376 | // Use 'create' member to get this. |
| 377 | TimingInfo() : TG("... Pass execution timing report ...") {} |
| 378 | |
| 379 | // TimingDtor - Print out information about timing information |
| 380 | ~TimingInfo() { |
| 381 | // Delete all of the timers... |
| 382 | TimingData.clear(); |
| 383 | // TimerGroup is deleted next, printing the report. |
| 384 | } |
| 385 | |
| 386 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer |
| 387 | // to a non null value (if the -time-passes option is enabled) or it leaves it |
| 388 | // null. It may be called multiple times. |
| 389 | static void createTheTimeInfo(); |
| 390 | |
| 391 | void passStarted(Pass *P) { |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 392 | if (dynamic_cast<PMDataManager *>(P)) |
| 393 | return; |
| 394 | |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 395 | sys::SmartScopedLock<true> Lock(*TimingInfoMutex); |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 396 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 397 | if (I == TimingData.end()) |
| 398 | I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first; |
| 399 | I->second.startTimer(); |
| 400 | } |
Owen Anderson | 5a6960f | 2009-06-18 20:51:00 +0000 | [diff] [blame] | 401 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 402 | void passEnded(Pass *P) { |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 403 | if (dynamic_cast<PMDataManager *>(P)) |
| 404 | return; |
| 405 | |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 406 | sys::SmartScopedLock<true> Lock(*TimingInfoMutex); |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 407 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 408 | assert(I != TimingData.end() && "passStarted/passEnded not nested right!"); |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 409 | I->second.stopTimer(); |
| 410 | } |
| 411 | }; |
| 412 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 413 | } // End of anon namespace |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 414 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 415 | static TimingInfo *TheTimeInfo; |
| 416 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 417 | //===----------------------------------------------------------------------===// |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 418 | // PMTopLevelManager implementation |
| 419 | |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 420 | /// Initialize top level manager. Create first pass manager. |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 421 | PMTopLevelManager::PMTopLevelManager(enum TopLevelManagerType t) { |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 422 | if (t == TLM_Pass) { |
| 423 | MPPassManager *MPP = new MPPassManager(1); |
| 424 | MPP->setTopLevelManager(this); |
| 425 | addPassManager(MPP); |
| 426 | activeStack.push(MPP); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 427 | } else if (t == TLM_Function) { |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 428 | FPPassManager *FPP = new FPPassManager(1); |
| 429 | FPP->setTopLevelManager(this); |
| 430 | addPassManager(FPP); |
| 431 | activeStack.push(FPP); |
| 432 | } |
| 433 | } |
| 434 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 435 | /// Set pass P as the last user of the given analysis passes. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 436 | void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 437 | Pass *P) { |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 438 | for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(), |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 439 | E = AnalysisPasses.end(); I != E; ++I) { |
| 440 | Pass *AP = *I; |
| 441 | LastUser[AP] = P; |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 442 | |
| 443 | if (P == AP) |
| 444 | continue; |
| 445 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 446 | // If AP is the last user of other passes then make P last user of |
| 447 | // such passes. |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 448 | for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 449 | LUE = LastUser.end(); LUI != LUE; ++LUI) { |
| 450 | if (LUI->second == AP) |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 451 | // DenseMap iterator is not invalidated here because |
| 452 | // this is just updating exisitng entry. |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 453 | LastUser[LUI->first] = P; |
| 454 | } |
| 455 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /// Collect passes whose last user is P |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 459 | void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses, |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 460 | Pass *P) { |
| 461 | DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI = |
| 462 | InversedLastUser.find(P); |
| 463 | if (DMI == InversedLastUser.end()) |
| 464 | return; |
| 465 | |
| 466 | SmallPtrSet<Pass *, 8> &LU = DMI->second; |
| 467 | for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(), |
| 468 | E = LU.end(); I != E; ++I) { |
| 469 | LastUses.push_back(*I); |
| 470 | } |
| 471 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 474 | AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) { |
| 475 | AnalysisUsage *AnUsage = NULL; |
| 476 | DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P); |
| 477 | if (DMI != AnUsageMap.end()) |
| 478 | AnUsage = DMI->second; |
| 479 | else { |
| 480 | AnUsage = new AnalysisUsage(); |
| 481 | P->getAnalysisUsage(*AnUsage); |
| 482 | AnUsageMap[P] = AnUsage; |
| 483 | } |
| 484 | return AnUsage; |
| 485 | } |
| 486 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 487 | /// Schedule pass P for execution. Make sure that passes required by |
| 488 | /// P are run before P is run. Update analysis info maintained by |
| 489 | /// the manager. Remove dead passes. This is a recursive function. |
| 490 | void PMTopLevelManager::schedulePass(Pass *P) { |
| 491 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 492 | // TODO : Allocate function manager for this pass, other wise required set |
| 493 | // may be inserted into previous function manager |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 494 | |
Devang Patel | d74ede7 | 2007-03-06 01:06:16 +0000 | [diff] [blame] | 495 | // Give pass a chance to prepare the stage. |
| 496 | P->preparePassManager(activeStack); |
| 497 | |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 498 | // If P is an analysis pass and it is available then do not |
| 499 | // generate the analysis again. Stale analysis info should not be |
| 500 | // available at this point. |
Devang Patel | 718da66 | 2008-03-19 21:56:59 +0000 | [diff] [blame] | 501 | if (P->getPassInfo() && |
Nuno Lopes | 0460bb2 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 502 | P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo())) { |
| 503 | delete P; |
Devang Patel | af75ab8 | 2008-03-19 00:48:41 +0000 | [diff] [blame] | 504 | return; |
Nuno Lopes | 0460bb2 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 505 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 506 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 507 | AnalysisUsage *AnUsage = findAnalysisUsage(P); |
| 508 | |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 509 | bool checkAnalysis = true; |
| 510 | while (checkAnalysis) { |
| 511 | checkAnalysis = false; |
| 512 | |
| 513 | const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); |
| 514 | for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(), |
| 515 | E = RequiredSet.end(); I != E; ++I) { |
| 516 | |
| 517 | Pass *AnalysisPass = findAnalysisPass(*I); |
| 518 | if (!AnalysisPass) { |
| 519 | AnalysisPass = (*I)->createPass(); |
| 520 | if (P->getPotentialPassManagerType () == |
| 521 | AnalysisPass->getPotentialPassManagerType()) |
| 522 | // Schedule analysis pass that is managed by the same pass manager. |
| 523 | schedulePass(AnalysisPass); |
| 524 | else if (P->getPotentialPassManagerType () > |
| 525 | AnalysisPass->getPotentialPassManagerType()) { |
| 526 | // Schedule analysis pass that is managed by a new manager. |
| 527 | schedulePass(AnalysisPass); |
| 528 | // Recheck analysis passes to ensure that required analysises that |
| 529 | // are already checked are still available. |
| 530 | checkAnalysis = true; |
| 531 | } |
| 532 | else |
| 533 | // Do not schedule this analysis. Lower level analsyis |
| 534 | // passes are run on the fly. |
| 535 | delete AnalysisPass; |
| 536 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | // Now all required passes are available. |
| 541 | addTopLevelPass(P); |
| 542 | } |
| 543 | |
| 544 | /// Find the pass that implements Analysis AID. Search immutable |
| 545 | /// passes and all pass managers. If desired pass is not found |
| 546 | /// then return NULL. |
| 547 | Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { |
| 548 | |
| 549 | Pass *P = NULL; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 550 | // Check pass managers |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 551 | for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(), |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 552 | E = PassManagers.end(); P == NULL && I != E; ++I) { |
Dan Gohman | 73caf5f | 2008-03-13 01:48:32 +0000 | [diff] [blame] | 553 | PMDataManager *PMD = *I; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 554 | P = PMD->findAnalysisPass(AID, false); |
| 555 | } |
| 556 | |
| 557 | // Check other pass managers |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 558 | for (SmallVector<PMDataManager *, 8>::iterator |
| 559 | I = IndirectPassManagers.begin(), |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 560 | E = IndirectPassManagers.end(); P == NULL && I != E; ++I) |
| 561 | P = (*I)->findAnalysisPass(AID, false); |
| 562 | |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 563 | for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(), |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 564 | E = ImmutablePasses.end(); P == NULL && I != E; ++I) { |
| 565 | const PassInfo *PI = (*I)->getPassInfo(); |
| 566 | if (PI == AID) |
| 567 | P = *I; |
| 568 | |
| 569 | // If Pass not found then check the interfaces implemented by Immutable Pass |
| 570 | if (!P) { |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 571 | const std::vector<const PassInfo*> &ImmPI = |
| 572 | PI->getInterfacesImplemented(); |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 573 | if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end()) |
| 574 | P = *I; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 578 | return P; |
| 579 | } |
| 580 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 581 | // Print passes managed by this top level manager. |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 582 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 583 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 584 | if (PassDebugging < Structure) |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 585 | return; |
| 586 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 587 | // Print out the immutable passes |
| 588 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 589 | ImmutablePasses[i]->dumpPassStructure(0); |
| 590 | } |
| 591 | |
Dan Gohman | 73caf5f | 2008-03-13 01:48:32 +0000 | [diff] [blame] | 592 | // Every class that derives from PMDataManager also derives from Pass |
| 593 | // (sometimes indirectly), but there's no inheritance relationship |
| 594 | // between PMDataManager and Pass, so we have to dynamic_cast to get |
| 595 | // from a PMDataManager* to a Pass*. |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 596 | for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(), |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 597 | E = PassManagers.end(); I != E; ++I) |
Dan Gohman | 73caf5f | 2008-03-13 01:48:32 +0000 | [diff] [blame] | 598 | dynamic_cast<Pass *>(*I)->dumpPassStructure(1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 601 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 602 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 603 | if (PassDebugging < Arguments) |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 604 | return; |
| 605 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 606 | errs() << "Pass Arguments: "; |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 607 | for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(), |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 608 | E = PassManagers.end(); I != E; ++I) |
| 609 | (*I)->dumpPassArguments(); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 610 | errs() << "\n"; |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 613 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 614 | for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(), |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 615 | E = PassManagers.end(); I != E; ++I) |
| 616 | (*I)->initializeAnalysisInfo(); |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 617 | |
| 618 | // Initailize other pass managers |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 619 | for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(), |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 620 | E = IndirectPassManagers.end(); I != E; ++I) |
| 621 | (*I)->initializeAnalysisInfo(); |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 622 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 623 | for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(), |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 624 | DME = LastUser.end(); DMI != DME; ++DMI) { |
| 625 | DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI = |
| 626 | InversedLastUser.find(DMI->second); |
| 627 | if (InvDMI != InversedLastUser.end()) { |
| 628 | SmallPtrSet<Pass *, 8> &L = InvDMI->second; |
| 629 | L.insert(DMI->first); |
| 630 | } else { |
| 631 | SmallPtrSet<Pass *, 8> L; L.insert(DMI->first); |
| 632 | InversedLastUser[DMI->second] = L; |
| 633 | } |
| 634 | } |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 637 | /// Destructor |
| 638 | PMTopLevelManager::~PMTopLevelManager() { |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 639 | for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(), |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 640 | E = PassManagers.end(); I != E; ++I) |
| 641 | delete *I; |
| 642 | |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 643 | for (SmallVector<ImmutablePass *, 8>::iterator |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 644 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 645 | delete *I; |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 646 | |
| 647 | for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(), |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 648 | DME = AnUsageMap.end(); DMI != DME; ++DMI) |
| 649 | delete DMI->second; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 652 | //===----------------------------------------------------------------------===// |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 653 | // PMDataManager implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 654 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 655 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 656 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 657 | const PassInfo *PI = P->getPassInfo(); |
| 658 | if (PI == 0) return; |
| 659 | |
| 660 | AvailableAnalysis[PI] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 662 | //This pass is the current implementation of all of the interfaces it |
| 663 | //implements as well. |
| 664 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 665 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 666 | AvailableAnalysis[II[i]] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 669 | // Return true if P preserves high level analysis used by other |
| 670 | // passes managed by this manager |
| 671 | bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 672 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 673 | if (AnUsage->getPreservesAll()) |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 674 | return true; |
| 675 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 676 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 677 | for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(), |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 678 | E = HigherLevelAnalysis.end(); I != E; ++I) { |
| 679 | Pass *P1 = *I; |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 680 | if (!dynamic_cast<ImmutablePass*>(P1) && |
| 681 | std::find(PreservedSet.begin(), PreservedSet.end(), |
| 682 | P1->getPassInfo()) == |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 683 | PreservedSet.end()) |
| 684 | return false; |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | return true; |
| 688 | } |
| 689 | |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 690 | /// verifyPreservedAnalysis -- Verify analysis preserved by pass P. |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 691 | void PMDataManager::verifyPreservedAnalysis(Pass *P) { |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 692 | // Don't do this unless assertions are enabled. |
| 693 | #ifdef NDEBUG |
| 694 | return; |
| 695 | #endif |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 696 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 697 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 698 | |
Devang Patel | ef43253 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 699 | // Verify preserved analysis |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 700 | for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(), |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 701 | E = PreservedSet.end(); I != E; ++I) { |
| 702 | AnalysisID AID = *I; |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 703 | if (Pass *AP = findAnalysisPass(AID, true)) |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 704 | AP->verifyAnalysis(); |
Devang Patel | ef43253 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 705 | } |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 708 | /// verifyDomInfo - Verify dominator information if it is available. |
| 709 | void PMDataManager::verifyDomInfo(Pass &P, Function &F) { |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 710 | if (!VerifyDomInfo || !P.getResolver()) |
| 711 | return; |
| 712 | |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 713 | DominatorTree *DT = P.getAnalysisIfAvailable<DominatorTree>(); |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 714 | if (!DT) |
| 715 | return; |
| 716 | |
| 717 | DominatorTree OtherDT; |
| 718 | OtherDT.getBase().recalculate(F); |
| 719 | if (DT->compare(OtherDT)) { |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 720 | errs() << "Dominator Information for " << F.getName() << "\n"; |
| 721 | errs() << "Pass '" << P.getPassName() << "'\n"; |
| 722 | errs() << "----- Valid -----\n"; |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 723 | OtherDT.dump(); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 724 | errs() << "----- Invalid -----\n"; |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 725 | DT->dump(); |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 726 | llvm_unreachable("Invalid dominator info"); |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 729 | DominanceFrontier *DF = P.getAnalysisIfAvailable<DominanceFrontier>(); |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 730 | if (!DF) |
| 731 | return; |
| 732 | |
| 733 | DominanceFrontier OtherDF; |
| 734 | std::vector<BasicBlock*> DTRoots = DT->getRoots(); |
| 735 | OtherDF.calculate(*DT, DT->getNode(DTRoots[0])); |
| 736 | if (DF->compare(OtherDF)) { |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 737 | errs() << "Dominator Information for " << F.getName() << "\n"; |
| 738 | errs() << "Pass '" << P.getPassName() << "'\n"; |
| 739 | errs() << "----- Valid -----\n"; |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 740 | OtherDF.dump(); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 741 | errs() << "----- Invalid -----\n"; |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 742 | DF->dump(); |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 743 | llvm_unreachable("Invalid dominator info"); |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
Devang Patel | 67c79a4 | 2008-07-01 19:50:56 +0000 | [diff] [blame] | 747 | /// Remove Analysis not preserved by Pass P |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 748 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 749 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 750 | if (AnUsage->getPreservesAll()) |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 751 | return; |
| 752 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 753 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 754 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | be6bd55e | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 755 | E = AvailableAnalysis.end(); I != E; ) { |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 756 | std::map<AnalysisID, Pass*>::iterator Info = I++; |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 757 | if (!dynamic_cast<ImmutablePass*>(Info->second) |
| 758 | && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 759 | PreservedSet.end()) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 760 | // Remove this analysis |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 761 | if (PassDebugging >= Details) { |
| 762 | Pass *S = Info->second; |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 763 | errs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 764 | errs() << S->getPassName() << "'\n"; |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 765 | } |
Dan Gohman | 193e4c0 | 2008-11-06 21:57:17 +0000 | [diff] [blame] | 766 | AvailableAnalysis.erase(Info); |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 767 | } |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 768 | } |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 769 | |
| 770 | // Check inherited analysis also. If P is not preserving analysis |
| 771 | // provided by parent manager then remove it here. |
| 772 | for (unsigned Index = 0; Index < PMT_Last; ++Index) { |
| 773 | |
| 774 | if (!InheritedAnalysis[Index]) |
| 775 | continue; |
| 776 | |
| 777 | for (std::map<AnalysisID, Pass*>::iterator |
| 778 | I = InheritedAnalysis[Index]->begin(), |
| 779 | E = InheritedAnalysis[Index]->end(); I != E; ) { |
| 780 | std::map<AnalysisID, Pass *>::iterator Info = I++; |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 781 | if (!dynamic_cast<ImmutablePass*>(Info->second) && |
| 782 | std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 783 | PreservedSet.end()) |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 784 | // Remove this analysis |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 785 | InheritedAnalysis[Index]->erase(Info); |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 786 | } |
| 787 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 790 | /// Remove analysis passes that are not used any longer |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 791 | void PMDataManager::removeDeadPasses(Pass *P, const StringRef &Msg, |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 792 | enum PassDebuggingString DBG_STR) { |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 793 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 794 | SmallVector<Pass *, 12> DeadPasses; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 795 | |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 796 | // If this is a on the fly manager then it does not have TPM. |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 797 | if (!TPM) |
| 798 | return; |
| 799 | |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 800 | TPM->collectLastUses(DeadPasses, P); |
| 801 | |
Devang Patel | 656a917 | 2008-06-06 17:50:36 +0000 | [diff] [blame] | 802 | if (PassDebugging >= Details && !DeadPasses.empty()) { |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 803 | errs() << " -*- '" << P->getPassName(); |
| 804 | errs() << "' is the last user of following pass instances."; |
| 805 | errs() << " Free these instances\n"; |
Evan Cheng | 93af6ce | 2008-06-04 09:13:31 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 808 | for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(), |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 809 | E = DeadPasses.end(); I != E; ++I) { |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 810 | |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 811 | dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg); |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 813 | { |
| 814 | // If the pass crashes releasing memory, remember this. |
| 815 | PassManagerPrettyStackEntry X(*I); |
| 816 | |
| 817 | if (TheTimeInfo) TheTimeInfo->passStarted(*I); |
| 818 | (*I)->releaseMemory(); |
| 819 | if (TheTimeInfo) TheTimeInfo->passEnded(*I); |
| 820 | } |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 821 | if (const PassInfo *PI = (*I)->getPassInfo()) { |
| 822 | std::map<AnalysisID, Pass*>::iterator Pos = |
| 823 | AvailableAnalysis.find(PI); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 824 | |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 825 | // It is possible that pass is already removed from the AvailableAnalysis |
| 826 | if (Pos != AvailableAnalysis.end()) |
| 827 | AvailableAnalysis.erase(Pos); |
| 828 | |
| 829 | // Remove all interfaces this pass implements, for which it is also |
| 830 | // listed as the available implementation. |
| 831 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 832 | for (unsigned i = 0, e = II.size(); i != e; ++i) { |
| 833 | Pos = AvailableAnalysis.find(II[i]); |
| 834 | if (Pos != AvailableAnalysis.end() && Pos->second == *I) |
| 835 | AvailableAnalysis.erase(Pos); |
| 836 | } |
| 837 | } |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 838 | } |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 841 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 842 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 843 | void PMDataManager::add(Pass *P, bool ProcessAnalysis) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 844 | // This manager is going to manage pass P. Set up analysis resolver |
| 845 | // to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 846 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 847 | P->setResolver(AR); |
| 848 | |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 849 | // If a FunctionPass F is the last user of ModulePass info M |
| 850 | // then the F's manager, not F, records itself as a last user of M. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 851 | SmallVector<Pass *, 12> TransferLastUses; |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 852 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 853 | if (!ProcessAnalysis) { |
| 854 | // Add pass |
| 855 | PassVector.push_back(P); |
| 856 | return; |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 857 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 858 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 859 | // At the moment, this pass is the last user of all required passes. |
| 860 | SmallVector<Pass *, 12> LastUses; |
| 861 | SmallVector<Pass *, 8> RequiredPasses; |
| 862 | SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; |
| 863 | |
| 864 | unsigned PDepth = this->getDepth(); |
| 865 | |
| 866 | collectRequiredAnalysis(RequiredPasses, |
| 867 | ReqAnalysisNotAvailable, P); |
| 868 | for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(), |
| 869 | E = RequiredPasses.end(); I != E; ++I) { |
| 870 | Pass *PRequired = *I; |
| 871 | unsigned RDepth = 0; |
| 872 | |
| 873 | assert(PRequired->getResolver() && "Analysis Resolver is not set"); |
| 874 | PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); |
| 875 | RDepth = DM.getDepth(); |
| 876 | |
| 877 | if (PDepth == RDepth) |
| 878 | LastUses.push_back(PRequired); |
| 879 | else if (PDepth > RDepth) { |
| 880 | // Let the parent claim responsibility of last use |
| 881 | TransferLastUses.push_back(PRequired); |
| 882 | // Keep track of higher level analysis used by this manager. |
| 883 | HigherLevelAnalysis.push_back(PRequired); |
| 884 | } else |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 885 | llvm_unreachable("Unable to accomodate Required Pass"); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | // Set P as P's last user until someone starts using P. |
| 889 | // However, if P is a Pass Manager then it does not need |
| 890 | // to record its last user. |
| 891 | if (!dynamic_cast<PMDataManager *>(P)) |
| 892 | LastUses.push_back(P); |
| 893 | TPM->setLastUser(LastUses, P); |
| 894 | |
| 895 | if (!TransferLastUses.empty()) { |
| 896 | Pass *My_PM = dynamic_cast<Pass *>(this); |
| 897 | TPM->setLastUser(TransferLastUses, My_PM); |
| 898 | TransferLastUses.clear(); |
| 899 | } |
| 900 | |
| 901 | // Now, take care of required analysises that are not available. |
| 902 | for (SmallVector<AnalysisID, 8>::iterator |
| 903 | I = ReqAnalysisNotAvailable.begin(), |
| 904 | E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { |
| 905 | Pass *AnalysisPass = (*I)->createPass(); |
| 906 | this->addLowerLevelRequiredPass(P, AnalysisPass); |
| 907 | } |
| 908 | |
| 909 | // Take a note of analysis required and made available by this pass. |
| 910 | // Remove the analysis not preserved by this pass |
| 911 | removeNotPreservedAnalysis(P); |
| 912 | recordAvailableAnalysis(P); |
| 913 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 914 | // Add pass |
| 915 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 918 | |
| 919 | /// Populate RP with analysis pass that are required by |
| 920 | /// pass P and are available. Populate RP_NotAvail with analysis |
| 921 | /// pass that are required by pass P but are not available. |
| 922 | void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP, |
| 923 | SmallVector<AnalysisID, 8> &RP_NotAvail, |
| 924 | Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 925 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 926 | const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 927 | for (AnalysisUsage::VectorType::const_iterator |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 928 | I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) { |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 929 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
| 930 | RP.push_back(AnalysisPass); |
| 931 | else |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 932 | RP_NotAvail.push_back(*I); |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 933 | } |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 934 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 935 | const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 936 | for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(), |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 937 | E = IDs.end(); I != E; ++I) { |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 938 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
| 939 | RP.push_back(AnalysisPass); |
| 940 | else |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 941 | RP_NotAvail.push_back(*I); |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 942 | } |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 945 | // All Required analyses should be available to the pass as it runs! Here |
| 946 | // we fill in the AnalysisImpls member of the pass so that it can |
| 947 | // successfully use the getAnalysis() method to retrieve the |
| 948 | // implementations it needs. |
| 949 | // |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 950 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 951 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 952 | |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 953 | for (AnalysisUsage::VectorType::const_iterator |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 954 | I = AnUsage->getRequiredSet().begin(), |
| 955 | E = AnUsage->getRequiredSet().end(); I != E; ++I) { |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 956 | Pass *Impl = findAnalysisPass(*I, true); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 957 | if (Impl == 0) |
Devang Patel | 56a5c62 | 2007-04-16 20:44:16 +0000 | [diff] [blame] | 958 | // This may be analysis pass that is initialized on the fly. |
| 959 | // If that is not the case then it will raise an assert when it is used. |
| 960 | continue; |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 961 | AnalysisResolver *AR = P->getResolver(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 962 | assert(AR && "Analysis Resolver is not set"); |
Devang Patel | 984698a | 2006-12-09 01:11:34 +0000 | [diff] [blame] | 963 | AR->addAnalysisImplsPair(*I, Impl); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 967 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 968 | /// then return NULL. |
| 969 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 970 | |
| 971 | // Check if AvailableAnalysis map has one entry. |
| 972 | std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
| 973 | |
| 974 | if (I != AvailableAnalysis.end()) |
| 975 | return I->second; |
| 976 | |
| 977 | // Search Parents through TopLevelManager |
| 978 | if (SearchParent) |
| 979 | return TPM->findAnalysisPass(AID); |
| 980 | |
Devang Patel | 9d759b8 | 2006-12-09 00:09:12 +0000 | [diff] [blame] | 981 | return NULL; |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 984 | // Print list of passes that are last used by P. |
| 985 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
| 986 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 987 | SmallVector<Pass *, 12> LUses; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 988 | |
| 989 | // If this is a on the fly manager then it does not have TPM. |
| 990 | if (!TPM) |
| 991 | return; |
| 992 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 993 | TPM->collectLastUses(LUses, P); |
| 994 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 995 | for (SmallVector<Pass *, 12>::iterator I = LUses.begin(), |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 996 | E = LUses.end(); I != E; ++I) { |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 997 | llvm::errs() << "--" << std::string(Offset*2, ' '); |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 998 | (*I)->dumpPassStructure(0); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | void PMDataManager::dumpPassArguments() const { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1003 | for (SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(), |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1004 | E = PassVector.end(); I != E; ++I) { |
| 1005 | if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I)) |
| 1006 | PMD->dumpPassArguments(); |
| 1007 | else |
| 1008 | if (const PassInfo *PI = (*I)->getPassInfo()) |
| 1009 | if (!PI->isAnalysisGroup()) |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1010 | errs() << " -" << PI->getPassArgument(); |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Chris Lattner | dd6304f | 2007-08-10 06:17:04 +0000 | [diff] [blame] | 1014 | void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, |
| 1015 | enum PassDebuggingString S2, |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1016 | const StringRef &Msg) { |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 1017 | if (PassDebugging < Executions) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1018 | return; |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1019 | errs() << (void*)this << std::string(getDepth()*2+1, ' '); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1020 | switch (S1) { |
| 1021 | case EXECUTION_MSG: |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1022 | errs() << "Executing Pass '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | case MODIFICATION_MSG: |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1025 | errs() << "Made Modification '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1026 | break; |
| 1027 | case FREEING_MSG: |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1028 | errs() << " Freeing Pass '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1029 | break; |
| 1030 | default: |
| 1031 | break; |
| 1032 | } |
| 1033 | switch (S2) { |
| 1034 | case ON_BASICBLOCK_MSG: |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1035 | errs() << "' on BasicBlock '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | case ON_FUNCTION_MSG: |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1038 | errs() << "' on Function '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1039 | break; |
| 1040 | case ON_MODULE_MSG: |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1041 | errs() << "' on Module '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1042 | break; |
| 1043 | case ON_LOOP_MSG: |
Chris Lattner | f82f27b | 2009-09-15 04:45:26 +0000 | [diff] [blame] | 1044 | errs() << "' on Loop '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1045 | break; |
| 1046 | case ON_CG_MSG: |
Chris Lattner | f82f27b | 2009-09-15 04:45:26 +0000 | [diff] [blame] | 1047 | errs() << "' on Call Graph '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1048 | break; |
| 1049 | default: |
| 1050 | break; |
| 1051 | } |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1054 | void PMDataManager::dumpRequiredSet(const Pass *P) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1055 | if (PassDebugging < Details) |
| 1056 | return; |
| 1057 | |
| 1058 | AnalysisUsage analysisUsage; |
| 1059 | P->getAnalysisUsage(analysisUsage); |
| 1060 | dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet()); |
| 1061 | } |
| 1062 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1063 | void PMDataManager::dumpPreservedSet(const Pass *P) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1064 | if (PassDebugging < Details) |
| 1065 | return; |
| 1066 | |
| 1067 | AnalysisUsage analysisUsage; |
| 1068 | P->getAnalysisUsage(analysisUsage); |
| 1069 | dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet()); |
| 1070 | } |
| 1071 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1072 | void PMDataManager::dumpAnalysisUsage(const StringRef &Msg, const Pass *P, |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1073 | const AnalysisUsage::VectorType &Set) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1074 | assert(PassDebugging >= Details); |
| 1075 | if (Set.empty()) |
| 1076 | return; |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1077 | errs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1078 | for (unsigned i = 0; i != Set.size(); ++i) { |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1079 | if (i) errs() << ","; |
| 1080 | errs() << " " << Set[i]->getPassName(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1081 | } |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1082 | errs() << "\n"; |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1083 | } |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1084 | |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1085 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1086 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1087 | /// through getAnalysis interface. |
| 1088 | /// This should be handled by specific pass manager. |
| 1089 | void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 1090 | if (TPM) { |
| 1091 | TPM->dumpArguments(); |
| 1092 | TPM->dumpPasses(); |
| 1093 | } |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1094 | |
| 1095 | // Module Level pass may required Function Level analysis info |
| 1096 | // (e.g. dominator info). Pass manager uses on the fly function pass manager |
| 1097 | // to provide this on demand. In that case, in Pass manager terminology, |
| 1098 | // module level pass is requiring lower level analysis info managed by |
| 1099 | // lower level pass manager. |
| 1100 | |
| 1101 | // When Pass manager is not able to order required analysis info, Pass manager |
| 1102 | // checks whether any lower level manager will be able to provide this |
| 1103 | // analysis info on demand or not. |
Devang Patel | ab85d6b | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1104 | #ifndef NDEBUG |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1105 | errs() << "Unable to schedule '" << RequiredPass->getPassName(); |
| 1106 | errs() << "' required by '" << P->getPassName() << "'\n"; |
Devang Patel | ab85d6b | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1107 | #endif |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1108 | llvm_unreachable("Unable to schedule pass"); |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1111 | // Destructor |
| 1112 | PMDataManager::~PMDataManager() { |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 1113 | for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(), |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1114 | E = PassVector.end(); I != E; ++I) |
| 1115 | delete *I; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1118 | //===----------------------------------------------------------------------===// |
| 1119 | // NOTE: Is this the right place to define this method ? |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 1120 | // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist. |
| 1121 | Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const { |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1122 | return PM.findAnalysisPass(ID, dir); |
| 1123 | } |
| 1124 | |
Devang Patel | 9294281 | 2007-04-16 20:56:24 +0000 | [diff] [blame] | 1125 | Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI, |
| 1126 | Function &F) { |
| 1127 | return PM.getOnTheFlyPass(P, AnalysisPI, F); |
| 1128 | } |
| 1129 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1130 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1131 | // BBPassManager implementation |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1132 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1133 | /// Execute all of the passes scheduled for execution by invoking |
| 1134 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 1135 | /// the function, and if so, return true. |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1136 | bool BBPassManager::runOnFunction(Function &F) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1137 | if (F.isDeclaration()) |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1138 | return false; |
| 1139 | |
Devang Patel | e958559 | 2006-12-08 01:38:28 +0000 | [diff] [blame] | 1140 | bool Changed = doInitialization(F); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1141 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1142 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1143 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1144 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1145 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1146 | dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1147 | dumpRequiredSet(BP); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1148 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1149 | initializeAnalysisImpl(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1151 | { |
| 1152 | // If the pass crashes, remember this. |
| 1153 | PassManagerPrettyStackEntry X(BP, *I); |
| 1154 | |
| 1155 | if (TheTimeInfo) TheTimeInfo->passStarted(BP); |
| 1156 | Changed |= BP->runOnBasicBlock(*I); |
| 1157 | if (TheTimeInfo) TheTimeInfo->passEnded(BP); |
| 1158 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1159 | |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1160 | if (Changed) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1161 | dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1162 | I->getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1163 | dumpPreservedSet(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1164 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1165 | verifyPreservedAnalysis(BP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1166 | removeNotPreservedAnalysis(BP); |
| 1167 | recordAvailableAnalysis(BP); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1168 | removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1169 | } |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1170 | |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 1171 | return Changed |= doFinalization(F); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1174 | // Implement doInitialization and doFinalization |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1175 | bool BBPassManager::doInitialization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1176 | bool Changed = false; |
| 1177 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1178 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1179 | Changed |= getContainedPass(Index)->doInitialization(M); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1180 | |
| 1181 | return Changed; |
| 1182 | } |
| 1183 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1184 | bool BBPassManager::doFinalization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1185 | bool Changed = false; |
| 1186 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1187 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1188 | Changed |= getContainedPass(Index)->doFinalization(M); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1189 | |
| 1190 | return Changed; |
| 1191 | } |
| 1192 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1193 | bool BBPassManager::doInitialization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1194 | bool Changed = false; |
| 1195 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1196 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1197 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1198 | Changed |= BP->doInitialization(F); |
| 1199 | } |
| 1200 | |
| 1201 | return Changed; |
| 1202 | } |
| 1203 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1204 | bool BBPassManager::doFinalization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1205 | bool Changed = false; |
| 1206 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1207 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1208 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1209 | Changed |= BP->doFinalization(F); |
| 1210 | } |
| 1211 | |
| 1212 | return Changed; |
| 1213 | } |
| 1214 | |
| 1215 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1216 | //===----------------------------------------------------------------------===// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1217 | // FunctionPassManager implementation |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1218 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1219 | /// Create new Function pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1220 | FunctionPassManager::FunctionPassManager(ModuleProvider *P) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1221 | FPM = new FunctionPassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1222 | // FPM is the top level manager. |
| 1223 | FPM->setTopLevelManager(FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1224 | |
Dan Gohman | 565df95 | 2008-03-13 02:08:36 +0000 | [diff] [blame] | 1225 | AnalysisResolver *AR = new AnalysisResolver(*FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1226 | FPM->setResolver(AR); |
| 1227 | |
Devang Patel | 1f65368 | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 1228 | MP = P; |
| 1229 | } |
| 1230 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1231 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1232 | delete FPM; |
| 1233 | } |
| 1234 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1235 | /// add - Add a pass to the queue of passes to run. This passes |
| 1236 | /// ownership of the Pass to the PassManager. When the |
| 1237 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 1238 | /// there is no need to delete the pass. (TODO delete passes.) |
| 1239 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1240 | void FunctionPassManager::add(Pass *P) { |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1241 | FPM->add(P); |
| 1242 | } |
| 1243 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1244 | /// run - Execute all of the passes scheduled for execution. Keep |
| 1245 | /// track of whether any of the passes modifies the function, and if |
| 1246 | /// so, return true. |
| 1247 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1248 | bool FunctionPassManager::run(Function &F) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1249 | std::string errstr; |
| 1250 | if (MP->materializeFunction(&F, &errstr)) { |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 1251 | llvm_report_error("Error reading bitcode file: " + errstr); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1252 | } |
Devang Patel | 272908d | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 1253 | return FPM->run(F); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1257 | /// doInitialization - Run all of the initializers for the function passes. |
| 1258 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1259 | bool FunctionPassManager::doInitialization() { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1260 | return FPM->doInitialization(*MP->getModule()); |
| 1261 | } |
| 1262 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 1263 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1264 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1265 | bool FunctionPassManager::doFinalization() { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1266 | return FPM->doFinalization(*MP->getModule()); |
| 1267 | } |
| 1268 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1269 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1270 | // FunctionPassManagerImpl implementation |
| 1271 | // |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1272 | bool FunctionPassManagerImpl::doInitialization(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1273 | bool Changed = false; |
| 1274 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1275 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1276 | Changed |= getContainedManager(Index)->doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1277 | |
| 1278 | return Changed; |
| 1279 | } |
| 1280 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1281 | bool FunctionPassManagerImpl::doFinalization(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1282 | bool Changed = false; |
| 1283 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1284 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1285 | Changed |= getContainedManager(Index)->doFinalization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1286 | |
| 1287 | return Changed; |
| 1288 | } |
| 1289 | |
Devang Patel | ec9c58f | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1290 | /// cleanup - After running all passes, clean up pass manager cache. |
| 1291 | void FPPassManager::cleanup() { |
| 1292 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1293 | FunctionPass *FP = getContainedPass(Index); |
| 1294 | AnalysisResolver *AR = FP->getResolver(); |
| 1295 | assert(AR && "Analysis Resolver is not set"); |
| 1296 | AR->clearAnalysisImpls(); |
| 1297 | } |
| 1298 | } |
| 1299 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1300 | void FunctionPassManagerImpl::releaseMemoryOnTheFly() { |
| 1301 | if (!wasRun) |
| 1302 | return; |
| 1303 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1304 | FPPassManager *FPPM = getContainedManager(Index); |
| 1305 | for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) { |
| 1306 | FPPM->getContainedPass(Index)->releaseMemory(); |
| 1307 | } |
| 1308 | } |
Torok Edwin | 896556e | 2009-06-29 21:05:10 +0000 | [diff] [blame] | 1309 | wasRun = false; |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1312 | // Execute all the passes managed by this top level manager. |
| 1313 | // Return true if any function is modified by a pass. |
| 1314 | bool FunctionPassManagerImpl::run(Function &F) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1315 | bool Changed = false; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1316 | TimingInfo::createTheTimeInfo(); |
| 1317 | |
| 1318 | dumpArguments(); |
| 1319 | dumpPasses(); |
| 1320 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1321 | initializeAllAnalysisInfo(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1322 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1323 | Changed |= getContainedManager(Index)->runOnFunction(F); |
Devang Patel | ec9c58f | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1324 | |
| 1325 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1326 | getContainedManager(Index)->cleanup(); |
| 1327 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1328 | wasRun = true; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1329 | return Changed; |
| 1330 | } |
| 1331 | |
| 1332 | //===----------------------------------------------------------------------===// |
| 1333 | // FPPassManager implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1334 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1335 | char FPPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1336 | /// Print passes managed by this manager |
| 1337 | void FPPassManager::dumpPassStructure(unsigned Offset) { |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1338 | llvm::errs() << std::string(Offset*2, ' ') << "FunctionPass Manager\n"; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1339 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1340 | FunctionPass *FP = getContainedPass(Index); |
| 1341 | FP->dumpPassStructure(Offset + 1); |
| 1342 | dumpLastUses(FP, Offset+1); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1347 | /// Execute all of the passes scheduled for execution by invoking |
| 1348 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 1349 | /// the function, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1350 | bool FPPassManager::runOnFunction(Function &F) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1351 | if (F.isDeclaration()) |
| 1352 | return false; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1353 | |
| 1354 | bool Changed = false; |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1355 | |
Devang Patel | cbbf291 | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1356 | // Collect inherited analysis from Module level pass manager. |
| 1357 | populateInheritedAnalysis(TPM->activeStack); |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1358 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1359 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1360 | FunctionPass *FP = getContainedPass(Index); |
| 1361 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1362 | dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1363 | dumpRequiredSet(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1364 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1365 | initializeAnalysisImpl(FP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1366 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1367 | { |
| 1368 | PassManagerPrettyStackEntry X(FP, F); |
| 1369 | |
| 1370 | if (TheTimeInfo) TheTimeInfo->passStarted(FP); |
| 1371 | Changed |= FP->runOnFunction(F); |
| 1372 | if (TheTimeInfo) TheTimeInfo->passEnded(FP); |
| 1373 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1374 | |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1375 | if (Changed) |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1376 | dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1377 | dumpPreservedSet(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1378 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1379 | verifyPreservedAnalysis(FP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1380 | removeNotPreservedAnalysis(FP); |
| 1381 | recordAvailableAnalysis(FP); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1382 | removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG); |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 1383 | |
Devang Patel | 67c79a4 | 2008-07-01 19:50:56 +0000 | [diff] [blame] | 1384 | // If dominator information is available then verify the info if requested. |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 1385 | verifyDomInfo(*FP, F); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1386 | } |
| 1387 | return Changed; |
| 1388 | } |
| 1389 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1390 | bool FPPassManager::runOnModule(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1391 | bool Changed = doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1393 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 1394 | runOnFunction(*I); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1395 | |
| 1396 | return Changed |= doFinalization(M); |
| 1397 | } |
| 1398 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1399 | bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1400 | bool Changed = false; |
| 1401 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1402 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1403 | Changed |= getContainedPass(Index)->doInitialization(M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1404 | |
| 1405 | return Changed; |
| 1406 | } |
| 1407 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1408 | bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1409 | bool Changed = false; |
| 1410 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1411 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1412 | Changed |= getContainedPass(Index)->doFinalization(M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1413 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1414 | return Changed; |
| 1415 | } |
| 1416 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1417 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1418 | // MPPassManager implementation |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1419 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1420 | /// Execute all of the passes scheduled for execution by invoking |
| 1421 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 1422 | /// the module, and if so, return true. |
| 1423 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1424 | MPPassManager::runOnModule(Module &M) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1425 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1426 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1427 | // Initialize on-the-fly passes |
| 1428 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
| 1429 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 1430 | I != E; ++I) { |
| 1431 | FunctionPassManagerImpl *FPP = I->second; |
| 1432 | Changed |= FPP->doInitialization(M); |
| 1433 | } |
| 1434 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1435 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1436 | ModulePass *MP = getContainedPass(Index); |
| 1437 | |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1438 | dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, |
| 1439 | M.getModuleIdentifier().c_str()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1440 | dumpRequiredSet(MP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1441 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1442 | initializeAnalysisImpl(MP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1443 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1444 | { |
| 1445 | PassManagerPrettyStackEntry X(MP, M); |
| 1446 | if (TheTimeInfo) TheTimeInfo->passStarted(MP); |
| 1447 | Changed |= MP->runOnModule(M); |
| 1448 | if (TheTimeInfo) TheTimeInfo->passEnded(MP); |
| 1449 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1450 | |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1451 | if (Changed) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1452 | dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, |
| 1453 | M.getModuleIdentifier().c_str()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1454 | dumpPreservedSet(MP); |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 1455 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1456 | verifyPreservedAnalysis(MP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1457 | removeNotPreservedAnalysis(MP); |
| 1458 | recordAvailableAnalysis(MP); |
Devang Patel | d305c40 | 2007-08-10 18:29:32 +0000 | [diff] [blame] | 1459 | removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1460 | } |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Finalize on-the-fly passes |
| 1463 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
| 1464 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 1465 | I != E; ++I) { |
| 1466 | FunctionPassManagerImpl *FPP = I->second; |
| 1467 | // We don't know when is the last time an on-the-fly pass is run, |
| 1468 | // so we need to releaseMemory / finalize here |
| 1469 | FPP->releaseMemoryOnTheFly(); |
| 1470 | Changed |= FPP->doFinalization(M); |
| 1471 | } |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1472 | return Changed; |
| 1473 | } |
| 1474 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1475 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1476 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1477 | /// through getAnalysis interface. |
| 1478 | void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1479 | assert(P->getPotentialPassManagerType() == PMT_ModulePassManager && |
| 1480 | "Unable to handle Pass that requires lower level Analysis pass"); |
| 1481 | assert((P->getPotentialPassManagerType() < |
| 1482 | RequiredPass->getPotentialPassManagerType()) && |
| 1483 | "Unable to handle Pass that requires lower level Analysis pass"); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1484 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1485 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1486 | if (!FPP) { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1487 | FPP = new FunctionPassManagerImpl(0); |
| 1488 | // FPP is the top level manager. |
| 1489 | FPP->setTopLevelManager(FPP); |
| 1490 | |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1491 | OnTheFlyManagers[P] = FPP; |
| 1492 | } |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1493 | FPP->add(RequiredPass); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1494 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1495 | // Register P as the last user of RequiredPass. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1496 | SmallVector<Pass *, 12> LU; |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1497 | LU.push_back(RequiredPass); |
| 1498 | FPP->setLastUser(LU, P); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1499 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1500 | |
| 1501 | /// Return function pass corresponding to PassInfo PI, that is |
| 1502 | /// required by module pass MP. Instantiate analysis pass, by using |
| 1503 | /// its runOnFunction() for function F. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1504 | Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F){ |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1505 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1506 | assert(FPP && "Unable to find on the fly pass"); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1507 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1508 | FPP->releaseMemoryOnTheFly(); |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1509 | FPP->run(F); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1510 | return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(PI); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1514 | //===----------------------------------------------------------------------===// |
| 1515 | // PassManagerImpl implementation |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1516 | // |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1517 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1518 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1519 | bool PassManagerImpl::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1520 | bool Changed = false; |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1521 | TimingInfo::createTheTimeInfo(); |
| 1522 | |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 1523 | dumpArguments(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1524 | dumpPasses(); |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1525 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1526 | initializeAllAnalysisInfo(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1527 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1528 | Changed |= getContainedManager(Index)->runOnModule(M); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1529 | return Changed; |
| 1530 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1531 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1532 | //===----------------------------------------------------------------------===// |
| 1533 | // PassManager implementation |
| 1534 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1535 | /// Create new pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1536 | PassManager::PassManager() { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1537 | PM = new PassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1538 | // PM is the top level manager |
| 1539 | PM->setTopLevelManager(PM); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1542 | PassManager::~PassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1543 | delete PM; |
| 1544 | } |
| 1545 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1546 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 1547 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 1548 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 1549 | /// implies that all passes MUST be allocated with 'new'. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1550 | void PassManager::add(Pass *P) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1551 | PM->add(P); |
| 1552 | } |
| 1553 | |
| 1554 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1555 | /// whether any of the passes modifies the module, and if so, return true. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1556 | bool PassManager::run(Module &M) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1557 | return PM->run(M); |
| 1558 | } |
| 1559 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1560 | //===----------------------------------------------------------------------===// |
| 1561 | // TimingInfo Class - This class is used to calculate information about the |
| 1562 | // amount of time each pass takes to execute. This only happens with |
| 1563 | // -time-passes is enabled on the command line. |
| 1564 | // |
| 1565 | bool llvm::TimePassesIsEnabled = false; |
| 1566 | static cl::opt<bool,true> |
| 1567 | EnableTiming("time-passes", cl::location(TimePassesIsEnabled), |
| 1568 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
| 1569 | |
| 1570 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 1571 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 1572 | // null. It may be called multiple times. |
| 1573 | void TimingInfo::createTheTimeInfo() { |
| 1574 | if (!TimePassesIsEnabled || TheTimeInfo) return; |
| 1575 | |
| 1576 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 1577 | // This guarantees that the object will be constructed before static globals, |
| 1578 | // thus it will be destroyed before them. |
| 1579 | static ManagedStatic<TimingInfo> TTI; |
| 1580 | TheTimeInfo = &*TTI; |
| 1581 | } |
| 1582 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1583 | /// If TimingInfo is enabled then start pass timer. |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1584 | void llvm::StartPassTimer(Pass *P) { |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1585 | if (TheTimeInfo) |
| 1586 | TheTimeInfo->passStarted(P); |
| 1587 | } |
| 1588 | |
| 1589 | /// If TimingInfo is enabled then stop pass timer. |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 1590 | void llvm::StopPassTimer(Pass *P) { |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1591 | if (TheTimeInfo) |
| 1592 | TheTimeInfo->passEnded(P); |
| 1593 | } |
| 1594 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1595 | //===----------------------------------------------------------------------===// |
| 1596 | // PMStack implementation |
| 1597 | // |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 1598 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1599 | // Pop Pass Manager from the stack and clear its analysis info. |
| 1600 | void PMStack::pop() { |
| 1601 | |
| 1602 | PMDataManager *Top = this->top(); |
| 1603 | Top->initializeAnalysisInfo(); |
| 1604 | |
| 1605 | S.pop_back(); |
| 1606 | } |
| 1607 | |
| 1608 | // Push PM on the stack and set its top level manager. |
Dan Gohman | 11eecd6 | 2008-03-13 01:21:31 +0000 | [diff] [blame] | 1609 | void PMStack::push(PMDataManager *PM) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1610 | assert(PM && "Unable to push. Pass Manager expected"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1611 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1612 | if (!this->empty()) { |
| 1613 | PMTopLevelManager *TPM = this->top()->getTopLevelManager(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1614 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1615 | assert(TPM && "Unable to find top level manager"); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1616 | TPM->addIndirectPassManager(PM); |
| 1617 | PM->setTopLevelManager(TPM); |
| 1618 | } |
| 1619 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1620 | S.push_back(PM); |
| 1621 | } |
| 1622 | |
| 1623 | // Dump content of the pass manager stack. |
| 1624 | void PMStack::dump() { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1625 | for (std::deque<PMDataManager *>::iterator I = S.begin(), |
| 1626 | E = S.end(); I != E; ++I) |
| 1627 | printf("%s ", dynamic_cast<Pass *>(*I)->getPassName()); |
| 1628 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1629 | if (!S.empty()) |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1630 | printf("\n"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1633 | /// Find appropriate Module Pass Manager in the PM Stack and |
| 1634 | /// add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1635 | void ModulePass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1636 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1637 | // Find Module Pass Manager |
| 1638 | while(!PMS.empty()) { |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1639 | PassManagerType TopPMType = PMS.top()->getPassManagerType(); |
| 1640 | if (TopPMType == PreferredType) |
| 1641 | break; // We found desired pass manager |
| 1642 | else if (TopPMType > PMT_ModulePassManager) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1643 | PMS.pop(); // Pop children pass managers |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1644 | else |
| 1645 | break; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1646 | } |
Devang Patel | 18ff636 | 2008-09-09 21:38:40 +0000 | [diff] [blame] | 1647 | assert(!PMS.empty() && "Unable to find appropriate Pass Manager"); |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1648 | PMS.top()->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1651 | /// Find appropriate Function Pass Manager or Call Graph Pass Manager |
| 1652 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1653 | void FunctionPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1654 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1655 | |
Devang Patel | a328690 | 2008-09-09 17:56:50 +0000 | [diff] [blame] | 1656 | // Find Module Pass Manager |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1657 | while(!PMS.empty()) { |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1658 | if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) |
| 1659 | PMS.pop(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1660 | else |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | } |
| 1663 | FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top()); |
| 1664 | |
| 1665 | // Create new Function Pass Manager |
| 1666 | if (!FPP) { |
| 1667 | assert(!PMS.empty() && "Unable to create Function Pass Manager"); |
| 1668 | PMDataManager *PMD = PMS.top(); |
| 1669 | |
| 1670 | // [1] Create new Function Pass Manager |
| 1671 | FPP = new FPPassManager(PMD->getDepth() + 1); |
Devang Patel | cbbf291 | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1672 | FPP->populateInheritedAnalysis(PMS); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1673 | |
| 1674 | // [2] Set up new manager's top level manager |
| 1675 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1676 | TPM->addIndirectPassManager(FPP); |
| 1677 | |
| 1678 | // [3] Assign manager to manage this new manager. This may create |
| 1679 | // and push new managers into PMS |
Devang Patel | a328690 | 2008-09-09 17:56:50 +0000 | [diff] [blame] | 1680 | FPP->assignPassManager(PMS, PMD->getPassManagerType()); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1681 | |
| 1682 | // [4] Push new manager into PMS |
| 1683 | PMS.push(FPP); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1686 | // Assign FPP as the manager of this pass. |
| 1687 | FPP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1690 | /// Find appropriate Basic Pass Manager or Call Graph Pass Manager |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1691 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1692 | void BasicBlockPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1693 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1694 | BBPassManager *BBP = NULL; |
| 1695 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1696 | // Basic Pass Manager is a leaf pass manager. It does not handle |
| 1697 | // any other pass manager. |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1698 | if (!PMS.empty()) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1699 | BBP = dynamic_cast<BBPassManager *>(PMS.top()); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1700 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1701 | // If leaf manager is not Basic Block Pass manager then create new |
| 1702 | // basic Block Pass manager. |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1703 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1704 | if (!BBP) { |
| 1705 | assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); |
| 1706 | PMDataManager *PMD = PMS.top(); |
| 1707 | |
| 1708 | // [1] Create new Basic Block Manager |
| 1709 | BBP = new BBPassManager(PMD->getDepth() + 1); |
| 1710 | |
| 1711 | // [2] Set up new manager's top level manager |
| 1712 | // Basic Block Pass Manager does not live by itself |
| 1713 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1714 | TPM->addIndirectPassManager(BBP); |
| 1715 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1716 | // [3] Assign manager to manage this new manager. This may create |
| 1717 | // and push new managers into PMS |
Dan Gohman | 565df95 | 2008-03-13 02:08:36 +0000 | [diff] [blame] | 1718 | BBP->assignPassManager(PMS); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1719 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1720 | // [4] Push new manager into PMS |
| 1721 | PMS.push(BBP); |
| 1722 | } |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1723 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1724 | // Assign BBP as the manager of this pass. |
| 1725 | BBP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Dan Gohman | d3a20c9 | 2008-03-11 16:41:42 +0000 | [diff] [blame] | 1728 | PassManagerBase::~PassManagerBase() {} |
Gordon Henriksen | 878114b | 2008-03-16 04:20:44 +0000 | [diff] [blame] | 1729 | |
| 1730 | /*===-- C Bindings --------------------------------------------------------===*/ |
| 1731 | |
| 1732 | LLVMPassManagerRef LLVMCreatePassManager() { |
| 1733 | return wrap(new PassManager()); |
| 1734 | } |
| 1735 | |
| 1736 | LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { |
| 1737 | return wrap(new FunctionPassManager(unwrap(P))); |
| 1738 | } |
| 1739 | |
| 1740 | int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { |
| 1741 | return unwrap<PassManager>(PM)->run(*unwrap(M)); |
| 1742 | } |
| 1743 | |
| 1744 | int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { |
| 1745 | return unwrap<FunctionPassManager>(FPM)->doInitialization(); |
| 1746 | } |
| 1747 | |
| 1748 | int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { |
| 1749 | return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); |
| 1750 | } |
| 1751 | |
| 1752 | int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { |
| 1753 | return unwrap<FunctionPassManager>(FPM)->doFinalization(); |
| 1754 | } |
| 1755 | |
| 1756 | void LLVMDisposePassManager(LLVMPassManagerRef PM) { |
| 1757 | delete unwrap(PM); |
| 1758 | } |