[Bug tree-optimization/17742] New: C Optimization error with -O1 and -O2 on i686

m dot calderbank at iname dot com gcc-bugzilla@gcc.gnu.org
Wed Sep 29 22:37:00 GMT 2004


The following program compiles incorrectly on i686 using the latest CVS code and
optimization level -O1 or -O2. But -O3 is OK, as is -fno-ivopts.
I am aware that bug #17474 concerns -fno-ivopts but lack the knowledge to
determine if this is a related problem.

Compiled with gcc -O1 -save-temps test.c:

--- Begin test.c ---
#define advance(f) do { ++pos; if ((f) && pos >= argc) return(2); } while (0)
int pos;
int argc; char **argv;
int term();

int
main(int margc, char **margv)
{
  argc = margc; argv = margv;
  pos = 1;
  return term();
}

int
term()
{
  int value;
  if (pos >= argc) return (2);
  if (argv[pos][0] == '!' && argv[pos][1] == '\0')
    {
      value = 0;
      while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
        {
          advance (1);
          value = 1 - value;
        }
      return (value ? !term() : term());
    }
  return (0);
}
--- End test.c ---

Output of gcc -v:

Reading specs from
/home/mark/tarballs/gcc-cvs/gcc-build/usr/lib/gcc/i686-pc-linux-gnu/4.0.0/specs
Configured with: ../gcc/configure
--prefix=/home/mark/tarballs/gcc-cvs/gcc-build/usr
--libexecdir=/home/mark/tarballs/gcc-cvs/gcc-build/usr/lib --enable-shared
--enable-languages=c
Thread model: posix
gcc version 4.0.0 20040929 (experimental)

The file test.c is cut down from the coreutils program of the same name, which
also suffers from this bug. When supplied with three or more arguments of which
at least the first two are exclamation marks, the program enters an infinite
loop. e.g.

./a.out ! ! foo

I've included a portion of my assembly output below to point out what I think is
going wrong. Line (1) earmarks register %ecx for the 'value' variable. Line (2)
is supposed to be performing ++pos, but in fact performs "pos = original-pos +
value + 1". Lines (3) flip "value" between 0 and 1. Of course this breaks after
two iterations, and the loop never exits.

        movl    $0, %ecx               ; (1)
        leal    0(,%eax,4), %ebx
.L13:
        leal    1(%edi,%ecx), %eax     ; (2)
        movl    %eax, pos
        cmpl    %eax, -16(%ebp)
        jg      .L14
        movl    $2, %eax
        jmp     .L8
.L14:
        movl    $1, %eax               ; (3)
        subl    %ecx, %eax             ; (3)
        movl    %eax, %edx             ; (3)
        movl    %eax, %ecx             ; (3)
        leal    (%ebx,%eax,4), %eax
        movl    (%eax,%esi), %eax
        cmpb    $33, (%eax)
        jne     .L16
        cmpb    $0, 1(%eax)
        je      .L13

-- 
           Summary: C Optimization error with -O1 and -O2 on i686
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: m dot calderbank at iname dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17742



More information about the Gcc-bugs mailing list