[llvm] a5051f2 - [SCEV] Fix applyLoopGuards() chaining for ne predicates
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 24 12:46:56 PDT 2021
Author: Nikita Popov
Date: 2021-04-24T21:43:46+02:00
New Revision: a5051f2fa2f20405a7cd0630067afc338f6c6d44
URL: https://github.com/llvm/llvm-project/commit/a5051f2fa2f20405a7cd0630067afc338f6c6d44
DIFF: https://github.com/llvm/llvm-project/commit/a5051f2fa2f20405a7cd0630067afc338f6c6d44.diff
LOG: [SCEV] Fix applyLoopGuards() chaining for ne predicates
ICMP_NE predicates directly overwrote the rewritten result,
instead of chaining it with previous rewrites, as was done for
ICMP_ULT and ICMP_ULE. This means that some guards were effectively
discarded, depending on their order.
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 43ec432e4234..488f60de41e4 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -13433,30 +13433,22 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
if (!LHSUnknown)
return;
+ // Check whether LHS has already been rewritten. In that case we want to
+ // chain further rewrites onto the already rewritten value.
+ auto I = RewriteMap.find(LHSUnknown->getValue());
+ const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHS;
+
// TODO: use information from more predicates.
switch (Predicate) {
- case CmpInst::ICMP_ULT: {
- if (!containsAddRecurrence(RHS)) {
- const SCEV *Base = LHS;
- auto I = RewriteMap.find(LHSUnknown->getValue());
- if (I != RewriteMap.end())
- Base = I->second;
-
- RewriteMap[LHSUnknown->getValue()] =
- getUMinExpr(Base, getMinusSCEV(RHS, getOne(RHS->getType())));
- }
+ case CmpInst::ICMP_ULT:
+ if (!containsAddRecurrence(RHS))
+ RewriteMap[LHSUnknown->getValue()] = getUMinExpr(
+ RewrittenLHS, getMinusSCEV(RHS, getOne(RHS->getType())));
break;
- }
- case CmpInst::ICMP_ULE: {
- if (!containsAddRecurrence(RHS)) {
- const SCEV *Base = LHS;
- auto I = RewriteMap.find(LHSUnknown->getValue());
- if (I != RewriteMap.end())
- Base = I->second;
- RewriteMap[LHSUnknown->getValue()] = getUMinExpr(Base, RHS);
- }
+ case CmpInst::ICMP_ULE:
+ if (!containsAddRecurrence(RHS))
+ RewriteMap[LHSUnknown->getValue()] = getUMinExpr(RewrittenLHS, RHS);
break;
- }
case CmpInst::ICMP_EQ:
if (isa<SCEVConstant>(RHS))
RewriteMap[LHSUnknown->getValue()] = RHS;
@@ -13465,7 +13457,7 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
if (isa<SCEVConstant>(RHS) &&
cast<SCEVConstant>(RHS)->getValue()->isNullValue())
RewriteMap[LHSUnknown->getValue()] =
- getUMaxExpr(LHS, getOne(RHS->getType()));
+ getUMaxExpr(RewrittenLHS, getOne(RHS->getType()));
break;
default:
break;
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 4a176be0e9ed..7419fa9fd300 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
@@ -451,14 +451,14 @@ define void @test_guard_ne_ult(i32* nocapture readonly %data, i64 %count) {
; CHECK-LABEL: 'test_guard_ne_ult'
; CHECK-NEXT: Classifying expressions for: @test_guard_ne_ult
; CHECK-NEXT: %iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
-; CHECK-NEXT: --> {0,+,1}<nuw><%loop> U: [0,-1) S: [0,-1) Exits: (-1 + %count) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: --> {0,+,1}<nuw><%loop> U: [0,4) S: [0,4) Exits: (-1 + %count) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: %idx = getelementptr inbounds i32, i32* %data, i64 %iv
-; CHECK-NEXT: --> {%data,+,4}<%loop> U: full-set S: full-set Exits: (-4 + (4 * %count) + %data) LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: --> {%data,+,4}<nuw><%loop> U: full-set S: full-set Exits: (-4 + (4 * %count) + %data) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: %iv.next = add nuw i64 %iv, 1
-; CHECK-NEXT: --> {1,+,1}<nuw><%loop> U: [1,0) S: [1,0) Exits: %count LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: --> {1,+,1}<nuw><%loop> U: [1,5) S: [1,5) Exits: %count LoopDispositions: { %loop: Computable }
; CHECK-NEXT: Determining loop execution counts for: @test_guard_ne_ult
; CHECK-NEXT: Loop %loop: backedge-taken count is (-1 + %count)
-; CHECK-NEXT: Loop %loop: max backedge-taken count is -2
+; CHECK-NEXT: Loop %loop: max backedge-taken count is 3
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is (-1 + %count)
; CHECK-NEXT: Predicates:
; CHECK: Loop %loop: Trip multiple is 1
More information about the llvm-commits
mailing list