]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR tree-optimization/71588 (ICE on valid code at -O2 and -O3 on x86_64...
authorJakub Jelinek <jakub@redhat.com>
Thu, 7 Jul 2016 21:52:31 +0000 (23:52 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 7 Jul 2016 21:52:31 +0000 (23:52 +0200)
Backported from mainline
2016-06-21  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/71588
* tree-ssa-strlen.c (valid_builtin_call): New function.
(adjust_last_stmt, strlen_optimize_stmt): Use it.

* gcc.dg/pr71558.c: New test.

From-SVN: r238144

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

index c8db5319f4e4e01dbc37de96033193b4193765fb..1fc09f34a5dc4f38849efc8239c8be6b65129d74 100644 (file)
@@ -1,6 +1,12 @@
 2016-07-07  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2016-06-21  Jakub Jelinek  <jakub@redhat.com>
+
+       PR tree-optimization/71588
+       * tree-ssa-strlen.c (valid_builtin_call): New function.
+       (adjust_last_stmt, strlen_optimize_stmt): Use it.
+
        2016-06-10  Jakub Jelinek  <jakub@redhat.com>
 
        PR middle-end/71494
index 5a7410725aadf866a48c5a4b5e4d036755298acb..89aa60849019292aab5607eb8754527ce9e9a5ef 100644 (file)
@@ -1,6 +1,11 @@
 2016-07-07  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2016-06-21  Jakub Jelinek  <jakub@redhat.com>
+
+       PR tree-optimization/71588
+       * gcc.dg/pr71558.c: New test.
+
        2016-06-10  Jakub Jelinek  <jakub@redhat.com>
 
        PR middle-end/71494
diff --git a/gcc/testsuite/gcc.dg/pr71558.c b/gcc/testsuite/gcc.dg/pr71558.c
new file mode 100644 (file)
index 0000000..33a648e
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR tree-optimization/71588 */
+
+/* strcpy must not be pure, but make sure we don't ICE even when
+   it is declared incorrectly.  */
+char *strcpy (char *, const char *) __attribute__ ((__pure__));
+__SIZE_TYPE__ strlen (const char *);
+void *malloc (__SIZE_TYPE__);
+
+char a[20];
+
+char *
+foo (void)
+{
+  __SIZE_TYPE__ b = strlen (a);
+  char *c = malloc (b);
+  return strcpy (c, a);
+}
index 8d99a31d5df9883d4d66c5922bbd8ae55d44df16..bd6cbe82e8279ed77b2ac1b0c7809c5050b4caf0 100644 (file)
@@ -768,6 +768,49 @@ find_equal_ptrs (tree ptr, int idx)
     }
 }
 
+/* Return true if STMT is a call to a builtin function with the right
+   arguments and attributes that should be considered for optimization
+   by this pass.  */
+
+static bool
+valid_builtin_call (gimple stmt)
+{
+  if (!gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
+    return false;
+
+  tree callee = gimple_call_fndecl (stmt);
+  switch (DECL_FUNCTION_CODE (callee))
+    {
+    case BUILT_IN_STRCHR:
+    case BUILT_IN_STRLEN:
+      /* The above functions should be pure.  Punt if they aren't.  */
+      if (gimple_vdef (stmt) || gimple_vuse (stmt) == NULL_TREE)
+       return false;
+      break;
+
+    case BUILT_IN_MEMCPY:
+    case BUILT_IN_MEMCPY_CHK:
+    case BUILT_IN_MEMPCPY:
+    case BUILT_IN_MEMPCPY_CHK:
+    case BUILT_IN_STPCPY:
+    case BUILT_IN_STPCPY_CHK:
+    case BUILT_IN_STRCAT:
+    case BUILT_IN_STRCAT_CHK:
+    case BUILT_IN_STRCPY:
+    case BUILT_IN_STRCPY_CHK:
+      /* The above functions should be neither const nor pure.  Punt if they
+        aren't.  */
+      if (gimple_vdef (stmt) == NULL_TREE || gimple_vuse (stmt) == NULL_TREE)
+       return false;
+      break;
+
+    default:
+      break;
+    }
+
+  return true;
+}
+
 /* If the last .MEM setter statement before STMT is
    memcpy (x, y, strlen (y) + 1), the only .MEM use of it is STMT
    and STMT is known to overwrite x[strlen (x)], adjust the last memcpy to
@@ -842,7 +885,7 @@ adjust_last_stmt (strinfo si, gimple stmt, bool is_strcat)
       return;
     }
 
-  if (!gimple_call_builtin_p (last.stmt, BUILT_IN_NORMAL))
+  if (!valid_builtin_call (last.stmt))
     return;
 
   callee = gimple_call_fndecl (last.stmt);
@@ -1827,7 +1870,7 @@ strlen_optimize_stmt (gimple_stmt_iterator *gsi)
   if (is_gimple_call (stmt))
     {
       tree callee = gimple_call_fndecl (stmt);
-      if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
+      if (valid_builtin_call (stmt))
        switch (DECL_FUNCTION_CODE (callee))
          {
          case BUILT_IN_STRLEN: