1 //===- BranchProbabilityInfo.h - Branch Probability Analysis ----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass is used to evaluate branch probabilties. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H 14 #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H 15 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/ADT/DenseMapInfo.h" 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/IR/BasicBlock.h" 20 #include "llvm/IR/CFG.h" 21 #include "llvm/IR/PassManager.h" 22 #include "llvm/IR/ValueHandle.h" 23 #include "llvm/Pass.h" 24 #include "llvm/Support/BranchProbability.h" 25 #include "llvm/Support/Compiler.h" 26 #include <cassert> 27 #include <cstdint> 28 #include <memory> 29 #include <utility> 30 31 namespace llvm { 32 33 class Function; 34 class Loop; 35 class LoopInfo; 36 class raw_ostream; 37 class DominatorTree; 38 class PostDominatorTree; 39 class TargetLibraryInfo; 40 class Value; 41 42 /// Analysis providing branch probability information. 43 /// 44 /// This is a function analysis which provides information on the relative 45 /// probabilities of each "edge" in the function's CFG where such an edge is 46 /// defined by a pair (PredBlock and an index in the successors). The 47 /// probability of an edge from one block is always relative to the 48 /// probabilities of other edges from the block. The probabilites of all edges 49 /// from a block sum to exactly one (100%). 50 /// We use a pair (PredBlock and an index in the successors) to uniquely 51 /// identify an edge, since we can have multiple edges from Src to Dst. 52 /// As an example, we can have a switch which jumps to Dst with value 0 and 53 /// value 10. 54 /// 55 /// Process of computing branch probabilities can be logically viewed as three 56 /// step process: 57 /// 58 /// First, if there is a profile information associated with the branch then 59 /// it is trivially translated to branch probabilities. There is one exception 60 /// from this rule though. Probabilities for edges leading to "unreachable" 61 /// blocks (blocks with the estimated weight not greater than 62 /// UNREACHABLE_WEIGHT) are evaluated according to static estimation and 63 /// override profile information. If no branch probabilities were calculated 64 /// on this step then take the next one. 65 /// 66 /// Second, estimate absolute execution weights for each block based on 67 /// statically known information. Roots of such information are "cold", 68 /// "unreachable", "noreturn" and "unwind" blocks. Those blocks get their 69 /// weights set to BlockExecWeight::COLD, BlockExecWeight::UNREACHABLE, 70 /// BlockExecWeight::NORETURN and BlockExecWeight::UNWIND respectively. Then the 71 /// weights are propagated to the other blocks up the domination line. In 72 /// addition, if all successors have estimated weights set then maximum of these 73 /// weights assigned to the block itself (while this is not ideal heuristic in 74 /// theory it's simple and works reasonably well in most cases) and the process 75 /// repeats. Once the process of weights propagation converges branch 76 /// probabilities are set for all such branches that have at least one successor 77 /// with the weight set. Default execution weight (BlockExecWeight::DEFAULT) is 78 /// used for any successors which doesn't have its weight set. For loop back 79 /// branches we use their weights scaled by loop trip count equal to 80 /// 'LBH_TAKEN_WEIGHT/LBH_NOTTAKEN_WEIGHT'. 81 /// 82 /// Here is a simple example demonstrating how the described algorithm works. 83 /// 84 /// BB1 85 /// / \ 86 /// v v 87 /// BB2 BB3 88 /// / \ 89 /// v v 90 /// ColdBB UnreachBB 91 /// 92 /// Initially, ColdBB is associated with COLD_WEIGHT and UnreachBB with 93 /// UNREACHABLE_WEIGHT. COLD_WEIGHT is set to BB2 as maximum between its 94 /// successors. BB1 and BB3 has no explicit estimated weights and assumed to 95 /// have DEFAULT_WEIGHT. Based on assigned weights branches will have the 96 /// following probabilities: 97 /// P(BB1->BB2) = COLD_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) = 98 /// 0xffff / (0xffff + 0xfffff) = 0.0588(5.9%) 99 /// P(BB1->BB3) = DEFAULT_WEIGHT_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) = 100 /// 0xfffff / (0xffff + 0xfffff) = 0.941(94.1%) 101 /// P(BB2->ColdBB) = COLD_WEIGHT/(COLD_WEIGHT + UNREACHABLE_WEIGHT) = 1(100%) 102 /// P(BB2->UnreachBB) = 103 /// UNREACHABLE_WEIGHT/(COLD_WEIGHT+UNREACHABLE_WEIGHT) = 0(0%) 104 /// 105 /// If no branch probabilities were calculated on this step then take the next 106 /// one. 107 /// 108 /// Third, apply different kinds of local heuristics for each individual 109 /// branch until first match. For example probability of a pointer to be null is 110 /// estimated as PH_TAKEN_WEIGHT/(PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT). If 111 /// no local heuristic has been matched then branch is left with no explicit 112 /// probability set and assumed to have default probability. 113 class BranchProbabilityInfo { 114 public: 115 BranchProbabilityInfo() = default; 116 117 BranchProbabilityInfo(const Function &F, const LoopInfo &LI, 118 const TargetLibraryInfo *TLI = nullptr, 119 DominatorTree *DT = nullptr, 120 PostDominatorTree *PDT = nullptr) { 121 calculate(F, LI, TLI, DT, PDT); 122 } 123 BranchProbabilityInfo(BranchProbabilityInfo && Arg)124 BranchProbabilityInfo(BranchProbabilityInfo &&Arg) 125 : Handles(std::move(Arg.Handles)), Probs(std::move(Arg.Probs)), 126 LastF(Arg.LastF), 127 EstimatedBlockWeight(std::move(Arg.EstimatedBlockWeight)) { 128 for (auto &Handle : Handles) 129 Handle.setBPI(this); 130 } 131 132 BranchProbabilityInfo(const BranchProbabilityInfo &) = delete; 133 BranchProbabilityInfo &operator=(const BranchProbabilityInfo &) = delete; 134 135 BranchProbabilityInfo &operator=(BranchProbabilityInfo &&RHS) { 136 releaseMemory(); 137 Handles = std::move(RHS.Handles); 138 Probs = std::move(RHS.Probs); 139 EstimatedBlockWeight = std::move(RHS.EstimatedBlockWeight); 140 for (auto &Handle : Handles) 141 Handle.setBPI(this); 142 return *this; 143 } 144 145 LLVM_ABI bool invalidate(Function &, const PreservedAnalyses &PA, 146 FunctionAnalysisManager::Invalidator &); 147 148 LLVM_ABI void releaseMemory(); 149 150 LLVM_ABI void print(raw_ostream &OS) const; 151 152 /// Get an edge's probability, relative to other out-edges of the Src. 153 /// 154 /// This routine provides access to the fractional probability between zero 155 /// (0%) and one (100%) of this edge executing, relative to other edges 156 /// leaving the 'Src' block. The returned probability is never zero, and can 157 /// only be one if the source block has only one successor. 158 LLVM_ABI BranchProbability 159 getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const; 160 161 /// Get the probability of going from Src to Dst. 162 /// 163 /// It returns the sum of all probabilities for edges from Src to Dst. 164 LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, 165 const BasicBlock *Dst) const; 166 167 LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, 168 const_succ_iterator Dst) const; 169 170 /// Test if an edge is hot relative to other out-edges of the Src. 171 /// 172 /// Check whether this edge out of the source block is 'hot'. We define hot 173 /// as having a relative probability > 80%. 174 LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const; 175 176 /// Print an edge's probability. 177 /// 178 /// Retrieves an edge's probability similarly to \see getEdgeProbability, but 179 /// then prints that probability to the provided stream. That stream is then 180 /// returned. 181 LLVM_ABI raw_ostream &printEdgeProbability(raw_ostream &OS, 182 const BasicBlock *Src, 183 const BasicBlock *Dst) const; 184 185 public: 186 /// Set the raw probabilities for all edges from the given block. 187 /// 188 /// This allows a pass to explicitly set edge probabilities for a block. It 189 /// can be used when updating the CFG to update the branch probability 190 /// information. 191 LLVM_ABI void 192 setEdgeProbability(const BasicBlock *Src, 193 const SmallVectorImpl<BranchProbability> &Probs); 194 195 /// Copy outgoing edge probabilities from \p Src to \p Dst. 196 /// 197 /// This allows to keep probabilities unset for the destination if they were 198 /// unset for source. 199 LLVM_ABI void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst); 200 201 /// Swap outgoing edges probabilities for \p Src with branch terminator 202 LLVM_ABI void swapSuccEdgesProbabilities(const BasicBlock *Src); 203 getBranchProbStackProtector(bool IsLikely)204 static BranchProbability getBranchProbStackProtector(bool IsLikely) { 205 static const BranchProbability LikelyProb((1u << 20) - 1, 1u << 20); 206 return IsLikely ? LikelyProb : LikelyProb.getCompl(); 207 } 208 209 LLVM_ABI void calculate(const Function &F, const LoopInfo &LI, 210 const TargetLibraryInfo *TLI, DominatorTree *DT, 211 PostDominatorTree *PDT); 212 213 /// Forget analysis results for the given basic block. 214 LLVM_ABI void eraseBlock(const BasicBlock *BB); 215 216 // Data structure to track SCCs for handling irreducible loops. 217 class SccInfo { 218 // Enum of types to classify basic blocks in SCC. Basic block belonging to 219 // SCC is 'Inner' until it is either 'Header' or 'Exiting'. Note that a 220 // basic block can be 'Header' and 'Exiting' at the same time. 221 enum SccBlockType { 222 Inner = 0x0, 223 Header = 0x1, 224 Exiting = 0x2, 225 }; 226 // Map of basic blocks to SCC IDs they belong to. If basic block doesn't 227 // belong to any SCC it is not in the map. 228 using SccMap = DenseMap<const BasicBlock *, int>; 229 // Each basic block in SCC is attributed with one or several types from 230 // SccBlockType. Map value has uint32_t type (instead of SccBlockType) 231 // since basic block may be for example "Header" and "Exiting" at the same 232 // time and we need to be able to keep more than one value from 233 // SccBlockType. 234 using SccBlockTypeMap = DenseMap<const BasicBlock *, uint32_t>; 235 // Vector containing classification of basic blocks for all SCCs where i'th 236 // vector element corresponds to SCC with ID equal to i. 237 using SccBlockTypeMaps = std::vector<SccBlockTypeMap>; 238 239 SccMap SccNums; 240 SccBlockTypeMaps SccBlocks; 241 242 public: 243 LLVM_ABI explicit SccInfo(const Function &F); 244 245 /// If \p BB belongs to some SCC then ID of that SCC is returned, otherwise 246 /// -1 is returned. If \p BB belongs to more than one SCC at the same time 247 /// result is undefined. 248 LLVM_ABI int getSCCNum(const BasicBlock *BB) const; 249 /// Returns true if \p BB is a 'header' block in SCC with \p SccNum ID, 250 /// false otherwise. isSCCHeader(const BasicBlock * BB,int SccNum)251 bool isSCCHeader(const BasicBlock *BB, int SccNum) const { 252 return getSccBlockType(BB, SccNum) & Header; 253 } 254 /// Returns true if \p BB is an 'exiting' block in SCC with \p SccNum ID, 255 /// false otherwise. isSCCExitingBlock(const BasicBlock * BB,int SccNum)256 bool isSCCExitingBlock(const BasicBlock *BB, int SccNum) const { 257 return getSccBlockType(BB, SccNum) & Exiting; 258 } 259 /// Fills in \p Enters vector with all such blocks that don't belong to 260 /// SCC with \p SccNum ID but there is an edge to a block belonging to the 261 /// SCC. 262 LLVM_ABI void 263 getSccEnterBlocks(int SccNum, SmallVectorImpl<BasicBlock *> &Enters) const; 264 /// Fills in \p Exits vector with all such blocks that don't belong to 265 /// SCC with \p SccNum ID but there is an edge from a block belonging to the 266 /// SCC. 267 LLVM_ABI void getSccExitBlocks(int SccNum, 268 SmallVectorImpl<BasicBlock *> &Exits) const; 269 270 private: 271 /// Returns \p BB's type according to classification given by SccBlockType 272 /// enum. Please note that \p BB must belong to SSC with \p SccNum ID. 273 LLVM_ABI uint32_t getSccBlockType(const BasicBlock *BB, int SccNum) const; 274 /// Calculates \p BB's type and stores it in internal data structures for 275 /// future use. Please note that \p BB must belong to SSC with \p SccNum ID. 276 void calculateSccBlockType(const BasicBlock *BB, int SccNum); 277 }; 278 279 private: 280 // We need to store CallbackVH's in order to correctly handle basic block 281 // removal. 282 class BasicBlockCallbackVH final : public CallbackVH { 283 BranchProbabilityInfo *BPI; 284 deleted()285 void deleted() override { 286 assert(BPI != nullptr); 287 BPI->eraseBlock(cast<BasicBlock>(getValPtr())); 288 } 289 290 public: setBPI(BranchProbabilityInfo * BPI)291 void setBPI(BranchProbabilityInfo *BPI) { this->BPI = BPI; } 292 293 BasicBlockCallbackVH(const Value *V, BranchProbabilityInfo *BPI = nullptr) CallbackVH(const_cast<Value * > (V))294 : CallbackVH(const_cast<Value *>(V)), BPI(BPI) {} 295 }; 296 297 /// Pair of Loop and SCC ID number. Used to unify handling of normal and 298 /// SCC based loop representations. 299 using LoopData = std::pair<Loop *, int>; 300 /// Helper class to keep basic block along with its loop data information. 301 class LoopBlock { 302 public: 303 LLVM_ABI explicit LoopBlock(const BasicBlock *BB, const LoopInfo &LI, 304 const SccInfo &SccI); 305 getBlock()306 const BasicBlock *getBlock() const { return BB; } getBlock()307 BasicBlock *getBlock() { return const_cast<BasicBlock *>(BB); } getLoopData()308 LoopData getLoopData() const { return LD; } getLoop()309 Loop *getLoop() const { return LD.first; } getSccNum()310 int getSccNum() const { return LD.second; } 311 belongsToLoop()312 bool belongsToLoop() const { return getLoop() || getSccNum() != -1; } belongsToSameLoop(const LoopBlock & LB)313 bool belongsToSameLoop(const LoopBlock &LB) const { 314 return (LB.getLoop() && getLoop() == LB.getLoop()) || 315 (LB.getSccNum() != -1 && getSccNum() == LB.getSccNum()); 316 } 317 318 private: 319 const BasicBlock *const BB = nullptr; 320 LoopData LD = {nullptr, -1}; 321 }; 322 323 // Pair of LoopBlocks representing an edge from first to second block. 324 using LoopEdge = std::pair<const LoopBlock &, const LoopBlock &>; 325 326 DenseSet<BasicBlockCallbackVH, DenseMapInfo<Value*>> Handles; 327 328 // Since we allow duplicate edges from one basic block to another, we use 329 // a pair (PredBlock and an index in the successors) to specify an edge. 330 using Edge = std::pair<const BasicBlock *, unsigned>; 331 332 DenseMap<Edge, BranchProbability> Probs; 333 334 /// Track the last function we run over for printing. 335 const Function *LastF = nullptr; 336 337 const LoopInfo *LI = nullptr; 338 339 /// Keeps information about all SCCs in a function. 340 std::unique_ptr<const SccInfo> SccI; 341 342 /// Keeps mapping of a basic block to its estimated weight. 343 SmallDenseMap<const BasicBlock *, uint32_t> EstimatedBlockWeight; 344 345 /// Keeps mapping of a loop to estimated weight to enter the loop. 346 SmallDenseMap<LoopData, uint32_t> EstimatedLoopWeight; 347 348 /// Helper to construct LoopBlock for \p BB. getLoopBlock(const BasicBlock * BB)349 LoopBlock getLoopBlock(const BasicBlock *BB) const { 350 return LoopBlock(BB, *LI, *SccI); 351 } 352 353 /// Returns true if destination block belongs to some loop and source block is 354 /// either doesn't belong to any loop or belongs to a loop which is not inner 355 /// relative to the destination block. 356 bool isLoopEnteringEdge(const LoopEdge &Edge) const; 357 /// Returns true if source block belongs to some loop and destination block is 358 /// either doesn't belong to any loop or belongs to a loop which is not inner 359 /// relative to the source block. 360 bool isLoopExitingEdge(const LoopEdge &Edge) const; 361 /// Returns true if \p Edge is either enters to or exits from some loop, false 362 /// in all other cases. 363 bool isLoopEnteringExitingEdge(const LoopEdge &Edge) const; 364 /// Returns true if source and destination blocks belongs to the same loop and 365 /// destination block is loop header. 366 bool isLoopBackEdge(const LoopEdge &Edge) const; 367 // Fills in \p Enters vector with all "enter" blocks to a loop \LB belongs to. 368 void getLoopEnterBlocks(const LoopBlock &LB, 369 SmallVectorImpl<BasicBlock *> &Enters) const; 370 // Fills in \p Exits vector with all "exit" blocks from a loop \LB belongs to. 371 void getLoopExitBlocks(const LoopBlock &LB, 372 SmallVectorImpl<BasicBlock *> &Exits) const; 373 374 /// Returns estimated weight for \p BB. std::nullopt if \p BB has no estimated 375 /// weight. 376 std::optional<uint32_t> getEstimatedBlockWeight(const BasicBlock *BB) const; 377 378 /// Returns estimated weight to enter \p L. In other words it is weight of 379 /// loop's header block not scaled by trip count. Returns std::nullopt if \p L 380 /// has no no estimated weight. 381 std::optional<uint32_t> getEstimatedLoopWeight(const LoopData &L) const; 382 383 /// Return estimated weight for \p Edge. Returns std::nullopt if estimated 384 /// weight is unknown. 385 std::optional<uint32_t> getEstimatedEdgeWeight(const LoopEdge &Edge) const; 386 387 /// Iterates over all edges leading from \p SrcBB to \p Successors and 388 /// returns maximum of all estimated weights. If at least one edge has unknown 389 /// estimated weight std::nullopt is returned. 390 template <class IterT> 391 std::optional<uint32_t> 392 getMaxEstimatedEdgeWeight(const LoopBlock &SrcBB, 393 iterator_range<IterT> Successors) const; 394 395 /// If \p LoopBB has no estimated weight then set it to \p BBWeight and 396 /// return true. Otherwise \p BB's weight remains unchanged and false is 397 /// returned. In addition all blocks/loops that might need their weight to be 398 /// re-estimated are put into BlockWorkList/LoopWorkList. 399 bool updateEstimatedBlockWeight(LoopBlock &LoopBB, uint32_t BBWeight, 400 SmallVectorImpl<BasicBlock *> &BlockWorkList, 401 SmallVectorImpl<LoopBlock> &LoopWorkList); 402 403 /// Starting from \p LoopBB (including \p LoopBB itself) propagate \p BBWeight 404 /// up the domination tree. 405 void propagateEstimatedBlockWeight(const LoopBlock &LoopBB, DominatorTree *DT, 406 PostDominatorTree *PDT, uint32_t BBWeight, 407 SmallVectorImpl<BasicBlock *> &WorkList, 408 SmallVectorImpl<LoopBlock> &LoopWorkList); 409 410 /// Returns block's weight encoded in the IR. 411 std::optional<uint32_t> getInitialEstimatedBlockWeight(const BasicBlock *BB); 412 413 // Computes estimated weights for all blocks in \p F. 414 void estimateBlockWeights(const Function &F, DominatorTree *DT, 415 PostDominatorTree *PDT); 416 417 /// Based on computed weights by \p computeEstimatedBlockWeight set 418 /// probabilities on branches. 419 bool calcEstimatedHeuristics(const BasicBlock *BB); 420 bool calcMetadataWeights(const BasicBlock *BB); 421 bool calcPointerHeuristics(const BasicBlock *BB); 422 bool calcZeroHeuristics(const BasicBlock *BB, const TargetLibraryInfo *TLI); 423 bool calcFloatingPointHeuristics(const BasicBlock *BB); 424 }; 425 426 /// Analysis pass which computes \c BranchProbabilityInfo. 427 class BranchProbabilityAnalysis 428 : public AnalysisInfoMixin<BranchProbabilityAnalysis> { 429 friend AnalysisInfoMixin<BranchProbabilityAnalysis>; 430 431 LLVM_ABI static AnalysisKey Key; 432 433 public: 434 /// Provide the result type for this analysis pass. 435 using Result = BranchProbabilityInfo; 436 437 /// Run the analysis pass over a function and produce BPI. 438 LLVM_ABI BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM); 439 }; 440 441 /// Printer pass for the \c BranchProbabilityAnalysis results. 442 class BranchProbabilityPrinterPass 443 : public PassInfoMixin<BranchProbabilityPrinterPass> { 444 raw_ostream &OS; 445 446 public: BranchProbabilityPrinterPass(raw_ostream & OS)447 explicit BranchProbabilityPrinterPass(raw_ostream &OS) : OS(OS) {} 448 449 LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 450 isRequired()451 static bool isRequired() { return true; } 452 }; 453 454 /// Legacy analysis pass which computes \c BranchProbabilityInfo. 455 class LLVM_ABI BranchProbabilityInfoWrapperPass : public FunctionPass { 456 BranchProbabilityInfo BPI; 457 458 public: 459 static char ID; 460 461 BranchProbabilityInfoWrapperPass(); 462 getBPI()463 BranchProbabilityInfo &getBPI() { return BPI; } getBPI()464 const BranchProbabilityInfo &getBPI() const { return BPI; } 465 466 void getAnalysisUsage(AnalysisUsage &AU) const override; 467 bool runOnFunction(Function &F) override; 468 void releaseMemory() override; 469 void print(raw_ostream &OS, const Module *M = nullptr) const override; 470 }; 471 472 } // end namespace llvm 473 474 #endif // LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H 475