https://gcc.gnu.org/g:0333a956e3f953cfc8796d41948fecced4264eed
commit r17-6644-g0333a956e3f953cfc8796d41948fecced4264eed Author: Shivam Gupta <[email protected]> Date: Tue Nov 24 10:53:24 2026 -0700 [PATCH v4] match.pd: Simplify (t * (C + 1)) / C to t + t / C [PR126294] Simplify (t * (C + 1)) / C to t + t / C for signed and unsigned types when the multiplication does not overflow, either because signed overflow is undefined or because value ranges prove it. Unsigned division by a power of two has already become a right shift when ranges are available, so also simplify (t * (2^k + 1)) >> k to t + (t >> k) for unsigned types under the same range condition. Both transformations require the multiplication to have a single use, since otherwise it is kept and the result needs an extra operation. For exact_div only, also simplify (t * (C - 1)) / C to t - t / C. For trunc_div the result is t - ceil (t / C), which differs unless C divides t. exact_div cannot be produced from C source and the GIMPLE front end cannot express it, so this case has no testcase. Changes since v3: * Compare the shift count using wi::to_widest so a narrow or negative count cannot pass the bounds check. Addressed sashiko review comment. Changes since v2: * Compare the constants using the sign of the type, so unsigned divisors with the most significant bit set are not rejected. * Allow the division transformation without range information when signed overflow is undefined and not sanitized, matching the (t * u) / v pattern above, instead of requiring a known range. * Require a single use of the multiplication: single_use for the division arm, which shares its match expression with the (t * u) / v pattern, and :s for the shift pattern. * Add tests for the above, including -fwrapv. Changes since v1: * Add a comment explaining the !vr0.varying_p () check. * Restrict the (t * (2^k + 1)) >> k pattern to unsigned types. Bootstrapped and regtested on x86_64-linux-gnu. PR tree-optimization/126294 gcc/ChangeLog: * match.pd: Simplify (t * (C + 1)) / C to t + t / C and, for exact_div, (t * (C - 1)) / C to t - t / C, and (t * (2^k + 1)) >> k to t + (t >> k), when the multiplication has a single use and does not overflow. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr126294.c: New test. * gcc.dg/tree-ssa/pr126294-2.c: New test. Signed-off-by: Shivam Gupta <[email protected]> Diff: --- gcc/match.pd | 51 +++++++- gcc/testsuite/gcc.dg/tree-ssa/pr126294-2.c | 23 ++++ gcc/testsuite/gcc.dg/tree-ssa/pr126294.c | 187 +++++++++++++++++++++++++++++ 3 files changed, 260 insertions(+), 1 deletion(-) diff --git a/gcc/match.pd b/gcc/match.pd index 59204f98e0e3..9e717a95ae04 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1255,8 +1255,34 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (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)) - (mult @0 (div! @1 @2)))) + (mult @0 (div! @1 @2)) ))) + + /* Otherwise simplify (t * (C + 1)) / C -> t + t / C, and for exact_div + only (t * (C - 1)) / C -> t - t / C. The latter is wrong for + trunc_div, where the result is t - ceil (t / C). */ + (if (INTEGRAL_TYPE_P (type) + && (div == TRUNC_DIV_EXPR || div == EXACT_DIV_EXPR) + && single_use (@3)) + (with { + wide_int c1 = wi::to_wide (@1); + wide_int c2 = wi::to_wide (@2); + int_range_max vr0, vr1; + } + /* C must be positive; c1 > 0 also rejects c2 + 1 wrapping. */ + (if (wi::gt_p (c2, 0, TYPE_SIGN (type)) + && wi::gt_p (c1, 0, TYPE_SIGN (type)) + && (c1 == c2 + 1 + || (div == EXACT_DIV_EXPR && c1 == c2 - 1)) + /* t * C1 doesn't overflow. */ + && ((TYPE_OVERFLOW_UNDEFINED (type) + && !TYPE_OVERFLOW_SANITIZED (type)) + || (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)))) + (if (c1 == c2 + 1) + (plus @0 (div @0 @2)) + (minus @0 (div @0 @2)))))))) #endif /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v. */ (simplify @@ -1305,6 +1331,29 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) #endif ))))))) +#if GIMPLE +/* Simplify (t * (2^k + 1)) >> k -> t + (t >> k). Unsigned t / 2^k has + already become a shift when ranges are available, so the division + pattern above cannot see it. */ +(simplify + (rshift (mult:s@3 @0 INTEGER_CST@1) INTEGER_CST@2) + (if (INTEGRAL_TYPE_P (type) + && TYPE_UNSIGNED (type) + /* Reject negative or too-large shift counts. */ + && wi::ltu_p (wi::to_widest (@2), TYPE_PRECISION (type))) + (with { + wide_int d = wi::lshift (wi::one (TYPE_PRECISION (type)), + tree_to_uhwi (@2)); + int_range_max vr0, vr1; + } + (if (wi::to_wide (@1) == d + 1 + && gimple_match_range_of_expr (vr0, @0, @3) + && gimple_match_range_of_expr (vr1, @1) + /* t * C1 doesn't overflow. */ + && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) + (plus @0 (rshift @0 @2)))))) +#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/tree-ssa/pr126294-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr126294-2.c new file mode 100644 index 000000000000..aa9e116d2d1b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126294-2.c @@ -0,0 +1,23 @@ +/* With -fwrapv, signed overflow is defined, so a range is required. */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fwrapv -fdump-tree-optimized" } */ +/* { dg-require-effective-target int32plus } */ + +/* Unknown range: t * 19 may wrap, no transformation. */ +int +no_range_signed (int t) +{ + return t * 19 / 18; +} + +/* Known range: transformed. */ +int +range_signed (short b) +{ + int t = b; + return t * 11 / 10; +} + +/* { dg-final { scan-tree-dump "\\* 19;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "\\* 11;" "optimized" } } */ +/* { dg-final { scan-tree-dump " / 10;" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126294.c b/gcc/testsuite/gcc.dg/tree-ssa/pr126294.c new file mode 100644 index 000000000000..c9d15e988289 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126294.c @@ -0,0 +1,187 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-require-effective-target int32plus } */ + +/* Transformed: (t * (C + 1)) / C -> t + t / C. */ + +unsigned +plus_1 (unsigned short b) +{ + unsigned t = b; + return t * 7 / 6; +} + +int +signed_plus_1 (short b) +{ + int t = b; + return t * 11 / 10; +} + +/* Known negative range. */ +int +signed_plus_neg (short b) +{ + int t = b; + if (t >= 0) + return 0; + return t * 25 / 24; +} + +/* Signed division by 2^k is not turned into a shift. */ +int +signed_pow2 (short b) +{ + int t = b; + return t * 9 / 8; +} + +/* No range needed: signed overflow is undefined. */ +int +no_range_signed (int t) +{ + return t * 19 / 18; +} + +/* Unsigned divisor with the sign bit set. t * 0x80000003 only + avoids overflow for t in [0, 1]. */ +unsigned +large_c (unsigned t) +{ + if (t > 1) + return 0; + return t * 0x80000003u / 0x80000002u; +} + +/* Transformed: (t * (2^k + 1)) >> k -> t + (t >> k). */ + +/* Original PR reproducer: unsigned t / 2 has become t >> 1. */ +unsigned +pr126294 (unsigned short b) +{ + unsigned t = b; + return t * 3 / 2; +} + +/* Not transformed: (t * (C - 1)) / C with trunc_div. */ + +unsigned +minus_1 (unsigned short b) +{ + unsigned t = b; + return t * 12 / 13; +} + +int +signed_minus_1 (short b) +{ + int t = b; + return t * 16 / 17; +} + +/* Known non-negative range. */ +int +signed_minus_nonneg (short b) +{ + int t = b; + if (t < 0) + return 0; + return t * 14 / 15; +} + +/* Not transformed: the multiplication may overflow. */ + +unsigned +no_range_unsigned (unsigned t) +{ + return t * 20 / 19; +} + +/* Range known but too wide. */ +unsigned +overflow_range (unsigned t) +{ + if (t > 0x7fffffff) + return 0; + return t * 23 / 22; +} + +unsigned +no_range_shift (unsigned t) +{ + return t * 33 / 32; +} + +/* Not transformed: constants not adjacent. */ + +unsigned +no_transform (unsigned short b) +{ + unsigned t = b; + return t * 5 / 3; +} + +/* Not transformed: the multiplication has another use. */ + +unsigned +multi_use_div (unsigned short b, unsigned *p) +{ + unsigned t = b; + unsigned m = t * 29; + *p = m; + return m / 28; +} + +/* Undefined-overflow path. */ +int +multi_use_signed (int t, int *p) +{ + int m = t * 31; + *p = m; + return m / 30; +} + +/* Shift pattern. */ +unsigned +multi_use_shift (unsigned short b, unsigned *p) +{ + unsigned t = b; + unsigned m = t * 65; + *p = m; + return m / 64; +} + +/* Transformed: (t * (C + 1)) / C -> t + t / C. */ +/* { dg-final { scan-tree-dump-not "\\* 7;" "optimized" } } */ +/* { dg-final { scan-tree-dump " / 6;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "\\* 11;" "optimized" } } */ +/* { dg-final { scan-tree-dump " / 10;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "\\* 25;" "optimized" } } */ +/* { dg-final { scan-tree-dump " / 24;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "\\* 9;" "optimized" } } */ +/* { dg-final { scan-tree-dump " / 8;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "\\* 19;" "optimized" } } */ +/* { dg-final { scan-tree-dump " / 18;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "\\* 2147483651;" "optimized" } } */ + +/* Transformed: (t * (2^k + 1)) >> k -> t + (t >> k). */ +/* { dg-final { scan-tree-dump-not "\\* 3;" "optimized" } } */ +/* { dg-final { scan-tree-dump " >> 1;" "optimized" } } */ + +/* Not transformed: (t * (C - 1)) / C with trunc_div. */ +/* { dg-final { scan-tree-dump "\\* 12;" "optimized" } } */ +/* { dg-final { scan-tree-dump "\\* 16;" "optimized" } } */ +/* { dg-final { scan-tree-dump "\\* 14;" "optimized" } } */ + +/* Not transformed: the multiplication may overflow. */ +/* { dg-final { scan-tree-dump "\\* 20;" "optimized" } } */ +/* { dg-final { scan-tree-dump "\\* 23;" "optimized" } } */ +/* { dg-final { scan-tree-dump "\\* 33;" "optimized" } } */ + +/* Not transformed: constants not adjacent. */ +/* { dg-final { scan-tree-dump "\\* 5;" "optimized" } } */ + +/* Not transformed: the multiplication has another use. */ +/* { dg-final { scan-tree-dump "\\* 29;" "optimized" } } */ +/* { dg-final { scan-tree-dump "\\* 31;" "optimized" } } */ +/* { dg-final { scan-tree-dump "\\* 65;" "optimized" } } */
