https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66673
Bug ID: 66673 Summary: swapping variables via chained xor fails Product: gcc Version: 4.4.6 Status: UNCONFIRMED Severity: major Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: joe.carnuccio at qlogic dot com Target Milestone: --- This is the same as 39121 which has been marked RESOLVED INVALID (to which I strongly disagree): this produces incorrect executable: *p ^= *q ^= *p ^= *q; ( if gcc option "-Os" is used, then that produces correct executable ) ( by contrast, this always produces correct executable: a ^= b ^= a ^= b; ) root@elab305:/home/joe/test/c# cat x.c #include <stdio.h> int main(int argc, char **argv) { int a = 0x32, b = 0x45; int *p = &a, *q = &b; *p ^= *q ^= *p ^= *q; printf("%x %x\n", a, b); return 0; } root@elab305:/home/joe/test/c# make -B x cc -c -o x.o x.c cc x.o -o x root@elab305:/home/joe/test/c# ./x 0 32 <--INCORRECT root@elab305:/home/joe/test/c# make -B x CFLAGS+='-Os' cc -Os -c -o x.o x.c cc x.o -o x root@elab305:/home/joe/test/c# ./x 45 32 <--CORRECT root@elab305:/home/joe/test/c# Notice that when -Os (optimize for space rather than speed) is used, the executable produces the correct result. Also, doing the chained xor on the integer variables a and b themselves always produces the correct result (regardless of optimization). root@elab305:/home/joe/test/c# gcc --version gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3) . . . root@elab305:/home/joe/test/c# uname -a Linux elab305 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux root@elab305:/home/joe/test/c# cat /etc/issue Red Hat Enterprise Linux Server release 6.2 (Santiago) Kernel \r on an \m