Skip to content

[InstCombine] Fold usub_sat((sub nuw C1, A), C2) to usub_sat(C1 - C2, A) or 0 #82280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,8 +2139,22 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
}

// usub_sat((sub nuw C, A), C1) -> usub_sat(usub_sat(C, C1), A)
// which after that:
// usub_sat((sub nuw C, A), C1) -> usub_sat(C - C1, A) if C1 u< C
// usub_sat((sub nuw C, A), C1) -> 0 otherwise
Constant *C, *C1;
Value *A;
if (IID == Intrinsic::usub_sat &&
match(Arg0, m_NUWSub(m_ImmConstant(C), m_Value(A))) &&
match(Arg1, m_ImmConstant(C1))) {
auto *NewC = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, C, C1);
auto *NewSub =
Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, NewC, A);
return replaceInstUsesWith(*SI, NewSub);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative way to phrase this would be like this: https://alive2.llvm.org/ce/z/_Gw9fQ

This way we don't have to check the C2->ult(C1) condition explicitly, which means that we can more easily generalize this to non-splat vectors.

It would look something like this:

if (IID == Intrinsic::usub_sat &&
    match(Arg0, m_NUWSub(m_ImmConstant(C1), m_Value(A))) &&
    match(Arg1, m_ImmConstant(C2))) {
  auto *NewC = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, C1, C2);
  auto *NewSub = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, NewC, A);
  return replaceInstUsesWith(*SI, NewSub);
}

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that when phrased like this, the following generalization to uadd.sat + add nuw also holds: https://alive2.llvm.org/ce/z/Y4VGfs


// ssub.sat(X, C) -> sadd.sat(X, -C) if C != MIN
Constant *C;
if (IID == Intrinsic::ssub_sat && match(Arg1, m_Constant(C)) &&
C->isNotMinSignedValue()) {
Value *NegVal = ConstantExpr::getNeg(C);
Expand Down
89 changes: 89 additions & 0 deletions llvm/test/Transforms/InstCombine/unsigned_saturated_sub.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,95 @@ declare void @use(i64)
declare void @usei32(i32)
declare void @usei1(i1)

; usub_sat((sub nuw C1, A), C2) to usub_sat(usub_sat(C1 - C2), A)
define i32 @usub_sat_C1_C2(i32 %a){
; CHECK-LABEL: @usub_sat_C1_C2(
; CHECK-NEXT: [[COND:%.*]] = call i32 @llvm.usub.sat.i32(i32 50, i32 [[A:%.*]])
; CHECK-NEXT: ret i32 [[COND]]
;
%add = sub nuw i32 64, %a
%cond = call i32 @llvm.usub.sat.i32(i32 %add, i32 14)
ret i32 %cond
}

define i32 @usub_sat_C1_C2_produce_0(i32 %a){
; CHECK-LABEL: @usub_sat_C1_C2_produce_0(
; CHECK-NEXT: ret i32 0
;
%add = sub nuw i32 14, %a
%cond = call i32 @llvm.usub.sat.i32(i32 %add, i32 14)
ret i32 %cond
}

define i32 @usub_sat_C1_C2_produce_0_too(i32 %a){
; CHECK-LABEL: @usub_sat_C1_C2_produce_0_too(
; CHECK-NEXT: ret i32 0
;
%add = sub nuw i32 12, %a
%cond = call i32 @llvm.usub.sat.i32(i32 %add, i32 14)
ret i32 %cond
}

; vector tests
define <2 x i16> @usub_sat_C1_C2_splat(<2 x i16> %a) {
; CHECK-LABEL: @usub_sat_C1_C2_splat(
; CHECK-NEXT: [[COND:%.*]] = call <2 x i16> @llvm.usub.sat.v2i16(<2 x i16> <i16 50, i16 50>, <2 x i16> [[A:%.*]])
; CHECK-NEXT: ret <2 x i16> [[COND]]
;
%add = sub nuw <2 x i16> <i16 64, i16 64>, %a
%cond = call <2 x i16> @llvm.usub.sat.v2i16(<2 x i16> %add, <2 x i16> <i16 14, i16 14>)
ret <2 x i16> %cond
}

define <2 x i16> @usub_sat_C1_C2_non_splat(<2 x i16> %a) {
; CHECK-LABEL: @usub_sat_C1_C2_non_splat(
; CHECK-NEXT: [[COND:%.*]] = call <2 x i16> @llvm.usub.sat.v2i16(<2 x i16> <i16 30, i16 50>, <2 x i16> [[A:%.*]])
; CHECK-NEXT: ret <2 x i16> [[COND]]
;
%add = sub nuw <2 x i16> <i16 50, i16 64>, %a
%cond = call <2 x i16> @llvm.usub.sat.v2i16(<2 x i16> %add, <2 x i16> <i16 20, i16 14>)
ret <2 x i16> %cond
}

define <2 x i16> @usub_sat_C1_C2_splat_produce_0(<2 x i16> %a){
; CHECK-LABEL: @usub_sat_C1_C2_splat_produce_0(
; CHECK-NEXT: ret <2 x i16> zeroinitializer
;
%add = sub nuw <2 x i16> <i16 14, i16 14>, %a
%cond = call <2 x i16> @llvm.usub.sat.v2i16(<2 x i16> %add, <2 x i16> <i16 14, i16 14>)
ret <2 x i16> %cond
}

define <2 x i16> @usub_sat_C1_C2_splat_produce_0_too(<2 x i16> %a){
; CHECK-LABEL: @usub_sat_C1_C2_splat_produce_0_too(
; CHECK-NEXT: ret <2 x i16> zeroinitializer
;
%add = sub nuw <2 x i16> <i16 12, i16 12>, %a
%cond = call <2 x i16> @llvm.usub.sat.v2i16(<2 x i16> %add, <2 x i16> <i16 14, i16 14>)
ret <2 x i16> %cond
}

define <2 x i16> @usub_sat_C1_C2_non_splat_produce_0_too(<2 x i16> %a){
; CHECK-LABEL: @usub_sat_C1_C2_non_splat_produce_0_too(
; CHECK-NEXT: ret <2 x i16> zeroinitializer
;
%add = sub nuw <2 x i16> <i16 12, i16 13>, %a
%cond = call <2 x i16> @llvm.usub.sat.v2i16(<2 x i16> %add, <2 x i16> <i16 14, i16 15>)
ret <2 x i16> %cond
}

; negative tests this souldn't work
define i32 @usub_sat_C1_C2_without_nuw(i32 %a){
; CHECK-LABEL: @usub_sat_C1_C2_without_nuw(
; CHECK-NEXT: [[ADD:%.*]] = sub i32 12, [[A:%.*]]
; CHECK-NEXT: [[COND:%.*]] = call i32 @llvm.usub.sat.i32(i32 [[ADD]], i32 14)
; CHECK-NEXT: ret i32 [[COND]]
;
%add = sub i32 12, %a
%cond = call i32 @llvm.usub.sat.i32(i32 %add, i32 14)
ret i32 %cond
}

; (a > b) ? a - b : 0 -> usub.sat(a, b)

define i64 @max_sub_ugt(i64 %a, i64 %b) {
Expand Down