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