//===- BranchProbabilityInfo.h - Branch Probability Analysis ----*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This pass is used to evaluate branch probabilties. // //===----------------------------------------------------------------------===// #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/DenseSet.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/CFG.h" #include "llvm/IR/PassManager.h" #include "llvm/IR/ValueHandle.h" #include "llvm/Pass.h" #include "llvm/Support/BranchProbability.h" #include <cassert> #include <cstdint> #include <memory> #include <utility> namespace llvm { class Function; class Loop; class LoopInfo; class raw_ostream; class DominatorTree; class PostDominatorTree; class TargetLibraryInfo; class Value; /// Analysis providing branch probability information. /// /// This is a function analysis which provides information on the relative /// probabilities of each "edge" in the function's CFG where such an edge is /// defined by a pair (PredBlock and an index in the successors). The /// probability of an edge from one block is always relative to the /// probabilities of other edges from the block. The probabilites of all edges /// from a block sum to exactly one (100%). /// We use a pair (PredBlock and an index in the successors) to uniquely /// identify an edge, since we can have multiple edges from Src to Dst. /// As an example, we can have a switch which jumps to Dst with value 0 and /// value 10. /// /// Process of computing branch probabilities can be logically viewed as three /// step process: /// /// First, if there is a profile information associated with the branch then /// it is trivially translated to branch probabilities. There is one exception /// from this rule though. Probabilities for edges leading to "unreachable" /// blocks (blocks with the estimated weight not greater than /// UNREACHABLE_WEIGHT) are evaluated according to static estimation and /// override profile information. If no branch probabilities were calculated /// on this step then take the next one. /// /// Second, estimate absolute execution weights for each block based on /// statically known information. Roots of such information are "cold", /// "unreachable", "noreturn" and "unwind" blocks. Those blocks get their /// weights set to BlockExecWeight::COLD, BlockExecWeight::UNREACHABLE, /// BlockExecWeight::NORETURN and BlockExecWeight::UNWIND respectively. Then the /// weights are propagated to the other blocks up the domination line. In /// addition, if all successors have estimated weights set then maximum of these /// weights assigned to the block itself (while this is not ideal heuristic in /// theory it's simple and works reasonably well in most cases) and the process /// repeats. Once the process of weights propagation converges branch /// probabilities are set for all such branches that have at least one successor /// with the weight set. Default execution weight (BlockExecWeight::DEFAULT) is /// used for any successors which doesn't have its weight set. For loop back /// branches we use their weights scaled by loop trip count equal to /// 'LBH_TAKEN_WEIGHT/LBH_NOTTAKEN_WEIGHT'. /// /// Here is a simple example demonstrating how the described algorithm works. /// /// BB1 /// / \ /// v v /// BB2 BB3 /// / \ /// v v /// ColdBB UnreachBB /// /// Initially, ColdBB is associated with COLD_WEIGHT and UnreachBB with /// UNREACHABLE_WEIGHT. COLD_WEIGHT is set to BB2 as maximum between its /// successors. BB1 and BB3 has no explicit estimated weights and assumed to /// have DEFAULT_WEIGHT. Based on assigned weights branches will have the /// following probabilities: /// P(BB1->BB2) = COLD_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) = /// 0xffff / (0xffff + 0xfffff) = 0.0588(5.9%) /// P(BB1->BB3) = DEFAULT_WEIGHT_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) = /// 0xfffff / (0xffff + 0xfffff) = 0.941(94.1%) /// P(BB2->ColdBB) = COLD_WEIGHT/(COLD_WEIGHT + UNREACHABLE_WEIGHT) = 1(100%) /// P(BB2->UnreachBB) = /// UNREACHABLE_WEIGHT/(COLD_WEIGHT+UNREACHABLE_WEIGHT) = 0(0%) /// /// If no branch probabilities were calculated on this step then take the next /// one. /// /// Third, apply different kinds of local heuristics for each individual /// branch until first match. For example probability of a pointer to be null is /// estimated as PH_TAKEN_WEIGHT/(PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT). If /// no local heuristic has been matched then branch is left with no explicit /// probability set and assumed to have default probability. class BranchProbabilityInfo { … }; /// Analysis pass which computes \c BranchProbabilityInfo. class BranchProbabilityAnalysis : public AnalysisInfoMixin<BranchProbabilityAnalysis> { … }; /// Printer pass for the \c BranchProbabilityAnalysis results. class BranchProbabilityPrinterPass : public PassInfoMixin<BranchProbabilityPrinterPass> { … }; /// Legacy analysis pass which computes \c BranchProbabilityInfo. class BranchProbabilityInfoWrapperPass : public FunctionPass { … }; } // end namespace llvm #endif // LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H