Type-punning

Silvius Rus rus@google.com
Tue Jun 26 22:36:00 GMT 2007


Herman Geza wrote:
> void foo(float *a) {
> 	int *b = (int*)a; // type-punning warning
>
> 	// here, access to a and b shouldn't be optimized, as the compiler 
> knows that a and b point to the same address
> }
>
> Is this reasonable?
>   
Even if it were trivial to implement, I would vote against it, because 
it would encourage people to write non-compliant code.

In terms of compilation time, it is not reasonable: the standard is 
clear about this so that compilers can optimize based on declared types, 
without having to perform overly complex (and expensive) alias analysis.

For the corner case where you have non-compliant code that breaks with 
-fstrict-aliasing and is much slower with -fno-strict-aliasing, and 
which you cannot modify, I think you are on your own.  You could in 
principle write a pass that flags all the possible references (including 
possible aliases) to potentially type-punned addresses.  Then you would 
have to make sure that this information is understood by the relevant 
optimization passes, and that it is preserved across different 
representations, i.e., from GIMPLE to RTL.  All this work just to fight 
the standard :).




More information about the Gcc mailing list