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