public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
@ 2023-12-09  5:22 Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 2023-12-09  5:22 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:818dc6f5a02810bc2e7bcf0a97442d83ca50bd90

commit 818dc6f5a02810bc2e7bcf0a97442d83ca50bd90
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                                | 57 ++++++++++++++++++++++----
 gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c | 10 +++++
 2 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 12a535d313f..f6c96498f07 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -4284,6 +4284,40 @@ 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.  It is assumed that LEN has already been tested by
+   itself.  */
+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 (bits
+      && !can_store_by_pieces (bits + len, constfun, constfundata,
+			       align, memsetp))
+    return false;
+
+  /* BITS set are expected to be generally in the low range and
+     contiguous.  We do NOT want to repeat the test above in case BITS
+     has a single bit set, so we terminate the loop when BITS == BIT.
+     In the unlikely case that BITS has the MSB set, also terminate in
+     case BIT gets shifted out.  */
+  for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
+    {
+      if ((bits & bit) == 0)
+	continue;
+
+      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
@@ -4341,7 +4375,11 @@ try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
   else
     /* Huh, max_len < min_len?  Punt.  See pr100843.c.  */
     return false;
-  if (min_len >= blksize)
+  if (min_len >= blksize
+      /* ??? Maybe try smaller fixed-prefix blksizes before
+	 punting?  */
+      && can_store_by_pieces (blksize, builtin_memset_read_str,
+			      &valc, align, true))
     {
       min_len -= blksize;
       min_bits = floor_log2 (min_len);
@@ -4367,8 +4405,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 +4425,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);
+}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
@ 2023-12-11 16:20 Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 2023-12-11 16:20 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:52537a3b3e1507fa6d121eb6d39a06804243a8ad

commit 52537a3b3e1507fa6d121eb6d39a06804243a8ad
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                                | 57 ++++++++++++++++++++++----
 gcc/expr.cc                                    | 16 +++-----
 gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c | 10 +++++
 3 files changed, 63 insertions(+), 20 deletions(-)

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 12a535d313f..f6c96498f07 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -4284,6 +4284,40 @@ 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.  It is assumed that LEN has already been tested by
+   itself.  */
+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 (bits
+      && !can_store_by_pieces (bits + len, constfun, constfundata,
+			       align, memsetp))
+    return false;
+
+  /* BITS set are expected to be generally in the low range and
+     contiguous.  We do NOT want to repeat the test above in case BITS
+     has a single bit set, so we terminate the loop when BITS == BIT.
+     In the unlikely case that BITS has the MSB set, also terminate in
+     case BIT gets shifted out.  */
+  for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
+    {
+      if ((bits & bit) == 0)
+	continue;
+
+      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
@@ -4341,7 +4375,11 @@ try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
   else
     /* Huh, max_len < min_len?  Punt.  See pr100843.c.  */
     return false;
-  if (min_len >= blksize)
+  if (min_len >= blksize
+      /* ??? Maybe try smaller fixed-prefix blksizes before
+	 punting?  */
+      && can_store_by_pieces (blksize, builtin_memset_read_str,
+			      &valc, align, true))
     {
       min_len -= blksize;
       min_bits = floor_log2 (min_len);
@@ -4367,8 +4405,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 +4425,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/expr.cc b/gcc/expr.cc
index 178b3ec6d5a..076ba706537 100644
--- a/gcc/expr.cc
+++ b/gcc/expr.cc
@@ -2451,15 +2451,12 @@ emit_block_move_via_loop (rtx x, rtx y, rtx size,
 
   opt_scalar_int_mode int_move_mode
     = int_mode_for_size (incr * BITS_PER_UNIT, 1);
-  if (!int_move_mode.exists ()
-      || (GET_MODE_BITSIZE (as_a <scalar_int_mode> (int_move_mode))
-	  != incr * BITS_PER_UNIT))
+  if (!int_move_mode.exists (&move_mode)
+      || GET_MODE_BITSIZE (int_move_mode.require ()) != incr * BITS_PER_UNIT)
     {
       move_mode = BLKmode;
       gcc_checking_assert (can_move_by_pieces (incr, align));
     }
-  else
-    move_mode = as_a <scalar_int_mode> (int_move_mode);
 
   x_addr = force_operand (XEXP (x, 0), NULL_RTX);
   y_addr = force_operand (XEXP (y, 0), NULL_RTX);
@@ -2705,16 +2702,13 @@ emit_block_cmp_via_loop (rtx x, rtx y, rtx len, tree len_type, rtx target,
 
   opt_scalar_int_mode int_cmp_mode
     = int_mode_for_size (incr * BITS_PER_UNIT, 1);
-  if (!int_cmp_mode.exists ()
-      || (GET_MODE_BITSIZE (as_a <scalar_int_mode> (int_cmp_mode))
-	  != incr * BITS_PER_UNIT)
-      || !can_compare_p (NE, as_a <scalar_int_mode> (int_cmp_mode), ccp_jump))
+  if (!int_cmp_mode.exists (&cmp_mode)
+      || GET_MODE_BITSIZE (int_cmp_mode.require ()) != incr * BITS_PER_UNIT
+      || !can_compare_p (NE, cmp_mode, ccp_jump))
     {
       cmp_mode = BLKmode;
       gcc_checking_assert (incr != 1);
     }
-  else
-    cmp_mode = as_a <scalar_int_mode> (int_cmp_mode);
 
   /* Save the base addresses.  */
   x_addr = force_operand (XEXP (x, 0), NULL_RTX);
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);
+}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
@ 2023-12-11 16:17 Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 2023-12-11 16:17 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:9d04e48769886a4ecb0aa1d45ca483baa68dfc00

commit 9d04e48769886a4ecb0aa1d45ca483baa68dfc00
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                                | 57 ++++++++++++++++++++++----
 gcc/expr.cc                                    | 14 +++----
 gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c | 10 +++++
 3 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 12a535d313f..f6c96498f07 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -4284,6 +4284,40 @@ 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.  It is assumed that LEN has already been tested by
+   itself.  */
+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 (bits
+      && !can_store_by_pieces (bits + len, constfun, constfundata,
+			       align, memsetp))
+    return false;
+
+  /* BITS set are expected to be generally in the low range and
+     contiguous.  We do NOT want to repeat the test above in case BITS
+     has a single bit set, so we terminate the loop when BITS == BIT.
+     In the unlikely case that BITS has the MSB set, also terminate in
+     case BIT gets shifted out.  */
+  for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
+    {
+      if ((bits & bit) == 0)
+	continue;
+
+      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
@@ -4341,7 +4375,11 @@ try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
   else
     /* Huh, max_len < min_len?  Punt.  See pr100843.c.  */
     return false;
-  if (min_len >= blksize)
+  if (min_len >= blksize
+      /* ??? Maybe try smaller fixed-prefix blksizes before
+	 punting?  */
+      && can_store_by_pieces (blksize, builtin_memset_read_str,
+			      &valc, align, true))
     {
       min_len -= blksize;
       min_bits = floor_log2 (min_len);
@@ -4367,8 +4405,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 +4425,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/expr.cc b/gcc/expr.cc
index 178b3ec6d5a..4c9e6aa95bc 100644
--- a/gcc/expr.cc
+++ b/gcc/expr.cc
@@ -2451,15 +2451,13 @@ emit_block_move_via_loop (rtx x, rtx y, rtx size,
 
   opt_scalar_int_mode int_move_mode
     = int_mode_for_size (incr * BITS_PER_UNIT, 1);
-  if (!int_move_mode.exists ()
-      || (GET_MODE_BITSIZE (as_a <scalar_int_mode> (int_move_mode))
+  if (!int_move_mode.exists (&move_mode)
+      || (GET_MODE_BITSIZE (as_a <scalar_int_mode> (move_mode))
 	  != incr * BITS_PER_UNIT))
     {
       move_mode = BLKmode;
       gcc_checking_assert (can_move_by_pieces (incr, align));
     }
-  else
-    move_mode = as_a <scalar_int_mode> (int_move_mode);
 
   x_addr = force_operand (XEXP (x, 0), NULL_RTX);
   y_addr = force_operand (XEXP (y, 0), NULL_RTX);
@@ -2705,16 +2703,14 @@ emit_block_cmp_via_loop (rtx x, rtx y, rtx len, tree len_type, rtx target,
 
   opt_scalar_int_mode int_cmp_mode
     = int_mode_for_size (incr * BITS_PER_UNIT, 1);
-  if (!int_cmp_mode.exists ()
-      || (GET_MODE_BITSIZE (as_a <scalar_int_mode> (int_cmp_mode))
+  if (!int_cmp_mode.exists (&cmp_mode)
+      || (GET_MODE_BITSIZE (as_a <scalar_int_mode> (cmp_mode))
 	  != incr * BITS_PER_UNIT)
-      || !can_compare_p (NE, as_a <scalar_int_mode> (int_cmp_mode), ccp_jump))
+      || !can_compare_p (NE, cmp_mode, ccp_jump))
     {
       cmp_mode = BLKmode;
       gcc_checking_assert (incr != 1);
     }
-  else
-    cmp_mode = as_a <scalar_int_mode> (int_cmp_mode);
 
   /* Save the base addresses.  */
   x_addr = force_operand (XEXP (x, 0), NULL_RTX);
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);
+}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
@ 2023-12-11 16:09 Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 2023-12-11 16:09 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:248f3fa50078aa23e8c630ff5baaa79101c54667

commit 248f3fa50078aa23e8c630ff5baaa79101c54667
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                                | 57 ++++++++++++++++++++++----
 gcc/expr.cc                                    | 16 +++-----
 gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c | 10 +++++
 3 files changed, 63 insertions(+), 20 deletions(-)

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 12a535d313f..f6c96498f07 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -4284,6 +4284,40 @@ 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.  It is assumed that LEN has already been tested by
+   itself.  */
+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 (bits
+      && !can_store_by_pieces (bits + len, constfun, constfundata,
+			       align, memsetp))
+    return false;
+
+  /* BITS set are expected to be generally in the low range and
+     contiguous.  We do NOT want to repeat the test above in case BITS
+     has a single bit set, so we terminate the loop when BITS == BIT.
+     In the unlikely case that BITS has the MSB set, also terminate in
+     case BIT gets shifted out.  */
+  for (unsigned HOST_WIDE_INT bit = 1; bit < bits && bit; bit <<= 1)
+    {
+      if ((bits & bit) == 0)
+	continue;
+
+      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
@@ -4341,7 +4375,11 @@ try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
   else
     /* Huh, max_len < min_len?  Punt.  See pr100843.c.  */
     return false;
-  if (min_len >= blksize)
+  if (min_len >= blksize
+      /* ??? Maybe try smaller fixed-prefix blksizes before
+	 punting?  */
+      && can_store_by_pieces (blksize, builtin_memset_read_str,
+			      &valc, align, true))
     {
       min_len -= blksize;
       min_bits = floor_log2 (min_len);
@@ -4367,8 +4405,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 +4425,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/expr.cc b/gcc/expr.cc
index 178b3ec6d5a..530dcdd8e3b 100644
--- a/gcc/expr.cc
+++ b/gcc/expr.cc
@@ -2451,15 +2451,12 @@ emit_block_move_via_loop (rtx x, rtx y, rtx size,
 
   opt_scalar_int_mode int_move_mode
     = int_mode_for_size (incr * BITS_PER_UNIT, 1);
-  if (!int_move_mode.exists ()
-      || (GET_MODE_BITSIZE (as_a <scalar_int_mode> (int_move_mode))
-	  != incr * BITS_PER_UNIT))
+  if (!int_move_mode.exists (&move_mode)
+      || (GET_MODE_BITSIZE (mode_mode) != incr * BITS_PER_UNIT))
     {
       move_mode = BLKmode;
       gcc_checking_assert (can_move_by_pieces (incr, align));
     }
-  else
-    move_mode = as_a <scalar_int_mode> (int_move_mode);
 
   x_addr = force_operand (XEXP (x, 0), NULL_RTX);
   y_addr = force_operand (XEXP (y, 0), NULL_RTX);
@@ -2705,16 +2702,13 @@ emit_block_cmp_via_loop (rtx x, rtx y, rtx len, tree len_type, rtx target,
 
   opt_scalar_int_mode int_cmp_mode
     = int_mode_for_size (incr * BITS_PER_UNIT, 1);
-  if (!int_cmp_mode.exists ()
-      || (GET_MODE_BITSIZE (as_a <scalar_int_mode> (int_cmp_mode))
-	  != incr * BITS_PER_UNIT)
-      || !can_compare_p (NE, as_a <scalar_int_mode> (int_cmp_mode), ccp_jump))
+  if (!int_cmp_mode.exists (&cmp_mode)
+      || (GET_MODE_BITSIZE (cmp_mode) != incr * BITS_PER_UNIT)
+      || !can_compare_p (NE, cmp_mode, ccp_jump))
     {
       cmp_mode = BLKmode;
       gcc_checking_assert (incr != 1);
     }
-  else
-    cmp_mode = as_a <scalar_int_mode> (int_cmp_mode);
 
   /* Save the base addresses.  */
   x_addr = force_operand (XEXP (x, 0), NULL_RTX);
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);
+}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
@ 2023-12-09  5:02 Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 2023-12-09  5:02 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:0470f1fc6fc6b39c07cda57bd94d640b359bfa9a

commit 0470f1fc6fc6b39c07cda57bd94d640b359bfa9a
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                                | 57 ++++++++++++++++++++++----
 gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c | 10 +++++
 2 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 12a535d313f..ce98f509942 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -4284,6 +4284,40 @@ 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.  It is assumed that LEN has already been tested by
+   itself.  */
+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 (bits
+      && !can_store_by_pieces (bits + len, constfun, constfundata,
+			       align, memsetp))
+    return false;
+
+  /* Avoid the loop if we're just going to repeat the same single
+     test.  */
+  if (!len && popcount_hwi (bits) == 1)
+    return true;
+
+  for (int i = ffs_hwi (bits); i > 0; i = ffs_hwi (bits))
+    {
+      unsigned HOST_WIDE_INT bit = 1;
+      bit <<= i - 1;
+      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
@@ -4341,7 +4375,11 @@ try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
   else
     /* Huh, max_len < min_len?  Punt.  See pr100843.c.  */
     return false;
-  if (min_len >= blksize)
+  if (min_len >= blksize
+      /* ??? Maybe try smaller fixed-prefix blksizes before
+	 punting?  */
+      && can_store_by_pieces (blksize, builtin_memset_read_str,
+			      &valc, align, true))
     {
       min_len -= blksize;
       min_bits = floor_log2 (min_len);
@@ -4367,8 +4405,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 +4425,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);
+}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
@ 2023-12-09  2:02 Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 2023-12-09  2:02 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:2264750ec763326af0d966dd508b59c78394e932

commit 2264750ec763326af0d966dd508b59c78394e932
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                                | 57 ++++++++++++++++++++++----
 gcc/testsuite/gcc.dg/inline-mem-cmp-pr112778.c | 10 +++++
 2 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 12a535d313f..ad8497192a2 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -4284,6 +4284,40 @@ 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.  It is assumed that LEN has already been tested by
+   itself.  */
+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 (bits
+      && !can_store_by_pieces (bits + len, constfun, constfundata,
+			       align, memsetp))
+    return false;
+
+  /* Avoid the loop if we're just going to repeat the same single
+     test.  */
+  if (!len && popcount_hwi (bits) == 1)
+    return true;
+
+  for (int i = ctz_hwi (bits); i >= 0; i = ctz_hwi (bits))
+    {
+      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
@@ -4341,7 +4375,11 @@ try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
   else
     /* Huh, max_len < min_len?  Punt.  See pr100843.c.  */
     return false;
-  if (min_len >= blksize)
+  if (min_len >= blksize
+      /* ??? Maybe try smaller fixed-prefix blksizes before
+	 punting?  */
+      && can_store_by_pieces (blksize, builtin_memset_read_str,
+			      &valc, align, true))
     {
       min_len -= blksize;
       min_bits = floor_log2 (min_len);
@@ -4367,8 +4405,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 +4425,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);
+}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
@ 2023-12-09  1:49 Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 2023-12-09  1:49 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4a06be4b7a52c19e64dc607fae56e05d559a9d47

commit 4a06be4b7a52c19e64dc607fae56e05d559a9d47
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 12a535d313f..f243174f274 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; i = ctz_hwi (bits))
+    {
+      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);
+}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778]
@ 2023-12-09  1:37 Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 2023-12-09  1:37 UTC (permalink / raw)
  To: gcc-cvs

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);
+}

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-12-11 16:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-09  5:22 [gcc(refs/users/aoliva/heads/testme)] -finline-stringops: check base blksize for memset [PR112778] Alexandre Oliva
  -- strict thread matches above, loose matches on Subject: below --
2023-12-11 16:20 Alexandre Oliva
2023-12-11 16:17 Alexandre Oliva
2023-12-11 16:09 Alexandre Oliva
2023-12-09  5:02 Alexandre Oliva
2023-12-09  2:02 Alexandre Oliva
2023-12-09  1:49 Alexandre Oliva
2023-12-09  1:37 Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).