1 //===- BranchProbabilityInfo.cpp - Branch Probability Analysis ------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Loops should be simplified before this analysis. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Analysis/BranchProbabilityInfo.h" 14 #include "llvm/ADT/PostOrderIterator.h" 15 #include "llvm/ADT/SCCIterator.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/Analysis/LoopInfo.h" 19 #include "llvm/Analysis/PostDominators.h" 20 #include "llvm/Analysis/TargetLibraryInfo.h" 21 #include "llvm/IR/Attributes.h" 22 #include "llvm/IR/BasicBlock.h" 23 #include "llvm/IR/CFG.h" 24 #include "llvm/IR/Constants.h" 25 #include "llvm/IR/Dominators.h" 26 #include "llvm/IR/Function.h" 27 #include "llvm/IR/InstrTypes.h" 28 #include "llvm/IR/Instruction.h" 29 #include "llvm/IR/Instructions.h" 30 #include "llvm/IR/LLVMContext.h" 31 #include "llvm/IR/Metadata.h" 32 #include "llvm/IR/PassManager.h" 33 #include "llvm/IR/Type.h" 34 #include "llvm/IR/Value.h" 35 #include "llvm/InitializePasses.h" 36 #include "llvm/Pass.h" 37 #include "llvm/Support/BranchProbability.h" 38 #include "llvm/Support/Casting.h" 39 #include "llvm/Support/CommandLine.h" 40 #include "llvm/Support/Debug.h" 41 #include "llvm/Support/MathExtras.h" 42 #include "llvm/Support/raw_ostream.h" 43 #include <cassert> 44 #include <cstdint> 45 #include <iterator> 46 #include <utility> 47 48 using namespace llvm; 49 50 #define DEBUG_TYPE "branch-prob" 51 52 static cl::opt<bool> PrintBranchProb( 53 "print-bpi", cl::init(false), cl::Hidden, 54 cl::desc("Print the branch probability info.")); 55 56 cl::opt<std::string> PrintBranchProbFuncName( 57 "print-bpi-func-name", cl::Hidden, 58 cl::desc("The option to specify the name of the function " 59 "whose branch probability info is printed.")); 60 61 INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob", 62 "Branch Probability Analysis", false, true) 63 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 64 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 65 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) 66 INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass, "branch-prob", 67 "Branch Probability Analysis", false, true) 68 69 BranchProbabilityInfoWrapperPass::BranchProbabilityInfoWrapperPass() 70 : FunctionPass(ID) { 71 initializeBranchProbabilityInfoWrapperPassPass( 72 *PassRegistry::getPassRegistry()); 73 } 74 75 char BranchProbabilityInfoWrapperPass::ID = 0; 76 77 // Weights are for internal use only. They are used by heuristics to help to 78 // estimate edges' probability. Example: 79 // 80 // Using "Loop Branch Heuristics" we predict weights of edges for the 81 // block BB2. 82 // ... 83 // | 84 // V 85 // BB1<-+ 86 // | | 87 // | | (Weight = 124) 88 // V | 89 // BB2--+ 90 // | 91 // | (Weight = 4) 92 // V 93 // BB3 94 // 95 // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875 96 // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125 97 static const uint32_t LBH_TAKEN_WEIGHT = 124; 98 static const uint32_t LBH_NONTAKEN_WEIGHT = 4; 99 // Unlikely edges within a loop are half as likely as other edges 100 static const uint32_t LBH_UNLIKELY_WEIGHT = 62; 101 102 /// Unreachable-terminating branch taken probability. 103 /// 104 /// This is the probability for a branch being taken to a block that terminates 105 /// (eventually) in unreachable. These are predicted as unlikely as possible. 106 /// All reachable probability will proportionally share the remaining part. 107 static const BranchProbability UR_TAKEN_PROB = BranchProbability::getRaw(1); 108 109 /// Weight for a branch taken going into a cold block. 110 /// 111 /// This is the weight for a branch taken toward a block marked 112 /// cold. A block is marked cold if it's postdominated by a 113 /// block containing a call to a cold function. Cold functions 114 /// are those marked with attribute 'cold'. 115 static const uint32_t CC_TAKEN_WEIGHT = 4; 116 117 /// Weight for a branch not-taken into a cold block. 118 /// 119 /// This is the weight for a branch not taken toward a block marked 120 /// cold. 121 static const uint32_t CC_NONTAKEN_WEIGHT = 64; 122 123 static const uint32_t PH_TAKEN_WEIGHT = 20; 124 static const uint32_t PH_NONTAKEN_WEIGHT = 12; 125 126 static const uint32_t ZH_TAKEN_WEIGHT = 20; 127 static const uint32_t ZH_NONTAKEN_WEIGHT = 12; 128 129 static const uint32_t FPH_TAKEN_WEIGHT = 20; 130 static const uint32_t FPH_NONTAKEN_WEIGHT = 12; 131 132 /// This is the probability for an ordered floating point comparison. 133 static const uint32_t FPH_ORD_WEIGHT = 1024 * 1024 - 1; 134 /// This is the probability for an unordered floating point comparison, it means 135 /// one or two of the operands are NaN. Usually it is used to test for an 136 /// exceptional case, so the result is unlikely. 137 static const uint32_t FPH_UNO_WEIGHT = 1; 138 139 /// Invoke-terminating normal branch taken weight 140 /// 141 /// This is the weight for branching to the normal destination of an invoke 142 /// instruction. We expect this to happen most of the time. Set the weight to an 143 /// absurdly high value so that nested loops subsume it. 144 static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1; 145 146 /// Invoke-terminating normal branch not-taken weight. 147 /// 148 /// This is the weight for branching to the unwind destination of an invoke 149 /// instruction. This is essentially never taken. 150 static const uint32_t IH_NONTAKEN_WEIGHT = 1; 151 152 BranchProbabilityInfo::SccInfo::SccInfo(const Function &F) { 153 // Record SCC numbers of blocks in the CFG to identify irreducible loops. 154 // FIXME: We could only calculate this if the CFG is known to be irreducible 155 // (perhaps cache this info in LoopInfo if we can easily calculate it there?). 156 int SccNum = 0; 157 for (scc_iterator<const Function *> It = scc_begin(&F); !It.isAtEnd(); 158 ++It, ++SccNum) { 159 // Ignore single-block SCCs since they either aren't loops or LoopInfo will 160 // catch them. 161 const std::vector<const BasicBlock *> &Scc = *It; 162 if (Scc.size() == 1) 163 continue; 164 165 LLVM_DEBUG(dbgs() << "BPI: SCC " << SccNum << ":"); 166 for (const auto *BB : Scc) { 167 LLVM_DEBUG(dbgs() << " " << BB->getName()); 168 SccNums[BB] = SccNum; 169 calculateSccBlockType(BB, SccNum); 170 } 171 LLVM_DEBUG(dbgs() << "\n"); 172 } 173 } 174 175 int BranchProbabilityInfo::SccInfo::getSCCNum(const BasicBlock *BB) const { 176 auto SccIt = SccNums.find(BB); 177 if (SccIt == SccNums.end()) 178 return -1; 179 return SccIt->second; 180 } 181 182 void BranchProbabilityInfo::SccInfo::getSccEnterBlocks( 183 int SccNum, SmallVectorImpl<BasicBlock *> &Enters) const { 184 185 for (auto MapIt : SccBlocks[SccNum]) { 186 const auto *BB = MapIt.first; 187 if (isSCCHeader(BB, SccNum)) 188 for (const auto *Pred : predecessors(BB)) 189 if (getSCCNum(Pred) != SccNum) 190 Enters.push_back(const_cast<BasicBlock *>(BB)); 191 } 192 } 193 194 void BranchProbabilityInfo::SccInfo::getSccExitBlocks( 195 int SccNum, SmallVectorImpl<BasicBlock *> &Exits) const { 196 for (auto MapIt : SccBlocks[SccNum]) { 197 const auto *BB = MapIt.first; 198 if (isSCCExitingBlock(BB, SccNum)) 199 for (const auto *Succ : successors(BB)) 200 if (getSCCNum(Succ) != SccNum) 201 Exits.push_back(const_cast<BasicBlock *>(BB)); 202 } 203 } 204 205 uint32_t BranchProbabilityInfo::SccInfo::getSccBlockType(const BasicBlock *BB, 206 int SccNum) const { 207 assert(getSCCNum(BB) == SccNum); 208 209 assert(SccBlocks.size() > static_cast<unsigned>(SccNum) && "Unknown SCC"); 210 const auto &SccBlockTypes = SccBlocks[SccNum]; 211 212 auto It = SccBlockTypes.find(BB); 213 if (It != SccBlockTypes.end()) { 214 return It->second; 215 } 216 return Inner; 217 } 218 219 void BranchProbabilityInfo::SccInfo::calculateSccBlockType(const BasicBlock *BB, 220 int SccNum) { 221 assert(getSCCNum(BB) == SccNum); 222 uint32_t BlockType = Inner; 223 224 if (llvm::any_of(make_range(pred_begin(BB), pred_end(BB)), 225 [&](const BasicBlock *Pred) { 226 // Consider any block that is an entry point to the SCC as 227 // a header. 228 return getSCCNum(Pred) != SccNum; 229 })) 230 BlockType |= Header; 231 232 if (llvm::any_of( 233 make_range(succ_begin(BB), succ_end(BB)), 234 [&](const BasicBlock *Succ) { return getSCCNum(Succ) != SccNum; })) 235 BlockType |= Exiting; 236 237 // Lazily compute the set of headers for a given SCC and cache the results 238 // in the SccHeaderMap. 239 if (SccBlocks.size() <= static_cast<unsigned>(SccNum)) 240 SccBlocks.resize(SccNum + 1); 241 auto &SccBlockTypes = SccBlocks[SccNum]; 242 243 if (BlockType != Inner) { 244 bool IsInserted; 245 std::tie(std::ignore, IsInserted) = 246 SccBlockTypes.insert(std::make_pair(BB, BlockType)); 247 assert(IsInserted && "Duplicated block in SCC"); 248 } 249 } 250 251 BranchProbabilityInfo::LoopBlock::LoopBlock(const BasicBlock *BB, 252 const LoopInfo &LI, 253 const SccInfo &SccI) 254 : BB(BB) { 255 LD.first = LI.getLoopFor(BB); 256 if (!LD.first) { 257 LD.second = SccI.getSCCNum(BB); 258 } 259 } 260 261 bool BranchProbabilityInfo::isLoopEnteringEdge(const LoopEdge &Edge) const { 262 const auto &SrcBlock = Edge.first; 263 const auto &DstBlock = Edge.second; 264 return (DstBlock.getLoop() && 265 !DstBlock.getLoop()->contains(SrcBlock.getLoop())) || 266 // Assume that SCCs can't be nested. 267 (DstBlock.getSccNum() != -1 && 268 SrcBlock.getSccNum() != DstBlock.getSccNum()); 269 } 270 271 bool BranchProbabilityInfo::isLoopExitingEdge(const LoopEdge &Edge) const { 272 return isLoopEnteringEdge({Edge.second, Edge.first}); 273 } 274 275 bool BranchProbabilityInfo::isLoopEnteringExitingEdge( 276 const LoopEdge &Edge) const { 277 return isLoopEnteringEdge(Edge) || isLoopExitingEdge(Edge); 278 } 279 280 bool BranchProbabilityInfo::isLoopBackEdge(const LoopEdge &Edge) const { 281 const auto &SrcBlock = Edge.first; 282 const auto &DstBlock = Edge.second; 283 return SrcBlock.belongsToSameLoop(DstBlock) && 284 ((DstBlock.getLoop() && 285 DstBlock.getLoop()->getHeader() == DstBlock.getBlock()) || 286 (DstBlock.getSccNum() != -1 && 287 SccI->isSCCHeader(DstBlock.getBlock(), DstBlock.getSccNum()))); 288 } 289 290 void BranchProbabilityInfo::getLoopEnterBlocks( 291 const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Enters) const { 292 if (LB.getLoop()) { 293 auto *Header = LB.getLoop()->getHeader(); 294 Enters.append(pred_begin(Header), pred_end(Header)); 295 } else { 296 assert(LB.getSccNum() != -1 && "LB doesn't belong to any loop?"); 297 SccI->getSccEnterBlocks(LB.getSccNum(), Enters); 298 } 299 } 300 301 void BranchProbabilityInfo::getLoopExitBlocks( 302 const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Exits) const { 303 if (LB.getLoop()) { 304 LB.getLoop()->getExitBlocks(Exits); 305 } else { 306 assert(LB.getSccNum() != -1 && "LB doesn't belong to any loop?"); 307 SccI->getSccExitBlocks(LB.getSccNum(), Exits); 308 } 309 } 310 311 static void UpdatePDTWorklist(const BasicBlock *BB, PostDominatorTree *PDT, 312 SmallVectorImpl<const BasicBlock *> &WorkList, 313 SmallPtrSetImpl<const BasicBlock *> &TargetSet) { 314 SmallVector<BasicBlock *, 8> Descendants; 315 SmallPtrSet<const BasicBlock *, 16> NewItems; 316 317 PDT->getDescendants(const_cast<BasicBlock *>(BB), Descendants); 318 for (auto *BB : Descendants) 319 if (TargetSet.insert(BB).second) 320 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 321 if (!TargetSet.count(*PI)) 322 NewItems.insert(*PI); 323 WorkList.insert(WorkList.end(), NewItems.begin(), NewItems.end()); 324 } 325 326 /// Compute a set of basic blocks that are post-dominated by unreachables. 327 void BranchProbabilityInfo::computePostDominatedByUnreachable( 328 const Function &F, PostDominatorTree *PDT) { 329 SmallVector<const BasicBlock *, 8> WorkList; 330 for (auto &BB : F) { 331 const Instruction *TI = BB.getTerminator(); 332 if (TI->getNumSuccessors() == 0) { 333 if (isa<UnreachableInst>(TI) || 334 // If this block is terminated by a call to 335 // @llvm.experimental.deoptimize then treat it like an unreachable 336 // since the @llvm.experimental.deoptimize call is expected to 337 // practically never execute. 338 BB.getTerminatingDeoptimizeCall()) 339 UpdatePDTWorklist(&BB, PDT, WorkList, PostDominatedByUnreachable); 340 } 341 } 342 343 while (!WorkList.empty()) { 344 const BasicBlock *BB = WorkList.pop_back_val(); 345 if (PostDominatedByUnreachable.count(BB)) 346 continue; 347 // If the terminator is an InvokeInst, check only the normal destination 348 // block as the unwind edge of InvokeInst is also very unlikely taken. 349 if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) { 350 if (PostDominatedByUnreachable.count(II->getNormalDest())) 351 UpdatePDTWorklist(BB, PDT, WorkList, PostDominatedByUnreachable); 352 } 353 // If all the successors are unreachable, BB is unreachable as well. 354 else if (!successors(BB).empty() && 355 llvm::all_of(successors(BB), [this](const BasicBlock *Succ) { 356 return PostDominatedByUnreachable.count(Succ); 357 })) 358 UpdatePDTWorklist(BB, PDT, WorkList, PostDominatedByUnreachable); 359 } 360 } 361 362 /// compute a set of basic blocks that are post-dominated by ColdCalls. 363 void BranchProbabilityInfo::computePostDominatedByColdCall( 364 const Function &F, PostDominatorTree *PDT) { 365 SmallVector<const BasicBlock *, 8> WorkList; 366 for (auto &BB : F) 367 for (auto &I : BB) 368 if (const CallInst *CI = dyn_cast<CallInst>(&I)) 369 if (CI->hasFnAttr(Attribute::Cold)) 370 UpdatePDTWorklist(&BB, PDT, WorkList, PostDominatedByColdCall); 371 372 while (!WorkList.empty()) { 373 const BasicBlock *BB = WorkList.pop_back_val(); 374 375 // If the terminator is an InvokeInst, check only the normal destination 376 // block as the unwind edge of InvokeInst is also very unlikely taken. 377 if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) { 378 if (PostDominatedByColdCall.count(II->getNormalDest())) 379 UpdatePDTWorklist(BB, PDT, WorkList, PostDominatedByColdCall); 380 } 381 // If all of successor are post dominated then BB is also done. 382 else if (!successors(BB).empty() && 383 llvm::all_of(successors(BB), [this](const BasicBlock *Succ) { 384 return PostDominatedByColdCall.count(Succ); 385 })) 386 UpdatePDTWorklist(BB, PDT, WorkList, PostDominatedByColdCall); 387 } 388 } 389 390 /// Calculate edge weights for successors lead to unreachable. 391 /// 392 /// Predict that a successor which leads necessarily to an 393 /// unreachable-terminated block as extremely unlikely. 394 bool BranchProbabilityInfo::calcUnreachableHeuristics(const BasicBlock *BB) { 395 const Instruction *TI = BB->getTerminator(); 396 (void) TI; 397 assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); 398 assert(!isa<InvokeInst>(TI) && 399 "Invokes should have already been handled by calcInvokeHeuristics"); 400 401 SmallVector<unsigned, 4> UnreachableEdges; 402 SmallVector<unsigned, 4> ReachableEdges; 403 404 for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) 405 if (PostDominatedByUnreachable.count(*I)) 406 UnreachableEdges.push_back(I.getSuccessorIndex()); 407 else 408 ReachableEdges.push_back(I.getSuccessorIndex()); 409 410 // Skip probabilities if all were reachable. 411 if (UnreachableEdges.empty()) 412 return false; 413 414 SmallVector<BranchProbability, 4> EdgeProbabilities( 415 BB->getTerminator()->getNumSuccessors(), BranchProbability::getUnknown()); 416 if (ReachableEdges.empty()) { 417 BranchProbability Prob(1, UnreachableEdges.size()); 418 for (unsigned SuccIdx : UnreachableEdges) 419 EdgeProbabilities[SuccIdx] = Prob; 420 setEdgeProbability(BB, EdgeProbabilities); 421 return true; 422 } 423 424 auto UnreachableProb = UR_TAKEN_PROB; 425 auto ReachableProb = 426 (BranchProbability::getOne() - UR_TAKEN_PROB * UnreachableEdges.size()) / 427 ReachableEdges.size(); 428 429 for (unsigned SuccIdx : UnreachableEdges) 430 EdgeProbabilities[SuccIdx] = UnreachableProb; 431 for (unsigned SuccIdx : ReachableEdges) 432 EdgeProbabilities[SuccIdx] = ReachableProb; 433 434 setEdgeProbability(BB, EdgeProbabilities); 435 return true; 436 } 437 438 // Scales all values in Weights so that the total fits in 64 bits. Returns the 439 // total. 440 // FIXME: only scale by the minimum necessary to fit the total within 64 bits. 441 static uint64_t ScaleWeights(MutableArrayRef<uint64_t> Weights) { 442 uint64_t Total = 0; 443 bool Overflowed = false; 444 for (uint64_t W : Weights) { 445 Total = SaturatingAdd(Total, W, &Overflowed); 446 if (Overflowed) 447 break; 448 } 449 if (Overflowed) { 450 uint64_t ScaledTotal = 0; 451 for (uint64_t &W : Weights) { 452 W /= UINT32_MAX; 453 ScaledTotal += W; 454 } 455 return ScaledTotal; 456 } 457 return Total; 458 } 459 460 // Propagate existing explicit probabilities from either profile data or 461 // 'expect' intrinsic processing. Examine metadata against unreachable 462 // heuristic. The probability of the edge coming to unreachable block is 463 // set to min of metadata and unreachable heuristic. 464 bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) { 465 const Instruction *TI = BB->getTerminator(); 466 assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); 467 if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI) || 468 isa<InvokeInst>(TI))) 469 return false; 470 471 MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); 472 if (!WeightsNode) 473 return false; 474 475 // Check that the number of successors is manageable. 476 assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors"); 477 478 // Ensure there are weights for all of the successors. Note that the first 479 // operand to the metadata node is a name, not a weight. 480 if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1) 481 return false; 482 483 // Build up the final weights that will be used in a temporary buffer. 484 SmallVector<uint64_t, 2> Weights; 485 SmallVector<unsigned, 2> UnreachableIdxs; 486 SmallVector<unsigned, 2> ReachableIdxs; 487 Weights.reserve(TI->getNumSuccessors()); 488 for (unsigned I = 1, E = WeightsNode->getNumOperands(); I != E; ++I) { 489 ConstantInt *Weight = 490 mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(I)); 491 if (!Weight) 492 return false; 493 // TODO: remove scaling by UINT32_MAX and use full uint64_t range. 494 uint64_t WeightVal = Weight->getZExtValue(); 495 Weights.push_back(WeightVal); 496 // WeightSum += WeightVal; 497 if (PostDominatedByUnreachable.count(TI->getSuccessor(I - 1))) 498 UnreachableIdxs.push_back(I - 1); 499 else 500 ReachableIdxs.push_back(I - 1); 501 } 502 assert(Weights.size() == TI->getNumSuccessors() && "Checked above"); 503 504 uint64_t WeightSum = ScaleWeights(Weights); 505 506 if (WeightSum == 0 || ReachableIdxs.size() == 0) { 507 for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) 508 Weights[I] = 1; 509 WeightSum = TI->getNumSuccessors(); 510 } 511 512 // Set the probability. 513 SmallVector<BranchProbability, 2> BP; 514 for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) 515 BP.push_back( 516 BranchProbability::getBranchProbability(Weights[I], WeightSum)); 517 518 // Examine the metadata against unreachable heuristic. 519 // If the unreachable heuristic is more strong then we use it for this edge. 520 if (UnreachableIdxs.size() == 0 || ReachableIdxs.size() == 0) { 521 setEdgeProbability(BB, BP); 522 return true; 523 } 524 525 auto UnreachableProb = UR_TAKEN_PROB; 526 for (auto I : UnreachableIdxs) 527 if (UnreachableProb < BP[I]) { 528 BP[I] = UnreachableProb; 529 } 530 531 // Sum of all edge probabilities must be 1.0. If we modified the probability 532 // of some edges then we must distribute the introduced difference over the 533 // reachable blocks. 534 // 535 // Proportional distribution: the relation between probabilities of the 536 // reachable edges is kept unchanged. That is for any reachable edges i and j: 537 // newBP[i] / newBP[j] == oldBP[i] / oldBP[j] => 538 // newBP[i] / oldBP[i] == newBP[j] / oldBP[j] == K 539 // Where K is independent of i,j. 540 // newBP[i] == oldBP[i] * K 541 // We need to find K. 542 // Make sum of all reachables of the left and right parts: 543 // sum_of_reachable(newBP) == K * sum_of_reachable(oldBP) 544 // Sum of newBP must be equal to 1.0: 545 // sum_of_reachable(newBP) + sum_of_unreachable(newBP) == 1.0 => 546 // sum_of_reachable(newBP) = 1.0 - sum_of_unreachable(newBP) 547 // Where sum_of_unreachable(newBP) is what has been just changed. 548 // Finally: 549 // K == sum_of_reachable(newBP) / sum_of_reachable(oldBP) => 550 // K == (1.0 - sum_of_unreachable(newBP)) / sum_of_reachable(oldBP) 551 BranchProbability NewUnreachableSum = BranchProbability::getZero(); 552 for (auto I : UnreachableIdxs) 553 NewUnreachableSum += BP[I]; 554 555 BranchProbability NewReachableSum = 556 BranchProbability::getOne() - NewUnreachableSum; 557 558 BranchProbability OldReachableSum = BranchProbability::getZero(); 559 for (auto I : ReachableIdxs) 560 OldReachableSum += BP[I]; 561 562 if (OldReachableSum != NewReachableSum) { // Anything to dsitribute? 563 if (OldReachableSum.isZero()) { 564 // If all oldBP[i] are zeroes then the proportional distribution results 565 // in all zero probabilities and the error stays big. In this case we 566 // evenly spread NewReachableSum over the reachable edges. 567 BranchProbability PerEdge = NewReachableSum / ReachableIdxs.size(); 568 for (auto I : ReachableIdxs) 569 BP[I] = PerEdge; 570 } else { 571 for (auto I : ReachableIdxs) { 572 // We use uint64_t to avoid double rounding error of the following 573 // calculation: BP[i] = BP[i] * NewReachableSum / OldReachableSum 574 // The formula is taken from the private constructor 575 // BranchProbability(uint32_t Numerator, uint32_t Denominator) 576 uint64_t Mul = static_cast<uint64_t>(NewReachableSum.getNumerator()) * 577 BP[I].getNumerator(); 578 uint32_t Div = static_cast<uint32_t>( 579 divideNearest(Mul, OldReachableSum.getNumerator())); 580 BP[I] = BranchProbability::getRaw(Div); 581 } 582 } 583 } 584 585 setEdgeProbability(BB, BP); 586 587 return true; 588 } 589 590 /// Calculate edge weights for edges leading to cold blocks. 591 /// 592 /// A cold block is one post-dominated by a block with a call to a 593 /// cold function. Those edges are unlikely to be taken, so we give 594 /// them relatively low weight. 595 /// 596 /// Return true if we could compute the weights for cold edges. 597 /// Return false, otherwise. 598 bool BranchProbabilityInfo::calcColdCallHeuristics(const BasicBlock *BB) { 599 const Instruction *TI = BB->getTerminator(); 600 (void) TI; 601 assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); 602 assert(!isa<InvokeInst>(TI) && 603 "Invokes should have already been handled by calcInvokeHeuristics"); 604 605 // Determine which successors are post-dominated by a cold block. 606 SmallVector<unsigned, 4> ColdEdges; 607 SmallVector<unsigned, 4> NormalEdges; 608 for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) 609 if (PostDominatedByColdCall.count(*I)) 610 ColdEdges.push_back(I.getSuccessorIndex()); 611 else 612 NormalEdges.push_back(I.getSuccessorIndex()); 613 614 // Skip probabilities if no cold edges. 615 if (ColdEdges.empty()) 616 return false; 617 618 SmallVector<BranchProbability, 4> EdgeProbabilities( 619 BB->getTerminator()->getNumSuccessors(), BranchProbability::getUnknown()); 620 if (NormalEdges.empty()) { 621 BranchProbability Prob(1, ColdEdges.size()); 622 for (unsigned SuccIdx : ColdEdges) 623 EdgeProbabilities[SuccIdx] = Prob; 624 setEdgeProbability(BB, EdgeProbabilities); 625 return true; 626 } 627 628 auto ColdProb = BranchProbability::getBranchProbability( 629 CC_TAKEN_WEIGHT, 630 (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(ColdEdges.size())); 631 auto NormalProb = BranchProbability::getBranchProbability( 632 CC_NONTAKEN_WEIGHT, 633 (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(NormalEdges.size())); 634 635 for (unsigned SuccIdx : ColdEdges) 636 EdgeProbabilities[SuccIdx] = ColdProb; 637 for (unsigned SuccIdx : NormalEdges) 638 EdgeProbabilities[SuccIdx] = NormalProb; 639 640 setEdgeProbability(BB, EdgeProbabilities); 641 return true; 642 } 643 644 // Calculate Edge Weights using "Pointer Heuristics". Predict a comparison 645 // between two pointer or pointer and NULL will fail. 646 bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) { 647 const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 648 if (!BI || !BI->isConditional()) 649 return false; 650 651 Value *Cond = BI->getCondition(); 652 ICmpInst *CI = dyn_cast<ICmpInst>(Cond); 653 if (!CI || !CI->isEquality()) 654 return false; 655 656 Value *LHS = CI->getOperand(0); 657 658 if (!LHS->getType()->isPointerTy()) 659 return false; 660 661 assert(CI->getOperand(1)->getType()->isPointerTy()); 662 663 BranchProbability TakenProb(PH_TAKEN_WEIGHT, 664 PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT); 665 BranchProbability UntakenProb(PH_NONTAKEN_WEIGHT, 666 PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT); 667 668 // p != 0 -> isProb = true 669 // p == 0 -> isProb = false 670 // p != q -> isProb = true 671 // p == q -> isProb = false; 672 bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE; 673 if (!isProb) 674 std::swap(TakenProb, UntakenProb); 675 676 setEdgeProbability( 677 BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb})); 678 return true; 679 } 680 681 // Compute the unlikely successors to the block BB in the loop L, specifically 682 // those that are unlikely because this is a loop, and add them to the 683 // UnlikelyBlocks set. 684 static void 685 computeUnlikelySuccessors(const BasicBlock *BB, Loop *L, 686 SmallPtrSetImpl<const BasicBlock*> &UnlikelyBlocks) { 687 // Sometimes in a loop we have a branch whose condition is made false by 688 // taking it. This is typically something like 689 // int n = 0; 690 // while (...) { 691 // if (++n >= MAX) { 692 // n = 0; 693 // } 694 // } 695 // In this sort of situation taking the branch means that at the very least it 696 // won't be taken again in the next iteration of the loop, so we should 697 // consider it less likely than a typical branch. 698 // 699 // We detect this by looking back through the graph of PHI nodes that sets the 700 // value that the condition depends on, and seeing if we can reach a successor 701 // block which can be determined to make the condition false. 702 // 703 // FIXME: We currently consider unlikely blocks to be half as likely as other 704 // blocks, but if we consider the example above the likelyhood is actually 705 // 1/MAX. We could therefore be more precise in how unlikely we consider 706 // blocks to be, but it would require more careful examination of the form 707 // of the comparison expression. 708 const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 709 if (!BI || !BI->isConditional()) 710 return; 711 712 // Check if the branch is based on an instruction compared with a constant 713 CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition()); 714 if (!CI || !isa<Instruction>(CI->getOperand(0)) || 715 !isa<Constant>(CI->getOperand(1))) 716 return; 717 718 // Either the instruction must be a PHI, or a chain of operations involving 719 // constants that ends in a PHI which we can then collapse into a single value 720 // if the PHI value is known. 721 Instruction *CmpLHS = dyn_cast<Instruction>(CI->getOperand(0)); 722 PHINode *CmpPHI = dyn_cast<PHINode>(CmpLHS); 723 Constant *CmpConst = dyn_cast<Constant>(CI->getOperand(1)); 724 // Collect the instructions until we hit a PHI 725 SmallVector<BinaryOperator *, 1> InstChain; 726 while (!CmpPHI && CmpLHS && isa<BinaryOperator>(CmpLHS) && 727 isa<Constant>(CmpLHS->getOperand(1))) { 728 // Stop if the chain extends outside of the loop 729 if (!L->contains(CmpLHS)) 730 return; 731 InstChain.push_back(cast<BinaryOperator>(CmpLHS)); 732 CmpLHS = dyn_cast<Instruction>(CmpLHS->getOperand(0)); 733 if (CmpLHS) 734 CmpPHI = dyn_cast<PHINode>(CmpLHS); 735 } 736 if (!CmpPHI || !L->contains(CmpPHI)) 737 return; 738 739 // Trace the phi node to find all values that come from successors of BB 740 SmallPtrSet<PHINode*, 8> VisitedInsts; 741 SmallVector<PHINode*, 8> WorkList; 742 WorkList.push_back(CmpPHI); 743 VisitedInsts.insert(CmpPHI); 744 while (!WorkList.empty()) { 745 PHINode *P = WorkList.back(); 746 WorkList.pop_back(); 747 for (BasicBlock *B : P->blocks()) { 748 // Skip blocks that aren't part of the loop 749 if (!L->contains(B)) 750 continue; 751 Value *V = P->getIncomingValueForBlock(B); 752 // If the source is a PHI add it to the work list if we haven't 753 // already visited it. 754 if (PHINode *PN = dyn_cast<PHINode>(V)) { 755 if (VisitedInsts.insert(PN).second) 756 WorkList.push_back(PN); 757 continue; 758 } 759 // If this incoming value is a constant and B is a successor of BB, then 760 // we can constant-evaluate the compare to see if it makes the branch be 761 // taken or not. 762 Constant *CmpLHSConst = dyn_cast<Constant>(V); 763 if (!CmpLHSConst || !llvm::is_contained(successors(BB), B)) 764 continue; 765 // First collapse InstChain 766 for (Instruction *I : llvm::reverse(InstChain)) { 767 CmpLHSConst = ConstantExpr::get(I->getOpcode(), CmpLHSConst, 768 cast<Constant>(I->getOperand(1)), true); 769 if (!CmpLHSConst) 770 break; 771 } 772 if (!CmpLHSConst) 773 continue; 774 // Now constant-evaluate the compare 775 Constant *Result = ConstantExpr::getCompare(CI->getPredicate(), 776 CmpLHSConst, CmpConst, true); 777 // If the result means we don't branch to the block then that block is 778 // unlikely. 779 if (Result && 780 ((Result->isZeroValue() && B == BI->getSuccessor(0)) || 781 (Result->isOneValue() && B == BI->getSuccessor(1)))) 782 UnlikelyBlocks.insert(B); 783 } 784 } 785 } 786 787 // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges 788 // as taken, exiting edges as not-taken. 789 bool BranchProbabilityInfo::calcLoopBranchHeuristics(const BasicBlock *BB, 790 const LoopInfo &LI) { 791 LoopBlock LB(BB, LI, *SccI.get()); 792 if (!LB.belongsToLoop()) 793 return false; 794 795 SmallPtrSet<const BasicBlock*, 8> UnlikelyBlocks; 796 if (LB.getLoop()) 797 computeUnlikelySuccessors(BB, LB.getLoop(), UnlikelyBlocks); 798 799 SmallVector<unsigned, 8> BackEdges; 800 SmallVector<unsigned, 8> ExitingEdges; 801 SmallVector<unsigned, 8> InEdges; // Edges from header to the loop. 802 SmallVector<unsigned, 8> UnlikelyEdges; 803 804 for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 805 LoopBlock SuccLB(*I, LI, *SccI.get()); 806 LoopEdge Edge(LB, SuccLB); 807 bool IsUnlikelyEdge = 808 LB.getLoop() && (UnlikelyBlocks.find(*I) != UnlikelyBlocks.end()); 809 810 if (IsUnlikelyEdge) 811 UnlikelyEdges.push_back(I.getSuccessorIndex()); 812 else if (isLoopExitingEdge(Edge)) 813 ExitingEdges.push_back(I.getSuccessorIndex()); 814 else if (isLoopBackEdge(Edge)) 815 BackEdges.push_back(I.getSuccessorIndex()); 816 else { 817 InEdges.push_back(I.getSuccessorIndex()); 818 } 819 } 820 821 if (BackEdges.empty() && ExitingEdges.empty() && UnlikelyEdges.empty()) 822 return false; 823 824 // Collect the sum of probabilities of back-edges/in-edges/exiting-edges, and 825 // normalize them so that they sum up to one. 826 unsigned Denom = (BackEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) + 827 (InEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) + 828 (UnlikelyEdges.empty() ? 0 : LBH_UNLIKELY_WEIGHT) + 829 (ExitingEdges.empty() ? 0 : LBH_NONTAKEN_WEIGHT); 830 831 SmallVector<BranchProbability, 4> EdgeProbabilities( 832 BB->getTerminator()->getNumSuccessors(), BranchProbability::getUnknown()); 833 if (uint32_t numBackEdges = BackEdges.size()) { 834 BranchProbability TakenProb = BranchProbability(LBH_TAKEN_WEIGHT, Denom); 835 auto Prob = TakenProb / numBackEdges; 836 for (unsigned SuccIdx : BackEdges) 837 EdgeProbabilities[SuccIdx] = Prob; 838 } 839 840 if (uint32_t numInEdges = InEdges.size()) { 841 BranchProbability TakenProb = BranchProbability(LBH_TAKEN_WEIGHT, Denom); 842 auto Prob = TakenProb / numInEdges; 843 for (unsigned SuccIdx : InEdges) 844 EdgeProbabilities[SuccIdx] = Prob; 845 } 846 847 if (uint32_t numExitingEdges = ExitingEdges.size()) { 848 BranchProbability NotTakenProb = BranchProbability(LBH_NONTAKEN_WEIGHT, 849 Denom); 850 auto Prob = NotTakenProb / numExitingEdges; 851 for (unsigned SuccIdx : ExitingEdges) 852 EdgeProbabilities[SuccIdx] = Prob; 853 } 854 855 if (uint32_t numUnlikelyEdges = UnlikelyEdges.size()) { 856 BranchProbability UnlikelyProb = BranchProbability(LBH_UNLIKELY_WEIGHT, 857 Denom); 858 auto Prob = UnlikelyProb / numUnlikelyEdges; 859 for (unsigned SuccIdx : UnlikelyEdges) 860 EdgeProbabilities[SuccIdx] = Prob; 861 } 862 863 setEdgeProbability(BB, EdgeProbabilities); 864 return true; 865 } 866 867 bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB, 868 const TargetLibraryInfo *TLI) { 869 const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 870 if (!BI || !BI->isConditional()) 871 return false; 872 873 Value *Cond = BI->getCondition(); 874 ICmpInst *CI = dyn_cast<ICmpInst>(Cond); 875 if (!CI) 876 return false; 877 878 auto GetConstantInt = [](Value *V) { 879 if (auto *I = dyn_cast<BitCastInst>(V)) 880 return dyn_cast<ConstantInt>(I->getOperand(0)); 881 return dyn_cast<ConstantInt>(V); 882 }; 883 884 Value *RHS = CI->getOperand(1); 885 ConstantInt *CV = GetConstantInt(RHS); 886 if (!CV) 887 return false; 888 889 // If the LHS is the result of AND'ing a value with a single bit bitmask, 890 // we don't have information about probabilities. 891 if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0))) 892 if (LHS->getOpcode() == Instruction::And) 893 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) 894 if (AndRHS->getValue().isPowerOf2()) 895 return false; 896 897 // Check if the LHS is the return value of a library function 898 LibFunc Func = NumLibFuncs; 899 if (TLI) 900 if (CallInst *Call = dyn_cast<CallInst>(CI->getOperand(0))) 901 if (Function *CalledFn = Call->getCalledFunction()) 902 TLI->getLibFunc(*CalledFn, Func); 903 904 bool isProb; 905 if (Func == LibFunc_strcasecmp || 906 Func == LibFunc_strcmp || 907 Func == LibFunc_strncasecmp || 908 Func == LibFunc_strncmp || 909 Func == LibFunc_memcmp || 910 Func == LibFunc_bcmp) { 911 // strcmp and similar functions return zero, negative, or positive, if the 912 // first string is equal, less, or greater than the second. We consider it 913 // likely that the strings are not equal, so a comparison with zero is 914 // probably false, but also a comparison with any other number is also 915 // probably false given that what exactly is returned for nonzero values is 916 // not specified. Any kind of comparison other than equality we know 917 // nothing about. 918 switch (CI->getPredicate()) { 919 case CmpInst::ICMP_EQ: 920 isProb = false; 921 break; 922 case CmpInst::ICMP_NE: 923 isProb = true; 924 break; 925 default: 926 return false; 927 } 928 } else if (CV->isZero()) { 929 switch (CI->getPredicate()) { 930 case CmpInst::ICMP_EQ: 931 // X == 0 -> Unlikely 932 isProb = false; 933 break; 934 case CmpInst::ICMP_NE: 935 // X != 0 -> Likely 936 isProb = true; 937 break; 938 case CmpInst::ICMP_SLT: 939 // X < 0 -> Unlikely 940 isProb = false; 941 break; 942 case CmpInst::ICMP_SGT: 943 // X > 0 -> Likely 944 isProb = true; 945 break; 946 default: 947 return false; 948 } 949 } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { 950 // InstCombine canonicalizes X <= 0 into X < 1. 951 // X <= 0 -> Unlikely 952 isProb = false; 953 } else if (CV->isMinusOne()) { 954 switch (CI->getPredicate()) { 955 case CmpInst::ICMP_EQ: 956 // X == -1 -> Unlikely 957 isProb = false; 958 break; 959 case CmpInst::ICMP_NE: 960 // X != -1 -> Likely 961 isProb = true; 962 break; 963 case CmpInst::ICMP_SGT: 964 // InstCombine canonicalizes X >= 0 into X > -1. 965 // X >= 0 -> Likely 966 isProb = true; 967 break; 968 default: 969 return false; 970 } 971 } else { 972 return false; 973 } 974 975 BranchProbability TakenProb(ZH_TAKEN_WEIGHT, 976 ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); 977 BranchProbability UntakenProb(ZH_NONTAKEN_WEIGHT, 978 ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); 979 if (!isProb) 980 std::swap(TakenProb, UntakenProb); 981 982 setEdgeProbability( 983 BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb})); 984 return true; 985 } 986 987 bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) { 988 const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 989 if (!BI || !BI->isConditional()) 990 return false; 991 992 Value *Cond = BI->getCondition(); 993 FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond); 994 if (!FCmp) 995 return false; 996 997 uint32_t TakenWeight = FPH_TAKEN_WEIGHT; 998 uint32_t NontakenWeight = FPH_NONTAKEN_WEIGHT; 999 bool isProb; 1000 if (FCmp->isEquality()) { 1001 // f1 == f2 -> Unlikely 1002 // f1 != f2 -> Likely 1003 isProb = !FCmp->isTrueWhenEqual(); 1004 } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) { 1005 // !isnan -> Likely 1006 isProb = true; 1007 TakenWeight = FPH_ORD_WEIGHT; 1008 NontakenWeight = FPH_UNO_WEIGHT; 1009 } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) { 1010 // isnan -> Unlikely 1011 isProb = false; 1012 TakenWeight = FPH_ORD_WEIGHT; 1013 NontakenWeight = FPH_UNO_WEIGHT; 1014 } else { 1015 return false; 1016 } 1017 1018 BranchProbability TakenProb(TakenWeight, TakenWeight + NontakenWeight); 1019 BranchProbability UntakenProb(NontakenWeight, TakenWeight + NontakenWeight); 1020 if (!isProb) 1021 std::swap(TakenProb, UntakenProb); 1022 1023 setEdgeProbability( 1024 BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb})); 1025 return true; 1026 } 1027 1028 bool BranchProbabilityInfo::calcInvokeHeuristics(const BasicBlock *BB) { 1029 const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()); 1030 if (!II) 1031 return false; 1032 1033 BranchProbability TakenProb(IH_TAKEN_WEIGHT, 1034 IH_TAKEN_WEIGHT + IH_NONTAKEN_WEIGHT); 1035 setEdgeProbability( 1036 BB, SmallVector<BranchProbability, 2>({TakenProb, TakenProb.getCompl()})); 1037 return true; 1038 } 1039 1040 void BranchProbabilityInfo::releaseMemory() { 1041 Probs.clear(); 1042 MaxSuccIdx.clear(); 1043 Handles.clear(); 1044 } 1045 1046 bool BranchProbabilityInfo::invalidate(Function &, const PreservedAnalyses &PA, 1047 FunctionAnalysisManager::Invalidator &) { 1048 // Check whether the analysis, all analyses on functions, or the function's 1049 // CFG have been preserved. 1050 auto PAC = PA.getChecker<BranchProbabilityAnalysis>(); 1051 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() || 1052 PAC.preservedSet<CFGAnalyses>()); 1053 } 1054 1055 void BranchProbabilityInfo::print(raw_ostream &OS) const { 1056 OS << "---- Branch Probabilities ----\n"; 1057 // We print the probabilities from the last function the analysis ran over, 1058 // or the function it is currently running over. 1059 assert(LastF && "Cannot print prior to running over a function"); 1060 for (const auto &BI : *LastF) { 1061 for (const_succ_iterator SI = succ_begin(&BI), SE = succ_end(&BI); SI != SE; 1062 ++SI) { 1063 printEdgeProbability(OS << " ", &BI, *SI); 1064 } 1065 } 1066 } 1067 1068 bool BranchProbabilityInfo:: 1069 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const { 1070 // Hot probability is at least 4/5 = 80% 1071 // FIXME: Compare against a static "hot" BranchProbability. 1072 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); 1073 } 1074 1075 const BasicBlock * 1076 BranchProbabilityInfo::getHotSucc(const BasicBlock *BB) const { 1077 auto MaxProb = BranchProbability::getZero(); 1078 const BasicBlock *MaxSucc = nullptr; 1079 1080 for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 1081 const BasicBlock *Succ = *I; 1082 auto Prob = getEdgeProbability(BB, Succ); 1083 if (Prob > MaxProb) { 1084 MaxProb = Prob; 1085 MaxSucc = Succ; 1086 } 1087 } 1088 1089 // Hot probability is at least 4/5 = 80% 1090 if (MaxProb > BranchProbability(4, 5)) 1091 return MaxSucc; 1092 1093 return nullptr; 1094 } 1095 1096 /// Get the raw edge probability for the edge. If can't find it, return a 1097 /// default probability 1/N where N is the number of successors. Here an edge is 1098 /// specified using PredBlock and an 1099 /// index to the successors. 1100 BranchProbability 1101 BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, 1102 unsigned IndexInSuccessors) const { 1103 auto I = Probs.find(std::make_pair(Src, IndexInSuccessors)); 1104 1105 if (I != Probs.end()) 1106 return I->second; 1107 1108 return {1, static_cast<uint32_t>(succ_size(Src))}; 1109 } 1110 1111 BranchProbability 1112 BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, 1113 const_succ_iterator Dst) const { 1114 return getEdgeProbability(Src, Dst.getSuccessorIndex()); 1115 } 1116 1117 /// Get the raw edge probability calculated for the block pair. This returns the 1118 /// sum of all raw edge probabilities from Src to Dst. 1119 BranchProbability 1120 BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, 1121 const BasicBlock *Dst) const { 1122 auto Prob = BranchProbability::getZero(); 1123 bool FoundProb = false; 1124 uint32_t EdgeCount = 0; 1125 for (const_succ_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I) 1126 if (*I == Dst) { 1127 ++EdgeCount; 1128 auto MapI = Probs.find(std::make_pair(Src, I.getSuccessorIndex())); 1129 if (MapI != Probs.end()) { 1130 FoundProb = true; 1131 Prob += MapI->second; 1132 } 1133 } 1134 uint32_t succ_num = std::distance(succ_begin(Src), succ_end(Src)); 1135 return FoundProb ? Prob : BranchProbability(EdgeCount, succ_num); 1136 } 1137 1138 /// Set the edge probability for a given edge specified by PredBlock and an 1139 /// index to the successors. 1140 void BranchProbabilityInfo::setEdgeProbability(const BasicBlock *Src, 1141 unsigned IndexInSuccessors, 1142 BranchProbability Prob) { 1143 Probs[std::make_pair(Src, IndexInSuccessors)] = Prob; 1144 Handles.insert(BasicBlockCallbackVH(Src, this)); 1145 LLVM_DEBUG(dbgs() << "set edge " << Src->getName() << " -> " 1146 << IndexInSuccessors << " successor probability to " << Prob 1147 << "\n"); 1148 1149 unsigned &SuccIdx = MaxSuccIdx[Src]; 1150 SuccIdx = std::max(SuccIdx, IndexInSuccessors); 1151 } 1152 1153 /// Set the edge probability for all edges at once. 1154 void BranchProbabilityInfo::setEdgeProbability( 1155 const BasicBlock *Src, const SmallVectorImpl<BranchProbability> &Probs) { 1156 assert(Src->getTerminator()->getNumSuccessors() == Probs.size()); 1157 if (Probs.size() == 0) 1158 return; // Nothing to set. 1159 1160 uint64_t TotalNumerator = 0; 1161 for (unsigned SuccIdx = 0; SuccIdx < Probs.size(); ++SuccIdx) { 1162 setEdgeProbability(Src, SuccIdx, Probs[SuccIdx]); 1163 TotalNumerator += Probs[SuccIdx].getNumerator(); 1164 } 1165 1166 // Because of rounding errors the total probability cannot be checked to be 1167 // 1.0 exactly. That is TotalNumerator == BranchProbability::getDenominator. 1168 // Instead, every single probability in Probs must be as accurate as possible. 1169 // This results in error 1/denominator at most, thus the total absolute error 1170 // should be within Probs.size / BranchProbability::getDenominator. 1171 assert(TotalNumerator <= BranchProbability::getDenominator() + Probs.size()); 1172 assert(TotalNumerator >= BranchProbability::getDenominator() - Probs.size()); 1173 } 1174 1175 raw_ostream & 1176 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, 1177 const BasicBlock *Src, 1178 const BasicBlock *Dst) const { 1179 const BranchProbability Prob = getEdgeProbability(Src, Dst); 1180 OS << "edge " << Src->getName() << " -> " << Dst->getName() 1181 << " probability is " << Prob 1182 << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); 1183 1184 return OS; 1185 } 1186 1187 void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) { 1188 // Note that we cannot use successors of BB because the terminator of BB may 1189 // have changed when eraseBlock is called as a BasicBlockCallbackVH callback. 1190 auto It = MaxSuccIdx.find(BB); 1191 if (It == MaxSuccIdx.end()) 1192 return; 1193 1194 for (unsigned I = 0, E = It->second; I <= E; ++I) { 1195 auto MapI = Probs.find(std::make_pair(BB, I)); 1196 if (MapI != Probs.end()) 1197 Probs.erase(MapI); 1198 } 1199 MaxSuccIdx.erase(BB); 1200 } 1201 1202 void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LI, 1203 const TargetLibraryInfo *TLI, 1204 PostDominatorTree *PDT) { 1205 LLVM_DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName() 1206 << " ----\n\n"); 1207 LastF = &F; // Store the last function we ran on for printing. 1208 assert(PostDominatedByUnreachable.empty()); 1209 assert(PostDominatedByColdCall.empty()); 1210 1211 SccI = std::make_unique<SccInfo>(F); 1212 1213 std::unique_ptr<PostDominatorTree> PDTPtr; 1214 1215 if (!PDT) { 1216 PDTPtr = std::make_unique<PostDominatorTree>(const_cast<Function &>(F)); 1217 PDT = PDTPtr.get(); 1218 } 1219 1220 computePostDominatedByUnreachable(F, PDT); 1221 computePostDominatedByColdCall(F, PDT); 1222 1223 // Walk the basic blocks in post-order so that we can build up state about 1224 // the successors of a block iteratively. 1225 for (auto BB : post_order(&F.getEntryBlock())) { 1226 LLVM_DEBUG(dbgs() << "Computing probabilities for " << BB->getName() 1227 << "\n"); 1228 // If there is no at least two successors, no sense to set probability. 1229 if (BB->getTerminator()->getNumSuccessors() < 2) 1230 continue; 1231 if (calcMetadataWeights(BB)) 1232 continue; 1233 if (calcInvokeHeuristics(BB)) 1234 continue; 1235 if (calcUnreachableHeuristics(BB)) 1236 continue; 1237 if (calcColdCallHeuristics(BB)) 1238 continue; 1239 if (calcLoopBranchHeuristics(BB, LI)) 1240 continue; 1241 if (calcPointerHeuristics(BB)) 1242 continue; 1243 if (calcZeroHeuristics(BB, TLI)) 1244 continue; 1245 if (calcFloatingPointHeuristics(BB)) 1246 continue; 1247 } 1248 1249 PostDominatedByUnreachable.clear(); 1250 PostDominatedByColdCall.clear(); 1251 SccI.reset(); 1252 1253 if (PrintBranchProb && 1254 (PrintBranchProbFuncName.empty() || 1255 F.getName().equals(PrintBranchProbFuncName))) { 1256 print(dbgs()); 1257 } 1258 } 1259 1260 void BranchProbabilityInfoWrapperPass::getAnalysisUsage( 1261 AnalysisUsage &AU) const { 1262 // We require DT so it's available when LI is available. The LI updating code 1263 // asserts that DT is also present so if we don't make sure that we have DT 1264 // here, that assert will trigger. 1265 AU.addRequired<DominatorTreeWrapperPass>(); 1266 AU.addRequired<LoopInfoWrapperPass>(); 1267 AU.addRequired<TargetLibraryInfoWrapperPass>(); 1268 AU.addRequired<PostDominatorTreeWrapperPass>(); 1269 AU.setPreservesAll(); 1270 } 1271 1272 bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) { 1273 const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 1274 const TargetLibraryInfo &TLI = 1275 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 1276 PostDominatorTree &PDT = 1277 getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree(); 1278 BPI.calculate(F, LI, &TLI, &PDT); 1279 return false; 1280 } 1281 1282 void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); } 1283 1284 void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS, 1285 const Module *) const { 1286 BPI.print(OS); 1287 } 1288 1289 AnalysisKey BranchProbabilityAnalysis::Key; 1290 BranchProbabilityInfo 1291 BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) { 1292 BranchProbabilityInfo BPI; 1293 BPI.calculate(F, AM.getResult<LoopAnalysis>(F), 1294 &AM.getResult<TargetLibraryAnalysis>(F), 1295 &AM.getResult<PostDominatorTreeAnalysis>(F)); 1296 return BPI; 1297 } 1298 1299 PreservedAnalyses 1300 BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { 1301 OS << "Printing analysis results of BPI for function " 1302 << "'" << F.getName() << "':" 1303 << "\n"; 1304 AM.getResult<BranchProbabilityAnalysis>(F).print(OS); 1305 return PreservedAnalyses::all(); 1306 } 1307