This is the mail archive of the
gcc-prs@gcc.gnu.org
mailing list for the GCC project.
Re: optimization/7427: gcc-3.1.1 -O2 problem for checksum calculation (powerpc)
- From: Makoto Fujiwara <makoto at ki dot nu>
- To: nobody at gcc dot gnu dot org
- Cc: gcc-prs at gcc dot gnu dot org,
- Date: 29 Jul 2002 01:16:00 -0000
- Subject: Re: optimization/7427: gcc-3.1.1 -O2 problem for checksum calculation (powerpc)
- Reply-to: Makoto Fujiwara <makoto at ki dot nu>
The following reply was made to PR optimization/7427; it has been noted by GNATS.
From: Makoto Fujiwara <makoto@ki.nu>
To: Andrew Pinski <pinskia@physics.uc.edu>
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 <stdio.h>
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 <stdio.h>
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.