public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/56407] New: Optimizations (-O2 -O3) make comparison of arrays of ints to fail
@ 2013-02-20 11:53 quintin at lix dot polytechnique.fr
  2013-02-20 13:11 ` [Bug c/56407] [4.7 Regression] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: quintin at lix dot polytechnique.fr @ 2013-02-20 11:53 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 56407
           Summary: Optimizations (-O2 -O3) make comparison of arrays of
                    ints to fail
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: quintin@lix.polytechnique.fr


Created attachment 29509
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29509
The .c .i .s .o files

This is my first bug report, so I hope this is really a bug and that I file it
correctly.

Versions of my stuff:
- gcc (Debian 4.7.2-5) 4.7.2
- Linux 3.2.0-4-amd64 #1 SMP Debian 3.2.35-2 x86_64 GNU/Linux
- Intel(R) Core(TM)2 Duo CPU T9600 @ 2.80GHz

gcc compile stuff:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5'
--with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs
--enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.7 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object
--enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5) 

The bug appears when I compile the program with -O2 or -O3:
$ gcc -O3 gcc_bug.c -o gcc_bug
$ ./gcc_bug && echo ok || echo fail
fail

But it works fine with -O1:
$ gcc -O1 gcc_bug.c -o gcc_bug
$ ./gcc_bug && echo ok || echo fail
ok

It works fine with an older version of gcc:
$ gcc --version
gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)
$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-gnu-unique-object
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib
--with-ppl --with-cloog --with-tune=generic --with-arch_32=i686
--build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)
$ gcc -O3 gcc_bug.c -o gcc_bug
$ ./gcc_bug && echo ok || echo fail
ok

When the program fails with gcc 4.7.2 valgrind tells me:
$ gcc -O3 -g gcc_bug.c -o gcc_bug
$ valgrind ./gcc_bug
==3383== Memcheck, a memory error detector
==3383== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3383== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3383== Command: ./gcc_bug
==3383== 
==3383== Conditional jump or move depends on uninitialised value(s)
==3383==    at 0x40079E: test (gcc_bug.c:21)
==3383==    by 0x400482: main (gcc_bug.c:41)
==3383== 
==3383== 
==3383== HEAP SUMMARY:
==3383==     in use at exit: 0 bytes in 0 blocks
==3383==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3383== 
==3383== All heap blocks were freed -- no leaks are possible
==3383== 
==3383== For counts of detected and suppressed errors, rerun with: -v
==3383== Use --track-origins=yes to see where uninitialised values come from
==3383== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 4 from 4)

Whereas all is fine for valgrind with -O1. Also all is fine with -fwrapv:
$ gcc -O3 -fwrapv gcc_bug.c -o gcc_bug
$ ./gcc_bug && echo ok || echo fail
ok

I suppose the bug appears when gcc is doing some optimizations but I do not
know which. Any small modifications to the program make it work. For example if
I remove the ``if ( comm ) {" line or if I replace ``for( j = 1 ; j <= sz" by
``for( j = 1 ; j < sz".

======================================
extern void exit(int);
extern int rand(void);

void copy(int *r,int *a,int na) {
  int i;
  for( i = 0 ; i < na ; i++ ) {
    r[i] = a[i];
  }
}

void random(int *a,int na) {
  int i;
  for( i = 0 ; i < na ; i++ ) {
    a[i] = rand();
  }
}

int cmp(int *a,int *b,int n) {
  int i;
  for( i = 0 ; i < n ; i++ ) {
    if ( a[i] != b[i] ) return -1;
  }
  return 0;
}

void test(int sz,int comm) {
  int j,n;
  int v[64],w[64];
  for( j = 1 ; j <= sz ; j++ ) {
    n = (2 * j - 1) * (2 * j - 1);
    random(w,n);
    copy(v,w,n);
    if ( comm ) {
      if ( cmp(v,w,n) ) exit(-1);
    }
  }
  exit(0);
}

int main(void) {
  test(2,1);
  return 0;
}
======================================


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

* [Bug c/56407] [4.7 Regression] Optimizations (-O2 -O3) make comparison of arrays of ints to fail
  2013-02-20 11:53 [Bug c/56407] New: Optimizations (-O2 -O3) make comparison of arrays of ints to fail quintin at lix dot polytechnique.fr
@ 2013-02-20 13:11 ` jakub at gcc dot gnu.org
  2013-04-03  9:53 ` [Bug tree-optimization/56407] " rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-20 13:11 UTC (permalink / raw)
  To: gcc-bugs


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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2013-02-20
                 CC|                            |jakub at gcc dot gnu.org
   Target Milestone|---                         |4.7.3
            Summary|Optimizations (-O2 -O3)     |[4.7 Regression]
                   |make comparison of arrays   |Optimizations (-O2 -O3)
                   |of ints to fail             |make comparison of arrays
                   |                            |of ints to fail
     Ever Confirmed|0                           |1

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-20 13:10:50 UTC ---
Started around http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173853
and got fixed (or made latent) with
http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=186566
Seems -fno-ivopts fixes this.


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

* [Bug tree-optimization/56407] [4.7 Regression] Optimizations (-O2 -O3) make comparison of arrays of ints to fail
  2013-02-20 11:53 [Bug c/56407] New: Optimizations (-O2 -O3) make comparison of arrays of ints to fail quintin at lix dot polytechnique.fr
  2013-02-20 13:11 ` [Bug c/56407] [4.7 Regression] " jakub at gcc dot gnu.org
@ 2013-04-03  9:53 ` rguenth at gcc dot gnu.org
  2013-04-03 10:23 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-04-03  9:53 UTC (permalink / raw)
  To: gcc-bugs


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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2
          Component|c                           |tree-optimization
      Known to work|                            |4.8.0


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

* [Bug tree-optimization/56407] [4.7 Regression] Optimizations (-O2 -O3) make comparison of arrays of ints to fail
  2013-02-20 11:53 [Bug c/56407] New: Optimizations (-O2 -O3) make comparison of arrays of ints to fail quintin at lix dot polytechnique.fr
  2013-02-20 13:11 ` [Bug c/56407] [4.7 Regression] " jakub at gcc dot gnu.org
  2013-04-03  9:53 ` [Bug tree-optimization/56407] " rguenth at gcc dot gnu.org
@ 2013-04-03 10:23 ` rguenth at gcc dot gnu.org
  2013-04-03 10:26 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-04-03 10:23 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> 2013-04-03 10:22:53 UTC ---
Testcase, also fails at -O2:

/* { dg-do run } */

extern void abort(void);
extern int rand(void);

static void copy(int *r,int *a,int na)
{
  int i;
  for( i = 0 ; i < na ; i++ )
    r[i] = a[i];
}

static void foo(int *a,int na)
{
  int i;
  for( i = 0 ; i < na ; i++ )
    a[i] = rand();
}

static int cmp(int *a,int *b,int n)
{
  int i;
  for( i = 0 ; i < n ; i++ )
    if ( a[i] != b[i] )
      return -1;
  return 0;
}

void __attribute__((noinline,noclone))
test(int sz,int comm)
{
  int j,n;
  int v[64],w[64];
  for( j = 1 ; j <= sz ; j++ )
    {
      n = (2 * j - 1) * (2 * j - 1);
      foo(w,n);
      copy(v,w,n);
      if ( comm )
        if ( cmp(v,w,n) ) abort ();
    }
}

int main()
{
  test(2,1);
  return 0;
}


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

* [Bug tree-optimization/56407] [4.7 Regression] Optimizations (-O2 -O3) make comparison of arrays of ints to fail
  2013-02-20 11:53 [Bug c/56407] New: Optimizations (-O2 -O3) make comparison of arrays of ints to fail quintin at lix dot polytechnique.fr
                   ` (2 preceding siblings ...)
  2013-04-03 10:23 ` rguenth at gcc dot gnu.org
@ 2013-04-03 10:26 ` rguenth at gcc dot gnu.org
  2013-04-11  7:59 ` rguenth at gcc dot gnu.org
  2014-06-12 13:24 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-04-03 10:26 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> 2013-04-03 10:26:53 UTC ---
Author: rguenth
Date: Wed Apr  3 10:25:23 2013
New Revision: 197399

URL: http://gcc.gnu.org/viewcvs?rev=197399&root=gcc&view=rev
Log:
2013-04-03  Richard Biener  <rguenther@suse.de>

    PR tree-optimization/56407
    * gcc.dg/torture/pr56407.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr56407.c
Modified:
    trunk/gcc/testsuite/ChangeLog

Author: rguenth
Date: Wed Apr  3 10:26:13 2013
New Revision: 197400

URL: http://gcc.gnu.org/viewcvs?rev=197400&root=gcc&view=rev
Log:
2013-04-03  Richard Biener  <rguenther@suse.de>

    PR tree-optimization/56407
    * gcc.dg/torture/pr56407.c: New testcase.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/torture/pr56407.c
Modified:
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog


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

* [Bug tree-optimization/56407] [4.7 Regression] Optimizations (-O2 -O3) make comparison of arrays of ints to fail
  2013-02-20 11:53 [Bug c/56407] New: Optimizations (-O2 -O3) make comparison of arrays of ints to fail quintin at lix dot polytechnique.fr
                   ` (3 preceding siblings ...)
  2013-04-03 10:26 ` rguenth at gcc dot gnu.org
@ 2013-04-11  7:59 ` rguenth at gcc dot gnu.org
  2014-06-12 13:24 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-04-11  7:59 UTC (permalink / raw)
  To: gcc-bugs


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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|4.7.3                       |4.7.4

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> 2013-04-11 07:59:32 UTC ---
GCC 4.7.3 is being released, adjusting target milestone.


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

* [Bug tree-optimization/56407] [4.7 Regression] Optimizations (-O2 -O3) make comparison of arrays of ints to fail
  2013-02-20 11:53 [Bug c/56407] New: Optimizations (-O2 -O3) make comparison of arrays of ints to fail quintin at lix dot polytechnique.fr
                   ` (4 preceding siblings ...)
  2013-04-11  7:59 ` rguenth at gcc dot gnu.org
@ 2014-06-12 13:24 ` rguenth at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-06-12 13:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56407

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|4.7.4                       |4.8.0
      Known to fail|                            |4.7.4

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed in 4.8.0.


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

end of thread, other threads:[~2014-06-12 13:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-20 11:53 [Bug c/56407] New: Optimizations (-O2 -O3) make comparison of arrays of ints to fail quintin at lix dot polytechnique.fr
2013-02-20 13:11 ` [Bug c/56407] [4.7 Regression] " jakub at gcc dot gnu.org
2013-04-03  9:53 ` [Bug tree-optimization/56407] " rguenth at gcc dot gnu.org
2013-04-03 10:23 ` rguenth at gcc dot gnu.org
2013-04-03 10:26 ` rguenth at gcc dot gnu.org
2013-04-11  7:59 ` rguenth at gcc dot gnu.org
2014-06-12 13:24 ` rguenth at gcc dot gnu.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).