Skip to content

Commit 142aca7

Browse files
committed
[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
1 parent 8ef0532 commit 142aca7

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,15 @@ static Instruction *foldSubOfMinMax(BinaryOperator &I,
17841784
}
17851785
}
17861786

1787+
// sub Op0, smin((sub nsw Op0, Z), 0) --> smax Op0, Z
1788+
// sub Op0, smax((sub nsw Op0, Z), 0) --> smin Op0, Z
1789+
if (MinMax->isSigned() && match(Y, m_ZeroInt()) &&
1790+
match(X, m_NSWSub(m_Specific(Op0), m_Value(Z)))) {
1791+
Intrinsic::ID InvID = getInverseMinMaxIntrinsic(MinMax->getIntrinsicID());
1792+
Function *F = Intrinsic::getDeclaration(I.getModule(), InvID, Ty);
1793+
return CallInst::Create(F, {Op0, Z});
1794+
}
1795+
17871796
return nullptr;
17881797
}
17891798

llvm/test/Transforms/InstCombine/sub-minmax.ll

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,7 @@ define i8 @sub_add_umin_use_m(i8 %x, i8 %y, i8 %z) {
882882
define <2 x i8> @sub_smax0_sub_nsw(<2 x i8> %x, <2 x i8> %y) {
883883
; CHECK-LABEL: define {{[^@]+}}@sub_smax0_sub_nsw
884884
; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) {
885-
; CHECK-NEXT: [[SUB:%.*]] = sub nsw <2 x i8> [[X]], [[Y]]
886-
; CHECK-NEXT: [[M:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[SUB]], <2 x i8> <i8 0, i8 poison>)
887-
; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> [[X]], [[M]]
885+
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.smin.v2i8(<2 x i8> [[X]], <2 x i8> [[Y]])
888886
; CHECK-NEXT: ret <2 x i8> [[R]]
889887
;
890888
%sub = sub nsw <2 x i8> %x, %y
@@ -899,7 +897,7 @@ define i8 @sub_smax0_sub_nsw_use(i8 %x, i8 %y) {
899897
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[X]], [[Y]]
900898
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[SUB]], i8 0)
901899
; CHECK-NEXT: call void @use8(i8 [[M]])
902-
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X]], [[M]]
900+
; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.smin.i8(i8 [[X]], i8 [[Y]])
903901
; CHECK-NEXT: ret i8 [[R]]
904902
;
905903
%sub = sub nsw i8 %x, %y
@@ -909,6 +907,8 @@ define i8 @sub_smax0_sub_nsw_use(i8 %x, i8 %y) {
909907
ret i8 %r
910908
}
911909

910+
; negative test - must have nsw
911+
912912
define i8 @sub_smax0_sub(i8 %x, i8 %y) {
913913
; CHECK-LABEL: define {{[^@]+}}@sub_smax0_sub
914914
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
@@ -923,15 +923,17 @@ define i8 @sub_smax0_sub(i8 %x, i8 %y) {
923923
ret i8 %r
924924
}
925925

926+
; negative test - wrong op
927+
926928
define i8 @sub_smax0_sub_commute(i8 %x, i8 %y) {
927929
; CHECK-LABEL: define {{[^@]+}}@sub_smax0_sub_commute
928930
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
929-
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[Y]]
931+
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[X]], [[Y]]
930932
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[SUB]], i8 0)
931933
; CHECK-NEXT: [[R:%.*]] = sub i8 [[M]], [[X]]
932934
; CHECK-NEXT: ret i8 [[R]]
933935
;
934-
%sub = sub i8 %x, %y
936+
%sub = sub nsw i8 %x, %y
935937
%m = call i8 @llvm.smax.i8(i8 %sub, i8 0)
936938
%r = sub i8 %m, %x
937939
ret i8 %r
@@ -942,8 +944,7 @@ define i8 @sub_smin0_sub_nsw_use(i8 %x, i8 %y) {
942944
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
943945
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[X]], [[Y]]
944946
; CHECK-NEXT: call void @use8(i8 [[SUB]])
945-
; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[SUB]], i8 0)
946-
; CHECK-NEXT: [[R:%.*]] = sub i8 [[X]], [[M]]
947+
; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.smax.i8(i8 [[X]], i8 [[Y]])
947948
; CHECK-NEXT: ret i8 [[R]]
948949
;
949950
%sub = sub nsw i8 %x, %y
@@ -956,9 +957,7 @@ define i8 @sub_smin0_sub_nsw_use(i8 %x, i8 %y) {
956957
define <2 x i8> @sub_smin0_sub_nsw(<2 x i8> %x, <2 x i8> %y) {
957958
; CHECK-LABEL: define {{[^@]+}}@sub_smin0_sub_nsw
958959
; CHECK-SAME: (<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) {
959-
; CHECK-NEXT: [[SUB:%.*]] = sub nsw <2 x i8> [[X]], [[Y]]
960-
; CHECK-NEXT: [[M:%.*]] = call <2 x i8> @llvm.smin.v2i8(<2 x i8> [[SUB]], <2 x i8> zeroinitializer)
961-
; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> [[X]], [[M]]
960+
; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[X]], <2 x i8> [[Y]])
962961
; CHECK-NEXT: ret <2 x i8> [[R]]
963962
;
964963
%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) {
967966
ret <2 x i8> %r
968967
}
969968

969+
; negative test - must have nsw
970+
970971
define i8 @sub_smin0_sub(i8 %x, i8 %y) {
971972
; CHECK-LABEL: define {{[^@]+}}@sub_smin0_sub
972973
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {
@@ -981,6 +982,8 @@ define i8 @sub_smin0_sub(i8 %x, i8 %y) {
981982
ret i8 %r
982983
}
983984

985+
; negative test - wrong op
986+
984987
define i8 @sub_smin0_sub_nsw_commute(i8 %x, i8 %y) {
985988
; CHECK-LABEL: define {{[^@]+}}@sub_smin0_sub_nsw_commute
986989
; CHECK-SAME: (i8 [[X:%.*]], i8 [[Y:%.*]]) {

0 commit comments

Comments
 (0)