https://gcc.gnu.org/g:28c87896d4511c47aba149dd8dba29837a134131

commit r17-6491-g28c87896d4511c47aba149dd8dba29837a134131
Author: Dmitrii Vovdenko <[email protected]>
Date:   Fri Nov 20 13:46:50 2026 +0300

    match: Expose constant factors through signed widening conversions
    
    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.
    
    Combine the constants directly 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
    and, when the constants differ, a quotient that fits the destination type.
    For multiplication, require the product of the constants to fit the
    destination type.
    Use separate patterns for division and multiplication, producing the
    simplified result directly.  Handle equal constants first in the division
    pattern.
    
    gcc/ChangeLog:
    
            * match.pd: Combine constant factors across 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]>

Diff:
---
 gcc/match.pd                                       | 53 ++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-1.c | 35 +++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-2.c | 29 +++++++++
 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-3.c | 18 ++++++
 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-4.c |  8 +++
 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-5.c | 35 +++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-6.c | 73 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/widen-mult-const-7.c | 40 ++++++++++++
 8 files changed, 291 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index ddc8ab7f224d..fa163a36bf53 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1174,6 +1174,59 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
                               wi::exact_log2 (wi::to_wide (@1))); }))))
 #endif
 
+#if GIMPLE
+/* Simplify (T)(X * C1) * Y / C2 -> ((T)X * Y) * ((T)C1 / C2), where
+   C2 is nonzero and C1 is an exact multiple of C2.  When C1 equals C2,
+   simplify to (T)X * Y instead.
+   This is valid when T and the type of X are signed with undefined
+   overflow and T is not narrower.  When C1 and C2 differ, exclude C1
+   equal to zero or minus one so that (T)X * Y cannot overflow on defined
+   executions, and require C1 / C2 to fit in T.  Exclude overflow
+   instrumentation and require single-use intermediates to avoid
+   duplicating computations.  */
+(for div (trunc_div exact_div)
+ (simplify
+  (div (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))
+       && !integer_zerop (@3))
+   (if (wi::to_widest (@1) == wi::to_widest (@3))
+    (mult (convert @0) @2)
+    (with { widest_int quot; }
+     (if (!integer_zerop (@1)
+         && !integer_minus_onep (@1)
+         && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@3),
+                               SIGNED, &quot)
+         && wi::fits_to_tree_p (quot, type))
+      (mult (mult (convert @0) @2) { wide_int_to_tree (type, quot); })))))))
+
+/* Simplify (T)(X * C1) * Y * C2 -> ((T)X * Y) * ((T)C1 * C2).
+   This is valid when T and the type of X are signed with undefined
+   overflow and T is not narrower.  Exclude C1 equal to zero or minus one
+   so that (T)X * Y cannot overflow on defined executions, and require
+   C1 * C2 to fit in T.  Exclude overflow instrumentation and require
+   single-use intermediates to avoid duplicating computations.  */
+(simplify
+ (mult (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))
+      && !integer_zerop (@1)
+      && !integer_minus_onep (@1))
+  (with { widest_int prod = wi::to_widest (@1) * wi::to_widest (@3); }
+   (if (wi::fits_to_tree_p (prod, type))
+    (mult (mult (convert @0) @2) { wide_int_to_tree (type, prod); })))))
+#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 000000000000..cd24da3dc69e
--- /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 000000000000..fd5db2d5ea83
--- /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 000000000000..28df037b5160
--- /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 000000000000..f9167e6831c9
--- /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 000000000000..d2c274c547ae
--- /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 000000000000..2e0de2ef62e9
--- /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 000000000000..0356ff4d4417
--- /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" } } */

Reply via email to