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 equal-constant division, different-constant
division, and multiplication, producing the simplified result directly.
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]>
---
Hello,
Changes since v2:
Split the rule into three explicit patterns: division with equal constants,
division with different constants,
and multiplication. Produce the simplified result directly instead of relying
on subsequent reassociation and
folding. Both division patterns handle trunc_div and exact_div.
The different-constant division and multiplication rules must now exclude inner
constants 0 and -1.
These are correctness checks: the new transformation is invalid in general for
either value because
the introduced intermediate (T)X * Y can overflow on an execution where the
original expression is defined.
Equal-constant division needs only the nonzero check.
I would like to submit this match.pd optimization for inclusion in GCC.
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 guarded GIMPLE patterns, as described above.
The patterns are restricted to GIMPLE because they rely 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.
Testing status:
Bootstrapped and regression-tested on x86_64-pc-linux-gnu with all default
languages (C, C++, Fortran and Objective-C), --enable-checking=yes and
--disable-multilib, comparing against an identically configured unpatched
build at the same revision.
There are no new regressions in the gcc, g++, gfortran, objc, libstdc++,
libatomic, libitm and libgomp testsuites. Some tests and a feature probe
timed out during host suspension; isolated reruns on both compilers
matched with no unexpected failures. The remaining failures and XPASS
results match the baseline. All seven new tests pass (29 checks).
The patch is based on d5b9d3a458a58766d03445441e3952d43824c402. There
is no associated Bugzilla PR for this submission.
OK for trunk? I do not have write access; could someone commit this
on my behalf if approved?
Thank you,
Dmitrii Vovdenko
gcc/match.pd | 69 ++++++++++++++++++
.../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, 307 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..2a50d7de86c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1125,6 +1125,75 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
wi::exact_log2 (wi::to_wide (@1))); }))))
#endif
+#if GIMPLE
+/* Simplify (T)(X * C) * Y / (T)C -> (T)X * Y, where C is nonzero.
+ This is valid when T and the type of X are signed with undefined
+ overflow and T is not narrower. On defined executions, the quotient
+ is representable in T, so the replacement does not introduce overflow.
+ 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) @@1)
+ (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))
+ (mult (convert @0) @2))))
+
+/* Simplify (T)(X * C1) * Y / C2 -> ((T)X * Y) * ((T)C1 / C2), where
+ C1 and C2 differ, C2 is nonzero, and C1 is an exact multiple of 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. */
+(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 (@1)
+ && !integer_minus_onep (@1)
+ && !integer_zerop (@3)
+ && wi::to_widest (@1) != wi::to_widest (@3))
+ (with { widest_int quot; }
+ (if (wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@3),
+ SIGNED, ")
+ && 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 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