[InstCombine] fold sub of min/max of sub with common operand

x - max(x - y, 0) --> min(x, y)
  x - min(x - y, 0) --> max(x, y)

https://alive2.llvm.org/ce/z/2YkqFe

issue #55470
This commit is contained in:
Sanjay Patel 2022-07-04 18:55:24 -04:00
parent 8ef053273a
commit 142aca7741
2 changed files with 23 additions and 11 deletions

View File

@ -1784,6 +1784,15 @@ static Instruction *foldSubOfMinMax(BinaryOperator &I,
}
}
// sub Op0, smin((sub nsw Op0, Z), 0) --> smax Op0, Z
// sub Op0, smax((sub nsw Op0, Z), 0) --> smin Op0, Z
if (MinMax->isSigned() && match(Y, m_ZeroInt()) &&
match(X, m_NSWSub(m_Specific(Op0), m_Value(Z)))) {
Intrinsic::ID InvID = getInverseMinMaxIntrinsic(MinMax->getIntrinsicID());
Function *F = Intrinsic::getDeclaration(I.getModule(), InvID, Ty);
return CallInst::Create(F, {Op0, Z});
}
return nullptr;
}

View File

@ -882,9 +882,7 @@ define i8 @sub_add_umin_use_m(i8 %x, i8 %y, i8 %z) {
define <2 x i8> @sub_smax0_sub_nsw(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: define {{[^@]+}}@sub_smax0_sub_nsw
; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) {
; CHECK-NEXT: [[SUB:%.*]] = sub nsw <2 x i8> [[X]], [[Y]]
; CHECK-NEXT: [[M:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[SUB]], <2 x i8> <i8 0, i8 poison>)
; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> [[X]], [[M]]
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.smin.v2i8(<2 x i8> [[X]], <2 x i8> [[Y]])
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%sub = sub nsw <2 x i8> %x, %y
@ -899,7 +897,7 @@ define i8 @sub_smax0_sub_nsw_use(i8 %x, i8 %y) {
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[X]], [[Y]]
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[SUB]], i8 0)
; CHECK-NEXT: call void @use8(i8 [[M]])
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X]], [[M]]
; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.smin.i8(i8 [[X]], i8 [[Y]])
; CHECK-NEXT: ret i8 [[R]]
;
%sub = sub nsw i8 %x, %y
@ -909,6 +907,8 @@ define i8 @sub_smax0_sub_nsw_use(i8 %x, i8 %y) {
ret i8 %r
}
; negative test - must have nsw
define i8 @sub_smax0_sub(i8 %x, i8 %y) {
; CHECK-LABEL: define {{[^@]+}}@sub_smax0_sub
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
@ -923,15 +923,17 @@ define i8 @sub_smax0_sub(i8 %x, i8 %y) {
ret i8 %r
}
; negative test - wrong op
define i8 @sub_smax0_sub_commute(i8 %x, i8 %y) {
; CHECK-LABEL: define {{[^@]+}}@sub_smax0_sub_commute
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[Y]]
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[X]], [[Y]]
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[SUB]], i8 0)
; CHECK-NEXT: [[R:%.*]] = sub i8 [[M]], [[X]]
; CHECK-NEXT: ret i8 [[R]]
;
%sub = sub i8 %x, %y
%sub = sub nsw i8 %x, %y
%m = call i8 @llvm.smax.i8(i8 %sub, i8 0)
%r = sub i8 %m, %x
ret i8 %r
@ -942,8 +944,7 @@ define i8 @sub_smin0_sub_nsw_use(i8 %x, i8 %y) {
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[X]], [[Y]]
; CHECK-NEXT: call void @use8(i8 [[SUB]])
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[SUB]], i8 0)
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X]], [[M]]
; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.smax.i8(i8 [[X]], i8 [[Y]])
; CHECK-NEXT: ret i8 [[R]]
;
%sub = sub nsw i8 %x, %y
@ -956,9 +957,7 @@ define i8 @sub_smin0_sub_nsw_use(i8 %x, i8 %y) {
define <2 x i8> @sub_smin0_sub_nsw(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: define {{[^@]+}}@sub_smin0_sub_nsw
; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) {
; CHECK-NEXT: [[SUB:%.*]] = sub nsw <2 x i8> [[X]], [[Y]]
; CHECK-NEXT: [[M:%.*]] = call <2 x i8> @llvm.smin.v2i8(<2 x i8> [[SUB]], <2 x i8> zeroinitializer)
; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> [[X]], [[M]]
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[X]], <2 x i8> [[Y]])
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%sub = sub nsw <2 x i8> %x, %y
@ -967,6 +966,8 @@ define <2 x i8> @sub_smin0_sub_nsw(<2 x i8> %x, <2 x i8> %y) {
ret <2 x i8> %r
}
; negative test - must have nsw
define i8 @sub_smin0_sub(i8 %x, i8 %y) {
; CHECK-LABEL: define {{[^@]+}}@sub_smin0_sub
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
@ -981,6 +982,8 @@ define i8 @sub_smin0_sub(i8 %x, i8 %y) {
ret i8 %r
}
; negative test - wrong op
define i8 @sub_smin0_sub_nsw_commute(i8 %x, i8 %y) {
; CHECK-LABEL: define {{[^@]+}}@sub_smin0_sub_nsw_commute
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {