Skip to content

Commit 7f9c836

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 c384c8c commit 7f9c836

File tree

2 files changed

+88
-32
lines changed

2 files changed

+88
-32
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,6 +3487,67 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant(
34873487
Value *And = Builder.CreateAnd(BOp0, NotBOC);
34883488
return new ICmpInst(Pred, And, NotBOC);
34893489
}
3490+
// (icmp eq (or (select cond, 0, NonZero), Other))
3491+
// -> (and cond, (icmp eq Other, 0))
3492+
// (icmp ne (or (select cond, NonZero, 0), Other))
3493+
// -> (or cond, (icmp ne Other, 0))
3494+
// (icmp ne (or (select cond, 0, NonZero), Other))
3495+
// -> (or (not cond), (icmp ne Other, 0))
3496+
// (icmp eq (or (select cond, NonZero, 0), Other))
3497+
// -> (and (not cond), (icmp eq Other, 0))
3498+
Value *Cond, *TV, *FV, *Other;
3499+
if (C.isZero() &&
3500+
match(BO, m_c_Or(m_Select(m_Value(Cond), m_Value(TV), m_Value(FV)),
3501+
m_Value(Other)))) {
3502+
const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
3503+
auto IsNonZero = [&](Value *V) {
3504+
return isKnownNonZero(V, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT);
3505+
};
3506+
// Easy case is if eq/ne matches whether 0 is trueval/falseval.
3507+
if (Pred == ICmpInst::ICMP_EQ
3508+
? (match(TV, m_SpecificInt(C)) && IsNonZero(FV))
3509+
: (match(FV, m_SpecificInt(C)) && IsNonZero(TV))) {
3510+
Value *Cmp = Builder.CreateICmp(
3511+
Pred, Other, Constant::getNullValue(Other->getType()));
3512+
return BinaryOperator::Create(
3513+
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or, Cmp,
3514+
Cond);
3515+
}
3516+
// Harder case is if eq/ne matches whether 0 is falseval/trueval. In this
3517+
// case we need to invert the select condition so we need to be careful to
3518+
// avoid creating extra instructions.
3519+
if (Pred == ICmpInst::ICMP_EQ
3520+
? (match(FV, m_SpecificInt(C)) && IsNonZero(TV))
3521+
: (match(TV, m_SpecificInt(C)) && IsNonZero(FV))) {
3522+
Value *NotCond = nullptr;
3523+
// If the select is one use, we are essentially replacing select with
3524+
// `(not Cond)`.
3525+
if (match(BO, m_c_Or(m_OneUse(m_Select(m_Specific(Cond), m_Specific(TV),
3526+
m_Specific(FV))),
3527+
m_Value()))) {
3528+
NotCond = Builder.CreateNot(Cond);
3529+
} else {
3530+
// Otherwise, see if we can get NotCond for free.
3531+
Instruction *Ins = dyn_cast<Instruction>(Cond);
3532+
bool InvertAll = Ins && InstCombiner::canFreelyInvertAllUsersOf(
3533+
Ins, /*IgnoredUser=*/nullptr);
3534+
if (Ins)
3535+
Builder.SetInsertPoint(Ins);
3536+
NotCond = getFreelyInverted(Cond, InvertAll, &Builder);
3537+
if (NotCond && InvertAll) {
3538+
freelyInvertAllUsersOf(Ins, /*IgnoredUser=*/nullptr);
3539+
replaceInstUsesWith(*Ins, NotCond);
3540+
}
3541+
}
3542+
if (NotCond) {
3543+
Value *Cmp = Builder.CreateICmp(
3544+
Pred, Other, Constant::getNullValue(Other->getType()));
3545+
return BinaryOperator::Create(
3546+
Pred == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or,
3547+
Cmp, NotCond);
3548+
}
3549+
}
3550+
}
34903551
break;
34913552
}
34923553
case Instruction::UDiv:

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

Lines changed: 27 additions & 32 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
@@ -35,11 +33,9 @@ define i1 @src_tv_eq_fail_tv_nonzero(i1 %c0, i8 %x, i8 %yy) {
3533

3634
define i1 @src_fv_ne(i1 %c0, i8 %x, i8 %yy) {
3735
; CHECK-LABEL: @src_fv_ne(
38-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
39-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 [[Y]], i8 0
40-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
41-
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
42-
; CHECK-NEXT: ret i1 [[R]]
36+
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0
37+
; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[C0:%.*]]
38+
; CHECK-NEXT: ret i1 [[R1]]
4339
;
4440
%y = add nuw i8 %yy, 1
4541
%sel = select i1 %c0, i8 %y, i8 0
@@ -65,11 +61,10 @@ define i1 @src_fv_ne_fail_maybe_zero(i1 %c0, i8 %x, i8 %yy) {
6561

6662
define i1 @src_tv_ne(i1 %c0, i8 %x, i8 %yy) {
6763
; CHECK-LABEL: @src_tv_ne(
68-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
69-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 0, i8 [[Y]]
70-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
71-
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
72-
; CHECK-NEXT: ret i1 [[R]]
64+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true
65+
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0
66+
; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[TMP1]]
67+
; CHECK-NEXT: ret i1 [[R1]]
7368
;
7469
%y = add nuw i8 %yy, 1
7570
%sel = select i1 %c0, i8 0, i8 %y
@@ -95,11 +90,10 @@ define i1 @src_tv_ne_fail_cmp_nonzero(i1 %c0, i8 %x, i8 %yy) {
9590

9691
define i1 @src_fv_eq(i1 %c0, i8 %x, i8 %yy) {
9792
; CHECK-LABEL: @src_fv_eq(
98-
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
99-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0:%.*]], i8 [[Y]], i8 0
100-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
101-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
102-
; CHECK-NEXT: ret i1 [[R]]
93+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true
94+
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX:%.*]], 0
95+
; CHECK-NEXT: [[R1:%.*]] = and i1 [[R]], [[TMP1]]
96+
; CHECK-NEXT: ret i1 [[R1]]
10397
;
10498
%y = add nuw i8 %yy, 1
10599
%sel = select i1 %c0, i8 %y, i8 0
@@ -155,13 +149,13 @@ define i1 @src_fv_eq_invert2(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
155149
; CHECK-LABEL: @src_fv_eq_invert2(
156150
; CHECK-NEXT: [[C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
157151
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
158-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0]], i8 [[Y]], i8 0
159152
; CHECK-NEXT: [[CC:%.*]] = or i1 [[C0]], [[C1:%.*]]
160153
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[CC]], i8 [[Y]], i8 [[B]]
161-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
162-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
154+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0]], true
155+
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX:%.*]], 0
156+
; CHECK-NEXT: [[R1:%.*]] = and i1 [[R]], [[TMP1]]
163157
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
164-
; CHECK-NEXT: ret i1 [[R]]
158+
; CHECK-NEXT: ret i1 [[R1]]
165159
;
166160
%c0 = icmp ugt i8 %a, %b
167161
%y = add nuw i8 %yy, 1
@@ -177,12 +171,13 @@ define i1 @src_fv_eq_invert2(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
177171

178172
define i1 @src_fv_eq_invert3(i8 %a, i8 %b, i8 %x, i8 %yy) {
179173
; CHECK-LABEL: @src_fv_eq_invert3(
180-
; CHECK-NEXT: [[C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
174+
; CHECK-NEXT: [[C1:%.*]] = icmp ule i8 [[A:%.*]], [[B:%.*]]
175+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
176+
; CHECK-NEXT: [[C0:%.*]] = icmp ugt i8 [[A]], [[B]]
181177
; CHECK-NEXT: [[Y:%.*]] = add nuw i8 [[YY:%.*]], 1
182-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C0]], i8 [[Y]], i8 0
183-
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[C0]], i8 [[Y]], i8 [[B]]
184-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
185-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0
178+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C1]], i8 0, i8 [[Y]]
179+
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[C1]], i8 [[B]], i8 [[Y]]
180+
; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[C1]]
186181
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
187182
; CHECK-NEXT: call void @use.i8(i8 [[SEL]])
188183
; CHECK-NEXT: ret i1 [[R]]
@@ -208,11 +203,11 @@ define i1 @src_tv_ne_invert(i1 %c1, i8 %a, i8 %b, i8 %x, i8 %yy) {
208203
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[NOT_C0]], i8 [[Y]], i8 0
209204
; CHECK-NEXT: [[CC:%.*]] = or i1 [[C0]], [[C1:%.*]]
210205
; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[CC]], i8 [[Y]], i8 [[B]]
211-
; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]]
212-
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX]], 0
206+
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0
207+
; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[NOT_C0]]
213208
; CHECK-NEXT: call void @use.i8(i8 [[SEL]])
214209
; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]])
215-
; CHECK-NEXT: ret i1 [[R]]
210+
; CHECK-NEXT: ret i1 [[R1]]
216211
;
217212
%not_c0 = icmp ugt i8 %a, %b
218213
call void @use.i1(i1 %not_c0)

0 commit comments

Comments
 (0)