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,30 @@ return new ICmpInst(NewPred, X, NewC); } + const APInt *LHS; + // icmp(X | LHS, C) --> icmp(X, 0) + if (C.isNonNegative() && match(Or, m_OneUse(m_Or(m_Value(X), m_APInt(LHS))))) { + switch (Pred) { + // X | LHS s< C --> X s< 0 iff LHS s>= C s>= 0 + case ICmpInst::ICMP_SLT: + // X | LHS s>= C --> X s>= 0 iff LHS s>= C s>= 0 + case ICmpInst::ICMP_SGE: + if (LHS->sge(C)) + return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType())); + break; + // X | LHS s<= C --> X s< 0 iff LHS s> C s>= 0 + case ICmpInst::ICMP_SLE: + // X | LHS s> C --> X s>= 0 iff LHS s> C s>= 0 + case ICmpInst::ICMP_SGT: + if (LHS->sgt(C)) + return new ICmpInst(ICmpInst::getFlippedStrictnessPredicate(Pred), X, + ConstantInt::getNullValue(X->getType())); + break; + 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 @@ -4634,8 +4634,7 @@ define i1 @or_positive_sgt_zero(i8 %a) { ; CHECK-LABEL: @or_positive_sgt_zero( -; CHECK-NEXT: [[B:%.*]] = or i8 [[A:%.*]], 24 -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[A:%.*]], -1 ; CHECK-NEXT: ret i1 [[CMP]] ; %b = or i8 %a, 24 @@ -4645,8 +4644,7 @@ define <2 x i1> @or_postive_sgt_zero_vec(<2 x i8> %a) { ; CHECK-LABEL: @or_postive_sgt_zero_vec( -; CHECK-NEXT: [[B:%.*]] = or <2 x i8> [[A:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[B]], zeroinitializer +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; @@ -4678,8 +4676,7 @@ define i1 @or_positive_sge_postive(i8 %a) { ; CHECK-LABEL: @or_positive_sge_postive( -; CHECK-NEXT: [[B:%.*]] = or i8 [[A:%.*]], 24 -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], 23 +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[A:%.*]], -1 ; CHECK-NEXT: ret i1 [[CMP]] ; %b = or i8 %a, 24 @@ -4689,8 +4686,7 @@ define <2 x i1> @or_postive_sge_positive_vec(<2 x i8> %a) { ; CHECK-LABEL: @or_postive_sge_positive_vec( -; CHECK-NEXT: [[B:%.*]] = or <2 x i8> [[A:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[B]], +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> [[A:%.*]], ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; @@ -4701,8 +4697,7 @@ define i1 @or_positive_sle_zero(i8 %a) { ; CHECK-LABEL: @or_positive_sle_zero( -; CHECK-NEXT: [[B:%.*]] = or i8 [[A:%.*]], 24 -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[B]], 1 +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[A:%.*]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %b = or i8 %a, 24 @@ -4712,8 +4707,7 @@ define <2 x i1> @or_postive_sle_zero_vec(<2 x i8> %a) { ; CHECK-LABEL: @or_postive_sle_zero_vec( -; CHECK-NEXT: [[B:%.*]] = or <2 x i8> [[A:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[B]], +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[A:%.*]], zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; @@ -4745,8 +4739,7 @@ define i1 @or_positive_slt_postive(i8 %a) { ; CHECK-LABEL: @or_positive_slt_postive( -; CHECK-NEXT: [[B:%.*]] = or i8 [[A:%.*]], 24 -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[B]], 24 +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[A:%.*]], 0 ; CHECK-NEXT: ret i1 [[CMP]] ; %b = or i8 %a, 24 @@ -4756,8 +4749,7 @@ define <2 x i1> @or_postive_slt_positive_vec(<2 x i8> %a) { ; CHECK-LABEL: @or_postive_slt_positive_vec( -; CHECK-NEXT: [[B:%.*]] = or <2 x i8> [[A:%.*]], -; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[B]], +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[A:%.*]], zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[CMP]] ;