SATURDAY, NOVEMBER 28, 2026
gcc/simplify-rtx
simplify-rtx: Preserves volatile reads in smax (-A, A) to abs (A) folding
GCC optimization pass prevents incorrect folding of volatile reads into abs() calls.
The smax (-A, A) to abs (A) optimization in simplify-rtx incorrectly dropped volatile reads. This change ensures that the negated operand’s side effects are checked before folding, preventing volatile memory accesses from being lost and reordered. New tests verify the fix for cases involving volatile integer reads.
In Details
The simplify-rtx.cc pass simplify_binary_operation_1 for SMAX used to fold smax (-A, A) to abs (A). This overlooked side effects on A if it was a volatile memory read. This commit adds a !side_effects_p check on the negated operand before performing the fold, preserving volatile accesses as seen in PR126726.
For Context
- smax
- A GCC internal representation (RTX) for a select maximum operation, often optimized into conditional moves or other efficient instructions.
- volatile read
- A memory access that must not be optimized away or reordered by the compiler, typically used for memory-mapped I/O or shared memory where external factors can change the value.
- RTX
- Register Transfer Language, an intermediate representation used in GCC that describes operations on registers and memory.
- folding
- The compiler's process of simplifying expressions or patterns in the intermediate representation into a more efficient form.