Consider:

struct B {
  unsigned b : 2;
};

void
store (struct B *b, int v)
{
  b->b += v;
}

./cc1 -O2 -fomit-frame-pointer -mregparm=3 generates
store:
        movb    (%eax), %cl
        andl    $3, %ecx
        addl    %edx, %ecx
        andl    $3, %ecx
        movl    (%eax), %edx
        andl    $-4, %edx
        orl     %ecx, %edx
        movl    %edx, (%eax)
        ret

We don't need two "andl".  We could do something like

store:
        movl    (%eax), %ecx
        addl    %ecx, %edx
        andl    $3, %edx
        andl    $-4, %ecx
        orl     %ecx, %edx
        movl    %edx, (%eax)
        ret

-- 
           Summary: addition in a multibit bitfield is inefficient
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P2
         Component: middle-end
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: kazu at cs dot umass dot edu
                CC: gcc-bugs at gcc dot gnu dot org
GCC target triplet: i686-pc-linux-gnu


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

Reply via email to