Skip to content

Commit a98b397

Browse files
nikictstellar
authored andcommitted
[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 8d54c8a)
1 parent 9b3867e commit a98b397

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13969,7 +13969,7 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
1396913969
if (ExactRegion.isWrappedSet() || ExactRegion.isFullSet())
1397013970
return false;
1397113971
auto I = RewriteMap.find(LHSUnknown->getValue());
13972-
const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHS;
13972+
const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHSUnknown;
1397313973
RewriteMap[LHSUnknown->getValue()] = getUMaxExpr(
1397413974
getConstant(ExactRegion.getUnsignedMin()),
1397513975
getUMinExpr(RewrittenLHS, getConstant(ExactRegion.getUnsignedMax())));

llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,46 @@ exit:
13271327
ret void
13281328
}
13291329

1330+
; Same as @optimized_range_check_unsigned, but %N already has a range limited
1331+
; to [2,4) beforehand.
1332+
define void @optimized_range_check_unsigned3(i16* %pred, i1 %c) {
1333+
; CHECK-LABEL: 'optimized_range_check_unsigned3'
1334+
; CHECK-NEXT: Classifying expressions for: @optimized_range_check_unsigned3
1335+
; CHECK-NEXT: %N = select i1 %c, i32 2, i32 3
1336+
; CHECK-NEXT: --> %N U: [2,4) S: [2,4)
1337+
; CHECK-NEXT: %N.off = add i32 %N, -1
1338+
; CHECK-NEXT: --> (-1 + %N)<nsw> U: [1,3) S: [1,3)
1339+
; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
1340+
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,3) S: [0,3) Exits: (-1 + %N)<nsw> LoopDispositions: { %loop: Computable }
1341+
; CHECK-NEXT: %gep = getelementptr inbounds i16, i16* %pred, i32 %iv
1342+
; 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 }
1343+
; CHECK-NEXT: %iv.next = add nuw nsw i32 %iv, 1
1344+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,4) S: [1,4) Exits: %N LoopDispositions: { %loop: Computable }
1345+
; CHECK-NEXT: Determining loop execution counts for: @optimized_range_check_unsigned3
1346+
; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + %N)<nsw>
1347+
; CHECK-NEXT: Loop %loop: max backedge-taken count is 2
1348+
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %N)<nsw>
1349+
; CHECK-NEXT: Predicates:
1350+
; CHECK: Loop %loop: Trip multiple is 1
1351+
;
1352+
entry:
1353+
%N = select i1 %c, i32 2, i32 3
1354+
%N.off = add i32 %N, -1
1355+
%cmp = icmp ult i32 %N.off, 7
1356+
br i1 %cmp, label %loop, label %exit
1357+
1358+
loop:
1359+
%iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
1360+
%gep = getelementptr inbounds i16, i16* %pred, i32 %iv
1361+
store i16 0, i16* %gep, align 2
1362+
%iv.next = add nuw nsw i32 %iv, 1
1363+
%ec = icmp eq i32 %iv.next, %N
1364+
br i1 %ec, label %exit, label %loop
1365+
1366+
exit:
1367+
ret void
1368+
}
1369+
13301370
; Similar to @optimized_range_check_unsigned, but the initial compare checks
13311371
; against unsigned max (-1), which breaks the range check idiom.
13321372
define void @not_optimized_range_check_unsigned1(i16* %pred, i32 %N) {

0 commit comments

Comments
 (0)