Bug 997 - Incorrect behavior in presence of glibc memset optimization
Summary: Incorrect behavior in presence of glibc memset optimization
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 2.97
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2000-12-05 23:56 UTC by dana
Modified: 2003-07-25 17:33 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
memset-builtins.i.gz (2.77 KB, application/x-gzip )
2003-05-21 15:17 UTC, dana
Details

Note You need to log in before you can comment on or make changes to this bug.
Description dana 2000-12-05 23:56:00 UTC
This program uses memset with a small compile-time fixed size.  If the glibc string optimizations are allowed (the default when optimization is used), then the memset is replaced with a large chunk of code using __builtin_memset among other things.

The test will compile properly with all snapshots I have.  When compiled with "-mcpu=i686 -O" I see these results with the following compilers:

2.95.2     OK
20000313   OK
20000807   Abort
20000904   Abort
20001002   OK
20001106   OK
20001113   Abort
20001127   Abort
20001204   Abort

The problem is seen in the check2() function where on ia32 we should be returning 1 and we do not.  check1() is identical except for an early constant return.  We abort if they are not equal, which should let us just test for this error.

To add more information, with -mcpu=i386, I get failure with -O but success with -Os, -O2, or -O3.  -mcpu=i686 gives failure on all -O levels >= 1.  Compiling without optimization avoids the problem.

Release:
gcc version 2.97 20001127 (experimental)

Environment:
i686-pc-linux-gnu, Pentium III, RedHat 6.2 Linux

How-To-Repeat:
Use included .i file to ensure you see the same memset optimization.

#include <string.h>

typedef union {
    unsigned short  a_ushort;
    unsigned long   a_ulong;
} align_t;

static int check1(void)
{
   /* Returns 1 on ia32 */
   int r;
   align_t a;
   memset(&a,0xff,sizeof(a));
   if  (a.a_ushort == 0xFFFF)
      return 1;
   r = (a.a_ushort == 0xFFFF)  ?  1  :  0;
   return r;
}

static int check2(void)
{
   /* Should return 1 on ia32 */
   int r;
   align_t a;
   memset(&a,0xff,sizeof(a));
   r = (a.a_ushort == 0xFFFF)  ?  1  :  0;
   return r;
}

int main(void)
{
   if (check1() != check2())
      abort();
   return 0;
}
Comment 1 dana 2000-12-05 23:56:00 UTC
Fix:
1. Compile without optimization (-O0).

2. Compile the source with -D__NO_STRING_INLINES to prevent glibc from touching memset.

3. Use "-O2 -mcpu=i386"
Comment 2 Richard Henderson 2001-01-16 02:26:59 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Appears to be the same underlying bug as
      gcc.c-torture/execute/20001009-2.c
    
    That is, everything is fine until combine simplifies
    (HImode -1 == HImode 0xffff) -> 0.
Comment 3 Richard Henderson 2001-01-16 10:26:59 UTC
From: rth@gcc.gnu.org
To: dana@boi.hp.com, gcc-gnats@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: optimization/997
Date: 16 Jan 2001 10:26:59 -0000

 Synopsis: Incorrect behavior in presence of glibc memset optimization
 
 State-Changed-From-To: open->analyzed
 State-Changed-By: rth
 State-Changed-When: Tue Jan 16 02:26:59 2001
 State-Changed-Why:
     Appears to be the same underlying bug as
       gcc.c-torture/execute/20001009-2.c
     
     That is, everything is fine until combine simplifies
     (HImode -1 == HImode 0xffff) -> 0.
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=997&database=gcc
Comment 4 Richard Henderson 2001-02-14 14:25:14 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: 2001-02-13  Alexandre Oliva  <aoliva@redhat.com>
    
            * explow.c (trunc_int_for_mode): Sign-extend value to mode.
Comment 5 Richard Henderson 2001-02-14 22:25:15 UTC
From: rth@gcc.gnu.org
To: dana@boi.hp.com, gcc-gnats@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: optimization/997
Date: 14 Feb 2001 22:25:15 -0000

 Synopsis: Incorrect behavior in presence of glibc memset optimization
 
 State-Changed-From-To: analyzed->closed
 State-Changed-By: rth
 State-Changed-When: Wed Feb 14 14:25:14 2001
 State-Changed-Why:
     2001-02-13  Alexandre Oliva  <aoliva@redhat.com>
     
             * explow.c (trunc_int_for_mode): Sign-extend value to mode.
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=997&database=gcc