https://gcc.gnu.org/g:85f255021606d2bf147c7083e8b49e8987d5c393
commit r17-6643-g85f255021606d2bf147c7083e8b49e8987d5c393 Author: Shivam Gupta <[email protected]> Date: Tue Nov 24 10:48:59 2026 -0700 [PATCH v3] tree-optimization: Reduce (t * u) / v when gcd(u, v) > 1 For an integer division (t * u) / v where the constants u and v share a common factor g = gcd (u, v) > 1, rewrite it as (t * (u / g)) / (v / g). This is done when t * u is known not to overflow: always for types with undefined overflow, otherwise only if the value range of t shows that the multiplication cannot wrap. The smaller constants avoid overflow and give cheaper division-by-constant sequences. Changes since v2: * Compute the gcd in widest_int; wi::gcd on the type-precision wide_int mishandles INT_MIN and unsigned constants with the top bit set, which could miscompile (new runtime test). * Require a single-use multiply (mult:s). Changes since v1: * Fix the scan counts in pr126311.c. * Add a bounded unsigned case in the same test file. Regression tested on x86_64-linux-gnu. PR tree-optimization/126311 gcc/ChangeLog: * match.pd ((t * u) / v): Reduce integer constants u and v by their greatest common divisor if gcd(u, v) > 1. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr126311.c: New test. * gcc.dg/torture/pr126311.c: New test. Signed-off-by: Shivam Gupta <[email protected]> Diff: --- gcc/match.pd | 29 +++++++++++++++++++++++ gcc/testsuite/gcc.dg/torture/pr126311.c | 40 ++++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr126311.c | 39 +++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index 1930b847b83a..59204f98e0e3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1276,6 +1276,35 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) #endif )))) +/* Simplify (t * u) / v -> (t * (u / g)) / (v / g) where g = gcd(u, v) > 1. */ +(for div (trunc_div exact_div) + (simplify + (div (mult:s@3 @0 INTEGER_CST@1) INTEGER_CST@2) + (if (INTEGRAL_TYPE_P (type) + && !integer_zerop (@1) + && !integer_zerop (@2)) + (with { + widest_int w1 = wi::to_widest (@1); + widest_int w2 = wi::to_widest (@2); + widest_int g = wi::gcd (w1, w2, SIGNED); + } + (if (wi::gts_p (g, 1) + && g != wi::abs (w2)) + (with { + tree c1 = wide_int_to_tree (type, wi::div_trunc (w1, g, SIGNED)); + tree c2 = wide_int_to_tree (type, wi::div_trunc (w2, g, SIGNED)); + } + (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type)) + (div (mult @0 { c1; }) { c2; }) +#if GIMPLE + (with { int_range_max vr0, vr1; } + (if (gimple_match_range_of_expr (vr0, @0, @3) + && gimple_match_range_of_expr (vr1, @1) + && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) + (div (mult @0 { c1; }) { c2; }))) +#endif + ))))))) + /* Simplify (t * u) % u -> 0. The product is an exact multiple of u, so the remainder is zero under every rounding convention as long as the multiplication does not overflow. Mirrors (t * u) / u -> t above. */ diff --git a/gcc/testsuite/gcc.dg/torture/pr126311.c b/gcc/testsuite/gcc.dg/torture/pr126311.c new file mode 100644 index 000000000000..7329d1ab3784 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr126311.c @@ -0,0 +1,40 @@ +/* { dg-do run } */ +/* PR tree-optimization/126311 */ + +/* Corner cases for the (t * u) / v fold: it must not reduce by a gcd + that doesn't divide v. */ + +#define UMAX (__INT_MAX__ * 2U + 1) +#define DIV (UMAX - 2) +#define T_MAX (DIV / 3) /* Largest t with t * 3 < DIV. */ + +/* The result is always 0. Don't reduce by 6, which abs (INT_MIN) + wrapping would give as the gcd. */ +__attribute__((noipa)) int +f1 (int t) +{ + return (t * 6) / (-__INT_MAX__ - 1); +} + +/* t * 3 can't wrap and stays below DIV, so the result is always 0. + Don't reduce by 3, which doesn't divide DIV. */ +__attribute__((noipa)) unsigned +f2 (unsigned x) +{ + unsigned t = x % (T_MAX + 1); + return (t * 3u) / DIV; +} + +int +main () +{ + if (f1 (__INT_MAX__ / 6) != 0) + __builtin_abort (); + if (f1 (-(__INT_MAX__ / 6)) != 0) + __builtin_abort (); + if (f2 (T_MAX) != 0) + __builtin_abort (); + if (f2 (T_MAX - 1) != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126311.c b/gcc/testsuite/gcc.dg/tree-ssa/pr126311.c new file mode 100644 index 000000000000..304f2f86a321 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126311.c @@ -0,0 +1,39 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* gcd(6, 10) == 2: (t * 6) / 10 -> (t * 3) / 5 */ +int src_pos (int t) { + return (t * 6) / 10; +} + +/* Same with a negative multiplier: (t * -6) / 10 -> (t * -3) / 5 */ +int src_neg (int t) { + return (t * -6) / 10; +} + +/* Reduced: b is unsigned short, so t is in [0, 65535] and t * 6u cannot wrap. */ +unsigned int src_unsigned_bounded (unsigned short b) { + unsigned int t = b; + return (t * 6u) / 10u; +} + +/* Not reduced: t may be any unsigned value, so ranger can't prove + t * 6u doesn't wrap. */ +unsigned int src_unsigned_unbounded (unsigned int t) { + return (t * 6u) / 10u; +} + +/* No common factor: gcd(7, 10) == 1 (should stay / 10) */ +int src_gcd_one (int t) { + return (t * 7) / 10; +} + +/* Not reduced: the product has another use (mult:s). */ +int src_multi_use (int t, int *p) { + int m = t * 6; + *p = m; + return m / 10; +} + +/* { dg-final { scan-tree-dump-times " / 5" 3 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " / 10" 3 "optimized" } } */
