[PATCH] fix aliasing for chars in global structures - take II
Dan Nicolaescu
dann@godzilla.ICS.UCI.EDU
Fri Mar 22 10:49:00 GMT 2002
Fergus Henderson <fjh@cs.mu.oz.au> writes:
> On 22-Mar-2002, Dan Nicolaescu <dann@godzilla.ICS.UCI.EDU> wrote:
> >
> > Currently references to a "char" member of a structure are put in
> > alias set zero, ie like they are a char*.
>
> Right. That matches what the standard specifies.
>
> Consider the following program:
>
> struct first { char f1; int x; };
> struct second { char f2; double y; };
>
> void
> g1 (struct first *ps1, struct second *ps2)
> {
> ps1->f1++;
> ps2->f2++;
> ps1->f1++;
> ps2->f2++;
> }
>
> int
> main (void)
> {
> void *p = calloc(sizeof(struct first) + sizeof(struct second), 1);
> g1 (p, p);
> printf("%d\n", ((struct first *)p)->f1);
> return 0;
> }
>
> Isn't that program strictly conforming C, which should output `4'?
It does.
Here is the generated SPARC assembly
g1:
!#PROLOGUE# 0
!#PROLOGUE# 1
ldub [%o0], %o3
add %o3, 1, %o3
stb %o3, [%o0]
ldub [%o1], %o2
add %o2, 1, %o2
stb %o2, [%o1]
ldub [%o0], %o3
add %o3, 1, %o3
stb %o3, [%o0]
ldub [%o1], %o0
add %o0, 1, %o0
retl
stb %o0, [%o1]
> And won't your patch mis-optimize it to output something
> different?
I don't think so. The the references to ps1->f1 and ps2->f2 will be in
the same alias set. Look at the "g3" example in my previous mail.
More information about the Gcc-patches
mailing list