Bug 28386 - [4.1 regression] Wrong code with if condition in loop
Summary: [4.1 regression] Wrong code with if condition in loop
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.1.0
: P3 blocker
Target Milestone: 4.1.2
Assignee: Eric Botcazou
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: monitored, patch, wrong-code
Depends on:
Blocks:
 
Reported: 2006-07-14 20:02 UTC by Volker Reichelt
Modified: 2006-09-05 07:08 UTC (History)
6 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2006-08-11 07:31:09


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Volker Reichelt 2006-07-14 20:02:09 UTC
Consider the following testcase - a variant from
http://gcc.gnu.org/ml/gcc-help/2006-07/msg00177.html :

=========================================================
#include <stdio.h>

volatile char s[256][3];

int main()
{
    int i, j=0;

    for (i = 0; i < 256; i++)
        if (i >= 128 && i < 256)
            { printf("%d ", s[i - 128][0]); ++j; }
    printf("\nj = %d\n", j);
    return 0;
}
=========================================================

This program should generate the following output
(wrapped to 80 characters per line):

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
j = 128

With -Os I get the output

-125 4 -21 -8 -16 93 -119 -24 0 -127 17 80 -2 89 -61 3 0 2 100 10 61 100 0 0 0 0
 -107 0 0 0 0 12 0 4 0 -108 8 0 -127 5 0 4 0 112 8 0 0 11 0 0 0 -28 64 0 -107 2 
0 0 0 17 0 0 -126 17 0 4 0 8 0 0 0 -2 111 4 -1 1 0 -1 -126 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 -1 -1 0 -1 0 0 0 -108 -8 64 0 -52 -80 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 
j = 255

Parts of the if-condition ionside the loop have been optimized out.
Happens only on the 4.1 branch.
Comment 1 Volker Reichelt 2006-07-14 20:10:03 UTC
Confirmed.
Comment 2 Richard Biener 2006-07-15 11:40:40 UTC
This is going wrong in RTL land.  Tree optimization looks ok - we're merging the
two IVs and using a wrapping unsigned IV going from 0xffffff81 to 127:

<bb 0>:
  ivtmp.33D.1833 = 0ffffff81;
  jD.1769 = 0;

<L0>:;
  if (ivtmp.33D.1833 <= 127) goto <L1>; else goto <L2>;

<L1>:;
  D.1775 = (intD.0) ivtmp.33D.1833;
  D.1776 = sD.1765[D.1775][0];
  printf (&"%d "[0], (intD.0) D.1776);
  jD.1769 = jD.1769 + 1;

<L2>:;
  ivtmp.33D.1833 = ivtmp.33D.1833 + 1;
  if (ivtmp.33D.1833 != 128) goto <L0>; else goto <L4>;

<L4>:;
  printf (&"\nj = %d\n"[0], jD.1769);
  return 0;

of course, it's the particular choice of IVs that triggers the bug later on.
Comment 3 Richard Biener 2006-07-15 11:54:39 UTC
Goes away with -floop-optimize2.  *sigh*
Comment 4 Eric Botcazou 2006-08-11 07:31:09 UTC
Investigating.
Comment 5 Eric Botcazou 2006-09-05 07:06:58 UTC
Subject: Bug 28386

Author: ebotcazou
Date: Tue Sep  5 07:06:46 2006
New Revision: 116693

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116693
Log:
	PR rtl-optimization/28386
	* loop.c (biased_biv_may_wrap_p): Rename to biv_may_wrap_p and
	remove 'bias' parameter.
	(maybe_eliminate_biv_1): Adjust for above change.


Added:
    branches/gcc-4_1-branch/gcc/testsuite/gcc.c-torture/execute/20060905-1.c
Modified:
    branches/gcc-4_1-branch/gcc/ChangeLog
    branches/gcc-4_1-branch/gcc/loop.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 6 Eric Botcazou 2006-09-05 07:08:34 UTC
Fixed in upcoming 4.1.2 release.