public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR tree-optimization/92342: Optimize b & -(a==c) in match.pd
@ 2023-01-03 13:17 Roger Sayle
  2023-01-12 13:10 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Roger Sayle @ 2023-01-03 13:17 UTC (permalink / raw)
  To: 'GCC Patches', 'Andrew Pinski'; +Cc: 'Richard Biener'

[-- Attachment #1: Type: text/plain, Size: 1615 bytes --]


This patch is an update/tweak of Andrew Pinski's two patches for
PR tree-optimization/92342, that were originally posted back in November:
https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585111.html
https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585112.html

Technically, the first of those was approved by Richard Biener, though
never committed, and my first thought was to simply push it for Andrew,
but the review of the second piece expressed concerns over comparisons
in non-integral modes, where the result may not be zero-one valued.
Indeed both transformations misbehave in the presence of vector mode
comparisons (these transformations are already implemented for
vec_cond elsewhere in match.pd), so my minor contribution is to limit
these new transformations to scalars, by testing that both the operands
and results are INTEGRAL_TYPE_P.

This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check, both with and without --target_board=unix{-m32},
with no new failures.  Ok for mainline?


2023-01-03  Andrew Pinski  <apinski@marvell.com>
	    Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog:
	PR tree-optimization/92342
	* match.pd ((m1 CMP m2) * d -> (m1 CMP m2) ? d : 0):
	Use tcc_comparison and :c for the multiply.
	(b & -(a CMP c) -> (a CMP c)?b:0): New pattern.

gcc/testsuite/ChangeLog:
	PR tree-optimization/92342
	* gcc.dg/tree-ssa/andnegcmp-1.c: New test.
	* gcc.dg/tree-ssa/andnegcmp-2.c: New test.
	* gcc.dg/tree-ssa/multcmp-1.c: New test.
	* gcc.dg/tree-ssa/multcmp-1.c: New test.


Thanks in advance (and thanks to Andrew).
Roger
--


[-- Attachment #2: patchap2.txt --]
[-- Type: text/plain, Size: 3028 bytes --]

diff --git a/gcc/match.pd b/gcc/match.pd
index 697d8de..2e1f113 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2076,10 +2076,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
 (if (!canonicalize_math_p ())
- (for cmp (gt lt ge le)
+ (for cmp (tcc_comparison)
   (simplify
-   (mult (convert (cmp @0 @1)) @2)
-   (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
+   (mult:c (convert (cmp@0 @1 @2)) @3)
+   (if (INTEGRAL_TYPE_P (type)
+	&& INTEGRAL_TYPE_P (TREE_TYPE (@0)))
+     (cond @0 @3 { build_zero_cst (type); })))
+/* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
+  (simplify
+   (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
+   (if (INTEGRAL_TYPE_P (type)
+	&& INTEGRAL_TYPE_P (TREE_TYPE (@0)))
+     (cond @0 @3 { build_zero_cst (type); })))
+ )
+)
 
 /* For integral types with undefined overflow and C != 0 fold
    x * C EQ/NE y * C into x EQ/NE y.  */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
new file mode 100644
index 0000000..6f16783
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-1.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/92342 */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 == m2;
+  d = -d;
+  int e = d & c;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
new file mode 100644
index 0000000..0e25c8a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/andnegcmp-2.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/92342 */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 < m2;
+  d = -d;
+  int e = c & d;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
new file mode 100644
index 0000000..fb44cac
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-1.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 == m2;
+  int e = d * c;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
new file mode 100644
index 0000000..be38b2e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/multcmp-2.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+f (int m1, int m2, int c)
+{
+  int d = m1 != m2;
+  int e = c * d;
+  return e;
+}
+
+/* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */

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

* Re: [PATCH] PR tree-optimization/92342: Optimize b & -(a==c) in match.pd
  2023-01-03 13:17 [PATCH] PR tree-optimization/92342: Optimize b & -(a==c) in match.pd Roger Sayle
@ 2023-01-12 13:10 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-01-12 13:10 UTC (permalink / raw)
  To: Roger Sayle; +Cc: GCC Patches, Andrew Pinski

On Tue, Jan 3, 2023 at 2:17 PM Roger Sayle <roger@nextmovesoftware.com> wrote:
>
>
> This patch is an update/tweak of Andrew Pinski's two patches for
> PR tree-optimization/92342, that were originally posted back in November:
> https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585111.html
> https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585112.html
>
> Technically, the first of those was approved by Richard Biener, though
> never committed, and my first thought was to simply push it for Andrew,
> but the review of the second piece expressed concerns over comparisons
> in non-integral modes, where the result may not be zero-one valued.
> Indeed both transformations misbehave in the presence of vector mode
> comparisons (these transformations are already implemented for
> vec_cond elsewhere in match.pd), so my minor contribution is to limit
> these new transformations to scalars, by testing that both the operands
> and results are INTEGRAL_TYPE_P.
>
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> and make -k check, both with and without --target_board=unix{-m32},
> with no new failures.  Ok for mainline?

OK.

Thanks,
Richard.

>
> 2023-01-03  Andrew Pinski  <apinski@marvell.com>
>             Roger Sayle  <roger@nextmovesoftware.com>
>
> gcc/ChangeLog:
>         PR tree-optimization/92342
>         * match.pd ((m1 CMP m2) * d -> (m1 CMP m2) ? d : 0):
>         Use tcc_comparison and :c for the multiply.
>         (b & -(a CMP c) -> (a CMP c)?b:0): New pattern.
>
> gcc/testsuite/ChangeLog:
>         PR tree-optimization/92342
>         * gcc.dg/tree-ssa/andnegcmp-1.c: New test.
>         * gcc.dg/tree-ssa/andnegcmp-2.c: New test.
>         * gcc.dg/tree-ssa/multcmp-1.c: New test.
>         * gcc.dg/tree-ssa/multcmp-1.c: New test.
>
>
> Thanks in advance (and thanks to Andrew).
> Roger
> --
>

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

end of thread, other threads:[~2023-01-12 13:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-03 13:17 [PATCH] PR tree-optimization/92342: Optimize b & -(a==c) in match.pd Roger Sayle
2023-01-12 13:10 ` 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).