LLVM API Documentation
00001 //===-- BranchProbabilityInfo.cpp - Branch Probability Analysis -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Loops should be simplified before this analysis. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Analysis/BranchProbabilityInfo.h" 00015 #include "llvm/ADT/PostOrderIterator.h" 00016 #include "llvm/Analysis/LoopInfo.h" 00017 #include "llvm/IR/CFG.h" 00018 #include "llvm/IR/Constants.h" 00019 #include "llvm/IR/Function.h" 00020 #include "llvm/IR/Instructions.h" 00021 #include "llvm/IR/LLVMContext.h" 00022 #include "llvm/IR/Metadata.h" 00023 #include "llvm/Support/Debug.h" 00024 00025 using namespace llvm; 00026 00027 #define DEBUG_TYPE "branch-prob" 00028 00029 INITIALIZE_PASS_BEGIN(BranchProbabilityInfo, "branch-prob", 00030 "Branch Probability Analysis", false, true) 00031 INITIALIZE_PASS_DEPENDENCY(LoopInfo) 00032 INITIALIZE_PASS_END(BranchProbabilityInfo, "branch-prob", 00033 "Branch Probability Analysis", false, true) 00034 00035 char BranchProbabilityInfo::ID = 0; 00036 00037 // Weights are for internal use only. They are used by heuristics to help to 00038 // estimate edges' probability. Example: 00039 // 00040 // Using "Loop Branch Heuristics" we predict weights of edges for the 00041 // block BB2. 00042 // ... 00043 // | 00044 // V 00045 // BB1<-+ 00046 // | | 00047 // | | (Weight = 124) 00048 // V | 00049 // BB2--+ 00050 // | 00051 // | (Weight = 4) 00052 // V 00053 // BB3 00054 // 00055 // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875 00056 // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125 00057 static const uint32_t LBH_TAKEN_WEIGHT = 124; 00058 static const uint32_t LBH_NONTAKEN_WEIGHT = 4; 00059 00060 /// \brief Unreachable-terminating branch taken weight. 00061 /// 00062 /// This is the weight for a branch being taken to a block that terminates 00063 /// (eventually) in unreachable. These are predicted as unlikely as possible. 00064 static const uint32_t UR_TAKEN_WEIGHT = 1; 00065 00066 /// \brief Unreachable-terminating branch not-taken weight. 00067 /// 00068 /// This is the weight for a branch not being taken toward a block that 00069 /// terminates (eventually) in unreachable. Such a branch is essentially never 00070 /// taken. Set the weight to an absurdly high value so that nested loops don't 00071 /// easily subsume it. 00072 static const uint32_t UR_NONTAKEN_WEIGHT = 1024*1024 - 1; 00073 00074 /// \brief Weight for a branch taken going into a cold block. 00075 /// 00076 /// This is the weight for a branch taken toward a block marked 00077 /// cold. A block is marked cold if it's postdominated by a 00078 /// block containing a call to a cold function. Cold functions 00079 /// are those marked with attribute 'cold'. 00080 static const uint32_t CC_TAKEN_WEIGHT = 4; 00081 00082 /// \brief Weight for a branch not-taken into a cold block. 00083 /// 00084 /// This is the weight for a branch not taken toward a block marked 00085 /// cold. 00086 static const uint32_t CC_NONTAKEN_WEIGHT = 64; 00087 00088 static const uint32_t PH_TAKEN_WEIGHT = 20; 00089 static const uint32_t PH_NONTAKEN_WEIGHT = 12; 00090 00091 static const uint32_t ZH_TAKEN_WEIGHT = 20; 00092 static const uint32_t ZH_NONTAKEN_WEIGHT = 12; 00093 00094 static const uint32_t FPH_TAKEN_WEIGHT = 20; 00095 static const uint32_t FPH_NONTAKEN_WEIGHT = 12; 00096 00097 /// \brief Invoke-terminating normal branch taken weight 00098 /// 00099 /// This is the weight for branching to the normal destination of an invoke 00100 /// instruction. We expect this to happen most of the time. Set the weight to an 00101 /// absurdly high value so that nested loops subsume it. 00102 static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1; 00103 00104 /// \brief Invoke-terminating normal branch not-taken weight. 00105 /// 00106 /// This is the weight for branching to the unwind destination of an invoke 00107 /// instruction. This is essentially never taken. 00108 static const uint32_t IH_NONTAKEN_WEIGHT = 1; 00109 00110 // Standard weight value. Used when none of the heuristics set weight for 00111 // the edge. 00112 static const uint32_t NORMAL_WEIGHT = 16; 00113 00114 // Minimum weight of an edge. Please note, that weight is NEVER 0. 00115 static const uint32_t MIN_WEIGHT = 1; 00116 00117 static uint32_t getMaxWeightFor(BasicBlock *BB) { 00118 return UINT32_MAX / BB->getTerminator()->getNumSuccessors(); 00119 } 00120 00121 00122 /// \brief Calculate edge weights for successors lead to unreachable. 00123 /// 00124 /// Predict that a successor which leads necessarily to an 00125 /// unreachable-terminated block as extremely unlikely. 00126 bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) { 00127 TerminatorInst *TI = BB->getTerminator(); 00128 if (TI->getNumSuccessors() == 0) { 00129 if (isa<UnreachableInst>(TI)) 00130 PostDominatedByUnreachable.insert(BB); 00131 return false; 00132 } 00133 00134 SmallVector<unsigned, 4> UnreachableEdges; 00135 SmallVector<unsigned, 4> ReachableEdges; 00136 00137 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 00138 if (PostDominatedByUnreachable.count(*I)) 00139 UnreachableEdges.push_back(I.getSuccessorIndex()); 00140 else 00141 ReachableEdges.push_back(I.getSuccessorIndex()); 00142 } 00143 00144 // If all successors are in the set of blocks post-dominated by unreachable, 00145 // this block is too. 00146 if (UnreachableEdges.size() == TI->getNumSuccessors()) 00147 PostDominatedByUnreachable.insert(BB); 00148 00149 // Skip probabilities if this block has a single successor or if all were 00150 // reachable. 00151 if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty()) 00152 return false; 00153 00154 uint32_t UnreachableWeight = 00155 std::max(UR_TAKEN_WEIGHT / (unsigned)UnreachableEdges.size(), MIN_WEIGHT); 00156 for (SmallVectorImpl<unsigned>::iterator I = UnreachableEdges.begin(), 00157 E = UnreachableEdges.end(); 00158 I != E; ++I) 00159 setEdgeWeight(BB, *I, UnreachableWeight); 00160 00161 if (ReachableEdges.empty()) 00162 return true; 00163 uint32_t ReachableWeight = 00164 std::max(UR_NONTAKEN_WEIGHT / (unsigned)ReachableEdges.size(), 00165 NORMAL_WEIGHT); 00166 for (SmallVectorImpl<unsigned>::iterator I = ReachableEdges.begin(), 00167 E = ReachableEdges.end(); 00168 I != E; ++I) 00169 setEdgeWeight(BB, *I, ReachableWeight); 00170 00171 return true; 00172 } 00173 00174 // Propagate existing explicit probabilities from either profile data or 00175 // 'expect' intrinsic processing. 00176 bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) { 00177 TerminatorInst *TI = BB->getTerminator(); 00178 if (TI->getNumSuccessors() == 1) 00179 return false; 00180 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI)) 00181 return false; 00182 00183 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); 00184 if (!WeightsNode) 00185 return false; 00186 00187 // Ensure there are weights for all of the successors. Note that the first 00188 // operand to the metadata node is a name, not a weight. 00189 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1) 00190 return false; 00191 00192 // Build up the final weights that will be used in a temporary buffer, but 00193 // don't add them until all weihts are present. Each weight value is clamped 00194 // to [1, getMaxWeightFor(BB)]. 00195 uint32_t WeightLimit = getMaxWeightFor(BB); 00196 SmallVector<uint32_t, 2> Weights; 00197 Weights.reserve(TI->getNumSuccessors()); 00198 for (unsigned i = 1, e = WeightsNode->getNumOperands(); i != e; ++i) { 00199 ConstantInt *Weight = dyn_cast<ConstantInt>(WeightsNode->getOperand(i)); 00200 if (!Weight) 00201 return false; 00202 Weights.push_back( 00203 std::max<uint32_t>(1, Weight->getLimitedValue(WeightLimit))); 00204 } 00205 assert(Weights.size() == TI->getNumSuccessors() && "Checked above"); 00206 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 00207 setEdgeWeight(BB, i, Weights[i]); 00208 00209 return true; 00210 } 00211 00212 /// \brief Calculate edge weights for edges leading to cold blocks. 00213 /// 00214 /// A cold block is one post-dominated by a block with a call to a 00215 /// cold function. Those edges are unlikely to be taken, so we give 00216 /// them relatively low weight. 00217 /// 00218 /// Return true if we could compute the weights for cold edges. 00219 /// Return false, otherwise. 00220 bool BranchProbabilityInfo::calcColdCallHeuristics(BasicBlock *BB) { 00221 TerminatorInst *TI = BB->getTerminator(); 00222 if (TI->getNumSuccessors() == 0) 00223 return false; 00224 00225 // Determine which successors are post-dominated by a cold block. 00226 SmallVector<unsigned, 4> ColdEdges; 00227 SmallVector<unsigned, 4> NormalEdges; 00228 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) 00229 if (PostDominatedByColdCall.count(*I)) 00230 ColdEdges.push_back(I.getSuccessorIndex()); 00231 else 00232 NormalEdges.push_back(I.getSuccessorIndex()); 00233 00234 // If all successors are in the set of blocks post-dominated by cold calls, 00235 // this block is in the set post-dominated by cold calls. 00236 if (ColdEdges.size() == TI->getNumSuccessors()) 00237 PostDominatedByColdCall.insert(BB); 00238 else { 00239 // Otherwise, if the block itself contains a cold function, add it to the 00240 // set of blocks postdominated by a cold call. 00241 assert(!PostDominatedByColdCall.count(BB)); 00242 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 00243 if (CallInst *CI = dyn_cast<CallInst>(I)) 00244 if (CI->hasFnAttr(Attribute::Cold)) { 00245 PostDominatedByColdCall.insert(BB); 00246 break; 00247 } 00248 } 00249 00250 // Skip probabilities if this block has a single successor. 00251 if (TI->getNumSuccessors() == 1 || ColdEdges.empty()) 00252 return false; 00253 00254 uint32_t ColdWeight = 00255 std::max(CC_TAKEN_WEIGHT / (unsigned) ColdEdges.size(), MIN_WEIGHT); 00256 for (SmallVectorImpl<unsigned>::iterator I = ColdEdges.begin(), 00257 E = ColdEdges.end(); 00258 I != E; ++I) 00259 setEdgeWeight(BB, *I, ColdWeight); 00260 00261 if (NormalEdges.empty()) 00262 return true; 00263 uint32_t NormalWeight = std::max( 00264 CC_NONTAKEN_WEIGHT / (unsigned) NormalEdges.size(), NORMAL_WEIGHT); 00265 for (SmallVectorImpl<unsigned>::iterator I = NormalEdges.begin(), 00266 E = NormalEdges.end(); 00267 I != E; ++I) 00268 setEdgeWeight(BB, *I, NormalWeight); 00269 00270 return true; 00271 } 00272 00273 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparsion 00274 // between two pointer or pointer and NULL will fail. 00275 bool BranchProbabilityInfo::calcPointerHeuristics(BasicBlock *BB) { 00276 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); 00277 if (!BI || !BI->isConditional()) 00278 return false; 00279 00280 Value *Cond = BI->getCondition(); 00281 ICmpInst *CI = dyn_cast<ICmpInst>(Cond); 00282 if (!CI || !CI->isEquality()) 00283 return false; 00284 00285 Value *LHS = CI->getOperand(0); 00286 00287 if (!LHS->getType()->isPointerTy()) 00288 return false; 00289 00290 assert(CI->getOperand(1)->getType()->isPointerTy()); 00291 00292 // p != 0 -> isProb = true 00293 // p == 0 -> isProb = false 00294 // p != q -> isProb = true 00295 // p == q -> isProb = false; 00296 unsigned TakenIdx = 0, NonTakenIdx = 1; 00297 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE; 00298 if (!isProb) 00299 std::swap(TakenIdx, NonTakenIdx); 00300 00301 setEdgeWeight(BB, TakenIdx, PH_TAKEN_WEIGHT); 00302 setEdgeWeight(BB, NonTakenIdx, PH_NONTAKEN_WEIGHT); 00303 return true; 00304 } 00305 00306 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges 00307 // as taken, exiting edges as not-taken. 00308 bool BranchProbabilityInfo::calcLoopBranchHeuristics(BasicBlock *BB) { 00309 Loop *L = LI->getLoopFor(BB); 00310 if (!L) 00311 return false; 00312 00313 SmallVector<unsigned, 8> BackEdges; 00314 SmallVector<unsigned, 8> ExitingEdges; 00315 SmallVector<unsigned, 8> InEdges; // Edges from header to the loop. 00316 00317 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 00318 if (!L->contains(*I)) 00319 ExitingEdges.push_back(I.getSuccessorIndex()); 00320 else if (L->getHeader() == *I) 00321 BackEdges.push_back(I.getSuccessorIndex()); 00322 else 00323 InEdges.push_back(I.getSuccessorIndex()); 00324 } 00325 00326 if (BackEdges.empty() && ExitingEdges.empty()) 00327 return false; 00328 00329 if (uint32_t numBackEdges = BackEdges.size()) { 00330 uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges; 00331 if (backWeight < NORMAL_WEIGHT) 00332 backWeight = NORMAL_WEIGHT; 00333 00334 for (SmallVectorImpl<unsigned>::iterator EI = BackEdges.begin(), 00335 EE = BackEdges.end(); EI != EE; ++EI) { 00336 setEdgeWeight(BB, *EI, backWeight); 00337 } 00338 } 00339 00340 if (uint32_t numInEdges = InEdges.size()) { 00341 uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges; 00342 if (inWeight < NORMAL_WEIGHT) 00343 inWeight = NORMAL_WEIGHT; 00344 00345 for (SmallVectorImpl<unsigned>::iterator EI = InEdges.begin(), 00346 EE = InEdges.end(); EI != EE; ++EI) { 00347 setEdgeWeight(BB, *EI, inWeight); 00348 } 00349 } 00350 00351 if (uint32_t numExitingEdges = ExitingEdges.size()) { 00352 uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges; 00353 if (exitWeight < MIN_WEIGHT) 00354 exitWeight = MIN_WEIGHT; 00355 00356 for (SmallVectorImpl<unsigned>::iterator EI = ExitingEdges.begin(), 00357 EE = ExitingEdges.end(); EI != EE; ++EI) { 00358 setEdgeWeight(BB, *EI, exitWeight); 00359 } 00360 } 00361 00362 return true; 00363 } 00364 00365 bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) { 00366 BranchInst * BI = dyn_cast<BranchInst>(BB->getTerminator()); 00367 if (!BI || !BI->isConditional()) 00368 return false; 00369 00370 Value *Cond = BI->getCondition(); 00371 ICmpInst *CI = dyn_cast<ICmpInst>(Cond); 00372 if (!CI) 00373 return false; 00374 00375 Value *RHS = CI->getOperand(1); 00376 ConstantInt *CV = dyn_cast<ConstantInt>(RHS); 00377 if (!CV) 00378 return false; 00379 00380 bool isProb; 00381 if (CV->isZero()) { 00382 switch (CI->getPredicate()) { 00383 case CmpInst::ICMP_EQ: 00384 // X == 0 -> Unlikely 00385 isProb = false; 00386 break; 00387 case CmpInst::ICMP_NE: 00388 // X != 0 -> Likely 00389 isProb = true; 00390 break; 00391 case CmpInst::ICMP_SLT: 00392 // X < 0 -> Unlikely 00393 isProb = false; 00394 break; 00395 case CmpInst::ICMP_SGT: 00396 // X > 0 -> Likely 00397 isProb = true; 00398 break; 00399 default: 00400 return false; 00401 } 00402 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { 00403 // InstCombine canonicalizes X <= 0 into X < 1. 00404 // X <= 0 -> Unlikely 00405 isProb = false; 00406 } else if (CV->isAllOnesValue()) { 00407 switch (CI->getPredicate()) { 00408 case CmpInst::ICMP_EQ: 00409 // X == -1 -> Unlikely 00410 isProb = false; 00411 break; 00412 case CmpInst::ICMP_NE: 00413 // X != -1 -> Likely 00414 isProb = true; 00415 break; 00416 case CmpInst::ICMP_SGT: 00417 // InstCombine canonicalizes X >= 0 into X > -1. 00418 // X >= 0 -> Likely 00419 isProb = true; 00420 break; 00421 default: 00422 return false; 00423 } 00424 } else { 00425 return false; 00426 } 00427 00428 unsigned TakenIdx = 0, NonTakenIdx = 1; 00429 00430 if (!isProb) 00431 std::swap(TakenIdx, NonTakenIdx); 00432 00433 setEdgeWeight(BB, TakenIdx, ZH_TAKEN_WEIGHT); 00434 setEdgeWeight(BB, NonTakenIdx, ZH_NONTAKEN_WEIGHT); 00435 00436 return true; 00437 } 00438 00439 bool BranchProbabilityInfo::calcFloatingPointHeuristics(BasicBlock *BB) { 00440 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 00441 if (!BI || !BI->isConditional()) 00442 return false; 00443 00444 Value *Cond = BI->getCondition(); 00445 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond); 00446 if (!FCmp) 00447 return false; 00448 00449 bool isProb; 00450 if (FCmp->isEquality()) { 00451 // f1 == f2 -> Unlikely 00452 // f1 != f2 -> Likely 00453 isProb = !FCmp->isTrueWhenEqual(); 00454 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) { 00455 // !isnan -> Likely 00456 isProb = true; 00457 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) { 00458 // isnan -> Unlikely 00459 isProb = false; 00460 } else { 00461 return false; 00462 } 00463 00464 unsigned TakenIdx = 0, NonTakenIdx = 1; 00465 00466 if (!isProb) 00467 std::swap(TakenIdx, NonTakenIdx); 00468 00469 setEdgeWeight(BB, TakenIdx, FPH_TAKEN_WEIGHT); 00470 setEdgeWeight(BB, NonTakenIdx, FPH_NONTAKEN_WEIGHT); 00471 00472 return true; 00473 } 00474 00475 bool BranchProbabilityInfo::calcInvokeHeuristics(BasicBlock *BB) { 00476 InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()); 00477 if (!II) 00478 return false; 00479 00480 setEdgeWeight(BB, 0/*Index for Normal*/, IH_TAKEN_WEIGHT); 00481 setEdgeWeight(BB, 1/*Index for Unwind*/, IH_NONTAKEN_WEIGHT); 00482 return true; 00483 } 00484 00485 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const { 00486 AU.addRequired<LoopInfo>(); 00487 AU.setPreservesAll(); 00488 } 00489 00490 bool BranchProbabilityInfo::runOnFunction(Function &F) { 00491 DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName() 00492 << " ----\n\n"); 00493 LastF = &F; // Store the last function we ran on for printing. 00494 LI = &getAnalysis<LoopInfo>(); 00495 assert(PostDominatedByUnreachable.empty()); 00496 assert(PostDominatedByColdCall.empty()); 00497 00498 // Walk the basic blocks in post-order so that we can build up state about 00499 // the successors of a block iteratively. 00500 for (po_iterator<BasicBlock *> I = po_begin(&F.getEntryBlock()), 00501 E = po_end(&F.getEntryBlock()); 00502 I != E; ++I) { 00503 DEBUG(dbgs() << "Computing probabilities for " << I->getName() << "\n"); 00504 if (calcUnreachableHeuristics(*I)) 00505 continue; 00506 if (calcMetadataWeights(*I)) 00507 continue; 00508 if (calcColdCallHeuristics(*I)) 00509 continue; 00510 if (calcLoopBranchHeuristics(*I)) 00511 continue; 00512 if (calcPointerHeuristics(*I)) 00513 continue; 00514 if (calcZeroHeuristics(*I)) 00515 continue; 00516 if (calcFloatingPointHeuristics(*I)) 00517 continue; 00518 calcInvokeHeuristics(*I); 00519 } 00520 00521 PostDominatedByUnreachable.clear(); 00522 PostDominatedByColdCall.clear(); 00523 return false; 00524 } 00525 00526 void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const { 00527 OS << "---- Branch Probabilities ----\n"; 00528 // We print the probabilities from the last function the analysis ran over, 00529 // or the function it is currently running over. 00530 assert(LastF && "Cannot print prior to running over a function"); 00531 for (Function::const_iterator BI = LastF->begin(), BE = LastF->end(); 00532 BI != BE; ++BI) { 00533 for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI); 00534 SI != SE; ++SI) { 00535 printEdgeProbability(OS << " ", BI, *SI); 00536 } 00537 } 00538 } 00539 00540 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const { 00541 uint32_t Sum = 0; 00542 00543 for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 00544 uint32_t Weight = getEdgeWeight(BB, I.getSuccessorIndex()); 00545 uint32_t PrevSum = Sum; 00546 00547 Sum += Weight; 00548 assert(Sum > PrevSum); (void) PrevSum; 00549 } 00550 00551 return Sum; 00552 } 00553 00554 bool BranchProbabilityInfo:: 00555 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const { 00556 // Hot probability is at least 4/5 = 80% 00557 // FIXME: Compare against a static "hot" BranchProbability. 00558 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); 00559 } 00560 00561 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const { 00562 uint32_t Sum = 0; 00563 uint32_t MaxWeight = 0; 00564 BasicBlock *MaxSucc = nullptr; 00565 00566 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 00567 BasicBlock *Succ = *I; 00568 uint32_t Weight = getEdgeWeight(BB, Succ); 00569 uint32_t PrevSum = Sum; 00570 00571 Sum += Weight; 00572 assert(Sum > PrevSum); (void) PrevSum; 00573 00574 if (Weight > MaxWeight) { 00575 MaxWeight = Weight; 00576 MaxSucc = Succ; 00577 } 00578 } 00579 00580 // Hot probability is at least 4/5 = 80% 00581 if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5)) 00582 return MaxSucc; 00583 00584 return nullptr; 00585 } 00586 00587 /// Get the raw edge weight for the edge. If can't find it, return 00588 /// DEFAULT_WEIGHT value. Here an edge is specified using PredBlock and an index 00589 /// to the successors. 00590 uint32_t BranchProbabilityInfo:: 00591 getEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors) const { 00592 DenseMap<Edge, uint32_t>::const_iterator I = 00593 Weights.find(std::make_pair(Src, IndexInSuccessors)); 00594 00595 if (I != Weights.end()) 00596 return I->second; 00597 00598 return DEFAULT_WEIGHT; 00599 } 00600 00601 uint32_t BranchProbabilityInfo::getEdgeWeight(const BasicBlock *Src, 00602 succ_const_iterator Dst) const { 00603 return getEdgeWeight(Src, Dst.getSuccessorIndex()); 00604 } 00605 00606 /// Get the raw edge weight calculated for the block pair. This returns the sum 00607 /// of all raw edge weights from Src to Dst. 00608 uint32_t BranchProbabilityInfo:: 00609 getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const { 00610 uint32_t Weight = 0; 00611 DenseMap<Edge, uint32_t>::const_iterator MapI; 00612 for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I) 00613 if (*I == Dst) { 00614 MapI = Weights.find(std::make_pair(Src, I.getSuccessorIndex())); 00615 if (MapI != Weights.end()) 00616 Weight += MapI->second; 00617 } 00618 return (Weight == 0) ? DEFAULT_WEIGHT : Weight; 00619 } 00620 00621 /// Set the edge weight for a given edge specified by PredBlock and an index 00622 /// to the successors. 00623 void BranchProbabilityInfo:: 00624 setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors, 00625 uint32_t Weight) { 00626 Weights[std::make_pair(Src, IndexInSuccessors)] = Weight; 00627 DEBUG(dbgs() << "set edge " << Src->getName() << " -> " 00628 << IndexInSuccessors << " successor weight to " 00629 << Weight << "\n"); 00630 } 00631 00632 /// Get an edge's probability, relative to other out-edges from Src. 00633 BranchProbability BranchProbabilityInfo:: 00634 getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const { 00635 uint32_t N = getEdgeWeight(Src, IndexInSuccessors); 00636 uint32_t D = getSumForBlock(Src); 00637 00638 return BranchProbability(N, D); 00639 } 00640 00641 /// Get the probability of going from Src to Dst. It returns the sum of all 00642 /// probabilities for edges from Src to Dst. 00643 BranchProbability BranchProbabilityInfo:: 00644 getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const { 00645 00646 uint32_t N = getEdgeWeight(Src, Dst); 00647 uint32_t D = getSumForBlock(Src); 00648 00649 return BranchProbability(N, D); 00650 } 00651 00652 raw_ostream & 00653 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, 00654 const BasicBlock *Src, 00655 const BasicBlock *Dst) const { 00656 00657 const BranchProbability Prob = getEdgeProbability(Src, Dst); 00658 OS << "edge " << Src->getName() << " -> " << Dst->getName() 00659 << " probability is " << Prob 00660 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); 00661 00662 return OS; 00663 }