re PR tree-optimization/79697 (unused realloc(0, n) not eliminated)
authorPrathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
Sat, 29 Apr 2017 10:05:13 +0000 (10:05 +0000)
committerPrathamesh Kulkarni <prathamesh3492@gcc.gnu.org>
Sat, 29 Apr 2017 10:05:13 +0000 (10:05 +0000)
2017-04-29  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

PR tree-optimization/79697
* tree-ssa-dce.c (mark_stmt_if_obviously_necessary): Check if callee
is BUILT_IN_STRDUP, BUILT_IN_STRNDUP, BUILT_IN_REALLOC.
(propagate_necessity): Check if def_callee is BUILT_IN_STRDUP or
BUILT_IN_STRNDUP.
* gimple-fold.c (gimple_fold_builtin_realloc): New function.
(gimple_fold_builtin): Call gimple_fold_builtin_realloc.

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

From-SVN: r247407

gcc/ChangeLog
gcc/gimple-fold.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/pr79697.c [new file with mode: 0644]
gcc/tree-ssa-dce.c

index f8486784f4c0f05e2480d5e0ef104a7eb62e5334..a2082928881091ebab4b243dc50fb398f2f88287 100644 (file)
@@ -1,3 +1,13 @@
+2017-04-29  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
+
+       PR tree-optimization/79697
+       * tree-ssa-dce.c (mark_stmt_if_obviously_necessary): Check if callee
+       is BUILT_IN_STRDUP, BUILT_IN_STRNDUP, BUILT_IN_REALLOC.
+       (propagate_necessity): Check if def_callee is BUILT_IN_STRDUP or
+       BUILT_IN_STRNDUP.
+       * gimple-fold.c (gimple_fold_builtin_realloc): New function.
+       (gimple_fold_builtin): Call gimple_fold_builtin_realloc.
+
 2017-04-28  Martin Sebor  <msebor@redhat.com>
 
        PR tree-optimization/80523
index a6a958cefa4c560df9b828d55100e2802c2a1fd1..5ebdcdfd796f983255d13f939628253ff689d9d3 100644 (file)
@@ -3251,6 +3251,28 @@ gimple_fold_builtin_acc_on_device (gimple_stmt_iterator *gsi, tree arg0)
   return true;
 }
 
+/* Fold realloc (0, n) -> malloc (n).  */
+
+static bool
+gimple_fold_builtin_realloc (gimple_stmt_iterator *gsi)
+{
+  gimple *stmt = gsi_stmt (*gsi);
+  tree arg = gimple_call_arg (stmt, 0);
+  tree size = gimple_call_arg (stmt, 1);
+
+  if (operand_equal_p (arg, null_pointer_node, 0))
+    {
+      tree fn_malloc = builtin_decl_implicit (BUILT_IN_MALLOC);
+      if (fn_malloc)
+       {
+         gcall *repl = gimple_build_call (fn_malloc, 1, size);
+         replace_call_with_call_and_fold (gsi, repl);
+         return true;
+       }
+    }
+  return false;
+}
+
 /* Fold the non-target builtin at *GSI and return whether any simplification
    was made.  */
 
@@ -3409,6 +3431,9 @@ gimple_fold_builtin (gimple_stmt_iterator *gsi)
     case BUILT_IN_ACC_ON_DEVICE:
       return gimple_fold_builtin_acc_on_device (gsi,
                                                gimple_call_arg (stmt, 0));
+    case BUILT_IN_REALLOC:
+      return gimple_fold_builtin_realloc (gsi);
+
     default:;
     }
 
index b7d5af6f1bdf577ae43f43297aba6e314c327219..59f1a31bfd77bf37f01e388ff977e82214207842 100644 (file)
@@ -1,3 +1,8 @@
+2017-04-29  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
+
+       PR tree-optimization/79697
+       * gcc.dg/tree-ssa/pr79697.c: New test.
+
 2017-04-29  Tom de Vries  <tom@codesourcery.com>
 
        * gcc.dg/tree-prof/pr66295.c: Require effective target avx512f.
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
new file mode 100644 (file)
index 0000000..d4f6473
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-gimple -fdump-tree-cddce-details -fdump-tree-optimized" } */
+
+void f(void)
+{
+  __builtin_strdup ("abc");
+}
+
+void g(void)
+{
+  __builtin_strndup ("abc", 3);
+}
+
+void h(void)
+{
+  __builtin_realloc (0, 10);
+}
+
+void k(void)
+{
+  char *p = __builtin_strdup ("abc");
+  __builtin_free (p);
+
+  char *q = __builtin_strndup ("abc", 3);
+  __builtin_free (q);
+}
+
+/* { dg-final { scan-tree-dump "Deleting : __builtin_strdup" "cddce1" } } */
+/* { dg-final { scan-tree-dump "Deleting : __builtin_strndup" "cddce1" } } */
+/* { dg-final { scan-tree-dump "__builtin_malloc" "gimple" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_strdup" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_strndup" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_free" "optimized" } } */
index 5ebe57b0983678d22e81a8a45c4104e89d4cbb2f..e17659df91fcee0f34a91b0c2953f75db3ad3762 100644 (file)
@@ -233,6 +233,8 @@ mark_stmt_if_obviously_necessary (gimple *stmt, bool aggressive)
            case BUILT_IN_CALLOC:
            case BUILT_IN_ALLOCA:
            case BUILT_IN_ALLOCA_WITH_ALIGN:
+           case BUILT_IN_STRDUP:
+           case BUILT_IN_STRNDUP:
              return;
 
            default:;
@@ -780,7 +782,9 @@ propagate_necessity (bool aggressive)
                  && DECL_BUILT_IN_CLASS (def_callee) == BUILT_IN_NORMAL
                  && (DECL_FUNCTION_CODE (def_callee) == BUILT_IN_ALIGNED_ALLOC
                      || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_MALLOC
-                     || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_CALLOC))
+                     || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_CALLOC
+                     || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_STRDUP
+                     || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_STRNDUP))
                {
                  gimple *bounds_def_stmt;
                  tree bounds;