GCC Bugzilla – Bug 28386
[4.1 regression] Wrong code with if condition in loop
Last modified: 2006-09-05 07:08:34 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.
Confirmed.
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.
Goes away with -floop-optimize2. *sigh*
Investigating.
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
Fixed in upcoming 4.1.2 release.