Hi!

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.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-01-18  Jakub Jelinek  <ja...@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.

--- gcc/fold-const.cc.jj        2022-01-18 13:25:08.057491249 +0100
+++ gcc/fold-const.cc   2022-01-18 13:53:26.106261913 +0100
@@ -16587,6 +16587,15 @@ address_compare (tree_code code, tree ty
               || 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
--- gcc/testsuite/gcc.dg/tree-ssa/pr89074.c.jj  2022-01-18 18:42:05.125004996 
+0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr89074.c     2022-01-18 18:43:39.249694890 
+0100
@@ -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;
+}

        Jakub

Reply via email to