Re: About GCC4 Optimization

From: Linus Torvalds
Date: Sun Mar 25 2007 - 00:28:29 EST




On Sun, 25 Mar 2007, yuan cooper wrote:
> ?> during my work, I found?here is a bug with GCC4 O2 optimization.

Technically, it's a misfeature fo gcc4, not a bug.

The C language allows for type-based alias detection, and gcc notices that
a "float *" cannot ever alias with a "unsigned long *", so it decides to
not even do the loads and stores..

Now, there's two things wrong with this picture:

- gcc is being an ass. type-based alias detection should happen only as a
last resort, and gcc should know and notice that *despite* the types
being different, they definitely alias.

So what gcc does may be technically legal, but it's still a horribly
bad thing to do. Sadly, some gcc people seem to care more about "letter
of the law" than "sanity and quality of implementation".

- as a result, you should always compile any kernel stuff with
"-fno-strict-aliasing", which should turn this off. If it *still*
happens with that flag, then it is indeed a compiler bug.

> float ftmp;
> unsigned long tmp;
> ftmp = 1.0/1024.0;
> tmp?= *(unsigned long *)(&ftmp);
> tmp?= (tmp >> 11) && 0xFFF;
> ?> if optimization level is O2, gcc will MOV eax to tmp, but current eax has a random value.
> -O is ok and gcc3 with O2 is ok too.

That said, you really _really_ shouldn't be doing FP in the kernel anyway.

Linus