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 @@ -1953,6 +1953,24 @@ return new ICmpInst(NewPred, X, NewC); } + BinaryOperator* BO; + const APInt* LHS; + // icmp(bin(X, Y) | LHS, C) --> icmp(bin(X, Y)) iff LHS > C s>= 0 + if (match(Or, m_c_Or(m_BinOp(BO), m_APInt(LHS)))) { + if (LHS->isStrictlyPositive() && C.isNonNegative() && LHS->sgt(C)) { + switch (Pred) { + case ICmpInst::ICMP_SLE: + case ICmpInst::ICMP_SLT: + return new ICmpInst(ICmpInst::ICMP_SLT, BO, ConstantInt::getNullValue(BO->getType())); + case ICmpInst::ICMP_SGE: + case ICmpInst::ICMP_SGT: + return new ICmpInst(ICmpInst::ICMP_SGE, BO, ConstantInt::getNullValue(BO->getType())); + default: + break; + } + } + } + if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse()) return nullptr; diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -4631,10 +4631,10 @@ define i1 @mul_add_constant_sgt(i8 %a, i8 %b) { ; CHECK-LABEL: @mul_add_constant_sgt( ; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[A:%.*]], [[B:%.*]] -; CHECK-NEXT: [[ADD:%.*]] = or i8 [[MUL1]], 24 -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[ADD]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[MUL1]], -1 ; CHECK-NEXT: ret i1 [[CMP]] ; + %mul1 = mul nsw i8 %a, %b %add = or i8 %mul1, 24 %cmp = icmp sgt i8 %add, 0 @@ -4644,8 +4644,7 @@ define <2 x i1> @mul_add_constant_vec_sgt(<2 x i8> %a, <2 x i8> %b) { ; CHECK-LABEL: @mul_add_constant_vec_sgt( ; CHECK-NEXT: [[MUL1:%.*]] = mul nsw <2 x i8> [[A:%.*]], [[B:%.*]] -; CHECK-NEXT: [[ADD:%.*]] = or <2 x i8> [[MUL1]], -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[ADD]], zeroinitializer +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[MUL1]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; @@ -4669,8 +4668,7 @@ define i1 @mul_add_constant_sle(i8 %a, i8 %b) { ; CHECK-LABEL: @mul_add_constant_sle( ; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i8 [[A:%.*]], [[B:%.*]] -; CHECK-NEXT: [[ADD:%.*]] = or i8 [[MUL1]], 24 -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[ADD]], 1 +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[MUL1]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %mul1 = mul nsw i8 %a, %b @@ -4682,8 +4680,7 @@ define <2 x i1> @mul_add_constant_vec_sle(<2 x i8> %a, <2 x i8> %b) { ; CHECK-LABEL: @mul_add_constant_vec_sle( ; CHECK-NEXT: [[MUL1:%.*]] = mul nsw <2 x i8> [[A:%.*]], [[B:%.*]] -; CHECK-NEXT: [[ADD:%.*]] = or <2 x i8> [[MUL1]], -; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[ADD]], +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[MUL1]], zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[CMP]] ;