Skip to content

[InstCombine] fold icmp with add/sub instructions having the same operands #143241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7728,6 +7728,30 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
}
}

// icmp slt (sub nsw x, y), (add nsw x, y) --> icmp sgt y, 0
// icmp ult (sub nuw x, y), (add nuw x, y) --> icmp ugt y, 0
// icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y) --> icmp eq y, 0
{
Value *A, *B;
CmpPredicate CmpPred;
if (match(&I, m_c_ICmp(CmpPred, m_Sub(m_Value(A), m_Value(B)),
m_c_Add(m_Deferred(A), m_Deferred(B))))) {
auto *I0 = cast<OverflowingBinaryOperator>(Op0);
auto *I1 = cast<OverflowingBinaryOperator>(Op1);
bool I0NUW = I0->hasNoUnsignedWrap();
bool I1NUW = I1->hasNoUnsignedWrap();
bool I0NSW = I0->hasNoSignedWrap();
bool I1NSW = I1->hasNoSignedWrap();
if ((ICmpInst::isUnsigned(Pred) && I0NUW && I1NUW) ||
(ICmpInst::isSigned(Pred) && I0NSW && I1NSW) ||
(ICmpInst::isEquality(Pred) &&
((I0NUW || I0NSW) && (I1NUW || I1NSW)))) {
return new ICmpInst(CmpPredicate::getSwapped(CmpPred), B,
ConstantInt::get(Op0->getType(), 0));
}
}
}

// Try to optimize equality comparisons against alloca-based pointers.
if (Op0->getType()->isPointerTy() && I.isEquality()) {
assert(Op1->getType()->isPointerTy() &&
Expand Down
111 changes: 111 additions & 0 deletions llvm/test/Transforms/InstCombine/icmp-subadd.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define i1 @test-same-operands-sub-add-nsw-icmp-sgt(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-icmp-sgt(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[B]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%sub = sub nsw i8 %a, %b
%add = add nsw i8 %a, %b
%cmp = icmp sgt i8 %sub, %add
ret i1 %cmp
}

define i1 @test-same-operands-sub-add-nsw-icmp-slt(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-icmp-slt(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%sub = sub nsw i8 %a, %b
%add = add nsw i8 %a, %b
%cmp = icmp slt i8 %sub, %add
ret i1 %cmp
}

define i1 @test-same-operands-sub-add-nsw-icmp-sle(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-icmp-sle(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], -1
; CHECK-NEXT: ret i1 [[CMP]]
;
%sub = sub nsw i8 %a, %b
%add = add nsw i8 %a, %b
%cmp = icmp sle i8 %sub, %add
ret i1 %cmp
}

define i1 @test-same-operands-sub-add-nsw-nuw-icmp-eq(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-nuw-icmp-eq(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[B]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%sub = sub nsw i8 %a, %b
%add = add nuw i8 %a, %b
%cmp = icmp eq i8 %sub, %add
ret i1 %cmp
}

define i1 @test-same-operands-sub-add-nsw-icmp-eq(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-icmp-eq(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[B]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%sub = sub nsw i8 %a, %b
%add = add nsw i8 %a, %b
%cmp = icmp eq i8 %sub, %add
ret i1 %cmp
}

define i1 @test-add-sub-nsw-icmp-sgt(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-add-sub-nsw-icmp-sgt(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%sub = sub nsw i8 %a, %b
%add = add nsw i8 %a, %b
%cmp = icmp sgt i8 %add, %sub
ret i1 %cmp
}

define i1 @test-add-sub-nuw-icmp-uge(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-add-sub-nuw-icmp-uge(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: ret i1 true
;
%sub = sub nuw i8 %a, %b
%add = add nuw i8 %a, %b
%cmp = icmp uge i8 %add, %sub
ret i1 %cmp
}

; Check not folded
define i1 @test-add-sub-nuw-icmp-sge(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-add-sub-nuw-icmp-sge(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: [[SUB:%.*]] = sub nuw i8 [[A]], [[B]]
; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[A]], [[B]]
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i8 [[ADD]], [[SUB]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%sub = sub nuw i8 %a, %b
%add = add nuw i8 %a, %b
%cmp = icmp sge i8 %add, %sub
ret i1 %cmp
}

define i1 @test-add-swap-sub-nuw-icmp-uge(i8 %a, i8 %b) {
; CHECK-LABEL: define i1 @test-add-swap-sub-nuw-icmp-uge(
; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
; CHECK-NEXT: ret i1 true
;
%sub = sub nuw i8 %a, %b
%add = add nuw i8 %b, %a
%cmp = icmp uge i8 %add, %sub
ret i1 %cmp
}
Loading