[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

-O2 optimalization produces wrong code



>Submitter-Id:	net
>Originator:	Robert Samal
>Organization:	Charles University, Prague
>Confidential:	no
>Synopsis:	-O2 optimalization produces wrong code
>Severity:	serious 
>Priority:	medium
>Category:	c
>Class:		wrong-code 
>Release:	3.0.4 (Debian testing/unstable)
>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
>Description:
On the following code, gcc (version 3.0 or 3.3) with -O2 (or higer) produces
wrong code from a very short program ...
>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;
}
>Fix:
Compile without optimization or with version 2.95 ... ;-)



Reply to: