[llvm] 8d54c8a - [SCEV] Fix applyLoopGuards() with range check idiom (PR51760)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 6 13:22:54 PDT 2021
Author: Nikita Popov
Date: 2021-09-06T22:22:41+02:00
New Revision: 8d54c8a0c3d7d4a50186ae7087780c6082e5bb46
URL: https://github.com/llvm/llvm-project/commit/8d54c8a0c3d7d4a50186ae7087780c6082e5bb46
DIFF: https://github.com/llvm/llvm-project/commit/8d54c8a0c3d7d4a50186ae7087780c6082e5bb46.diff
LOG: [SCEV] Fix applyLoopGuards() with range check idiom (PR51760)
Due to a typo, this replaced %x with umax(C1, umin(C2, %x + C3))
rather than umax(C1, umin(C2, %x)). This didn't make a difference
for the existing tests, because the result is only used for range
calculation, and %x will usually have an unknown starting range,
and the additional offset keeps it unknown. However, if %x already
has a known range, we may compute a result range that is too
small.
Added:
Modified:
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 9ceae4aeca7a7..af385e99cacde 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -13977,7 +13977,7 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
if (ExactRegion.isWrappedSet() || ExactRegion.isFullSet())
return false;
auto I = RewriteMap.find(LHSUnknown->getValue());
- const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHS;
+ const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHSUnknown;
RewriteMap[LHSUnknown->getValue()] = getUMaxExpr(
getConstant(ExactRegion.getUnsignedMin()),
getUMinExpr(RewrittenLHS, getConstant(ExactRegion.getUnsignedMax())));
diff --git a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
index b303caeb6131a..a8e31b681f43c 100644
--- a/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
+++ b/llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll
@@ -1326,6 +1326,46 @@ exit:
ret void
}
+; Same as @optimized_range_check_unsigned, but %N already has a range limited
+; to [2,4) beforehand.
+define void @optimized_range_check_unsigned3(i16* %pred, i1 %c) {
+; CHECK-LABEL: 'optimized_range_check_unsigned3'
+; CHECK-NEXT: Classifying expressions for: @optimized_range_check_unsigned3
+; CHECK-NEXT: %N = select i1 %c, i32 2, i32 3
+; CHECK-NEXT: --> %N U: [2,4) S: [2,4)
+; CHECK-NEXT: %N.off = add i32 %N, -1
+; CHECK-NEXT: --> (-1 + %N)<nsw> U: [1,3) S: [1,3)
+; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
+; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,3) S: [0,3) Exits: (-1 + %N)<nsw> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %gep = getelementptr inbounds i16, i16* %pred, i32 %iv
+; CHECK-NEXT: --> {%pred,+,2}<nuw><%loop> U: full-set S: full-set Exits: ((2 * (zext i32 (-1 + %N)<nsw> to i64))<nuw><nsw> + %pred) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %iv.next = add nuw nsw i32 %iv, 1
+; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,4) S: [1,4) Exits: %N LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: Determining loop execution counts for: @optimized_range_check_unsigned3
+; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + %N)<nsw>
+; CHECK-NEXT: Loop %loop: max backedge-taken count is 2
+; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %N)<nsw>
+; CHECK-NEXT: Predicates:
+; CHECK: Loop %loop: Trip multiple is 1
+;
+entry:
+ %N = select i1 %c, i32 2, i32 3
+ %N.off = add i32 %N, -1
+ %cmp = icmp ult i32 %N.off, 7
+ br i1 %cmp, label %loop, label %exit
+
+loop:
+ %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
+ %gep = getelementptr inbounds i16, i16* %pred, i32 %iv
+ store i16 0, i16* %gep, align 2
+ %iv.next = add nuw nsw i32 %iv, 1
+ %ec = icmp eq i32 %iv.next, %N
+ br i1 %ec, label %exit, label %loop
+
+exit:
+ ret void
+}
+
; Similar to @optimized_range_check_unsigned, but the initial compare checks
; against unsigned max (-1), which breaks the range check idiom.
define void @not_optimized_range_check_unsigned1(i16* %pred, i32 %N) {
More information about the llvm-commits
mailing list