[PATCH] C undefined behavior fix
Linus Torvalds
torvalds@transmeta.com
Mon Jan 7 15:14:00 GMT 2002
On Mon, 7 Jan 2002, mike stump wrote:
>
> Any expansive text on volatile will cover it. We can see some of the
> bits of it from a C c99 working draft:
Note that if we start using "volatile", then gcc really _has_ to improve
its code generation here. Quite frankly, as it is, "volatile" absolutely
_sucks_ code-generation-wise.
Just as an example, look at your two "hide()" functions:
#define hide(x) ({ void *vp = x; asm ("" : "+r" (vp)); vp; })
inline void *hide(void *vp) { void *volatile v; v = vp; return v; }
I already sent out an example of why the "asm" thing generates suboptimal
code, because it forgets the value and is unable to use the knowledge.
However, with "volatile", the code generated _really_ sucks.
Here's the code for the "asm" version of ((*(int *)(0x10000+hide(i)):
movl $i, %eax
movl 65536(%eax), %eax
and here's the code that gcc generates from the "volatile" inline
function:
movl $i, %eax
movl %eax, (%esp)
movl (%esp), %eax
movl 65536(%eax), %eax
which is just not acceptable for any real use.
Linus
More information about the Gcc
mailing list