Reduce (t * u) / v when gcd(u, v) > 1.
Optimizes division expressions by reducing constants when they share common factors.
GCC’s tree optimization pass now simplifies integer divisions of the form (t * u) / v when u and v share a common divisor greater than one. By dividing u and v by their greatest common divisor (gcd), the compiler generates smaller constants, which can lead to more efficient division operations and potentially avoid overflow issues. This optimization is applied when t * u is known not to overflow.
In Details
The tree-optimization pass, using match.pd patterns, now reduces the constants in (t * u) / v expressions when gcd(u, v) > 1. The expression is rewritten as (t * (u / g)) / (v / g), where g = gcd(u, v). This transformation is applied if t * u is guaranteed not to overflow. This optimization aims to reduce the magnitude of constants involved in division, leading to potentially cheaper division-by-constant sequences and mitigating overflow risks. The optimization requires a single-use multiplication.
- tree-optimization
- A phase in the GCC compiler that operates on an intermediate representation called 'tree' to perform various code optimizations.
- gcd
- Greatest Common Divisor. The largest positive integer that divides two or more integers without leaving a remainder.
- overflow
- A condition that occurs when an arithmetic operation produces a result that is too large to be stored in the allocated memory space for that data type.
- division-by-constant
- An optimization where a division operation by a constant value is replaced by a sequence of shifts, additions, and subtractions, which can be faster on some architectures.