[llvm] 142aca7 - [InstCombine] fold sub of min/max of sub with common operand
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 4 16:57:23 PDT 2022
Author: Sanjay Patel
Date: 2022-07-04T18:55:24-04:00
New Revision: 142aca7741d5b06207e87bf4880fbe308c8d6823
URL: https://github.com/llvm/llvm-project/commit/142aca7741d5b06207e87bf4880fbe308c8d6823
DIFF: https://github.com/llvm/llvm-project/commit/142aca7741d5b06207e87bf4880fbe308c8d6823.diff
LOG: [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
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/sub-minmax.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index b429280aae6f..f53cce10f61e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -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;
}
diff --git a/llvm/test/Transforms/InstCombine/sub-minmax.ll b/llvm/test/Transforms/InstCombine/sub-minmax.ll
index 0191ebe9daec..e2c407ff3c8e 100644
--- a/llvm/test/Transforms/InstCombine/sub-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/sub-minmax.ll
@@ -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:%.*]]) {
More information about the llvm-commits
mailing list