The program here enclosed has this behavior (tested on i386 and ppc, gcc
4.0.3):

$ gcc -O1 test.c
$ ./a.out 42
42
$ gcc -O2 test.c
$ ./a.out 42
0

on ppc (from debian sid) this chunk of the code:
25:  tmp=((struct a *)bp)->a2;
26:  bp->b1=0;
27:  bp->b2=0;
28:  bp->b3=0;
29:  bp->b5 = tmp;

gets translated in this (wrong) way (gcc -S -g -O2):

  .loc 1 22 0
  lha 29,0(4)
  .loc 1 26 0
  stw 0,0(3)           <--  b1 is zeroed line 26
  .loc 1 27 0
  stw 0,4(3)
  .loc 1 17 0
  mr 31,4
  .loc 1 25 0
  lha 11,2(3)          <-- line 25, reads a2 which is part of b1!
.LVL5:
  .loc 1 22 0
  lha 9,2(4)
  .loc 1 28 0
  stb 0,8(3)
.LVL6:
  .loc 1 29 0
  sth 11,10(3)         <-- store R11 into b5

I have tried to read the open bugs and it seems to me that this bug has not
been
submitted yet. If it has been already filed, I apologize in advance.

         renzo
----------
$ gcc -v
Using built-in specs.
Target: powerpc-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.0 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-java-awt=gtk-default --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre --enable-mpfr
--disable-softfloat --enable-targets=powerpc-linux,powerpc64-linux
--with-cpu=default32 --disable-werror --enable-checking=release
powerpc-linux-gnu
Thread model: posix
gcc version 4.0.3 (Debian 4.0.3-1)

The C test source is:
-------------------------
#include <stdio.h>

struct a {
  short a1;
  short a2;
};

struct b {
  int b1;
  int b2;
  char b3;
  char b4;
  short b5;
};

void f(struct b *bp, void *m)
{
  int tmp;
  struct a *ap;
  struct a savea;
  ap=(struct a *)m;
  savea = *ap;
  savea.a2 += 4;

  tmp=((struct a *)bp)->a2;
  bp->b1=0;
  bp->b2=0;
  bp->b3=0;
  bp->b5 = tmp;

  if (fc()) {
    *ap = savea;
  }
}

int fc()
{
  return (time() % 2);
}

main(int argc,char *argv[])
{
  struct b myb;
  struct a mya;
  mya.a2=0;
  ((struct a *)(&myb))->a2 = atoi(argv[1]);
  f(&myb,&mya);
  printf("%d\n",myb.b5);
}


-- 
           Summary: wrong optimization (-O2), wrong instruction reordering.
           Product: gcc
           Version: 4.0.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: renzo at cs dot unibo dot it


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26867

Reply via email to