Skip to content

Commit 8f79754

Browse files
authored
[SCEV] Better preserve wrapping info in SimplifyICmpOperands for UGE. (#144404)
Update SimplifyICmpOperands to only try subtracting 1 from RHS first, if RHS is an op we can fold the subtract directly into. Otherwise try adding to LHS first, as we can preserve NUW flags. This improves results in a few cases, including the modified test case from berkeley-abc and new code to be added in #128061. Note that there are more cases where the results can be improved by better ordering here which I'll try to investigate as follow-up. PR: #144404
1 parent 0a7b0c8 commit 8f79754

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10892,7 +10892,12 @@ bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
1089210892
}
1089310893
break;
1089410894
case ICmpInst::ICMP_UGE:
10895-
if (!getUnsignedRangeMin(RHS).isMinValue()) {
10895+
// If RHS is an op we can fold the -1, try that first.
10896+
// Otherwise prefer LHS to preserve the nuw flag.
10897+
if ((isa<SCEVConstant>(RHS) ||
10898+
(isa<SCEVAddExpr, SCEVAddRecExpr>(RHS) &&
10899+
isa<SCEVConstant>(cast<SCEVNAryExpr>(RHS)->getOperand(0)))) &&
10900+
!getUnsignedRangeMin(RHS).isMinValue()) {
1089610901
RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
1089710902
Pred = ICmpInst::ICMP_UGT;
1089810903
Changed = true;
@@ -10901,6 +10906,10 @@ bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
1090110906
SCEV::FlagNUW);
1090210907
Pred = ICmpInst::ICMP_UGT;
1090310908
Changed = true;
10909+
} else if (!getUnsignedRangeMin(RHS).isMinValue()) {
10910+
RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
10911+
Pred = ICmpInst::ICMP_UGT;
10912+
Changed = true;
1090410913
}
1090510914
break;
1090610915
default:

llvm/test/Transforms/IndVarSimplify/simplify-icmp-operands-order.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,12 @@ loop.latch:
5353

5454
define void @test_simplifycompare_rhs_not_constant1() {
5555
; CHECK-LABEL: define void @test_simplifycompare_rhs_not_constant1() {
56-
; CHECK-NEXT: [[ENTRY:.*]]:
56+
; CHECK-NEXT: [[ENTRY:.*:]]
5757
; CHECK-NEXT: [[P:%.*]] = alloca i64, align 8
5858
; CHECK-NEXT: br label %[[LOOP:.*]]
5959
; CHECK: [[LOOP]]:
60-
; CHECK-NEXT: [[PTR_IV:%.*]] = phi ptr [ [[P]], %[[ENTRY]] ], [ [[PTR_IV_NEXT:%.*]], %[[LOOP]] ]
61-
; CHECK-NEXT: [[PTR_IV_NEXT]] = getelementptr i8, ptr [[PTR_IV]], i64 -8
62-
; CHECK-NEXT: call void @use(ptr [[PTR_IV]])
63-
; CHECK-NEXT: [[EC:%.*]] = icmp ult ptr [[PTR_IV_NEXT]], [[P]]
64-
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]]
60+
; CHECK-NEXT: call void @use(ptr [[P]])
61+
; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[LOOP]]
6562
; CHECK: [[EXIT]]:
6663
; CHECK-NEXT: ret void
6764
;

0 commit comments

Comments
 (0)