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 @@ -4384,6 +4384,11 @@ // icmp C, (C-D) -> icmp D, 0 for equalities or if there is no overflow. if (C == Op0 && NoOp1WrapProblem) return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType())); + // (1 - B) == B -> false(1 == 0) for equalities or if there is no overflow. + if (B == Op1 && NoOp0WrapProblem && Pred == CmpInst::ICMP_EQ && + match(A, m_One())) + return new ICmpInst(CmpInst::ICMP_EQ, A, + Constant::getNullValue(B->getType()) /*Zero*/); // Convert sub-with-unsigned-overflow comparisons into a comparison of args. // (A - B) u>/u<= A --> B u>/u<= A diff --git a/llvm/test/Transforms/InstCombine/icmp-sub.ll b/llvm/test/Transforms/InstCombine/icmp-sub.ll --- a/llvm/test/Transforms/InstCombine/icmp-sub.ll +++ b/llvm/test/Transforms/InstCombine/icmp-sub.ll @@ -561,3 +561,14 @@ bb_exit: ret void } + +define i1 @sub_false(i32 %x) { +; CHECK-LABEL: @sub_false( +; CHECK-NEXT: entry: +; CHECK-NEXT: ret i1 false +; +entry: + %sub = sub i32 1, %x + %cmp = icmp eq i32 %sub, %x + ret i1 %cmp +}