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