public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
@ 2017-02-25  9:13 Prathamesh Kulkarni
  2017-02-25  9:52 ` Marc Glisse
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Prathamesh Kulkarni @ 2017-02-25  9:13 UTC (permalink / raw)
  To: gcc Patches, Richard Biener

[-- Attachment #1: Type: text/plain, Size: 216 bytes --]

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

[-- Attachment #2: pr79697-1.txt --]
[-- Type: text/plain, Size: 1619 bytes --]

2017-02-25  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.

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-02-25  9:13 PR79697: Delete calls to strdup, strndup, realloc if there is no lhs Prathamesh Kulkarni
@ 2017-02-25  9:52 ` Marc Glisse
  2017-02-25 20:54   ` Prathamesh Kulkarni
  2017-02-28 19:20 ` Peter Bergner
  2017-04-24 21:21 ` Jeff Law
  2 siblings, 1 reply; 12+ messages in thread
From: Marc Glisse @ 2017-02-25  9:52 UTC (permalink / raw)
  To: Prathamesh Kulkarni; +Cc: gcc Patches, Richard Biener

On Sat, 25 Feb 2017, Prathamesh Kulkarni wrote:

> 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 ?

Instead of specializing realloc(0,*) wherever we can perform the same 
optimization as with malloc, wouldn't it be better to optimize:
realloc(0,n) -> malloc(n)
and let the malloc optimizations happen?

(realloc(p,0)->free(p) is more tricky because of the return value, like 
malloc(0))

-- 
Marc Glisse

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-02-25  9:52 ` Marc Glisse
@ 2017-02-25 20:54   ` Prathamesh Kulkarni
  2017-02-26  9:12     ` Martin Sebor
  0 siblings, 1 reply; 12+ messages in thread
From: Prathamesh Kulkarni @ 2017-02-25 20:54 UTC (permalink / raw)
  To: gcc Patches, marc.glisse; +Cc: Richard Biener

[-- Attachment #1: Type: text/plain, Size: 853 bytes --]

On 25 February 2017 at 14:43, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Sat, 25 Feb 2017, Prathamesh Kulkarni wrote:
>
>> 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 ?
>
>
> Instead of specializing realloc(0,*) wherever we can perform the same
> optimization as with malloc, wouldn't it be better to optimize:
> realloc(0,n) -> malloc(n)
> and let the malloc optimizations happen?
Thanks for the suggestions. In the attached patch, realloc (0, n) is
folded to malloc (n).
Bootstrap+test in progress on x86_64-unknown-linux-gnu.
Does the patch look OK ?

Thanks,
Prathamesh
>
> (realloc(p,0)->free(p) is more tricky because of the return value, like
> malloc(0))
>
> --
> Marc Glisse

[-- Attachment #2: pr79697-2.diff --]
[-- Type: text/plain, Size: 2287 bytes --]

diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index a75dd91..e6eceea 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -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:;
     }
 
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..8219289
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-gimple -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 "__builtin_malloc" "gimple" } } */
diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c
index 5ebe57b..4225c3c 100644
--- a/gcc/tree-ssa-dce.c
+++ b/gcc/tree-ssa-dce.c
@@ -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:;

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-02-25 20:54   ` Prathamesh Kulkarni
@ 2017-02-26  9:12     ` Martin Sebor
  2017-02-26 22:08       ` Prathamesh Kulkarni
  0 siblings, 1 reply; 12+ messages in thread
From: Martin Sebor @ 2017-02-26  9:12 UTC (permalink / raw)
  To: Prathamesh Kulkarni, gcc Patches, marc.glisse; +Cc: Richard Biener

On 02/25/2017 11:54 AM, Prathamesh Kulkarni wrote:
> On 25 February 2017 at 14:43, Marc Glisse <marc.glisse@inria.fr> wrote:
>> On Sat, 25 Feb 2017, Prathamesh Kulkarni wrote:
>>
>>> 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 ?
>>
>>
>> Instead of specializing realloc(0,*) wherever we can perform the same
>> optimization as with malloc, wouldn't it be better to optimize:
>> realloc(0,n) -> malloc(n)
>> and let the malloc optimizations happen?
> Thanks for the suggestions. In the attached patch, realloc (0, n) is
> folded to malloc (n).
> Bootstrap+test in progress on x86_64-unknown-linux-gnu.
> Does the patch look OK ?

Although it's not part of the bug report, I wonder if a complete
patch should also extend the malloc/free DCE to str{,n}dup/free
calls and eliminate pairs like these:

   void f (const char *s)
   {
     char *p = strdup (src);
     free (p);
   }

(That seems to be just a matter of adding a couple of conditionals
for BUILT_IN_STR{,N}DUP in propagate_necessity).

Martin

PS Another optimization, though one that's most likely outside
the scope of this patch, is to eliminate all of the following:

   void f (const char *s, char *s2)
   {
     char *p = strdup (s);
     strcpy (p, s2);
     free (p);
   }

as is done in:

   void f (unsigned n, const char *s)
   {
     char *p = malloc (n);
     memcpy (p, s, n);
     free (p);
   }

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-02-26  9:12     ` Martin Sebor
@ 2017-02-26 22:08       ` Prathamesh Kulkarni
  2017-04-24  7:33         ` Prathamesh Kulkarni
  0 siblings, 1 reply; 12+ messages in thread
From: Prathamesh Kulkarni @ 2017-02-26 22:08 UTC (permalink / raw)
  To: Martin Sebor; +Cc: gcc Patches, marc.glisse, Richard Biener

[-- Attachment #1: Type: text/plain, Size: 2204 bytes --]

On 26 February 2017 at 05:16, Martin Sebor <msebor@gmail.com> wrote:
> On 02/25/2017 11:54 AM, Prathamesh Kulkarni wrote:
>>
>> On 25 February 2017 at 14:43, Marc Glisse <marc.glisse@inria.fr> wrote:
>>>
>>> On Sat, 25 Feb 2017, Prathamesh Kulkarni wrote:
>>>
>>>> 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 ?
>>>
>>>
>>>
>>> Instead of specializing realloc(0,*) wherever we can perform the same
>>> optimization as with malloc, wouldn't it be better to optimize:
>>> realloc(0,n) -> malloc(n)
>>> and let the malloc optimizations happen?
>>
>> Thanks for the suggestions. In the attached patch, realloc (0, n) is
>> folded to malloc (n).
>> Bootstrap+test in progress on x86_64-unknown-linux-gnu.
>> Does the patch look OK ?
>
>
> Although it's not part of the bug report, I wonder if a complete
> patch should also extend the malloc/free DCE to str{,n}dup/free
> calls and eliminate pairs like these:
>
>   void f (const char *s)
>   {
>     char *p = strdup (src);
>     free (p);
>   }
>
> (That seems to be just a matter of adding a couple of conditionals
> for BUILT_IN_STR{,N}DUP in propagate_necessity).
Hi Martin,
Thanks for the suggestions, I have updated the patch accordingly.
Does it look OK ?
>
> Martin
>
> PS Another optimization, though one that's most likely outside
> the scope of this patch, is to eliminate all of the following:
>
>   void f (const char *s, char *s2)
>   {
>     char *p = strdup (s);
>     strcpy (p, s2);
>     free (p);
>   }
>
> as is done in:
>
>   void f (unsigned n, const char *s)
>   {
>     char *p = malloc (n);
>     memcpy (p, s, n);
>     free (p);
>   }
>
Hmm, dse1 detects that the store to p in memcpy() is dead and deletes the call.
cddce1 then removes calls to  malloc and free thus making the function empty.
It doesn't work if memcpy is replaced by strcpy:

void f (unsigned n, char *s2)
{
  char *p = __builtin_malloc (n);
  __builtin_strcpy (p, s2);
  __builtin_free (p);
}

I suppose strcpy should be special-cased too in dse similar to memcpy ?

Thanks,
Prathamesh

[-- Attachment #2: pr79697-3.diff --]
[-- Type: text/plain, Size: 3240 bytes --]

diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index a75dd91..e6eceea 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -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:;
     }
 
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..d4f6473
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
@@ -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" } } */
diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c
index 5ebe57b..e17659d 100644
--- a/gcc/tree-ssa-dce.c
+++ b/gcc/tree-ssa-dce.c
@@ -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;

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-02-25  9:13 PR79697: Delete calls to strdup, strndup, realloc if there is no lhs Prathamesh Kulkarni
  2017-02-25  9:52 ` Marc Glisse
@ 2017-02-28 19:20 ` Peter Bergner
  2017-02-28 22:02   ` Peter Bergner
  2017-04-24 21:21 ` Jeff Law
  2 siblings, 1 reply; 12+ messages in thread
From: Peter Bergner @ 2017-02-28 19:20 UTC (permalink / raw)
  To: Prathamesh Kulkarni; +Cc: gcc Patches, Richard Biener

On 2/25/17 2:40 AM, Prathamesh Kulkarni wrote:
> 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.

Why limit ourselves to strdup and strndup?  Can't we do the same
for all functions that are marked as const/pure since we know
they have no side effects other than their return value?

Peter


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-02-28 19:20 ` Peter Bergner
@ 2017-02-28 22:02   ` Peter Bergner
  0 siblings, 0 replies; 12+ messages in thread
From: Peter Bergner @ 2017-02-28 22:02 UTC (permalink / raw)
  To: Prathamesh Kulkarni; +Cc: gcc Patches, Richard Biener

On 2/28/17 12:49 PM, Peter Bergner wrote:
> On 2/25/17 2:40 AM, Prathamesh Kulkarni wrote:
>> 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.
> 
> Why limit ourselves to strdup and strndup?  Can't we do the same
> for all functions that are marked as const/pure since we know
> they have no side effects other than their return value?

Nevermind.  Marc reminded me in the PR that these are not const/pure
functions.

Peter


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-02-26 22:08       ` Prathamesh Kulkarni
@ 2017-04-24  7:33         ` Prathamesh Kulkarni
  0 siblings, 0 replies; 12+ messages in thread
From: Prathamesh Kulkarni @ 2017-04-24  7:33 UTC (permalink / raw)
  To: Martin Sebor; +Cc: gcc Patches, marc.glisse, Richard Biener

[-- Attachment #1: Type: text/plain, Size: 2623 bytes --]

On 27 February 2017 at 00:10, Prathamesh Kulkarni
<prathamesh.kulkarni@linaro.org> wrote:
> On 26 February 2017 at 05:16, Martin Sebor <msebor@gmail.com> wrote:
>> On 02/25/2017 11:54 AM, Prathamesh Kulkarni wrote:
>>>
>>> On 25 February 2017 at 14:43, Marc Glisse <marc.glisse@inria.fr> wrote:
>>>>
>>>> On Sat, 25 Feb 2017, Prathamesh Kulkarni wrote:
>>>>
>>>>> 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 ?
>>>>
>>>>
>>>>
>>>> Instead of specializing realloc(0,*) wherever we can perform the same
>>>> optimization as with malloc, wouldn't it be better to optimize:
>>>> realloc(0,n) -> malloc(n)
>>>> and let the malloc optimizations happen?
>>>
>>> Thanks for the suggestions. In the attached patch, realloc (0, n) is
>>> folded to malloc (n).
>>> Bootstrap+test in progress on x86_64-unknown-linux-gnu.
>>> Does the patch look OK ?
>>
>>
>> Although it's not part of the bug report, I wonder if a complete
>> patch should also extend the malloc/free DCE to str{,n}dup/free
>> calls and eliminate pairs like these:
>>
>>   void f (const char *s)
>>   {
>>     char *p = strdup (src);
>>     free (p);
>>   }
>>
>> (That seems to be just a matter of adding a couple of conditionals
>> for BUILT_IN_STR{,N}DUP in propagate_necessity).
> Hi Martin,
> Thanks for the suggestions, I have updated the patch accordingly.
> Does it look OK ?
Hi,
The attached patch for PR79697 passes bootstrap+test on x86_64 and
cross-tested on arm*-*-* and aarch64*-*-*.
As per the previous suggestions, the patch folds realloc (0, n) -> malloc (n).
Is it OK for trunk ?

Thanks,
Prathamesh
>>
>> Martin
>>
>> PS Another optimization, though one that's most likely outside
>> the scope of this patch, is to eliminate all of the following:
>>
>>   void f (const char *s, char *s2)
>>   {
>>     char *p = strdup (s);
>>     strcpy (p, s2);
>>     free (p);
>>   }
>>
>> as is done in:
>>
>>   void f (unsigned n, const char *s)
>>   {
>>     char *p = malloc (n);
>>     memcpy (p, s, n);
>>     free (p);
>>   }
>>
> Hmm, dse1 detects that the store to p in memcpy() is dead and deletes the call.
> cddce1 then removes calls to  malloc and free thus making the function empty.
> It doesn't work if memcpy is replaced by strcpy:
>
> void f (unsigned n, char *s2)
> {
>   char *p = __builtin_malloc (n);
>   __builtin_strcpy (p, s2);
>   __builtin_free (p);
> }
>
> I suppose strcpy should be special-cased too in dse similar to memcpy ?
>
> Thanks,
> Prathamesh

[-- Attachment #2: pr79697-3.txt --]
[-- Type: text/plain, Size: 3720 bytes --]

2017-04-24  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.

diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index a75dd91..e6eceea 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -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:;
     }
 
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..d4f6473
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
@@ -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" } } */
diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c
index 5ebe57b..e17659d 100644
--- a/gcc/tree-ssa-dce.c
+++ b/gcc/tree-ssa-dce.c
@@ -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;

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-02-25  9:13 PR79697: Delete calls to strdup, strndup, realloc if there is no lhs Prathamesh Kulkarni
  2017-02-25  9:52 ` Marc Glisse
  2017-02-28 19:20 ` Peter Bergner
@ 2017-04-24 21:21 ` Jeff Law
  2017-04-25  7:01   ` Prathamesh Kulkarni
  2 siblings, 1 reply; 12+ messages in thread
From: Jeff Law @ 2017-04-24 21:21 UTC (permalink / raw)
  To: Prathamesh Kulkarni, gcc Patches, Richard Biener

On 02/25/2017 01:40 AM, Prathamesh Kulkarni wrote:
> 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
> 
> 
> pr79697-1.txt
> 
> 
> 2017-02-25  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.
> 
> testsuite/
> 	* gcc.dg/tree-ssa/pr79697.c: New test.
OK for the trunk.

jeff

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-04-24 21:21 ` Jeff Law
@ 2017-04-25  7:01   ` Prathamesh Kulkarni
  2017-04-28 20:28     ` Jeff Law
  0 siblings, 1 reply; 12+ messages in thread
From: Prathamesh Kulkarni @ 2017-04-25  7:01 UTC (permalink / raw)
  To: Jeff Law; +Cc: gcc Patches, Richard Biener

On 25 April 2017 at 02:41, Jeff Law <law@redhat.com> wrote:
> On 02/25/2017 01:40 AM, Prathamesh Kulkarni wrote:
>>
>> 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
>>
>>
>> pr79697-1.txt
>>
>>
>> 2017-02-25  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.
>>
>> testsuite/
>>         * gcc.dg/tree-ssa/pr79697.c: New test.
>
> OK for the trunk.
Hi Jeff,
Did you intend to approve the original patch (pr79697-1.txt) or the
latest one (pr79697-3.txt that also folds realloc (0, n) to malloc
(n)) ?

Thanks,
Prathamesh
>
> jeff
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-04-25  7:01   ` Prathamesh Kulkarni
@ 2017-04-28 20:28     ` Jeff Law
  2017-04-29 15:17       ` Prathamesh Kulkarni
  0 siblings, 1 reply; 12+ messages in thread
From: Jeff Law @ 2017-04-28 20:28 UTC (permalink / raw)
  To: Prathamesh Kulkarni; +Cc: gcc Patches, Richard Biener

On 04/25/2017 12:06 AM, Prathamesh Kulkarni wrote:
> On 25 April 2017 at 02:41, Jeff Law <law@redhat.com> wrote:
>> On 02/25/2017 01:40 AM, Prathamesh Kulkarni wrote:
>>>
>>> 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
>>>
>>>
>>> pr79697-1.txt
>>>
>>>
>>> 2017-02-25  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.
>>>
>>> testsuite/
>>>          * gcc.dg/tree-ssa/pr79697.c: New test.
>>
>> OK for the trunk.
> Hi Jeff,
> Did you intend to approve the original patch (pr79697-1.txt) or the
> latest one (pr79697-3.txt that also folds realloc (0, n) to malloc
> (n)) ?
I was referring to the 3rd iteration.  Sorry to have replied to the 
first patch in the series -- my patches queue is a bit of a mess as 
we're just getting started on gcc-8 stuff.


jeff

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PR79697: Delete calls to strdup, strndup, realloc if there is no lhs
  2017-04-28 20:28     ` Jeff Law
@ 2017-04-29 15:17       ` Prathamesh Kulkarni
  0 siblings, 0 replies; 12+ messages in thread
From: Prathamesh Kulkarni @ 2017-04-29 15:17 UTC (permalink / raw)
  To: Jeff Law; +Cc: gcc Patches, Richard Biener

On 29 April 2017 at 01:37, Jeff Law <law@redhat.com> wrote:
> On 04/25/2017 12:06 AM, Prathamesh Kulkarni wrote:
>>
>> On 25 April 2017 at 02:41, Jeff Law <law@redhat.com> wrote:
>>>
>>> On 02/25/2017 01:40 AM, Prathamesh Kulkarni wrote:
>>>>
>>>>
>>>> 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
>>>>
>>>>
>>>> pr79697-1.txt
>>>>
>>>>
>>>> 2017-02-25  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.
>>>>
>>>> testsuite/
>>>>          * gcc.dg/tree-ssa/pr79697.c: New test.
>>>
>>>
>>> OK for the trunk.
>>
>> Hi Jeff,
>> Did you intend to approve the original patch (pr79697-1.txt) or the
>> latest one (pr79697-3.txt that also folds realloc (0, n) to malloc
>> (n)) ?
>
> I was referring to the 3rd iteration.  Sorry to have replied to the first
> patch in the series -- my patches queue is a bit of a mess as we're just
> getting started on gcc-8 stuff.
Committed as r247407, thanks!
>
>
> jeff
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2017-04-29 13:33 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-25  9:13 PR79697: Delete calls to strdup, strndup, realloc if there is no lhs Prathamesh Kulkarni
2017-02-25  9:52 ` Marc Glisse
2017-02-25 20:54   ` Prathamesh Kulkarni
2017-02-26  9:12     ` Martin Sebor
2017-02-26 22:08       ` Prathamesh Kulkarni
2017-04-24  7:33         ` Prathamesh Kulkarni
2017-02-28 19:20 ` Peter Bergner
2017-02-28 22:02   ` Peter Bergner
2017-04-24 21:21 ` Jeff Law
2017-04-25  7:01   ` Prathamesh Kulkarni
2017-04-28 20:28     ` Jeff Law
2017-04-29 15:17       ` Prathamesh Kulkarni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).