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

--- Comment #3 from Benjamin B. Meier <benjamin.meier70 at gmail dot com> ---
> The reason for the difference is because in the case where there is an int in 
> the structure, the alignment is 4 bytes so a 2 or 4 byte read for a and b 
> will not cause an fault while in the first case where there is no int, the 
> alignment is 1 and might cause a fault. Gcc is conservative here on doing the 
> optimization.

Okay, got it. The alignment actually makes a difference here. Thanks, for the
explanation.

> Also || is a short cutting or in c so I don't think this optimization can be 
> done for the original case in a well defined way.

I see that as well, however reading the right hand side expression of the
||-operator has in this case no observable side-effect. Plus if we know that
s->a can be read, we know as well that s cannot be a NULL pointer. Given that,
I would assume the optimization should be valid (and applied).

Another supporting argument for ignoring the short cutting behavior is the
version of "MyStruct" with "int x;": In this case the compiler also always
reads both operands (and therefore ignores the short cut).

So if the compiler uses a nice optimization without jumps if it knows the
alignment is at least 2 bytes, it probably could also use an optimization for
the case that the alignment is only 1 byte. A possible optimization would then
look like the code of f2.

Finally, it's nothing really important, but I still do not yet completely see
why optimizing the jump away in f is an issue:)

Reply via email to