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