Bug 11536

Summary: [3.3/3.4 Regression] [strength-reduce] -O2 optimalization produces wrong code
Product: gcc Reporter: samal <samal>
Component: rtl-optimizationAssignee: Eric Botcazou <ebotcazou>
Status: RESOLVED FIXED    
Severity: critical CC: gcc-bugs
Priority: P1 Keywords: wrong-code
Version: 3.0.4   
Target Milestone: 3.3.1   
Host: i386-pc-linux-gnu Target: i?86-*-* powerpc-*-*
Build: i386-pc-linux-gnu Known to work:
Known to fail: Last reconfirmed: 2003-07-16 02:17:44

Description samal@kam.mff.cuni.cz 2003-07-16 01:52:58 UTC
On the following code, gcc (version 3.0 or 3.3) with -O2 (or higer) produces
wrong code from a very short program ...

Environment:
System: Linux camelot 2.4.19 #1 Fri Aug 16 17:01:16 CEST 2002 i686 unknown
Architecture: i686

	
host: i386-pc-linux-gnu
build: i386-pc-linux-gnu
target: i386-pc-linux-gnu
configured with: ../src/configure -v --enable-languages=c,c++,java,f77,proto,objc --prefix=/usr --infodir=/share/info --mandir=/share/man --enable-shared --with-gnu-as --with-gnu-ld --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --disable-checking --enable-threads=posix --enable-java-gc=boehm --with-cpp-install-dir=bin --enable-objc-gc i386-linux

How-To-Repeat:
gcc -O2 the_following_file

/* bug in gcc -O2
Reading specs from /usr/lib/gcc-lib/i386-linux/3.0.4/specs
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f77,proto,objc --prefix=/usr
 --infodir=/share/info --mandir=/share/man --enable-shared --with-gnu-as
 --with-gnu-ld --with-system-zlib --enable-long-long --enable-nls
 --without-included-gettext --disable-checking --enable-threads=posix
 --enable-java-gc=boehm --with-cpp-install-dir=bin --enable-objc-gc
 i386-linux
 Thread model: posix
 gcc version 3.0.4

The same problem at another machine
Reading specs from /usr/lib/gcc-lib/i386-linux/3.3/specs
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib
--enable-nls --without-included-gettext --enable-__cxa_atexit
--enable-clocale=gnu --enable-debug --enable-java-gc=boehm
--enable-java-awt=xlib --enable-objc-gc i386-linux Thread model: posix
gcc version 3.3 (Debian)

****************

When compiled without optimalization, the program behaves correctly, it is
it print lines upto "B f=4, l=12". However, when compiled with -O2, 
it finishes with "  A f=1, l=3",  when the inner while cycle condition
is still satisfied ...
Maybe the two while-cycles are somehow mixed up?

*/

int main()
{
  int i;
  int first= 0;
  int last= 0;

  while (last<3) {
    last = first;
    while(first<=last) {
      first++;
      for (i=0;i<3;i++){
        last++;
        printf("C f=%d, l=%d\n",first,last);
      }
      printf("B f=%d, l=%d\n",first,last);
      if (last>10) return 0;
    }
    printf("  A f=%d, l=%d\n",first,last);
  }

  return 0;
}
Comment 1 samal@kam.mff.cuni.cz 2003-07-16 01:52:58 UTC
Fix:
Compile without optimization or with version 2.95 ... ;-)
Comment 2 Andrew Pinski 2003-07-16 02:17:43 UTC
I can confirm this on i686-pc-linux with gcc 3.0.4, 3.3.1 (20030707), the mainline 
(20030714) but it did not produce bad code with 2.95.3.  I can also confirm this on 
powerpc-apple-darwin6.6 (mainline 20030715) so it looks to be strength reduce bug 
because if I use -fno-strength-reduce, I get the correct result. I also get bad code with the 
mainline (20030715) on i686-unknown-openbsd3.1.
The code looks valid to me, no pointers at all so this is not the aliasing problem that the 
other strength-reduce problems are about.
Here is the diff for powerpc with temp.s being the correct code (-O2 -fno-strength-reduce), 
t.s (-O2):
--- t.s Tue Jul 15 22:15:53 2003
+++ temp.s      Tue Jul 15 22:15:55 2003
@@ -23,17 +23,19 @@
        stwu r1,-80(r1)
 L12:
        mr r30,r28
-       li r29,2
+L11:
        addi r28,r28,1
-L19:
+       li r29,0
+L9:
        addi r30,r30,1
        addis r3,r31,ha16(LC0-"L00000000001$pb")
        la r3,lo16(LC0-"L00000000001$pb")(r3)
+       addi r29,r29,1
        mr r4,r28
        mr r5,r30
        bl L_printf$stub
-       addic. r29,r29,-1
-       bge+ cr0,L19
+       cmpwi cr7,r29,2
+       ble+ cr7,L9
        addis r3,r31,ha16(LC1-"L00000000001$pb")
        mr r4,r28
        la r3,lo16(LC1-"L00000000001$pb")(r3)
@@ -41,6 +43,8 @@
        bl L_printf$stub
        cmpwi cr7,r30,10
        bgt- cr7,L1
+       cmpw cr7,r28,r30
+       ble+ cr7,L11
        addis r3,r31,ha16(LC2-"L00000000001$pb")
        mr r4,r28
        la r3,lo16(LC2-"L00000000001$pb")(r3)
Comment 3 Andrew Pinski 2003-07-16 02:34:29 UTC
Here is a testcase suited for the testsuite:
void f(char*,int,int) __attribute__((__noinline__));

void f(char *t,int i,int j){if(t[0]=='C')abort();}

int main()
{
  int i;
  int first= 0;
  int last= 0;

  while (last<3) {
    last = first;
    while(first<=last) {
      first++;
      for (i=0;i<3;i++){
        last++;
        f("A",first,last);
      }
      if (last>10) return 0;
      f("B",first,last);
    }
    f("C",first,last);
  }
  return 0;
}
Comment 4 Eric Botcazou 2003-07-17 10:01:51 UTC
Investigating.
Comment 5 GCC Commits 2003-07-21 16:52:46 UTC
Subject: Bug 11536

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2003-07-21 16:52:38

Modified files:
	gcc            : ChangeLog unroll.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg: 20030721-1.c 

Log message:
	PR optimization/11536
	* unroll.c (loop_iterations): Do not replace a register holding
	the final value by its equivalent before the loop if it is not
	invariant.
	
	PR optimization/11536
	* gcc.dg/20030721-1.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.579&r2=2.580
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/unroll.c.diff?cvsroot=gcc&r1=1.200&r2=1.201
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2901&r2=1.2902
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030721-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 6 GCC Commits 2003-07-21 17:50:08 UTC
Subject: Bug 11536

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	mmitchel@gcc.gnu.org	2003-07-21 17:50:05

Modified files:
	gcc            : ChangeLog unroll.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg: 20030721-1.c 

Log message:
	PR optimization/11536
	* unroll.c (loop_iterations): Do not replace a register holding
	the final value by its equivalent before the loop if it is not
	invariant.
	
	PR optimization/11536
	* gcc.dg/20030721-1.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.678&r2=1.16114.2.679
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/unroll.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.184.2.4&r2=1.184.2.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.239&r2=1.2261.2.240
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/20030721-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1

Comment 7 Mark Mitchell 2003-07-21 18:11:48 UTC
Fixed in GCC 3.3.1 and GCC 3.4.