The following C function:

typedef struct {

    unsigned    x : 1,
                y : 1,
                z : 1,
                a : 3,
                b : 18,
                c : 5;
} FOO;

extern volatile FOO var;

void    bar( void )
{
FOO x;

    x.x = 1;
    x.y = 0;
    x.z = 1;
    x.a = 0;
    x.b = 55;
    x.c = 0;

    var = x;
}

static  const FOO y = { 1,0,1,0,55,0 };

void    baz( void )
{
    var = y;
}

when compiled with -Os, -O2 or -O3 generates a very short function for bar():

        ldr     r3, .L3
        ldr     r2, .L3+4
        str     r2, [r3, #0]
        bx      lr
        .align  2
.L3:
        .word   var
        .word   3525

but a rather long one for baz:

        ldr     r3, .L7
        ldr     r2, [r3, #0]
        orr     r2, r2, #1
        str     r2, [r3, #0]
        ldr     r2, [r3, #0]
        bic     r2, r2, #2
        str     r2, [r3, #0]
        ldr     r2, [r3, #0]
        orr     r2, r2, #4
        str     r2, [r3, #0]
        ldr     r2, [r3, #0]
        bic     r2, r2, #56
        str     r2, [r3, #0]
        ldr     r2, [r3, #0]
        bic     r2, r2, #16711680
        bic     r2, r2, #61952
        orr     r2, r2, #3520
        str     r2, [r3, #0]
        ldr     r2, [r3, #0]
        bic     r2, r2, #520093696
        str     r2, [r3, #0]
        bx      lr
        .align  2
.L7:
        .word   var

It seems that when the compiler sees that 'y' is a constant, it generates a
sequence of individual field loads for structure members for a temporary
structure (just like what bar() does), but then it does not run the optimizer
over the generated sequence.


-- 
           Summary: Bitfield optimisation problem
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: zoltan at bendor dot com dot au
  GCC host triplet: i586-gnu-linux
GCC target triplet: arm-elf-none


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43003

Reply via email to