Skip to content

Commit

Permalink
[InstCombine] Fold uadd.sat(a, b) == 0 and usub.sat(a, b) == 0
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nikic committed Oct 20, 2019
1 parent 5fa36e4 commit b1b7a2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
22 changes: 22 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Expand Up @@ -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;
}
Expand Down
18 changes: 8 additions & 10 deletions llvm/test/Transforms/InstCombine/saturating-add-sub.ll
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit b1b7a2f

Please sign in to comment.