Bug 23282 - [4.0 Regression] wrong results at -O on x86
Summary: [4.0 Regression] wrong results at -O on x86
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.0.1
: P1 critical
Target Milestone: 4.0.3
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2005-08-08 06:26 UTC by Joost VandeVondele
Modified: 2006-01-19 00:55 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.1.0 3.4.0
Known to fail: 4.0.0 4.0.1 4.0.2
Last reconfirmed: 2006-01-15 21:22:01


Attachments
patch for gcc-4_0-branch (same as in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22442#c5) (642 bytes, patch)
2005-11-03 10:39 UTC, Serge Belyshev
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Joost VandeVondele 2005-08-08 06:26:59 UTC
Using gcc 4.0.1 :

Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: /data1/vondele/gcc-401/gcc/configure --
prefix=/data1/vondele/gcc-401/result --with-gmp-dir=/data1/vondele/gmp-4.1.4/ -
-with-mpfr=/home/vondele/ --enable-languages=c,f95
Thread model: posix
gcc version 4.0.1

The following program generates wrong results (-1 instead of 4 for the second 
number printed out) if compiled with -O. It works fine with gcc 3.3.4 and the 
portland group c compiler:

#include <stdio.h>

void sub(int *m) {
  int index, l, count;

  l = 2;

  for(count=2; count>0; count--, l++) {
    index = l*l+1;

    printf("%d\n", m[index-2*l]);

    m[index+1] = -1;
  }
}


int main(void) {
  int i, m[20];

  for(i=0; i<20; i++)
    m[i] = i;

  sub(m);

  for(i=0; i<20; i++)
    printf("%d ", m[i]);

  putchar('\n');
}

vondele> gcc test.c
vondele> ./a.out
1
4
0 1 2 3 4 5 -1 7 8 9 10 -1 12 13 14 15 16 17 18 19

vondele> gcc -O test.c
vondele> ./a.out
1
-1
0 1 2 3 4 5 -1 7 8 9 10 -1 12 13 14 15 16 17 18 19
Comment 1 Andrew Pinski 2005-08-08 06:38:03 UTC
This might already be fixed in 4.0.2.
Comment 2 Serge Belyshev 2005-08-08 08:00:14 UTC
Confirmed, smaller testcase:

void abort (void);

int main (void)
{
  int j, a, b;
  
  for (j = 0; j < 2; j++)
    {
      a = j * j;
      b = a - 2 * j;
    }
  if (b != -1)
    abort ();
  return 0;
}

This bug fixed (or masked) on mainline by this patch:

2005-05-06  Zdenek Dvorak  <dvorakz@suse.cz>

	PR tree-optimization/19401
	* tree-flow.h (tree_unroll_loops_completely): Declaration changed.
	* tree-ssa-loop-ivcanon.c (enum unroll_level): New.
        ...
Comment 3 Richard Biener 2005-08-08 08:25:27 UTC
Mainline seems to be really fixed, even with complete loop unrolling disabled.
4.0.2 as of 20050728 is still affected by the bug (you can hide it there, too,
with specifying -fpeel-loops, which enables complete unrolling).

Must be a IVOPTs or scalar evolution problem (the latter got some fixes on
the mainline).
Comment 4 Steven Bosscher 2005-08-08 08:44:55 UTC
GCC 4.0.2 (CVS) does this:  
  
ivcanon dump:  
  
  # b_9 = PHI <b_3(1)>;  
<L2>:;  
  if (b_9 != -1) goto <L3>; else goto <L4>;  
  
<L3>:;  
  printf (&"%d\n"[0], b_9);  
  abort ();  
  
  
.ivopts dump:  
  # b_9 = PHI <b_3(1)>;  
<L2>:;  
  if (b_9 != -1) goto <L3>; else goto <L4>;  
  
<L3>:;  
  printf (&"%d\n"[0], b_9);  
  abort ();  
  
  
Looks like final value replacement screws up.  
  
Comment 5 Steven Bosscher 2005-08-08 09:08:24 UTC
Definitely IVopts. 
Comment 6 Zdenek Dvorak 2005-08-16 08:03:17 UTC
Seems like a duplicate of PR22442 to me.
Comment 7 Serge Belyshev 2005-08-16 10:17:58 UTC
(In reply to comment #6)
> Seems like a duplicate of PR22442 to me.

Even if this is a dup, tescase from that bug does not fail on 4.0 and vice versa ;)
Testcase from this bug still fails on 4.0 branch.
Comment 8 Zdenek Dvorak 2005-08-16 10:21:56 UTC
Subject: Re:  [4.0 Regression] wrong results at -O on x86

> > Seems like a duplicate of PR22442 to me.
> 
> Even if this is a dup, tescase from that bug does not fail on 4.0 and
> vice versa ;)
> Testcase from this bug still fails on 4.0 branch.

The patch for PR22442 should fix the problem.  The underlying problem
seems to be the same, it just shows up in a different optimizer this
time.
Comment 9 Joost VandeVondele 2005-09-09 08:54:05 UTC
> The patch for PR22442 should fix the problem.  

has anybody updated this patch so far or can it be applied as is ?
Comment 10 Serge Belyshev 2005-11-03 10:39:49 UTC
Created attachment 10121 [details]
patch for gcc-4_0-branch (same as in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22442#c5)

(In reply to comment #9)
> > The patch for PR22442 should fix the problem.  
> 
> has anybody updated this patch so far or can it be applied as is ?

just do:

svn merge -r 101961:101962 svn://gcc.gnu.org/svn/gcc/trunk/gcc/tree-chrec.c gcc/tree-chrec.c

in your working copy of gcc-4_0-branch (see detailed instructions how to use subversion here: http://gcc.gnu.org/wiki/SvnHelp )

Zdenek, please apply this patch to gcc-4_0-branch too.
Comment 11 Joost VandeVondele 2005-12-01 10:21:53 UTC
> 
> svn merge -r 101961:101962 svn://gcc.gnu.org/svn/gcc/trunk/gcc/tree-chrec.c
> gcc/tree-chrec.c
> 

Yes, this fixes the problem for me (tested on _4_0_1_release). This really should be applied to the branch I think.
Comment 12 Serge Belyshev 2005-12-21 15:58:03 UTC
Zdenek, ping!

Please apply patch for this bug to gcc-4_0-branch.
Comment 13 Joost VandeVondele 2006-01-03 09:36:05 UTC
Still causing trouble with gcc version 4.0.3 20060103 (prerelease)
Comment 14 Serge Belyshev 2006-01-15 21:22:01 UTC
PING!!!
Comment 15 Zdenek Dvorak 2006-01-18 23:31:21 UTC
Subject: Bug 23282

Author: rakdver
Date: Wed Jan 18 23:31:16 2006
New Revision: 109920

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=109920
Log:
	PR tree-optimization/23282
	* tree-chrec.c (chrec_fold_multiply_poly_poly): Associate chrecs
	correctly.

	PR tree-optimization/23282
	* gcc.c-torture/execute/pr23282.c: New test.


Added:
    branches/gcc-4_0-branch/gcc/testsuite/gcc.c-torture/execute/pr23282.c
Modified:
    branches/gcc-4_0-branch/gcc/ChangeLog
    branches/gcc-4_0-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_0-branch/gcc/tree-chrec.c

Comment 16 Andrew Pinski 2006-01-19 00:55:03 UTC
Fixed.