Optimize smax(a, -a) to abs(a) at RTL level
GCC now optimizes smax(a, -a) and smax(-a, a) to abs(a) for integer operands.
GCC’s RTL (Register Transfer Language) optimization pass now detects and simplifies specific forms of the smax (signed maximum) operation. For integer operands, the patterns smax(a, -a) and smax(-a, a) are recognized and transformed into abs(a). This optimization can lead to more efficient code by replacing a conditional-like operation with a direct absolute value computation.
In Details
This commit enhances simplify_context::simplify_binary_operation_1 in simplify-rtx.cc to optimize smax(a, -a) and smax(-a, a) for integer operands into abs(a). This targets a common pattern that arises from certain code structures or other optimizations. Previously, this specific simplification was not performed at the RTL level, potentially leaving redundant conditional logic. This change improves the quality of the generated RTL, potentially leading to smaller and faster code.
- RTL
- Register Transfer Language. An intermediate representation used by GCC to describe operations on registers and memory. It sits between the high-level language and the machine code.
- smax
- Signed maximum. An operation that returns the larger of two signed integer values.
- abs
- Absolute value. An operation that returns the non-negative value of a number, effectively making negative numbers positive.
- optimization pass
- A stage in the compiler's pipeline that analyzes and transforms the intermediate representation to improve code performance, size, or other metrics.