[gcc(refs/vendors/riscv/heads/gcc-15-with-riscv-opts)] forwprop: Allow nop conversions for vector constructor.

Jeff Law law@gcc.gnu.org
Fri Jan 30 07:17:45 GMT 2026


https://gcc.gnu.org/g:e95a3a96736769ff527b484433619f68ffaff897

commit e95a3a96736769ff527b484433619f68ffaff897
Author: Robin Dapp <rdapp@ventanamicro.com>
Date:   Sun Nov 16 18:42:04 2025 +0100

    forwprop: Allow nop conversions for vector constructor.
    
    I observed a vect-construct forwprop opportunity in x264 that we
    could handle when checking for a nop conversion instead of a useless
    conversion.  IMHO a nop-conversion check is sufficient as we're only
    dealing with permutations in simplify_vector_constructor.
    
    This patch replaces uses of useless_type_conversion_p with
    tree_nop_conversion_p in simplify_vector_constructor.
    
    It was bootstrapped and regtested on x86 and power10, regtested on
    aarch64 and riscv64.
    
    There is a single scan-test failure on power
    (gcc.target/powerpc/builtins-1.c).  The code actually looks better so
    I took the liberty of adjusting the test expectation.
    
    gcc/ChangeLog:
    
            * tree-ssa-forwprop.cc (simplify_vector_constructor):
            Allow nop conversions.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/powerpc/builtins-1.c: Adjust test expectation.
            * gcc.target/riscv/rvv/autovec/pr118019-3.c: New test.
    
    (cherry picked from commit 283eb27d5f674b6fb11d8640495993e2ec693608)

Diff:
---
 gcc/testsuite/gcc.target/powerpc/builtins-1.c      |  2 +-
 .../gcc.target/riscv/rvv/autovec/pr118019-3.c      | 51 ++++++++++++++++++++++
 gcc/tree-ssa-forwprop.cc                           | 16 +++----
 3 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/gcc/testsuite/gcc.target/powerpc/builtins-1.c b/gcc/testsuite/gcc.target/powerpc/builtins-1.c
index 8410a5fd4319..4e7e5384675f 100644
--- a/gcc/testsuite/gcc.target/powerpc/builtins-1.c
+++ b/gcc/testsuite/gcc.target/powerpc/builtins-1.c
@@ -1035,4 +1035,4 @@ foo156 (vector unsigned short usa)
 /* { dg-final { scan-assembler-times {\mvmrglb\M} 3 } } */
 /* { dg-final { scan-assembler-times {\mvmrgew\M} 4 } } */
 /* { dg-final { scan-assembler-times {\mvsplth|xxsplth\M} 4 } } */
-/* { dg-final { scan-assembler-times {\mxxpermdi\M} 44 } } */
+/* { dg-final { scan-assembler-times {\mxxpermdi\M} 42 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118019-3.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118019-3.c
new file mode 100644
index 000000000000..6cb761f63c0f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr118019-3.c
@@ -0,0 +1,51 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -march=rv64gcv_zvl512b -mabi=lp64d -mno-vector-strict-align -mmax-vectorization -fdump-tree-optimized" } */
+
+/* Ensure we don't use a vector constructor but a permute.  */
+
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+
+#define HADAMARD4(d0, d1, d2, d3, s0, s1, s2, s3) {\
+    int t0 = s0 + s1;\
+    int t1 = s0 - s1;\
+    int t2 = s2 + s3;\
+    int t3 = s2 - s3;\
+    d0 = t0 + t2;\
+    d2 = t0 - t2;\
+    d1 = t1 + t3;\
+    d3 = t1 - t3;\
+}
+
+uint32_t
+abs2 (uint32_t a)
+{
+  uint32_t s = ((a >> 15) & 0x10001) * 0xffff;
+  return (a + s) ^ s;
+}
+
+int
+x264_pixel_satd_8x4 (uint8_t *pix1, int i_pix1, uint8_t *pix2, int i_pix2)
+{
+  uint32_t tmp[4][4];
+  uint32_t a0, a1, a2, a3;
+  int sum = 0;
+  for (int i = 0; i < 4; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      a0 = (pix1[0] - pix2[0]) + ((pix1[4] - pix2[4]) << 16);
+      a1 = (pix1[1] - pix2[1]) + ((pix1[5] - pix2[5]) << 16);
+      a2 = (pix1[2] - pix2[2]) + ((pix1[6] - pix2[6]) << 16);
+      a3 = (pix1[3] - pix2[3]) + ((pix1[7] - pix2[7]) << 16);
+      HADAMARD4 (tmp[i][0], tmp[i][1], tmp[i][2], tmp[i][3], a0, a1, a2, a3);
+    }
+#pragma GCC unroll 4
+  for (int i = 0; i < 4; i++)
+    {
+      HADAMARD4 (a0, a1, a2, a3, tmp[0][i], tmp[1][i], tmp[2][i], tmp[3][i]);
+      sum += abs2 (a0) + abs2 (a1) + abs2 (a2) + abs2 (a3);
+    }
+  return (((uint16_t) sum) + ((uint32_t) sum >> 16)) >> 1;
+}
+
+/* { dg-final { scan-tree-dump-not "BIT_FIELD_REF" "optimized" } } */
diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc
index a9203ad83555..4c906aad8984 100644
--- a/gcc/tree-ssa-forwprop.cc
+++ b/gcc/tree-ssa-forwprop.cc
@@ -2843,11 +2843,11 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
       if (op1
 	  && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
 	  && VECTOR_TYPE_P (TREE_TYPE (ref))
-	  && (useless_type_conversion_p (TREE_TYPE (op1),
-					TREE_TYPE (TREE_TYPE (ref)))
+	  && (tree_nop_conversion_p (TREE_TYPE (op1),
+				     TREE_TYPE (TREE_TYPE (ref)))
 	      || (VECTOR_TYPE_P (TREE_TYPE (op1))
-		  && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
-						TREE_TYPE (TREE_TYPE (ref)))
+		  && tree_nop_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
+					    TREE_TYPE (TREE_TYPE (ref)))
 		  && TYPE_VECTOR_SUBPARTS (TREE_TYPE (op1))
 			.is_constant (&nsubelts)))
 	  && constant_multiple_p (bit_field_size (op1), nsubelts,
@@ -3082,8 +3082,8 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
 	    {
 	      gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
 				    TYPE_VECTOR_SUBPARTS (src_type))
-			  && useless_type_conversion_p (TREE_TYPE (type),
-							TREE_TYPE (src_type)));
+			  && tree_nop_conversion_p (TREE_TYPE (type),
+						    TREE_TYPE (src_type)));
 	      tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
 	      orig[0] = make_ssa_name (type);
 	      gassign *assign = gimple_build_assign (orig[0], rhs);
@@ -3210,8 +3210,8 @@ simplify_vector_constructor (gimple_stmt_iterator *gsi)
 	{
 	  gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
 				TYPE_VECTOR_SUBPARTS (perm_type))
-		      && useless_type_conversion_p (TREE_TYPE (type),
-						    TREE_TYPE (perm_type)));
+		      && tree_nop_conversion_p (TREE_TYPE (type),
+						TREE_TYPE (perm_type)));
 	  res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
 	}
       /* Blend in the actual constant.  */


More information about the Gcc-cvs mailing list