[Bug tree-optimization/67618] malloc+memset optimization breaks code
daniel.gutson at tallertechnologies dot com
gcc-bugzilla@gcc.gnu.org
Thu Sep 17 20:28:00 GMT 2015
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67618
--- Comment #9 from Daniel Gutson <daniel.gutson at tallertechnologies dot com> ---
(In reply to Marc Glisse from comment #8)
> (bugzilla bug that reset the component...)
>
> (In reply to Daniel Gutson from comment #6)
> > That's why the 'if (ptr != NULL)' should not be ignored, which currently is.
> > The 'if' prevents the UB.
>
> Uh, if you consider it UB, I don't understand the problem. At runtime,
> either malloc succeeded and the transformation is fine, or x<=12 and the
> transformation is fine, or the call to memset is undefined behavior so
> anything is fine (including the transformation). Unless you explicitly want
> to catch the trap, I don't understand what you are saying. Could you detail
> step by step where a well-defined behavior in the original program is turned
> into a different behavior in the optimization?
See this example: ('function' is same as above)
void caller(void)
{
void* ptr = function(1);
*(char*)ptr = 1;
}
In this case, calloc was called instead of (only) malloc because the 'if' was
ignored, resulting in a suboptimized code (since calloc is usually slower than
malloc alone).
The resulting steps are:
calloc(1)
*ptr = 1;
whereas I just wanted
malloc(1)
*ptr = 1;
IMHO, the optimization should take the 'if' into account and only apply if it
is the usual 'if (ptr != NULL)' pattern.
(Additionally, it should check that the context caller function is not 'calloc'
itself).
More information about the Gcc-bugs
mailing list