strict aliasing benefit examples
Albert Cahalan
acahalan@gmail.com
Wed Nov 29 18:10:00 GMT 2006
On 11/29/06, Paolo Bonzini <paolo.bonzini@lu.unisi.ch> wrote:
>
> >> int f(int *a, float *b)
> >> {
> >> *a = 1;
> >> *b = 2.0;
> >> return *a == 2;
> >> }
> >>
> >
> > Problem: people don't write code that way. (well I hope not)
> > People declare a few local variables, load them with data via
> > the pointers, do stuff with the local variables, then save back
> > the results via the pointers.
> >
> > So that won't convince many Visual Studio 2005 fans. :-(
>
> Then, the answer is that GCC's stronger aliasing allows you to use one
> line of code instead of three.
>
> Consider that most people can only write at most ~50 SLOC/day (including
> debugging and documentation), with a stunning independence from the
> programming language and programming style. If you take it with the
> necessary grain of salt, this is quite an argument.
It's an argument to favor K+R style over GNU, Allman, and Whitesmith.
This could be holding back gcc development. :-)
Since humans have to do a bit of alias analysis when maintaining
or writing code, the extra clarity of pulling things into temporary
variables isn't wasted.
I guess I can imagine that macro expansion might result in some
cases where strict-aliasing is of benefit. Most people fail to use a
temporary in a macro, probably because __typeof__ is gcc-only.
I can probably fit 20 lines of code on a readable slide. Ideas?
BTW, there are more normal programming habits that defeat
type-based alias analysis. People pick data types by habit.
Mostly, people will use the same type for nearly everything.
More information about the Gcc
mailing list