Skip to content

Commit 430db44

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
1 parent 92b7743 commit 430db44

File tree

2 files changed

+72
-27
lines changed

2 files changed

+72
-27
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,6 +3491,57 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
34913491
Value *And = Builder.CreateAnd(BOp0, NotBOC);
34923492
return new ICmpInst(Pred, And, NotBOC);
34933493
}
3494+
// (icmp eq (or (select cond, 0, NonZero), Other), 0)
3495+
// -> (and cond, (icmp eq Other, 0))
3496+
// (icmp ne (or (select cond, NonZero, 0), Other), 0)
3497+
// -> (or cond, (icmp ne Other, 0))
3498+
Value *Cond, *TV, *FV, *Other;
3499+
if (C.isZero() &&
3500+
match(BO,
3501+
m_OneUse(m_c_Or(m_Select(m_Value(Cond), m_Value(TV), m_Value(FV)),
3502+
m_Value(Other))))) {
3503+
const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3504+
// Easy case is if eq/ne matches whether 0 is trueval/falseval.
3505+
if (Pred == ICmpInst::ICMP_EQ
3506+
? (match(TV, m_SpecificInt(C)) && isKnownNonZero(FV, Q))
3507+
: (match(FV, m_SpecificInt(C)) && isKnownNonZero(TV, Q))) {
3508+
Value *Cmp = Builder.CreateICmp(
3509+
Pred, Other, Constant::getNullValue(Other->getType()));
3510+
return BinaryOperator::Create(
3511+
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3512+
Cond);
3513+
}
3514+
// Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3515+
// case we need to invert the select condition so we need to be careful to
3516+
// avoid creating extra instructions.
3517+
// (icmp ne (or (select cond, 0, NonZero), Other), 0)
3518+
// -> (or (not cond), (icmp ne Other, 0))
3519+
// (icmp eq (or (select cond, NonZero, 0), Other), 0)
3520+
// -> (and (not cond), (icmp eq Other, 0))
3521+
if (Pred == ICmpInst::ICMP_EQ
3522+
? (match(FV, m_SpecificInt(C)) && isKnownNonZero(TV, Q))
3523+
: (match(TV, m_SpecificInt(C)) && isKnownNonZero(FV, Q))) {
3524+
Value *NotCond = nullptr;
3525+
// If the select is one use, we are essentially replacing select with
3526+
// `(not Cond)`.
3527+
auto SelectMatcher =
3528+
m_Select(m_Specific(Cond), m_Specific(TV), m_Specific(FV));
3529+
if (match(BO, m_c_Or(m_OneUse(SelectMatcher), m_Value())))
3530+
NotCond = Builder.CreateNot(Cond);
3531+
// Otherwise, see if we can get NotCond for free.
3532+
else if (match(BO, m_c_Or(SelectMatcher, m_Value())))
3533+
NotCond =
3534+
getFreelyInverted(Cond, /*WillInvertAllUses=*/false, &Builder);
3535+
3536+
if (NotCond) {
3537+
Value *Cmp = Builder.CreateICmp(
3538+
Pred, Other, Constant::getNullValue(Other->getType()));
3539+
return BinaryOperator::Create(
3540+
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or,
3541+
Cmp, NotCond);
3542+
}
3543+
}
3544+
}
34943545
break;
34953546
}
34963547
case Instruction::UDiv:

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ 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
12-
; CHECK-NEXT: ret i1 [[R]]
8+
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX:%.*]], 0
9+
; CHECK-NEXT: [[R1:%.*]] = and i1 [[R]], [[C0:%.*]]
10+
; CHECK-NEXT: ret i1 [[R1]]
1311
;
1412
%y = add nuw i8 %yy, 1
1513
%sel = select i1 %c0, i8 0, i8 %y
@@ -52,11 +50,9 @@ 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
59-
; CHECK-NEXT: ret i1 [[R]]
53+
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0
54+
; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[C0:%.*]]
55+
; CHECK-NEXT: ret i1 [[R1]]
6056
;
6157
%y = add nuw i8 %yy, 1
6258
%sel = select i1 %c0, i8 %y, i8 0
@@ -82,11 +78,10 @@ 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
89-
; CHECK-NEXT: ret i1 [[R]]
81+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true
82+
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0
83+
; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[TMP1]]
84+
; CHECK-NEXT: ret i1 [[R1]]
9085
;
9186
%y = add nuw i8 %yy, 1
9287
%sel = select i1 %c0, i8 0, i8 %y
@@ -112,11 +107,10 @@ 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
119-
; CHECK-NEXT: ret i1 [[R]]
110+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true
111+
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX:%.*]], 0
112+
; CHECK-NEXT: [[R1:%.*]] = and i1 [[R]], [[TMP1]]
113+
; CHECK-NEXT: ret i1 [[R1]]
120114
;
121115
%y = add nuw i8 %yy, 1
122116
%sel = select i1 %c0, i8 %y, i8 0
@@ -172,13 +166,13 @@ 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: [[R:%.*]] = icmp eq i8 [[SELX:%.*]], 0
173+
; CHECK-NEXT: [[R1:%.*]] = and i1 [[R]], [[TMP1]]
180174
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
181-
; CHECK-NEXT: ret i1 [[R]]
175+
; CHECK-NEXT: ret i1 [[R1]]
182176
;
183177
%c0 = icmp ugt i8 %a, %b
184178
%y = add nuw i8 %yy, 1
@@ -275,11 +269,11 @@ define i1 @src_tv_ne_invert(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
275269
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[NOT_C0]], i8 [[Y]], i8 0
276270
; CHECK-NEXT: [[CC:%.*]] = or i1 [[C0]], [[C1:%.*]]
277271
; 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
272+
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0
273+
; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[NOT_C0]]
280274
; CHECK-NEXT: call void @use.i8(i8 [[SEL]])
281275
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
282-
; CHECK-NEXT: ret i1 [[R]]
276+
; CHECK-NEXT: ret i1 [[R1]]
283277
;
284278
%not_c0 = icmp ugt i8 %a, %b
285279
call void @use.i1(i1 %not_c0)

0 commit comments

Comments
 (0)