From 7f9c8369f9d8e0e1a1f371245d32343e47e9cf73 Mon Sep 17 00:00:00 2001 From: Noah Goldstein Date: Tue, 9 Apr 2024 14:36:16 -0500 Subject: [PATCH] [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 --- .../InstCombine/InstCombineCompares.cpp | 61 +++++++++++++++++++ .../icmp-or-of-select-with-zero.ll | 59 ++++++++---------- 2 files changed, 88 insertions(+), 32 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 53aa84d53f3085..b0e51c71c797a0 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3487,6 +3487,67 @@ Instruction *InstCombinerImpl::foldICmpBinOpEqualityWithConstant( Value *And = Builder.CreateAnd(BOp0, NotBOC); return new ICmpInst(Pred, And, NotBOC); } + // (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)) + Value *Cond, *TV, *FV, *Other; + if (C.isZero() && + match(BO, m_c_Or(m_Select(m_Value(Cond), m_Value(TV), m_Value(FV)), + m_Value(Other)))) { + const SimplifyQuery Q = SQ.getWithInstruction(&Cmp); + auto IsNonZero = [&](Value *V) { + return isKnownNonZero(V, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT); + }; + // Easy case is if eq/ne matches whether 0 is trueval/falseval. + if (Pred == ICmpInst::ICMP_EQ + ? (match(TV, m_SpecificInt(C)) && IsNonZero(FV)) + : (match(FV, m_SpecificInt(C)) && IsNonZero(TV))) { + 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. + if (Pred == ICmpInst::ICMP_EQ + ? (match(FV, m_SpecificInt(C)) && IsNonZero(TV)) + : (match(TV, m_SpecificInt(C)) && IsNonZero(FV))) { + Value *NotCond = nullptr; + // If the select is one use, we are essentially replacing select with + // `(not Cond)`. + if (match(BO, m_c_Or(m_OneUse(m_Select(m_Specific(Cond), m_Specific(TV), + m_Specific(FV))), + m_Value()))) { + NotCond = Builder.CreateNot(Cond); + } else { + // Otherwise, see if we can get NotCond for free. + Instruction *Ins = dyn_cast(Cond); + bool InvertAll = Ins && InstCombiner::canFreelyInvertAllUsersOf( + Ins, /*IgnoredUser=*/nullptr); + if (Ins) + Builder.SetInsertPoint(Ins); + NotCond = getFreelyInverted(Cond, InvertAll, &Builder); + if (NotCond && InvertAll) { + freelyInvertAllUsersOf(Ins, /*IgnoredUser=*/nullptr); + replaceInstUsesWith(*Ins, NotCond); + } + } + if (NotCond) { + 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: diff --git a/llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll b/llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll index abb91349809f43..f7da7a50973a50 100644 --- a/llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll +++ b/llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll @@ -5,11 +5,9 @@ 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: ret i1 [[R]] +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX:%.*]], 0 +; CHECK-NEXT: [[R1:%.*]] = and i1 [[R]], [[C0:%.*]] +; CHECK-NEXT: ret i1 [[R1]] ; %y = add nuw i8 %yy, 1 %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) { 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: ret i1 [[R]] +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0 +; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[C0:%.*]] +; CHECK-NEXT: ret i1 [[R1]] ; %y = add nuw i8 %yy, 1 %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) { 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: ret i1 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0 +; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[TMP1]] +; CHECK-NEXT: ret i1 [[R1]] ; %y = add nuw i8 %yy, 1 %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) { 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: ret i1 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[C0:%.*]], true +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX:%.*]], 0 +; CHECK-NEXT: [[R1:%.*]] = and i1 [[R]], [[TMP1]] +; CHECK-NEXT: ret i1 [[R1]] ; %y = add nuw i8 %yy, 1 %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) { ; 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: [[R:%.*]] = icmp eq i8 [[SELX:%.*]], 0 +; CHECK-NEXT: [[R1:%.*]] = and i1 [[R]], [[TMP1]] ; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]]) -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: ret i1 [[R1]] ; %c0 = icmp ugt i8 %a, %b %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) { define i1 @src_fv_eq_invert3(i8 %a, i8 %b, i8 %x, i8 %yy) { ; CHECK-LABEL: @src_fv_eq_invert3( -; CHECK-NEXT: [[C0:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[C1:%.*]] = icmp ule i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0 +; 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: [[SEL_OTHER:%.*]] = select i1 [[C0]], i8 [[Y]], i8 [[B]] -; CHECK-NEXT: [[SELX:%.*]] = or i8 [[SEL]], [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[SELX]], 0 +; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C1]], i8 0, i8 [[Y]] +; CHECK-NEXT: [[SEL_OTHER:%.*]] = select i1 [[C1]], i8 [[B]], i8 [[Y]] +; CHECK-NEXT: [[R:%.*]] = and i1 [[TMP1]], [[C1]] ; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]]) ; CHECK-NEXT: call void @use.i8(i8 [[SEL]]) ; 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) { ; 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: [[R:%.*]] = icmp ne i8 [[SELX:%.*]], 0 +; CHECK-NEXT: [[R1:%.*]] = or i1 [[R]], [[NOT_C0]] ; CHECK-NEXT: call void @use.i8(i8 [[SEL]]) ; CHECK-NEXT: call void @use.i8(i8 [[SEL_OTHER]]) -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: ret i1 [[R1]] ; %not_c0 = icmp ugt i8 %a, %b call void @use.i1(i1 %not_c0)