This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: avoiding recursive calls of calloc due to optimization
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Andrew Pinski <pinskia at gmail dot com>
- Cc: Daniel Gutson <daniel dot gutson at tallertechnologies dot com>, gcc Mailing List <gcc at gcc dot gnu dot org>, glisse at gcc dot gnu dot org, Sebastian Huber <sebastian dot huber at embedded-brains dot de>
- Date: Mon, 21 Sep 2015 19:32:49 +0200
- Subject: Re: avoiding recursive calls of calloc due to optimization
- Authentication-results: sourceware.org; auth=none
- References: <CAF5HaEWYyPpxQe_iJqF8LXn_Yp-8aJ6uzMP-nzK9Y5UhRX_JwQ at mail dot gmail dot com> <CA+=Sn1nTxi2V8YjxeMUc41KATyecGqs+=oR_c4W3WnHWZwTkpg at mail dot gmail dot com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Mon, Sep 21, 2015 at 10:26:23AM -0700, Andrew Pinski wrote:
> On Mon, Sep 21, 2015 at 10:20 AM, Daniel Gutson
> <daniel.gutson@tallertechnologies.com> wrote:
> > This is derived from https://gcc.gnu.org/ml/gcc-help/2015-03/msg00091.html
> >
> > Currently, gcc provides an optimization that transforms a call to
> > malloc and a call to memset into a call to calloc.
> > This is fine except when it takes place within the calloc() function
> > implementation itself, causing a recursive call.
> > Two alternatives have been proposed: -fno-malloc-builtin and disable
> > optimizations in calloc().
> > I think the former is suboptimal since it affects all the code just
> > because of the implementation of one function (calloc()),
> > whereas the latter is suboptimal too since it disables the
> > optimizations in the whole function (calloc too).
> > I think of two alternatives: either make -fno-calloc-builtin to
> > disable the optimization, or make the optimization aware of the
> > function context where it is operating and prevent it to do the
> > transformation if the function is calloc().
> >
> > Please help me to find the best alternative so we can implent it.
>
> Did you try the optimize attribute?
>
> Also you can try the following:
>
> size_t ns = size*elements;
> If (ns / elements != size)
> return NULL;
> void *ptr = malloc (ns);
> asm ("":"+r"(ptr));
> memset (ptr, 0, ns);
>
> Notice I put in a check for overflow in there.
You can use
size_t ns;
if (__builtin_mul_overflow (size, elements, &ns))
return ns;
for that part ;)
Jakub