On 02/25/2017 11:54 AM, Prathamesh Kulkarni wrote:
On 25 February 2017 at 14:43, Marc Glisse <marc.gli...@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);
  }

Reply via email to