[InstCombine] Fold uadd.sat(a, b) == 0 and usub.sat(a, b) == 0

This adds folds for comparing uadd.sat/usub.sat with zero:

 * uadd.sat(a, b) == 0 => a == 0 && b == 0 => (a | b) == 0
 * usub.sat(a, b) == 0 => a <= b

And inverted forms for !=.

Differential Revision: https://reviews.llvm.org/D69224

llvm-svn: 375374
This commit is contained in:
Nikita Popov 2019-10-20 20:19:42 +00:00
parent 5fa36e42c4
commit b1b7a2f7b6
2 changed files with 30 additions and 10 deletions

View File

@ -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;
}

View File

@ -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)