https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68360

            Bug ID: 68360
           Summary: GCC bitfield processing code is very inefficient
           Product: gcc
           Version: 5.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vorfeed.canal at gmail dot com
  Target Milestone: ---

It looks like GCC couldn't generate efficient code where clang could. I'm not
talking about something extremely complicated - just the code where few bits
must be copied without modifications.

E.g.

$ cat test.c
#include <string.h>

struct test {
  unsigned :32;
  unsigned :2;
  unsigned a:2;
  unsigned :2;
  unsigned b:1;
  unsigned :1;
};

void bar(struct test*);

void foo(unsigned int i) {
  struct test a;
  a.a = (i >> 2) & 0x3;
  a.b = (i >> 6) & 0x1;
  bar(&a);
}

$ clang -O3 -S test2.c -o-
        .text
        .file   "test2.c"
        .globl  foo
        .align  16, 0x90
        .type   foo,@function
foo:                                    # @foo
# BB#0:
        subl    $28, %esp
        movl    32(%esp), %eax
        andl    $76, %eax
        movl    %eax, 20(%esp)
        movl    $0, 16(%esp)
        leal    16(%esp), %eax
        movl    %eax, (%esp)
        calll   bar
        addl    $28, %esp
        retl
.Lfunc_end0:
        .size   foo, .Lfunc_end0-foo


        .ident  "clang version 3.6 "
        .section        ".note.GNU-stack","",@progbits
$ g++ -O3 -S test2.c -o-
        .file   "test2.c"
        .section        .text.unlikely,"ax",@progbits
.LCOLDB0:
        .text
.LHOTB0:
        .p2align 4,,15
        .globl  _Z3fooj
        .type   _Z3fooj, @function
_Z3fooj:
.LFB14:
        .cfi_startproc
        subq    $24, %rsp
        .cfi_def_cfa_offset 32
        movl    %edi, %edx
        shrl    $6, %edi
        movzbl  4(%rsp), %ecx
        shrl    $2, %edx
        andl    $1, %edi
        andl    $3, %edx
        movl    %edi, %eax
        sall    $2, %edx
        sall    $6, %eax
        andl    $-77, %ecx
        orl     %edx, %ecx
        movl    %ecx, %edi
        orl     %eax, %edi
        movb    %dil, 4(%rsp)
        movq    %rsp, %rdi
        call    _Z3barP4test
        addq    $24, %rsp
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc
.LFE14:
        .size   _Z3fooj, .-_Z3fooj
        .section        .text.unlikely
.LCOLDE0:
        .text
.LHOTE0:
        .ident  "GCC: (GNU) 5.2.0"
        .section        .note.GNU-stack,"",@progbits

Reply via email to