public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] match.pd: reassociate multiplications
@ 2017-07-21 15:56 Alexander Monakov
  2017-07-21 15:56 ` [PATCH v2 2/2] combine successive multiplications by constants Alexander Monakov
  2017-07-25 14:18 ` [PATCH v2 1/2] match.pd: reassociate multiplications Richard Biener
  0 siblings, 2 replies; 4+ messages in thread
From: Alexander Monakov @ 2017-07-21 15:56 UTC (permalink / raw)
  To: gcc-patches

Previous revision here: https://gcc.gnu.org/ml/gcc-patches/2017-07/msg00889.html

Reassociate (X * CST) * Y to (X * Y) * CST, this pushes constants in
multiplication chains to outermost factors, where they can be combined.

Changed in this revision:
- remove !TYPE_OVERFLOW_SANITIZED and !TYPE_SATURATING checks;
 (in previous discussion Richard indicated that introducing false negatives
  in UBSAN by concealing signed overflow is not a concern, and saturating
  types shouldn't appear here because the constant operand should be FIXED_CST)

The checks for @1 being 0 or -1 remain as they are required for correctness,
but since this rule is ordered after the simpler rules that fold X * {0, -1},
those checks are always false at runtime.


	* match.pd ((X * CST) * Y): Reassociate to (X * Y) * CST.
testsuite/
	* gcc.dg/tree-ssa/assoc-2.c: New testcase.

---
 gcc/match.pd                            | 8 ++++++++
 gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c | 8 ++++++++
 2 files changed, 16 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 7f5807c..39e1e5c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2213,6 +2213,14 @@ 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))
+  (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" } } */
-- 
1.8.3.1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] combine successive multiplications by constants
  2017-07-21 15:56 [PATCH v2 1/2] match.pd: reassociate multiplications Alexander Monakov
@ 2017-07-21 15:56 ` Alexander Monakov
  2017-07-25 14:19   ` Richard Biener
  2017-07-25 14:18 ` [PATCH v2 1/2] match.pd: reassociate multiplications Richard Biener
  1 sibling, 1 reply; 4+ messages in thread
From: Alexander Monakov @ 2017-07-21 15:56 UTC (permalink / raw)
  To: gcc-patches

Previous revision here: https://gcc.gnu.org/ml/gcc-patches/2017-07/msg01090.html

Reassociate X * CST1 * CST2 to X * (CST1 * CST2).

Changed in this revision:
- remove the check for @2 being 0 or -1

	* match.pd ((X * CST1) * CST2): Simplify to X * (CST1 * CST2).
testsuite:
	* gcc.dg/tree-ssa/assoc-2.c: Enhance.
	* gcc.dg/tree-ssa/slsr-4.c: Adjust.

---
 gcc/match.pd                            | 13 +++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c | 13 ++++++++++++-
 gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c  |  8 ++------
 3 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 39e1e5c..732b80c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -284,6 +284,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
      { build_zero_cst (type); })))))
 
+/* Combine successive multiplications.  Similar to above, but handling
+   overflow is different.  */
+(simplify
+ (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
+ (with {
+   bool overflow_p;
+   wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
+  }
+  /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
+     otherwise undefined overflow implies that @0 must be zero.  */
+  (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
+   (mult @0 { wide_int_to_tree (type, mul); }))))
+
 /* Optimize A / A to 1.0 if we don't care about
    NaNs or Infinities.  */
 (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c b/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
index a92c882..cc0e9d4 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
@@ -5,4 +5,15 @@ int f0(int a, int b){
   return a * 33 * b * 55;
 }
 
-/* { dg-final { scan-tree-dump-times "mult_expr" 2 "gimple" } } */
+int f1(int a){
+  a *= 33;
+  return a * 55;
+}
+
+int f2(int a, int b){
+  a *= 33;
+  return a * b * 55;
+}
+
+/* { dg-final { scan-tree-dump-times "mult_expr" 7 "gimple" } } */
+/* { dg-final { scan-tree-dump-times "mult_expr" 5 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c b/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c
index 17d7b4c..1e943b7 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c
@@ -23,13 +23,9 @@ f (int i)
   foo (y);
 }
 
-/* { dg-final { scan-tree-dump-times "\\* 4" 1 "slsr" } } */
-/* { dg-final { scan-tree-dump-times "\\* 10" 1 "slsr" } } */
-/* { dg-final { scan-tree-dump-times "\\+ 20;" 1 "slsr" } } */
+/* { dg-final { scan-tree-dump-times "\\* 40" 1 "slsr" } } */
 /* { dg-final { scan-tree-dump-times "\\+ 200" 1 "slsr" } } */
-/* { dg-final { scan-tree-dump-times "\\- 16;" 1 "slsr" } } */
 /* { dg-final { scan-tree-dump-times "\\- 160" 1 "slsr" } } */
-/* { dg-final { scan-tree-dump-times "\\* 4" 1 "optimized" } } */
-/* { dg-final { scan-tree-dump-times "\\* 10" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\\* 40" 1 "optimized" } } */
 /* { dg-final { scan-tree-dump-times "\\+ 200" 1 "optimized" } } */
 /* { dg-final { scan-tree-dump-times "\\+ 40" 1 "optimized" } } */
-- 
1.8.3.1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] match.pd: reassociate multiplications
  2017-07-21 15:56 [PATCH v2 1/2] match.pd: reassociate multiplications Alexander Monakov
  2017-07-21 15:56 ` [PATCH v2 2/2] combine successive multiplications by constants Alexander Monakov
@ 2017-07-25 14:18 ` Richard Biener
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Biener @ 2017-07-25 14:18 UTC (permalink / raw)
  To: Alexander Monakov; +Cc: GCC Patches

On Fri, Jul 21, 2017 at 5:55 PM, Alexander Monakov <amonakov@ispras.ru> wrote:
> Previous revision here: https://gcc.gnu.org/ml/gcc-patches/2017-07/msg00889.html
>
> Reassociate (X * CST) * Y to (X * Y) * CST, this pushes constants in
> multiplication chains to outermost factors, where they can be combined.
>
> Changed in this revision:
> - remove !TYPE_OVERFLOW_SANITIZED and !TYPE_SATURATING checks;
>  (in previous discussion Richard indicated that introducing false negatives
>   in UBSAN by concealing signed overflow is not a concern, and saturating
>   types shouldn't appear here because the constant operand should be FIXED_CST)
>
> The checks for @1 being 0 or -1 remain as they are required for correctness,
> but since this rule is ordered after the simpler rules that fold X * {0, -1},
> those checks are always false at runtime.

Ok.

Thanks,
Richard.

>
>         * match.pd ((X * CST) * Y): Reassociate to (X * Y) * CST.
> testsuite/
>         * gcc.dg/tree-ssa/assoc-2.c: New testcase.
>
> ---
>  gcc/match.pd                            | 8 ++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c | 8 ++++++++
>  2 files changed, 16 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 7f5807c..39e1e5c 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -2213,6 +2213,14 @@ 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))
> +  (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" } } */
> --
> 1.8.3.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] combine successive multiplications by constants
  2017-07-21 15:56 ` [PATCH v2 2/2] combine successive multiplications by constants Alexander Monakov
@ 2017-07-25 14:19   ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2017-07-25 14:19 UTC (permalink / raw)
  To: Alexander Monakov; +Cc: GCC Patches

On Fri, Jul 21, 2017 at 5:55 PM, Alexander Monakov <amonakov@ispras.ru> wrote:
> Previous revision here: https://gcc.gnu.org/ml/gcc-patches/2017-07/msg01090.html
>
> Reassociate X * CST1 * CST2 to X * (CST1 * CST2).
>
> Changed in this revision:
> - remove the check for @2 being 0 or -1

Ok.

Thanks,
Richard.

>         * match.pd ((X * CST1) * CST2): Simplify to X * (CST1 * CST2).
> testsuite:
>         * gcc.dg/tree-ssa/assoc-2.c: Enhance.
>         * gcc.dg/tree-ssa/slsr-4.c: Adjust.
>
> ---
>  gcc/match.pd                            | 13 +++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c | 13 ++++++++++++-
>  gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c  |  8 ++------
>  3 files changed, 27 insertions(+), 7 deletions(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 39e1e5c..732b80c 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -284,6 +284,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>          || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
>       { build_zero_cst (type); })))))
>
> +/* Combine successive multiplications.  Similar to above, but handling
> +   overflow is different.  */
> +(simplify
> + (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
> + (with {
> +   bool overflow_p;
> +   wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
> +  }
> +  /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
> +     otherwise undefined overflow implies that @0 must be zero.  */
> +  (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
> +   (mult @0 { wide_int_to_tree (type, mul); }))))
> +
>  /* Optimize A / A to 1.0 if we don't care about
>     NaNs or Infinities.  */
>  (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c b/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
> index a92c882..cc0e9d4 100644
> --- a/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/assoc-2.c
> @@ -5,4 +5,15 @@ int f0(int a, int b){
>    return a * 33 * b * 55;
>  }
>
> -/* { dg-final { scan-tree-dump-times "mult_expr" 2 "gimple" } } */
> +int f1(int a){
> +  a *= 33;
> +  return a * 55;
> +}
> +
> +int f2(int a, int b){
> +  a *= 33;
> +  return a * b * 55;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "mult_expr" 7 "gimple" } } */
> +/* { dg-final { scan-tree-dump-times "mult_expr" 5 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c b/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c
> index 17d7b4c..1e943b7 100644
> --- a/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/slsr-4.c
> @@ -23,13 +23,9 @@ f (int i)
>    foo (y);
>  }
>
> -/* { dg-final { scan-tree-dump-times "\\* 4" 1 "slsr" } } */
> -/* { dg-final { scan-tree-dump-times "\\* 10" 1 "slsr" } } */
> -/* { dg-final { scan-tree-dump-times "\\+ 20;" 1 "slsr" } } */
> +/* { dg-final { scan-tree-dump-times "\\* 40" 1 "slsr" } } */
>  /* { dg-final { scan-tree-dump-times "\\+ 200" 1 "slsr" } } */
> -/* { dg-final { scan-tree-dump-times "\\- 16;" 1 "slsr" } } */
>  /* { dg-final { scan-tree-dump-times "\\- 160" 1 "slsr" } } */
> -/* { dg-final { scan-tree-dump-times "\\* 4" 1 "optimized" } } */
> -/* { dg-final { scan-tree-dump-times "\\* 10" 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "\\* 40" 1 "optimized" } } */
>  /* { dg-final { scan-tree-dump-times "\\+ 200" 1 "optimized" } } */
>  /* { dg-final { scan-tree-dump-times "\\+ 40" 1 "optimized" } } */
> --
> 1.8.3.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-07-25 14:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-21 15:56 [PATCH v2 1/2] match.pd: reassociate multiplications Alexander Monakov
2017-07-21 15:56 ` [PATCH v2 2/2] combine successive multiplications by constants Alexander Monakov
2017-07-25 14:19   ` Richard Biener
2017-07-25 14:18 ` [PATCH v2 1/2] match.pd: reassociate multiplications Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).