[llvm] Fix exact backedge count algorithm in Scalar-Evolution (PR #92560)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 17 07:46:02 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: vaibhav (mrdaybird)
<details>
<summary>Changes</summary>
Fixes #<!-- -->92554 (std::reverse will auto-vectorize now)
When calculating number of times a exit condition containing a comparison is executed, we mostly assume that RHS of comparison should be loop invariant, but it may be another add-recurrence.
In that case, we can try the computation with `LHS = LHS - RHS` and `RHS = 0`.
For now, I have only changed `howManyLessThans`, so it only affects `X < Y` type of conditions.
Other conditions will also need to be modified.
@<!-- -->fhahn
---
Full diff: https://github.com/llvm/llvm-project/pull/92560.diff
1 Files Affected:
- (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+9-5)
``````````diff
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 515b9d0744f6e..9bf5bf80b4570 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -12941,12 +12941,16 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
return RHS;
}
- // When the RHS is not invariant, we do not know the end bound of the loop and
- // cannot calculate the ExactBECount needed by ExitLimit. However, we can
- // calculate the MaxBECount, given the start, stride and max value for the end
- // bound of the loop (RHS), and the fact that IV does not overflow (which is
- // checked above).
if (!isLoopInvariant(RHS, L)) {
+ // If RHS is an add recurrence, try again with lhs=lhs-rhs and rhs=0
+ if(auto RHSAddRec = dyn_cast<SCEVAddRecExpr>(RHS)){
+ return howManyLessThans(getMinusSCEV(IV, RHSAddRec),
+ getZero(IV->getType()), L, true, ControlsOnlyExit, AllowPredicates);
+ }
+ // If we cannot calculate ExactBECount, we can calculate the MaxBECount,
+ // given the start, stride and max value for the end bound of the
+ // loop (RHS), and the fact that IV does not overflow (which is
+ // checked above).
const SCEV *MaxBECount = computeMaxBECountForLT(
Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
``````````
</details>
https://github.com/llvm/llvm-project/pull/92560
More information about the llvm-commits
mailing list