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