From nobody@FreeBSD.org  Thu Mar 31 08:45:45 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id E02E0106564A
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 31 Mar 2011 08:45:45 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id CFD1B8FC18
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 31 Mar 2011 08:45:45 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p2V8jjg8048844
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 31 Mar 2011 08:45:45 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p2V8jj5u048843;
	Thu, 31 Mar 2011 08:45:45 GMT
	(envelope-from nobody)
Message-Id: <201103310845.p2V8jj5u048843@red.freebsd.org>
Date: Thu, 31 Mar 2011 08:45:45 GMT
From: Dmitry Afanasiev <KOT@MATPOCKuH.Ru>
To: freebsd-gnats-submit@FreeBSD.org
Subject: gcc makes incorrect code when swapping array elements by xor without optimization
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         156082
>Category:       gnu
>Synopsis:       gcc(1) makes incorrect code when swapping array elements by xor without optimization
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Mar 31 08:50:12 UTC 2011
>Closed-Date:    
>Last-Modified:  Mon Apr 04 06:26:38 UTC 2011
>Originator:     Dmitry Afanasiev
>Release:        FreeBSD 9.0-CURRENT i386
>Organization:
>Environment:
FreeBSD orion 9.0-CURRENT FreeBSD 9.0-CURRENT #6: Sun Mar 27 03:24:55 MSD 2011     root@orion:/usr/obj/usr/src/sys/orion  amd64

>Description:
Base gcc 4.2.1 makes incorrect code when swapping array elements by xor without optimization.
Simple .c code attached as xor.txt.
Checked on FreeBSD 9.0 on i386/amd64/sparc64 platforms.
>How-To-Repeat:
# gcc -o xor xor.txt
# gcc -o xoro1 -O1 xor.txt
# ./xor
a0 = 127 b0 = 33
a1 = 127 b1 = 33
a2 = 127 b2[0] = 33
a3 = 127 b3[0] = 0
# ./xoro1
a0 = 127 b0 = 33
a1 = 127 b1 = 33
a2 = 127 b2[0] = 33
a3 = 127 b3[0] = 33

But in theory output must be identical.
>Fix:


Patch attached with submission follows:

#include <stdio.h>
#include <sysexits.h>

int
main(int argc, char *argv[])
{
	int a0 = 33;
	int b0 = 127;
	int a1 = 33;
	int b1 = 127;
	int a2 = 33;
	int b2[] = {127};
	int a3 = 33;
	int b3[] = {127};

	a0 ^= b0 ^= a0 ^= b0;
	b1 ^= a1 ^= b1 ^= a1;
	a2 ^= b2[0] ^= a2 ^= b2[0];
	b3[0] ^= a3 ^= b3[0] ^= a3;

	printf("a0 = %i b0 = %i\n", a0, b0);
	printf("a1 = %i b1 = %i\n", a1, b1);
	printf("a2 = %i b2[0] = %i\n", a2, b2[0]);
	printf("a3 = %i b3[0] = %i\n", a3, b3[0]);

	return EX_OK;
}


>Release-Note:
>Audit-Trail:
>Unformatted:
