Skip to content

Commit

Permalink
[InstCombine] Fold (icmp eq/ne (or (select cond, 0/NZ, 0/NZ), X), 0)
Browse files Browse the repository at this point in the history
Four cases:
`(icmp eq (or (select cond, 0, NonZero), Other))`
 -> `(and cond, (icmp eq Other, 0))`
`(icmp ne (or (select cond, NonZero, 0), Other))`
 -> `(or cond, (icmp ne Other, 0))`
`(icmp ne (or (select cond, 0, NonZero), Other))`
 -> `(or (not cond), (icmp ne Other, 0))`
`(icmp eq (or (select cond, NonZero, 0), Other))`
 -> `(and (not cond), (icmp eq Other, 0))`

These cases came up in tests on: #88088

Proofs: https://alive2.llvm.org/ce/z/ojGo_J

Closes #88183
  • Loading branch information
goldsteinn committed Aug 5, 2024
1 parent 8fda5ba commit b4ac7f4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
46 changes: 46 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3530,6 +3530,52 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
Value *And = Builder.CreateAnd(BOp0, NotBOC);
return new ICmpInst(Pred, And, NotBOC);
}
// (icmp eq (or (select cond, 0, NonZero), Other), 0)
// -> (and cond, (icmp eq Other, 0))
// (icmp ne (or (select cond, NonZero, 0), Other), 0)
// -> (or cond, (icmp ne Other, 0))
Value *Cond, *TV, *FV, *Other, *Sel;
if (C.isZero() &&
match(BO,
m_OneUse(m_c_Or(m_CombineAnd(m_Value(Sel),
m_Select(m_Value(Cond), m_Value(TV),
m_Value(FV))),
m_Value(Other))))) {
const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
// Easy case is if eq/ne matches whether 0 is trueval/falseval.
if (Pred == ICmpInst::ICMP_EQ
? (match(TV, m_Zero()) && isKnownNonZero(FV, Q))
: (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) {
Value *Cmp = Builder.CreateICmp(
Pred, Other, Constant::getNullValue(Other->getType()));
return BinaryOperator::Create(
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
Cond);
}
// Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
// case we need to invert the select condition so we need to be careful to
// avoid creating extra instructions.
// (icmp ne (or (select cond, 0, NonZero), Other), 0)
// -> (or (not cond), (icmp ne Other, 0))
// (icmp eq (or (select cond, NonZero, 0), Other), 0)
// -> (and (not cond), (icmp eq Other, 0))
//
// Only do this if the inner select has one use, in which case we are
// replacing `select` with `(not cond)`. Otherwise, we will create more
// uses. NB: Trying to freely invert cond doesn't make sense here, as if
// cond was freely invertable, the select arms would have been inverted.
if (Sel->hasOneUse() &&
(Pred == ICmpInst::ICMP_EQ
? (match(FV, m_Zero()) && isKnownNonZero(TV, Q))
: (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) {
Value *NotCond = Builder.CreateNot(Cond);
Value *Cmp = Builder.CreateICmp(
Pred, Other, Constant::getNullValue(Other->getType()));
return BinaryOperator::Create(
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
NotCond);
}
}
break;
}
case Instruction::UDiv:
Expand Down
40 changes: 19 additions & 21 deletions llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ declare void @use.i8(i8)
declare void @use.i1(i1)
define i1 @src_tv_eq(i1 %c0, i8 %x, i8 %yy) {
; CHECK-LABEL: @src_tv_eq(
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 0, i8 [[Y]]
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[C0:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%y = add nuw i8 %yy, 1
Expand Down Expand Up @@ -52,10 +50,8 @@ define i1 @src_tv_eq_fail_tv_nonzero(i1 %c0, i8 %x, i8 %yy) {

define i1 @src_fv_ne(i1 %c0, i8 %x, i8 %yy) {
; CHECK-LABEL: @src_fv_ne(
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 [[Y]], i8 0
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[C0:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%y = add nuw i8 %yy, 1
Expand All @@ -82,10 +78,9 @@ define i1 @src_fv_ne_fail_maybe_zero(i1 %c0, i8 %x, i8 %yy) {

define i1 @src_tv_ne(i1 %c0, i8 %x, i8 %yy) {
; CHECK-LABEL: @src_tv_ne(
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 0, i8 [[Y]]
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP2]], [[TMP1]]
; CHECK-NEXT: ret i1 [[R]]
;
%y = add nuw i8 %yy, 1
Expand All @@ -112,10 +107,9 @@ define i1 @src_tv_ne_fail_cmp_nonzero(i1 %c0, i8 %x, i8 %yy) {

define i1 @src_fv_eq(i1 %c0, i8 %x, i8 %yy) {
; CHECK-LABEL: @src_fv_eq(
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 [[Y]], i8 0
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[X:%.*]], 0
; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP2]], [[TMP1]]
; CHECK-NEXT: ret i1 [[R]]
;
%y = add nuw i8 %yy, 1
Expand Down Expand Up @@ -172,11 +166,11 @@ define i1 @src_fv_eq_invert2(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
; CHECK-LABEL: @src_fv_eq_invert2(
; CHECK-NEXT: [[C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0]], i8 [[Y]], i8 0
; CHECK-NEXT: [[CC:%.*]] = or i1 [[C0]], [[C1:%.*]]
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[CC]], i8 [[Y]], i8 [[B]]
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0]], true
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[X:%.*]], 0
; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP2]], [[TMP1]]
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
; CHECK-NEXT: ret i1 [[R]]
;
Expand All @@ -192,6 +186,9 @@ define i1 @src_fv_eq_invert2(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
ret i1 %r
}




define i1 @src_fv_eq_invert2_fail_wrong_binop(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
; CHECK-LABEL: @src_fv_eq_invert2_fail_wrong_binop(
; CHECK-NEXT: [[C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
Expand Down Expand Up @@ -266,6 +263,7 @@ define i1 @src_fv_eq_invert3(i8 %a, i8 %b, i8 %x, i8 %yy) {
ret i1 %r
}


define i1 @src_tv_ne_invert(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
; CHECK-LABEL: @src_tv_ne_invert(
; CHECK-NEXT: [[NOT_C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
Expand All @@ -275,8 +273,8 @@ define i1 @src_tv_ne_invert(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[NOT_C0]], i8 [[Y]], i8 0
; CHECK-NEXT: [[CC:%.*]] = or i1 [[C0]], [[C1:%.*]]
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[CC]], i8 [[Y]], i8 [[B]]
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[NOT_C0]]
; CHECK-NEXT: call void @use.i8(i8 [[SEL]])
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
; CHECK-NEXT: ret i1 [[R]]
Expand Down

0 comments on commit b4ac7f4

Please sign in to comment.