Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/BranchProbabilityInfo.cpp
35234 views
//===- BranchProbabilityInfo.cpp - Branch Probability Analysis ------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Loops should be simplified before this analysis.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Analysis/BranchProbabilityInfo.h"13#include "llvm/ADT/PostOrderIterator.h"14#include "llvm/ADT/SCCIterator.h"15#include "llvm/ADT/STLExtras.h"16#include "llvm/ADT/SmallVector.h"17#include "llvm/Analysis/ConstantFolding.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/ProfDataUtils.h"34#include "llvm/IR/Type.h"35#include "llvm/IR/Value.h"36#include "llvm/InitializePasses.h"37#include "llvm/Pass.h"38#include "llvm/Support/BranchProbability.h"39#include "llvm/Support/Casting.h"40#include "llvm/Support/CommandLine.h"41#include "llvm/Support/Debug.h"42#include "llvm/Support/raw_ostream.h"43#include <cassert>44#include <cstdint>45#include <iterator>46#include <map>47#include <utility>4849using namespace llvm;5051#define DEBUG_TYPE "branch-prob"5253static cl::opt<bool> PrintBranchProb(54"print-bpi", cl::init(false), cl::Hidden,55cl::desc("Print the branch probability info."));5657cl::opt<std::string> PrintBranchProbFuncName(58"print-bpi-func-name", cl::Hidden,59cl::desc("The option to specify the name of the function "60"whose branch probability info is printed."));6162INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob",63"Branch Probability Analysis", false, true)64INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)65INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)66INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)67INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)68INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass, "branch-prob",69"Branch Probability Analysis", false, true)7071BranchProbabilityInfoWrapperPass::BranchProbabilityInfoWrapperPass()72: FunctionPass(ID) {73initializeBranchProbabilityInfoWrapperPassPass(74*PassRegistry::getPassRegistry());75}7677char BranchProbabilityInfoWrapperPass::ID = 0;7879// Weights are for internal use only. They are used by heuristics to help to80// estimate edges' probability. Example:81//82// Using "Loop Branch Heuristics" we predict weights of edges for the83// block BB2.84// ...85// |86// V87// BB1<-+88// | |89// | | (Weight = 124)90// V |91// BB2--+92// |93// | (Weight = 4)94// V95// BB396//97// Probability of the edge BB2->BB1 = 124 / (124 + 4) = 0.9687598// Probability of the edge BB2->BB3 = 4 / (124 + 4) = 0.0312599static const uint32_t LBH_TAKEN_WEIGHT = 124;100static const uint32_t LBH_NONTAKEN_WEIGHT = 4;101102/// Unreachable-terminating branch taken probability.103///104/// This is the probability for a branch being taken to a block that terminates105/// (eventually) in unreachable. These are predicted as unlikely as possible.106/// All reachable probability will proportionally share the remaining part.107static const BranchProbability UR_TAKEN_PROB = BranchProbability::getRaw(1);108109/// Heuristics and lookup tables for non-loop branches:110/// Pointer Heuristics (PH)111static const uint32_t PH_TAKEN_WEIGHT = 20;112static const uint32_t PH_NONTAKEN_WEIGHT = 12;113static const BranchProbability114PtrTakenProb(PH_TAKEN_WEIGHT, PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);115static const BranchProbability116PtrUntakenProb(PH_NONTAKEN_WEIGHT, PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);117118using ProbabilityList = SmallVector<BranchProbability>;119using ProbabilityTable = std::map<CmpInst::Predicate, ProbabilityList>;120121/// Pointer comparisons:122static const ProbabilityTable PointerTable{123{ICmpInst::ICMP_NE, {PtrTakenProb, PtrUntakenProb}}, /// p != q -> Likely124{ICmpInst::ICMP_EQ, {PtrUntakenProb, PtrTakenProb}}, /// p == q -> Unlikely125};126127/// Zero Heuristics (ZH)128static const uint32_t ZH_TAKEN_WEIGHT = 20;129static const uint32_t ZH_NONTAKEN_WEIGHT = 12;130static const BranchProbability131ZeroTakenProb(ZH_TAKEN_WEIGHT, ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);132static const BranchProbability133ZeroUntakenProb(ZH_NONTAKEN_WEIGHT, ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);134135/// Integer compares with 0:136static const ProbabilityTable ICmpWithZeroTable{137{CmpInst::ICMP_EQ, {ZeroUntakenProb, ZeroTakenProb}}, /// X == 0 -> Unlikely138{CmpInst::ICMP_NE, {ZeroTakenProb, ZeroUntakenProb}}, /// X != 0 -> Likely139{CmpInst::ICMP_SLT, {ZeroUntakenProb, ZeroTakenProb}}, /// X < 0 -> Unlikely140{CmpInst::ICMP_SGT, {ZeroTakenProb, ZeroUntakenProb}}, /// X > 0 -> Likely141};142143/// Integer compares with -1:144static const ProbabilityTable ICmpWithMinusOneTable{145{CmpInst::ICMP_EQ, {ZeroUntakenProb, ZeroTakenProb}}, /// X == -1 -> Unlikely146{CmpInst::ICMP_NE, {ZeroTakenProb, ZeroUntakenProb}}, /// X != -1 -> Likely147// InstCombine canonicalizes X >= 0 into X > -1148{CmpInst::ICMP_SGT, {ZeroTakenProb, ZeroUntakenProb}}, /// X >= 0 -> Likely149};150151/// Integer compares with 1:152static const ProbabilityTable ICmpWithOneTable{153// InstCombine canonicalizes X <= 0 into X < 1154{CmpInst::ICMP_SLT, {ZeroUntakenProb, ZeroTakenProb}}, /// X <= 0 -> Unlikely155};156157/// strcmp and similar functions return zero, negative, or positive, if the158/// first string is equal, less, or greater than the second. We consider it159/// likely that the strings are not equal, so a comparison with zero is160/// probably false, but also a comparison with any other number is also161/// probably false given that what exactly is returned for nonzero values is162/// not specified. Any kind of comparison other than equality we know163/// nothing about.164static const ProbabilityTable ICmpWithLibCallTable{165{CmpInst::ICMP_EQ, {ZeroUntakenProb, ZeroTakenProb}},166{CmpInst::ICMP_NE, {ZeroTakenProb, ZeroUntakenProb}},167};168169// Floating-Point Heuristics (FPH)170static const uint32_t FPH_TAKEN_WEIGHT = 20;171static const uint32_t FPH_NONTAKEN_WEIGHT = 12;172173/// This is the probability for an ordered floating point comparison.174static const uint32_t FPH_ORD_WEIGHT = 1024 * 1024 - 1;175/// This is the probability for an unordered floating point comparison, it means176/// one or two of the operands are NaN. Usually it is used to test for an177/// exceptional case, so the result is unlikely.178static const uint32_t FPH_UNO_WEIGHT = 1;179180static const BranchProbability FPOrdTakenProb(FPH_ORD_WEIGHT,181FPH_ORD_WEIGHT + FPH_UNO_WEIGHT);182static const BranchProbability183FPOrdUntakenProb(FPH_UNO_WEIGHT, FPH_ORD_WEIGHT + FPH_UNO_WEIGHT);184static const BranchProbability185FPTakenProb(FPH_TAKEN_WEIGHT, FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);186static const BranchProbability187FPUntakenProb(FPH_NONTAKEN_WEIGHT, FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);188189/// Floating-Point compares:190static const ProbabilityTable FCmpTable{191{FCmpInst::FCMP_ORD, {FPOrdTakenProb, FPOrdUntakenProb}}, /// !isnan -> Likely192{FCmpInst::FCMP_UNO, {FPOrdUntakenProb, FPOrdTakenProb}}, /// isnan -> Unlikely193};194195/// Set of dedicated "absolute" execution weights for a block. These weights are196/// meaningful relative to each other and their derivatives only.197enum class BlockExecWeight : std::uint32_t {198/// Special weight used for cases with exact zero probability.199ZERO = 0x0,200/// Minimal possible non zero weight.201LOWEST_NON_ZERO = 0x1,202/// Weight to an 'unreachable' block.203UNREACHABLE = ZERO,204/// Weight to a block containing non returning call.205NORETURN = LOWEST_NON_ZERO,206/// Weight to 'unwind' block of an invoke instruction.207UNWIND = LOWEST_NON_ZERO,208/// Weight to a 'cold' block. Cold blocks are the ones containing calls marked209/// with attribute 'cold'.210COLD = 0xffff,211/// Default weight is used in cases when there is no dedicated execution212/// weight set. It is not propagated through the domination line either.213DEFAULT = 0xfffff214};215216BranchProbabilityInfo::SccInfo::SccInfo(const Function &F) {217// Record SCC numbers of blocks in the CFG to identify irreducible loops.218// FIXME: We could only calculate this if the CFG is known to be irreducible219// (perhaps cache this info in LoopInfo if we can easily calculate it there?).220int SccNum = 0;221for (scc_iterator<const Function *> It = scc_begin(&F); !It.isAtEnd();222++It, ++SccNum) {223// Ignore single-block SCCs since they either aren't loops or LoopInfo will224// catch them.225const std::vector<const BasicBlock *> &Scc = *It;226if (Scc.size() == 1)227continue;228229LLVM_DEBUG(dbgs() << "BPI: SCC " << SccNum << ":");230for (const auto *BB : Scc) {231LLVM_DEBUG(dbgs() << " " << BB->getName());232SccNums[BB] = SccNum;233calculateSccBlockType(BB, SccNum);234}235LLVM_DEBUG(dbgs() << "\n");236}237}238239int BranchProbabilityInfo::SccInfo::getSCCNum(const BasicBlock *BB) const {240auto SccIt = SccNums.find(BB);241if (SccIt == SccNums.end())242return -1;243return SccIt->second;244}245246void BranchProbabilityInfo::SccInfo::getSccEnterBlocks(247int SccNum, SmallVectorImpl<BasicBlock *> &Enters) const {248249for (auto MapIt : SccBlocks[SccNum]) {250const auto *BB = MapIt.first;251if (isSCCHeader(BB, SccNum))252for (const auto *Pred : predecessors(BB))253if (getSCCNum(Pred) != SccNum)254Enters.push_back(const_cast<BasicBlock *>(BB));255}256}257258void BranchProbabilityInfo::SccInfo::getSccExitBlocks(259int SccNum, SmallVectorImpl<BasicBlock *> &Exits) const {260for (auto MapIt : SccBlocks[SccNum]) {261const auto *BB = MapIt.first;262if (isSCCExitingBlock(BB, SccNum))263for (const auto *Succ : successors(BB))264if (getSCCNum(Succ) != SccNum)265Exits.push_back(const_cast<BasicBlock *>(Succ));266}267}268269uint32_t BranchProbabilityInfo::SccInfo::getSccBlockType(const BasicBlock *BB,270int SccNum) const {271assert(getSCCNum(BB) == SccNum);272273assert(SccBlocks.size() > static_cast<unsigned>(SccNum) && "Unknown SCC");274const auto &SccBlockTypes = SccBlocks[SccNum];275276auto It = SccBlockTypes.find(BB);277if (It != SccBlockTypes.end()) {278return It->second;279}280return Inner;281}282283void BranchProbabilityInfo::SccInfo::calculateSccBlockType(const BasicBlock *BB,284int SccNum) {285assert(getSCCNum(BB) == SccNum);286uint32_t BlockType = Inner;287288if (llvm::any_of(predecessors(BB), [&](const BasicBlock *Pred) {289// Consider any block that is an entry point to the SCC as290// a header.291return getSCCNum(Pred) != SccNum;292}))293BlockType |= Header;294295if (llvm::any_of(successors(BB), [&](const BasicBlock *Succ) {296return getSCCNum(Succ) != SccNum;297}))298BlockType |= Exiting;299300// Lazily compute the set of headers for a given SCC and cache the results301// in the SccHeaderMap.302if (SccBlocks.size() <= static_cast<unsigned>(SccNum))303SccBlocks.resize(SccNum + 1);304auto &SccBlockTypes = SccBlocks[SccNum];305306if (BlockType != Inner) {307bool IsInserted;308std::tie(std::ignore, IsInserted) =309SccBlockTypes.insert(std::make_pair(BB, BlockType));310assert(IsInserted && "Duplicated block in SCC");311}312}313314BranchProbabilityInfo::LoopBlock::LoopBlock(const BasicBlock *BB,315const LoopInfo &LI,316const SccInfo &SccI)317: BB(BB) {318LD.first = LI.getLoopFor(BB);319if (!LD.first) {320LD.second = SccI.getSCCNum(BB);321}322}323324bool BranchProbabilityInfo::isLoopEnteringEdge(const LoopEdge &Edge) const {325const auto &SrcBlock = Edge.first;326const auto &DstBlock = Edge.second;327return (DstBlock.getLoop() &&328!DstBlock.getLoop()->contains(SrcBlock.getLoop())) ||329// Assume that SCCs can't be nested.330(DstBlock.getSccNum() != -1 &&331SrcBlock.getSccNum() != DstBlock.getSccNum());332}333334bool BranchProbabilityInfo::isLoopExitingEdge(const LoopEdge &Edge) const {335return isLoopEnteringEdge({Edge.second, Edge.first});336}337338bool BranchProbabilityInfo::isLoopEnteringExitingEdge(339const LoopEdge &Edge) const {340return isLoopEnteringEdge(Edge) || isLoopExitingEdge(Edge);341}342343bool BranchProbabilityInfo::isLoopBackEdge(const LoopEdge &Edge) const {344const auto &SrcBlock = Edge.first;345const auto &DstBlock = Edge.second;346return SrcBlock.belongsToSameLoop(DstBlock) &&347((DstBlock.getLoop() &&348DstBlock.getLoop()->getHeader() == DstBlock.getBlock()) ||349(DstBlock.getSccNum() != -1 &&350SccI->isSCCHeader(DstBlock.getBlock(), DstBlock.getSccNum())));351}352353void BranchProbabilityInfo::getLoopEnterBlocks(354const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Enters) const {355if (LB.getLoop()) {356auto *Header = LB.getLoop()->getHeader();357Enters.append(pred_begin(Header), pred_end(Header));358} else {359assert(LB.getSccNum() != -1 && "LB doesn't belong to any loop?");360SccI->getSccEnterBlocks(LB.getSccNum(), Enters);361}362}363364void BranchProbabilityInfo::getLoopExitBlocks(365const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Exits) const {366if (LB.getLoop()) {367LB.getLoop()->getExitBlocks(Exits);368} else {369assert(LB.getSccNum() != -1 && "LB doesn't belong to any loop?");370SccI->getSccExitBlocks(LB.getSccNum(), Exits);371}372}373374// Propagate existing explicit probabilities from either profile data or375// 'expect' intrinsic processing. Examine metadata against unreachable376// heuristic. The probability of the edge coming to unreachable block is377// set to min of metadata and unreachable heuristic.378bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {379const Instruction *TI = BB->getTerminator();380assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");381if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI) ||382isa<InvokeInst>(TI) || isa<CallBrInst>(TI)))383return false;384385MDNode *WeightsNode = getValidBranchWeightMDNode(*TI);386if (!WeightsNode)387return false;388389// Check that the number of successors is manageable.390assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors");391392// Build up the final weights that will be used in a temporary buffer.393// Compute the sum of all weights to later decide whether they need to394// be scaled to fit in 32 bits.395uint64_t WeightSum = 0;396SmallVector<uint32_t, 2> Weights;397SmallVector<unsigned, 2> UnreachableIdxs;398SmallVector<unsigned, 2> ReachableIdxs;399400extractBranchWeights(WeightsNode, Weights);401for (unsigned I = 0, E = Weights.size(); I != E; ++I) {402WeightSum += Weights[I];403const LoopBlock SrcLoopBB = getLoopBlock(BB);404const LoopBlock DstLoopBB = getLoopBlock(TI->getSuccessor(I));405auto EstimatedWeight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});406if (EstimatedWeight &&407*EstimatedWeight <= static_cast<uint32_t>(BlockExecWeight::UNREACHABLE))408UnreachableIdxs.push_back(I);409else410ReachableIdxs.push_back(I);411}412assert(Weights.size() == TI->getNumSuccessors() && "Checked above");413414// If the sum of weights does not fit in 32 bits, scale every weight down415// accordingly.416uint64_t ScalingFactor =417(WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;418419if (ScalingFactor > 1) {420WeightSum = 0;421for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) {422Weights[I] /= ScalingFactor;423WeightSum += Weights[I];424}425}426assert(WeightSum <= UINT32_MAX &&427"Expected weights to scale down to 32 bits");428429if (WeightSum == 0 || ReachableIdxs.size() == 0) {430for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)431Weights[I] = 1;432WeightSum = TI->getNumSuccessors();433}434435// Set the probability.436SmallVector<BranchProbability, 2> BP;437for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)438BP.push_back({ Weights[I], static_cast<uint32_t>(WeightSum) });439440// Examine the metadata against unreachable heuristic.441// If the unreachable heuristic is more strong then we use it for this edge.442if (UnreachableIdxs.size() == 0 || ReachableIdxs.size() == 0) {443setEdgeProbability(BB, BP);444return true;445}446447auto UnreachableProb = UR_TAKEN_PROB;448for (auto I : UnreachableIdxs)449if (UnreachableProb < BP[I]) {450BP[I] = UnreachableProb;451}452453// Sum of all edge probabilities must be 1.0. If we modified the probability454// of some edges then we must distribute the introduced difference over the455// reachable blocks.456//457// Proportional distribution: the relation between probabilities of the458// reachable edges is kept unchanged. That is for any reachable edges i and j:459// newBP[i] / newBP[j] == oldBP[i] / oldBP[j] =>460// newBP[i] / oldBP[i] == newBP[j] / oldBP[j] == K461// Where K is independent of i,j.462// newBP[i] == oldBP[i] * K463// We need to find K.464// Make sum of all reachables of the left and right parts:465// sum_of_reachable(newBP) == K * sum_of_reachable(oldBP)466// Sum of newBP must be equal to 1.0:467// sum_of_reachable(newBP) + sum_of_unreachable(newBP) == 1.0 =>468// sum_of_reachable(newBP) = 1.0 - sum_of_unreachable(newBP)469// Where sum_of_unreachable(newBP) is what has been just changed.470// Finally:471// K == sum_of_reachable(newBP) / sum_of_reachable(oldBP) =>472// K == (1.0 - sum_of_unreachable(newBP)) / sum_of_reachable(oldBP)473BranchProbability NewUnreachableSum = BranchProbability::getZero();474for (auto I : UnreachableIdxs)475NewUnreachableSum += BP[I];476477BranchProbability NewReachableSum =478BranchProbability::getOne() - NewUnreachableSum;479480BranchProbability OldReachableSum = BranchProbability::getZero();481for (auto I : ReachableIdxs)482OldReachableSum += BP[I];483484if (OldReachableSum != NewReachableSum) { // Anything to dsitribute?485if (OldReachableSum.isZero()) {486// If all oldBP[i] are zeroes then the proportional distribution results487// in all zero probabilities and the error stays big. In this case we488// evenly spread NewReachableSum over the reachable edges.489BranchProbability PerEdge = NewReachableSum / ReachableIdxs.size();490for (auto I : ReachableIdxs)491BP[I] = PerEdge;492} else {493for (auto I : ReachableIdxs) {494// We use uint64_t to avoid double rounding error of the following495// calculation: BP[i] = BP[i] * NewReachableSum / OldReachableSum496// The formula is taken from the private constructor497// BranchProbability(uint32_t Numerator, uint32_t Denominator)498uint64_t Mul = static_cast<uint64_t>(NewReachableSum.getNumerator()) *499BP[I].getNumerator();500uint32_t Div = static_cast<uint32_t>(501divideNearest(Mul, OldReachableSum.getNumerator()));502BP[I] = BranchProbability::getRaw(Div);503}504}505}506507setEdgeProbability(BB, BP);508509return true;510}511512// Calculate Edge Weights using "Pointer Heuristics". Predict a comparison513// between two pointer or pointer and NULL will fail.514bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) {515const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());516if (!BI || !BI->isConditional())517return false;518519Value *Cond = BI->getCondition();520ICmpInst *CI = dyn_cast<ICmpInst>(Cond);521if (!CI || !CI->isEquality())522return false;523524Value *LHS = CI->getOperand(0);525526if (!LHS->getType()->isPointerTy())527return false;528529assert(CI->getOperand(1)->getType()->isPointerTy());530531auto Search = PointerTable.find(CI->getPredicate());532if (Search == PointerTable.end())533return false;534setEdgeProbability(BB, Search->second);535return true;536}537538// Compute the unlikely successors to the block BB in the loop L, specifically539// those that are unlikely because this is a loop, and add them to the540// UnlikelyBlocks set.541static void542computeUnlikelySuccessors(const BasicBlock *BB, Loop *L,543SmallPtrSetImpl<const BasicBlock*> &UnlikelyBlocks) {544// Sometimes in a loop we have a branch whose condition is made false by545// taking it. This is typically something like546// int n = 0;547// while (...) {548// if (++n >= MAX) {549// n = 0;550// }551// }552// In this sort of situation taking the branch means that at the very least it553// won't be taken again in the next iteration of the loop, so we should554// consider it less likely than a typical branch.555//556// We detect this by looking back through the graph of PHI nodes that sets the557// value that the condition depends on, and seeing if we can reach a successor558// block which can be determined to make the condition false.559//560// FIXME: We currently consider unlikely blocks to be half as likely as other561// blocks, but if we consider the example above the likelyhood is actually562// 1/MAX. We could therefore be more precise in how unlikely we consider563// blocks to be, but it would require more careful examination of the form564// of the comparison expression.565const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());566if (!BI || !BI->isConditional())567return;568569// Check if the branch is based on an instruction compared with a constant570CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());571if (!CI || !isa<Instruction>(CI->getOperand(0)) ||572!isa<Constant>(CI->getOperand(1)))573return;574575// Either the instruction must be a PHI, or a chain of operations involving576// constants that ends in a PHI which we can then collapse into a single value577// if the PHI value is known.578Instruction *CmpLHS = dyn_cast<Instruction>(CI->getOperand(0));579PHINode *CmpPHI = dyn_cast<PHINode>(CmpLHS);580Constant *CmpConst = dyn_cast<Constant>(CI->getOperand(1));581// Collect the instructions until we hit a PHI582SmallVector<BinaryOperator *, 1> InstChain;583while (!CmpPHI && CmpLHS && isa<BinaryOperator>(CmpLHS) &&584isa<Constant>(CmpLHS->getOperand(1))) {585// Stop if the chain extends outside of the loop586if (!L->contains(CmpLHS))587return;588InstChain.push_back(cast<BinaryOperator>(CmpLHS));589CmpLHS = dyn_cast<Instruction>(CmpLHS->getOperand(0));590if (CmpLHS)591CmpPHI = dyn_cast<PHINode>(CmpLHS);592}593if (!CmpPHI || !L->contains(CmpPHI))594return;595596// Trace the phi node to find all values that come from successors of BB597SmallPtrSet<PHINode*, 8> VisitedInsts;598SmallVector<PHINode*, 8> WorkList;599WorkList.push_back(CmpPHI);600VisitedInsts.insert(CmpPHI);601while (!WorkList.empty()) {602PHINode *P = WorkList.pop_back_val();603for (BasicBlock *B : P->blocks()) {604// Skip blocks that aren't part of the loop605if (!L->contains(B))606continue;607Value *V = P->getIncomingValueForBlock(B);608// If the source is a PHI add it to the work list if we haven't609// already visited it.610if (PHINode *PN = dyn_cast<PHINode>(V)) {611if (VisitedInsts.insert(PN).second)612WorkList.push_back(PN);613continue;614}615// If this incoming value is a constant and B is a successor of BB, then616// we can constant-evaluate the compare to see if it makes the branch be617// taken or not.618Constant *CmpLHSConst = dyn_cast<Constant>(V);619if (!CmpLHSConst || !llvm::is_contained(successors(BB), B))620continue;621// First collapse InstChain622const DataLayout &DL = BB->getDataLayout();623for (Instruction *I : llvm::reverse(InstChain)) {624CmpLHSConst = ConstantFoldBinaryOpOperands(625I->getOpcode(), CmpLHSConst, cast<Constant>(I->getOperand(1)), DL);626if (!CmpLHSConst)627break;628}629if (!CmpLHSConst)630continue;631// Now constant-evaluate the compare632Constant *Result = ConstantFoldCompareInstOperands(633CI->getPredicate(), CmpLHSConst, CmpConst, DL);634// If the result means we don't branch to the block then that block is635// unlikely.636if (Result &&637((Result->isZeroValue() && B == BI->getSuccessor(0)) ||638(Result->isOneValue() && B == BI->getSuccessor(1))))639UnlikelyBlocks.insert(B);640}641}642}643644std::optional<uint32_t>645BranchProbabilityInfo::getEstimatedBlockWeight(const BasicBlock *BB) const {646auto WeightIt = EstimatedBlockWeight.find(BB);647if (WeightIt == EstimatedBlockWeight.end())648return std::nullopt;649return WeightIt->second;650}651652std::optional<uint32_t>653BranchProbabilityInfo::getEstimatedLoopWeight(const LoopData &L) const {654auto WeightIt = EstimatedLoopWeight.find(L);655if (WeightIt == EstimatedLoopWeight.end())656return std::nullopt;657return WeightIt->second;658}659660std::optional<uint32_t>661BranchProbabilityInfo::getEstimatedEdgeWeight(const LoopEdge &Edge) const {662// For edges entering a loop take weight of a loop rather than an individual663// block in the loop.664return isLoopEnteringEdge(Edge)665? getEstimatedLoopWeight(Edge.second.getLoopData())666: getEstimatedBlockWeight(Edge.second.getBlock());667}668669template <class IterT>670std::optional<uint32_t> BranchProbabilityInfo::getMaxEstimatedEdgeWeight(671const LoopBlock &SrcLoopBB, iterator_range<IterT> Successors) const {672SmallVector<uint32_t, 4> Weights;673std::optional<uint32_t> MaxWeight;674for (const BasicBlock *DstBB : Successors) {675const LoopBlock DstLoopBB = getLoopBlock(DstBB);676auto Weight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});677678if (!Weight)679return std::nullopt;680681if (!MaxWeight || *MaxWeight < *Weight)682MaxWeight = Weight;683}684685return MaxWeight;686}687688// Updates \p LoopBB's weight and returns true. If \p LoopBB has already689// an associated weight it is unchanged and false is returned.690//691// Please note by the algorithm the weight is not expected to change once set692// thus 'false' status is used to track visited blocks.693bool BranchProbabilityInfo::updateEstimatedBlockWeight(694LoopBlock &LoopBB, uint32_t BBWeight,695SmallVectorImpl<BasicBlock *> &BlockWorkList,696SmallVectorImpl<LoopBlock> &LoopWorkList) {697BasicBlock *BB = LoopBB.getBlock();698699// In general, weight is assigned to a block when it has final value and700// can't/shouldn't be changed. However, there are cases when a block701// inherently has several (possibly "contradicting") weights. For example,702// "unwind" block may also contain "cold" call. In that case the first703// set weight is favored and all consequent weights are ignored.704if (!EstimatedBlockWeight.insert({BB, BBWeight}).second)705return false;706707for (BasicBlock *PredBlock : predecessors(BB)) {708LoopBlock PredLoop = getLoopBlock(PredBlock);709// Add affected block/loop to a working list.710if (isLoopExitingEdge({PredLoop, LoopBB})) {711if (!EstimatedLoopWeight.count(PredLoop.getLoopData()))712LoopWorkList.push_back(PredLoop);713} else if (!EstimatedBlockWeight.count(PredBlock))714BlockWorkList.push_back(PredBlock);715}716return true;717}718719// Starting from \p BB traverse through dominator blocks and assign \p BBWeight720// to all such blocks that are post dominated by \BB. In other words to all721// blocks that the one is executed if and only if another one is executed.722// Importantly, we skip loops here for two reasons. First weights of blocks in723// a loop should be scaled by trip count (yet possibly unknown). Second there is724// no any value in doing that because that doesn't give any additional725// information regarding distribution of probabilities inside the loop.726// Exception is loop 'enter' and 'exit' edges that are handled in a special way727// at calcEstimatedHeuristics.728//729// In addition, \p WorkList is populated with basic blocks if at leas one730// successor has updated estimated weight.731void BranchProbabilityInfo::propagateEstimatedBlockWeight(732const LoopBlock &LoopBB, DominatorTree *DT, PostDominatorTree *PDT,733uint32_t BBWeight, SmallVectorImpl<BasicBlock *> &BlockWorkList,734SmallVectorImpl<LoopBlock> &LoopWorkList) {735const BasicBlock *BB = LoopBB.getBlock();736const auto *DTStartNode = DT->getNode(BB);737const auto *PDTStartNode = PDT->getNode(BB);738739// TODO: Consider propagating weight down the domination line as well.740for (const auto *DTNode = DTStartNode; DTNode != nullptr;741DTNode = DTNode->getIDom()) {742auto *DomBB = DTNode->getBlock();743// Consider blocks which lie on one 'line'.744if (!PDT->dominates(PDTStartNode, PDT->getNode(DomBB)))745// If BB doesn't post dominate DomBB it will not post dominate dominators746// of DomBB as well.747break;748749LoopBlock DomLoopBB = getLoopBlock(DomBB);750const LoopEdge Edge{DomLoopBB, LoopBB};751// Don't propagate weight to blocks belonging to different loops.752if (!isLoopEnteringExitingEdge(Edge)) {753if (!updateEstimatedBlockWeight(DomLoopBB, BBWeight, BlockWorkList,754LoopWorkList))755// If DomBB has weight set then all it's predecessors are already756// processed (since we propagate weight up to the top of IR each time).757break;758} else if (isLoopExitingEdge(Edge)) {759LoopWorkList.push_back(DomLoopBB);760}761}762}763764std::optional<uint32_t>765BranchProbabilityInfo::getInitialEstimatedBlockWeight(const BasicBlock *BB) {766// Returns true if \p BB has call marked with "NoReturn" attribute.767auto hasNoReturn = [&](const BasicBlock *BB) {768for (const auto &I : reverse(*BB))769if (const CallInst *CI = dyn_cast<CallInst>(&I))770if (CI->hasFnAttr(Attribute::NoReturn))771return true;772773return false;774};775776// Important note regarding the order of checks. They are ordered by weight777// from lowest to highest. Doing that allows to avoid "unstable" results778// when several conditions heuristics can be applied simultaneously.779if (isa<UnreachableInst>(BB->getTerminator()) ||780// If this block is terminated by a call to781// @llvm.experimental.deoptimize then treat it like an unreachable782// since it is expected to practically never execute.783// TODO: Should we actually treat as never returning call?784BB->getTerminatingDeoptimizeCall())785return hasNoReturn(BB)786? static_cast<uint32_t>(BlockExecWeight::NORETURN)787: static_cast<uint32_t>(BlockExecWeight::UNREACHABLE);788789// Check if the block is an exception handling block.790if (BB->isEHPad())791return static_cast<uint32_t>(BlockExecWeight::UNWIND);792793// Check if the block contains 'cold' call.794for (const auto &I : *BB)795if (const CallInst *CI = dyn_cast<CallInst>(&I))796if (CI->hasFnAttr(Attribute::Cold))797return static_cast<uint32_t>(BlockExecWeight::COLD);798799return std::nullopt;800}801802// Does RPO traversal over all blocks in \p F and assigns weights to803// 'unreachable', 'noreturn', 'cold', 'unwind' blocks. In addition it does its804// best to propagate the weight to up/down the IR.805void BranchProbabilityInfo::computeEestimateBlockWeight(806const Function &F, DominatorTree *DT, PostDominatorTree *PDT) {807SmallVector<BasicBlock *, 8> BlockWorkList;808SmallVector<LoopBlock, 8> LoopWorkList;809SmallDenseMap<LoopData, SmallVector<BasicBlock *, 4>> LoopExitBlocks;810811// By doing RPO we make sure that all predecessors already have weights812// calculated before visiting theirs successors.813ReversePostOrderTraversal<const Function *> RPOT(&F);814for (const auto *BB : RPOT)815if (auto BBWeight = getInitialEstimatedBlockWeight(BB))816// If we were able to find estimated weight for the block set it to this817// block and propagate up the IR.818propagateEstimatedBlockWeight(getLoopBlock(BB), DT, PDT, *BBWeight,819BlockWorkList, LoopWorkList);820821// BlockWorklist/LoopWorkList contains blocks/loops with at least one822// successor/exit having estimated weight. Try to propagate weight to such823// blocks/loops from successors/exits.824// Process loops and blocks. Order is not important.825do {826while (!LoopWorkList.empty()) {827const LoopBlock LoopBB = LoopWorkList.pop_back_val();828const LoopData LD = LoopBB.getLoopData();829if (EstimatedLoopWeight.count(LD))830continue;831832auto Res = LoopExitBlocks.try_emplace(LD);833SmallVectorImpl<BasicBlock *> &Exits = Res.first->second;834if (Res.second)835getLoopExitBlocks(LoopBB, Exits);836auto LoopWeight = getMaxEstimatedEdgeWeight(837LoopBB, make_range(Exits.begin(), Exits.end()));838839if (LoopWeight) {840// If we never exit the loop then we can enter it once at maximum.841if (LoopWeight <= static_cast<uint32_t>(BlockExecWeight::UNREACHABLE))842LoopWeight = static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO);843844EstimatedLoopWeight.insert({LD, *LoopWeight});845// Add all blocks entering the loop into working list.846getLoopEnterBlocks(LoopBB, BlockWorkList);847}848}849850while (!BlockWorkList.empty()) {851// We can reach here only if BlockWorkList is not empty.852const BasicBlock *BB = BlockWorkList.pop_back_val();853if (EstimatedBlockWeight.count(BB))854continue;855856// We take maximum over all weights of successors. In other words we take857// weight of "hot" path. In theory we can probably find a better function858// which gives higher accuracy results (comparing to "maximum") but I859// can't860// think of any right now. And I doubt it will make any difference in861// practice.862const LoopBlock LoopBB = getLoopBlock(BB);863auto MaxWeight = getMaxEstimatedEdgeWeight(LoopBB, successors(BB));864865if (MaxWeight)866propagateEstimatedBlockWeight(LoopBB, DT, PDT, *MaxWeight,867BlockWorkList, LoopWorkList);868}869} while (!BlockWorkList.empty() || !LoopWorkList.empty());870}871872// Calculate edge probabilities based on block's estimated weight.873// Note that gathered weights were not scaled for loops. Thus edges entering874// and exiting loops requires special processing.875bool BranchProbabilityInfo::calcEstimatedHeuristics(const BasicBlock *BB) {876assert(BB->getTerminator()->getNumSuccessors() > 1 &&877"expected more than one successor!");878879const LoopBlock LoopBB = getLoopBlock(BB);880881SmallPtrSet<const BasicBlock *, 8> UnlikelyBlocks;882uint32_t TC = LBH_TAKEN_WEIGHT / LBH_NONTAKEN_WEIGHT;883if (LoopBB.getLoop())884computeUnlikelySuccessors(BB, LoopBB.getLoop(), UnlikelyBlocks);885886// Changed to 'true' if at least one successor has estimated weight.887bool FoundEstimatedWeight = false;888SmallVector<uint32_t, 4> SuccWeights;889uint64_t TotalWeight = 0;890// Go over all successors of BB and put their weights into SuccWeights.891for (const BasicBlock *SuccBB : successors(BB)) {892std::optional<uint32_t> Weight;893const LoopBlock SuccLoopBB = getLoopBlock(SuccBB);894const LoopEdge Edge{LoopBB, SuccLoopBB};895896Weight = getEstimatedEdgeWeight(Edge);897898if (isLoopExitingEdge(Edge) &&899// Avoid adjustment of ZERO weight since it should remain unchanged.900Weight != static_cast<uint32_t>(BlockExecWeight::ZERO)) {901// Scale down loop exiting weight by trip count.902Weight = std::max(903static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO),904Weight.value_or(static_cast<uint32_t>(BlockExecWeight::DEFAULT)) /905TC);906}907bool IsUnlikelyEdge = LoopBB.getLoop() && UnlikelyBlocks.contains(SuccBB);908if (IsUnlikelyEdge &&909// Avoid adjustment of ZERO weight since it should remain unchanged.910Weight != static_cast<uint32_t>(BlockExecWeight::ZERO)) {911// 'Unlikely' blocks have twice lower weight.912Weight = std::max(913static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO),914Weight.value_or(static_cast<uint32_t>(BlockExecWeight::DEFAULT)) / 2);915}916917if (Weight)918FoundEstimatedWeight = true;919920auto WeightVal =921Weight.value_or(static_cast<uint32_t>(BlockExecWeight::DEFAULT));922TotalWeight += WeightVal;923SuccWeights.push_back(WeightVal);924}925926// If non of blocks have estimated weight bail out.927// If TotalWeight is 0 that means weight of each successor is 0 as well and928// equally likely. Bail out early to not deal with devision by zero.929if (!FoundEstimatedWeight || TotalWeight == 0)930return false;931932assert(SuccWeights.size() == succ_size(BB) && "Missed successor?");933const unsigned SuccCount = SuccWeights.size();934935// If the sum of weights does not fit in 32 bits, scale every weight down936// accordingly.937if (TotalWeight > UINT32_MAX) {938uint64_t ScalingFactor = TotalWeight / UINT32_MAX + 1;939TotalWeight = 0;940for (unsigned Idx = 0; Idx < SuccCount; ++Idx) {941SuccWeights[Idx] /= ScalingFactor;942if (SuccWeights[Idx] == static_cast<uint32_t>(BlockExecWeight::ZERO))943SuccWeights[Idx] =944static_cast<uint32_t>(BlockExecWeight::LOWEST_NON_ZERO);945TotalWeight += SuccWeights[Idx];946}947assert(TotalWeight <= UINT32_MAX && "Total weight overflows");948}949950// Finally set probabilities to edges according to estimated block weights.951SmallVector<BranchProbability, 4> EdgeProbabilities(952SuccCount, BranchProbability::getUnknown());953954for (unsigned Idx = 0; Idx < SuccCount; ++Idx) {955EdgeProbabilities[Idx] =956BranchProbability(SuccWeights[Idx], (uint32_t)TotalWeight);957}958setEdgeProbability(BB, EdgeProbabilities);959return true;960}961962bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB,963const TargetLibraryInfo *TLI) {964const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());965if (!BI || !BI->isConditional())966return false;967968Value *Cond = BI->getCondition();969ICmpInst *CI = dyn_cast<ICmpInst>(Cond);970if (!CI)971return false;972973auto GetConstantInt = [](Value *V) {974if (auto *I = dyn_cast<BitCastInst>(V))975return dyn_cast<ConstantInt>(I->getOperand(0));976return dyn_cast<ConstantInt>(V);977};978979Value *RHS = CI->getOperand(1);980ConstantInt *CV = GetConstantInt(RHS);981if (!CV)982return false;983984// If the LHS is the result of AND'ing a value with a single bit bitmask,985// we don't have information about probabilities.986if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0)))987if (LHS->getOpcode() == Instruction::And)988if (ConstantInt *AndRHS = GetConstantInt(LHS->getOperand(1)))989if (AndRHS->getValue().isPowerOf2())990return false;991992// Check if the LHS is the return value of a library function993LibFunc Func = NumLibFuncs;994if (TLI)995if (CallInst *Call = dyn_cast<CallInst>(CI->getOperand(0)))996if (Function *CalledFn = Call->getCalledFunction())997TLI->getLibFunc(*CalledFn, Func);998999ProbabilityTable::const_iterator Search;1000if (Func == LibFunc_strcasecmp ||1001Func == LibFunc_strcmp ||1002Func == LibFunc_strncasecmp ||1003Func == LibFunc_strncmp ||1004Func == LibFunc_memcmp ||1005Func == LibFunc_bcmp) {1006Search = ICmpWithLibCallTable.find(CI->getPredicate());1007if (Search == ICmpWithLibCallTable.end())1008return false;1009} else if (CV->isZero()) {1010Search = ICmpWithZeroTable.find(CI->getPredicate());1011if (Search == ICmpWithZeroTable.end())1012return false;1013} else if (CV->isOne()) {1014Search = ICmpWithOneTable.find(CI->getPredicate());1015if (Search == ICmpWithOneTable.end())1016return false;1017} else if (CV->isMinusOne()) {1018Search = ICmpWithMinusOneTable.find(CI->getPredicate());1019if (Search == ICmpWithMinusOneTable.end())1020return false;1021} else {1022return false;1023}10241025setEdgeProbability(BB, Search->second);1026return true;1027}10281029bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) {1030const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());1031if (!BI || !BI->isConditional())1032return false;10331034Value *Cond = BI->getCondition();1035FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);1036if (!FCmp)1037return false;10381039ProbabilityList ProbList;1040if (FCmp->isEquality()) {1041ProbList = !FCmp->isTrueWhenEqual() ?1042// f1 == f2 -> Unlikely1043ProbabilityList({FPTakenProb, FPUntakenProb}) :1044// f1 != f2 -> Likely1045ProbabilityList({FPUntakenProb, FPTakenProb});1046} else {1047auto Search = FCmpTable.find(FCmp->getPredicate());1048if (Search == FCmpTable.end())1049return false;1050ProbList = Search->second;1051}10521053setEdgeProbability(BB, ProbList);1054return true;1055}10561057void BranchProbabilityInfo::releaseMemory() {1058Probs.clear();1059Handles.clear();1060}10611062bool BranchProbabilityInfo::invalidate(Function &, const PreservedAnalyses &PA,1063FunctionAnalysisManager::Invalidator &) {1064// Check whether the analysis, all analyses on functions, or the function's1065// CFG have been preserved.1066auto PAC = PA.getChecker<BranchProbabilityAnalysis>();1067return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||1068PAC.preservedSet<CFGAnalyses>());1069}10701071void BranchProbabilityInfo::print(raw_ostream &OS) const {1072OS << "---- Branch Probabilities ----\n";1073// We print the probabilities from the last function the analysis ran over,1074// or the function it is currently running over.1075assert(LastF && "Cannot print prior to running over a function");1076for (const auto &BI : *LastF) {1077for (const BasicBlock *Succ : successors(&BI))1078printEdgeProbability(OS << " ", &BI, Succ);1079}1080}10811082bool BranchProbabilityInfo::1083isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {1084// Hot probability is at least 4/5 = 80%1085// FIXME: Compare against a static "hot" BranchProbability.1086return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);1087}10881089/// Get the raw edge probability for the edge. If can't find it, return a1090/// default probability 1/N where N is the number of successors. Here an edge is1091/// specified using PredBlock and an1092/// index to the successors.1093BranchProbability1094BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,1095unsigned IndexInSuccessors) const {1096auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));1097assert((Probs.end() == Probs.find(std::make_pair(Src, 0))) ==1098(Probs.end() == I) &&1099"Probability for I-th successor must always be defined along with the "1100"probability for the first successor");11011102if (I != Probs.end())1103return I->second;11041105return {1, static_cast<uint32_t>(succ_size(Src))};1106}11071108BranchProbability1109BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,1110const_succ_iterator Dst) const {1111return getEdgeProbability(Src, Dst.getSuccessorIndex());1112}11131114/// Get the raw edge probability calculated for the block pair. This returns the1115/// sum of all raw edge probabilities from Src to Dst.1116BranchProbability1117BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,1118const BasicBlock *Dst) const {1119if (!Probs.count(std::make_pair(Src, 0)))1120return BranchProbability(llvm::count(successors(Src), Dst), succ_size(Src));11211122auto Prob = BranchProbability::getZero();1123for (const_succ_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I)1124if (*I == Dst)1125Prob += Probs.find(std::make_pair(Src, I.getSuccessorIndex()))->second;11261127return Prob;1128}11291130/// Set the edge probability for all edges at once.1131void BranchProbabilityInfo::setEdgeProbability(1132const BasicBlock *Src, const SmallVectorImpl<BranchProbability> &Probs) {1133assert(Src->getTerminator()->getNumSuccessors() == Probs.size());1134eraseBlock(Src); // Erase stale data if any.1135if (Probs.size() == 0)1136return; // Nothing to set.11371138Handles.insert(BasicBlockCallbackVH(Src, this));1139uint64_t TotalNumerator = 0;1140for (unsigned SuccIdx = 0; SuccIdx < Probs.size(); ++SuccIdx) {1141this->Probs[std::make_pair(Src, SuccIdx)] = Probs[SuccIdx];1142LLVM_DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << SuccIdx1143<< " successor probability to " << Probs[SuccIdx]1144<< "\n");1145TotalNumerator += Probs[SuccIdx].getNumerator();1146}11471148// Because of rounding errors the total probability cannot be checked to be1149// 1.0 exactly. That is TotalNumerator == BranchProbability::getDenominator.1150// Instead, every single probability in Probs must be as accurate as possible.1151// This results in error 1/denominator at most, thus the total absolute error1152// should be within Probs.size / BranchProbability::getDenominator.1153assert(TotalNumerator <= BranchProbability::getDenominator() + Probs.size());1154assert(TotalNumerator >= BranchProbability::getDenominator() - Probs.size());1155(void)TotalNumerator;1156}11571158void BranchProbabilityInfo::copyEdgeProbabilities(BasicBlock *Src,1159BasicBlock *Dst) {1160eraseBlock(Dst); // Erase stale data if any.1161unsigned NumSuccessors = Src->getTerminator()->getNumSuccessors();1162assert(NumSuccessors == Dst->getTerminator()->getNumSuccessors());1163if (NumSuccessors == 0)1164return; // Nothing to set.1165if (!this->Probs.contains(std::make_pair(Src, 0)))1166return; // No probability is set for edges from Src. Keep the same for Dst.11671168Handles.insert(BasicBlockCallbackVH(Dst, this));1169for (unsigned SuccIdx = 0; SuccIdx < NumSuccessors; ++SuccIdx) {1170auto Prob = this->Probs[std::make_pair(Src, SuccIdx)];1171this->Probs[std::make_pair(Dst, SuccIdx)] = Prob;1172LLVM_DEBUG(dbgs() << "set edge " << Dst->getName() << " -> " << SuccIdx1173<< " successor probability to " << Prob << "\n");1174}1175}11761177void BranchProbabilityInfo::swapSuccEdgesProbabilities(const BasicBlock *Src) {1178assert(Src->getTerminator()->getNumSuccessors() == 2);1179if (!Probs.contains(std::make_pair(Src, 0)))1180return; // No probability is set for edges from Src1181assert(Probs.contains(std::make_pair(Src, 1)));1182std::swap(Probs[std::make_pair(Src, 0)], Probs[std::make_pair(Src, 1)]);1183}11841185raw_ostream &1186BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,1187const BasicBlock *Src,1188const BasicBlock *Dst) const {1189const BranchProbability Prob = getEdgeProbability(Src, Dst);1190OS << "edge ";1191Src->printAsOperand(OS, false, Src->getModule());1192OS << " -> ";1193Dst->printAsOperand(OS, false, Dst->getModule());1194OS << " probability is " << Prob1195<< (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");11961197return OS;1198}11991200void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) {1201LLVM_DEBUG(dbgs() << "eraseBlock " << BB->getName() << "\n");12021203// Note that we cannot use successors of BB because the terminator of BB may1204// have changed when eraseBlock is called as a BasicBlockCallbackVH callback.1205// Instead we remove prob data for the block by iterating successors by their1206// indices from 0 till the last which exists. There could not be prob data for1207// a pair (BB, N) if there is no data for (BB, N-1) because the data is always1208// set for all successors from 0 to M at once by the method1209// setEdgeProbability().1210Handles.erase(BasicBlockCallbackVH(BB, this));1211for (unsigned I = 0;; ++I) {1212auto MapI = Probs.find(std::make_pair(BB, I));1213if (MapI == Probs.end()) {1214assert(Probs.count(std::make_pair(BB, I + 1)) == 0 &&1215"Must be no more successors");1216return;1217}1218Probs.erase(MapI);1219}1220}12211222void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LoopI,1223const TargetLibraryInfo *TLI,1224DominatorTree *DT,1225PostDominatorTree *PDT) {1226LLVM_DEBUG(dbgs() << "---- Branch Probability Info : " << F.getName()1227<< " ----\n\n");1228LastF = &F; // Store the last function we ran on for printing.1229LI = &LoopI;12301231SccI = std::make_unique<SccInfo>(F);12321233assert(EstimatedBlockWeight.empty());1234assert(EstimatedLoopWeight.empty());12351236std::unique_ptr<DominatorTree> DTPtr;1237std::unique_ptr<PostDominatorTree> PDTPtr;12381239if (!DT) {1240DTPtr = std::make_unique<DominatorTree>(const_cast<Function &>(F));1241DT = DTPtr.get();1242}12431244if (!PDT) {1245PDTPtr = std::make_unique<PostDominatorTree>(const_cast<Function &>(F));1246PDT = PDTPtr.get();1247}12481249computeEestimateBlockWeight(F, DT, PDT);12501251// Walk the basic blocks in post-order so that we can build up state about1252// the successors of a block iteratively.1253for (const auto *BB : post_order(&F.getEntryBlock())) {1254LLVM_DEBUG(dbgs() << "Computing probabilities for " << BB->getName()1255<< "\n");1256// If there is no at least two successors, no sense to set probability.1257if (BB->getTerminator()->getNumSuccessors() < 2)1258continue;1259if (calcMetadataWeights(BB))1260continue;1261if (calcEstimatedHeuristics(BB))1262continue;1263if (calcPointerHeuristics(BB))1264continue;1265if (calcZeroHeuristics(BB, TLI))1266continue;1267if (calcFloatingPointHeuristics(BB))1268continue;1269}12701271EstimatedLoopWeight.clear();1272EstimatedBlockWeight.clear();1273SccI.reset();12741275if (PrintBranchProb && (PrintBranchProbFuncName.empty() ||1276F.getName() == PrintBranchProbFuncName)) {1277print(dbgs());1278}1279}12801281void BranchProbabilityInfoWrapperPass::getAnalysisUsage(1282AnalysisUsage &AU) const {1283// We require DT so it's available when LI is available. The LI updating code1284// asserts that DT is also present so if we don't make sure that we have DT1285// here, that assert will trigger.1286AU.addRequired<DominatorTreeWrapperPass>();1287AU.addRequired<LoopInfoWrapperPass>();1288AU.addRequired<TargetLibraryInfoWrapperPass>();1289AU.addRequired<DominatorTreeWrapperPass>();1290AU.addRequired<PostDominatorTreeWrapperPass>();1291AU.setPreservesAll();1292}12931294bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) {1295const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();1296const TargetLibraryInfo &TLI =1297getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);1298DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();1299PostDominatorTree &PDT =1300getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();1301BPI.calculate(F, LI, &TLI, &DT, &PDT);1302return false;1303}13041305void BranchProbabilityInfoWrapperPass::releaseMemory() { BPI.releaseMemory(); }13061307void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS,1308const Module *) const {1309BPI.print(OS);1310}13111312AnalysisKey BranchProbabilityAnalysis::Key;1313BranchProbabilityInfo1314BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) {1315auto &LI = AM.getResult<LoopAnalysis>(F);1316auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);1317auto &DT = AM.getResult<DominatorTreeAnalysis>(F);1318auto &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);1319BranchProbabilityInfo BPI;1320BPI.calculate(F, LI, &TLI, &DT, &PDT);1321return BPI;1322}13231324PreservedAnalyses1325BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {1326OS << "Printing analysis 'Branch Probability Analysis' for function '"1327<< F.getName() << "':\n";1328AM.getResult<BranchProbabilityAnalysis>(F).print(OS);1329return PreservedAnalyses::all();1330}133113321333