Aliasing and forwarding optimization

----Snip–
struct st1{
int a;
};
struct st2{
int b;
};
struct st {
struct st1 obj1;
struct st2 obj2;
}Obj;

int test1(struct st1 * ptr1 , struct st2 * ptr2, struct st2 *ptr3) {
ptr1->a = 10;
*ptr3 = *ptr2;
return ptr1->a;
}
–Snip—

For the above case GCC is able to store forward the value 10 to the return place.
LLVM is not doing this.
GCC
https://godbolt.org/z/FCjCXy
LLVM
https://godbolt.org/z/TFgnig

My understanding is that under strict aliasing rules accessing objects of different types don’t alias.
In this case we are accessing “struct st2” object and “int” type object. so aliasing should not prevent the forwarding of store 10 to the return place .

Can someone please clarify this?

regards,
Venkat.

:

If you see the LLVM IR output, the second line is lowered into a load/store of an int. Maybe that’s why.

https://godbolt.org/z/u3VyAM

I’m not sure what the strict aliasing rules say about this, though.

HTH

Hi Hiroshi,

If you see the LLVM IR output, the second line is lowered into a load/store of an int. Maybe that’s why.

%7 = load i32, i32* %5, align 4, !dbg !39, !tbaa !40
store i32 %7, i32* %6, align 4, !dbg !39, !tbaa !40

yes the structure copy seem to happen as integer type load and store. Sometimes I also see structure type bitcasted to integer pointer type and then load/store happens in integer type.

Alias is conservatively saying may-alias?

regards,
venkat.