Package: gcc-3.3
Version: 1:3.3.3-6
Severity: wishlist

 I saw some code that redundantly tests a malloc return for NULL, but
returns it either way.  I wondered whether gcc would notice the
optimization.  It turns out that it gets it right with -march=athlon, or
pentium3, but wrong with i386 or i486.

 This C code:
#define NULL ((void *)0)
void *malloc( int );
void *foo(void)
{
   void *p = malloc(10);

   if( !p ) return NULL;
   return p;
}

 compiles to this assembly with
gcc gcc-redundancy.c -S -o /dev/tty -O3 -fomit-frame-poiner -march=i486
foo:
        subl    $24, %esp
        pushl   $10
        call    malloc
        testl   %eax, %eax
        movl    %eax, %ecx
        sete    %al
        andl    $255, %eax
        decl    %eax
        andl    %ecx, %eax
        addl    $28, %esp
        ret

 A better version is produced by
gcc gcc-redundancy.c -S -o /dev/tty -O3 -fomit-frame-poiner -march=athlon-xp
foo:
        subl    $12, %esp
        movl    $10, (%esp)
        call    malloc
        addl    $12, %esp
        ret

 Unless I'm missing something, there's no reason why that code wouldn't be
better for i486 too.

 If the C is  if( !p ) p=NULL; return p;  gcc always gets it right.

 happy hacking,

-- System Information:
Debian Release: 3.0
  APT prefers unstable
  APT policy: (500, 'unstable')
Architecture: i386 (i686)
Kernel: Linux 2.4.24-ck1
Locale: LANG=C, LC_CTYPE=C

Versions of packages gcc-3.3 depends on:
ii  binutils                   2.14.90.0.7-6 The GNU assembler, linker and bina
ii  cpp-3.3                    1:3.3.3-6     The GNU C preprocessor
ii  gcc-3.3-base               1:3.3.3-6     The GNU Compiler Collection (base 
ii  libc6                      2.3.2.ds1-11  GNU C Library: Shared libraries an
ii  libgcc1                    1:3.3.3-6     GCC support library

-- no debconf information


Reply via email to