re PR tree-optimization/57230 (tree-ssa-strlen incorrectly optimizes a strlen to 0)
authorJakub Jelinek <jakub@redhat.com>
Mon, 13 May 2013 07:50:38 +0000 (09:50 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 13 May 2013 07:50:38 +0000 (09:50 +0200)
PR tree-optimization/57230
* tree-ssa-strlen.c (handle_char_store): Record length for
array store from STRING_CST.

* gcc.dg/strlenopt-24.c: New test.

From-SVN: r198815

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/strlenopt-24.c [new file with mode: 0644]
gcc/tree-ssa-strlen.c

index 2d1bbea..1f6e5c2 100644 (file)
@@ -1,6 +1,10 @@
 2013-05-13  Jakub Jelinek  <jakub@redhat.com>
 
        PR tree-optimization/57230
+       * tree-ssa-strlen.c (handle_char_store): Record length for
+       array store from STRING_CST.
+
+       PR tree-optimization/57230
        * tree-ssa-strlen.c (handle_char_store): Add missing integer_zerop
        check.
 
index 52ccf1a..e465f2a 100644 (file)
@@ -1,6 +1,9 @@
 2013-05-13  Jakub Jelinek  <jakub@redhat.com>
 
        PR tree-optimization/57230
+       * gcc.dg/strlenopt-24.c: New test.
+
+       PR tree-optimization/57230
        * gcc.dg/strlenopt-23.c: New test.
 
 2013-05-12  Oleg Endo  <olegendo@gcc.gnu.org>
diff --git a/gcc/testsuite/gcc.dg/strlenopt-24.c b/gcc/testsuite/gcc.dg/strlenopt-24.c
new file mode 100644 (file)
index 0000000..962e04f
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR tree-optimization/57230 */
+/* { dg-do run } */
+/* { dg-options "-O2 -fdump-tree-strlen" } */
+
+#include "strlenopt.h"
+
+int
+main ()
+{
+  char p[] = "hello world";
+  if (strlen (p) != 11)
+    abort ();
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "strlen \\(" 0 "strlen" } } *
+/* { dg-final { cleanup-tree-dump "strlen" } } */
index 4b6cfda..5ab3764 100644 (file)
@@ -1740,6 +1740,25 @@ handle_char_store (gimple_stmt_iterator *gsi)
       if (si != NULL)
        si->writable = true;
     }
+  else if (idx == 0
+          && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST
+          && ssaname == NULL_TREE
+          && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE)
+    {
+      size_t l = strlen (TREE_STRING_POINTER (gimple_assign_rhs1 (stmt)));
+      HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs));
+      if (a > 0 && (unsigned HOST_WIDE_INT) a > l)
+       {
+         int idx = new_addr_stridx (lhs);
+         if (idx != 0)
+           {
+             si = new_strinfo (build_fold_addr_expr (lhs), idx,
+                               build_int_cst (size_type_node, l));
+             set_strinfo (idx, si);
+             si->dont_invalidate = true;
+           }
+       }
+    }
 
   if (si != NULL && initializer_zerop (gimple_assign_rhs1 (stmt)))
     {