From 3c59cf5aa79b8c17f59663a0ea4caede6828565f Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 6 Sep 2021 22:18:11 +0200 Subject: [PATCH] [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. (cherry picked from commit 8d54c8a0c3d7d4a50186ae7087780c6082e5bb46) --- lib/Analysis/ScalarEvolution.cpp | 2 +- .../max-backedge-taken-count-guard-info.ll | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index f22d834b5e5..2d980e6935b 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -13969,7 +13969,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/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll b/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll index 9e7957a3736..7dd21827d0b 100644 --- a/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll +++ b/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll @@ -1327,6 +1327,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) U: [1,3) S: [1,3) +; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ] +; CHECK-NEXT: --> {0,+,1}<%loop> U: [0,3) S: [0,3) Exits: (-1 + %N) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: %gep = getelementptr inbounds i16, i16* %pred, i32 %iv +; CHECK-NEXT: --> {%pred,+,2}<%loop> U: full-set S: full-set Exits: ((2 * (zext i32 (-1 + %N) to i64)) + %pred) LoopDispositions: { %loop: Computable } +; CHECK-NEXT: %iv.next = add nuw nsw i32 %iv, 1 +; CHECK-NEXT: --> {1,+,1}<%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) +; CHECK-NEXT: Loop %loop: max backedge-taken count is 2 +; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %N) +; 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) {