[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-05-01 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

Martin Sebor  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |FIXED

--- Comment #9 from Martin Sebor  ---
Fixed by Prathamesh's patch in comment #8.

[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-04-29 Thread prathamesh3492 at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

--- Comment #8 from prathamesh3492 at gcc dot gnu.org ---
Author: prathamesh3492
Date: Sat Apr 29 10:05:13 2017
New Revision: 247407

URL: https://gcc.gnu.org/viewcvs?rev=247407=gcc=rev
Log:
2017-04-29  Prathamesh Kulkarni  

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.

Added:
trunk/gcc/testsuite/gcc.dg/tree-ssa/pr79697.c
Modified:
trunk/gcc/ChangeLog
trunk/gcc/gimple-fold.c
trunk/gcc/testsuite/ChangeLog
trunk/gcc/tree-ssa-dce.c

[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-02-28 Thread bergner at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

--- Comment #7 from Peter Bergner  ---
(In reply to Marc Glisse from comment #6)
> * malloc, strdup, etc are not pure!

Ahh, of course not.  Nevermind. :-)

[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-02-28 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

--- Comment #6 from Marc Glisse  ---
(In reply to Peter Bergner from comment #5)
> Why do we have to special case these functions?  Why can't we do this for
> all functions that are marked as const/pure, since we know they have no side
> effects other than their return value?

* I expect we already optimize pure/const functions. Do you have an example
where we don't?
* malloc, strdup, etc are not pure!

[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-02-28 Thread bergner at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

Peter Bergner  changed:

   What|Removed |Added

 CC||bergner at gcc dot gnu.org

--- Comment #5 from Peter Bergner  ---
(In reply to prathamesh3492 from comment #4)
> The attached (untested) patch does not mark stmt necessary if it's a call
> stmt and callee is strdup/strndup, and ...

Why do we have to special case these functions?  Why can't we do this for all
functions that are marked as const/pure, since we know they have no side
effects other than their return value?

[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-02-23 Thread prathamesh3492 at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

prathamesh3492 at gcc dot gnu.org changed:

   What|Removed |Added

 CC||prathamesh3492 at gcc dot 
gnu.org

--- Comment #4 from prathamesh3492 at gcc dot gnu.org ---
Created attachment 40821
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40821=edit
untested patch

In cddce/dce pass, mark_stmt_if_obviously_necessary() does not special case
strdup, strndup, and marks them as obviously necessary.
From the dump of cddce1:
Marking useful stmt: __builtin_strdup ("abc");

The attached (untested) patch does not mark stmt necessary if it's a call stmt
and callee is strdup/strndup, and for realloc if the first arg compares equal
to null_pointer_node and can thus delete these calls.
Does it look OK ?

Thanks,
Prathamesh

[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-02-23 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

--- Comment #3 from Andrew Pinski  ---
It makes sense to convert realloc(0, n) to just malloc and the rest just works.

[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-02-23 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

Martin Sebor  changed:

   What|Removed |Added

   See Also||https://gcc.gnu.org/bugzill
   ||a/show_bug.cgi?id=79696

--- Comment #2 from Martin Sebor  ---
Bug 79696 tracks the missing warnings.

[Bug tree-optimization/79697] unused realloc(0, n) not eliminated

2017-02-23 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79697

--- Comment #1 from Martin Sebor  ---
Ditto for __builtin_strdup and __builtin_strndup.  Both of those calls can (and
arguably should) be eliminated.  If they're not eliminated (but even if they
are) a warning on them can and arguably should be issued as suggested in bug
79696.

Clang 5.0 eliminates both of the calls but doesn't issue a warning.  Clang does
not, however, eliminate the unused call to realloc in the test case in comment
#1.

$ cat t.c && gcc -O2 -S -Wall -Wextra -Wpedantic -Wunused-result
-fdump-tree-optimized=/dev/stdout t.c
void f (void)
{
  __builtin_strdup ("abc");
}

void g (void)
{
  __builtin_strndup ("abc", 2);
}


;; Function f (f, funcdef_no=0, decl_uid=1795, cgraph_uid=0, symbol_order=0)

f ()
{
   [100.00%]:
  __builtin_strdup ("abc"); [tail call]
  return;

}



;; Function g (g, funcdef_no=1, decl_uid=1798, cgraph_uid=1, symbol_order=1)

g ()
{
   [100.00%]:
  __builtin_strndup ("abc", 2); [tail call]
  return;

}