[InstCombine] fold sub-of-umax to 0-usubsat

Op0 - umax(X, Op0) --> 0 - usub.sat(X, Op1)

I'm not sure if this is really an improvement in IR because
we probably have better recognition/analysis for min/max,
but this lines up with the fold we do for the icmp+select
idiom and removes another diff from D98152.

This is similar to the previous fold in the code that was
added with:
83c2fb9f66
baa6a85130

https://alive2.llvm.org/ce/z/5MrVB9
This commit is contained in:
Sanjay Patel 2021-11-09 12:31:15 -05:00
parent de12ca31d4
commit 2a88d00cf2
2 changed files with 10 additions and 4 deletions

View File

@ -2175,6 +2175,12 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
return replaceInstUsesWith(
I, Builder.CreateIntrinsic(Intrinsic::usub_sat, {Ty}, {X, Op1}));
// Op0 - umax(X, Op0) --> 0 - usub.sat(X, Op1)
if (match(Op1, m_OneUse(m_c_UMax(m_Value(X), m_Specific(Op0))))) {
Value *USub = Builder.CreateIntrinsic(Intrinsic::usub_sat, {Ty}, {X, Op0});
return BinaryOperator::CreateNeg(USub);
}
// C - ctpop(X) => ctpop(~X) if C is bitwidth
if (match(Op0, m_SpecificInt(Ty->getScalarSizeInBits())) &&
match(Op1, m_OneUse(m_Intrinsic<Intrinsic::ctpop>(m_Value(X)))))

View File

@ -534,8 +534,8 @@ define i8 @umax_sub_op0_use(i8 %x, i8 %y) {
define i8 @umax_sub_op1(i8 %x, i8 %y) {
; CHECK-LABEL: @umax_sub_op1(
; CHECK-NEXT: [[U:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[Y]], [[U]]
; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
; CHECK-NEXT: [[R:%.*]] = sub i8 0, [[TMP1]]
; CHECK-NEXT: ret i8 [[R]]
;
%u = call i8 @llvm.umax.i8(i8 %x, i8 %y)
@ -545,8 +545,8 @@ define i8 @umax_sub_op1(i8 %x, i8 %y) {
define <2 x i8> @umax_sub_op1_vec_commute(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @umax_sub_op1_vec_commute(
; CHECK-NEXT: [[U:%.*]] = call <2 x i8> @llvm.umax.v2i8(<2 x i8> [[Y:%.*]], <2 x i8> [[X:%.*]])
; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> [[Y]], [[U]]
; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i8> @llvm.usub.sat.v2i8(<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]])
; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> zeroinitializer, [[TMP1]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%u = call <2 x i8> @llvm.umax.v2i8(<2 x i8> %y, <2 x i8> %x)