Hi,
The attached patch deletes calls to strdup, strndup if it's
return-value is unused,
and same for realloc if the first arg is NULL.
Bootstrap+tested on x86_64-unknown-linux-gnu.
OK for GCC 8 ?

Thanks,
Prathamesh
2017-02-25  Prathamesh Kulkarni  <prathamesh.kulka...@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.

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

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
new file mode 100644
index 0000000..a6e75a6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-cddce-details" } */
+
+void f(void)
+{
+  __builtin_strdup ("abc");
+}
+
+void g(void)
+{
+  __builtin_strndup ("abc", 3);
+}
+
+void h(void)
+{
+  __builtin_realloc (0, 10);
+}
+
+/* { dg-final { scan-tree-dump "Deleting : __builtin_strdup" "cddce1" } } */
+/* { dg-final { scan-tree-dump "Deleting : __builtin_strndup" "cddce1" } } */
+/* { dg-final { scan-tree-dump "Deleting : __builtin_realloc" "cddce1" } } */
diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c
index 5ebe57b..b0f62b0 100644
--- a/gcc/tree-ssa-dce.c
+++ b/gcc/tree-ssa-dce.c
@@ -233,8 +233,17 @@ 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;
 
+           case BUILT_IN_REALLOC:
+             {
+               tree arg0 = gimple_call_arg (stmt, 0);
+               if (operand_equal_p (arg0, null_pointer_node, 0))
+                 return;
+               break;
+             }
            default:;
            }
        /* Most, but not all function calls are required.  Function calls that

Reply via email to