|
|
|||
File indexing completed on 2026-05-10 08:43:11
0001 //===- BranchProbabilityInfo.h - Branch Probability Analysis ----*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This pass is used to evaluate branch probabilties. 0010 // 0011 //===----------------------------------------------------------------------===// 0012 0013 #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H 0014 #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H 0015 0016 #include "llvm/ADT/DenseMap.h" 0017 #include "llvm/ADT/DenseMapInfo.h" 0018 #include "llvm/ADT/DenseSet.h" 0019 #include "llvm/IR/BasicBlock.h" 0020 #include "llvm/IR/CFG.h" 0021 #include "llvm/IR/PassManager.h" 0022 #include "llvm/IR/ValueHandle.h" 0023 #include "llvm/Pass.h" 0024 #include "llvm/Support/BranchProbability.h" 0025 #include <cassert> 0026 #include <cstdint> 0027 #include <memory> 0028 #include <utility> 0029 0030 namespace llvm { 0031 0032 class Function; 0033 class Loop; 0034 class LoopInfo; 0035 class raw_ostream; 0036 class DominatorTree; 0037 class PostDominatorTree; 0038 class TargetLibraryInfo; 0039 class Value; 0040 0041 /// Analysis providing branch probability information. 0042 /// 0043 /// This is a function analysis which provides information on the relative 0044 /// probabilities of each "edge" in the function's CFG where such an edge is 0045 /// defined by a pair (PredBlock and an index in the successors). The 0046 /// probability of an edge from one block is always relative to the 0047 /// probabilities of other edges from the block. The probabilites of all edges 0048 /// from a block sum to exactly one (100%). 0049 /// We use a pair (PredBlock and an index in the successors) to uniquely 0050 /// identify an edge, since we can have multiple edges from Src to Dst. 0051 /// As an example, we can have a switch which jumps to Dst with value 0 and 0052 /// value 10. 0053 /// 0054 /// Process of computing branch probabilities can be logically viewed as three 0055 /// step process: 0056 /// 0057 /// First, if there is a profile information associated with the branch then 0058 /// it is trivially translated to branch probabilities. There is one exception 0059 /// from this rule though. Probabilities for edges leading to "unreachable" 0060 /// blocks (blocks with the estimated weight not greater than 0061 /// UNREACHABLE_WEIGHT) are evaluated according to static estimation and 0062 /// override profile information. If no branch probabilities were calculated 0063 /// on this step then take the next one. 0064 /// 0065 /// Second, estimate absolute execution weights for each block based on 0066 /// statically known information. Roots of such information are "cold", 0067 /// "unreachable", "noreturn" and "unwind" blocks. Those blocks get their 0068 /// weights set to BlockExecWeight::COLD, BlockExecWeight::UNREACHABLE, 0069 /// BlockExecWeight::NORETURN and BlockExecWeight::UNWIND respectively. Then the 0070 /// weights are propagated to the other blocks up the domination line. In 0071 /// addition, if all successors have estimated weights set then maximum of these 0072 /// weights assigned to the block itself (while this is not ideal heuristic in 0073 /// theory it's simple and works reasonably well in most cases) and the process 0074 /// repeats. Once the process of weights propagation converges branch 0075 /// probabilities are set for all such branches that have at least one successor 0076 /// with the weight set. Default execution weight (BlockExecWeight::DEFAULT) is 0077 /// used for any successors which doesn't have its weight set. For loop back 0078 /// branches we use their weights scaled by loop trip count equal to 0079 /// 'LBH_TAKEN_WEIGHT/LBH_NOTTAKEN_WEIGHT'. 0080 /// 0081 /// Here is a simple example demonstrating how the described algorithm works. 0082 /// 0083 /// BB1 0084 /// / \ 0085 /// v v 0086 /// BB2 BB3 0087 /// / \ 0088 /// v v 0089 /// ColdBB UnreachBB 0090 /// 0091 /// Initially, ColdBB is associated with COLD_WEIGHT and UnreachBB with 0092 /// UNREACHABLE_WEIGHT. COLD_WEIGHT is set to BB2 as maximum between its 0093 /// successors. BB1 and BB3 has no explicit estimated weights and assumed to 0094 /// have DEFAULT_WEIGHT. Based on assigned weights branches will have the 0095 /// following probabilities: 0096 /// P(BB1->BB2) = COLD_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) = 0097 /// 0xffff / (0xffff + 0xfffff) = 0.0588(5.9%) 0098 /// P(BB1->BB3) = DEFAULT_WEIGHT_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) = 0099 /// 0xfffff / (0xffff + 0xfffff) = 0.941(94.1%) 0100 /// P(BB2->ColdBB) = COLD_WEIGHT/(COLD_WEIGHT + UNREACHABLE_WEIGHT) = 1(100%) 0101 /// P(BB2->UnreachBB) = 0102 /// UNREACHABLE_WEIGHT/(COLD_WEIGHT+UNREACHABLE_WEIGHT) = 0(0%) 0103 /// 0104 /// If no branch probabilities were calculated on this step then take the next 0105 /// one. 0106 /// 0107 /// Third, apply different kinds of local heuristics for each individual 0108 /// branch until first match. For example probability of a pointer to be null is 0109 /// estimated as PH_TAKEN_WEIGHT/(PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT). If 0110 /// no local heuristic has been matched then branch is left with no explicit 0111 /// probability set and assumed to have default probability. 0112 class BranchProbabilityInfo { 0113 public: 0114 BranchProbabilityInfo() = default; 0115 0116 BranchProbabilityInfo(const Function &F, const LoopInfo &LI, 0117 const TargetLibraryInfo *TLI = nullptr, 0118 DominatorTree *DT = nullptr, 0119 PostDominatorTree *PDT = nullptr) { 0120 calculate(F, LI, TLI, DT, PDT); 0121 } 0122 0123 BranchProbabilityInfo(BranchProbabilityInfo &&Arg) 0124 : Handles(std::move(Arg.Handles)), Probs(std::move(Arg.Probs)), 0125 LastF(Arg.LastF), 0126 EstimatedBlockWeight(std::move(Arg.EstimatedBlockWeight)) { 0127 for (auto &Handle : Handles) 0128 Handle.setBPI(this); 0129 } 0130 0131 BranchProbabilityInfo(const BranchProbabilityInfo &) = delete; 0132 BranchProbabilityInfo &operator=(const BranchProbabilityInfo &) = delete; 0133 0134 BranchProbabilityInfo &operator=(BranchProbabilityInfo &&RHS) { 0135 releaseMemory(); 0136 Handles = std::move(RHS.Handles); 0137 Probs = std::move(RHS.Probs); 0138 EstimatedBlockWeight = std::move(RHS.EstimatedBlockWeight); 0139 for (auto &Handle : Handles) 0140 Handle.setBPI(this); 0141 return *this; 0142 } 0143 0144 bool invalidate(Function &, const PreservedAnalyses &PA, 0145 FunctionAnalysisManager::Invalidator &); 0146 0147 void releaseMemory(); 0148 0149 void print(raw_ostream &OS) const; 0150 0151 /// Get an edge's probability, relative to other out-edges of the Src. 0152 /// 0153 /// This routine provides access to the fractional probability between zero 0154 /// (0%) and one (100%) of this edge executing, relative to other edges 0155 /// leaving the 'Src' block. The returned probability is never zero, and can 0156 /// only be one if the source block has only one successor. 0157 BranchProbability getEdgeProbability(const BasicBlock *Src, 0158 unsigned IndexInSuccessors) const; 0159 0160 /// Get the probability of going from Src to Dst. 0161 /// 0162 /// It returns the sum of all probabilities for edges from Src to Dst. 0163 BranchProbability getEdgeProbability(const BasicBlock *Src, 0164 const BasicBlock *Dst) const; 0165 0166 BranchProbability getEdgeProbability(const BasicBlock *Src, 0167 const_succ_iterator Dst) const; 0168 0169 /// Test if an edge is hot relative to other out-edges of the Src. 0170 /// 0171 /// Check whether this edge out of the source block is 'hot'. We define hot 0172 /// as having a relative probability > 80%. 0173 bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const; 0174 0175 /// Print an edge's probability. 0176 /// 0177 /// Retrieves an edge's probability similarly to \see getEdgeProbability, but 0178 /// then prints that probability to the provided stream. That stream is then 0179 /// returned. 0180 raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src, 0181 const BasicBlock *Dst) const; 0182 0183 public: 0184 /// Set the raw probabilities for all edges from the given block. 0185 /// 0186 /// This allows a pass to explicitly set edge probabilities for a block. It 0187 /// can be used when updating the CFG to update the branch probability 0188 /// information. 0189 void setEdgeProbability(const BasicBlock *Src, 0190 const SmallVectorImpl<BranchProbability> &Probs); 0191 0192 /// Copy outgoing edge probabilities from \p Src to \p Dst. 0193 /// 0194 /// This allows to keep probabilities unset for the destination if they were 0195 /// unset for source. 0196 void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst); 0197 0198 /// Swap outgoing edges probabilities for \p Src with branch terminator 0199 void swapSuccEdgesProbabilities(const BasicBlock *Src); 0200 0201 static BranchProbability getBranchProbStackProtector(bool IsLikely) { 0202 static const BranchProbability LikelyProb((1u << 20) - 1, 1u << 20); 0203 return IsLikely ? LikelyProb : LikelyProb.getCompl(); 0204 } 0205 0206 void calculate(const Function &F, const LoopInfo &LI, 0207 const TargetLibraryInfo *TLI, DominatorTree *DT, 0208 PostDominatorTree *PDT); 0209 0210 /// Forget analysis results for the given basic block. 0211 void eraseBlock(const BasicBlock *BB); 0212 0213 // Data structure to track SCCs for handling irreducible loops. 0214 class SccInfo { 0215 // Enum of types to classify basic blocks in SCC. Basic block belonging to 0216 // SCC is 'Inner' until it is either 'Header' or 'Exiting'. Note that a 0217 // basic block can be 'Header' and 'Exiting' at the same time. 0218 enum SccBlockType { 0219 Inner = 0x0, 0220 Header = 0x1, 0221 Exiting = 0x2, 0222 }; 0223 // Map of basic blocks to SCC IDs they belong to. If basic block doesn't 0224 // belong to any SCC it is not in the map. 0225 using SccMap = DenseMap<const BasicBlock *, int>; 0226 // Each basic block in SCC is attributed with one or several types from 0227 // SccBlockType. Map value has uint32_t type (instead of SccBlockType) 0228 // since basic block may be for example "Header" and "Exiting" at the same 0229 // time and we need to be able to keep more than one value from 0230 // SccBlockType. 0231 using SccBlockTypeMap = DenseMap<const BasicBlock *, uint32_t>; 0232 // Vector containing classification of basic blocks for all SCCs where i'th 0233 // vector element corresponds to SCC with ID equal to i. 0234 using SccBlockTypeMaps = std::vector<SccBlockTypeMap>; 0235 0236 SccMap SccNums; 0237 SccBlockTypeMaps SccBlocks; 0238 0239 public: 0240 explicit SccInfo(const Function &F); 0241 0242 /// If \p BB belongs to some SCC then ID of that SCC is returned, otherwise 0243 /// -1 is returned. If \p BB belongs to more than one SCC at the same time 0244 /// result is undefined. 0245 int getSCCNum(const BasicBlock *BB) const; 0246 /// Returns true if \p BB is a 'header' block in SCC with \p SccNum ID, 0247 /// false otherwise. 0248 bool isSCCHeader(const BasicBlock *BB, int SccNum) const { 0249 return getSccBlockType(BB, SccNum) & Header; 0250 } 0251 /// Returns true if \p BB is an 'exiting' block in SCC with \p SccNum ID, 0252 /// false otherwise. 0253 bool isSCCExitingBlock(const BasicBlock *BB, int SccNum) const { 0254 return getSccBlockType(BB, SccNum) & Exiting; 0255 } 0256 /// Fills in \p Enters vector with all such blocks that don't belong to 0257 /// SCC with \p SccNum ID but there is an edge to a block belonging to the 0258 /// SCC. 0259 void getSccEnterBlocks(int SccNum, 0260 SmallVectorImpl<BasicBlock *> &Enters) const; 0261 /// Fills in \p Exits vector with all such blocks that don't belong to 0262 /// SCC with \p SccNum ID but there is an edge from a block belonging to the 0263 /// SCC. 0264 void getSccExitBlocks(int SccNum, 0265 SmallVectorImpl<BasicBlock *> &Exits) const; 0266 0267 private: 0268 /// Returns \p BB's type according to classification given by SccBlockType 0269 /// enum. Please note that \p BB must belong to SSC with \p SccNum ID. 0270 uint32_t getSccBlockType(const BasicBlock *BB, int SccNum) const; 0271 /// Calculates \p BB's type and stores it in internal data structures for 0272 /// future use. Please note that \p BB must belong to SSC with \p SccNum ID. 0273 void calculateSccBlockType(const BasicBlock *BB, int SccNum); 0274 }; 0275 0276 private: 0277 // We need to store CallbackVH's in order to correctly handle basic block 0278 // removal. 0279 class BasicBlockCallbackVH final : public CallbackVH { 0280 BranchProbabilityInfo *BPI; 0281 0282 void deleted() override { 0283 assert(BPI != nullptr); 0284 BPI->eraseBlock(cast<BasicBlock>(getValPtr())); 0285 } 0286 0287 public: 0288 void setBPI(BranchProbabilityInfo *BPI) { this->BPI = BPI; } 0289 0290 BasicBlockCallbackVH(const Value *V, BranchProbabilityInfo *BPI = nullptr) 0291 : CallbackVH(const_cast<Value *>(V)), BPI(BPI) {} 0292 }; 0293 0294 /// Pair of Loop and SCC ID number. Used to unify handling of normal and 0295 /// SCC based loop representations. 0296 using LoopData = std::pair<Loop *, int>; 0297 /// Helper class to keep basic block along with its loop data information. 0298 class LoopBlock { 0299 public: 0300 explicit LoopBlock(const BasicBlock *BB, const LoopInfo &LI, 0301 const SccInfo &SccI); 0302 0303 const BasicBlock *getBlock() const { return BB; } 0304 BasicBlock *getBlock() { return const_cast<BasicBlock *>(BB); } 0305 LoopData getLoopData() const { return LD; } 0306 Loop *getLoop() const { return LD.first; } 0307 int getSccNum() const { return LD.second; } 0308 0309 bool belongsToLoop() const { return getLoop() || getSccNum() != -1; } 0310 bool belongsToSameLoop(const LoopBlock &LB) const { 0311 return (LB.getLoop() && getLoop() == LB.getLoop()) || 0312 (LB.getSccNum() != -1 && getSccNum() == LB.getSccNum()); 0313 } 0314 0315 private: 0316 const BasicBlock *const BB = nullptr; 0317 LoopData LD = {nullptr, -1}; 0318 }; 0319 0320 // Pair of LoopBlocks representing an edge from first to second block. 0321 using LoopEdge = std::pair<const LoopBlock &, const LoopBlock &>; 0322 0323 DenseSet<BasicBlockCallbackVH, DenseMapInfo<Value*>> Handles; 0324 0325 // Since we allow duplicate edges from one basic block to another, we use 0326 // a pair (PredBlock and an index in the successors) to specify an edge. 0327 using Edge = std::pair<const BasicBlock *, unsigned>; 0328 0329 DenseMap<Edge, BranchProbability> Probs; 0330 0331 /// Track the last function we run over for printing. 0332 const Function *LastF = nullptr; 0333 0334 const LoopInfo *LI = nullptr; 0335 0336 /// Keeps information about all SCCs in a function. 0337 std::unique_ptr<const SccInfo> SccI; 0338 0339 /// Keeps mapping of a basic block to its estimated weight. 0340 SmallDenseMap<const BasicBlock *, uint32_t> EstimatedBlockWeight; 0341 0342 /// Keeps mapping of a loop to estimated weight to enter the loop. 0343 SmallDenseMap<LoopData, uint32_t> EstimatedLoopWeight; 0344 0345 /// Helper to construct LoopBlock for \p BB. 0346 LoopBlock getLoopBlock(const BasicBlock *BB) const { 0347 return LoopBlock(BB, *LI, *SccI); 0348 } 0349 0350 /// Returns true if destination block belongs to some loop and source block is 0351 /// either doesn't belong to any loop or belongs to a loop which is not inner 0352 /// relative to the destination block. 0353 bool isLoopEnteringEdge(const LoopEdge &Edge) const; 0354 /// Returns true if source block belongs to some loop and destination block is 0355 /// either doesn't belong to any loop or belongs to a loop which is not inner 0356 /// relative to the source block. 0357 bool isLoopExitingEdge(const LoopEdge &Edge) const; 0358 /// Returns true if \p Edge is either enters to or exits from some loop, false 0359 /// in all other cases. 0360 bool isLoopEnteringExitingEdge(const LoopEdge &Edge) const; 0361 /// Returns true if source and destination blocks belongs to the same loop and 0362 /// destination block is loop header. 0363 bool isLoopBackEdge(const LoopEdge &Edge) const; 0364 // Fills in \p Enters vector with all "enter" blocks to a loop \LB belongs to. 0365 void getLoopEnterBlocks(const LoopBlock &LB, 0366 SmallVectorImpl<BasicBlock *> &Enters) const; 0367 // Fills in \p Exits vector with all "exit" blocks from a loop \LB belongs to. 0368 void getLoopExitBlocks(const LoopBlock &LB, 0369 SmallVectorImpl<BasicBlock *> &Exits) const; 0370 0371 /// Returns estimated weight for \p BB. std::nullopt if \p BB has no estimated 0372 /// weight. 0373 std::optional<uint32_t> getEstimatedBlockWeight(const BasicBlock *BB) const; 0374 0375 /// Returns estimated weight to enter \p L. In other words it is weight of 0376 /// loop's header block not scaled by trip count. Returns std::nullopt if \p L 0377 /// has no no estimated weight. 0378 std::optional<uint32_t> getEstimatedLoopWeight(const LoopData &L) const; 0379 0380 /// Return estimated weight for \p Edge. Returns std::nullopt if estimated 0381 /// weight is unknown. 0382 std::optional<uint32_t> getEstimatedEdgeWeight(const LoopEdge &Edge) const; 0383 0384 /// Iterates over all edges leading from \p SrcBB to \p Successors and 0385 /// returns maximum of all estimated weights. If at least one edge has unknown 0386 /// estimated weight std::nullopt is returned. 0387 template <class IterT> 0388 std::optional<uint32_t> 0389 getMaxEstimatedEdgeWeight(const LoopBlock &SrcBB, 0390 iterator_range<IterT> Successors) const; 0391 0392 /// If \p LoopBB has no estimated weight then set it to \p BBWeight and 0393 /// return true. Otherwise \p BB's weight remains unchanged and false is 0394 /// returned. In addition all blocks/loops that might need their weight to be 0395 /// re-estimated are put into BlockWorkList/LoopWorkList. 0396 bool updateEstimatedBlockWeight(LoopBlock &LoopBB, uint32_t BBWeight, 0397 SmallVectorImpl<BasicBlock *> &BlockWorkList, 0398 SmallVectorImpl<LoopBlock> &LoopWorkList); 0399 0400 /// Starting from \p LoopBB (including \p LoopBB itself) propagate \p BBWeight 0401 /// up the domination tree. 0402 void propagateEstimatedBlockWeight(const LoopBlock &LoopBB, DominatorTree *DT, 0403 PostDominatorTree *PDT, uint32_t BBWeight, 0404 SmallVectorImpl<BasicBlock *> &WorkList, 0405 SmallVectorImpl<LoopBlock> &LoopWorkList); 0406 0407 /// Returns block's weight encoded in the IR. 0408 std::optional<uint32_t> getInitialEstimatedBlockWeight(const BasicBlock *BB); 0409 0410 // Computes estimated weights for all blocks in \p F. 0411 void computeEestimateBlockWeight(const Function &F, DominatorTree *DT, 0412 PostDominatorTree *PDT); 0413 0414 /// Based on computed weights by \p computeEstimatedBlockWeight set 0415 /// probabilities on branches. 0416 bool calcEstimatedHeuristics(const BasicBlock *BB); 0417 bool calcMetadataWeights(const BasicBlock *BB); 0418 bool calcPointerHeuristics(const BasicBlock *BB); 0419 bool calcZeroHeuristics(const BasicBlock *BB, const TargetLibraryInfo *TLI); 0420 bool calcFloatingPointHeuristics(const BasicBlock *BB); 0421 }; 0422 0423 /// Analysis pass which computes \c BranchProbabilityInfo. 0424 class BranchProbabilityAnalysis 0425 : public AnalysisInfoMixin<BranchProbabilityAnalysis> { 0426 friend AnalysisInfoMixin<BranchProbabilityAnalysis>; 0427 0428 static AnalysisKey Key; 0429 0430 public: 0431 /// Provide the result type for this analysis pass. 0432 using Result = BranchProbabilityInfo; 0433 0434 /// Run the analysis pass over a function and produce BPI. 0435 BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM); 0436 }; 0437 0438 /// Printer pass for the \c BranchProbabilityAnalysis results. 0439 class BranchProbabilityPrinterPass 0440 : public PassInfoMixin<BranchProbabilityPrinterPass> { 0441 raw_ostream &OS; 0442 0443 public: 0444 explicit BranchProbabilityPrinterPass(raw_ostream &OS) : OS(OS) {} 0445 0446 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 0447 0448 static bool isRequired() { return true; } 0449 }; 0450 0451 /// Legacy analysis pass which computes \c BranchProbabilityInfo. 0452 class BranchProbabilityInfoWrapperPass : public FunctionPass { 0453 BranchProbabilityInfo BPI; 0454 0455 public: 0456 static char ID; 0457 0458 BranchProbabilityInfoWrapperPass(); 0459 0460 BranchProbabilityInfo &getBPI() { return BPI; } 0461 const BranchProbabilityInfo &getBPI() const { return BPI; } 0462 0463 void getAnalysisUsage(AnalysisUsage &AU) const override; 0464 bool runOnFunction(Function &F) override; 0465 void releaseMemory() override; 0466 void print(raw_ostream &OS, const Module *M = nullptr) const override; 0467 }; 0468 0469 } // end namespace llvm 0470 0471 #endif // LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|