Bug 5230 - suboptimal code generated with -fPIC
Summary: suboptimal code generated with -fPIC
Status: CLOSED WORKSFORME
Alias: None
Product: Sisyphus
Classification: Development
Component: gcc4.4 (show other bugs)
Version: unstable
Hardware: all Linux
: P1 enhancement
Assignee: Gleb F-Malinovskiy
QA Contact: qa-sisyphus
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-09-20 12:43 MSD by Alexey Voinov
Modified: 2010-11-04 23:22 MSK (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexey Voinov 2004-09-20 12:43:06 MSD
$ 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 Alexey Voinov 2005-01-11 18:38:02 MSK
still in gcc3.4
Comment 2 Dmitry V. Levin 2006-05-27 03:00:01 MSD
Same with gcc-4.1.1; maybe I should report it upstream? :)
Comment 3 Andrey Rahmatullin 2008-10-31 16:00:44 MSK
Ой что я нашёл :)
#6052 сюда же.
Comment 4 Kirill A. Shutemov 2010-11-04 23:22:20 MSK
$ 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.