This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] match.pd: reassociate multiplications with constants
On Thu, 13 Jul 2017, Marc Glisse wrote:
> I notice that we do not turn (X*10)*10 into X*100 in GIMPLE [...]
I've completely missed that. Posting another patch to address that.
> Relying on inner expressions being folded can be slightly dangerous,
> especially for generic IIRC. It seems easy enough to check that @1 is neither
> 0 nor -1 for safety.
Yep - done.
> Probably needs :s on the inner multiplication.
Not 100% sure, but done in this version.
> Unless the test on TYPE_OVERFLOW_SANITIZED etc is shared with adjacent
> transformations, I'd rather put it inside, with the other if, but that's a
> matter of taste.
Done, probably better that way.
> One small testcase please? Or is there already one that is currently failing?
No, I'm not aware of any preexisting testcase. Added.
* match.pd ((X * CST) * Y): Reassociate to (X * Y) * CST.
testsuite/
* gcc.dg/tree-ssa/assoc-2.c: New testcase.
diff --git a/gcc/match.pd b/gcc/match.pd
index 4c64b21..36045f1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2139,6 +2139,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(mult @0 integer_minus_onep)
(negate @0))
+/* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
+ signed overflow for CST != 0 && CST != -1. */
+(simplify
+ (mult:c (mult:s @0 INTEGER_CST@1) @2)
+ (if (TREE_CODE (@2) != INTEGER_CST
+ && !integer_zerop (@1) && !integer_minus_onep (@1)
+ && !TYPE_OVERFLOW_SANITIZED (type) && !TYPE_SATURATING (type))
+ (mult (mult @0 @2) @1)))
+
/* True if we can easily extract the real and imaginary parts of a complex
number. */
(match compositional_complex
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c b/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
new file mode 100644
index 0000000..a92c882
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-gimple-raw -fdump-tree-optimized-raw" } */
+
+int f0(int a, int b){
+ return a * 33 * b * 55;
+}
+
+/* { dg-final { scan-tree-dump-times "mult_expr" 2 "gimple" } } */