Bitfield Optimization

Accessing and storing bitfields are relative expensive, since most architectures do not support bit memory operations and require a series of load/shift/mask/store instructions. Run-time performance can be improved through various bitfield optimizations including keeping bitfields in registers, performing constant propagation through bitfields, and combining adjacent bitfield stores into one store.

Example:

In the code fragment below, the two bitfield assignments can be combined into one store operation, thus eliminating some of the load/shift/mask/store instructions.

struct
{
  int bit1 : 1;
  int bit2 : 1;
} bits;

bits.bit1 = 1;
bits.bit2 = 1;
  

In the code fragment below, the compiler has generated a two-bit alias for bit1 and bit2 and combined the two stores into one store.

struct
{
  int t : 2;   /* compiler-generated alias for bit1 and bit2 */
} bits;

bits.t = 3;