Preserve potentially trapping remainder in `X % Y < Y`
Division by zero exceptions are now correctly preserved in optimizations involving the remainder operator.
This commit refines pattern matching in match.pd to preserve potentially trapping exceptions when optimizing expressions involving the remainder operator (%). Previously, certain folds related to X % Y < Y and X % Y >= Y would drop the remainder even when Y could be zero and non-call exceptions were enabled, or when the divisor was explicitly zero. The updated patterns now use the same conditions as existing folds for (X % Y) ==/!= X and (X / Y) ==/!= 0 to ensure that division by zero exceptions are correctly handled.
In Details
The pattern matching rules for X % Y < Y and X % Y >= Y in match.pd have been updated to preserve a possible zero-divisor exception. These folds previously dropped the remainder when Y could be zero and non-call exceptions were enabled, or with an explicit zero divisor. The new rules align with the conditions used in folds for (X % Y) ==/!= X and (X / Y) ==/!= 0 (as per r17-3016), ensuring that such potentially trapping operations are not prematurely optimized away without proper exception handling.
- match.pd
- A GCC file containing pattern matching definitions for various tree operations, used during optimization passes.
- remainder operator (`%`)
- A binary operator that returns the remainder of a division operation.
- trapping exceptions
- Floating-point or integer exceptions that, when enabled, cause the program to halt or transfer control to an exception handler upon occurrence (e.g., division by zero).
- folding
- An optimization technique where constant expressions or function calls with constant arguments are evaluated at compile time.
- pattern matching
- A compiler technique used to identify specific code structures or sequences that can be replaced by more efficient code or intrinsics.