diff mbox

PR79697: Delete calls to strdup, strndup, realloc if there is no lhs

Message ID CAAgBjM==xR+5sHG5appwJdwJBFzNcBN76HF4idWnOm8t8u8gRA@mail.gmail.com
State New
Headers show

Commit Message

Prathamesh Kulkarni Feb. 25, 2017, 8:40 a.m. UTC
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.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.

Comments

Marc Glisse Feb. 25, 2017, 9:13 a.m. UTC | #1
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))
Peter Bergner Feb. 28, 2017, 6:49 p.m. UTC | #2
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
Peter Bergner Feb. 28, 2017, 9:55 p.m. UTC | #3
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
Jeff Law April 24, 2017, 9:11 p.m. UTC | #4
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
Prathamesh Kulkarni April 25, 2017, 6:06 a.m. UTC | #5
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
>
Jeff Law April 28, 2017, 8:07 p.m. UTC | #6
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
Prathamesh Kulkarni April 29, 2017, 1:33 p.m. UTC | #7
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
>
diff mbox

Patch

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