Prevent cache-block clearing and by-pieces expansion from bypassing
an explicit memset inline threshold.
Assisted-by: qwenwork
gcc/ChangeLog:
* config/riscv/riscv-string.cc (riscv_expand_block_clear): Check the
memset threshold.
* config/riscv/riscv.cc (riscv_use_by_pieces_infrastructure_p): Honor
the memset threshold before considering by-pieces expansion.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/setmem-cbo-threshold.c: New test.
* gcc.target/riscv/setmem-pieces-threshold.c: New test.
---
gcc/config/riscv/riscv-string.cc | 6 ++++
gcc/config/riscv/riscv.cc | 6 ++++
.../gcc.target/riscv/setmem-cbo-threshold.c | 12 ++++++++
.../riscv/setmem-pieces-threshold.c | 28 +++++++++++++++++++
4 files changed, 52 insertions(+)
create mode 100644 gcc/testsuite/gcc.target/riscv/setmem-cbo-threshold.c
create mode 100644 gcc/testsuite/gcc.target/riscv/setmem-pieces-threshold.c
diff --git a/gcc/config/riscv/riscv-string.cc b/gcc/config/riscv/riscv-string.cc
index b0b7efece91..a580220aa8e 100644
--- a/gcc/config/riscv/riscv-string.cc
+++ b/gcc/config/riscv/riscv-string.cc
@@ -1117,6 +1117,12 @@ riscv_expand_block_clear (rtx dest, rtx length, bool
testing_p)
if (optimize_function_for_size_p (cfun))
return false;
+ if (CONST_INT_P (length)
+ && riscv_memset_size_threshold >= 0
+ && UINTVAL (length)
+ > (unsigned HOST_WIDE_INT) riscv_memset_size_threshold)
+ return false;
+
return riscv_expand_block_clear_zicboz_zic64b (dest, length, testing_p);
}
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 3e0aa58d3f3..cd2d586ed72 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -15986,6 +15986,12 @@ riscv_use_by_pieces_infrastructure_p (unsigned
HOST_WIDE_INT size,
unsigned alignment,
enum by_pieces_operation op, bool speed_p)
{
+ /* Do not bypass an explicit memset threshold through by-pieces. */
+ if ((op == CLEAR_BY_PIECES || op == SET_BY_PIECES)
+ && riscv_memset_size_threshold >= 0
+ && size > (unsigned HOST_WIDE_INT) riscv_memset_size_threshold)
+ return false;
+
/* Query the expanders whether they can handle the given operation. */
if ((op == CLEAR_BY_PIECES || op == SET_BY_PIECES)
&& speed_p && size > UNITS_PER_WORD)
diff --git a/gcc/testsuite/gcc.target/riscv/setmem-cbo-threshold.c
b/gcc/testsuite/gcc.target/riscv/setmem-cbo-threshold.c
new file mode 100644
index 00000000000..d12a9357877
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/setmem-cbo-threshold.c
@@ -0,0 +1,12 @@
+/* { dg-do compile { target rv64 } } */
+/* { dg-options "-O2 -march=rv64gcv_zvl256b_zic64b_zicboz -mabi=lp64d
--param=memset-size-threshold=63" } */
+
+void
+clear_block (void *dst)
+{
+ dst = __builtin_assume_aligned (dst, 64);
+ __builtin_memset (dst, 0, 256);
+}
+
+/* { dg-final { scan-assembler-not "cbo\\.zero" } } */
+/* { dg-final { scan-assembler "\\t(call|tail)\\tmemset" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/setmem-pieces-threshold.c
b/gcc/testsuite/gcc.target/riscv/setmem-pieces-threshold.c
new file mode 100644
index 00000000000..e2599cd99cd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/setmem-pieces-threshold.c
@@ -0,0 +1,28 @@
+/* { dg-do compile { target rv64 } } */
+/* { dg-options "-O2 -march=rv64gcv -mabi=lp64d -mtune=generic-ooo
--param=memset-size-threshold=3" } */
+
+void
+clear_short (void *dst)
+{
+ __builtin_memset (dst, 0, 7);
+}
+
+void
+clear_words (void *dst)
+{
+ __builtin_memset (dst, 0, 32);
+}
+
+void
+fill_short (void *dst, int value)
+{
+ __builtin_memset (dst, value, 7);
+}
+
+void
+fill_words (void *dst, int value)
+{
+ __builtin_memset (dst, value, 32);
+}
+
+/* { dg-final { scan-assembler-times "\\t(call|tail)\\tmemset" 4 } } */
--
2.52.0