Avoid spurious volatile load before bit-field store
Fixes a bug where volatile bit-field stores incorrectly emitted a read-modify-write cycle.
When storing to a bit-field that occupies an entire memory unit, the compiler previously emitted a read of the memory destination before the write, even if no surrounding bits needed preserving. For non-volatile destinations, this read was optimized away, but for volatile destinations, it remained as a spurious memory access. This change modifies the store operation to directly write the value when the bit-field fills the entire unit, eliminating the unnecessary volatile read and preventing incorrect behavior with memory-mapped I/O registers that have side effects on reads.
In Details
The store_fixed_bit_field_1 operation implements bit-field stores using a read-modify-write sequence. This is problematic for volatile destinations where the entire memory unit is overwritten by the bit-field, as the read is unnecessary but cannot be removed. On strict-alignment targets, misaligned volatile stores are decomposed into per-unit stores, exacerbating the issue by introducing dead volatile loads before each store. This patch modifies the logic to emit a direct store when the bit-field fully occupies the unit and the destination is memory, avoiding the spurious volatile load. Thi…
- bit-field store
- Storing a value into a specific portion of a larger data type, where that portion is smaller than the full type.
- read-modify-write
- A memory operation that first reads a value from memory, modifies it, and then writes the result back to the same memory location.
- volatile destination
- A memory location that must be accessed directly without compiler optimizations that might assume its value remains unchanged or that accesses can be reordered or omitted.
- strict-alignment
- A memory access requirement where data must reside at an address that is a multiple of its size.
- misaligned store
- A memory write operation where the target address is not aligned to the natural boundary for the data type being stored.