https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56955

--- Comment #13 from Dan Gohman <sunfish at mozilla dot com> ---
(In reply to Paul Eggert from comment #12)
> (In reply to Rich Felker from comment #10)
> > This assumption only aids
> > optimization in the case where a pointer residing in the obtained memory is
> > used (e.g. dereferenced or compared with another pointer) before anything is
> > stored to it.
> 
> No, it also aids optimization because GCC can infer lack of aliasing
> elsewhere, even if no pointer in the newly allocated memory is
> used-before-set.  Consider the contrived example am.c (which I've added as
> an attachment to this report).  It has two functions f and g that differ
> only in that f calls m which has __attribute__ ((malloc)) whereas g calls n
> which does not.  With the weaker assumption you're suggesting, GCC could not
> optimize away the reload from a->next in f, because of the intervening
> assignment '*p = q'.

Actually, GCC and Clang both eliminate the reload of a->next in f (and not in
g). The weaker assumption is sufficient for that. *p can't alias a or b without
violating the weaker assumption.

What GCC is additionally doing in f is deleting the stores to a->next and
b->next as dead stores. That's really clever. However, the weaker assumption is
actually sufficient for that too: First, forward b to eliminate the load of
a->next. Then, it can be proved that a doesn't escape, and is defined by an
attribute malloc function, so the stores through it can't be loaded anywhere
else, so they're dead. Then, it can be proved that b doesn't escape either, and
is also defined by an attribute malloc function, so the stores through it are
dead too.

Consequently, the weaker assumption is still fairly strong. Further, the weaker
assumption would be usable by a much broader set of functions, so it may even
provide overall stronger alias information in practice.

Reply via email to