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 *OrC; + // icmp(X | OrC, C) --> icmp(X, 0) + if (C.isNonNegative() && match(Or, m_Or(m_Value(X), m_APInt(OrC)))) { + switch (Pred) { + // X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0 + case ICmpInst::ICMP_SLT: + // X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0 + case ICmpInst::ICMP_SGE: + if (OrC->sge(C)) + return new ICmpInst(Pred, X, ConstantInt::getNullValue(X->getType())); + break; + // X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0 + case ICmpInst::ICMP_SLE: + // X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0 + case ICmpInst::ICMP_SGT: + if (OrC->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 @@ -4633,8 +4633,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 @@ -4644,8 +4643,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]] ; @@ -4697,8 +4695,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 @@ -4708,8 +4705,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]] ; @@ -4730,8 +4726,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 @@ -4741,8 +4736,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]] ; @@ -4794,8 +4788,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 @@ -4805,8 +4798,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]] ; @@ -4965,7 +4957,7 @@ ; CHECK-LABEL: @or_positive_sgt_zero_multi_use( ; CHECK-NEXT: [[B:%.*]] = or i8 [[A:%.*]], 24 ; CHECK-NEXT: call void @use_i8(i8 [[B]]) -; 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