"(type) minmax ((wide_type) a, (wide_type) b) to minmax (a, b)" is limited
to the single use of the result. It doesn't support:
typedef int v2si __attribute__((vector_size (8)));
typedef long long v2di __attribute__((vector_size (16)));
v2si
func (v2si a, v2si b, v2di *p)
{
v2di x = __builtin_convertvector (a, v2di);
v2di y = __builtin_convertvector (b, v2di);
v2di z = x < y ? x : y;
*p = z;
return __builtin_convertvector (z, v2si);
}
Change it to
minmax ((wide_type) a, (wide_type) b) -> (wide_type) minmax (a, b)
instead and add "(type) ((wide_type) a) -> a" for integer types. Now
we generate
pminsd %xmm1, %xmm0
pmovsxdq %xmm0, %xmm1
movaps %xmm1, (%rdi)
instead of
pmovsxdq %xmm0, %xmm2
pmovsxdq %xmm1, %xmm1
movdqa %xmm2, %xmm0
movdqa %xmm2, %xmm3
pcmpgtq %xmm1, %xmm0
pblendvb %xmm0, %xmm1, %xmm3
movdqa %xmm3, %xmm0
movaps %xmm3, (%rdi)
shufps $232, %xmm3, %xmm0
gcc/
PR middle-end/126784
* match.pd ((type) minmax ((wide_type) a, (wide_type) b)): Changed
to ...
(minmax ((wide_type) a, (wide_type) b)): This.
((type) ((wide_type) a) -> a) New.
gcc/testsuite/
PR middle-end/126784
* g++.dg/tree-ssa/vec-narrow-1.C: Use -msse4 and require int128
for x86.
* g++.dg/tree-ssa/vec-narrow-minmax-2.C: Likewise.
* g++.target/i386/pr126784-1.C: New test.
* g++.target/i386/pr126784-2.C: Likewise.
* gcc.target/i386/pr126784-1.c: Likewise.
* gcc.target/i386/pr126784-2.c: Likewise.
* gcc.target/i386/pr126788-1.c: Likewise.
--
H.J.
From c6495b3b7ebbeae402ab791e5c6a0e42d4e59a2e Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <[email protected]>
Date: Tue, 11 Aug 2026 12:39:54 +0800
Subject: [PATCH] match.pd: Change the MIN/MAX narrowing to MIN/MAX + convert
"(type) minmax ((wide_type) a, (wide_type) b) to minmax (a, b)" is limited
to the single use of the result. It doesn't support:
typedef int v2si __attribute__((vector_size (8)));
typedef long long v2di __attribute__((vector_size (16)));
v2si
func (v2si a, v2si b, v2di *p)
{
v2di x = __builtin_convertvector (a, v2di);
v2di y = __builtin_convertvector (b, v2di);
v2di z = x < y ? x : y;
*p = z;
return __builtin_convertvector (z, v2si);
}
Change it to
minmax ((wide_type) a, (wide_type) b) -> (wide_type) minmax (a, b)
instead and add "(type) ((wide_type) a) -> a" for integer types. Now
we generate
pminsd %xmm1, %xmm0
pmovsxdq %xmm0, %xmm1
movaps %xmm1, (%rdi)
instead of
pmovsxdq %xmm0, %xmm2
pmovsxdq %xmm1, %xmm1
movdqa %xmm2, %xmm0
movdqa %xmm2, %xmm3
pcmpgtq %xmm1, %xmm0
pblendvb %xmm0, %xmm1, %xmm3
movdqa %xmm3, %xmm0
movaps %xmm3, (%rdi)
shufps $232, %xmm3, %xmm0
gcc/
PR middle-end/126784
* match.pd ((type) minmax ((wide_type) a, (wide_type) b)): Changed
to ...
(minmax ((wide_type) a, (wide_type) b)): This.
((type) ((wide_type) a) -> a) New.
gcc/testsuite/
PR middle-end/126784
* g++.dg/tree-ssa/vec-narrow-1.C: Use -msse4 and require int128
for x86.
* g++.dg/tree-ssa/vec-narrow-minmax-2.C: Likewise.
* g++.target/i386/pr126784-1.C: New test.
* g++.target/i386/pr126784-2.C: Likewise.
* gcc.target/i386/pr126784-1.c: Likewise.
* gcc.target/i386/pr126784-2.c: Likewise.
* gcc.target/i386/pr126788-1.c: Likewise.
Signed-off-by: H.J. Lu <[email protected]>
---
gcc/match.pd | 30 ++++++++++++-------
gcc/testsuite/g++.dg/tree-ssa/vec-narrow-1.C | 3 +-
.../g++.dg/tree-ssa/vec-narrow-minmax-2.C | 2 ++
gcc/testsuite/g++.target/i386/pr126784-1.C | 26 ++++++++++++++++
gcc/testsuite/g++.target/i386/pr126784-2.C | 28 +++++++++++++++++
gcc/testsuite/gcc.target/i386/pr126784-1.c | 24 +++++++++++++++
gcc/testsuite/gcc.target/i386/pr126784-2.c | 27 +++++++++++++++++
gcc/testsuite/gcc.target/i386/pr126788-1.c | 22 ++++++++++++++
8 files changed, 150 insertions(+), 12 deletions(-)
create mode 100644 gcc/testsuite/g++.target/i386/pr126784-1.C
create mode 100644 gcc/testsuite/g++.target/i386/pr126784-2.C
create mode 100644 gcc/testsuite/gcc.target/i386/pr126784-1.c
create mode 100644 gcc/testsuite/gcc.target/i386/pr126784-2.c
create mode 100644 gcc/testsuite/gcc.target/i386/pr126788-1.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 50e73177022..62b307b4790 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4673,7 +4673,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& !TYPE_OVERFLOW_SANITIZED (type))
(minus (minmax @0 @1) @2))))
-/* (type) minmax ((wide_type) a, (wide_type) b) -> minmax (a, b)
+/* minmax ((wide_type) a, (wide_type) b) -> (wide_type) minmax (a, b)
when type matches the type of a and b, and wide_type is a wider
type with the same signedness as type. Extension is monotone, so it
commutes with the comparison, and the truncation is then exact. The
@@ -4683,18 +4683,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(for minmax (min max)
MINMAX (MIN_EXPR MAX_EXPR)
(simplify
- (convert (minmax:c@4 (convert@2 @0) (convert@3 @1)))
+ (minmax:c (convert@2 @0) (convert@3 @1))
(if (ANY_INTEGRAL_TYPE_P (type)
- && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@2))
- && types_match (type, TREE_TYPE (@0))
- && types_match (type, TREE_TYPE (@1))
- && types_match (TREE_TYPE (@2), TREE_TYPE (@3))
- && element_precision (TREE_TYPE (@2)) > element_precision (type)
- && TYPE_UNSIGNED (TREE_TYPE (@2)) == TYPE_UNSIGNED (type)
+ && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && types_match (type, TREE_TYPE (@2))
+ && types_match (type, TREE_TYPE (@3))
+ && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
+ && element_precision (TREE_TYPE (@0)) < element_precision (type)
+ && TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (type)
&& (!VECTOR_TYPE_P (type)
- || (single_use (@4)
- && target_supports_op_p (type, MINMAX, optab_vector))))
- (minmax @0 @1))))
+ || target_supports_op_p (TREE_TYPE (@0), MINMAX, optab_vector)))
+ (convert (minmax @0 @1)))))
+
+/* (type) ((wide_type) a) -> a. */
+(simplify
+ (convert (convert@1 @0))
+ (if (ANY_INTEGRAL_TYPE_P (type)
+ && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@1))
+ && types_match (type, TREE_TYPE (@0))
+ && element_precision (type) < element_precision (TREE_TYPE (@1)))
+ @0))
/* max (a, a + CST) -> a + CST where CST is positive. */
/* max (a, a + CST) -> a where CST is negative. */
diff --git a/gcc/testsuite/g++.dg/tree-ssa/vec-narrow-1.C b/gcc/testsuite/g++.dg/tree-ssa/vec-narrow-1.C
index cec5b0ba345..1f863b1d4a2 100644
--- a/gcc/testsuite/g++.dg/tree-ssa/vec-narrow-1.C
+++ b/gcc/testsuite/g++.dg/tree-ssa/vec-narrow-1.C
@@ -1,6 +1,7 @@
// { dg-do compile }
// { dg-options "-O2 -fdump-tree-optimized" }
-// { dg-additional-options "-mavx512vl -mavx512dq" { target { i?86-*-* x86_64-*-* } } }
+// { dg-additional-options "-msse4" { target { i?86-*-* x86_64-*-* } } }
+// { dg-require-effective-target int128 { target { i?86-*-* x86_64-*-* } } }
// Extension is monotone, so it commutes with the comparison and the outer
// truncation is exact. The argument is lanewise, so a widened vector
// MIN/MAX feeding a truncating conversion narrows.
diff --git a/gcc/testsuite/g++.dg/tree-ssa/vec-narrow-minmax-2.C b/gcc/testsuite/g++.dg/tree-ssa/vec-narrow-minmax-2.C
index d7fb7f06f16..7df25de0942 100644
--- a/gcc/testsuite/g++.dg/tree-ssa/vec-narrow-minmax-2.C
+++ b/gcc/testsuite/g++.dg/tree-ssa/vec-narrow-minmax-2.C
@@ -1,5 +1,7 @@
// { dg-do compile }
// { dg-options "-O2 -fdump-tree-optimized" }
+// { dg-additional-options "-msse4" { target { i?86-*-* x86_64-*-* } } }
+// { dg-require-effective-target int128 { target { i?86-*-* x86_64-*-* } } }
typedef int v2si __attribute__((vector_size (8)));
typedef long long v2di __attribute__((vector_size (16)));
diff --git a/gcc/testsuite/g++.target/i386/pr126784-1.C b/gcc/testsuite/g++.target/i386/pr126784-1.C
new file mode 100644
index 00000000000..611d67ed37a
--- /dev/null
+++ b/gcc/testsuite/g++.target/i386/pr126784-1.C
@@ -0,0 +1,26 @@
+/* { dg-do compile { target { *-*-linux* && { ! ia32 } } } } */
+/* { dg-options "-O2 -march=x86-64 -msse4 -std=c++17" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc'). */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } } */
+
+/*
+**_Z4funcDv2_iS_:
+**.LFB0:
+** .cfi_startproc
+** pminsd %xmm1, %xmm0
+** ret
+** .cfi_endproc
+**...
+*/
+
+typedef int v2si __attribute__((vector_size (8)));
+typedef long long v2di __attribute__((vector_size (16)));
+
+v2si
+func (v2si a, v2si b)
+{
+ v2di x = __builtin_convertvector (a, v2di);
+ v2di y = __builtin_convertvector (b, v2di);
+ v2di z = x < y ? x : y;
+ return __builtin_convertvector (z, v2si);
+}
diff --git a/gcc/testsuite/g++.target/i386/pr126784-2.C b/gcc/testsuite/g++.target/i386/pr126784-2.C
new file mode 100644
index 00000000000..3255b39b9a1
--- /dev/null
+++ b/gcc/testsuite/g++.target/i386/pr126784-2.C
@@ -0,0 +1,28 @@
+/* { dg-do compile { target { *-*-linux* && { ! ia32 } } } } */
+/* { dg-options "-O2 -march=x86-64 -msse4 -std=c++17" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc'). */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } } */
+
+/*
+**_Z4funcDv2_iS_PDv2_x:
+**.LFB0:
+** .cfi_startproc
+** pminsd %xmm1, %xmm0
+** pmovsxdq %xmm0, %xmm1
+** movaps %xmm1, \(%rdi\)
+** ret
+**...
+*/
+
+typedef int v2si __attribute__((vector_size (8)));
+typedef long long v2di __attribute__((vector_size (16)));
+
+v2si
+func (v2si a, v2si b, v2di *p)
+{
+ v2di x = __builtin_convertvector (a, v2di);
+ v2di y = __builtin_convertvector (b, v2di);
+ v2di z = x < y ? x : y;
+ *p = z;
+ return __builtin_convertvector (z, v2si);
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr126784-1.c b/gcc/testsuite/gcc.target/i386/pr126784-1.c
new file mode 100644
index 00000000000..73b581b0e7c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr126784-1.c
@@ -0,0 +1,24 @@
+/* { dg-do compile { target { *-*-linux* && { ! ia32 } } } } */
+/* { dg-options "-O2" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc'). */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } } */
+
+/*
+**func:
+**.LFB0:
+** .cfi_startproc
+** cmpl %esi, %edi
+** movl %esi, %eax
+** cmovle %edi, %eax
+** ret
+**...
+*/
+
+int
+func (int a, int b)
+{
+ long long int x = a;
+ long long int y = b;
+ long long int z = x < y ? x : y;
+ return z;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr126784-2.c b/gcc/testsuite/gcc.target/i386/pr126784-2.c
new file mode 100644
index 00000000000..f72495be5f9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr126784-2.c
@@ -0,0 +1,27 @@
+/* { dg-do compile { target { *-*-linux* && { ! ia32 } } } } */
+/* { dg-options "-O2" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc'). */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } } */
+
+/*
+**func:
+**.LFB0:
+** .cfi_startproc
+** cmpl %esi, %edi
+** movl %esi, %eax
+** cmovle %edi, %eax
+** movslq %eax, %rcx
+** movq %rcx, \(%rdx\)
+** ret
+**...
+*/
+
+int
+func (int a, int b, long long int *p)
+{
+ long long int x = a;
+ long long int y = b;
+ long long int z = x < y ? x : y;
+ *p = z;
+ return z;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr126788-1.c b/gcc/testsuite/gcc.target/i386/pr126788-1.c
new file mode 100644
index 00000000000..8a76b1045db
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr126788-1.c
@@ -0,0 +1,22 @@
+/* { dg-do compile { target { *-*-linux* && { ! ia32 } } } } */
+/* { dg-options "-O2" } */
+/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc'). */
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } } */
+
+/*
+**func:
+**.LFB0:
+** .cfi_startproc
+** ret
+**...
+*/
+
+typedef int v2si __attribute__((vector_size (8)));
+typedef long long v2di __attribute__((vector_size (16)));
+
+v2si
+func (v2si a)
+{
+ v2di z = __builtin_convertvector (a, v2di);
+ return __builtin_convertvector (z, v2si);
+}
--
2.55.0