[haiku-development] Re: Disabling Strict Aliasing for GCC4 Builds

  • From: "Michael Lotz" <mmlr@xxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Thu, 17 Apr 2008 23:37:48 +0200

Hi Stephan

> Yes, but I don't understand the problem with that particular code. I 
> mean 
> with regards to aliasing. (I have read the Wikipedia article.) The 
> pointer 
> there is a right hand value, thus there is no pointer that points to 
> the 
> same location in memory that is used by someDouble AFAICT. The 
> location is 
> only used to assign the same value to another location in memory, 
> which 
> stores the value of someInt. Is the problem here that there *is* a 
> second 
> pointer to the same location from the compilers perspective?

Yes bad example of course. In fact GCC4 will output the corresponding 
warning though. It is really as you say that there is a second pointer 
pointing to the same location at all.

Maybe let me extend the example:

{
        double someDouble = 0.0;
        someDouble += 5.0;
        int variable = *(int *)&someDouble;
        someDouble -= 3.0;
        return 0;
}

Through strict aliasing rules, it's now like the compiler is allowed to 
assume that something (someDouble in this case) cannot be referenced by 
any pointer as there are no double pointers around. It therefore could 
optimize the variable manipulation of someDouble. It would be allowed 
to put the value into registers and keep it there (not writing the 
changed value back to the memory location) until later on (which is 
quite possible in that example as the variable is used again later in 
the code and would profit from being still in the register). If this 
was the case, then you'd obviously not get the expected value back when 
reading out someDouble by casting it to an int and dereferencing it. 
You'd only get back what is stored at the memory location and not what 
the real value at this point in time (in the register) actually is. I 
don't really know if this would happen with the example above of 
course, one would have to look at the assembler output to check.

The second reference on the wikipedia article 
(http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html) 
gives some more examples on the topic.

Regards
Micheal

Other related posts: