re PR tree-optimization/83501 (strlen(a) not folded after strcpy(a, "..."))
authorPrathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
Wed, 3 Jan 2018 16:07:32 +0000 (16:07 +0000)
committerPrathamesh Kulkarni <prathamesh3492@gcc.gnu.org>
Wed, 3 Jan 2018 16:07:32 +0000 (16:07 +0000)
2018-01-03  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

PR tree-optimization/83501
* tree-ssa-strlen.c (get_string_cst): New.
(handle_char_store): Call get_string_cst.

testsuite/
* gcc.dg/tree-ssa/pr83501.c: New test.

From-SVN: r256180

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

index 95b5ed2468da9f42a4b9ec930a5865f4b63fc95e..d95c297e96a46618c5cbd2c6278d72aef1e7f2d4 100644 (file)
@@ -1,3 +1,9 @@
+2018-01-03  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
+
+       PR tree-optimization/83501
+       * tree-ssa-strlen.c (get_string_cst): New.
+       (handle_char_store): Call get_string_cst.
+
 2018-01-03  Martin Liska  <mliska@suse.cz>
 
        PR tree-optimization/83593
index e252ae0f080c9d9ffd51bca24d691170869b6e8b..c18e6c7fd69bb864ddd24d74dfc49bd1983b4c39 100644 (file)
@@ -1,3 +1,8 @@
+2018-01-03  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
+
+       PR tree-optimization/83501
+       * gcc.dg/tree-ssa/pr83501-1.c: New test.
+
 2018-01-03  Nathan Sidwell  <nathan@acm.org>
 
        PR c++/83667
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c
new file mode 100644 (file)
index 0000000..d8d3bf6
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-strlen" } */
+
+char a[4];
+
+void f (void)
+{
+  __builtin_strcpy (a, "abc");
+
+  if (__builtin_strlen (a) != 3)
+    __builtin_abort ();
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin_strlen" "strlen" } } */
index 8f7020c14b3fb4723a1ae45528fe4dc48b024f46..f4fd69f58de49eead45fc4daf64c630c38116507 100644 (file)
@@ -2772,6 +2772,21 @@ handle_pointer_plus (gimple_stmt_iterator *gsi)
     }
 }
 
+/* Check if RHS is string_cst possibly wrapped by mem_ref.  */
+static tree
+get_string_cst (tree rhs)
+{
+  if (TREE_CODE (rhs) == MEM_REF
+      && integer_zerop (TREE_OPERAND (rhs, 1)))
+    {
+      rhs = TREE_OPERAND (rhs, 0);
+      if (TREE_CODE (rhs) == ADDR_EXPR)
+       rhs = TREE_OPERAND (rhs, 0);
+    }
+
+  return (TREE_CODE (rhs) == STRING_CST) ? rhs : NULL_TREE;
+}
+
 /* Handle a single character store.  */
 
 static bool
@@ -2927,11 +2942,11 @@ handle_char_store (gimple_stmt_iterator *gsi)
        }
     }
   else if (idx == 0
-          && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST
+          && (rhs = get_string_cst (gimple_assign_rhs1 (stmt)))
           && ssaname == NULL_TREE
           && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE)
     {
-      size_t l = strlen (TREE_STRING_POINTER (gimple_assign_rhs1 (stmt)));
+      size_t l = strlen (TREE_STRING_POINTER (rhs));
       HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs));
       if (a > 0 && (unsigned HOST_WIDE_INT) a > l)
        {