Bug 30931 - [4.1 regression] infinite loop with -O1 -fstrength-reduce
Summary: [4.1 regression] infinite loop with -O1 -fstrength-reduce
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.1.3
: P3 normal
Target Milestone: 4.1.3
Assignee: Eric Botcazou
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2007-02-22 22:39 UTC by Peter Bergner
Modified: 2007-02-27 20:23 UTC (History)
3 users (show)

See Also:
Host:
Target: i?86-*-*
Build:
Known to work: 3.4.6 4.2.0
Known to fail: 4.0.0 4.0.4 4.1.0 4.1.2
Last reconfirmed: 2007-02-26 19:18:18


Attachments
Reduced testcase showing the infinite looping (179 bytes, text/plain)
2007-02-22 22:40 UTC, Peter Bergner
Details
Good assembly output from the 4.1 compiler (-O1 -fno-strength-reduce) (187 bytes, text/plain)
2007-02-23 14:17 UTC, Peter Bergner
Details
Bad assembly output from the 4.1 compiler (-O1 -fstrength-reduce) (183 bytes, text/plain)
2007-02-23 14:17 UTC, Peter Bergner
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Bergner 2007-02-22 22:39:30 UTC
The simple test which I'll attach shortly, loops infinitely when compiled with -O1 -fstrength-reduce and executes fine with plain -O1.  Doing a little debugging, it seems the loop index variable "i" is not incremented within the loop causing us to loop forever.

I have tested this using the latest FSF 4.1, 4.2 and mainline compiler sources and it only fails using the 4.1 compiler.  Using an older 3.4.6 distro compiler, it executes fine, so this _looks_ like a regression.
Comment 1 Peter Bergner 2007-02-22 22:40:44 UTC
Created attachment 13092 [details]
Reduced testcase showing the infinite looping
Comment 2 Steven Bosscher 2007-02-22 23:26:52 UTC
I cannot reproduce this.
Please paste the output of gcc -v.
Comment 3 Andrew Pinski 2007-02-22 23:29:43 UTC
> it only fails using the 4.1 compiler.

Well that is because loop.c has been removed in 4.2 and above.
Comment 4 Peter Bergner 2007-02-22 23:32:23 UTC
This is using source checked out this afternoon (revision 122219):

bg47:bergner% ./install/gcc-4.1/bin/gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../../gcc-4_1-20070222/configure --enable-shared --enable-threads=posix --enable-checking --enable-languages=c --prefix=/tmp/bergner/install/gcc-4.1
Thread model: posix
gcc version 4.1.3 20070222 (prerelease)
Comment 5 Richard Biener 2007-02-23 10:03:25 UTC
Works for me.  Can you tell us all options in effect?  I.e. gcc -O -fstrength-reduce t.c -v output?
Comment 6 Peter Bergner 2007-02-23 14:06:15 UTC
Here's the output you asked for using the latest 4.1 sources I built yesterday. This also fails for me using the system compilers on Ubuntu Edgy and SLES10. I'll try and track down a x86 RHEL5 system to test there too.

Please note it's not the compiler that is looping forever, but the built binary when executed.

bg47:bergner% ./install/gcc-4.1/bin/gcc -O1 -fstrength-reduce pr30311.c -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../../gcc-4_1-20070222/configure --enable-shared --enable-threads=posix --enable-checking --enable-languages=c --prefix=/tmp/bergner/install/gcc-4.1
Thread model: posix
gcc version 4.1.3 20070222 (prerelease)
 /tmp/bergner/install/gcc-4.1/libexec/gcc/i686-pc-linux-gnu/4.1.3/cc1 -quiet -v pr30311.c -quiet -dumpbase pr30311.c -mtune=pentiumpro -auxbase pr30311 -O1 -version -fstrength-reduce -o /tmp/cc6Cx7Y6.s
ignoring nonexistent directory "/tmp/bergner/install/gcc-4.1/lib/gcc/i686-pc-linux-gnu/4.1.3/../../../../i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /tmp/bergner/install/gcc-4.1/include
 /tmp/bergner/install/gcc-4.1/lib/gcc/i686-pc-linux-gnu/4.1.3/include
 /usr/include
End of search list.
GNU C version 4.1.3 20070222 (prerelease) (i686-pc-linux-gnu)
        compiled by GNU C version 3.4.4 20050721 (Red Hat 3.4.4-2).
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 3c763771770d7626aeca3ba678fea9f5
 as -V -Qy -o /tmp/ccmkRFA6.o /tmp/cc6Cx7Y6.s
GNU assembler version 2.15.92.0.2 (i386-redhat-linux) using BFD version 2.15.92.0.2 20040927
 /tmp/bergner/install/gcc-4.1/libexec/gcc/i686-pc-linux-gnu/4.1.3/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /tmp/bergner/install/gcc-4.1/lib/gcc/i686-pc-linux-gnu/4.1.3/crtbegin.o -L/tmp/bergner/install/gcc-4.1/lib/gcc/i686-pc-linux-gnu/4.1.3 -L/tmp/bergner/install/gcc-4.1/lib/gcc/i686-pc-linux-gnu/4.1.3/../../.. /tmp/ccmkRFA6.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /tmp/bergner/install/gcc-4.1/lib/gcc/i686-pc-linux-gnu/4.1.3/crtend.o /usr/lib/crtn.o
Comment 7 Richard Biener 2007-02-23 14:13:03 UTC
Confirmed.
Comment 8 Peter Bergner 2007-02-23 14:17:06 UTC
Created attachment 13097 [details]
Good assembly output from the 4.1 compiler (-O1 -fno-strength-reduce)
Comment 9 Peter Bergner 2007-02-23 14:17:48 UTC
Created attachment 13098 [details]
Bad assembly output from the 4.1 compiler (-O1 -fstrength-reduce)
Comment 10 Paolo Bonzini 2007-02-23 14:30:33 UTC
No, i (which is in %edx) *is* being incremented.  It's the strength-reduced derived induction variable that is constructed in a wrong way.  The optimized code looks like:

void bug (struct s *p)
{
  int i;
  struct s *pp = p;
  for (i=0; i < 2; i++)
    {
      goto loop_cond;

     loop:
      /* ??? I think gcc assumes that pp (%eax) is an induction variable of
         the inner loop... */
      pp[-1].first = 0;
     loop_cond:
      if (!pp[0].first)
        goto break_out_of_inner_loop;
      if (pp[0].done)
        goto loop;

     break_out_of_inner_loop;
      pp++; /* actually adds sizeof (struct s) in the asm code */
      i++;
    }
}

Comment 11 Paolo Bonzini 2007-02-23 14:32:27 UTC
>   for (i=0; i < 2; i++)

Well, that's obviously

    for (i=0; i < 2; )

in my code of comment #10.
Comment 12 Eric Botcazou 2007-02-26 19:18:18 UTC
Investigating.
Comment 13 Eric Botcazou 2007-02-27 20:21:32 UTC
Subject: Bug 30931

Author: ebotcazou
Date: Tue Feb 27 20:21:17 2007
New Revision: 122383

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=122383
Log:
	PR rtl-optimization/30931
	* loop.c (combine_givs_p): Return false if either GIV is not
	always executed.


Added:
    branches/gcc-4_1-branch/gcc/testsuite/gcc.c-torture/execute/20070227-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 14 Eric Botcazou 2007-02-27 20:23:16 UTC
Fixed on 4.1 branch.