On Mon, Nov 2, 2026 at 9:21 AM Dmitrii Vovdenko <[email protected]> wrote: > > A signed widening conversion can hide a constant factor from the existing > multiplication reassociation and constant division rules. For example, > (long long) (a * 30) * b / 6 retains the narrow multiplication and division > instead of becoming ((long long) a * b) * 5. > > Expose the inner multiplication in a multiply/multiply or multiply/divide > chain when both types are signed integral types with undefined overflow > and the conversion does not narrow. Exclude overflow instrumentation and > use the existing single-use restrictions to avoid duplicating producers. > > For division, require a nonzero divisor that divides the inner constant. > For multiplication, require the product of the constants to fit the > destination type. > Let the existing simplifications reassociate and combine the constants.
https://gcc.sashiko.dev/#/patchset/20261102082104.3745544-1-vov.dm.an%40gmail.com explains why I'd like to see the result expression simplified rather than leaving that to recursive simplification. Richard. > gcc/ChangeLog: > > * match.pd: Expose constant multiplication through signed > non-narrowing conversions in multiplication and division chains. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/widen-mult-const-1.c: New test. > * gcc.dg/tree-ssa/widen-mult-const-2.c: Likewise. > * gcc.dg/tree-ssa/widen-mult-const-3.c: Likewise. > * gcc.dg/tree-ssa/widen-mult-const-4.c: Likewise. > * gcc.dg/tree-ssa/widen-mult-const-5.c: Likewise. > * gcc.dg/tree-ssa/widen-mult-const-6.c: Likewise. > * gcc.dg/tree-ssa/widen-mult-const-7.c: Likewise. > > Signed-off-by: Dmitrii Vovdenko <[email protected]> > --- > Hello, > > Changes since v1: > > Added widen-mult-const-7.c using the GIMPLE frontend and explicit __EXACT_DIV > operations, as suggested in review. It covers full cancellation and partial > cancellation, checking the optimized statements and the remaining factor of > 5. The other six tests are unchanged. > > Removed the exclusions for zero and minus one inner factors, and retained the > nonzero outer constant check only for division, following the review > comments. Also corrected the explanatory expression to show the conversion of > C1 explicitly. > > I would appreciate feedback on the correctness conditions and approach > of this match.pd optimization before completing full validation. > > My motivation example have appeared in fixed point arithmetic library. After > inlining, an > example looks like this: > > int foo (int a, int b) > { > return (static_cast<long long>(a * (1 << 16)) * b) / (1 << 16); > } > > On defined executions, it can be simplified to: > > int foo (int a, int b) > { > return static_cast<long long>(a) * b; > } > > The conversion prevents the existing patterns from exposing and > cancelling the constant factor in the original expression. The patch > below addresses this with a guarded GIMPLE pattern, as described above. > The pattern is restricted to GIMPLE because it relies on single-use > checks to avoid duplicating shared multiplications, and the motivating > code only appears after inlining. > > Seven regression tests cover complete and partial cancellation, including > explicit exact division through the GIMPLE frontend, constant > multiplication, negative and commuted operands, wrapping and trapping > arithmetic, shared intermediate values, unsigned sources, and constant > product overflow. Runtime checks exercise signed integer boundaries. > The positive compile tests disable tree reassociation to exercise the > folding patterns independently of that pass. > > This implementation is deliberately narrow. A more general approach > could construct a temporary candidate expression after exposing the > constant, simplify it using the existing machinery, and evaluate its > profitability before committing the transformation. That would add > considerable complexity, and I would appreciate advice on whether it is > warranted. > > Testing status: > > Tested on x86_64-pc-linux-gnu with a non-bootstrapped build (C and C++), > comparing against an unpatched build at the same revision. There are no > new failures in the gcc, g++, libstdc++, libatomic, libitm, and libgomp > testsuites, and the new tests pass. A full bootstrap and regression > test is pending. > > The patch is based on d5b9d3a458a58766d03445441e3952d43824c402. There > is no associated Bugzilla PR for this submission. > > Is exposing the factor through this guarded match.pd pattern an > appropriate approach? Are there additional correctness or profitability > restrictions that should be considered? > > Thank you, > Dmitrii Vovdenko > > gcc/match.pd | 24 +++++++ > .../gcc.dg/tree-ssa/widen-mult-const-1.c | 35 +++++++++ > .../gcc.dg/tree-ssa/widen-mult-const-2.c | 29 ++++++++ > .../gcc.dg/tree-ssa/widen-mult-const-3.c | 18 +++++ > .../gcc.dg/tree-ssa/widen-mult-const-4.c | 8 ++ > .../gcc.dg/tree-ssa/widen-mult-const-5.c | 35 +++++++++ > .../gcc.dg/tree-ssa/widen-mult-const-6.c | 73 +++++++++++++++++++ > .../gcc.dg/tree-ssa/widen-mult-const-7.c | 40 +++++++++++ > 8 files changed, 262 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-1.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-2.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-3.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-4.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-5.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-6.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-7.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index f3d9e55f139..7b32940d1a1 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -1125,6 +1125,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > wi::exact_log2 (wi::to_wide (@1))); })))) > #endif > > +#if GIMPLE > +/* Simplify (T)(X * C1) * Y op C2 -> ((T)X * (T)C1) * Y op C2, where op is a > + multiplication or a division, to expose C1 to the reassociation and > + constant folding patterns. This is valid when T and the type of X are > + signed with undefined overflow and T is not narrower, since X * C1 then > + cannot overflow. Only do this when C1 and C2 can be combined. */ > +(for op (mult trunc_div exact_div) > + (simplify > + (op (mult:cs (convert:s (mult:s @0 INTEGER_CST@1)) @2) INTEGER_CST@3) > + (if (INTEGRAL_TYPE_P (type) > + && INTEGRAL_TYPE_P (TREE_TYPE (@0)) > + && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@0)) > + && TYPE_OVERFLOW_UNDEFINED (type) > + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) > + && !TYPE_OVERFLOW_SANITIZED (type) > + && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) > + && (op == MULT_EXPR > + ? wi::fits_to_tree_p (wi::to_widest (@1) * wi::to_widest (@3), > type) > + : (!integer_zerop (@3) > + && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@3), > + SIGNED)))) > + (op (mult (mult (convert @0) (convert @1)) @2) @3)))) > +#endif > + > (for div (trunc_div ceil_div floor_div round_div exact_div) > /* Simplify (t * u) / u -> t. */ > (simplify > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-1.c > b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-1.c > new file mode 100644 > index 00000000000..cd24da3dc69 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-1.c > @@ -0,0 +1,35 @@ > +/* { dg-do compile } */ > +/* { dg-require-effective-target int32 } */ > +/* { dg-require-effective-target longlong64 } */ > +/* { dg-options "-O2 -fno-tree-reassoc -fdump-tree-optimized-raw" } */ > + > +__attribute__((noipa)) long long > +cancel (int a, long long b) > +{ > + return (long long) (a * 65536) * b / 65536; > +} > + > +__attribute__((noipa)) long long > +partial (int a, long long b) > +{ > + return (long long) (a * 30) * b / 6; > +} > + > +__attribute__((noipa)) long long > +negative (int a, long long b) > +{ > + return (long long) (a * -30) * b / 6; > +} > + > +__attribute__((noipa)) long long > +commuted (int a, long long b) > +{ > + return b * (long long) (-30 * a) / -30; > +} > + > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, > [[:alnum:]_]*_[0-9]+(?:\(D\))?, [[:alnum:]_]*_[0-9]+(?:\(D\))?,} 4 > "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<nop_expr, } 4 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {gimple_return <} 4 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {gimple_[a-z_]+} 14 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, 5,} 1 > "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, -5,} 1 > "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-2.c > b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-2.c > new file mode 100644 > index 00000000000..fd5db2d5ea8 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-2.c > @@ -0,0 +1,29 @@ > +/* { dg-do compile } */ > +/* { dg-require-effective-target int32 } */ > +/* { dg-require-effective-target longlong64 } */ > +/* { dg-options "-O2 -fno-tree-reassoc -fdump-tree-optimized-raw" } */ > + > +__attribute__((noipa)) long long > +multiply (int a, long long b) > +{ > + return (long long) (a * 30) * b * 7; > +} > + > +__attribute__((noipa)) long long > +negative (int a, long long b) > +{ > + return (long long) (a * -30) * b * 7; > +} > + > +__attribute__((noipa)) long long > +commuted (int a, long long b) > +{ > + return -7 * (b * (long long) (-30 * a)); > +} > + > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, > [[:alnum:]_]*_[0-9]+(?:\(D\))?, [[:alnum:]_]*_[0-9]+(?:\(D\))?,} 3 > "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<nop_expr, } 3 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {gimple_return <} 3 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {gimple_[a-z_]+} 12 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, 210,} 2 > "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, -210,} 1 > "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-3.c > b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-3.c > new file mode 100644 > index 00000000000..28df037b516 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-3.c > @@ -0,0 +1,18 @@ > +/* { dg-do compile } */ > +/* { dg-require-effective-target int32 } */ > +/* { dg-require-effective-target longlong64 } */ > +/* { dg-options "-O2 -fwrapv -fdump-tree-optimized-raw" } */ > + > +long long > +divide (int a, long long b) > +{ > + return (long long) (a * 30) * b / 6; > +} > + > +long long > +multiply (int a, long long b) > +{ > + return (long long) (a * 30) * b * 7; > +} > + > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, 30,} 2 > "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-4.c > b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-4.c > new file mode 100644 > index 00000000000..f9167e6831c > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-4.c > @@ -0,0 +1,8 @@ > +/* { dg-do compile } */ > +/* { dg-require-effective-target int32 } */ > +/* { dg-require-effective-target longlong64 } */ > +/* { dg-options "-O2 -ftrapv -fdump-tree-optimized-raw" } */ > + > +#include "widen-mult-const-3.c" > + > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, 30,} 2 > "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-5.c > b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-5.c > new file mode 100644 > index 00000000000..d2c274c547a > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-5.c > @@ -0,0 +1,35 @@ > +/* { dg-do compile } */ > +/* { dg-require-effective-target int32 } */ > +/* { dg-require-effective-target longlong64 } */ > +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ > + > +volatile long long sink; > + > +long long > +mismatch (int a, long long b) > +{ > + return (long long) (a * 30) * b / 7; > +} > + > +long long > +shared (int a, long long b) > +{ > + long long x = (long long) (a * 30); > + sink = x; > + return x * b / 6; > +} > + > +long long > +unsigned_source (unsigned int a, long long b) > +{ > + return (long long) (a * 30U) * b / 6; > +} > + > +long long > +constant_overflow (int a, long long b) > +{ > + return (long long) (a * 2) * b * 4611686018427387904LL; > +} > + > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, 30,} 3 > "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, 2,} 1 > "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-6.c > b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-6.c > new file mode 100644 > index 00000000000..2e0de2ef62e > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-6.c > @@ -0,0 +1,73 @@ > +/* { dg-do run } */ > +/* { dg-require-effective-target int32 } */ > +/* { dg-require-effective-target longlong64 } */ > +/* { dg-options "-O2" } */ > + > +__attribute__((noipa)) long long > +cancel (int a, long long b) > +{ > + return (long long) (a * 65536) * b / 65536; > +} > + > +__attribute__((noipa)) long long > +partial (int a, long long b) > +{ > + return (long long) (a * 30) * b / 6; > +} > + > +__attribute__((noipa)) long long > +multiply (int a, long long b) > +{ > + return (long long) (a * 30) * b * 7; > +} > + > +__attribute__((noipa)) long long > +negative (int a, long long b) > +{ > + return (long long) (a * -30) * b / 6; > +} > + > +__attribute__((noipa)) long long > +constant_overflow (int a, long long b) > +{ > + return (long long) (a * 2) * b * 4611686018427387904LL; > +} > + > +__attribute__((noipa)) long long > +constant_min (int a, long long b) > +{ > + return (long long) (a * -2) * b * 4611686018427387904LL; > +} > + > +int > +main (void) > +{ > + const int inputs[] = {-32768, -32767, -1, 0, 1, 32767}; > + for (unsigned int i = 0; i < sizeof (inputs) / sizeof (inputs[0]); ++i) > + for (unsigned int j = 0; j < sizeof (inputs) / sizeof (inputs[0]); ++j) > + { > + int a = inputs[i]; > + long long b = inputs[j]; > + long long product = (long long) a * b; > + if (cancel (a, b) != product > + || partial (a, b) != product * 5 > + || multiply (a, b) != product * 210 > + || negative (a, b) != product * -5) > + __builtin_abort (); > + } > + > + long long min = -__LONG_LONG_MAX__ - 1; > + if (cancel (-32768, 4294967296LL) != min / 65536 > + || partial (1, __LONG_LONG_MAX__ / 30) > + != (__LONG_LONG_MAX__ / 30) * 5 > + || multiply (1, __LONG_LONG_MAX__ / 210) > + != (__LONG_LONG_MAX__ / 210) * 210 > + || constant_overflow (-1, 1) != min > + || constant_overflow (1, -1) != min > + || constant_overflow (1, 0) != 0 > + || constant_min (1, 1) != min > + || constant_min (-1, -1) != min) > + __builtin_abort (); > + > + return 0; > +} > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-7.c > b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-7.c > new file mode 100644 > index 00000000000..0356ff4d441 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-7.c > @@ -0,0 +1,40 @@ > +/* { dg-do compile } */ > +/* { dg-require-effective-target int32 } */ > +/* { dg-require-effective-target longlong64 } */ > +/* { dg-options "-O2 -fgimple -fno-tree-reassoc -fdump-tree-optimized-raw" } > */ > + > +long long __GIMPLE > +cancel_exact (int a, long long b) > +{ > + int narrow; > + long long wide; > + long long product; > + long long result; > + > + narrow = a * 65536; > + wide = (long long) narrow; > + product = wide * b; > + result = product __EXACT_DIV 65536LL; > + return result; > +} > + > +long long __GIMPLE > +partial_exact (int a, long long b) > +{ > + int narrow; > + long long wide; > + long long product; > + long long result; > + > + narrow = a * 30; > + wide = (long long) narrow; > + product = wide * b; > + result = product __EXACT_DIV 6LL; > + return result; > +} > + > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, > [[:alnum:]_]*_[0-9]+(?:\(D\))?, [[:alnum:]_]*_[0-9]+(?:\(D\))?,} 2 > "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<nop_expr, } 2 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {gimple_return <} 2 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {gimple_[a-z_]+} 7 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times {<mult_expr, [^,]*, [^,]*, 5,} 1 > "optimized" } } */ > -- > 2.43.0 >
