re PR tree-optimization/87314 (pointless comparison of malloc result to a string...
authorRichard Biener <rguenther@suse.de>
Fri, 3 May 2019 10:44:17 +0000 (10:44 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Fri, 3 May 2019 10:44:17 +0000 (10:44 +0000)
2019-05-03  Richard Biener  <rguenther@suse.de>

PR middle-end/87314
* match.pd (cmp (convert1?@2 addr@0) (convert2? addr@1)):
Handle STRING_CST vs DECL or STRING_CST.

* gcc.dg/pr87314-1.c: New testcase.

From-SVN: r270845

gcc/ChangeLog
gcc/match.pd
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr87314-1.c [new file with mode: 0644]

index 80c34bea46695629a49eaa0813f0ac60ff549e93..e9def2fc5d70109ef82ef3c7a80bb831aae6983a 100644 (file)
@@ -1,3 +1,9 @@
+2019-05-03  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/87314
+       * match.pd (cmp (convert1?@2 addr@0) (convert2? addr@1)):
+       Handle STRING_CST vs DECL or STRING_CST.
+
 2019-05-03  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/88963
index 039ddacfed1e849dc8e101239110f98207fbdd48..ec03a680e3354f493c6632e8962d17b57d19c0a3 100644 (file)
@@ -3905,7 +3905,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
          equal = (base0 == base1);
        if (equal == 0)
         {
-          if (!DECL_P (base0) || !DECL_P (base1))
+          HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
+          off0.is_constant (&ioff0);
+          off1.is_constant (&ioff1);
+          if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
+              || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
+              || (TREE_CODE (base0) == STRING_CST
+                  && TREE_CODE (base1) == STRING_CST
+                  && ioff0 >= 0 && ioff1 >= 0
+                  && ioff0 < TREE_STRING_LENGTH (base0)
+                  && ioff1 < TREE_STRING_LENGTH (base1)
+                  /* This is a too conservative test that the STRING_CSTs
+                     will not end up being string-merged.  */
+                  && strncmp (TREE_STRING_POINTER (base0) + ioff0,
+                              TREE_STRING_POINTER (base1) + ioff1,
+                              MIN (TREE_STRING_LENGTH (base0) - ioff0,
+                                   TREE_STRING_LENGTH (base1) - ioff1)) != 0))
+            ;
+          else if (!DECL_P (base0) || !DECL_P (base1))
             equal = 2;
           else if (cmp != EQ_EXPR && cmp != NE_EXPR)
             equal = 2;
index 57a07d167e55670534b02880bae74bdf25574a5d..0e23e5741d5a392fcc3f4052fa6d6d990203381f 100644 (file)
@@ -1,3 +1,8 @@
+2019-05-03  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/87314
+       * gcc.dg/pr87314-1.c: New testcase.
+
 2019-05-03  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/88963
diff --git a/gcc/testsuite/gcc.dg/pr87314-1.c b/gcc/testsuite/gcc.dg/pr87314-1.c
new file mode 100644 (file)
index 0000000..4dc85c8
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-original" } */
+
+int f(){ int a; return &a==(void *)"hello"; }
+int g(){ return "bye"=="hello"; }
+int h() { return "bye"=="hellobye"+5; }
+
+/* { dg-final { scan-tree-dump-times "hello" 1 "original" } } */
+/* The test in h() should be retained because the result depends on
+   string merging.  */
+/* { dg-final { scan-assembler "hello" } } */