Subject: Re: gcc optimizer bug in netbsd-1-6 on alpha (gcc 2.95.3 20010315 (release) (NetBSD nb3))
To: Martin Husemann <martin@duskware.de>
From: David Laight <david@l8s.co.uk>
List: tech-toolchain
Date: 08/15/2003 15:37:28
On Fri, Aug 15, 2003 at 08:27:56AM +0200, Martin Husemann wrote:
> On Thu, Aug 14, 2003 at 02:40:22PM -0400, Greg A. Woods wrote:
> 
> > 		unsigned long inet = 0;		/* an IPv4 address */
> > 	
> > 		printf("parse_address(): inet addr given: [%s]\n",
> > 		       inet_ntoa(*((struct in_addr *) &inet)));

> Example (on sparc64, gcc 3.3.1, with inet = 0 replaced by 
> inet = 0x0102030405060708):
> 
> Compiled with -O (does not include -fstrict-alias):
> parse_address(): inet addr given: [1.2.3.4]
> 
> Compiled with -O2:
> parse_address(): inet addr given: [1.0.0.0]
> 
> This is *not* a compiler bug. According to the C standard you are invoking
> undefined behaviour.

I suspexcct what is hapening is that specifying '&inet' isn't enough to
cause the assignment 'inet = 0x0102030405060708;' to happen.
I'd say this is a bug! regardless of the presence of the cast.

The rule about strict-aliasing is there to alow compilers to track
values across assignments that use pointers, otherwise all tracked
values (except auto variables whose address is never taken) have to be
flushed and discarded every time a write is done via a pointer.
eg with:
	extern int a;
	char **p;
	...
	a = 1;
	*p = 0
	if (a) ...
the compiler must re-read 'a' because '*p' might reference 'a'.

gcc is goint OTT.

	David

-- 
David Laight: david@l8s.co.uk