gcc 4.1.0 (and 4.2-20050415) produces wrong output for this program.
I can reproduce it on i386 and mips/mipsel.

Calls for foo_read() must not be discarded (for example, 'mem' might point a
memory-mapped IO register),
but gcc -O optimizes a foo_read() call away.
gcc -O -fno-unit-at-a-time produces correct output.

static inline int inline_read(volatile int *mem)
{
        return *mem;
}
int foo_read(volatile int *mem)
{
        return inline_read(mem);
}
unsigned int foo(volatile int *mem)
{
        foo_read(mem);
        return foo_read(mem);
}

gcc -O output:
        .file   "foo.c"
        .text
.globl foo_read
        .type   foo_read, @function
foo_read:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        movl    (%eax), %eax
        popl    %ebp
        ret
        .size   foo_read, .-foo_read
.globl foo
        .type   foo, @function
foo:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $4, %esp
        movl    8(%ebp), %eax
        movl    %eax, (%esp)
        call    foo_read
        leave
        ret
        .size   foo, .-foo
        .ident  "GCC: (GNU) 4.1.0"
        .section        .note.GNU-stack,"",@progbits

gcc -O -fno-unit-at-a-time output:
        .file   "foo.c"
        .text
.globl foo_read
        .type   foo_read, @function
foo_read:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        movl    (%eax), %eax
        popl    %ebp
        ret
        .size   foo_read, .-foo_read
.globl foo
        .type   foo, @function
foo:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ebx
        subl    $4, %esp
        movl    8(%ebp), %ebx
        movl    %ebx, (%esp)
        call    foo_read
        movl    %ebx, (%esp)
        call    foo_read
        addl    $4, %esp
        popl    %ebx
        popl    %ebp
        ret
        .size   foo, .-foo
        .ident  "GCC: (GNU) 4.1.0"
        .section        .note.GNU-stack,"",@progbits


-- 
           Summary: a call for function with side-effects discarded by
                    optimization
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: anemo at mba dot ocn dot ne dot jp


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

Reply via email to