Skip to content
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

[InstCombine] Fold (icmp eq/ne (add nuw x, y), 0) -> (icmp eq/ne (or x, y), 0) #88088

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3453,6 +3453,11 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
if (Value *NegVal = dyn_castNegVal(BOp0))
return new ICmpInst(Pred, NegVal, BOp1);
if (BO->hasOneUse()) {
// (add nuw A, B) != 0 -> (or A, B) != 0
if (match(BO, m_NUWAdd(m_Value(), m_Value()))) {
Value *Or = Builder.CreateOr(BOp0, BOp1);
return new ICmpInst(Pred, Or, Constant::getNullValue(BO->getType()));
}
Value *Neg = Builder.CreateNeg(BOp1);
Neg->takeName(BO);
return new ICmpInst(Pred, BOp0, Neg);
Expand Down
36 changes: 28 additions & 8 deletions llvm/test/Transforms/InstCombine/icmp-add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ declare void @use(i32)
define i1 @cvt_icmp_0_zext_plus_zext_eq_i16(i16 %arg, i16 %arg1) {
; CHECK-LABEL: @cvt_icmp_0_zext_plus_zext_eq_i16(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[I:%.*]] = zext i16 [[ARG:%.*]] to i32
; CHECK-NEXT: [[I2:%.*]] = zext i16 [[ARG1:%.*]] to i32
; CHECK-NEXT: [[I3:%.*]] = sub nsw i32 0, [[I]]
; CHECK-NEXT: [[I4:%.*]] = icmp eq i32 [[I2]], [[I3]]
; CHECK-NEXT: [[TMP0:%.*]] = or i16 [[ARG1:%.*]], [[ARG:%.*]]
; CHECK-NEXT: [[I4:%.*]] = icmp eq i16 [[TMP0]], 0
; CHECK-NEXT: ret i1 [[I4]]
;
bb:
Expand All @@ -27,10 +25,8 @@ bb:
define i1 @cvt_icmp_0_zext_plus_zext_eq_i8(i8 %arg, i8 %arg1) {
; CHECK-LABEL: @cvt_icmp_0_zext_plus_zext_eq_i8(
; CHECK-NEXT: bb:
; CHECK-NEXT: [[I:%.*]] = zext i8 [[ARG:%.*]] to i32
; CHECK-NEXT: [[I2:%.*]] = zext i8 [[ARG1:%.*]] to i32
; CHECK-NEXT: [[I3:%.*]] = sub nsw i32 0, [[I]]
; CHECK-NEXT: [[I4:%.*]] = icmp eq i32 [[I2]], [[I3]]
; CHECK-NEXT: [[TMP0:%.*]] = or i8 [[ARG1:%.*]], [[ARG:%.*]]
; CHECK-NEXT: [[I4:%.*]] = icmp eq i8 [[TMP0]], 0
; CHECK-NEXT: ret i1 [[I4]]
;
bb:
Expand Down Expand Up @@ -3003,4 +2999,28 @@ define i1 @icmp_dec_notnonzero(i8 %x) {
ret i1 %c
}

define i1 @icmp_addnuw_nonzero(i8 %x, i8 %y) {
; CHECK-LABEL: @icmp_addnuw_nonzero(
; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[TMP1]], 0
; CHECK-NEXT: ret i1 [[C]]
;
%i = add nuw i8 %x, %y
%c = icmp eq i8 %i, 0
ret i1 %c
}

define i1 @icmp_addnuw_nonzero_fail_multiuse(i32 %x, i32 %y) {
; CHECK-LABEL: @icmp_addnuw_nonzero_fail_multiuse(
; CHECK-NEXT: [[I:%.*]] = add nuw i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[I]], 0
; CHECK-NEXT: call void @use(i32 [[I]])
; CHECK-NEXT: ret i1 [[C]]
;
%i = add nuw i32 %x, %y
%c = icmp eq i32 %i, 0
call void @use(i32 %i)
ret i1 %c
}

declare void @llvm.assume(i1)
Loading