Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 1 | //===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===// |
| 2 | // |
| 3 | // This file implements the LLVM Pass infrastructure. It is primarily |
| 4 | // responsible with ensuring that passes are executed and batched together |
| 5 | // optimally. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 9 | #include "llvm/PassManager.h" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 10 | #include "PassManagerT.h" // PassManagerT implementation |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 11 | #include "llvm/Module.h" |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 12 | #include "Support/STLExtras.h" |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 13 | #include "Support/TypeInfo.h" |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 15 | #include <sys/resource.h> |
| 16 | #include <sys/unistd.h> |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | // AnalysisID Class Implementation |
| 20 | // |
| 21 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 22 | static std::vector<const PassInfo*> CFGOnlyAnalyses; |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 24 | void RegisterPassBase::setPreservesCFG() { |
| 25 | CFGOnlyAnalyses.push_back(PIObj); |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // AnalysisResolver Class Implementation |
| 30 | // |
| 31 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 32 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 33 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 34 | P->Resolver = AR; |
| 35 | } |
| 36 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | // AnalysisUsage Class Implementation |
| 39 | // |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 40 | |
| 41 | // preservesCFG - This function should be called to by the pass, iff they do |
| 42 | // not: |
| 43 | // |
| 44 | // 1. Add or remove basic blocks from the function |
| 45 | // 2. Modify terminator instructions in any way. |
| 46 | // |
| 47 | // This function annotates the AnalysisUsage info object to say that analyses |
| 48 | // that only depend on the CFG are preserved by this pass. |
| 49 | // |
| 50 | void AnalysisUsage::preservesCFG() { |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 51 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 52 | // that only depend on the CFG (like dominators, loop info, etc...) |
| 53 | // |
| 54 | Preserved.insert(Preserved.end(), |
| 55 | CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end()); |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
| 60 | // PassManager implementation - The PassManager class is a simple Pimpl class |
| 61 | // that wraps the PassManagerT template. |
| 62 | // |
| 63 | PassManager::PassManager() : PM(new PassManagerT<Module>()) {} |
| 64 | PassManager::~PassManager() { delete PM; } |
| 65 | void PassManager::add(Pass *P) { PM->add(P); } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 66 | bool PassManager::run(Module &M) { return PM->run(M); } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 68 | |
| 69 | //===----------------------------------------------------------------------===// |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 70 | // TimingInfo Class - This class is used to calculate information about the |
| 71 | // amount of time each pass takes to execute. This only happens with |
| 72 | // -time-passes is enabled on the command line. |
| 73 | // |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 74 | static cl::opt<bool> |
| 75 | EnableTiming("time-passes", |
| 76 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 78 | static TimeRecord getTimeRecord() { |
| 79 | static unsigned long PageSize = 0; |
| 80 | |
| 81 | if (PageSize == 0) { |
| 82 | #ifdef _SC_PAGE_SIZE |
| 83 | PageSize = sysconf(_SC_PAGE_SIZE); |
| 84 | #else |
| 85 | #ifdef _SC_PAGESIZE |
| 86 | PageSize = sysconf(_SC_PAGESIZE); |
| 87 | #else |
| 88 | PageSize = getpagesize(); |
| 89 | #endif |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | struct rusage RU; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 94 | struct timeval T; |
| 95 | gettimeofday(&T, 0); |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 96 | if (getrusage(RUSAGE_SELF, &RU)) { |
| 97 | perror("getrusage call failed: -time-passes info incorrect!"); |
| 98 | } |
| 99 | |
| 100 | TimeRecord Result; |
| 101 | Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0; |
| 102 | Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0; |
| 103 | Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0; |
| 104 | Result.MaxRSS = RU.ru_maxrss*PageSize; |
| 105 | |
| 106 | return Result; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chris Lattner | e821d78 | 2002-08-20 18:47:53 +0000 | [diff] [blame] | 109 | bool TimeRecord::operator<(const TimeRecord &TR) const { |
| 110 | // Primary sort key is User+System time |
| 111 | if (UserTime+SystemTime < TR.UserTime+TR.SystemTime) |
| 112 | return true; |
| 113 | if (UserTime+SystemTime > TR.UserTime+TR.SystemTime) |
| 114 | return false; |
| 115 | |
| 116 | // Secondary sort key is Wall Time |
| 117 | return Elapsed < TR.Elapsed; |
| 118 | } |
| 119 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 120 | void TimeRecord::passStart(const TimeRecord &T) { |
| 121 | Elapsed -= T.Elapsed; |
| 122 | UserTime -= T.UserTime; |
| 123 | SystemTime -= T.SystemTime; |
| 124 | RSSTemp = T.MaxRSS; |
| 125 | } |
| 126 | |
| 127 | void TimeRecord::passEnd(const TimeRecord &T) { |
| 128 | Elapsed += T.Elapsed; |
| 129 | UserTime += T.UserTime; |
| 130 | SystemTime += T.SystemTime; |
| 131 | RSSTemp = T.MaxRSS - RSSTemp; |
| 132 | MaxRSS = std::max(MaxRSS, RSSTemp); |
| 133 | } |
| 134 | |
Chris Lattner | 5ec216b | 2002-08-19 15:43:33 +0000 | [diff] [blame] | 135 | static void printVal(double Val, double Total) { |
| 136 | if (Total < 1e-7) // Avoid dividing by zero... |
Chris Lattner | ca5afe7 | 2002-08-19 20:42:12 +0000 | [diff] [blame] | 137 | fprintf(stderr, " ----- "); |
Chris Lattner | 5ec216b | 2002-08-19 15:43:33 +0000 | [diff] [blame] | 138 | else |
| 139 | fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total); |
| 140 | } |
| 141 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 142 | void TimeRecord::print(const char *PassName, const TimeRecord &Total) const { |
Chris Lattner | 5ec216b | 2002-08-19 15:43:33 +0000 | [diff] [blame] | 143 | printVal(UserTime, Total.UserTime); |
| 144 | printVal(SystemTime, Total.SystemTime); |
| 145 | printVal(UserTime+SystemTime, Total.UserTime+Total.SystemTime); |
| 146 | printVal(Elapsed, Total.Elapsed); |
| 147 | |
| 148 | fprintf(stderr, " "); |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 149 | |
| 150 | if (Total.MaxRSS) |
| 151 | std::cerr << MaxRSS << "\t"; |
| 152 | std::cerr << PassName << "\n"; |
| 153 | } |
| 154 | |
| 155 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 156 | // Create method. If Timing is enabled, this creates and returns a new timing |
| 157 | // object, otherwise it returns null. |
| 158 | // |
| 159 | TimingInfo *TimingInfo::create() { |
| 160 | return EnableTiming ? new TimingInfo() : 0; |
| 161 | } |
| 162 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 163 | void TimingInfo::passStarted(Pass *P) { |
| 164 | TimingData[P].passStart(getTimeRecord()); |
| 165 | } |
| 166 | void TimingInfo::passEnded(Pass *P) { |
| 167 | TimingData[P].passEnd(getTimeRecord()); |
| 168 | } |
| 169 | void TimeRecord::sum(const TimeRecord &TR) { |
| 170 | Elapsed += TR.Elapsed; |
| 171 | UserTime += TR.UserTime; |
| 172 | SystemTime += TR.SystemTime; |
| 173 | MaxRSS += TR.MaxRSS; |
| 174 | } |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 175 | |
| 176 | // TimingDtor - Print out information about timing information |
| 177 | TimingInfo::~TimingInfo() { |
| 178 | // Iterate over all of the data, converting it into the dual of the data map, |
| 179 | // so that the data is sorted by amount of time taken, instead of pointer. |
| 180 | // |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 181 | std::vector<std::pair<TimeRecord, Pass*> > Data; |
| 182 | TimeRecord Total; |
| 183 | for (std::map<Pass*, TimeRecord>::iterator I = TimingData.begin(), |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 184 | E = TimingData.end(); I != E; ++I) |
| 185 | // Throw out results for "grouping" pass managers... |
| 186 | if (!dynamic_cast<AnalysisResolver*>(I->first)) { |
| 187 | Data.push_back(std::make_pair(I->second, I->first)); |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 188 | Total.sum(I->second); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Sort the data by time as the primary key, in reverse order... |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 192 | std::sort(Data.begin(), Data.end(), |
| 193 | std::greater<std::pair<TimeRecord, Pass*> >()); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 194 | |
| 195 | // Print out timing header... |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 196 | std::cerr << std::string(79, '=') << "\n" |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 197 | << " ... Pass execution timing report ...\n" |
| 198 | << std::string(79, '=') << "\n Total Execution Time: " |
| 199 | << (Total.UserTime+Total.SystemTime) << " seconds (" |
| 200 | << Total.Elapsed << " wall clock)\n\n ---User Time--- " |
| 201 | << "--System Time-- --User+System-- ---Wall Time---"; |
| 202 | |
| 203 | if (Total.MaxRSS) |
| 204 | std::cerr << " ---Mem---"; |
| 205 | std::cerr << " --- Pass Name ---\n"; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 206 | |
| 207 | // Loop through all of the timing data, printing it out... |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 208 | for (unsigned i = 0, e = Data.size(); i != e; ++i) |
| 209 | Data[i].first.print(Data[i].second->getPassName(), Total); |
| 210 | |
| 211 | Total.print("TOTAL", Total); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 215 | void PMDebug::PrintArgumentInformation(const Pass *P) { |
| 216 | // Print out passes in pass manager... |
| 217 | if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) { |
| 218 | for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i) |
| 219 | PrintArgumentInformation(PM->getContainedPass(i)); |
| 220 | |
| 221 | } else { // Normal pass. Print argument information... |
| 222 | // Print out arguments for registered passes that are _optimizations_ |
| 223 | if (const PassInfo *PI = P->getPassInfo()) |
| 224 | if (PI->getPassType() & PassInfo::Optimization) |
| 225 | std::cerr << " -" << PI->getPassArgument(); |
| 226 | } |
| 227 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 228 | |
| 229 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 230 | Pass *P, Annotable *V) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 231 | if (PassDebugging >= Executions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 232 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 233 | << P->getPassName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 234 | if (V) { |
| 235 | std::cerr << "' on "; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 236 | |
| 237 | if (dynamic_cast<Module*>(V)) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 238 | std::cerr << "Module\n"; return; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 239 | } else if (Function *F = dynamic_cast<Function*>(V)) |
| 240 | std::cerr << "Function '" << F->getName(); |
| 241 | else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V)) |
| 242 | std::cerr << "BasicBlock '" << BB->getName(); |
| 243 | else if (Value *Val = dynamic_cast<Value*>(V)) |
| 244 | std::cerr << typeid(*Val).name() << " '" << Val->getName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 245 | } |
| 246 | std::cerr << "'...\n"; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 251 | Pass *P, const std::vector<AnalysisID> &Set){ |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 252 | if (PassDebugging >= Details && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 253 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 254 | for (unsigned i = 0; i != Set.size(); ++i) |
| 255 | std::cerr << " " << Set[i]->getPassName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 256 | std::cerr << "\n"; |
| 257 | } |
| 258 | } |
| 259 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 260 | //===----------------------------------------------------------------------===// |
| 261 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 262 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 263 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 264 | void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) { |
| 265 | PM->addPass(this, AU); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 266 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 268 | // dumpPassStructure - Implement the -debug-passes=Structure option |
| 269 | void Pass::dumpPassStructure(unsigned Offset) { |
| 270 | std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; |
| 271 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 272 | |
| 273 | // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass. |
| 274 | // |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 275 | const char *Pass::getPassName() const { |
| 276 | if (const PassInfo *PI = getPassInfo()) |
| 277 | return PI->getPassName(); |
| 278 | return typeid(*this).name(); |
| 279 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 281 | // print - Print out the internal state of the pass. This is called by Analyse |
| 282 | // to print out the contents of an analysis. Otherwise it is not neccesary to |
| 283 | // implement this method. |
| 284 | // |
| 285 | void Pass::print(std::ostream &O) const { |
| 286 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; |
| 287 | } |
| 288 | |
| 289 | // dump - call print(std::cerr); |
| 290 | void Pass::dump() const { |
| 291 | print(std::cerr, 0); |
| 292 | } |
| 293 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 294 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 295 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 296 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 297 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 298 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 299 | // for every function in the module, then by finalizing. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 300 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 301 | bool FunctionPass::run(Module &M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 302 | bool Changed = doInitialization(M); |
| 303 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 304 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 305 | if (!I->isExternal()) // Passes are not run on external functions! |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 306 | Changed |= runOnFunction(*I); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 307 | |
| 308 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 311 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 312 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 313 | bool FunctionPass::run(Function &F) { |
| 314 | if (F.isExternal()) return false;// Passes are not run on external functions! |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 316 | return doInitialization(*F.getParent()) | runOnFunction(F) |
| 317 | | doFinalization(*F.getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 318 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 319 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 320 | void FunctionPass::addToPassManager(PassManagerT<Module> *PM, |
| 321 | AnalysisUsage &AU) { |
| 322 | PM->addPass(this, AU); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 323 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 324 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 325 | void FunctionPass::addToPassManager(PassManagerT<Function> *PM, |
| 326 | AnalysisUsage &AU) { |
| 327 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | //===----------------------------------------------------------------------===// |
| 331 | // BasicBlockPass Implementation |
| 332 | // |
| 333 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 334 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 335 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 336 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 337 | bool BasicBlockPass::runOnFunction(Function &F) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 338 | bool Changed = false; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 339 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 340 | Changed |= runOnBasicBlock(*I); |
| 341 | return Changed; |
| 342 | } |
| 343 | |
| 344 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 345 | // finalize. |
| 346 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 347 | bool BasicBlockPass::run(BasicBlock &BB) { |
| 348 | Module &M = *BB.getParent()->getParent(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 349 | return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M); |
| 350 | } |
| 351 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 352 | void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 353 | AnalysisUsage &AU) { |
| 354 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 358 | AnalysisUsage &AU) { |
| 359 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 362 | |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // Pass Registration mechanism |
| 365 | // |
| 366 | static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0; |
| 367 | static std::vector<PassRegistrationListener*> *Listeners = 0; |
| 368 | |
| 369 | // getPassInfo - Return the PassInfo data structure that corresponds to this |
| 370 | // pass... |
| 371 | const PassInfo *Pass::getPassInfo() const { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 372 | if (PassInfoCache) return PassInfoCache; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 373 | return lookupPassInfo(typeid(*this)); |
| 374 | } |
| 375 | |
| 376 | const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 377 | if (PassInfoMap == 0) return 0; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 378 | std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI); |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 379 | return (I != PassInfoMap->end()) ? I->second : 0; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void RegisterPassBase::registerPass(PassInfo *PI) { |
| 383 | if (PassInfoMap == 0) |
| 384 | PassInfoMap = new std::map<TypeInfo, PassInfo*>(); |
| 385 | |
| 386 | assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && |
| 387 | "Pass already registered!"); |
| 388 | PIObj = PI; |
| 389 | PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); |
| 390 | |
| 391 | // Notify any listeners... |
| 392 | if (Listeners) |
| 393 | for (std::vector<PassRegistrationListener*>::iterator |
| 394 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 395 | (*I)->passRegistered(PI); |
| 396 | } |
| 397 | |
| 398 | RegisterPassBase::~RegisterPassBase() { |
| 399 | assert(PassInfoMap && "Pass registered but not in map!"); |
| 400 | std::map<TypeInfo, PassInfo*>::iterator I = |
| 401 | PassInfoMap->find(PIObj->getTypeInfo()); |
| 402 | assert(I != PassInfoMap->end() && "Pass registered but not in map!"); |
| 403 | |
| 404 | // Remove pass from the map... |
| 405 | PassInfoMap->erase(I); |
| 406 | if (PassInfoMap->empty()) { |
| 407 | delete PassInfoMap; |
| 408 | PassInfoMap = 0; |
| 409 | } |
| 410 | |
| 411 | // Notify any listeners... |
| 412 | if (Listeners) |
| 413 | for (std::vector<PassRegistrationListener*>::iterator |
| 414 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 415 | (*I)->passUnregistered(PIObj); |
| 416 | |
| 417 | // Delete the PassInfo object itself... |
| 418 | delete PIObj; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | |
| 423 | //===----------------------------------------------------------------------===// |
| 424 | // PassRegistrationListener implementation |
| 425 | // |
| 426 | |
| 427 | // PassRegistrationListener ctor - Add the current object to the list of |
| 428 | // PassRegistrationListeners... |
| 429 | PassRegistrationListener::PassRegistrationListener() { |
| 430 | if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); |
| 431 | Listeners->push_back(this); |
| 432 | } |
| 433 | |
| 434 | // dtor - Remove object from list of listeners... |
| 435 | PassRegistrationListener::~PassRegistrationListener() { |
| 436 | std::vector<PassRegistrationListener*>::iterator I = |
| 437 | std::find(Listeners->begin(), Listeners->end(), this); |
| 438 | assert(Listeners && I != Listeners->end() && |
| 439 | "PassRegistrationListener not registered!"); |
| 440 | Listeners->erase(I); |
| 441 | |
| 442 | if (Listeners->empty()) { |
| 443 | delete Listeners; |
| 444 | Listeners = 0; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // enumeratePasses - Iterate over the registered passes, calling the |
| 449 | // passEnumerate callback on each PassInfo object. |
| 450 | // |
| 451 | void PassRegistrationListener::enumeratePasses() { |
| 452 | if (PassInfoMap) |
| 453 | for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(), |
| 454 | E = PassInfoMap->end(); I != E; ++I) |
| 455 | passEnumerate(I->second); |
| 456 | } |