This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Linux and aliasing?


> It seems that other people use more casts for "normal" things, and are
> actually afraid of my proposal for performance reasons. I'm surprised:
> people that do things like that are usually not the people who complain
> about others coding standards ;)

Well, no. The 'normal' kind of cast is very common, and frequently
used in the Linux kernel. For example, if a tty driver routine is
called (e.g. drivers/char/rocket.c :-), it fetches driver_data and
casts it to the device-specific type (i.e. (struct r_port *)).

In these cases, people typically save the cast result in a variable
instead of derefencing it, so they would not suffer from your
anti-aliasing mechanism. These uses of casts are conforming C code:
The driver put an r_port pointer into driver_data earlier on.

> Anyway, I grepped the kernel for "likely" places where my change would
> make a difference by using the following heuristic grep:
> 
> 	grep '\*(.*\* *)' */*.c
> 
> and in basically all cases the compiler would have done the right thing if
> it had followed my proposal.

It is actually the other casts that the Linux contributors need to
worry about. Alias problems are very hard to find (as you pointed
out), and somebody will have to go over the complete kernel source and
investigate every single cast - if you ever plan to turn-on
-fstrict-aliasing.

I did the inverse grep

       grep '(.*\* *)' */*.c | grep -v '\*(.*\* *)' */*.c

and found only one place (fs/binfmt_aout.c:create_aout_tables) where
pointers are aliased in different types, and dereferenced later. The
hidden treasures are probably in the header files (as earlier examples
indicate).

Regards,
Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]