libalias patch for review / testing
John Polstra
jdp at polstra.com
Thu Mar 18 08:49:03 PST 2004
On 16-Mar-2004 Dag-Erling Smørgrav wrote:
> Ruslan Ermilov <ru at FreeBSD.org> writes:
>> I know this code quite well. Where do you suspect could be a bug
>> affecting -O2 compiles, or you just simply fixed -O2 and hope it
>> will auto-fix the (possible) bugs in -O2?
>
> Since there is no inline asm, the most likely suspect is aliasing,
> which is what my patch tries to address.
To eliminate aliasing problems reliably, I think you're going to
have to use a union. That's the only kind of type punning that gcc
promises to allow even at high optimization levels. From the info
pages (in the description of "-fstrict-aliasing"):
Pay special attention to code like this:
union a_union {
int i;
double d;
};
int f() {
a_union t;
t.d = 3.0;
return t.i;
}
The practice of reading from a different union member than the one
most recently written to (called "type-punning") is common. Even
with `-fstrict-aliasing', type-punning is allowed, provided the
memory is accessed through the union type. So, the code above
will work as expected. However, this code might not:
int f() {
a_union t;
int* ip;
t.d = 3.0;
ip = &t.i;
return *ip;
}
Even if you use a union, it won't necessarily work with compilers
other than gcc.
John
More information about the freebsd-net
mailing list