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