Skip to content

Commit 29ae6b9

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 4227145 commit 29ae6b9

File tree

2 files changed

+74
-27
lines changed

2 files changed

+74
-27
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,59 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
34983498
Value *And = Builder.CreateAnd(BOp0, NotBOC);
34993499
return new ICmpInst(Pred, And, NotBOC);
35003500
}
3501+
// (icmp eq (or (select cond, 0, NonZero), Other))
3502+
// -> (and cond, (icmp eq Other, 0))
3503+
// (icmp ne (or (select cond, NonZero, 0), Other))
3504+
// -> (or cond, (icmp ne Other, 0))
3505+
// (icmp ne (or (select cond, 0, NonZero), Other))
3506+
// -> (or (not cond), (icmp ne Other, 0))
3507+
// (icmp eq (or (select cond, NonZero, 0), Other))
3508+
// -> (and (not cond), (icmp eq Other, 0))
3509+
Value *Cond, *TV, *FV, *Other;
3510+
if (C.isZero() && BO->hasOneUse() &&
3511+
match(BO, m_c_Or(m_Select(m_Value(Cond), m_Value(TV), m_Value(FV)),
3512+
m_Value(Other)))) {
3513+
const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3514+
auto IsNonZero = [&](Value *V) {
3515+
return isKnownNonZero(V, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT);
3516+
};
3517+
// Easy case is if eq/ne matches whether 0 is trueval/falseval.
3518+
if (Pred == ICmpInst::ICMP_EQ
3519+
? (match(TV, m_SpecificInt(C)) && IsNonZero(FV))
3520+
: (match(FV, m_SpecificInt(C)) && IsNonZero(TV))) {
3521+
Value *Cmp = Builder.CreateICmp(
3522+
Pred, Other, Constant::getNullValue(Other->getType()));
3523+
return BinaryOperator::Create(
3524+
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3525+
Cond);
3526+
}
3527+
// Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3528+
// case we need to invert the select condition so we need to be careful to
3529+
// avoid creating extra instructions.
3530+
if (Pred == ICmpInst::ICMP_EQ
3531+
? (match(FV, m_SpecificInt(C)) && IsNonZero(TV))
3532+
: (match(TV, m_SpecificInt(C)) && IsNonZero(FV))) {
3533+
Value *NotCond = nullptr;
3534+
// If the select is one use, we are essentially replacing select with
3535+
// `(not Cond)`.
3536+
if (match(BO, m_c_Or(m_OneUse(m_Select(m_Specific(Cond), m_Specific(TV),
3537+
m_Specific(FV))),
3538+
m_Value())))
3539+
NotCond = Builder.CreateNot(Cond);
3540+
// Otherwise, see if we can get NotCond for free.
3541+
else
3542+
NotCond =
3543+
getFreelyInverted(Cond, /*WillInvertAllUses=*/false, &Builder);
3544+
3545+
if (NotCond) {
3546+
Value *Cmp = Builder.CreateICmp(
3547+
Pred, Other, Constant::getNullValue(Other->getType()));
3548+
return BinaryOperator::Create(
3549+
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or,
3550+
Cmp, NotCond);
3551+
}
3552+
}
3553+
}
35013554
break;
35023555
}
35033556
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
@@ -225,11 +219,11 @@ define i1 @src_tv_ne_invert(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
225219
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[NOT_C0]], i8 [[Y]], i8 0
226220
; CHECK-NEXT: [[CC:%.*]] = or i1 [[C0]], [[C1:%.*]]
227221
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[CC]], i8 [[Y]], i8 [[B]]
228-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
229-
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
222+
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0
223+
; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[NOT_C0]]
230224
; CHECK-NEXT: call void @use.i8(i8 [[SEL]])
231225
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
232-
; CHECK-NEXT: ret i1 [[R]]
226+
; CHECK-NEXT: ret i1 [[R1]]
233227
;
234228
%not_c0 = icmp ugt i8 %a, %b
235229
call void @use.i1(i1 %not_c0)

0 commit comments

Comments
 (0)