public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc-head bug? feature?
@ 2005-10-02  0:21 Bernhard Fischer
  0 siblings, 0 replies; 2+ messages in thread
From: Bernhard Fischer @ 2005-10-02  0:21 UTC (permalink / raw)
  To: gcc-help; +Cc: Bernhard Fischer

[please keep me CC'd]
Hi,

Is this a user error (thus an optimisation feature) or a glitch in gcc?

$ for o in '' -O2; do echo "# o='$o'";gcc-4.1-HEAD -W -Wall -Wextra
-pedantic $o -g -Wall -I. -o bar41.$o foo.c ;./bar41.$o;done
# o=''
var1=1020308
var1=1020809
# o='-O2'
foo.c: In function 'main':
foo.c:7: warning: dereferencing type-punned pointer will break
strict-aliasing rules
var1=1020308
var1=1020308

with O2, the second assignment is "optimized out"?

gcc-4.0.1, gcc-3.4, gcc-3.3, gcc-2.95 all yield 1020308 and 1020809 for
Odefault and O2, as opposed to gcc-4.1.

$ cat foo.c 
#include <stdio.h>
int main(void)
{
	int var1 = 0x01020304;
	*(char *)&var1 = 0x08;
	printf("var1=%x\n", var1);
	*(short *)&var1 = 0x0809;
	printf("var1=%x\n", var1);
	return 0;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: gcc-head bug? feature?
@ 2005-10-02  1:03 Ryan Mansfield
  0 siblings, 0 replies; 2+ messages in thread
From: Ryan Mansfield @ 2005-10-02  1:03 UTC (permalink / raw)
  To: 'Bernhard Fischer', gcc-help


This is a problem with your code. You are violating aliasing rules as the
warning message states. See 6.5/7 of the C Standard for the exact rules.

Problems arising from alias violations happen to be the #1 invalid PR filed
in Bugzilla. I'd add the link but Bugzilla is currently being upgraded.

This problem is also mentioned under "Casting does not work as expected when
optimization is turned on. " in the non-bugs section of the bugs page:

http://gcc.gnu.org/bugs.html#known

With gcc 4.0, -Wstrict-aliasing=2 was added to catch more of these problems,
but does not catch all cases.

Regards,

Ryan Mansfield


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Bernhard Fischer
Sent: Saturday, October 01, 2005 7:56 PM
To: gcc-help@gcc.gnu.org
Cc: Bernhard Fischer
Subject: gcc-head bug? feature?

[please keep me CC'd]
Hi,

Is this a user error (thus an optimisation feature) or a glitch in gcc?

$ for o in '' -O2; do echo "# o='$o'";gcc-4.1-HEAD -W -Wall -Wextra
-pedantic $o -g -Wall -I. -o bar41.$o foo.c ;./bar41.$o;done # o=''
var1=1020308
var1=1020809
# o='-O2'
foo.c: In function 'main':
foo.c:7: warning: dereferencing type-punned pointer will break
strict-aliasing rules
var1=1020308
var1=1020308

with O2, the second assignment is "optimized out"?

gcc-4.0.1, gcc-3.4, gcc-3.3, gcc-2.95 all yield 1020308 and 1020809 for
Odefault and O2, as opposed to gcc-4.1.

$ cat foo.c
#include <stdio.h>
int main(void)
{
	int var1 = 0x01020304;
	*(char *)&var1 = 0x08;
	printf("var1=%x\n", var1);
	*(short *)&var1 = 0x0809;
	printf("var1=%x\n", var1);
	return 0;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-10-02  1:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-02  0:21 gcc-head bug? feature? Bernhard Fischer
2005-10-02  1:03 Ryan Mansfield

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).