[Bug c/42210] New: avr: optimizing assignment to a bit field

sjackman at gmail dot com gcc-bugzilla@gcc.gnu.org
Sun Nov 29 01:05:00 GMT 2009


When assigning a bool to a single bit of a bitfield located in the
bit-addressable region of memory, better code is produced by
       if (flag)
               bitfield.bit = true;
       else
               bitfield.bit = false;
than
       bitfield.bit = flag;

I've included a short test and the assembler output by both forms.
Should I file a bug suggesting a possible improvement here?

Cheers,
Shaun

#include <stdbool.h>
#include <stdint.h>

struct byte { uint8_t x0:1; uint8_t x1:1; uint8_t x2:1; uint8_t x3:1;
       uint8_t x4:1; uint8_t x5:1; uint8_t x6:1; uint8_t x7:1; };

volatile struct byte *const porte = (void*)0x23;

void set_flag_good(bool flag)
{
       if (flag)
               porte->x6 = true;
       else
               porte->x6 = false;
}

void set_flag_bad(bool flag)
{
       porte->x6 = flag;
}


00000000 <set_flag_good>:
  0:   88 23           and     r24, r24
  2:   01 f4           brne    .+0             ; 0x4 <set_flag_good+0x4>
                       2: R_AVR_7_PCREL        .text+0x8
  4:   1e 98           cbi     0x03, 6 ; 3
  6:   08 95           ret
  8:   1e 9a           sbi     0x03, 6 ; 3
  a:   08 95           ret

0000000c <set_flag_bad>:
  c:   81 70           andi    r24, 0x01       ; 1
  e:   82 95           swap    r24
 10:   88 0f           add     r24, r24
 12:   88 0f           add     r24, r24
 14:   80 7c           andi    r24, 0xC0       ; 192
 16:   93 b1           in      r25, 0x03       ; 3
 18:   9f 7b           andi    r25, 0xBF       ; 191
 1a:   98 2b           or      r25, r24
 1c:   93 b9           out     0x03, r25       ; 3
 1e:   08 95           ret


-- 
           Summary: avr: optimizing assignment to a bit field
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sjackman at gmail dot com
GCC target triplet: avr-unknown-none


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



More information about the Gcc-bugs mailing list