Skip to content

Commit

Permalink
[InstCombine] Fold (icmp eq/ne (add nuw x, y), 0) -> `(icmp eq/ne (…
Browse files Browse the repository at this point in the history
…or x, y), 0)`

`(icmp eq/ne (or x, y), 0)` is probably easier to analyze than `(icmp
eq/ne x, -y)`

Proof: https://alive2.llvm.org/ce/z/2-VTb6

Closes #88088
  • Loading branch information
goldsteinn committed Apr 9, 2024
1 parent 759bab0 commit 7599d47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
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
16 changes: 6 additions & 10 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 @@ -3005,8 +3001,8 @@ define i1 @icmp_dec_notnonzero(i8 %x) {

define i1 @icmp_addnuw_nonzero(i8 %x, i8 %y) {
; CHECK-LABEL: @icmp_addnuw_nonzero(
; CHECK-NEXT: [[I:%.*]] = sub i8 0, [[Y:%.*]]
; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[I]], [[X:%.*]]
; 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
Expand Down

0 comments on commit 7599d47

Please sign in to comment.