Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 1 | //===- ProfileVerifierPass.cpp - LLVM Pass to estimate profile info -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a pass that checks profiling information for |
| 11 | // plausibility. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #define DEBUG_TYPE "profile-verifier" |
| 15 | #include "llvm/Pass.h" |
| 16 | #include "llvm/Analysis/ProfileInfo.h" |
| 17 | #include "llvm/Support/CommandLine.h" |
| 18 | #include "llvm/Support/CFG.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include <set> |
| 22 | using namespace llvm; |
| 23 | |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 24 | static cl::opt<bool,true> |
| 25 | ProfileVerifierDisableAssertions("profile-verifier-noassert", |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 26 | cl::desc("Disable assertions")); |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | class VISIBILITY_HIDDEN ProfileVerifierPass : public FunctionPass { |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 30 | |
| 31 | struct DetailedBlockInfo { |
| 32 | const BasicBlock *BB; |
| 33 | double BBWeight; |
| 34 | double inWeight; |
| 35 | int inCount; |
| 36 | double outWeight; |
| 37 | int outCount; |
| 38 | }; |
| 39 | |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 40 | ProfileInfo *PI; |
| 41 | std::set<const BasicBlock*> BBisVisited; |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 42 | bool DisableAssertions; |
| 43 | |
| 44 | // When debugging is enabled, the verifier prints a whole slew of debug |
| 45 | // information, otherwise its just the assert. These are all the helper |
| 46 | // functions. |
| 47 | bool PrintedDebugTree; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 48 | std::set<const BasicBlock*> BBisPrinted; |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 49 | void debugEntry(DetailedBlockInfo*); |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 50 | void printDebugInfo(const BasicBlock *BB); |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 51 | |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 52 | public: |
| 53 | static char ID; // Class identification, replacement for typeinfo |
| 54 | |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 55 | explicit ProfileVerifierPass (bool da = false) : FunctionPass(&ID), |
| 56 | DisableAssertions(da) { |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 60 | AU.setPreservesAll(); |
| 61 | AU.addRequired<ProfileInfo>(); |
| 62 | } |
| 63 | |
| 64 | const char *getPassName() const { |
| 65 | return "Profiling information verifier"; |
| 66 | } |
| 67 | |
| 68 | /// run - Verify the profile information. |
| 69 | bool runOnFunction(Function &F); |
| 70 | void recurseBasicBlock(const BasicBlock *BB); |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 71 | |
| 72 | double ReadOrAssert(ProfileInfo::Edge); |
| 73 | void CheckValue(bool, const char*, DetailedBlockInfo*); |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 74 | }; |
| 75 | } // End of anonymous namespace |
| 76 | |
| 77 | char ProfileVerifierPass::ID = 0; |
| 78 | static RegisterPass<ProfileVerifierPass> |
| 79 | X("profile-verifier", "Verify profiling information", false, true); |
| 80 | |
| 81 | namespace llvm { |
| 82 | FunctionPass *createProfileVerifierPass() { |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 83 | return new ProfileVerifierPass(ProfileVerifierDisableAssertions); |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 87 | void ProfileVerifierPass::printDebugInfo(const BasicBlock *BB) { |
| 88 | |
| 89 | if (BBisPrinted.find(BB) != BBisPrinted.end()) return; |
| 90 | |
| 91 | double BBWeight = PI->getExecutionCount(BB); |
| 92 | if (BBWeight == ProfileInfo::MissingValue) { BBWeight = 0; } |
| 93 | double inWeight = 0; |
| 94 | int inCount = 0; |
| 95 | std::set<const BasicBlock*> ProcessedPreds; |
| 96 | for ( pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB); |
| 97 | bbi != bbe; ++bbi ) { |
| 98 | if (ProcessedPreds.insert(*bbi).second) { |
| 99 | double EdgeWeight = PI->getEdgeWeight(PI->getEdge(*bbi,BB)); |
| 100 | if (EdgeWeight == ProfileInfo::MissingValue) { EdgeWeight = 0; } |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 101 | errs()<<"calculated in-edge ("<<(*bbi)->getNameStr()<<","<<BB->getNameStr() |
| 102 | <<"): "<<EdgeWeight<<"\n"; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 103 | inWeight += EdgeWeight; |
| 104 | inCount++; |
| 105 | } |
| 106 | } |
| 107 | double outWeight = 0; |
| 108 | int outCount = 0; |
| 109 | std::set<const BasicBlock*> ProcessedSuccs; |
| 110 | for ( succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 111 | bbi != bbe; ++bbi ) { |
| 112 | if (ProcessedSuccs.insert(*bbi).second) { |
| 113 | double EdgeWeight = PI->getEdgeWeight(PI->getEdge(BB,*bbi)); |
| 114 | if (EdgeWeight == ProfileInfo::MissingValue) { EdgeWeight = 0; } |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 115 | errs()<<"calculated out-edge ("<<BB->getNameStr()<<","<<(*bbi)->getNameStr() |
| 116 | <<"): "<<EdgeWeight<<"\n"; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 117 | outWeight += EdgeWeight; |
| 118 | outCount++; |
| 119 | } |
| 120 | } |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 121 | errs()<<"Block "<<BB->getNameStr()<<" in "<<BB->getParent()->getNameStr() |
| 122 | <<",BBWeight="<<BBWeight<<",inWeight="<<inWeight<<",inCount="<<inCount |
| 123 | <<",outWeight="<<outWeight<<",outCount"<<outCount<<"\n"; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 124 | |
| 125 | // mark as visited and recurse into subnodes |
| 126 | BBisPrinted.insert(BB); |
| 127 | for ( succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 128 | bbi != bbe; ++bbi ) { |
| 129 | printDebugInfo(*bbi); |
| 130 | } |
| 131 | } |
| 132 | |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 133 | void ProfileVerifierPass::debugEntry (DetailedBlockInfo *DI) { |
| 134 | errs() << "TROUBLE: Block " << DI->BB->getNameStr() << " in " |
| 135 | << DI->BB->getParent()->getNameStr() << ":"; |
| 136 | errs() << "BBWeight=" << DI->BBWeight << ","; |
| 137 | errs() << "inWeight=" << DI->inWeight << ","; |
| 138 | errs() << "inCount=" << DI->inCount << ","; |
| 139 | errs() << "outWeight=" << DI->outWeight << ","; |
| 140 | errs() << "outCount=" << DI->outCount << ","; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 141 | if (!PrintedDebugTree) { |
| 142 | PrintedDebugTree = true; |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 143 | printDebugInfo(&(DI->BB->getParent()->getEntryBlock())); |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 146 | |
| 147 | // compare with relative error |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 148 | static bool Equals(double A, double B) { |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 149 | double maxRelativeError = 0.0000001; |
| 150 | if (A == B) |
| 151 | return true; |
| 152 | double relativeError; |
| 153 | if (fabs(B) > fabs(A)) |
| 154 | relativeError = fabs((A - B) / B); |
| 155 | else |
| 156 | relativeError = fabs((A - B) / A); |
| 157 | if (relativeError <= maxRelativeError) return true; |
| 158 | return false; |
| 159 | } |
| 160 | |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 161 | double ProfileVerifierPass::ReadOrAssert(ProfileInfo::Edge E) { |
| 162 | const char *Message = "ASSERT:Edge has missing value"; |
| 163 | double EdgeWeight = PI->getEdgeWeight(E); |
| 164 | if (EdgeWeight == ProfileInfo::MissingValue) { |
| 165 | if (DisableAssertions) { |
| 166 | errs() << Message << "\n"; |
| 167 | return 0; |
| 168 | } else { |
| 169 | assert(0 && Message); |
| 170 | } |
| 171 | } else { |
| 172 | return EdgeWeight; |
| 173 | } |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 176 | void ProfileVerifierPass::CheckValue(bool Error, const char *Message, DetailedBlockInfo *DI) { |
| 177 | if (Error) { |
| 178 | DEBUG(debugEntry(DI)); |
| 179 | if (DisableAssertions) { |
| 180 | errs() << Message << "\n"; |
| 181 | } else { |
| 182 | assert(0 && Message); |
| 183 | } |
| 184 | } |
| 185 | return; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void ProfileVerifierPass::recurseBasicBlock(const BasicBlock *BB) { |
| 189 | |
| 190 | if (BBisVisited.find(BB) != BBisVisited.end()) return; |
| 191 | |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 192 | DetailedBlockInfo DI; |
| 193 | DI.BB = BB; |
| 194 | DI.outCount = DI.inCount = DI.inWeight = DI.outWeight = 0; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 195 | std::set<const BasicBlock*> ProcessedPreds; |
| 196 | for ( pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB); |
| 197 | bbi != bbe; ++bbi ) { |
| 198 | if (ProcessedPreds.insert(*bbi).second) { |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 199 | DI.inWeight += ReadOrAssert(PI->getEdge(*bbi,BB)); |
| 200 | DI.inCount++; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 204 | std::set<const BasicBlock*> ProcessedSuccs; |
| 205 | for ( succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 206 | bbi != bbe; ++bbi ) { |
| 207 | if (ProcessedSuccs.insert(*bbi).second) { |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 208 | DI.outWeight += ReadOrAssert(PI->getEdge(BB,*bbi)); |
| 209 | DI.outCount++; |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 213 | DI.BBWeight = PI->getExecutionCount(BB); |
| 214 | CheckValue(DI.BBWeight == ProfileInfo::MissingValue, "ASSERT:BasicBlock has missing value", &DI); |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 215 | |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 216 | if (DI.inCount > 0) { |
| 217 | CheckValue(!Equals(DI.inWeight,DI.BBWeight), "ASSERT:inWeight and BBWeight do not match", &DI); |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 218 | } |
Andreas Neustifter | c2ae05a | 2009-09-04 17:15:10 +0000 | [diff] [blame^] | 219 | if (DI.outCount > 0) { |
| 220 | CheckValue(!Equals(DI.outWeight,DI.BBWeight), "ASSERT:outWeight and BBWeight do not match", &DI); |
Andreas Neustifter | 76448f7 | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // mark as visited and recurse into subnodes |
| 224 | BBisVisited.insert(BB); |
| 225 | for ( succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 226 | bbi != bbe; ++bbi ) { |
| 227 | recurseBasicBlock(*bbi); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | bool ProfileVerifierPass::runOnFunction(Function &F) { |
| 232 | PI = &getAnalysis<ProfileInfo>(); |
| 233 | |
| 234 | if (PI->getExecutionCount(&F) == ProfileInfo::MissingValue) { |
| 235 | DEBUG(errs()<<"Function "<<F.getNameStr()<<" has no profile\n"); |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | PrintedDebugTree = false; |
| 240 | BBisVisited.clear(); |
| 241 | |
| 242 | const BasicBlock *entry = &F.getEntryBlock(); |
| 243 | recurseBasicBlock(entry); |
| 244 | |
| 245 | if (!DisableAssertions) |
| 246 | assert((PI->getExecutionCount(&F)==PI->getExecutionCount(entry)) && |
| 247 | "Function count and entry block count do not match"); |
| 248 | return false; |
| 249 | } |