This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: aggregate alias anomaly
Jeff Sturm <jsturm@one-point.com> writes:
> Given:
>
> struct X {int x;};
> struct Y {int y;};
>
> int f(struct X *x, struct Y *y) {
> x->x = 0;
> y->y = 1;
> return x->x;
> }
>
> gcc -O2 -fomit-frame-pointer yields:
>
> f:
> movl 4(%esp), %eax
> movl 8(%esp), %edx
> movl $0, (%eax)
> movl $1, (%edx)
> movl (%eax), %eax
> ret
>
> for every released compiler I tried, plus mainline. Note the useless load
> of x->x, given that x and y cannot possibly have conflicting alias sets.
I am not a language lawyer (nor have any desire to be one), but that
is not necessarily correct for C. See DR257 (and also the "What is an
object DR").
On the other hand, even if you change struct X and Y so that they
don't have a common initial sequence:
struct X {char c; int x;};
struct Y {int y; short d;};
your function still won't be optimized....
I posted a patch some time ago that solved a problem
related to this (or the same?):
http://gcc.gnu.org/ml/gcc-patches/2002-03/msg00576.html
This version even has a java example:
http://gcc.gnu.org/ml/gcc-patches/2002-03/msg01869.html
If there's interest I could revive that patch.