Bug 30181

Summary: gcc-2.95.3 -O2 optimalization bug
Product: Base System Reporter: lampa <lampa>
Component: gnuAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.3-RELEASE   
Hardware: Any   
OS: Any   

Description lampa 2001-08-29 07:20:00 UTC
gcc-2.95.3 (both FreeBSD port and original GNU version) generates
bad code for long long operations in some cases when -O2 is on:

#include <sys/types.h>
#include <netinet/in.h>

extern off_t
rfc1035NamePack(char *buf, size_t sz, const char *name);

static off_t
rfc1035QuestionPack(char *buf, size_t sz, const char *name, unsigned short type,
 unsigned short class)
{
    off_t off = 0;                  <-- this is long long
    unsigned short s;
    off += rfc1035NamePack(buf, sz, name);
    s = class;
    memcpy(buf + off, &s, sizeof(s));
    assert(off <= sz);
    return off;
}

The code is taken from squid-2.3.STABLE5, it's minimized to demonstrate bug.

gcc -v -O2 -S t.c
Using builtin specs.
gcc version 2.95.3 [FreeBSD] 20010315 (release)
 /usr/libexec/cpp -lang-c -v -D__GNUC__=2 -D__GNUC_MINOR__=95 -Di386 -D__FreeBSD
__=4 -D__FreeBSD_cc_version=440000 -Dunix -D__i386__ -D__FreeBSD__=4 -D__FreeBSD
_cc_version=440000 -D__unix__ -D__i386 -D__unix -Acpu(i386) -Amachine(i386) -Asy
stem(unix) -Asystem(FreeBSD) -D__OPTIMIZE__ -Acpu(i386) -Amachine(i386) -Di386 -
D__i386 -D__i386__ -D__ELF__ t.c /tmp/ccvoKazy.i
GNU CPP version 2.95.3 [FreeBSD] 20010315 (release) (i386 FreeBSD/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/include
 /usr/include
End of search list.
The following default directories have been omitted from the search path:
 /usr/include/g++
End of omitted list.
 /usr/libexec/cc1 /tmp/ccvoKazy.i -quiet -dumpbase t.c -O2 -version -o t.s
GNU C version 2.95.3 [FreeBSD] 20010315 (release) (i386-unknown-freebsd) compile
d by GNU C version 2.95.3 [FreeBSD] 20010315 (release).

      .file   "t.c"
        .version        "01.01"
gcc2_compiled.:
.text
        .p2align 2,0x90
        .type            rfc1035QuestionPack,@function
rfc1035QuestionPack:
        pushl %ebp
        movl %esp,%ebp
        subl $16,%esp
        pushl %edi
        pushl %esi
        movl 8(%ebp),%esi
        movl 12(%ebp),%eax
        xorl %edx,%edx       <---- this should be long long (%eax,%edx)
        movl %eax,12(%ebp)   <---- 2nd argument overwritten
        movl %edx,16(%ebp)   <---- 3rd argument overwritten
        movl 24(%ebp),%edi
        addl $-4,%esp
        pushl 16(%ebp)
        pushl %eax
        pushl %esi
        call rfc1035NamePack
        movl %eax,-8(%ebp)
        movl %edx,-4(%ebp)
        movl -8(%ebp),%eax
        movw %di,(%eax,%esi)
        addl $-12,%esp
        xorl %ecx,%ecx
        movl 12(%ebp),%eax
        movl 16(%ebp),%edx
        addl $16,%esp
        cmpl %edx,-4(%ebp)
        jg .L3
        jne .L4
        cmpl %eax,-8(%ebp)
        ja .L3
.L4:
        movl $1,%ecx
.L3:
        pushl %ecx
        call assert
        movl -8(%ebp),%eax
        movl -4(%ebp),%edx
        leal -24(%ebp),%esp
        popl %esi
        popl %esi
        popl %edi
        leave
        ret
.Lfe1:
        .size            rfc1035QuestionPack,.Lfe1-rfc1035QuestionPack
        .ident  "GCC: (GNU) c 2.95.3 [FreeBSD] 20010315 (release)"

Fix: 

Use -O optimalization only. This error may manifest anywhere where long long type is used (distribution, ports). This should be mentioned
in release notes.
gcc-3.0.1 seems to be fixed.
How-To-Repeat: gcc -O2 t.c
Comment 1 Bruce Evans 2001-08-29 13:30:00 UTC
On Tue, 28 Aug 2001, Petr Lampa wrote:

> >Description:
> gcc-2.95.3 (both FreeBSD port and original GNU version) generates
> bad code for long long operations in some cases when -O2 is on:

See also PR 19245.  It gives essentially the same example, with less
analysis and more followup.

Bruce
Comment 2 Kris Kennaway 2001-08-29 22:01:18 UTC
On Tue, Aug 28, 2001 at 11:15:58PM -0700, Petr Lampa wrote:

> >How-To-Repeat:
> gcc -O2 t.c
> >Fix:
> Use -O optimalization only. This error may manifest anywhere where
> long long type is used (distribution, ports). This should be
> mentioned in release notes.

It's currently documented in /etc/make.conf.

You need to report gcc bugs to the gcc developers; there's no-one in
FreeBSD who tracks these down and fixes them locally.

Kris
Comment 3 David E. O'Brien freebsd_committer freebsd_triage 2001-08-30 21:18:22 UTC
Responsible Changed
From-To: freebsd-bugs->obrien

I wish people would make me aware of these PRs when they see them....
Comment 4 David E. O'Brien freebsd_committer freebsd_triage 2001-09-10 17:27:01 UTC
Responsible Changed
From-To: obrien->freebsd-bugs
Comment 5 ashp freebsd_committer freebsd_triage 2002-01-18 03:22:09 UTC
State Changed
From-To: open->closed

We don't currently have the manpower to fix bugs in GCC.  This should be 
filed with the GCC guys directly.