tree-optimization/128052 - miscompilation of unaligned loads from covered stores
Fixes miscompilation of unaligned loads from constant stores by accurately calculating bit offsets.
This commit corrects a miscompilation issue related to unaligned loads from memory locations covered by a single constant store. The previous implementation in vn_reference_lookup_3 incorrectly calculated the bit span for such loads, especially when they started at a non-byte-aligned offset. This could lead to bits being dropped from the loaded value if the required data extended beyond the encoded buffer. The fix adjusts the buffer size and accurately accounts for the bit remainder and total span, ensuring correct data retrieval and preventing bit truncation.
In Details
The SSA SCCVN (Value Numbering for Sparse Conditional Constant Propagation) pass, specifically within vn_reference_lookup_3, had a bug in handling loads that were fully covered by a single constant store. The calculation of the bit range for such loads did not properly account for the bit remainder (offseti - offset2i) % BITS_PER_UNIT. This led to incorrect checks on the encoded buffer size, potentially causing a miscompilation where unaligned loads (especially those involving large _BitInt types spanning across byte boundaries) would drop bits from the loaded value because the necessar…
- SCCVN (Sparse Conditional Value Numbering)
- An optimization technique that uses value numbering to determine constant values and propagate them through the program, even in the presence of conditional branches.
- unaligned load
- A memory read operation where the address of the data is not aligned to a natural boundary of the data type (e.g., reading a 4-byte integer at an address not divisible by 4).
- constant store
- Storing a fixed, known value into memory during compilation or runtime.
- _BitInt
- A GCC extension for arbitrary-precision integers, allowing users to specify integers of any bit width.
- bit remainder
- The fractional part of a bit offset when measured in units of bytes. For example, an offset of 3 bits into a byte has a bit remainder of 3.