Skip to content

Commit cfcc8f3

Browse files
committed
[SCEV] Better preserve wrapping info in SimplifyICmpOperands for UGE.
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 llvm#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.
1 parent 267b859 commit cfcc8f3

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10892,7 +10892,11 @@ 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+
if ((isa<SCEVConstant>(RHS) ||
10897+
match(RHS,
10898+
m_scev_AffineAddRec(m_SCEVConstant(), m_SCEV(), m_Loop()))) &&
10899+
!getUnsignedRangeMin(RHS).isMinValue()) {
1089610900
RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
1089710901
Pred = ICmpInst::ICMP_UGT;
1089810902
Changed = true;
@@ -10901,6 +10905,10 @@ bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
1090110905
SCEV::FlagNUW);
1090210906
Pred = ICmpInst::ICMP_UGT;
1090310907
Changed = true;
10908+
} else if (!getUnsignedRangeMin(RHS).isMinValue()) {
10909+
RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
10910+
Pred = ICmpInst::ICMP_UGT;
10911+
Changed = true;
1090410912
}
1090510913
break;
1090610914
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)