Bug 5230 - suboptimal code generated with -fPIC
: suboptimal code generated with -fPIC
Status: CLOSED WORKSFORME
: Sisyphus
(All bugs in Sisyphus/gcc4.4)
: unstable
: all Linux
: P1 enhancement
Assigned To:
:
:
:
:
:
  Show dependency tree
 
Reported: 2004-09-20 12:43 by
Modified: 2010-11-04 23:22 (History)


Attachments


Note

You need to log in before you can comment on or make changes to this bug.


Description From 2004-09-20 12:43:06
$ cat > test.c
#include <stdlib.h>
void *f(int flag) { return flag ? malloc(1) : NULL; }

$ gcc -S -O3 -fomit-frame-pointer -fPIC -DPIC test.c -o -

        .file   "test.c"
        .text
        .p2align 4,,15
.globl f
        .type   f, @function
f:
        pushl   %ebx
        pushl   %eax
        call    .L3
.L3:
        popl    %ebx
        addl    $_GLOBAL_OFFSET_TABLE_+[.-.L3], %ebx
        pushl   %eax
        movl    16(%esp), %edx
        testl   %edx, %edx
        je      .L2
        subl    $12, %esp
        pushl   $1
        call    malloc@PLT
        addl    $16, %esp
.L1:
        popl    %edx
        popl    %ecx
        popl    %ebx
        ret
        .p2align 4,,7
.L2:
        xorl    %eax, %eax
        jmp     .L1
        .size   f, .-f
        .section        .note.GNU-stack,"",@progbits
        .ident  "GCC: (GNU) 3.3.3 20040412 (ALT Linux, build 3.3.3-alt5)"

It's clearly visible that check if flag is equal to 0 performed after loading
address of _GLOBAL_OFFSET_TABLE_ in %ebx. It would be more effective to perform
this check before loading GOT in %ebx.
------- Comment #1 From 2005-01-11 18:38:02 -------
still in gcc3.4
------- Comment #2 From 2006-05-27 03:00:01 -------
Same with gcc-4.1.1; maybe I should report it upstream? :)
------- Comment #3 From 2008-10-31 16:00:44 -------
Ой что я нашёл :)
#6052 сюда же.
------- Comment #4 From 2010-11-04 23:22:20 -------
$ gcc -S -O3 -fomit-frame-pointer -fPIC -DPIC test.c -o -
        .file   "test.c"
        .text
        .p2align 4,,15
.globl f
        .type   f, @function
f:
.LFB12:
        .cfi_startproc
        testl   %edi, %edi
        jne     .L4
        xorl    %eax, %eax
        ret
        .p2align 4,,10
        .p2align 3
.L4:
        movl    $1, %edi
        jmp     malloc@PLT
        .cfi_endproc
.LFE12:
        .size   f, .-f
        .ident  "GCC: (GNU) 4.5.1 20100924 (ALT Linux 4.5.1-alt1)"
        .section        .note.GNU-stack,"",@progbits


It seems fixed in gcc4.5.