Simplify (t * (C + 1)) / C to t + t / C in match.pd.
Optimizes division expressions in match.pd for better performance.
The GCC match.pd optimization pass now simplifies specific division and multiplication patterns to improve code efficiency. It transforms (t * (C + 1)) / C into t + t / C for signed and unsigned types, provided the multiplication doesn’t overflow. Additionally, for exact division, (t * (C - 1)) / C is simplified to t - t / C. For unsigned types, a related pattern involving right shifts is also optimized. These changes aim to reduce the number of operations required for these calculations.
In Details
The match.pd pattern matching file has been updated to simplify division and shift expressions. The patterns (t * (C + 1)) / C are transformed into t + t / C for both signed and unsigned types, contingent on the multiplication not overflowing. This holds true if signed overflow is undefined or if value ranges prove it. For exact_div, (t * (C - 1)) / C simplifies to t - t / C. For unsigned types, (t * (2^k + 1)) >> k is simplified to t + (t >> k). These transformations require the multiplication to have a single use.
- match.pd
- A file within GCC that defines patterns for various code transformations and optimizations, particularly for the tree-SSA (Static Single Assignment) form of intermediate representation.
- tree-SSA
- A compiler intermediate representation where every variable is assigned exactly once. This form simplifies many data flow analyses and optimizations.
- 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.
- exact_div
- An operation representing division that is guaranteed to have no remainder. This is often a specialized case of division used in compiler optimizations.