From d953c2c5714ed8503c4ae1b7d059a62e4e9a0624 Mon Sep 17 00:00:00 2001
From: Tsung Chun Lin <tclin914@gmail.com>
Date: Tue, 7 Jan 2025 14:48:31 -0700
Subject: [PATCH] Prefer scalar_int_mode if the size - 1 is equal to
 UNITS_PER_WORD.

Don't use the QI vector if its size is equal to UNITS_PER_WORD for
better code generation.

Before patch:

vsetivli        zero,4,e8,mf4,ta,ma
vmv.v.i v1,0
addi    a4,sp,12
vse8.v  v1,0(a4)

After patch:

sw      zero,12(sp)

gcc/
	* expr.cc (widest_fixed_size_mode_for_size): Prefer scalar modes
	over vector modes in more cases.

gcc/testsuite/

	* gcc.target/riscv/rvv/autovec/pr113469.c: Update expected output.
	* gcc.target/riscv/rvv/base/movqi-1.c: New test.
---
 gcc/expr.cc                                    |  3 ++-
 .../gcc.target/riscv/rvv/autovec/pr113469.c    |  1 -
 .../gcc.target/riscv/rvv/base/movqi-1.c        | 18 ++++++++++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/movqi-1.c

diff --git a/gcc/expr.cc b/gcc/expr.cc
index 635bb9efa9eb..235e79546113 100644
--- a/gcc/expr.cc
+++ b/gcc/expr.cc
@@ -1062,12 +1062,13 @@ widest_fixed_size_mode_for_size (unsigned int size, by_pieces_operation op)
   gcc_checking_assert (size > 1);
 
   /* Use QI vector only if size is wider than a WORD.  */
-  if (can_use_qi_vectors (op) && size > UNITS_PER_WORD)
+  if (can_use_qi_vectors (op))
     {
       machine_mode mode;
       fixed_size_mode candidate;
       FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
 	if (is_a<fixed_size_mode> (mode, &candidate)
+	    && GET_MODE_SIZE (candidate) > UNITS_PER_WORD
 	    && GET_MODE_INNER (candidate) == QImode)
 	  {
 	    if (GET_MODE_SIZE (candidate) >= size)
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr113469.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr113469.c
index 52e2580c53e6..6549ae61c672 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr113469.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr113469.c
@@ -51,5 +51,4 @@ void p(int buf, __builtin_va_list ab, int q) {
  } while (k);
 }
 
-/* { dg-final { scan-assembler-times {vsetivli\tzero,\s*4,\s*e8,\s*mf4,\s*t[au],\s*m[au]} 1 } } */
 /* { dg-final { scan-assembler-times {vsetivli\tzero,\s*8,\s*e8,\s*mf2,\s*t[au],\s*m[au]} 1 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/movqi-1.c b/gcc/testsuite/gcc.target/riscv/rvv/base/movqi-1.c
new file mode 100644
index 000000000000..bc461035e5b8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/movqi-1.c
@@ -0,0 +1,18 @@
+/* Test that we do not use QI vector to initilize the memory if the
+ * size of QI vector isn't larger than UNITS_PER_WORD */
+/* { dg-do compile } */
+/* { dg-options "-march=rv32gcv -mabi=ilp32 -O3" } */
+
+struct s {
+  int a;
+  int b : 1;
+};
+
+void q(struct s*);
+
+void g() {
+  struct s r = { 15, 0 };
+  q(&r);
+}
+
+/* { dg-final { scan-assembler-times {sw\tzero,12\(sp\)} 1 } } */
-- 
GitLab