Skip to content

Commit b4ac7f4

Browse files
committed
[InstCombine] Fold (icmp eq/ne (or (select cond, 0/NZ, 0/NZ), X), 0)
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
1 parent 8fda5ba commit b4ac7f4

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3530,6 +3530,52 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
35303530
Value *And = Builder.CreateAnd(BOp0, NotBOC);
35313531
return new ICmpInst(Pred, And, NotBOC);
35323532
}
3533+
// (icmp eq (or (select cond, 0, NonZero), Other), 0)
3534+
// -> (and cond, (icmp eq Other, 0))
3535+
// (icmp ne (or (select cond, NonZero, 0), Other), 0)
3536+
// -> (or cond, (icmp ne Other, 0))
3537+
Value *Cond, *TV, *FV, *Other, *Sel;
3538+
if (C.isZero() &&
3539+
match(BO,
3540+
m_OneUse(m_c_Or(m_CombineAnd(m_Value(Sel),
3541+
m_Select(m_Value(Cond), m_Value(TV),
3542+
m_Value(FV))),
3543+
m_Value(Other))))) {
3544+
const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3545+
// Easy case is if eq/ne matches whether 0 is trueval/falseval.
3546+
if (Pred == ICmpInst::ICMP_EQ
3547+
? (match(TV, m_Zero()) && isKnownNonZero(FV, Q))
3548+
: (match(FV, m_Zero()) && isKnownNonZero(TV, Q))) {
3549+
Value *Cmp = Builder.CreateICmp(
3550+
Pred, Other, Constant::getNullValue(Other->getType()));
3551+
return BinaryOperator::Create(
3552+
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3553+
Cond);
3554+
}
3555+
// Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3556+
// case we need to invert the select condition so we need to be careful to
3557+
// avoid creating extra instructions.
3558+
// (icmp ne (or (select cond, 0, NonZero), Other), 0)
3559+
// -> (or (not cond), (icmp ne Other, 0))
3560+
// (icmp eq (or (select cond, NonZero, 0), Other), 0)
3561+
// -> (and (not cond), (icmp eq Other, 0))
3562+
//
3563+
// Only do this if the inner select has one use, in which case we are
3564+
// replacing `select` with `(not cond)`. Otherwise, we will create more
3565+
// uses. NB: Trying to freely invert cond doesn't make sense here, as if
3566+
// cond was freely invertable, the select arms would have been inverted.
3567+
if (Sel->hasOneUse() &&
3568+
(Pred == ICmpInst::ICMP_EQ
3569+
? (match(FV, m_Zero()) && isKnownNonZero(TV, Q))
3570+
: (match(TV, m_Zero()) && isKnownNonZero(FV, Q)))) {
3571+
Value *NotCond = Builder.CreateNot(Cond);
3572+
Value *Cmp = Builder.CreateICmp(
3573+
Pred, Other, Constant::getNullValue(Other->getType()));
3574+
return BinaryOperator::Create(
3575+
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3576+
NotCond);
3577+
}
3578+
}
35333579
break;
35343580
}
35353581
case Instruction::UDiv:

llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ declare void @use.i8(i8)
55
declare void @use.i1(i1)
66
define i1 @src_tv_eq(i1 %c0, i8 %x, i8 %yy) {
77
; CHECK-LABEL: @src_tv_eq(
8-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
9-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 0, i8 [[Y]]
10-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
11-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
8+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
9+
; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[C0:%.*]]
1210
; CHECK-NEXT: ret i1 [[R]]
1311
;
1412
%y = add nuw i8 %yy, 1
@@ -52,10 +50,8 @@ define i1 @src_tv_eq_fail_tv_nonzero(i1 %c0, i8 %x, i8 %yy) {
5250

5351
define i1 @src_fv_ne(i1 %c0, i8 %x, i8 %yy) {
5452
; CHECK-LABEL: @src_fv_ne(
55-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
56-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 [[Y]], i8 0
57-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
58-
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
53+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
54+
; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[C0:%.*]]
5955
; CHECK-NEXT: ret i1 [[R]]
6056
;
6157
%y = add nuw i8 %yy, 1
@@ -82,10 +78,9 @@ define i1 @src_fv_ne_fail_maybe_zero(i1 %c0, i8 %x, i8 %yy) {
8278

8379
define i1 @src_tv_ne(i1 %c0, i8 %x, i8 %yy) {
8480
; CHECK-LABEL: @src_tv_ne(
85-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
86-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 0, i8 [[Y]]
87-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
88-
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
81+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true
82+
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[X:%.*]], 0
83+
; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP2]], [[TMP1]]
8984
; CHECK-NEXT: ret i1 [[R]]
9085
;
9186
%y = add nuw i8 %yy, 1
@@ -112,10 +107,9 @@ define i1 @src_tv_ne_fail_cmp_nonzero(i1 %c0, i8 %x, i8 %yy) {
112107

113108
define i1 @src_fv_eq(i1 %c0, i8 %x, i8 %yy) {
114109
; CHECK-LABEL: @src_fv_eq(
115-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
116-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 [[Y]], i8 0
117-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
118-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
110+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true
111+
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[X:%.*]], 0
112+
; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP2]], [[TMP1]]
119113
; CHECK-NEXT: ret i1 [[R]]
120114
;
121115
%y = add nuw i8 %yy, 1
@@ -172,11 +166,11 @@ define i1 @src_fv_eq_invert2(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
172166
; CHECK-LABEL: @src_fv_eq_invert2(
173167
; CHECK-NEXT: [[C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
174168
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
175-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0]], i8 [[Y]], i8 0
176169
; CHECK-NEXT: [[CC:%.*]] = or i1 [[C0]], [[C1:%.*]]
177170
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[CC]], i8 [[Y]], i8 [[B]]
178-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
179-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
171+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0]], true
172+
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[X:%.*]], 0
173+
; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP2]], [[TMP1]]
180174
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
181175
; CHECK-NEXT: ret i1 [[R]]
182176
;
@@ -192,6 +186,9 @@ define i1 @src_fv_eq_invert2(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
192186
ret i1 %r
193187
}
194188

189+
190+
191+
195192
define i1 @src_fv_eq_invert2_fail_wrong_binop(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
196193
; CHECK-LABEL: @src_fv_eq_invert2_fail_wrong_binop(
197194
; CHECK-NEXT: [[C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
@@ -266,6 +263,7 @@ define i1 @src_fv_eq_invert3(i8 %a, i8 %b, i8 %x, i8 %yy) {
266263
ret i1 %r
267264
}
268265

266+
269267
define i1 @src_tv_ne_invert(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
270268
; CHECK-LABEL: @src_tv_ne_invert(
271269
; CHECK-NEXT: [[NOT_C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
@@ -275,8 +273,8 @@ define i1 @src_tv_ne_invert(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
275273
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[NOT_C0]], i8 [[Y]], i8 0
276274
; CHECK-NEXT: [[CC:%.*]] = or i1 [[C0]], [[C1:%.*]]
277275
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[CC]], i8 [[Y]], i8 [[B]]
278-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
279-
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
276+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i8 [[X:%.*]], 0
277+
; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP1]], [[NOT_C0]]
280278
; CHECK-NEXT: call void @use.i8(i8 [[SEL]])
281279
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
282280
; CHECK-NEXT: ret i1 [[R]]

0 commit comments

Comments
 (0)