public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Nathan Sidwell <nathan@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/c++-modules] PR middle-end/96665 - memcmp of a constant string not folded
Date: Thu, 27 Aug 2020 18:14:12 +0000 (GMT)	[thread overview]
Message-ID: <20200827181412.019463938C21@sourceware.org> (raw)

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

commit d367f5fcb579d21c3093cf5c464f5787fe584a1d
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue Aug 18 12:57:18 2020 -0600

    PR middle-end/96665 - memcmp of a constant string not folded
    
    Related:
    PR middle-end/78257 - missing memcmp optimization with constant arrays
    
    gcc/ChangeLog:
    
            PR middle-end/96665
            PR middle-end/78257
            * expr.c (convert_to_bytes): Replace statically allocated buffer with
            a dynamically allocated one of sufficient size.
    
    gcc/testsuite/ChangeLog:
    
            PR middle-end/96665
            PR middle-end/78257
            * gcc.dg/memcmp-5.c: New test.

Diff:
---
 gcc/expr.c                      | 27 +++++++++++-----
 gcc/testsuite/gcc.dg/memcmp-5.c | 72 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 8 deletions(-)

diff --git a/gcc/expr.c b/gcc/expr.c
index dd2200ddea8..437faeaba08 100644
--- a/gcc/expr.c
+++ b/gcc/expr.c
@@ -11683,16 +11683,27 @@ convert_to_bytes (tree type, tree expr, vec<unsigned char> *bytes)
       return true;
     }
 
-  unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
-  int len = native_encode_expr (expr, charbuf, sizeof charbuf, 0);
-  if (len <= 0)
+  /* Except for RECORD_TYPE which may have an initialized flexible array
+     member, the size of a type is the same as the size of the initializer
+     (including any implicitly zeroed out members and padding).  Allocate
+     just enough for that many bytes.  */
+  tree expr_size = TYPE_SIZE_UNIT (TREE_TYPE (expr));
+  if (!expr_size || !tree_fits_uhwi_p (expr_size))
+    return false;
+  const unsigned HOST_WIDE_INT expr_bytes = tree_to_uhwi (expr_size);
+  const unsigned bytes_sofar = bytes->length ();
+  /* native_encode_expr can convert at most INT_MAX bytes.  vec is limited
+     to at most UINT_MAX.  */
+  if (bytes_sofar + expr_bytes > INT_MAX)
     return false;
 
-  unsigned n = bytes->length ();
-  bytes->safe_grow (n + len);
-  unsigned char *p = bytes->address ();
-  memcpy (p + n, charbuf, len);
-  return true;
+  /* Unlike for RECORD_TYPE, there is no need to clear the memory since
+     it's completely overwritten by native_encode_expr.  */
+  bytes->safe_grow (bytes_sofar + expr_bytes);
+  unsigned char *pnext = bytes->begin () + bytes_sofar;
+  int nbytes = native_encode_expr (expr, pnext, expr_bytes, 0);
+  /* NBYTES is zero on failure.  Otherwise it should equal EXPR_BYTES.  */
+  return (unsigned HOST_WIDE_INT) nbytes == expr_bytes;
 }
 
 /* Return a STRING_CST corresponding to ARG's constant initializer either
diff --git a/gcc/testsuite/gcc.dg/memcmp-5.c b/gcc/testsuite/gcc.dg/memcmp-5.c
new file mode 100644
index 00000000000..34bae92f6b0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/memcmp-5.c
@@ -0,0 +1,72 @@
+/* PR middle-end/78257 - missing memcmp optimization with constant arrays
+   { dg-do compile }
+   { dg-options "-O -Wall -fdump-tree-optimized" } */
+
+#define A "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
+          "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
+          "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
+          "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
+          "0"
+
+const char a257[sizeof A - 1] = A;
+const char a258[sizeof A] = A;
+
+_Static_assert (sizeof A == 258);
+_Static_assert (sizeof a257 == 257);
+
+/* Verify that initializers longer than 256 characters (an internal limit
+   on the size of a buffer used to store representations in) are handled.  */
+
+void eq_256plus (void)
+{
+  int n = 0;
+
+  n += __builtin_memcmp (a257,       A,       sizeof a257);
+  n += __builtin_memcmp (a257 +   1, A +   1, sizeof a257 - 1);
+  n += __builtin_memcmp (a257 +   2, A +   2, sizeof a257 - 2);
+  n += __builtin_memcmp (a257 + 127, A + 127, sizeof a257 - 127);
+  n += __builtin_memcmp (a257 + 128, A + 128, sizeof a257 - 128);
+  n += __builtin_memcmp (a257 + 255, A + 255, 2);
+  n += __builtin_memcmp (a257 + 256, A + 256, 1);
+
+  n += __builtin_memcmp (a258,       A,       sizeof a257);
+  n += __builtin_memcmp (a258 +   1, A +   1, sizeof a257 - 1);
+  n += __builtin_memcmp (a258 +   2, A +   2, sizeof a257 - 2);
+  n += __builtin_memcmp (a258 + 127, A + 127, sizeof a257 - 127);
+  n += __builtin_memcmp (a258 + 128, A + 128, sizeof a257 - 128);
+  n += __builtin_memcmp (a258 + 256, A + 256, 2);
+  n += __builtin_memcmp (a258 + 257, A + 257, 1);
+
+  if (n)
+    __builtin_abort ();
+}
+
+#define X "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
+          "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
+          "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
+          "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
+          "1"
+
+void lt_256plus (void)
+{
+  int n = 0;
+
+  n += 0 >  __builtin_memcmp (a257,       X,       sizeof a257);
+  n += 0 >  __builtin_memcmp (a257 +   1, X +   1, sizeof a257 - 1);
+  n += 0 >  __builtin_memcmp (a257 +   2, X +   2, sizeof a257 - 2);
+  n += 0 >  __builtin_memcmp (a257 + 127, X + 127, sizeof a257 - 127);
+  n += 0 >  __builtin_memcmp (a257 + 128, X + 128, sizeof a257 - 128);
+  n += 0 >  __builtin_memcmp (a257 + 255, X + 255, 2);
+  n += 0 >  __builtin_memcmp (a257 + 256, X + 256, 1);
+
+  n += 0 >  __builtin_memcmp (a258,       X,       sizeof a258);
+  n += 0 >  __builtin_memcmp (a258 +   1, X +   1, sizeof a258 - 1);
+  n += 0 >  __builtin_memcmp (a258 +   2, X +   2, sizeof a258 - 2);
+  n += 0 >  __builtin_memcmp (a258 + 127, X + 127, sizeof a257 - 127);
+  n += 0 >  __builtin_memcmp (a258 + 128, X + 128, sizeof a257 - 128);
+  n += 0 >  __builtin_memcmp (a258 + 256, X + 256, 2);
+  n += 0 == __builtin_memcmp (a258 + 257, X + 257, 1);
+
+  if (n != 14)
+    __builtin_abort ();
+}


                 reply	other threads:[~2020-08-27 18:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200827181412.019463938C21@sourceware.org \
    --to=nathan@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).