strict aliasing question

Howard Chu hyc@highlandsun.com
Sat Nov 11 00:59:00 GMT 2006


Joe Buck wrote:
> On Fri, Nov 10, 2006 at 04:18:25PM -0800, Howard Chu wrote:
>   
>> Richard Guenther wrote:
>>     
>>> If you compile with -O3 -combine *.c -o alias it will break.
>>>       
>> Hm, actually it still prints the correct result for me. What platform 
>> are you using where it actually makes a difference?
>>     
>
> Rather, he is saying that, with those flags, it is possible that gcc
> will do optimizations that breaks the code, but these optimizations
> might show up only on some platforms, with some releases, under
> some conditions.  You might luck out, but it is possible that a future
> gcc will do an optimization that changes the meaning of the code.
>   

OK, that's fair.
> The compiler is allowed to reason roughly as follows: "I have a copy of
> foo in register R1.  foo is of type long.  There have been no writes,
> since foo was loaded into R1, for any types compatible with type long.
> Therefore the copy of foo in R1 is still good, so I don't have to reload
> it from memory."  The C standard has rules of this form because, without
> them, it can be hard to do decent loop optimization.  The reason Richard
> mentioned the options he did was that they enable some optimization across
> function boundaries, meaning that the compiler is more likely to see that
> there can't have been any legal modifications to some objects.
>   
I understand that logic, in the general case. In this specific example, 
none of those conditions apply. foo is an uninitialized local variable. 
Therefore the compiler cannot know that it has a valid copy of it in any 
register. In fact what it should know is that it has no valid copy of 
it. And of course, there are no loops to consider here, so that type of 
reload optimization isn't relevant. As such, the compiler has no choice 
but to do the right thing, and load the value from memory, thus getting 
the correct result. Which it does.

-- 
  -- Howard Chu
  Chief Architect, Symas Corp.  http://www.symas.com
  Director, Highland Sun        http://highlandsun.com/hyc
  OpenLDAP Core Team            http://www.openldap.org/project/



More information about the Gcc mailing list