public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/26867]  New: wrong optimization (-O2), wrong instruction reordering.
@ 2006-03-25 11:46 renzo at cs dot unibo dot it
  2006-03-25 16:14 ` [Bug c/26867] " rguenth at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: renzo at cs dot unibo dot it @ 2006-03-25 11:46 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c/26867] wrong optimization (-O2), wrong instruction reordering.
  2006-03-25 11:46 [Bug c/26867] New: wrong optimization (-O2), wrong instruction reordering renzo at cs dot unibo dot it
@ 2006-03-25 16:14 ` rguenth at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-03-25 16:14 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2006-03-25 16:14 -------
It's the most reported bug.  And it's invalid, you are violating C aliasing
rules.
Use -fno-strict-aliasing.

*** This bug has been marked as a duplicate of 21920 ***


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-03-25 16:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-25 11:46 [Bug c/26867] New: wrong optimization (-O2), wrong instruction reordering renzo at cs dot unibo dot it
2006-03-25 16:14 ` [Bug c/26867] " rguenth at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).