Andreas Neustifter | e7ddcfd | 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" |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 15 | #include "llvm/Instructions.h" |
| 16 | #include "llvm/Module.h" |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
| 18 | #include "llvm/Analysis/ProfileInfo.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CallSite.h" |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CFG.h" |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 22 | #include "llvm/Support/InstIterator.h" |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include <set> |
| 26 | using namespace llvm; |
| 27 | |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 28 | static cl::opt<bool,false> |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 29 | ProfileVerifierDisableAssertions("profile-verifier-noassert", |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 30 | cl::desc("Disable assertions")); |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 33 | class ProfileVerifierPass : public FunctionPass { |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 34 | |
| 35 | struct DetailedBlockInfo { |
| 36 | const BasicBlock *BB; |
| 37 | double BBWeight; |
| 38 | double inWeight; |
| 39 | int inCount; |
| 40 | double outWeight; |
| 41 | int outCount; |
| 42 | }; |
| 43 | |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 44 | ProfileInfo *PI; |
| 45 | std::set<const BasicBlock*> BBisVisited; |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 46 | std::set<const Function*> FisVisited; |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 47 | bool DisableAssertions; |
| 48 | |
| 49 | // When debugging is enabled, the verifier prints a whole slew of debug |
| 50 | // information, otherwise its just the assert. These are all the helper |
| 51 | // functions. |
| 52 | bool PrintedDebugTree; |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 53 | std::set<const BasicBlock*> BBisPrinted; |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 54 | void debugEntry(DetailedBlockInfo*); |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 55 | void printDebugInfo(const BasicBlock *BB); |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 56 | |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 57 | public: |
| 58 | static char ID; // Class identification, replacement for typeinfo |
| 59 | |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 60 | explicit ProfileVerifierPass () : FunctionPass(&ID) { |
| 61 | DisableAssertions = ProfileVerifierDisableAssertions; |
| 62 | } |
| 63 | explicit ProfileVerifierPass (bool da) : FunctionPass(&ID), |
| 64 | DisableAssertions(da) { |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 68 | AU.setPreservesAll(); |
| 69 | AU.addRequired<ProfileInfo>(); |
| 70 | } |
| 71 | |
| 72 | const char *getPassName() const { |
| 73 | return "Profiling information verifier"; |
| 74 | } |
| 75 | |
| 76 | /// run - Verify the profile information. |
| 77 | bool runOnFunction(Function &F); |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 78 | void recurseBasicBlock(const BasicBlock*); |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 79 | |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 80 | bool exitReachable(const Function*); |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 81 | double ReadOrAssert(ProfileInfo::Edge); |
| 82 | void CheckValue(bool, const char*, DetailedBlockInfo*); |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 83 | }; |
| 84 | } // End of anonymous namespace |
| 85 | |
| 86 | char ProfileVerifierPass::ID = 0; |
| 87 | static RegisterPass<ProfileVerifierPass> |
| 88 | X("profile-verifier", "Verify profiling information", false, true); |
| 89 | |
| 90 | namespace llvm { |
| 91 | FunctionPass *createProfileVerifierPass() { |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 92 | return new ProfileVerifierPass(ProfileVerifierDisableAssertions); |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 96 | void ProfileVerifierPass::printDebugInfo(const BasicBlock *BB) { |
| 97 | |
| 98 | if (BBisPrinted.find(BB) != BBisPrinted.end()) return; |
| 99 | |
| 100 | double BBWeight = PI->getExecutionCount(BB); |
| 101 | if (BBWeight == ProfileInfo::MissingValue) { BBWeight = 0; } |
| 102 | double inWeight = 0; |
| 103 | int inCount = 0; |
| 104 | std::set<const BasicBlock*> ProcessedPreds; |
| 105 | for ( pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB); |
| 106 | bbi != bbe; ++bbi ) { |
| 107 | if (ProcessedPreds.insert(*bbi).second) { |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 108 | ProfileInfo::Edge E = PI->getEdge(*bbi,BB); |
| 109 | double EdgeWeight = PI->getEdgeWeight(E); |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 110 | if (EdgeWeight == ProfileInfo::MissingValue) { EdgeWeight = 0; } |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 111 | errs() << "calculated in-edge " << E << ": " << EdgeWeight << "\n"; |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 112 | inWeight += EdgeWeight; |
| 113 | inCount++; |
| 114 | } |
| 115 | } |
| 116 | double outWeight = 0; |
| 117 | int outCount = 0; |
| 118 | std::set<const BasicBlock*> ProcessedSuccs; |
| 119 | for ( succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 120 | bbi != bbe; ++bbi ) { |
| 121 | if (ProcessedSuccs.insert(*bbi).second) { |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 122 | ProfileInfo::Edge E = PI->getEdge(BB,*bbi); |
| 123 | double EdgeWeight = PI->getEdgeWeight(E); |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 124 | if (EdgeWeight == ProfileInfo::MissingValue) { EdgeWeight = 0; } |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 125 | errs() << "calculated out-edge " << E << ": " << EdgeWeight << "\n"; |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 126 | outWeight += EdgeWeight; |
| 127 | outCount++; |
| 128 | } |
| 129 | } |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 130 | errs()<<"Block "<<BB->getNameStr()<<" in "<<BB->getParent()->getNameStr() |
| 131 | <<",BBWeight="<<BBWeight<<",inWeight="<<inWeight<<",inCount="<<inCount |
| 132 | <<",outWeight="<<outWeight<<",outCount"<<outCount<<"\n"; |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 133 | |
| 134 | // mark as visited and recurse into subnodes |
| 135 | BBisPrinted.insert(BB); |
| 136 | for ( succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 137 | bbi != bbe; ++bbi ) { |
| 138 | printDebugInfo(*bbi); |
| 139 | } |
| 140 | } |
| 141 | |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 142 | void ProfileVerifierPass::debugEntry (DetailedBlockInfo *DI) { |
| 143 | errs() << "TROUBLE: Block " << DI->BB->getNameStr() << " in " |
| 144 | << DI->BB->getParent()->getNameStr() << ":"; |
| 145 | errs() << "BBWeight=" << DI->BBWeight << ","; |
| 146 | errs() << "inWeight=" << DI->inWeight << ","; |
| 147 | errs() << "inCount=" << DI->inCount << ","; |
| 148 | errs() << "outWeight=" << DI->outWeight << ","; |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 149 | errs() << "outCount=" << DI->outCount << "\n"; |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 150 | if (!PrintedDebugTree) { |
| 151 | PrintedDebugTree = true; |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 152 | printDebugInfo(&(DI->BB->getParent()->getEntryBlock())); |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 155 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 156 | // This compares A and B but considering maybe small differences. |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 157 | static bool Equals(double A, double B) { |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 158 | double maxRelativeError = 0.0000001; |
| 159 | if (A == B) |
| 160 | return true; |
| 161 | double relativeError; |
| 162 | if (fabs(B) > fabs(A)) |
| 163 | relativeError = fabs((A - B) / B); |
| 164 | else |
| 165 | relativeError = fabs((A - B) / A); |
| 166 | if (relativeError <= maxRelativeError) return true; |
| 167 | return false; |
| 168 | } |
| 169 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 170 | // This checks if the function "exit" is reachable from an given function |
| 171 | // via calls, this is necessary to check if a profile is valid despite the |
| 172 | // counts not fitting exactly. |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 173 | bool ProfileVerifierPass::exitReachable(const Function *F) { |
| 174 | if (!F) return false; |
| 175 | |
| 176 | if (FisVisited.count(F)) return false; |
| 177 | |
| 178 | Function *Exit = F->getParent()->getFunction("exit"); |
| 179 | if (Exit == F) { |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | FisVisited.insert(F); |
| 184 | bool exits = false; |
| 185 | for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 186 | if (const CallInst *CI = dyn_cast<CallInst>(&*I)) { |
| 187 | exits |= exitReachable(CI->getCalledFunction()); |
| 188 | if (exits) break; |
| 189 | } |
| 190 | } |
| 191 | return exits; |
| 192 | } |
| 193 | |
| 194 | #define ASSERTMESSAGE(M) \ |
| 195 | errs() << (M) << "\n"; \ |
| 196 | if (!DisableAssertions) assert(0 && (M)); |
| 197 | |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 198 | double ProfileVerifierPass::ReadOrAssert(ProfileInfo::Edge E) { |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 199 | double EdgeWeight = PI->getEdgeWeight(E); |
| 200 | if (EdgeWeight == ProfileInfo::MissingValue) { |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 201 | errs() << "Edge " << E << " in Function " |
Andreas Neustifter | b1b4c01 | 2009-09-11 08:43:15 +0000 | [diff] [blame] | 202 | << ProfileInfo::getFunction(E)->getNameStr() << ": "; |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 203 | ASSERTMESSAGE("ASSERT:Edge has missing value"); |
| 204 | return 0; |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 205 | } else { |
| 206 | return EdgeWeight; |
| 207 | } |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 210 | void ProfileVerifierPass::CheckValue(bool Error, const char *Message, |
| 211 | DetailedBlockInfo *DI) { |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 212 | if (Error) { |
| 213 | DEBUG(debugEntry(DI)); |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 214 | errs() << "Block " << DI->BB->getNameStr() << " in Function " |
| 215 | << DI->BB->getParent()->getNameStr() << ": "; |
| 216 | ASSERTMESSAGE(Message); |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 217 | } |
| 218 | return; |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 221 | // This calculates the Information for a block and then recurses into the |
| 222 | // successors. |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 223 | void ProfileVerifierPass::recurseBasicBlock(const BasicBlock *BB) { |
| 224 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 225 | // Break the recursion by remembering all visited blocks. |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 226 | if (BBisVisited.find(BB) != BBisVisited.end()) return; |
| 227 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 228 | // Use a data structure to store all the information, this can then be handed |
| 229 | // to debug printers. |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 230 | DetailedBlockInfo DI; |
| 231 | DI.BB = BB; |
Edward O'Callaghan | 2afd072 | 2009-11-02 02:55:39 +0000 | [diff] [blame] | 232 | DI.outCount = DI.inCount = 0; |
| 233 | DI.inWeight = DI.outWeight = 0.0; |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 234 | |
| 235 | // Read predecessors. |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 236 | std::set<const BasicBlock*> ProcessedPreds; |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 237 | pred_const_iterator bpi = pred_begin(BB), bpe = pred_end(BB); |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 238 | // If there are none, check for (0,BB) edge. |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 239 | if (bpi == bpe) { |
| 240 | DI.inWeight += ReadOrAssert(PI->getEdge(0,BB)); |
| 241 | DI.inCount++; |
| 242 | } |
| 243 | for (;bpi != bpe; ++bpi) { |
| 244 | if (ProcessedPreds.insert(*bpi).second) { |
| 245 | DI.inWeight += ReadOrAssert(PI->getEdge(*bpi,BB)); |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 246 | DI.inCount++; |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 250 | // Read successors. |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 251 | std::set<const BasicBlock*> ProcessedSuccs; |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 252 | succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 253 | // If there is an (0,BB) edge, consider it too. (This is done not only when |
| 254 | // there are no successors, but every time; not every function contains |
| 255 | // return blocks with no successors (think loop latch as return block)). |
| 256 | double w = PI->getEdgeWeight(PI->getEdge(BB,0)); |
| 257 | if (w != ProfileInfo::MissingValue) { |
| 258 | DI.outWeight += w; |
| 259 | DI.outCount++; |
Andreas Neustifter | 0c0de66 | 2009-09-10 16:30:38 +0000 | [diff] [blame] | 260 | } |
| 261 | for (;bbi != bbe; ++bbi) { |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 262 | if (ProcessedSuccs.insert(*bbi).second) { |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 263 | DI.outWeight += ReadOrAssert(PI->getEdge(BB,*bbi)); |
| 264 | DI.outCount++; |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 268 | // Read block weight. |
Andreas Neustifter | e8d372e | 2009-09-04 17:15:10 +0000 | [diff] [blame] | 269 | DI.BBWeight = PI->getExecutionCount(BB); |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 270 | CheckValue(DI.BBWeight == ProfileInfo::MissingValue, |
| 271 | "ASSERT:BasicBlock has missing value", &DI); |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 272 | |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 273 | // Check if this block is a setjmp target. |
| 274 | bool isSetJmpTarget = false; |
| 275 | if (DI.outWeight > DI.inWeight) { |
| 276 | for (BasicBlock::const_iterator i = BB->begin(), ie = BB->end(); |
| 277 | i != ie; ++i) { |
| 278 | if (const CallInst *CI = dyn_cast<CallInst>(&*i)) { |
| 279 | Function *F = CI->getCalledFunction(); |
| 280 | if (F && (F->getNameStr() == "_setjmp")) { |
| 281 | isSetJmpTarget = true; break; |
| 282 | } |
| 283 | } |
| 284 | } |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 285 | } |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 286 | // Check if this block is eventually reaching exit. |
| 287 | bool isExitReachable = false; |
| 288 | if (DI.inWeight > DI.outWeight) { |
| 289 | for (BasicBlock::const_iterator i = BB->begin(), ie = BB->end(); |
| 290 | i != ie; ++i) { |
| 291 | if (const CallInst *CI = dyn_cast<CallInst>(&*i)) { |
| 292 | FisVisited.clear(); |
| 293 | isExitReachable |= exitReachable(CI->getCalledFunction()); |
| 294 | if (isExitReachable) break; |
| 295 | } |
| 296 | } |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Andreas Neustifter | 43b1b0e | 2009-09-09 13:01:03 +0000 | [diff] [blame] | 299 | if (DI.inCount > 0 && DI.outCount == 0) { |
| 300 | // If this is a block with no successors. |
| 301 | if (!isSetJmpTarget) { |
| 302 | CheckValue(!Equals(DI.inWeight,DI.BBWeight), |
| 303 | "ASSERT:inWeight and BBWeight do not match", &DI); |
| 304 | } |
| 305 | } else if (DI.inCount == 0 && DI.outCount > 0) { |
| 306 | // If this is a block with no predecessors. |
| 307 | if (!isExitReachable) |
| 308 | CheckValue(!Equals(DI.BBWeight,DI.outWeight), |
| 309 | "ASSERT:BBWeight and outWeight do not match", &DI); |
| 310 | } else { |
| 311 | // If this block has successors and predecessors. |
| 312 | if (DI.inWeight > DI.outWeight && !isExitReachable) |
| 313 | CheckValue(!Equals(DI.inWeight,DI.outWeight), |
| 314 | "ASSERT:inWeight and outWeight do not match", &DI); |
| 315 | if (DI.inWeight < DI.outWeight && !isSetJmpTarget) |
| 316 | CheckValue(!Equals(DI.inWeight,DI.outWeight), |
| 317 | "ASSERT:inWeight and outWeight do not match", &DI); |
| 318 | } |
| 319 | |
| 320 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 321 | // Mark this block as visited, rescurse into successors. |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 322 | BBisVisited.insert(BB); |
| 323 | for ( succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB); |
| 324 | bbi != bbe; ++bbi ) { |
| 325 | recurseBasicBlock(*bbi); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | bool ProfileVerifierPass::runOnFunction(Function &F) { |
| 330 | PI = &getAnalysis<ProfileInfo>(); |
| 331 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 332 | // Prepare global variables. |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 333 | PrintedDebugTree = false; |
| 334 | BBisVisited.clear(); |
| 335 | |
Andreas Neustifter | 8a58c18 | 2009-09-11 08:39:33 +0000 | [diff] [blame] | 336 | // Fetch entry block and recurse into it. |
Andreas Neustifter | e7ddcfd | 2009-09-01 08:48:42 +0000 | [diff] [blame] | 337 | const BasicBlock *entry = &F.getEntryBlock(); |
| 338 | recurseBasicBlock(entry); |
| 339 | |
| 340 | if (!DisableAssertions) |
| 341 | assert((PI->getExecutionCount(&F)==PI->getExecutionCount(entry)) && |
| 342 | "Function count and entry block count do not match"); |
| 343 | return false; |
| 344 | } |