[llvm] [LoopUnroll] Use branch probability in multi-exit loop unrolling (PR #164799)

Joel E. Denny via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 7 11:46:10 PST 2026


================
@@ -482,10 +482,53 @@ CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder,
   return NewLoop;
 }
 
+// Calculates the edge probability from Src to Dst.
+// Dst has to be a successor to Src.
+// This uses branch_probability metadata directly. If data are missing or
+// probability cannot be computed, then std::nullopt is returned.
+// This does not use BranchProbabilityInfo and the values computed by this
+// will vary from BPI because BPI has its own more advanced heuristics to
+// determine probabilities without metadata.
+static std::optional<BranchProbability>
+computeBranchProbabilityUsingMetadata(BasicBlock *Src, BasicBlock *Dst) {
----------------
jdenny-ornl wrote:

`llvm::getBranchProbability` in `LoopUtils.cpp` has nearly the same functionality but exposes it more widely.  Can we combine them so we do not have to maintain both?

For example, you might change `llvm::getBranchProbability` so it accepts parameters `(BranchInst *B, BasicBlock *Dst)` and handles > 2 successors like this function.  `llvm::getBranchProbability`'s existing caller can easily use that form.  Then you might add an overload that wraps that version but accepts `(BasicBlock *Src, BasicBlock *Dst)` like this function.

https://github.com/llvm/llvm-project/pull/164799


More information about the llvm-commits mailing list