diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3292,6 +3292,17 @@ } } + // (sub C, X) == X, C is odd --> false + // (sub C, X) != X, C is odd --> true + if (LBO && LBO->getOperand(1) == RHS && + match(LHS, m_Sub(m_APInt(C), m_Value()))) { + if ((*C & 1) == 1) { + if (Pred == CmpInst::ICMP_EQ) + return ConstantInt::getFalse(getCompareTy(LHS)); + if (Pred == CmpInst::ICMP_NE) + return ConstantInt::getTrue(getCompareTy(LHS)); + } + } // TODO: This is overly constrained. LHS can be any power-of-2. // (1 << X) >u 0x8000 --> false // (1 << X) <=u 0x8000 --> true 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,41 @@ bb_exit: ret void } + +define i1 @sub_false(i32 %x) { +; CHECK-LABEL: @sub_false( +; CHECK-NEXT: ret i1 false +; + %sub = sub i32 1, %x + %cmp = icmp eq i32 %sub, %x + ret i1 %cmp +} + +define i1 @sub_nsw_odd(i8 %x, i8 %c) { +; CHECK-LABEL: @sub_nsw_odd( +; CHECK-NEXT: ret i1 true +; + %sub = sub nsw i8 3, %x + %cmp = icmp ne i8 %x, %sub + ret i1 %cmp +} + +define <2 x i1> @sub_nuw_odd(<2 x i8> %x) { +; CHECK-LABEL: @sub_nuw_odd( +; CHECK-NEXT: ret <2 x i1> +; + %sub = sub nuw <2 x i8> , %x + %cmp = icmp ne <2 x i8> %sub, %x + ret <2 x i1> %cmp +} + +define i1 @sub_nuw_even(i8 %x, i8 %c) { +; CHECK-LABEL: @sub_nuw_even( +; CHECK-NEXT: [[SUB:%.*]] = sub nuw i8 2, [[X:%.*]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[SUB]], [[X]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %sub = sub nuw i8 2, %x + %cmp = icmp ne i8 %sub, %x + ret i1 %cmp +}