aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/BranchProbabilityInfo.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-07-02[nfc] Rename API with typo in BranchProbabilityInfo (#146447)Mircea Trofin1-3/+4
2025-05-04[llvm] Remove unused local variables (NFC) (#138454)Kazu Hirata1-1/+0
2025-04-21[LLVM] Cleanup pass initialization for Analysis passes (#135858)Rahul Joshi1-4/+1
- Do not call pass initialization from pass constructors. - Instead, pass initialization should happen in the `initializeAnalysis` function. - https://github.com/llvm/llvm-project/issues/111767
2025-03-03[NFC]Make file-local cl::opt global variables static (#126486)chrisPyr1-1/+1
#125983
2025-01-22[Analysis] Avoid repeated hash lookups (NFC) (#123893)Kazu Hirata1-3/+5
2024-11-05[Analysis] Remove unused includes (NFC) (#114936)Kazu Hirata1-1/+0
Identified with misc-include-cleaner.
2024-06-27[IR] Add getDataLayout() helpers to BasicBlock and Instruction (#96902)Nikita Popov1-1/+1
This is a helper to avoid writing `getModule()->getDataLayout()`. I regularly try to use this method only to remember it doesn't exist... `getModule()->getDataLayout()` is also a common (the most common?) reason why code has to include the Module.h header.
2024-06-24[BPI] Use BasicBlock::isEHPad() to check exception handling block. (#95771)Enna11-6/+3
There is no need to iterate all predecessors of current block, check if current block is the invoke unwind destination of any predecessor. We can directly call `BasicBlock::isEHPad()` to check if current block is an exception handling block.
2024-06-03[BPI] Cache LoopExitBlocks to improve compile time (#93451)Enna11-5/+8
The `LoopBlock` stored in `LoopWorkList` consist of basic block and its loop data information. When iterate `LoopWorkList`, if estimated weight of a loop is not stored in `EstimatedLoopWeight`, `getLoopExitBlocks()` is called to get all exit blocks of the loop. The estimated weight of a loop is calculated by iterating over edges leading from basic block to all exit blocks of the loop. If at least one edge has unknown estimated weight, the estimated weight of loop is unknown and will not be stored in `EstimatedLoopWeight`. `LoopWorkList` can contain different blocks in a same loop, so there is wasted work that calls `getLoopExitBlocks()` for same loop multiple times. Since computing the exit blocks of loop is expensive and the loop structure is not mutated in Branch Probability Analysis, we can cache the result and improve compile time. With this change, the overall compile time for a file containing a very large loop is dropped by around 82%.
2024-05-09Replace uses of ConstantExpr::getCompare. (#91558)Eli Friedman1-2/+2
Use ICmpInst::compare() where possible, ConstantFoldCompareInstOperands in other places. This only changes places where the either the fold is guaranteed to succeed, or the code doesn't use the resulting compare if we fail to fold.
2024-05-07[Analysis, CodeGen, DebugInfo] Use StringRef::operator== instead of ↵Kazu Hirata1-3/+2
StringRef::equals (NFC) (#91304) I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator==/!= outnumber StringRef::equals by a factor of 53 under llvm/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
2023-12-02[BPI] Reuse the AsmWriter's BB naming scheme in BranchProbabilityPrinterPass ↵Mircea Trofin1-2/+5
(#73593) When using `BranchProbabilityPrinterPass`, if a BB has no name, we get pretty unusable information like `edge -> has probability...` (i.e. we have no idea what the vertices of that edge are). This patch uses `printAsOperand`, which uses the same naming scheme as `Function::dump`, so for example during debugging sessions, the IR obtained from a function and the names used by `BranchProbabilityPrinterPass` will match. A shortcoming is that `printAsOperand` will result in the numbering algorithm re-running for every edge and every vertex (when `BranchProbabilityPrinterPass` is run on a function). If, for the given scenario, this is a problem, we can revisit this subsequently. Another nuance is that the entry basic block will be numbered, which may be slightly confusing when it's anonymous, but it's easily identifiable - the first edge would have it as source (and the number should be easily recognizable)
2023-11-21Support BranchProbabilityInfo in update_analyze_test_checks.py (#72943)Matthias Braun1-3/+2
- Change `BranchProbabilityPrinterPass` output to match expectations of `update_analyze_test_checks.py`. - Add `Branch Probability Analysis` to list of supported analyses. - Process `llvm/test/Analysis/BranchProbabilityInfo/basic.ll` with `update_analyze_test_checks.py` as proof of concept. Leaving the other tests unchanged to reduce the amount of churn.
2023-04-18[BPI] Add method to swap outgoing edges probabilitiesMax Kazantsev1-0/+8
The motivation is need to update branch probability info after swapping successors of branch instruction. Differential Revision: https://reviews.llvm.org/D148237 Reviewed By: nikic
2023-03-17[Analysis] Make order of analysis executions more stableBjorn Pettersson1-4/+5
When debugging and using debug-pass-manager (e.g. in regression tests) we prefer a consistent order in which analysis passes are executed. But when for example doing return MyClass(AM.getResult<LoopAnalysis>(F), AM.getResult<DominatorTreeAnalysis>(F)); then the order in which LoopAnalysis and DominatorTreeAnalysis isn't guaranteed, and might for example depend on which compiler that is used when building LLVM. I've not scanned the full source tree, but this fixes some occurances of the above pattern found in lib/Analysis. This problem was discussed briefly in review for D146206.
2023-03-14[Analysis] Use *{Set,Map}::contains (NFC)Kazu Hirata1-1/+1
2023-01-19[llvm][ir] Purge MD_prof custom accessorsChristian Ulmann1-7/+2
This commit purges direct accesses to MD_prof metadata and replaces them with the accessors provided from the utility file wherever possible. This commit can be seen as the first step towards switching the branch weights to 64 bits. See post here: https://discourse.llvm.org/t/extend-md-prof-branch-weights-metadata-from-32-to-64-bits/67492 Reviewed By: davidxl, paulkirth Differential Revision: https://reviews.llvm.org/D141393
2022-12-16std::optional::value => operator*/operator->Fangrui Song1-1/+1
value() has undesired exception checking semantics and calls __throw_bad_optional_access in libc++. Moreover, the API is unavailable without _LIBCPP_NO_EXCEPTIONS on older Mach-O platforms (see _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS). This commit fixes LLVMAnalysis and its dependencies.
2022-12-14[Analysis] llvm::Optional => std::optionalFangrui Song1-8/+8
2022-12-02[Analysis] Use std::nullopt instead of None (NFC)Kazu Hirata1-4/+4
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-09-01[PGO] Support PGO annotation of CallBrInstRong Xu1-1/+1
We currently instrument CallBrInst but do not annotate it with the branch weight. This patch enables PGO annotation of CallBrInst. Differential Revision: https://reviews.llvm.org/D133040
2022-08-03[llvm][NFC] Refactor code to use ProfDataUtilsPaul Kirth1-13/+8
In this patch we replace common code patterns with the use of utility functions for dealing with profiling metadata. There should be no change in functionality, as the existing checks should be preserved in all cases. Reviewed By: bogner, davidxl Differential Revision: https://reviews.llvm.org/D128860
2022-07-27Revert "[llvm][NFC] Refactor code to use ProfDataUtils"Paul Kirth1-8/+13
This reverts commit 300c9a78819b4608b96bb26f9320bea6b8a0c4d0. We will reland once these issues are ironed out.
2022-07-27[llvm][NFC] Refactor code to use ProfDataUtilsPaul Kirth1-13/+8
In this patch we replace common code patterns with the use of utility functions for dealing with profiling metadata. There should be no change in functionality, as the existing checks should be preserved in all cases. Reviewed By: bogner, davidxl Differential Revision: https://reviews.llvm.org/D128860
2022-07-16[Analysis] Qualify auto variables in for loops (NFC)Kazu Hirata1-1/+1
2022-07-13[llvm] Use value instead of getValue (NFC)Kazu Hirata1-3/+2
2022-07-04[BPI] Avoid ConstantExpr::get()Nikita Popov1-2/+4
Use ConstantFoldBinaryOpOperands() instead, to prepare for the case where not all binary operators have a constant expression form. I believe this code actually intended to set OnlyIfReduced=true, however ConstantExpr::get() actually accepts a Flags argument at that position (and OnlyIfReducedTy as the next argument), so this ended up creating a constant expression with some random flag (probably exact or nuw depending on which).
2022-06-25Revert "Don't use Optional::hasValue (NFC)"Kazu Hirata1-2/+3
This reverts commit aa8feeefd3ac6c78ee8f67bf033976fc7d68bc6d.
2022-06-25Don't use Optional::hasValue (NFC)Kazu Hirata1-3/+2
2022-06-20[llvm] Don't use Optional::getValue (NFC)Kazu Hirata1-6/+4
2022-06-18[llvm] Use value_or instead of getValueOr (NFC)Kazu Hirata1-4/+3
2022-01-19[NFC] Add missing <map> includesNikita Popov1-0/+1
These were relying on a transitive include.
2021-11-22[BPI] Look-up tables for non-loop branches. NFC.Sjoerd Meijer1-104/+97
This adds and uses look-up tables for non-loop branch probabilities, which have have probabilities directly encoded into the tables for the different condition codes. Compared to having this logic inlined in different functions, as it used to be the case, I think this is compacter and thus also easier to check/cross reference. This also adds a test for pointer heuristics that was missing. Differential Revision: https://reviews.llvm.org/D114009
2021-11-11[BPI] Push exit block rather than exiting ones in getSccExitBlocksBin Cheng1-1/+1
The function BranchProbabilityInfo::SccInfo::getSccExitBlocks is supposed to collect all exit blocks for SCC rather than all exiting blocks. This patch fixes the typo. Reviewed By: ebrevnov Differential Revision: https://reviews.llvm.org/D113344
2021-07-17[Analaysis, CodeGen] Remove getHotSucc (NFC)Kazu Hirata1-20/+0
These functions seem to be unused for at least 5 years.
2021-06-04Fix some -Wunused-but-set-variable in -DLLVM_ENABLE_ASSERTIONS=off buildFangrui Song1-0/+1
2021-02-06[Analysis] Use range-based for loops (NFC)Kazu Hirata1-6/+3
2021-02-01[llvm] Use pop_back_val (NFC)Kazu Hirata1-2/+1
2020-12-23[BPI] Improve static heuristics for "cold" paths.Evgeniy Brevnov1-296/+349
Current approach doesn't work well in cases when multiple paths are predicted to be "cold". By "cold" paths I mean those containing "unreachable" instruction, call marked with 'cold' attribute and 'unwind' handler of 'invoke' instruction. The issue is that heuristics are applied one by one until the first match and essentially ignores relative hotness/coldness of other paths. New approach unifies processing of "cold" paths by assigning predefined absolute weight to each block estimated to be "cold". Then we propagate these weights up/down IR similarly to existing approach. And finally set up edge probabilities based on estimated block weights. One important difference is how we propagate weight up. Existing approach propagates the same weight to all blocks that are post-dominated by a block with some "known" weight. This is useless at least because it always gives 50\50 distribution which is assumed by default anyway. Worse, it causes the algorithm to skip further heuristics and can miss setting more accurate probability. New algorithm propagates the weight up only to the blocks that dominates and post-dominated by a block with some "known" weight. In other words, those blocks that are either always executed or not executed together. In addition new approach processes loops in an uniform way as well. Essentially loop exit edges are estimated as "cold" paths relative to back edges and should be considered uniformly with other coldness/hotness markers. Reviewed By: yrouban Differential Revision: https://reviews.llvm.org/D79485
2020-12-18[Analysis, CodeGen, IR] Use contains (NFC)Kazu Hirata1-2/+1
2020-11-17[BPI] Look through bitcasts in calcZeroHeuristicWei Wang1-1/+1
Constant hoisting may hide the constant value behind bitcast for And's operand. Track down the constant to make the BFI result consistent regardless of hoisting. Differential Revision: https://reviews.llvm.org/D91450
2020-11-15[JumpThreading] Call eraseBlock when folding a conditional branchKazu Hirata1-0/+2
This patch teaches the jump threading pass to call BPI->eraseBlock when it folds a conditional branch. Without this patch, BranchProbabilityInfo could end up with stale edge probabilities for the basic block containing the conditional branch -- one edge probability with less than 1.0 and the other for a removed edge. This patch is one of the steps before we can safely re-apply D91017. Differential Revision: https://reviews.llvm.org/D91511
2020-11-15[BranchProbabilityInfo] Use predecessors(BB) and successors(BB) (NFC)Kazu Hirata1-5/+4
2020-11-10Revert "[BranchProbabilityInfo] Use SmallVector (NFC)"Kazu Hirata1-27/+35
This reverts commit 2f1038c7b699e959e0521638e2e2818a849fe19c.
2020-11-10[BranchProbabilityInfo] Use a range-based for loop (NFC)Kazu Hirata1-2/+1
2020-11-09[BranchProbabilityInfo] Use SmallVector (NFC)Kazu Hirata1-35/+27
This patch simplifies BranchProbabilityInfo by changing the type of Probs. Without this patch: DenseMap<Edge, BranchProbability> Probs maps an ordered pair of a BasicBlock* and a successor index to an edge probability. With this patch: DenseMap<const BasicBlock *, SmallVector<BranchProbability, 2>> Probs maps a BasicBlock* to a vector of edge probabilities. BranchProbabilityInfo has a property that for a given basic block, we either have edge probabilities for all successors or do not have any edge probability at all. This property combined with the current map type leads to a somewhat complicated algorithm in eraseBlock to erase map entries one by one while increasing the successor index. The new map type allows us to remove the all edge probabilities for a given basic block in a more intuitive manner, namely: Probs.erase(BB); Differential Revision: https://reviews.llvm.org/D91017
2020-11-06[BranchProbabilityInfo] Simplify getEdgeProbability (NFC)Kazu Hirata1-11/+7
The patch simplifies BranchProbabilityInfo::getEdgeProbability by handling two cases separately, depending on whether we have edge probabilities. - If we have edge probabilities, then add up probabilities for successors being equal to Dst. - Otherwise, return the number of ocurrences divided by the total number of successors. Differential Revision: https://reviews.llvm.org/D90980
2020-11-06[BranchProbabilityInfo] Use succ_size (NFC)Kazu Hirata1-2/+1
2020-11-06[BranchProbabilityInfo] Introduce method copyEdgeProbabilities(). NFCYevgeny Rouban1-0/+19
A new method is introduced to allow bulk copy of outgoing edge probabilities from one block to another. This can be useful when a block is cloned from another one and we do not know if there are edge probabilities set for the original block or not. Copying outside of the BranchProbabilityInfo class makes the user unconditionally set the cloned block's edge probabilities even if they are unset for the original block. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D90839
2020-11-06[BranchProbabilityInfo] Remove block handles in eraseBlock()Yevgeny Rouban1-1/+2
BranchProbabilityInfo::eraseBlock() is a public method and can be called without deleting the block itself. This method is made remove the correspondent tracking handle from BranchProbabilityInfo::Handles along with the probabilities of the block. Handles.erase() call is moved to eraseBlock(). In setEdgeProbability() we need to add the block handle only once. Reviewed By: kazu Differential Revision: https://reviews.llvm.org/D90838