Bug 39755 - inline memcpy() incorrectly optimized on MIPS target
Summary: inline memcpy() incorrectly optimized on MIPS target
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.2.1
: P3 normal
Target Milestone: 3.4.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-13 19:52 UTC by Mike Sieweke
Modified: 2009-04-16 15:56 UTC (History)
1 user (show)

See Also:
Host: i386-gnu-linux
Target: mipsisa32-elf
Build: i386-gnu-linux
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Sieweke 2009-04-13 19:52:44 UTC
In one case the C compiler can optimize away an inline memcpy() on a
MIPS target.  The problem was duplicated with GCC 3.2.1 and 3.3.3.
The problem does not affect x86 targets or GCC 4.x (tested on 4.2.4).
The MIPS cross-compiler runs under RHEL.  Tested MIPS cross-compilers
under Cygwin and Mingw with the same results.

This is the test code and the resulting output.  The "if" statement and
16-byte memcpy() are optimized away, leaving only the 4-byte memcpy.

Compile with:  mipsisa32-elf-gcc -O2 -c test.c
---------------------------
#include <string.h>
typedef struct {
    char C[16];
    int  E;
} st;

void f( st *S, char* c );
void f( st *S, char* c )
{
    if ( S->E == 6 )
        memcpy( &S->C, c, 16 );
    else
        memcpy( &S->C, c, 4 );
}
---------------------------
00000000 <f>:
   0:   88a20000        lwl     v0,0(a1)
   4:   98a20003        lwr     v0,3(a1)
   8:   a8820000        swl     v0,0(a0)
   c:   03e00008        jr      ra
  10:   b8820003        swr     v0,3(a0)
---------------------------

Compiler info:
$ mipsisa32-elf-gcc -v                  
Reading specs from /tools/ecos/3.2.1_opt/Linux/opt/ecos/gnutools/mipsisa32-elf/bin/../lib/gcc-lib/mipsisa32-elf/3.2.1/specs
Configured with: /home/msieweke/ecos20/gnutools-src/gcc-3.2.1/configure --target=mipsisa32-elf --prefix=/home/msieweke/gnutools_321 --enable-languages=c,c++ --enable-gofast --with-gnu-as --with-gnu-ld --with-newlib --with-gxx-include-dir=/home/msieweke/gnutools_321/mipsisa32-elf/include -v
Thread model: single
gcc version 3.2.1
Comment 1 Richard Biener 2009-04-14 10:13:54 UTC
GCC 3.2.1 is no longer maintained, please reproduce with at least GCC 4.3.3.
Comment 2 Mike Sieweke 2009-04-16 15:06:35 UTC
As mentioned in the original report, the bug doesn't exist in GCC 4.x.x.  It
has since been tested with GCC 3.4.4, where the bug is fixed.
GCC 3.2.x - broken
GCC 3.3.x - broken
GCC 3.4.x - fixed
GCC 4.x.x - fixed
For a number of reasons, we're stuck using GCC 3.2.1 with only minor updates.
I tried porting parts of the MIPS code generator from 3.4.4 into 3.2.1, but
this is a larger project than it appears.  I don't expect anyone to spend much
time fixing this, but I would appreciate a hint about how I might approach
a fix.
Comment 3 Richard Biener 2009-04-16 15:56:42 UTC
Try bisecting the changes to cherry-pick the one fixing the bug.

Fixed in GCC 3.4.