[Bug c/41919] New: optimized code with -O2 or -O3 gives strange result

chenyang at cs dot utah dot edu gcc-bugzilla@gcc.gnu.org
Tue Nov 3 05:09:00 GMT 2009


Using r153799, got strange result with the code below. The expected result is
-104. 

yang@yang-working:~$ svngcc -O3 -o small3 small.c
yang@yang-working:~$ ./small3
2
yang@yang-working:~$ svngcc -O2 -o small2 small.c
yang@yang-working:~$ ./small2
2
yang@yang-working:~$ svngcc -O1 -o small1 small.c
yang@yang-working:~$ ./small1
-104
yang@yang-working:~$ cat small.c
#include <stdio.h>
#include <assert.h>

struct S1
{
  char f0;
};

int g_23 = 0;

static struct S1 foo (void)
{
  int *l_100 = &g_23;
  int **l_110 = &l_100;
  struct S1 l_128 = {
    1
  };
  assert (l_100 == &g_23);
  assert (l_100 == &g_23);
  assert (l_100 == &g_23);
  assert (l_100 == &g_23);
  assert (l_100 == &g_23);
  assert (l_100 == &g_23);
  assert (l_100 == &g_23);
  return l_128;
}

static char bar(char si1, char si2)
{
  return (si1 <= 0) ? si1 : (si2 * 2);
}

int main (void)
{
  struct S1 s = foo();
  printf("%d\n", bar(0x99 ^ (s.f0 && 1), 1));
  return 0;
}

yang@yang-working:~$ svngcc -v
Using built-in specs.
COLLECT_GCC=svngcc
COLLECT_LTO_WRAPPER=/home/yang/compilers/libexec/gcc/i686-pc-linux-gnu/4.5.0/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ../configure --enable-lto --prefix=/home/yang/compilers
--program-prefix=svn --enable-languages=c,c++
--with-libelf=/home/yang/compilers : (reconfigured) ../configure --enable-lto
--prefix=/home/yang/compilers --program-prefix=svn
--with-libelf=/home/yang/compilers --enable-languages=c,lto,c++ --no-create
--no-recursion : (reconfigured) ../configure --enable-lto
--prefix=/home/yang/compilers --program-prefix=svn
--with-libelf=/home/yang/compilers --enable-languages=c,c++,lto --no-create
--no-recursion
Thread model: posix
gcc version 4.5.0 20091102 (experimental) (GCC)


-- 
           Summary: optimized code with -O2 or -O3 gives strange result
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: chenyang at cs dot utah dot edu
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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



More information about the Gcc-bugs mailing list