diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 601295339ad4..a9f64feb600c 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3076,6 +3076,28 @@ Instruction *InstCombiner::foldICmpEqIntrinsicWithConstant(ICmpInst &Cmp, } break; } + + case Intrinsic::uadd_sat: { + // uadd.sat(a, b) == 0 -> (a | b) == 0 + if (C.isNullValue()) { + Value *Or = Builder.CreateOr(II->getArgOperand(0), II->getArgOperand(1)); + return replaceInstUsesWith(Cmp, Builder.CreateICmp( + Cmp.getPredicate(), Or, Constant::getNullValue(Ty))); + + } + break; + } + + case Intrinsic::usub_sat: { + // usub.sat(a, b) == 0 -> a <= b + if (C.isNullValue()) { + ICmpInst::Predicate NewPred = Cmp.getPredicate() == ICmpInst::ICMP_EQ + ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT; + return ICmpInst::Create(Instruction::ICmp, NewPred, + II->getArgOperand(0), II->getArgOperand(1)); + } + break; + } default: break; } diff --git a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll index fa40baadec9f..f5f719864b09 100644 --- a/llvm/test/Transforms/InstCombine/saturating-add-sub.ll +++ b/llvm/test/Transforms/InstCombine/saturating-add-sub.ll @@ -1158,9 +1158,9 @@ define i8 @test_scalar_uadd_sub_const(i8 %a) { define i1 @scalar_uadd_eq_zero(i8 %a, i8 %b) { ; CHECK-LABEL: @scalar_uadd_eq_zero( -; CHECK-NEXT: [[SAT:%.*]] = call i8 @llvm.uadd.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]]) -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[SAT]], 0 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 0 +; CHECK-NEXT: ret i1 [[TMP2]] ; %sat = call i8 @llvm.uadd.sat.i8(i8 %a, i8 %b) %cmp = icmp eq i8 %sat, 0 @@ -1169,9 +1169,9 @@ define i1 @scalar_uadd_eq_zero(i8 %a, i8 %b) { define i1 @scalar_uadd_ne_zero(i8 %a, i8 %b) { ; CHECK-LABEL: @scalar_uadd_ne_zero( -; CHECK-NEXT: [[SAT:%.*]] = call i8 @llvm.uadd.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]]) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[SAT]], 0 -; CHECK-NEXT: ret i1 [[CMP]] +; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[TMP1]], 0 +; CHECK-NEXT: ret i1 [[TMP2]] ; %sat = call i8 @llvm.uadd.sat.i8(i8 %a, i8 %b) %cmp = icmp ne i8 %sat, 0 @@ -1180,8 +1180,7 @@ define i1 @scalar_uadd_ne_zero(i8 %a, i8 %b) { define i1 @scalar_usub_eq_zero(i8 %a, i8 %b) { ; CHECK-LABEL: @scalar_usub_eq_zero( -; CHECK-NEXT: [[SAT:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]]) -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[SAT]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp ule i8 [[A:%.*]], [[B:%.*]] ; CHECK-NEXT: ret i1 [[CMP]] ; %sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 %b) @@ -1191,8 +1190,7 @@ define i1 @scalar_usub_eq_zero(i8 %a, i8 %b) { define i1 @scalar_usub_ne_zero(i8 %a, i8 %b) { ; CHECK-LABEL: @scalar_usub_ne_zero( -; CHECK-NEXT: [[SAT:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 [[B:%.*]]) -; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[SAT]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]] ; CHECK-NEXT: ret i1 [[CMP]] ; %sat = call i8 @llvm.usub.sat.i8(i8 %a, i8 %b)