[gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
Alexandre Oliva
aoliva@gcc.gnu.org
Sat Dec 9 01:37:54 GMT 2023
https://gcc.gnu.org/g:458bbb50ad43837010ef4da769973e3a05f04ef5
commit 458bbb50ad43837010ef4da769973e3a05f04ef5
Author: Alexandre Oliva <oliva@adacore.com>
Date: Fri Dec 8 21:41:47 2023 -0300
-finline-stringops: check base blksize for memset [PR112778]
The recently-added logic for -finline-stringops=memset introduced an
assumption that doesn't necessarily hold, namely, that
can_store_by_pieces of a larger size implies can_store_by_pieces by
smaller sizes. Checks for all sizes the by-multiple-pieces machinery
might use before committing to an expansion pattern.
for gcc/ChangeLog
PR target/112778
* builtins.cc (can_store_by_multiple_pieces): New.
(try_store_by_multiple_pieces): Call it.
for gcc/testsuite/ChangeLog
PR target/112778
* gcc.dg/inline-mem-cmp-pr112778.c: New.
Diff:
---
gcc/builtins.cc | 45 +++++++++++++++++++++-----
gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c | 10 ++++++
2 files changed, 47 insertions(+), 8 deletions(-)
diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 38b0acff131..d5135ca162a 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -4284,6 +4284,34 @@ expand_builtin_memset (tree exp, rtx target, machine_mode mode)
return expand_builtin_memset_args (dest, val, len, target, mode, exp);
}
+/* Check that store_by_pieces allows BITS + LEN (so that we don't
+ expand something too unreasonably long), and every power of 2 in
+ BITS. */
+static bool
+can_store_by_multiple_pieces (unsigned HOST_WIDE_INT bits,
+ by_pieces_constfn constfun,
+ void *constfundata, unsigned int align,
+ bool memsetp,
+ unsigned HOST_WIDE_INT len)
+{
+ if (!can_store_by_pieces (bits + len, constfun, constfundata, align, memsetp))
+ return false;
+
+ if (len && !can_store_by_pieces (len, constfun, constfundata, align, memsetp))
+ return false;
+
+ for (int i = ctz_hwi (bits); i >= 0;)
+ {
+ unsigned HOST_WIDE_INT bit = 1;
+ bit <<= i;
+ bits &= ~bit;
+ if (!can_store_by_pieces (bit, constfun, constfundata, align, memsetp))
+ return false;
+ }
+
+ return true;
+}
+
/* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
Return TRUE if successful, FALSE otherwise. TO is assumed to be
aligned at an ALIGN-bits boundary. LEN must be a multiple of
@@ -4367,8 +4395,9 @@ try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
happen because of the way max_bits and blksize are related, but
it doesn't hurt to test. */
if (blksize > xlenest
- || !can_store_by_pieces (xlenest, builtin_memset_read_str,
- &valc, align, true))
+ || !can_store_by_multiple_pieces (xlenest - blksize,
+ builtin_memset_read_str,
+ &valc, align, true, blksize))
{
if (!(flag_inline_stringops & ILSOP_MEMSET))
return false;
@@ -4386,17 +4415,17 @@ try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
of overflow. */
if (max_bits < orig_max_bits
&& xlenest + blksize >= xlenest
- && can_store_by_pieces (xlenest + blksize,
- builtin_memset_read_str,
- &valc, align, true))
+ && can_store_by_multiple_pieces (xlenest,
+ builtin_memset_read_str,
+ &valc, align, true, blksize))
{
max_loop = true;
break;
}
if (blksize
- && can_store_by_pieces (xlenest,
- builtin_memset_read_str,
- &valc, align, true))
+ && can_store_by_multiple_pieces (xlenest,
+ builtin_memset_read_str,
+ &valc, align, true, 0))
{
max_len += blksize;
min_len += blksize;
diff --git a/gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c b/gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c
new file mode 100644
index 00000000000..fdfc5b6f28c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-finline-stringops" } */
+
+char buf[3];
+
+int
+f ()
+{
+ __builtin_memset (buf, 'v', 3);
+}
More information about the Gcc-cvs
mailing list