10b57cec5SDimitry Andric //===- BranchProbabilityInfo.cpp - Branch Probability Analysis ------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // Loops should be simplified before this analysis. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h" 140b57cec5SDimitry Andric #include "llvm/ADT/PostOrderIterator.h" 150b57cec5SDimitry Andric #include "llvm/ADT/SCCIterator.h" 160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 180b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h" 19480093f4SDimitry Andric #include "llvm/Analysis/PostDominators.h" 200b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 210b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 220b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 230b57cec5SDimitry Andric #include "llvm/IR/CFG.h" 240b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 250b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 260b57cec5SDimitry Andric #include "llvm/IR/Function.h" 270b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 280b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 290b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 300b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 310b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 320b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 330b57cec5SDimitry Andric #include "llvm/IR/Type.h" 340b57cec5SDimitry Andric #include "llvm/IR/Value.h" 35480093f4SDimitry Andric #include "llvm/InitializePasses.h" 360b57cec5SDimitry Andric #include "llvm/Pass.h" 370b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h" 380b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 39480093f4SDimitry Andric #include "llvm/Support/CommandLine.h" 400b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 410b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 420b57cec5SDimitry Andric #include <cassert> 430b57cec5SDimitry Andric #include <cstdint> 440b57cec5SDimitry Andric #include <iterator> 450b57cec5SDimitry Andric #include <utility> 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric using namespace llvm; 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric #define DEBUG_TYPE "branch-prob" 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric static cl::opt<bool> PrintBranchProb( 520b57cec5SDimitry Andric "print-bpi", cl::init(false), cl::Hidden, 530b57cec5SDimitry Andric cl::desc("Print the branch probability info.")); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric cl::opt<std::string> PrintBranchProbFuncName( 560b57cec5SDimitry Andric "print-bpi-func-name", cl::Hidden, 570b57cec5SDimitry Andric cl::desc("The option to specify the name of the function " 580b57cec5SDimitry Andric "whose branch probability info is printed.")); 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob", 610b57cec5SDimitry Andric "Branch Probability Analysis", false, true) 620b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 630b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 64*5ffd83dbSDimitry Andric INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) 650b57cec5SDimitry Andric INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass, "branch-prob", 660b57cec5SDimitry Andric "Branch Probability Analysis", false, true) 670b57cec5SDimitry Andric 68480093f4SDimitry Andric BranchProbabilityInfoWrapperPass::BranchProbabilityInfoWrapperPass() 69480093f4SDimitry Andric : FunctionPass(ID) { 70480093f4SDimitry Andric initializeBranchProbabilityInfoWrapperPassPass( 71480093f4SDimitry Andric *PassRegistry::getPassRegistry()); 72480093f4SDimitry Andric } 73480093f4SDimitry Andric 740b57cec5SDimitry Andric char BranchProbabilityInfoWrapperPass::ID = 0; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric // Weights are for internal use only. They are used by heuristics to help to 770b57cec5SDimitry Andric // estimate edges' probability. Example: 780b57cec5SDimitry Andric // 790b57cec5SDimitry Andric // Using "Loop Branch Heuristics" we predict weights of edges for the 800b57cec5SDimitry Andric // block BB2. 810b57cec5SDimitry Andric // ... 820b57cec5SDimitry Andric // | 830b57cec5SDimitry Andric // V 840b57cec5SDimitry Andric // BB1<-+ 850b57cec5SDimitry Andric // | | 860b57cec5SDimitry Andric // | | (Weight = 124) 870b57cec5SDimitry Andric // V | 880b57cec5SDimitry Andric // BB2--+ 890b57cec5SDimitry Andric // | 900b57cec5SDimitry Andric // | (Weight = 4) 910b57cec5SDimitry Andric // V 920b57cec5SDimitry Andric // BB3 930b57cec5SDimitry Andric // 940b57cec5SDimitry Andric // Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.96875 950b57cec5SDimitry Andric // Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.03125 960b57cec5SDimitry Andric static const uint32_t LBH_TAKEN_WEIGHT = 124; 970b57cec5SDimitry Andric static const uint32_t LBH_NONTAKEN_WEIGHT = 4; 980b57cec5SDimitry Andric // Unlikely edges within a loop are half as likely as other edges 990b57cec5SDimitry Andric static const uint32_t LBH_UNLIKELY_WEIGHT = 62; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric /// Unreachable-terminating branch taken probability. 1020b57cec5SDimitry Andric /// 1030b57cec5SDimitry Andric /// This is the probability for a branch being taken to a block that terminates 1040b57cec5SDimitry Andric /// (eventually) in unreachable. These are predicted as unlikely as possible. 105*5ffd83dbSDimitry Andric /// All reachable probability will proportionally share the remaining part. 1060b57cec5SDimitry Andric static const BranchProbability UR_TAKEN_PROB = BranchProbability::getRaw(1); 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric /// Weight for a branch taken going into a cold block. 1090b57cec5SDimitry Andric /// 1100b57cec5SDimitry Andric /// This is the weight for a branch taken toward a block marked 1110b57cec5SDimitry Andric /// cold. A block is marked cold if it's postdominated by a 1120b57cec5SDimitry Andric /// block containing a call to a cold function. Cold functions 1130b57cec5SDimitry Andric /// are those marked with attribute 'cold'. 1140b57cec5SDimitry Andric static const uint32_t CC_TAKEN_WEIGHT = 4; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric /// Weight for a branch not-taken into a cold block. 1170b57cec5SDimitry Andric /// 1180b57cec5SDimitry Andric /// This is the weight for a branch not taken toward a block marked 1190b57cec5SDimitry Andric /// cold. 1200b57cec5SDimitry Andric static const uint32_t CC_NONTAKEN_WEIGHT = 64; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric static const uint32_t PH_TAKEN_WEIGHT = 20; 1230b57cec5SDimitry Andric static const uint32_t PH_NONTAKEN_WEIGHT = 12; 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric static const uint32_t ZH_TAKEN_WEIGHT = 20; 1260b57cec5SDimitry Andric static const uint32_t ZH_NONTAKEN_WEIGHT = 12; 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric static const uint32_t FPH_TAKEN_WEIGHT = 20; 1290b57cec5SDimitry Andric static const uint32_t FPH_NONTAKEN_WEIGHT = 12; 1300b57cec5SDimitry Andric 1318bcb0991SDimitry Andric /// This is the probability for an ordered floating point comparison. 1328bcb0991SDimitry Andric static const uint32_t FPH_ORD_WEIGHT = 1024 * 1024 - 1; 1338bcb0991SDimitry Andric /// This is the probability for an unordered floating point comparison, it means 1348bcb0991SDimitry Andric /// one or two of the operands are NaN. Usually it is used to test for an 1358bcb0991SDimitry Andric /// exceptional case, so the result is unlikely. 1368bcb0991SDimitry Andric static const uint32_t FPH_UNO_WEIGHT = 1; 1378bcb0991SDimitry Andric 1380b57cec5SDimitry Andric /// Invoke-terminating normal branch taken weight 1390b57cec5SDimitry Andric /// 1400b57cec5SDimitry Andric /// This is the weight for branching to the normal destination of an invoke 1410b57cec5SDimitry Andric /// instruction. We expect this to happen most of the time. Set the weight to an 1420b57cec5SDimitry Andric /// absurdly high value so that nested loops subsume it. 1430b57cec5SDimitry Andric static const uint32_t IH_TAKEN_WEIGHT = 1024 * 1024 - 1; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric /// Invoke-terminating normal branch not-taken weight. 1460b57cec5SDimitry Andric /// 1470b57cec5SDimitry Andric /// This is the weight for branching to the unwind destination of an invoke 1480b57cec5SDimitry Andric /// instruction. This is essentially never taken. 1490b57cec5SDimitry Andric static const uint32_t IH_NONTAKEN_WEIGHT = 1; 1500b57cec5SDimitry Andric 151480093f4SDimitry Andric static void UpdatePDTWorklist(const BasicBlock *BB, PostDominatorTree *PDT, 152480093f4SDimitry Andric SmallVectorImpl<const BasicBlock *> &WorkList, 153480093f4SDimitry Andric SmallPtrSetImpl<const BasicBlock *> &TargetSet) { 154480093f4SDimitry Andric SmallVector<BasicBlock *, 8> Descendants; 155480093f4SDimitry Andric SmallPtrSet<const BasicBlock *, 16> NewItems; 156480093f4SDimitry Andric 157480093f4SDimitry Andric PDT->getDescendants(const_cast<BasicBlock *>(BB), Descendants); 158480093f4SDimitry Andric for (auto *BB : Descendants) 159480093f4SDimitry Andric if (TargetSet.insert(BB).second) 160480093f4SDimitry Andric for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 161480093f4SDimitry Andric if (!TargetSet.count(*PI)) 162480093f4SDimitry Andric NewItems.insert(*PI); 163480093f4SDimitry Andric WorkList.insert(WorkList.end(), NewItems.begin(), NewItems.end()); 164480093f4SDimitry Andric } 165480093f4SDimitry Andric 166480093f4SDimitry Andric /// Compute a set of basic blocks that are post-dominated by unreachables. 167480093f4SDimitry Andric void BranchProbabilityInfo::computePostDominatedByUnreachable( 168480093f4SDimitry Andric const Function &F, PostDominatorTree *PDT) { 169480093f4SDimitry Andric SmallVector<const BasicBlock *, 8> WorkList; 170480093f4SDimitry Andric for (auto &BB : F) { 171480093f4SDimitry Andric const Instruction *TI = BB.getTerminator(); 1720b57cec5SDimitry Andric if (TI->getNumSuccessors() == 0) { 1730b57cec5SDimitry Andric if (isa<UnreachableInst>(TI) || 1740b57cec5SDimitry Andric // If this block is terminated by a call to 175480093f4SDimitry Andric // @llvm.experimental.deoptimize then treat it like an unreachable 176480093f4SDimitry Andric // since the @llvm.experimental.deoptimize call is expected to 177480093f4SDimitry Andric // practically never execute. 178480093f4SDimitry Andric BB.getTerminatingDeoptimizeCall()) 179480093f4SDimitry Andric UpdatePDTWorklist(&BB, PDT, WorkList, PostDominatedByUnreachable); 180480093f4SDimitry Andric } 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 183480093f4SDimitry Andric while (!WorkList.empty()) { 184480093f4SDimitry Andric const BasicBlock *BB = WorkList.pop_back_val(); 185480093f4SDimitry Andric if (PostDominatedByUnreachable.count(BB)) 186480093f4SDimitry Andric continue; 187480093f4SDimitry Andric // If the terminator is an InvokeInst, check only the normal destination 188480093f4SDimitry Andric // block as the unwind edge of InvokeInst is also very unlikely taken. 189480093f4SDimitry Andric if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) { 1900b57cec5SDimitry Andric if (PostDominatedByUnreachable.count(II->getNormalDest())) 191480093f4SDimitry Andric UpdatePDTWorklist(BB, PDT, WorkList, PostDominatedByUnreachable); 192480093f4SDimitry Andric } 193480093f4SDimitry Andric // If all the successors are unreachable, BB is unreachable as well. 194480093f4SDimitry Andric else if (!successors(BB).empty() && 195480093f4SDimitry Andric llvm::all_of(successors(BB), [this](const BasicBlock *Succ) { 196480093f4SDimitry Andric return PostDominatedByUnreachable.count(Succ); 197480093f4SDimitry Andric })) 198480093f4SDimitry Andric UpdatePDTWorklist(BB, PDT, WorkList, PostDominatedByUnreachable); 199480093f4SDimitry Andric } 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 202480093f4SDimitry Andric /// compute a set of basic blocks that are post-dominated by ColdCalls. 203480093f4SDimitry Andric void BranchProbabilityInfo::computePostDominatedByColdCall( 204480093f4SDimitry Andric const Function &F, PostDominatorTree *PDT) { 205480093f4SDimitry Andric SmallVector<const BasicBlock *, 8> WorkList; 206480093f4SDimitry Andric for (auto &BB : F) 207480093f4SDimitry Andric for (auto &I : BB) 208480093f4SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(&I)) 209480093f4SDimitry Andric if (CI->hasFnAttr(Attribute::Cold)) 210480093f4SDimitry Andric UpdatePDTWorklist(&BB, PDT, WorkList, PostDominatedByColdCall); 2110b57cec5SDimitry Andric 212480093f4SDimitry Andric while (!WorkList.empty()) { 213480093f4SDimitry Andric const BasicBlock *BB = WorkList.pop_back_val(); 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric // If the terminator is an InvokeInst, check only the normal destination 2160b57cec5SDimitry Andric // block as the unwind edge of InvokeInst is also very unlikely taken. 217480093f4SDimitry Andric if (auto *II = dyn_cast<InvokeInst>(BB->getTerminator())) { 218480093f4SDimitry Andric if (PostDominatedByColdCall.count(II->getNormalDest())) 219480093f4SDimitry Andric UpdatePDTWorklist(BB, PDT, WorkList, PostDominatedByColdCall); 2200b57cec5SDimitry Andric } 221480093f4SDimitry Andric // If all of successor are post dominated then BB is also done. 222480093f4SDimitry Andric else if (!successors(BB).empty() && 223480093f4SDimitry Andric llvm::all_of(successors(BB), [this](const BasicBlock *Succ) { 224480093f4SDimitry Andric return PostDominatedByColdCall.count(Succ); 225480093f4SDimitry Andric })) 226480093f4SDimitry Andric UpdatePDTWorklist(BB, PDT, WorkList, PostDominatedByColdCall); 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric /// Calculate edge weights for successors lead to unreachable. 2310b57cec5SDimitry Andric /// 2320b57cec5SDimitry Andric /// Predict that a successor which leads necessarily to an 2330b57cec5SDimitry Andric /// unreachable-terminated block as extremely unlikely. 2340b57cec5SDimitry Andric bool BranchProbabilityInfo::calcUnreachableHeuristics(const BasicBlock *BB) { 2350b57cec5SDimitry Andric const Instruction *TI = BB->getTerminator(); 2360b57cec5SDimitry Andric (void) TI; 2370b57cec5SDimitry Andric assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); 2380b57cec5SDimitry Andric assert(!isa<InvokeInst>(TI) && 2390b57cec5SDimitry Andric "Invokes should have already been handled by calcInvokeHeuristics"); 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric SmallVector<unsigned, 4> UnreachableEdges; 2420b57cec5SDimitry Andric SmallVector<unsigned, 4> ReachableEdges; 2430b57cec5SDimitry Andric 244*5ffd83dbSDimitry Andric for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) 2450b57cec5SDimitry Andric if (PostDominatedByUnreachable.count(*I)) 2460b57cec5SDimitry Andric UnreachableEdges.push_back(I.getSuccessorIndex()); 2470b57cec5SDimitry Andric else 2480b57cec5SDimitry Andric ReachableEdges.push_back(I.getSuccessorIndex()); 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric // Skip probabilities if all were reachable. 2510b57cec5SDimitry Andric if (UnreachableEdges.empty()) 2520b57cec5SDimitry Andric return false; 2530b57cec5SDimitry Andric 254*5ffd83dbSDimitry Andric SmallVector<BranchProbability, 4> EdgeProbabilities( 255*5ffd83dbSDimitry Andric BB->getTerminator()->getNumSuccessors(), BranchProbability::getUnknown()); 2560b57cec5SDimitry Andric if (ReachableEdges.empty()) { 2570b57cec5SDimitry Andric BranchProbability Prob(1, UnreachableEdges.size()); 2580b57cec5SDimitry Andric for (unsigned SuccIdx : UnreachableEdges) 259*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = Prob; 260*5ffd83dbSDimitry Andric setEdgeProbability(BB, EdgeProbabilities); 2610b57cec5SDimitry Andric return true; 2620b57cec5SDimitry Andric } 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric auto UnreachableProb = UR_TAKEN_PROB; 2650b57cec5SDimitry Andric auto ReachableProb = 2660b57cec5SDimitry Andric (BranchProbability::getOne() - UR_TAKEN_PROB * UnreachableEdges.size()) / 2670b57cec5SDimitry Andric ReachableEdges.size(); 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric for (unsigned SuccIdx : UnreachableEdges) 270*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = UnreachableProb; 2710b57cec5SDimitry Andric for (unsigned SuccIdx : ReachableEdges) 272*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = ReachableProb; 2730b57cec5SDimitry Andric 274*5ffd83dbSDimitry Andric setEdgeProbability(BB, EdgeProbabilities); 2750b57cec5SDimitry Andric return true; 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric // Propagate existing explicit probabilities from either profile data or 2790b57cec5SDimitry Andric // 'expect' intrinsic processing. Examine metadata against unreachable 2800b57cec5SDimitry Andric // heuristic. The probability of the edge coming to unreachable block is 2810b57cec5SDimitry Andric // set to min of metadata and unreachable heuristic. 2820b57cec5SDimitry Andric bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) { 2830b57cec5SDimitry Andric const Instruction *TI = BB->getTerminator(); 2840b57cec5SDimitry Andric assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); 285*5ffd83dbSDimitry Andric if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI) || 286*5ffd83dbSDimitry Andric isa<InvokeInst>(TI))) 2870b57cec5SDimitry Andric return false; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); 2900b57cec5SDimitry Andric if (!WeightsNode) 2910b57cec5SDimitry Andric return false; 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric // Check that the number of successors is manageable. 2940b57cec5SDimitry Andric assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors"); 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // Ensure there are weights for all of the successors. Note that the first 2970b57cec5SDimitry Andric // operand to the metadata node is a name, not a weight. 2980b57cec5SDimitry Andric if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1) 2990b57cec5SDimitry Andric return false; 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric // Build up the final weights that will be used in a temporary buffer. 3020b57cec5SDimitry Andric // Compute the sum of all weights to later decide whether they need to 3030b57cec5SDimitry Andric // be scaled to fit in 32 bits. 3040b57cec5SDimitry Andric uint64_t WeightSum = 0; 3050b57cec5SDimitry Andric SmallVector<uint32_t, 2> Weights; 3060b57cec5SDimitry Andric SmallVector<unsigned, 2> UnreachableIdxs; 3070b57cec5SDimitry Andric SmallVector<unsigned, 2> ReachableIdxs; 3080b57cec5SDimitry Andric Weights.reserve(TI->getNumSuccessors()); 309*5ffd83dbSDimitry Andric for (unsigned I = 1, E = WeightsNode->getNumOperands(); I != E; ++I) { 3100b57cec5SDimitry Andric ConstantInt *Weight = 311*5ffd83dbSDimitry Andric mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(I)); 3120b57cec5SDimitry Andric if (!Weight) 3130b57cec5SDimitry Andric return false; 3140b57cec5SDimitry Andric assert(Weight->getValue().getActiveBits() <= 32 && 3150b57cec5SDimitry Andric "Too many bits for uint32_t"); 3160b57cec5SDimitry Andric Weights.push_back(Weight->getZExtValue()); 3170b57cec5SDimitry Andric WeightSum += Weights.back(); 318*5ffd83dbSDimitry Andric if (PostDominatedByUnreachable.count(TI->getSuccessor(I - 1))) 319*5ffd83dbSDimitry Andric UnreachableIdxs.push_back(I - 1); 3200b57cec5SDimitry Andric else 321*5ffd83dbSDimitry Andric ReachableIdxs.push_back(I - 1); 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric assert(Weights.size() == TI->getNumSuccessors() && "Checked above"); 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric // If the sum of weights does not fit in 32 bits, scale every weight down 3260b57cec5SDimitry Andric // accordingly. 3270b57cec5SDimitry Andric uint64_t ScalingFactor = 3280b57cec5SDimitry Andric (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1; 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric if (ScalingFactor > 1) { 3310b57cec5SDimitry Andric WeightSum = 0; 332*5ffd83dbSDimitry Andric for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) { 333*5ffd83dbSDimitry Andric Weights[I] /= ScalingFactor; 334*5ffd83dbSDimitry Andric WeightSum += Weights[I]; 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric assert(WeightSum <= UINT32_MAX && 3380b57cec5SDimitry Andric "Expected weights to scale down to 32 bits"); 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric if (WeightSum == 0 || ReachableIdxs.size() == 0) { 341*5ffd83dbSDimitry Andric for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) 342*5ffd83dbSDimitry Andric Weights[I] = 1; 3430b57cec5SDimitry Andric WeightSum = TI->getNumSuccessors(); 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric // Set the probability. 3470b57cec5SDimitry Andric SmallVector<BranchProbability, 2> BP; 348*5ffd83dbSDimitry Andric for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) 349*5ffd83dbSDimitry Andric BP.push_back({ Weights[I], static_cast<uint32_t>(WeightSum) }); 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric // Examine the metadata against unreachable heuristic. 3520b57cec5SDimitry Andric // If the unreachable heuristic is more strong then we use it for this edge. 353*5ffd83dbSDimitry Andric if (UnreachableIdxs.size() == 0 || ReachableIdxs.size() == 0) { 354*5ffd83dbSDimitry Andric setEdgeProbability(BB, BP); 355*5ffd83dbSDimitry Andric return true; 356*5ffd83dbSDimitry Andric } 357*5ffd83dbSDimitry Andric 3580b57cec5SDimitry Andric auto UnreachableProb = UR_TAKEN_PROB; 359*5ffd83dbSDimitry Andric for (auto I : UnreachableIdxs) 360*5ffd83dbSDimitry Andric if (UnreachableProb < BP[I]) { 361*5ffd83dbSDimitry Andric BP[I] = UnreachableProb; 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric 364*5ffd83dbSDimitry Andric // Sum of all edge probabilities must be 1.0. If we modified the probability 365*5ffd83dbSDimitry Andric // of some edges then we must distribute the introduced difference over the 366*5ffd83dbSDimitry Andric // reachable blocks. 367*5ffd83dbSDimitry Andric // 368*5ffd83dbSDimitry Andric // Proportional distribution: the relation between probabilities of the 369*5ffd83dbSDimitry Andric // reachable edges is kept unchanged. That is for any reachable edges i and j: 370*5ffd83dbSDimitry Andric // newBP[i] / newBP[j] == oldBP[i] / oldBP[j] => 371*5ffd83dbSDimitry Andric // newBP[i] / oldBP[i] == newBP[j] / oldBP[j] == K 372*5ffd83dbSDimitry Andric // Where K is independent of i,j. 373*5ffd83dbSDimitry Andric // newBP[i] == oldBP[i] * K 374*5ffd83dbSDimitry Andric // We need to find K. 375*5ffd83dbSDimitry Andric // Make sum of all reachables of the left and right parts: 376*5ffd83dbSDimitry Andric // sum_of_reachable(newBP) == K * sum_of_reachable(oldBP) 377*5ffd83dbSDimitry Andric // Sum of newBP must be equal to 1.0: 378*5ffd83dbSDimitry Andric // sum_of_reachable(newBP) + sum_of_unreachable(newBP) == 1.0 => 379*5ffd83dbSDimitry Andric // sum_of_reachable(newBP) = 1.0 - sum_of_unreachable(newBP) 380*5ffd83dbSDimitry Andric // Where sum_of_unreachable(newBP) is what has been just changed. 381*5ffd83dbSDimitry Andric // Finally: 382*5ffd83dbSDimitry Andric // K == sum_of_reachable(newBP) / sum_of_reachable(oldBP) => 383*5ffd83dbSDimitry Andric // K == (1.0 - sum_of_unreachable(newBP)) / sum_of_reachable(oldBP) 384*5ffd83dbSDimitry Andric BranchProbability NewUnreachableSum = BranchProbability::getZero(); 385*5ffd83dbSDimitry Andric for (auto I : UnreachableIdxs) 386*5ffd83dbSDimitry Andric NewUnreachableSum += BP[I]; 387*5ffd83dbSDimitry Andric 388*5ffd83dbSDimitry Andric BranchProbability NewReachableSum = 389*5ffd83dbSDimitry Andric BranchProbability::getOne() - NewUnreachableSum; 390*5ffd83dbSDimitry Andric 391*5ffd83dbSDimitry Andric BranchProbability OldReachableSum = BranchProbability::getZero(); 392*5ffd83dbSDimitry Andric for (auto I : ReachableIdxs) 393*5ffd83dbSDimitry Andric OldReachableSum += BP[I]; 394*5ffd83dbSDimitry Andric 395*5ffd83dbSDimitry Andric if (OldReachableSum != NewReachableSum) { // Anything to dsitribute? 396*5ffd83dbSDimitry Andric if (OldReachableSum.isZero()) { 397*5ffd83dbSDimitry Andric // If all oldBP[i] are zeroes then the proportional distribution results 398*5ffd83dbSDimitry Andric // in all zero probabilities and the error stays big. In this case we 399*5ffd83dbSDimitry Andric // evenly spread NewReachableSum over the reachable edges. 400*5ffd83dbSDimitry Andric BranchProbability PerEdge = NewReachableSum / ReachableIdxs.size(); 401*5ffd83dbSDimitry Andric for (auto I : ReachableIdxs) 402*5ffd83dbSDimitry Andric BP[I] = PerEdge; 403*5ffd83dbSDimitry Andric } else { 404*5ffd83dbSDimitry Andric for (auto I : ReachableIdxs) { 405*5ffd83dbSDimitry Andric // We use uint64_t to avoid double rounding error of the following 406*5ffd83dbSDimitry Andric // calculation: BP[i] = BP[i] * NewReachableSum / OldReachableSum 407*5ffd83dbSDimitry Andric // The formula is taken from the private constructor 408*5ffd83dbSDimitry Andric // BranchProbability(uint32_t Numerator, uint32_t Denominator) 409*5ffd83dbSDimitry Andric uint64_t Mul = static_cast<uint64_t>(NewReachableSum.getNumerator()) * 410*5ffd83dbSDimitry Andric BP[I].getNumerator(); 411*5ffd83dbSDimitry Andric uint32_t Div = static_cast<uint32_t>( 412*5ffd83dbSDimitry Andric divideNearest(Mul, OldReachableSum.getNumerator())); 413*5ffd83dbSDimitry Andric BP[I] = BranchProbability::getRaw(Div); 414*5ffd83dbSDimitry Andric } 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 418*5ffd83dbSDimitry Andric setEdgeProbability(BB, BP); 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric return true; 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric /// Calculate edge weights for edges leading to cold blocks. 4240b57cec5SDimitry Andric /// 4250b57cec5SDimitry Andric /// A cold block is one post-dominated by a block with a call to a 4260b57cec5SDimitry Andric /// cold function. Those edges are unlikely to be taken, so we give 4270b57cec5SDimitry Andric /// them relatively low weight. 4280b57cec5SDimitry Andric /// 4290b57cec5SDimitry Andric /// Return true if we could compute the weights for cold edges. 4300b57cec5SDimitry Andric /// Return false, otherwise. 4310b57cec5SDimitry Andric bool BranchProbabilityInfo::calcColdCallHeuristics(const BasicBlock *BB) { 4320b57cec5SDimitry Andric const Instruction *TI = BB->getTerminator(); 4330b57cec5SDimitry Andric (void) TI; 4340b57cec5SDimitry Andric assert(TI->getNumSuccessors() > 1 && "expected more than one successor!"); 4350b57cec5SDimitry Andric assert(!isa<InvokeInst>(TI) && 4360b57cec5SDimitry Andric "Invokes should have already been handled by calcInvokeHeuristics"); 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric // Determine which successors are post-dominated by a cold block. 4390b57cec5SDimitry Andric SmallVector<unsigned, 4> ColdEdges; 4400b57cec5SDimitry Andric SmallVector<unsigned, 4> NormalEdges; 441*5ffd83dbSDimitry Andric for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) 4420b57cec5SDimitry Andric if (PostDominatedByColdCall.count(*I)) 4430b57cec5SDimitry Andric ColdEdges.push_back(I.getSuccessorIndex()); 4440b57cec5SDimitry Andric else 4450b57cec5SDimitry Andric NormalEdges.push_back(I.getSuccessorIndex()); 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric // Skip probabilities if no cold edges. 4480b57cec5SDimitry Andric if (ColdEdges.empty()) 4490b57cec5SDimitry Andric return false; 4500b57cec5SDimitry Andric 451*5ffd83dbSDimitry Andric SmallVector<BranchProbability, 4> EdgeProbabilities( 452*5ffd83dbSDimitry Andric BB->getTerminator()->getNumSuccessors(), BranchProbability::getUnknown()); 4530b57cec5SDimitry Andric if (NormalEdges.empty()) { 4540b57cec5SDimitry Andric BranchProbability Prob(1, ColdEdges.size()); 4550b57cec5SDimitry Andric for (unsigned SuccIdx : ColdEdges) 456*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = Prob; 457*5ffd83dbSDimitry Andric setEdgeProbability(BB, EdgeProbabilities); 4580b57cec5SDimitry Andric return true; 4590b57cec5SDimitry Andric } 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric auto ColdProb = BranchProbability::getBranchProbability( 4620b57cec5SDimitry Andric CC_TAKEN_WEIGHT, 4630b57cec5SDimitry Andric (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(ColdEdges.size())); 4640b57cec5SDimitry Andric auto NormalProb = BranchProbability::getBranchProbability( 4650b57cec5SDimitry Andric CC_NONTAKEN_WEIGHT, 4660b57cec5SDimitry Andric (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * uint64_t(NormalEdges.size())); 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric for (unsigned SuccIdx : ColdEdges) 469*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = ColdProb; 4700b57cec5SDimitry Andric for (unsigned SuccIdx : NormalEdges) 471*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = NormalProb; 4720b57cec5SDimitry Andric 473*5ffd83dbSDimitry Andric setEdgeProbability(BB, EdgeProbabilities); 4740b57cec5SDimitry Andric return true; 4750b57cec5SDimitry Andric } 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric // Calculate Edge Weights using "Pointer Heuristics". Predict a comparison 4780b57cec5SDimitry Andric // between two pointer or pointer and NULL will fail. 4790b57cec5SDimitry Andric bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) { 4800b57cec5SDimitry Andric const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 4810b57cec5SDimitry Andric if (!BI || !BI->isConditional()) 4820b57cec5SDimitry Andric return false; 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric Value *Cond = BI->getCondition(); 4850b57cec5SDimitry Andric ICmpInst *CI = dyn_cast<ICmpInst>(Cond); 4860b57cec5SDimitry Andric if (!CI || !CI->isEquality()) 4870b57cec5SDimitry Andric return false; 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric Value *LHS = CI->getOperand(0); 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric if (!LHS->getType()->isPointerTy()) 4920b57cec5SDimitry Andric return false; 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric assert(CI->getOperand(1)->getType()->isPointerTy()); 4950b57cec5SDimitry Andric 496*5ffd83dbSDimitry Andric BranchProbability TakenProb(PH_TAKEN_WEIGHT, 497*5ffd83dbSDimitry Andric PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT); 498*5ffd83dbSDimitry Andric BranchProbability UntakenProb(PH_NONTAKEN_WEIGHT, 499*5ffd83dbSDimitry Andric PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT); 500*5ffd83dbSDimitry Andric 5010b57cec5SDimitry Andric // p != 0 -> isProb = true 5020b57cec5SDimitry Andric // p == 0 -> isProb = false 5030b57cec5SDimitry Andric // p != q -> isProb = true 5040b57cec5SDimitry Andric // p == q -> isProb = false; 5050b57cec5SDimitry Andric bool isProb = CI->getPredicate() == ICmpInst::ICMP_NE; 5060b57cec5SDimitry Andric if (!isProb) 507*5ffd83dbSDimitry Andric std::swap(TakenProb, UntakenProb); 5080b57cec5SDimitry Andric 509*5ffd83dbSDimitry Andric setEdgeProbability( 510*5ffd83dbSDimitry Andric BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb})); 5110b57cec5SDimitry Andric return true; 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric static int getSCCNum(const BasicBlock *BB, 5150b57cec5SDimitry Andric const BranchProbabilityInfo::SccInfo &SccI) { 5160b57cec5SDimitry Andric auto SccIt = SccI.SccNums.find(BB); 5170b57cec5SDimitry Andric if (SccIt == SccI.SccNums.end()) 5180b57cec5SDimitry Andric return -1; 5190b57cec5SDimitry Andric return SccIt->second; 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric // Consider any block that is an entry point to the SCC as a header. 5230b57cec5SDimitry Andric static bool isSCCHeader(const BasicBlock *BB, int SccNum, 5240b57cec5SDimitry Andric BranchProbabilityInfo::SccInfo &SccI) { 5250b57cec5SDimitry Andric assert(getSCCNum(BB, SccI) == SccNum); 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric // Lazily compute the set of headers for a given SCC and cache the results 5280b57cec5SDimitry Andric // in the SccHeaderMap. 5290b57cec5SDimitry Andric if (SccI.SccHeaders.size() <= static_cast<unsigned>(SccNum)) 5300b57cec5SDimitry Andric SccI.SccHeaders.resize(SccNum + 1); 5310b57cec5SDimitry Andric auto &HeaderMap = SccI.SccHeaders[SccNum]; 5320b57cec5SDimitry Andric bool Inserted; 5330b57cec5SDimitry Andric BranchProbabilityInfo::SccHeaderMap::iterator HeaderMapIt; 5340b57cec5SDimitry Andric std::tie(HeaderMapIt, Inserted) = HeaderMap.insert(std::make_pair(BB, false)); 5350b57cec5SDimitry Andric if (Inserted) { 5360b57cec5SDimitry Andric bool IsHeader = llvm::any_of(make_range(pred_begin(BB), pred_end(BB)), 5370b57cec5SDimitry Andric [&](const BasicBlock *Pred) { 5380b57cec5SDimitry Andric return getSCCNum(Pred, SccI) != SccNum; 5390b57cec5SDimitry Andric }); 5400b57cec5SDimitry Andric HeaderMapIt->second = IsHeader; 5410b57cec5SDimitry Andric return IsHeader; 5420b57cec5SDimitry Andric } else 5430b57cec5SDimitry Andric return HeaderMapIt->second; 5440b57cec5SDimitry Andric } 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andric // Compute the unlikely successors to the block BB in the loop L, specifically 5470b57cec5SDimitry Andric // those that are unlikely because this is a loop, and add them to the 5480b57cec5SDimitry Andric // UnlikelyBlocks set. 5490b57cec5SDimitry Andric static void 5500b57cec5SDimitry Andric computeUnlikelySuccessors(const BasicBlock *BB, Loop *L, 5510b57cec5SDimitry Andric SmallPtrSetImpl<const BasicBlock*> &UnlikelyBlocks) { 5520b57cec5SDimitry Andric // Sometimes in a loop we have a branch whose condition is made false by 5530b57cec5SDimitry Andric // taking it. This is typically something like 5540b57cec5SDimitry Andric // int n = 0; 5550b57cec5SDimitry Andric // while (...) { 5560b57cec5SDimitry Andric // if (++n >= MAX) { 5570b57cec5SDimitry Andric // n = 0; 5580b57cec5SDimitry Andric // } 5590b57cec5SDimitry Andric // } 5600b57cec5SDimitry Andric // In this sort of situation taking the branch means that at the very least it 5610b57cec5SDimitry Andric // won't be taken again in the next iteration of the loop, so we should 5620b57cec5SDimitry Andric // consider it less likely than a typical branch. 5630b57cec5SDimitry Andric // 5640b57cec5SDimitry Andric // We detect this by looking back through the graph of PHI nodes that sets the 5650b57cec5SDimitry Andric // value that the condition depends on, and seeing if we can reach a successor 5660b57cec5SDimitry Andric // block which can be determined to make the condition false. 5670b57cec5SDimitry Andric // 5680b57cec5SDimitry Andric // FIXME: We currently consider unlikely blocks to be half as likely as other 5690b57cec5SDimitry Andric // blocks, but if we consider the example above the likelyhood is actually 5700b57cec5SDimitry Andric // 1/MAX. We could therefore be more precise in how unlikely we consider 5710b57cec5SDimitry Andric // blocks to be, but it would require more careful examination of the form 5720b57cec5SDimitry Andric // of the comparison expression. 5730b57cec5SDimitry Andric const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 5740b57cec5SDimitry Andric if (!BI || !BI->isConditional()) 5750b57cec5SDimitry Andric return; 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric // Check if the branch is based on an instruction compared with a constant 5780b57cec5SDimitry Andric CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition()); 5790b57cec5SDimitry Andric if (!CI || !isa<Instruction>(CI->getOperand(0)) || 5800b57cec5SDimitry Andric !isa<Constant>(CI->getOperand(1))) 5810b57cec5SDimitry Andric return; 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric // Either the instruction must be a PHI, or a chain of operations involving 5840b57cec5SDimitry Andric // constants that ends in a PHI which we can then collapse into a single value 5850b57cec5SDimitry Andric // if the PHI value is known. 5860b57cec5SDimitry Andric Instruction *CmpLHS = dyn_cast<Instruction>(CI->getOperand(0)); 5870b57cec5SDimitry Andric PHINode *CmpPHI = dyn_cast<PHINode>(CmpLHS); 5880b57cec5SDimitry Andric Constant *CmpConst = dyn_cast<Constant>(CI->getOperand(1)); 5890b57cec5SDimitry Andric // Collect the instructions until we hit a PHI 5900b57cec5SDimitry Andric SmallVector<BinaryOperator *, 1> InstChain; 5910b57cec5SDimitry Andric while (!CmpPHI && CmpLHS && isa<BinaryOperator>(CmpLHS) && 5920b57cec5SDimitry Andric isa<Constant>(CmpLHS->getOperand(1))) { 5930b57cec5SDimitry Andric // Stop if the chain extends outside of the loop 5940b57cec5SDimitry Andric if (!L->contains(CmpLHS)) 5950b57cec5SDimitry Andric return; 5960b57cec5SDimitry Andric InstChain.push_back(cast<BinaryOperator>(CmpLHS)); 5970b57cec5SDimitry Andric CmpLHS = dyn_cast<Instruction>(CmpLHS->getOperand(0)); 5980b57cec5SDimitry Andric if (CmpLHS) 5990b57cec5SDimitry Andric CmpPHI = dyn_cast<PHINode>(CmpLHS); 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric if (!CmpPHI || !L->contains(CmpPHI)) 6020b57cec5SDimitry Andric return; 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric // Trace the phi node to find all values that come from successors of BB 6050b57cec5SDimitry Andric SmallPtrSet<PHINode*, 8> VisitedInsts; 6060b57cec5SDimitry Andric SmallVector<PHINode*, 8> WorkList; 6070b57cec5SDimitry Andric WorkList.push_back(CmpPHI); 6080b57cec5SDimitry Andric VisitedInsts.insert(CmpPHI); 6090b57cec5SDimitry Andric while (!WorkList.empty()) { 6100b57cec5SDimitry Andric PHINode *P = WorkList.back(); 6110b57cec5SDimitry Andric WorkList.pop_back(); 6120b57cec5SDimitry Andric for (BasicBlock *B : P->blocks()) { 6130b57cec5SDimitry Andric // Skip blocks that aren't part of the loop 6140b57cec5SDimitry Andric if (!L->contains(B)) 6150b57cec5SDimitry Andric continue; 6160b57cec5SDimitry Andric Value *V = P->getIncomingValueForBlock(B); 6170b57cec5SDimitry Andric // If the source is a PHI add it to the work list if we haven't 6180b57cec5SDimitry Andric // already visited it. 6190b57cec5SDimitry Andric if (PHINode *PN = dyn_cast<PHINode>(V)) { 6200b57cec5SDimitry Andric if (VisitedInsts.insert(PN).second) 6210b57cec5SDimitry Andric WorkList.push_back(PN); 6220b57cec5SDimitry Andric continue; 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric // If this incoming value is a constant and B is a successor of BB, then 6250b57cec5SDimitry Andric // we can constant-evaluate the compare to see if it makes the branch be 6260b57cec5SDimitry Andric // taken or not. 6270b57cec5SDimitry Andric Constant *CmpLHSConst = dyn_cast<Constant>(V); 6280b57cec5SDimitry Andric if (!CmpLHSConst || 6290b57cec5SDimitry Andric std::find(succ_begin(BB), succ_end(BB), B) == succ_end(BB)) 6300b57cec5SDimitry Andric continue; 6310b57cec5SDimitry Andric // First collapse InstChain 6320b57cec5SDimitry Andric for (Instruction *I : llvm::reverse(InstChain)) { 6330b57cec5SDimitry Andric CmpLHSConst = ConstantExpr::get(I->getOpcode(), CmpLHSConst, 6340b57cec5SDimitry Andric cast<Constant>(I->getOperand(1)), true); 6350b57cec5SDimitry Andric if (!CmpLHSConst) 6360b57cec5SDimitry Andric break; 6370b57cec5SDimitry Andric } 6380b57cec5SDimitry Andric if (!CmpLHSConst) 6390b57cec5SDimitry Andric continue; 6400b57cec5SDimitry Andric // Now constant-evaluate the compare 6410b57cec5SDimitry Andric Constant *Result = ConstantExpr::getCompare(CI->getPredicate(), 6420b57cec5SDimitry Andric CmpLHSConst, CmpConst, true); 6430b57cec5SDimitry Andric // If the result means we don't branch to the block then that block is 6440b57cec5SDimitry Andric // unlikely. 6450b57cec5SDimitry Andric if (Result && 6460b57cec5SDimitry Andric ((Result->isZeroValue() && B == BI->getSuccessor(0)) || 6470b57cec5SDimitry Andric (Result->isOneValue() && B == BI->getSuccessor(1)))) 6480b57cec5SDimitry Andric UnlikelyBlocks.insert(B); 6490b57cec5SDimitry Andric } 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric } 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric // Calculate Edge Weights using "Loop Branch Heuristics". Predict backedges 6540b57cec5SDimitry Andric // as taken, exiting edges as not-taken. 6550b57cec5SDimitry Andric bool BranchProbabilityInfo::calcLoopBranchHeuristics(const BasicBlock *BB, 6560b57cec5SDimitry Andric const LoopInfo &LI, 6570b57cec5SDimitry Andric SccInfo &SccI) { 6580b57cec5SDimitry Andric int SccNum; 6590b57cec5SDimitry Andric Loop *L = LI.getLoopFor(BB); 6600b57cec5SDimitry Andric if (!L) { 6610b57cec5SDimitry Andric SccNum = getSCCNum(BB, SccI); 6620b57cec5SDimitry Andric if (SccNum < 0) 6630b57cec5SDimitry Andric return false; 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric SmallPtrSet<const BasicBlock*, 8> UnlikelyBlocks; 6670b57cec5SDimitry Andric if (L) 6680b57cec5SDimitry Andric computeUnlikelySuccessors(BB, L, UnlikelyBlocks); 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric SmallVector<unsigned, 8> BackEdges; 6710b57cec5SDimitry Andric SmallVector<unsigned, 8> ExitingEdges; 6720b57cec5SDimitry Andric SmallVector<unsigned, 8> InEdges; // Edges from header to the loop. 6730b57cec5SDimitry Andric SmallVector<unsigned, 8> UnlikelyEdges; 6740b57cec5SDimitry Andric 675*5ffd83dbSDimitry Andric for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 6760b57cec5SDimitry Andric // Use LoopInfo if we have it, otherwise fall-back to SCC info to catch 6770b57cec5SDimitry Andric // irreducible loops. 6780b57cec5SDimitry Andric if (L) { 6790b57cec5SDimitry Andric if (UnlikelyBlocks.count(*I) != 0) 6800b57cec5SDimitry Andric UnlikelyEdges.push_back(I.getSuccessorIndex()); 6810b57cec5SDimitry Andric else if (!L->contains(*I)) 6820b57cec5SDimitry Andric ExitingEdges.push_back(I.getSuccessorIndex()); 6830b57cec5SDimitry Andric else if (L->getHeader() == *I) 6840b57cec5SDimitry Andric BackEdges.push_back(I.getSuccessorIndex()); 6850b57cec5SDimitry Andric else 6860b57cec5SDimitry Andric InEdges.push_back(I.getSuccessorIndex()); 6870b57cec5SDimitry Andric } else { 6880b57cec5SDimitry Andric if (getSCCNum(*I, SccI) != SccNum) 6890b57cec5SDimitry Andric ExitingEdges.push_back(I.getSuccessorIndex()); 6900b57cec5SDimitry Andric else if (isSCCHeader(*I, SccNum, SccI)) 6910b57cec5SDimitry Andric BackEdges.push_back(I.getSuccessorIndex()); 6920b57cec5SDimitry Andric else 6930b57cec5SDimitry Andric InEdges.push_back(I.getSuccessorIndex()); 6940b57cec5SDimitry Andric } 6950b57cec5SDimitry Andric } 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric if (BackEdges.empty() && ExitingEdges.empty() && UnlikelyEdges.empty()) 6980b57cec5SDimitry Andric return false; 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric // Collect the sum of probabilities of back-edges/in-edges/exiting-edges, and 7010b57cec5SDimitry Andric // normalize them so that they sum up to one. 7020b57cec5SDimitry Andric unsigned Denom = (BackEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) + 7030b57cec5SDimitry Andric (InEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) + 7040b57cec5SDimitry Andric (UnlikelyEdges.empty() ? 0 : LBH_UNLIKELY_WEIGHT) + 7050b57cec5SDimitry Andric (ExitingEdges.empty() ? 0 : LBH_NONTAKEN_WEIGHT); 7060b57cec5SDimitry Andric 707*5ffd83dbSDimitry Andric SmallVector<BranchProbability, 4> EdgeProbabilities( 708*5ffd83dbSDimitry Andric BB->getTerminator()->getNumSuccessors(), BranchProbability::getUnknown()); 7090b57cec5SDimitry Andric if (uint32_t numBackEdges = BackEdges.size()) { 7100b57cec5SDimitry Andric BranchProbability TakenProb = BranchProbability(LBH_TAKEN_WEIGHT, Denom); 7110b57cec5SDimitry Andric auto Prob = TakenProb / numBackEdges; 7120b57cec5SDimitry Andric for (unsigned SuccIdx : BackEdges) 713*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = Prob; 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric if (uint32_t numInEdges = InEdges.size()) { 7170b57cec5SDimitry Andric BranchProbability TakenProb = BranchProbability(LBH_TAKEN_WEIGHT, Denom); 7180b57cec5SDimitry Andric auto Prob = TakenProb / numInEdges; 7190b57cec5SDimitry Andric for (unsigned SuccIdx : InEdges) 720*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = Prob; 7210b57cec5SDimitry Andric } 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric if (uint32_t numExitingEdges = ExitingEdges.size()) { 7240b57cec5SDimitry Andric BranchProbability NotTakenProb = BranchProbability(LBH_NONTAKEN_WEIGHT, 7250b57cec5SDimitry Andric Denom); 7260b57cec5SDimitry Andric auto Prob = NotTakenProb / numExitingEdges; 7270b57cec5SDimitry Andric for (unsigned SuccIdx : ExitingEdges) 728*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = Prob; 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric if (uint32_t numUnlikelyEdges = UnlikelyEdges.size()) { 7320b57cec5SDimitry Andric BranchProbability UnlikelyProb = BranchProbability(LBH_UNLIKELY_WEIGHT, 7330b57cec5SDimitry Andric Denom); 7340b57cec5SDimitry Andric auto Prob = UnlikelyProb / numUnlikelyEdges; 7350b57cec5SDimitry Andric for (unsigned SuccIdx : UnlikelyEdges) 736*5ffd83dbSDimitry Andric EdgeProbabilities[SuccIdx] = Prob; 7370b57cec5SDimitry Andric } 7380b57cec5SDimitry Andric 739*5ffd83dbSDimitry Andric setEdgeProbability(BB, EdgeProbabilities); 7400b57cec5SDimitry Andric return true; 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB, 7440b57cec5SDimitry Andric const TargetLibraryInfo *TLI) { 7450b57cec5SDimitry Andric const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 7460b57cec5SDimitry Andric if (!BI || !BI->isConditional()) 7470b57cec5SDimitry Andric return false; 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric Value *Cond = BI->getCondition(); 7500b57cec5SDimitry Andric ICmpInst *CI = dyn_cast<ICmpInst>(Cond); 7510b57cec5SDimitry Andric if (!CI) 7520b57cec5SDimitry Andric return false; 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric auto GetConstantInt = [](Value *V) { 7550b57cec5SDimitry Andric if (auto *I = dyn_cast<BitCastInst>(V)) 7560b57cec5SDimitry Andric return dyn_cast<ConstantInt>(I->getOperand(0)); 7570b57cec5SDimitry Andric return dyn_cast<ConstantInt>(V); 7580b57cec5SDimitry Andric }; 7590b57cec5SDimitry Andric 7600b57cec5SDimitry Andric Value *RHS = CI->getOperand(1); 7610b57cec5SDimitry Andric ConstantInt *CV = GetConstantInt(RHS); 7620b57cec5SDimitry Andric if (!CV) 7630b57cec5SDimitry Andric return false; 7640b57cec5SDimitry Andric 7650b57cec5SDimitry Andric // If the LHS is the result of AND'ing a value with a single bit bitmask, 7660b57cec5SDimitry Andric // we don't have information about probabilities. 7670b57cec5SDimitry Andric if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0))) 7680b57cec5SDimitry Andric if (LHS->getOpcode() == Instruction::And) 7690b57cec5SDimitry Andric if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) 7700b57cec5SDimitry Andric if (AndRHS->getValue().isPowerOf2()) 7710b57cec5SDimitry Andric return false; 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric // Check if the LHS is the return value of a library function 7740b57cec5SDimitry Andric LibFunc Func = NumLibFuncs; 7750b57cec5SDimitry Andric if (TLI) 7760b57cec5SDimitry Andric if (CallInst *Call = dyn_cast<CallInst>(CI->getOperand(0))) 7770b57cec5SDimitry Andric if (Function *CalledFn = Call->getCalledFunction()) 7780b57cec5SDimitry Andric TLI->getLibFunc(*CalledFn, Func); 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric bool isProb; 7810b57cec5SDimitry Andric if (Func == LibFunc_strcasecmp || 7820b57cec5SDimitry Andric Func == LibFunc_strcmp || 7830b57cec5SDimitry Andric Func == LibFunc_strncasecmp || 7840b57cec5SDimitry Andric Func == LibFunc_strncmp || 7850b57cec5SDimitry Andric Func == LibFunc_memcmp) { 7860b57cec5SDimitry Andric // strcmp and similar functions return zero, negative, or positive, if the 7870b57cec5SDimitry Andric // first string is equal, less, or greater than the second. We consider it 7880b57cec5SDimitry Andric // likely that the strings are not equal, so a comparison with zero is 7890b57cec5SDimitry Andric // probably false, but also a comparison with any other number is also 7900b57cec5SDimitry Andric // probably false given that what exactly is returned for nonzero values is 7910b57cec5SDimitry Andric // not specified. Any kind of comparison other than equality we know 7920b57cec5SDimitry Andric // nothing about. 7930b57cec5SDimitry Andric switch (CI->getPredicate()) { 7940b57cec5SDimitry Andric case CmpInst::ICMP_EQ: 7950b57cec5SDimitry Andric isProb = false; 7960b57cec5SDimitry Andric break; 7970b57cec5SDimitry Andric case CmpInst::ICMP_NE: 7980b57cec5SDimitry Andric isProb = true; 7990b57cec5SDimitry Andric break; 8000b57cec5SDimitry Andric default: 8010b57cec5SDimitry Andric return false; 8020b57cec5SDimitry Andric } 8030b57cec5SDimitry Andric } else if (CV->isZero()) { 8040b57cec5SDimitry Andric switch (CI->getPredicate()) { 8050b57cec5SDimitry Andric case CmpInst::ICMP_EQ: 8060b57cec5SDimitry Andric // X == 0 -> Unlikely 8070b57cec5SDimitry Andric isProb = false; 8080b57cec5SDimitry Andric break; 8090b57cec5SDimitry Andric case CmpInst::ICMP_NE: 8100b57cec5SDimitry Andric // X != 0 -> Likely 8110b57cec5SDimitry Andric isProb = true; 8120b57cec5SDimitry Andric break; 8130b57cec5SDimitry Andric case CmpInst::ICMP_SLT: 8140b57cec5SDimitry Andric // X < 0 -> Unlikely 8150b57cec5SDimitry Andric isProb = false; 8160b57cec5SDimitry Andric break; 8170b57cec5SDimitry Andric case CmpInst::ICMP_SGT: 8180b57cec5SDimitry Andric // X > 0 -> Likely 8190b57cec5SDimitry Andric isProb = true; 8200b57cec5SDimitry Andric break; 8210b57cec5SDimitry Andric default: 8220b57cec5SDimitry Andric return false; 8230b57cec5SDimitry Andric } 8240b57cec5SDimitry Andric } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { 8250b57cec5SDimitry Andric // InstCombine canonicalizes X <= 0 into X < 1. 8260b57cec5SDimitry Andric // X <= 0 -> Unlikely 8270b57cec5SDimitry Andric isProb = false; 8280b57cec5SDimitry Andric } else if (CV->isMinusOne()) { 8290b57cec5SDimitry Andric switch (CI->getPredicate()) { 8300b57cec5SDimitry Andric case CmpInst::ICMP_EQ: 8310b57cec5SDimitry Andric // X == -1 -> Unlikely 8320b57cec5SDimitry Andric isProb = false; 8330b57cec5SDimitry Andric break; 8340b57cec5SDimitry Andric case CmpInst::ICMP_NE: 8350b57cec5SDimitry Andric // X != -1 -> Likely 8360b57cec5SDimitry Andric isProb = true; 8370b57cec5SDimitry Andric break; 8380b57cec5SDimitry Andric case CmpInst::ICMP_SGT: 8390b57cec5SDimitry Andric // InstCombine canonicalizes X >= 0 into X > -1. 8400b57cec5SDimitry Andric // X >= 0 -> Likely 8410b57cec5SDimitry Andric isProb = true; 8420b57cec5SDimitry Andric break; 8430b57cec5SDimitry Andric default: 8440b57cec5SDimitry Andric return false; 8450b57cec5SDimitry Andric } 8460b57cec5SDimitry Andric } else { 8470b57cec5SDimitry Andric return false; 8480b57cec5SDimitry Andric } 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric BranchProbability TakenProb(ZH_TAKEN_WEIGHT, 8510b57cec5SDimitry Andric ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); 852*5ffd83dbSDimitry Andric BranchProbability UntakenProb(ZH_NONTAKEN_WEIGHT, 853*5ffd83dbSDimitry Andric ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); 854*5ffd83dbSDimitry Andric if (!isProb) 855*5ffd83dbSDimitry Andric std::swap(TakenProb, UntakenProb); 856*5ffd83dbSDimitry Andric 857*5ffd83dbSDimitry Andric setEdgeProbability( 858*5ffd83dbSDimitry Andric BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb})); 8590b57cec5SDimitry Andric return true; 8600b57cec5SDimitry Andric } 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) { 8630b57cec5SDimitry Andric const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 8640b57cec5SDimitry Andric if (!BI || !BI->isConditional()) 8650b57cec5SDimitry Andric return false; 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric Value *Cond = BI->getCondition(); 8680b57cec5SDimitry Andric FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond); 8690b57cec5SDimitry Andric if (!FCmp) 8700b57cec5SDimitry Andric return false; 8710b57cec5SDimitry Andric 8728bcb0991SDimitry Andric uint32_t TakenWeight = FPH_TAKEN_WEIGHT; 8738bcb0991SDimitry Andric uint32_t NontakenWeight = FPH_NONTAKEN_WEIGHT; 8740b57cec5SDimitry Andric bool isProb; 8750b57cec5SDimitry Andric if (FCmp->isEquality()) { 8760b57cec5SDimitry Andric // f1 == f2 -> Unlikely 8770b57cec5SDimitry Andric // f1 != f2 -> Likely 8780b57cec5SDimitry Andric isProb = !FCmp->isTrueWhenEqual(); 8790b57cec5SDimitry Andric } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) { 8800b57cec5SDimitry Andric // !isnan -> Likely 8810b57cec5SDimitry Andric isProb = true; 8828bcb0991SDimitry Andric TakenWeight = FPH_ORD_WEIGHT; 8838bcb0991SDimitry Andric NontakenWeight = FPH_UNO_WEIGHT; 8840b57cec5SDimitry Andric } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) { 8850b57cec5SDimitry Andric // isnan -> Unlikely 8860b57cec5SDimitry Andric isProb = false; 8878bcb0991SDimitry Andric TakenWeight = FPH_ORD_WEIGHT; 8888bcb0991SDimitry Andric NontakenWeight = FPH_UNO_WEIGHT; 8890b57cec5SDimitry Andric } else { 8900b57cec5SDimitry Andric return false; 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric 8938bcb0991SDimitry Andric BranchProbability TakenProb(TakenWeight, TakenWeight + NontakenWeight); 894*5ffd83dbSDimitry Andric BranchProbability UntakenProb(NontakenWeight, TakenWeight + NontakenWeight); 895*5ffd83dbSDimitry Andric if (!isProb) 896*5ffd83dbSDimitry Andric std::swap(TakenProb, UntakenProb); 897*5ffd83dbSDimitry Andric 898*5ffd83dbSDimitry Andric setEdgeProbability( 899*5ffd83dbSDimitry Andric BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb})); 9000b57cec5SDimitry Andric return true; 9010b57cec5SDimitry Andric } 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric bool BranchProbabilityInfo::calcInvokeHeuristics(const BasicBlock *BB) { 9040b57cec5SDimitry Andric const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()); 9050b57cec5SDimitry Andric if (!II) 9060b57cec5SDimitry Andric return false; 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric BranchProbability TakenProb(IH_TAKEN_WEIGHT, 9090b57cec5SDimitry Andric IH_TAKEN_WEIGHT + IH_NONTAKEN_WEIGHT); 910*5ffd83dbSDimitry Andric setEdgeProbability( 911*5ffd83dbSDimitry Andric BB, SmallVector<BranchProbability, 2>({TakenProb, TakenProb.getCompl()})); 9120b57cec5SDimitry Andric return true; 9130b57cec5SDimitry Andric } 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric void BranchProbabilityInfo::releaseMemory() { 9160b57cec5SDimitry Andric Probs.clear(); 917*5ffd83dbSDimitry Andric Handles.clear(); 918*5ffd83dbSDimitry Andric } 919*5ffd83dbSDimitry Andric 920*5ffd83dbSDimitry Andric bool BranchProbabilityInfo::invalidate(Function &, const PreservedAnalyses &PA, 921*5ffd83dbSDimitry Andric FunctionAnalysisManager::Invalidator &) { 922*5ffd83dbSDimitry Andric // Check whether the analysis, all analyses on functions, or the function's 923*5ffd83dbSDimitry Andric // CFG have been preserved. 924*5ffd83dbSDimitry Andric auto PAC = PA.getChecker<BranchProbabilityAnalysis>(); 925*5ffd83dbSDimitry Andric return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() || 926*5ffd83dbSDimitry Andric PAC.preservedSet<CFGAnalyses>()); 9270b57cec5SDimitry Andric } 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric void BranchProbabilityInfo::print(raw_ostream &OS) const { 9300b57cec5SDimitry Andric OS << "---- Branch Probabilities ----\n"; 9310b57cec5SDimitry Andric // We print the probabilities from the last function the analysis ran over, 9320b57cec5SDimitry Andric // or the function it is currently running over. 9330b57cec5SDimitry Andric assert(LastF && "Cannot print prior to running over a function"); 9340b57cec5SDimitry Andric for (const auto &BI : *LastF) { 935*5ffd83dbSDimitry Andric for (const_succ_iterator SI = succ_begin(&BI), SE = succ_end(&BI); SI != SE; 9360b57cec5SDimitry Andric ++SI) { 9370b57cec5SDimitry Andric printEdgeProbability(OS << " ", &BI, *SI); 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric bool BranchProbabilityInfo:: 9430b57cec5SDimitry Andric isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const { 9440b57cec5SDimitry Andric // Hot probability is at least 4/5 = 80% 9450b57cec5SDimitry Andric // FIXME: Compare against a static "hot" BranchProbability. 9460b57cec5SDimitry Andric return getEdgeProbability(Src, Dst) > BranchProbability(4, 5); 9470b57cec5SDimitry Andric } 9480b57cec5SDimitry Andric 9490b57cec5SDimitry Andric const BasicBlock * 9500b57cec5SDimitry Andric BranchProbabilityInfo::getHotSucc(const BasicBlock *BB) const { 9510b57cec5SDimitry Andric auto MaxProb = BranchProbability::getZero(); 9520b57cec5SDimitry Andric const BasicBlock *MaxSucc = nullptr; 9530b57cec5SDimitry Andric 954*5ffd83dbSDimitry Andric for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 9550b57cec5SDimitry Andric const BasicBlock *Succ = *I; 9560b57cec5SDimitry Andric auto Prob = getEdgeProbability(BB, Succ); 9570b57cec5SDimitry Andric if (Prob > MaxProb) { 9580b57cec5SDimitry Andric MaxProb = Prob; 9590b57cec5SDimitry Andric MaxSucc = Succ; 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric } 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric // Hot probability is at least 4/5 = 80% 9640b57cec5SDimitry Andric if (MaxProb > BranchProbability(4, 5)) 9650b57cec5SDimitry Andric return MaxSucc; 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric return nullptr; 9680b57cec5SDimitry Andric } 9690b57cec5SDimitry Andric 9700b57cec5SDimitry Andric /// Get the raw edge probability for the edge. If can't find it, return a 9710b57cec5SDimitry Andric /// default probability 1/N where N is the number of successors. Here an edge is 9720b57cec5SDimitry Andric /// specified using PredBlock and an 9730b57cec5SDimitry Andric /// index to the successors. 9740b57cec5SDimitry Andric BranchProbability 9750b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, 9760b57cec5SDimitry Andric unsigned IndexInSuccessors) const { 9770b57cec5SDimitry Andric auto I = Probs.find(std::make_pair(Src, IndexInSuccessors)); 9780b57cec5SDimitry Andric 9790b57cec5SDimitry Andric if (I != Probs.end()) 9800b57cec5SDimitry Andric return I->second; 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric return {1, static_cast<uint32_t>(succ_size(Src))}; 9830b57cec5SDimitry Andric } 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric BranchProbability 9860b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, 987*5ffd83dbSDimitry Andric const_succ_iterator Dst) const { 9880b57cec5SDimitry Andric return getEdgeProbability(Src, Dst.getSuccessorIndex()); 9890b57cec5SDimitry Andric } 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric /// Get the raw edge probability calculated for the block pair. This returns the 9920b57cec5SDimitry Andric /// sum of all raw edge probabilities from Src to Dst. 9930b57cec5SDimitry Andric BranchProbability 9940b57cec5SDimitry Andric BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src, 9950b57cec5SDimitry Andric const BasicBlock *Dst) const { 9960b57cec5SDimitry Andric auto Prob = BranchProbability::getZero(); 9970b57cec5SDimitry Andric bool FoundProb = false; 998*5ffd83dbSDimitry Andric uint32_t EdgeCount = 0; 999*5ffd83dbSDimitry Andric for (const_succ_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I) 10000b57cec5SDimitry Andric if (*I == Dst) { 1001*5ffd83dbSDimitry Andric ++EdgeCount; 10020b57cec5SDimitry Andric auto MapI = Probs.find(std::make_pair(Src, I.getSuccessorIndex())); 10030b57cec5SDimitry Andric if (MapI != Probs.end()) { 10040b57cec5SDimitry Andric FoundProb = true; 10050b57cec5SDimitry Andric Prob += MapI->second; 10060b57cec5SDimitry Andric } 10070b57cec5SDimitry Andric } 10080b57cec5SDimitry Andric uint32_t succ_num = std::distance(succ_begin(Src), succ_end(Src)); 1009*5ffd83dbSDimitry Andric return FoundProb ? Prob : BranchProbability(EdgeCount, succ_num); 10100b57cec5SDimitry Andric } 10110b57cec5SDimitry Andric 10120b57cec5SDimitry Andric /// Set the edge probability for a given edge specified by PredBlock and an 10130b57cec5SDimitry Andric /// index to the successors. 10140b57cec5SDimitry Andric void BranchProbabilityInfo::setEdgeProbability(const BasicBlock *Src, 10150b57cec5SDimitry Andric unsigned IndexInSuccessors, 10160b57cec5SDimitry Andric BranchProbability Prob) { 10170b57cec5SDimitry Andric Probs[std::make_pair(Src, IndexInSuccessors)] = Prob; 10180b57cec5SDimitry Andric Handles.insert(BasicBlockCallbackVH(Src, this)); 10190b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "set edge " << Src->getName() << " -> " 10200b57cec5SDimitry Andric << IndexInSuccessors << " successor probability to " << Prob 10210b57cec5SDimitry Andric << "\n"); 10220b57cec5SDimitry Andric } 10230b57cec5SDimitry Andric 1024*5ffd83dbSDimitry Andric /// Set the edge probability for all edges at once. 1025*5ffd83dbSDimitry Andric void BranchProbabilityInfo::setEdgeProbability( 1026*5ffd83dbSDimitry Andric const BasicBlock *Src, const SmallVectorImpl<BranchProbability> &Probs) { 1027*5ffd83dbSDimitry Andric assert(Src->getTerminator()->getNumSuccessors() == Probs.size()); 1028*5ffd83dbSDimitry Andric if (Probs.size() == 0) 1029*5ffd83dbSDimitry Andric return; // Nothing to set. 1030*5ffd83dbSDimitry Andric 1031*5ffd83dbSDimitry Andric uint64_t TotalNumerator = 0; 1032*5ffd83dbSDimitry Andric for (unsigned SuccIdx = 0; SuccIdx < Probs.size(); ++SuccIdx) { 1033*5ffd83dbSDimitry Andric setEdgeProbability(Src, SuccIdx, Probs[SuccIdx]); 1034*5ffd83dbSDimitry Andric TotalNumerator += Probs[SuccIdx].getNumerator(); 1035*5ffd83dbSDimitry Andric } 1036*5ffd83dbSDimitry Andric 1037*5ffd83dbSDimitry Andric // Because of rounding errors the total probability cannot be checked to be 1038*5ffd83dbSDimitry Andric // 1.0 exactly. That is TotalNumerator == BranchProbability::getDenominator. 1039*5ffd83dbSDimitry Andric // Instead, every single probability in Probs must be as accurate as possible. 1040*5ffd83dbSDimitry Andric // This results in error 1/denominator at most, thus the total absolute error 1041*5ffd83dbSDimitry Andric // should be within Probs.size / BranchProbability::getDenominator. 1042*5ffd83dbSDimitry Andric assert(TotalNumerator <= BranchProbability::getDenominator() + Probs.size()); 1043*5ffd83dbSDimitry Andric assert(TotalNumerator >= BranchProbability::getDenominator() - Probs.size()); 1044*5ffd83dbSDimitry Andric } 1045*5ffd83dbSDimitry Andric 10460b57cec5SDimitry Andric raw_ostream & 10470b57cec5SDimitry Andric BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, 10480b57cec5SDimitry Andric const BasicBlock *Src, 10490b57cec5SDimitry Andric const BasicBlock *Dst) const { 10500b57cec5SDimitry Andric const BranchProbability Prob = getEdgeProbability(Src, Dst); 10510b57cec5SDimitry Andric OS << "edge " << Src->getName() << " -> " << Dst->getName() 10520b57cec5SDimitry Andric << " probability is " << Prob 10530b57cec5SDimitry Andric << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n"); 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric return OS; 10560b57cec5SDimitry Andric } 10570b57cec5SDimitry Andric 10580b57cec5SDimitry Andric void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) { 1059*5ffd83dbSDimitry Andric for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 1060*5ffd83dbSDimitry Andric auto MapI = Probs.find(std::make_pair(BB, I.getSuccessorIndex())); 1061*5ffd83dbSDimitry Andric if (MapI != Probs.end()) 1062*5ffd83dbSDimitry Andric Probs.erase(MapI); 10630b57cec5SDimitry Andric } 10640b57cec5SDimitry Andric } 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LI, 1067*5ffd83dbSDimitry Andric const TargetLibraryInfo *TLI, 1068*5ffd83dbSDimitry Andric PostDominatorTree *PDT) { 10690b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName() 10700b57cec5SDimitry Andric << " ----\n\n"); 10710b57cec5SDimitry Andric LastF = &F; // Store the last function we ran on for printing. 10720b57cec5SDimitry Andric assert(PostDominatedByUnreachable.empty()); 10730b57cec5SDimitry Andric assert(PostDominatedByColdCall.empty()); 10740b57cec5SDimitry Andric 10750b57cec5SDimitry Andric // Record SCC numbers of blocks in the CFG to identify irreducible loops. 10760b57cec5SDimitry Andric // FIXME: We could only calculate this if the CFG is known to be irreducible 10770b57cec5SDimitry Andric // (perhaps cache this info in LoopInfo if we can easily calculate it there?). 10780b57cec5SDimitry Andric int SccNum = 0; 10790b57cec5SDimitry Andric SccInfo SccI; 10800b57cec5SDimitry Andric for (scc_iterator<const Function *> It = scc_begin(&F); !It.isAtEnd(); 10810b57cec5SDimitry Andric ++It, ++SccNum) { 10820b57cec5SDimitry Andric // Ignore single-block SCCs since they either aren't loops or LoopInfo will 10830b57cec5SDimitry Andric // catch them. 10840b57cec5SDimitry Andric const std::vector<const BasicBlock *> &Scc = *It; 10850b57cec5SDimitry Andric if (Scc.size() == 1) 10860b57cec5SDimitry Andric continue; 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "BPI: SCC " << SccNum << ":"); 10890b57cec5SDimitry Andric for (auto *BB : Scc) { 10900b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " " << BB->getName()); 10910b57cec5SDimitry Andric SccI.SccNums[BB] = SccNum; 10920b57cec5SDimitry Andric } 10930b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 10940b57cec5SDimitry Andric } 10950b57cec5SDimitry Andric 1096*5ffd83dbSDimitry Andric std::unique_ptr<PostDominatorTree> PDTPtr; 1097*5ffd83dbSDimitry Andric 1098*5ffd83dbSDimitry Andric if (!PDT) { 1099*5ffd83dbSDimitry Andric PDTPtr = std::make_unique<PostDominatorTree>(const_cast<Function &>(F)); 1100*5ffd83dbSDimitry Andric PDT = PDTPtr.get(); 1101*5ffd83dbSDimitry Andric } 1102*5ffd83dbSDimitry Andric 1103*5ffd83dbSDimitry Andric computePostDominatedByUnreachable(F, PDT); 1104*5ffd83dbSDimitry Andric computePostDominatedByColdCall(F, PDT); 1105480093f4SDimitry Andric 11060b57cec5SDimitry Andric // Walk the basic blocks in post-order so that we can build up state about 11070b57cec5SDimitry Andric // the successors of a block iteratively. 11080b57cec5SDimitry Andric for (auto BB : post_order(&F.getEntryBlock())) { 11090b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Computing probabilities for " << BB->getName() 11100b57cec5SDimitry Andric << "\n"); 11110b57cec5SDimitry Andric // If there is no at least two successors, no sense to set probability. 11120b57cec5SDimitry Andric if (BB->getTerminator()->getNumSuccessors() < 2) 11130b57cec5SDimitry Andric continue; 11140b57cec5SDimitry Andric if (calcMetadataWeights(BB)) 11150b57cec5SDimitry Andric continue; 11160b57cec5SDimitry Andric if (calcInvokeHeuristics(BB)) 11170b57cec5SDimitry Andric continue; 11180b57cec5SDimitry Andric if (calcUnreachableHeuristics(BB)) 11190b57cec5SDimitry Andric continue; 11200b57cec5SDimitry Andric if (calcColdCallHeuristics(BB)) 11210b57cec5SDimitry Andric continue; 11220b57cec5SDimitry Andric if (calcLoopBranchHeuristics(BB, LI, SccI)) 11230b57cec5SDimitry Andric continue; 11240b57cec5SDimitry Andric if (calcPointerHeuristics(BB)) 11250b57cec5SDimitry Andric continue; 11260b57cec5SDimitry Andric if (calcZeroHeuristics(BB, TLI)) 11270b57cec5SDimitry Andric continue; 11280b57cec5SDimitry Andric if (calcFloatingPointHeuristics(BB)) 11290b57cec5SDimitry Andric continue; 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric PostDominatedByUnreachable.clear(); 11330b57cec5SDimitry Andric PostDominatedByColdCall.clear(); 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andric if (PrintBranchProb && 11360b57cec5SDimitry Andric (PrintBranchProbFuncName.empty() || 11370b57cec5SDimitry Andric F.getName().equals(PrintBranchProbFuncName))) { 11380b57cec5SDimitry Andric print(dbgs()); 11390b57cec5SDimitry Andric } 11400b57cec5SDimitry Andric } 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::getAnalysisUsage( 11430b57cec5SDimitry Andric AnalysisUsage &AU) const { 11440b57cec5SDimitry Andric // We require DT so it's available when LI is available. The LI updating code 11450b57cec5SDimitry Andric // asserts that DT is also present so if we don't make sure that we have DT 11460b57cec5SDimitry Andric // here, that assert will trigger. 11470b57cec5SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>(); 11480b57cec5SDimitry Andric AU.addRequired<LoopInfoWrapperPass>(); 11490b57cec5SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>(); 1150*5ffd83dbSDimitry Andric AU.addRequired<PostDominatorTreeWrapperPass>(); 11510b57cec5SDimitry Andric AU.setPreservesAll(); 11520b57cec5SDimitry Andric } 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) { 11550b57cec5SDimitry Andric const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 11568bcb0991SDimitry Andric const TargetLibraryInfo &TLI = 11578bcb0991SDimitry Andric getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 1158*5ffd83dbSDimitry Andric PostDominatorTree &PDT = 1159*5ffd83dbSDimitry Andric getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree(); 1160*5ffd83dbSDimitry Andric BPI.calculate(F, LI, &TLI, &PDT); 11610b57cec5SDimitry Andric return false; 11620b57cec5SDimitry Andric } 11630b57cec5SDimitry Andric 11640b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); } 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS, 11670b57cec5SDimitry Andric const Module *) const { 11680b57cec5SDimitry Andric BPI.print(OS); 11690b57cec5SDimitry Andric } 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric AnalysisKey BranchProbabilityAnalysis::Key; 11720b57cec5SDimitry Andric BranchProbabilityInfo 11730b57cec5SDimitry Andric BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) { 11740b57cec5SDimitry Andric BranchProbabilityInfo BPI; 1175*5ffd83dbSDimitry Andric BPI.calculate(F, AM.getResult<LoopAnalysis>(F), 1176*5ffd83dbSDimitry Andric &AM.getResult<TargetLibraryAnalysis>(F), 1177*5ffd83dbSDimitry Andric &AM.getResult<PostDominatorTreeAnalysis>(F)); 11780b57cec5SDimitry Andric return BPI; 11790b57cec5SDimitry Andric } 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric PreservedAnalyses 11820b57cec5SDimitry Andric BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { 11830b57cec5SDimitry Andric OS << "Printing analysis results of BPI for function " 11840b57cec5SDimitry Andric << "'" << F.getName() << "':" 11850b57cec5SDimitry Andric << "\n"; 11860b57cec5SDimitry Andric AM.getResult<BranchProbabilityAnalysis>(F).print(OS); 11870b57cec5SDimitry Andric return PreservedAnalyses::all(); 11880b57cec5SDimitry Andric } 1189