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. Exclude zero and minus one as inner constants, matching
the restrictions of the subsequent multiplication reassociation rule.
Let the existing simplifications reassociate and combine the constants.
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.
Signed-off-by: Dmitrii Vovdenko <[email protected]>
---
Hello,
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.
Six regression tests cover complete and partial cancellation, 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 | 26 +++++++
.../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 +++++++++++++++++++
7 files changed, 224 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
diff --git a/gcc/match.pd b/gcc/match.pd
index f3d9e55f139..5e4e3b15e66 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1125,6 +1125,32 @@ 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 * 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))
+ && !integer_zerop (@1)
+ && !integer_minus_onep (@1)
+ && !integer_zerop (@3)
+ && (op == MULT_EXPR
+ ? wi::fits_to_tree_p (wi::to_widest (@1) * wi::to_widest (@3), type)
+ : 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;
+}
--
2.43.0