From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3230 invoked by alias); 29 Jul 2002 01:16:00 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 3216 invoked by uid 71); 29 Jul 2002 01:16:00 -0000 Date: Sun, 28 Jul 2002 18:16:00 -0000 Message-ID: <20020729011600.3209.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: Makoto Fujiwara Subject: Re: optimization/7427: gcc-3.1.1 -O2 problem for checksum calculation (powerpc) Reply-To: Makoto Fujiwara X-SW-Source: 2002-07/txt/msg00752.txt.bz2 List-Id: The following reply was made to PR optimization/7427; it has been noted by GNATS. From: Makoto Fujiwara To: Andrew Pinski Cc: gcc-gnats@gcc.gnu.org Subject: Re: optimization/7427: gcc-3.1.1 -O2 problem for checksum calculation (powerpc) Date: Mon, 29 Jul 2002 10:14:59 +0900 Andrew Pinski> Not a bug in gcc, but a bug in your code because of C aliasing rules. Andrew Pinski> > unsigned short *w = (void *)&tmp; Thanks Andrew, for super quick response. (1) 'gcc -Wall' gave nothing on the line 12. Is this still right ? (2) I have changed the code little bit and .. I have modified (a) and modifed (b) for further evaluation. (a) got printf in between while (b) does not. Code (a) with printf gave me the right sum even with -O2, but (b) gave the wrong sum. Is this right behavior of optimization ? (a) low = 0x181 hi = 0x8e84 sum = 0x68ac (b) sum = 0x8fb9 ------ (a) ------ #include struct buf { int data; }; bug(m) struct buf *m; { int sum = 0; struct buf tmp; unsigned short *w = (void *) &tmp; unsigned short low,hi; bzero(&tmp, sizeof tmp); tmp.data = m->data; //asm volatile ("" ::: "memory"); low = w[0]; hi = w[1]; printf("low = 0x%x\n", low); printf("hi = 0x%x\n", hi); sum += w[0]; sum += w[1]; printf("sum = 0x%x\n", sum); return 0; } main() { struct buf m; m.data = 0x12345678; bug(&m); } ----- (b) ------- #include struct buf { int data; }; bug(m) struct buf *m; { int sum = 0; struct buf tmp; unsigned short *w = (void *) &tmp; unsigned short low,hi; bzero(&tmp, sizeof tmp); tmp.data = m->data; //asm volatile ("" ::: "memory"); low = w[0]; hi = w[1]; // printf("low = 0x%x\n", low); // printf("hi = 0x%x\n", hi); sum += w[0]; sum += w[1]; printf("sum = 0x%x\n", sum); return 0; } main() { struct buf m; m.data = 0x12345678; bug(&m); } ------------------ --- Makoto Fujiwara, Chiba, Japan, Narita Airport and Disneyland prefecture.