diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4192,6 +4192,46 @@ ICmpInst::ICMP_SLT, Op, Constant::getNullValue(Op->getType()), CxtI, DL); } +static Instruction *foldICmpOrXX(ICmpInst &I, const SimplifyQuery &Q, + InstCombinerImpl &IC) { + Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A; + + // Normalize or operand as operand 0. + CmpInst::Predicate Pred = I.getPredicate(); + if (match(Op1, m_c_Or(m_Specific(Op0), m_Value()))) { + std::swap(Op0, Op1); + Pred = ICmpInst::getSwappedPredicate(Pred); + } + + if (!match(Op0, m_c_Or(m_Specific(Op1), m_Value(A)))) + return nullptr; + + // icmp (X | Y) u<= X --> (X | Y) == X + if (Pred == ICmpInst::ICMP_ULE) + return new ICmpInst(ICmpInst::ICMP_EQ, Op0, Op1); + + // icmp (X | Y) u> X --> (X | Y) != X + else if (Pred == ICmpInst::ICMP_UGT) + return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); + + if (ICmpInst::isEquality(Pred) && Op0->hasOneUse()) { + // icmp (X | ~Y) eq/ne ~Y + // if (X | Y).hasOneUse() + // --> (X & ~~Y) eq/ne 0 + if (match(Op1, m_Not(m_Value())) || match(Op1, m_ImmConstant())) + return new ICmpInst(Pred, + IC.Builder.CreateAnd(A, IC.Builder.CreateNot(Op1)), + Constant::getNullValue(Op1->getType())); + // icmp (X | noundef Y) eq/ne X + // if (X | noundef Y).hasOneUse() + // --> (X & noundef Y) eq/ne noundef Y + else if (isGuaranteedNotToBeUndefOrPoison(A, Q.AC, Q.CxtI, Q.DT, + /*Depth*/ 0)) + return new ICmpInst(Pred, IC.Builder.CreateAnd(Op1, A), A); + } + return nullptr; +} + static Instruction *foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A; @@ -4581,7 +4621,9 @@ ConstantExpr::getNeg(RHSC)); } - if (Instruction * R = foldICmpXorXX(I, Q, *this)) + if (Instruction *R = foldICmpXorXX(I, Q, *this)) + return R; + if (Instruction *R = foldICmpOrXX(I, Q, *this)) return R; { diff --git a/llvm/test/Transforms/InstCombine/icmp-of-or-x.ll b/llvm/test/Transforms/InstCombine/icmp-of-or-x.ll --- a/llvm/test/Transforms/InstCombine/icmp-of-or-x.ll +++ b/llvm/test/Transforms/InstCombine/icmp-of-or-x.ll @@ -8,7 +8,7 @@ define i1 @or_ugt(i8 %x, i8 %y) { ; CHECK-LABEL: @or_ugt( ; CHECK-NEXT: [[XN1:%.*]] = or i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[XN1]], [[X]] +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[XN1]], [[X]] ; CHECK-NEXT: ret i1 [[R]] ; %xn1 = or i8 %x, %y @@ -19,7 +19,7 @@ define <2 x i1> @or_ule(<2 x i8> %x, <2 x i8> %y) { ; CHECK-LABEL: @or_ule( ; CHECK-NEXT: [[XN1:%.*]] = or <2 x i8> [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp ule <2 x i8> [[XN1]], [[X]] +; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[XN1]], [[X]] ; CHECK-NEXT: ret <2 x i1> [[R]] ; %xn1 = or <2 x i8> %x, %y @@ -70,8 +70,8 @@ define i1 @or_eq_noundef(i8 %x, i8 noundef %y) { ; CHECK-LABEL: @or_eq_noundef( -; CHECK-NEXT: [[XN1:%.*]] = or i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[XN1]], [[X]] +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[TMP1]], [[Y]] ; CHECK-NEXT: ret i1 [[R]] ; %xn1 = or i8 %x, %y @@ -81,9 +81,8 @@ define i1 @or_eq_notY(i8 %x, i8 %y) { ; CHECK-LABEL: @or_eq_notY( -; CHECK-NEXT: [[NY:%.*]] = xor i8 [[Y:%.*]], -1 -; CHECK-NEXT: [[OR:%.*]] = or i8 [[NY]], [[X:%.*]] -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[OR]], [[NY]] +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %ny = xor i8 %y, -1 @@ -107,8 +106,8 @@ define <2 x i1> @or_ne_vecC(<2 x i8> %x) { ; CHECK-LABEL: @or_ne_vecC( -; CHECK-NEXT: [[OR:%.*]] = or <2 x i8> [[X:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[OR]], +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i8> [[TMP1]], zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %or = or <2 x i8> %x, @@ -129,8 +128,8 @@ define <2 x i1> @or_ne_noundef(<2 x i8> %x, <2 x i8> noundef %y) { ; CHECK-LABEL: @or_ne_noundef( -; CHECK-NEXT: [[XN1:%.*]] = or <2 x i8> [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[XN1]], [[X]] +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[R:%.*]] = icmp ne <2 x i8> [[TMP1]], [[Y]] ; CHECK-NEXT: ret <2 x i1> [[R]] ; %xn1 = or <2 x i8> %x, %y