Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 1 | //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the LLVM Pass infrastructure. It is primarily |
| 11 | // responsible with ensuring that passes are executed and batched together |
| 12 | // optimally. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 16 | #include "llvm/PassManager.h" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 17 | #include "PassManagerT.h" // PassManagerT implementation |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 19 | #include "llvm/ModuleProvider.h" |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 20 | #include "Support/STLExtras.h" |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 21 | #include "Support/TypeInfo.h" |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 22 | #include <set> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 675e7a9 | 2002-08-21 23:51:51 +0000 | [diff] [blame] | 25 | // IncludeFile - Stub function used to help linking out. |
| 26 | IncludeFile::IncludeFile(void*) {} |
| 27 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // AnalysisID Class Implementation |
| 30 | // |
| 31 | |
Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 32 | // getCFGOnlyAnalyses - A wrapper around the CFGOnlyAnalyses which make it |
| 33 | // initializer order independent. |
| 34 | static std::vector<const PassInfo*> &getCFGOnlyAnalyses() { |
| 35 | static std::vector<const PassInfo*> CFGOnlyAnalyses; |
| 36 | return CFGOnlyAnalyses; |
| 37 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 38 | |
Chris Lattner | dd99f5b | 2003-10-12 18:52:12 +0000 | [diff] [blame] | 39 | void RegisterPassBase::setOnlyUsesCFG() { |
Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 40 | getCFGOnlyAnalyses().push_back(PIObj); |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // AnalysisResolver Class Implementation |
| 45 | // |
| 46 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 47 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 48 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 49 | P->Resolver = AR; |
| 50 | } |
| 51 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
| 53 | // AnalysisUsage Class Implementation |
| 54 | // |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 56 | // setPreservesCFG - This function should be called to by the pass, iff they do |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 57 | // not: |
| 58 | // |
| 59 | // 1. Add or remove basic blocks from the function |
| 60 | // 2. Modify terminator instructions in any way. |
| 61 | // |
| 62 | // This function annotates the AnalysisUsage info object to say that analyses |
| 63 | // that only depend on the CFG are preserved by this pass. |
| 64 | // |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 65 | void AnalysisUsage::setPreservesCFG() { |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 66 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 67 | // that only depend on the CFG (like dominators, loop info, etc...) |
| 68 | // |
| 69 | Preserved.insert(Preserved.end(), |
Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 70 | getCFGOnlyAnalyses().begin(), getCFGOnlyAnalyses().end()); |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | // PassManager implementation - The PassManager class is a simple Pimpl class |
| 76 | // that wraps the PassManagerT template. |
| 77 | // |
| 78 | PassManager::PassManager() : PM(new PassManagerT<Module>()) {} |
| 79 | PassManager::~PassManager() { delete PM; } |
| 80 | void PassManager::add(Pass *P) { PM->add(P); } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 81 | bool PassManager::run(Module &M) { return PM->run(M); } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 82 | |
Brian Gaeke | b7a8388 | 2003-08-12 17:22:39 +0000 | [diff] [blame] | 83 | //===----------------------------------------------------------------------===// |
| 84 | // FunctionPassManager implementation - The FunctionPassManager class |
| 85 | // is a simple Pimpl class that wraps the PassManagerT template. It |
| 86 | // is like PassManager, but only deals in FunctionPasses. |
| 87 | // |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 88 | FunctionPassManager::FunctionPassManager(ModuleProvider *P) : |
| 89 | PM(new PassManagerT<Function>()), MP(P) {} |
Brian Gaeke | b7a8388 | 2003-08-12 17:22:39 +0000 | [diff] [blame] | 90 | FunctionPassManager::~FunctionPassManager() { delete PM; } |
| 91 | void FunctionPassManager::add(FunctionPass *P) { PM->add(P); } |
Brian Gaeke | 2cc4b9d | 2003-08-14 06:07:57 +0000 | [diff] [blame] | 92 | void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); } |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 93 | bool FunctionPassManager::run(Function &F) { |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 94 | MP->materializeFunction(&F); |
| 95 | return PM->run(F); |
| 96 | } |
Brian Gaeke | b7a8388 | 2003-08-12 17:22:39 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 98 | |
| 99 | //===----------------------------------------------------------------------===// |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 100 | // TimingInfo Class - This class is used to calculate information about the |
| 101 | // amount of time each pass takes to execute. This only happens with |
| 102 | // -time-passes is enabled on the command line. |
| 103 | // |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 104 | static cl::opt<bool> |
| 105 | EnableTiming("time-passes", |
| 106 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 107 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 108 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 109 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 110 | // null. It may be called multiple times. |
| 111 | void TimingInfo::createTheTimeInfo() { |
| 112 | if (!EnableTiming || TheTimeInfo) return; |
| 113 | |
| 114 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 115 | // This guarantees that the object will be constructed before static globals, |
| 116 | // thus it will be destroyed before them. |
| 117 | static TimingInfo TTI; |
| 118 | TheTimeInfo = &TTI; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 121 | void PMDebug::PrintArgumentInformation(const Pass *P) { |
| 122 | // Print out passes in pass manager... |
| 123 | if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) { |
| 124 | for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i) |
| 125 | PrintArgumentInformation(PM->getContainedPass(i)); |
| 126 | |
| 127 | } else { // Normal pass. Print argument information... |
| 128 | // Print out arguments for registered passes that are _optimizations_ |
| 129 | if (const PassInfo *PI = P->getPassInfo()) |
| 130 | if (PI->getPassType() & PassInfo::Optimization) |
| 131 | std::cerr << " -" << PI->getPassArgument(); |
| 132 | } |
| 133 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 134 | |
| 135 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
Chris Lattner | 43640d7 | 2004-02-29 22:37:04 +0000 | [diff] [blame^] | 136 | Pass *P, Module *M) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 137 | if (PassDebugging >= Executions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 138 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 139 | << P->getPassName(); |
Chris Lattner | 43640d7 | 2004-02-29 22:37:04 +0000 | [diff] [blame^] | 140 | if (M) std::cerr << "' on Module '" << M->getModuleIdentifier() << "'\n"; |
| 141 | std::cerr << "'...\n"; |
| 142 | } |
| 143 | } |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 43640d7 | 2004-02-29 22:37:04 +0000 | [diff] [blame^] | 145 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
| 146 | Pass *P, Function *F) { |
| 147 | if (PassDebugging >= Executions) { |
| 148 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
| 149 | << P->getPassName(); |
| 150 | if (F) std::cerr << "' on Function '" << F->getName(); |
| 151 | std::cerr << "'...\n"; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
| 156 | Pass *P, BasicBlock *BB) { |
| 157 | if (PassDebugging >= Executions) { |
| 158 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
| 159 | << P->getPassName(); |
| 160 | if (BB) std::cerr << "' on BasicBlock '" << BB->getName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 161 | std::cerr << "'...\n"; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 166 | Pass *P, const std::vector<AnalysisID> &Set){ |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 167 | if (PassDebugging >= Details && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 168 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 169 | for (unsigned i = 0; i != Set.size(); ++i) { |
| 170 | if (i) std::cerr << ","; |
| 171 | std::cerr << " " << Set[i]->getPassName(); |
| 172 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 173 | std::cerr << "\n"; |
| 174 | } |
| 175 | } |
| 176 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 177 | //===----------------------------------------------------------------------===// |
| 178 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 179 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 180 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 181 | void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) { |
| 182 | PM->addPass(this, AU); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 9ad7757 | 2003-03-21 21:41:02 +0000 | [diff] [blame] | 185 | bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const { |
| 186 | return Resolver->getAnalysisToUpdate(AnalysisID) != 0; |
| 187 | } |
| 188 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 189 | // dumpPassStructure - Implement the -debug-passes=Structure option |
| 190 | void Pass::dumpPassStructure(unsigned Offset) { |
| 191 | std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; |
| 192 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 193 | |
Brian Gaeke | 465a5cc | 2004-02-28 21:55:18 +0000 | [diff] [blame] | 194 | // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass. |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 195 | // |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 196 | const char *Pass::getPassName() const { |
| 197 | if (const PassInfo *PI = getPassInfo()) |
| 198 | return PI->getPassName(); |
| 199 | return typeid(*this).name(); |
| 200 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 201 | |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 202 | // print - Print out the internal state of the pass. This is called by Analyze |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 203 | // to print out the contents of an analysis. Otherwise it is not necessary to |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 204 | // implement this method. |
| 205 | // |
| 206 | void Pass::print(std::ostream &O) const { |
| 207 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; |
| 208 | } |
| 209 | |
| 210 | // dump - call print(std::cerr); |
| 211 | void Pass::dump() const { |
| 212 | print(std::cerr, 0); |
| 213 | } |
| 214 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 216 | // ImmutablePass Implementation |
| 217 | // |
| 218 | void ImmutablePass::addToPassManager(PassManagerT<Module> *PM, |
| 219 | AnalysisUsage &AU) { |
| 220 | PM->addPass(this, AU); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 225 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 226 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 227 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 228 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 229 | // for every function in the module, then by finalizing. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 230 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 231 | bool FunctionPass::run(Module &M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 232 | bool Changed = doInitialization(M); |
| 233 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 234 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 235 | if (!I->isExternal()) // Passes are not run on external functions! |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 236 | Changed |= runOnFunction(*I); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 237 | |
| 238 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 241 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 242 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 243 | bool FunctionPass::run(Function &F) { |
| 244 | if (F.isExternal()) return false;// Passes are not run on external functions! |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 246 | return doInitialization(*F.getParent()) | runOnFunction(F) |
| 247 | | doFinalization(*F.getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 249 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 250 | void FunctionPass::addToPassManager(PassManagerT<Module> *PM, |
| 251 | AnalysisUsage &AU) { |
| 252 | PM->addPass(this, AU); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 253 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 254 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 255 | void FunctionPass::addToPassManager(PassManagerT<Function> *PM, |
| 256 | AnalysisUsage &AU) { |
| 257 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | // BasicBlockPass Implementation |
| 262 | // |
| 263 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 264 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 265 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 266 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 267 | bool BasicBlockPass::runOnFunction(Function &F) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 268 | bool Changed = doInitialization(F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 269 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 270 | Changed |= runOnBasicBlock(*I); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 271 | return Changed | doFinalization(F); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 275 | // finalize. |
| 276 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 277 | bool BasicBlockPass::run(BasicBlock &BB) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 278 | Function &F = *BB.getParent(); |
| 279 | Module &M = *F.getParent(); |
| 280 | return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) | |
| 281 | doFinalization(F) | doFinalization(M); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 284 | void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 285 | AnalysisUsage &AU) { |
| 286 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 290 | AnalysisUsage &AU) { |
| 291 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 294 | |
| 295 | //===----------------------------------------------------------------------===// |
| 296 | // Pass Registration mechanism |
| 297 | // |
| 298 | static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0; |
| 299 | static std::vector<PassRegistrationListener*> *Listeners = 0; |
| 300 | |
| 301 | // getPassInfo - Return the PassInfo data structure that corresponds to this |
| 302 | // pass... |
| 303 | const PassInfo *Pass::getPassInfo() const { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 304 | if (PassInfoCache) return PassInfoCache; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 305 | return lookupPassInfo(typeid(*this)); |
| 306 | } |
| 307 | |
| 308 | const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 309 | if (PassInfoMap == 0) return 0; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 310 | std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI); |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 311 | return (I != PassInfoMap->end()) ? I->second : 0; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void RegisterPassBase::registerPass(PassInfo *PI) { |
| 315 | if (PassInfoMap == 0) |
| 316 | PassInfoMap = new std::map<TypeInfo, PassInfo*>(); |
| 317 | |
| 318 | assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && |
| 319 | "Pass already registered!"); |
| 320 | PIObj = PI; |
| 321 | PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); |
| 322 | |
| 323 | // Notify any listeners... |
| 324 | if (Listeners) |
| 325 | for (std::vector<PassRegistrationListener*>::iterator |
| 326 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 327 | (*I)->passRegistered(PI); |
| 328 | } |
| 329 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 330 | void RegisterPassBase::unregisterPass(PassInfo *PI) { |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 331 | assert(PassInfoMap && "Pass registered but not in map!"); |
| 332 | std::map<TypeInfo, PassInfo*>::iterator I = |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 333 | PassInfoMap->find(PI->getTypeInfo()); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 334 | assert(I != PassInfoMap->end() && "Pass registered but not in map!"); |
| 335 | |
| 336 | // Remove pass from the map... |
| 337 | PassInfoMap->erase(I); |
| 338 | if (PassInfoMap->empty()) { |
| 339 | delete PassInfoMap; |
| 340 | PassInfoMap = 0; |
| 341 | } |
| 342 | |
| 343 | // Notify any listeners... |
| 344 | if (Listeners) |
| 345 | for (std::vector<PassRegistrationListener*>::iterator |
| 346 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 347 | (*I)->passUnregistered(PI); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 348 | |
| 349 | // Delete the PassInfo object itself... |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 350 | delete PI; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 353 | //===----------------------------------------------------------------------===// |
| 354 | // Analysis Group Implementation Code |
| 355 | //===----------------------------------------------------------------------===// |
| 356 | |
| 357 | struct AnalysisGroupInfo { |
| 358 | const PassInfo *DefaultImpl; |
| 359 | std::set<const PassInfo *> Implementations; |
| 360 | AnalysisGroupInfo() : DefaultImpl(0) {} |
| 361 | }; |
| 362 | |
| 363 | static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0; |
| 364 | |
| 365 | // RegisterAGBase implementation |
| 366 | // |
| 367 | RegisterAGBase::RegisterAGBase(const std::type_info &Interface, |
| 368 | const std::type_info *Pass, bool isDefault) |
| 369 | : ImplementationInfo(0), isDefaultImplementation(isDefault) { |
| 370 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 371 | InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface)); |
| 372 | if (InterfaceInfo == 0) { // First reference to Interface, add it now. |
| 373 | InterfaceInfo = // Create the new PassInfo for the interface... |
| 374 | new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0); |
| 375 | registerPass(InterfaceInfo); |
| 376 | PIObj = 0; |
| 377 | } |
| 378 | assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup && |
| 379 | "Trying to join an analysis group that is a normal pass!"); |
| 380 | |
| 381 | if (Pass) { |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 382 | ImplementationInfo = Pass::lookupPassInfo(*Pass); |
| 383 | assert(ImplementationInfo && |
| 384 | "Must register pass before adding to AnalysisGroup!"); |
| 385 | |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 386 | // Make sure we keep track of the fact that the implementation implements |
| 387 | // the interface. |
| 388 | PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo); |
| 389 | IIPI->addInterfaceImplemented(InterfaceInfo); |
| 390 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 391 | // Lazily allocate to avoid nasty initialization order dependencies |
| 392 | if (AnalysisGroupInfoMap == 0) |
| 393 | AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>(); |
| 394 | |
| 395 | AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo]; |
| 396 | assert(AGI.Implementations.count(ImplementationInfo) == 0 && |
| 397 | "Cannot add a pass to the same analysis group more than once!"); |
| 398 | AGI.Implementations.insert(ImplementationInfo); |
| 399 | if (isDefault) { |
| 400 | assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 && |
| 401 | "Default implementation for analysis group already specified!"); |
| 402 | assert(ImplementationInfo->getNormalCtor() && |
| 403 | "Cannot specify pass as default if it does not have a default ctor"); |
| 404 | AGI.DefaultImpl = ImplementationInfo; |
| 405 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void RegisterAGBase::setGroupName(const char *Name) { |
| 411 | assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!"); |
| 412 | InterfaceInfo->setPassName(Name); |
| 413 | } |
| 414 | |
| 415 | RegisterAGBase::~RegisterAGBase() { |
| 416 | if (ImplementationInfo) { |
| 417 | assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?"); |
| 418 | AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo]; |
| 419 | |
| 420 | assert(AGI.Implementations.count(ImplementationInfo) && |
| 421 | "Pass not a member of analysis group?"); |
| 422 | |
| 423 | if (AGI.DefaultImpl == ImplementationInfo) |
| 424 | AGI.DefaultImpl = 0; |
| 425 | |
| 426 | AGI.Implementations.erase(ImplementationInfo); |
| 427 | |
| 428 | // Last member of this analysis group? Unregister PassInfo, delete map entry |
| 429 | if (AGI.Implementations.empty()) { |
| 430 | assert(AGI.DefaultImpl == 0 && |
| 431 | "Default implementation didn't unregister?"); |
| 432 | AnalysisGroupInfoMap->erase(InterfaceInfo); |
| 433 | if (AnalysisGroupInfoMap->empty()) { // Delete map if empty |
| 434 | delete AnalysisGroupInfoMap; |
| 435 | AnalysisGroupInfoMap = 0; |
| 436 | } |
| 437 | |
| 438 | unregisterPass(InterfaceInfo); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 444 | //===----------------------------------------------------------------------===// |
| 445 | // PassRegistrationListener implementation |
| 446 | // |
| 447 | |
| 448 | // PassRegistrationListener ctor - Add the current object to the list of |
| 449 | // PassRegistrationListeners... |
| 450 | PassRegistrationListener::PassRegistrationListener() { |
| 451 | if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); |
| 452 | Listeners->push_back(this); |
| 453 | } |
| 454 | |
| 455 | // dtor - Remove object from list of listeners... |
| 456 | PassRegistrationListener::~PassRegistrationListener() { |
| 457 | std::vector<PassRegistrationListener*>::iterator I = |
| 458 | std::find(Listeners->begin(), Listeners->end(), this); |
| 459 | assert(Listeners && I != Listeners->end() && |
| 460 | "PassRegistrationListener not registered!"); |
| 461 | Listeners->erase(I); |
| 462 | |
| 463 | if (Listeners->empty()) { |
| 464 | delete Listeners; |
| 465 | Listeners = 0; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // enumeratePasses - Iterate over the registered passes, calling the |
| 470 | // passEnumerate callback on each PassInfo object. |
| 471 | // |
| 472 | void PassRegistrationListener::enumeratePasses() { |
| 473 | if (PassInfoMap) |
| 474 | for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(), |
| 475 | E = PassInfoMap->end(); I != E; ++I) |
| 476 | passEnumerate(I->second); |
| 477 | } |