public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-6726] fold-const: Optimize &"foo"[0] == "foo" [PR89074]
Date: Wed, 19 Jan 2022 08:26:37 +0000 (GMT)	[thread overview]
Message-ID: <20220119082637.B93753857827@sourceware.org> (raw)

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

commit r12-6726-gb834435c8fa4cb9424787fe3044a49fef7992de8
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 19 09:26:13 2022 +0100

    fold-const: Optimize &"foo"[0] == "foo" [PR89074]
    
    This is a non-C++ related part from the PR89074 address_compare changes.
    For "foo" == "foo" we already optimize this from the (cmp @0 @0)
    simplification, because we use operand_equal_p in that case
    and operand_equal_p also compares the STRING_CSTs bytes rather than
    just addresses.
    
    2022-01-19  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/89074
            * fold-const.cc (address_compare): Consider different STRING_CSTs
            with the same lengths that memcmp the same as equal, not different.
    
            * gcc.dg/tree-ssa/pr89074.c: New test.

Diff:
---
 gcc/fold-const.cc                       |  9 +++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr89074.c | 12 ++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index ff6a74928a8..8fe633c03f9 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -16587,6 +16587,15 @@ address_compare (tree_code code, tree type, tree op0, tree op1,
 	       || TREE_CODE (base1) == SSA_NAME
 	       || TREE_CODE (base1) == STRING_CST))
     equal = (base0 == base1);
+  /* Assume different STRING_CSTs with the same content will be
+     merged.  */
+  if (equal == 0
+      && TREE_CODE (base0) == STRING_CST
+      && TREE_CODE (base1) == STRING_CST
+      && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
+      && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
+		 TREE_STRING_LENGTH (base0)) == 0)
+    equal = 1;
   if (equal == 1)
     {
       if (code == EQ_EXPR
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr89074.c b/gcc/testsuite/gcc.dg/tree-ssa/pr89074.c
new file mode 100644
index 00000000000..666dfa7e105
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr89074.c
@@ -0,0 +1,12 @@
+/* PR c++/89074 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump "return 1;" "optimized" } } */
+
+int
+foo (void)
+{
+  const char *a = &"foo"[0];
+  const char *b = "foo";
+  return a == b;
+}


                 reply	other threads:[~2022-01-19  8:26 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=20220119082637.B93753857827@sourceware.org \
    --to=jakub@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).