public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] For the `-A CMP -B -> B CMP A` pattern allow EQ/NE for all integer types
@ 2023-06-06 21:07 Andrew Pinski
  2023-06-07  2:53 ` Jeff Law
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2023-06-06 21:07 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

I noticed while looking at some code generation issue, that forwprop
was not handling `-a == 0` for unsigned types and I was confused why
it was not.
r6-1814-g66e1cacf608045 removed these from fold because they
were supposed to be already handled by the match.pd patterns
but it was missed that the match.pd patterns checked
TYPE_OVERFLOW_UNDEFINED while fold didn't do that for NE/EQ.
This patch removes the restriction on NE/EQ on TYPE_OVERFLOW_UNDEFINED.

OK? Bootstrapped and tested on x86_64-linux-gnu.

gcc/ChangeLog:

	PR tree-optimization/110134
	* match.pd (-A CMP -B -> B CMP A): Allow EQ/NE for all integer
	types.
	(-A CMP CST -> B CMP (-CST)): Likewise.

gcc/testsuite/ChangeLog:

	PR tree-optimization/110134
	* gcc.dg/tree-ssa/negneq-1.c: New test.
	* gcc.dg/tree-ssa/negneq-2.c: New test.
	* gcc.dg/tree-ssa/negneq-3.c: New test.
	* gcc.dg/tree-ssa/negneq-4.c: New test.
---
 gcc/match.pd                             |  8 ++++++--
 gcc/testsuite/gcc.dg/tree-ssa/negneq-1.c | 17 +++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/negneq-2.c | 17 +++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/negneq-3.c | 20 ++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/negneq-4.c | 20 ++++++++++++++++++++
 5 files changed, 80 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/negneq-1.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/negneq-2.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/negneq-3.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/negneq-4.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 16482b741ea..f9cbd757752 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5896,13 +5896,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (cmp (negate @0) (negate @1))
   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
-	   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
+	   && (cmp == EQ_EXPR
+	       || cmp == NE_EXPR
+	       || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
    (scmp @0 @1)))
  (simplify
   (cmp (negate @0) CONSTANT_CLASS_P@1)
   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
-	   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
+	   && (cmp == EQ_EXPR
+	       || cmp == NE_EXPR
+	       || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
     (if (tem && !TREE_OVERFLOW (tem))
      (scmp @0 { tem; }))))))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negneq-1.c b/gcc/testsuite/gcc.dg/tree-ssa/negneq-1.c
new file mode 100644
index 00000000000..94ff57d511b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/negneq-1.c
@@ -0,0 +1,17 @@
+/* PR tree-optimization/110134 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fu(unsigned a)
+{
+  a = -a;
+  return a != 0;
+}
+int fs(signed a)
+{
+  a = -a;
+  return a != 0;
+}
+
+/* We should have optimized out the a = -a; statements. */
+/* { dg-final { scan-tree-dump-not "= -a" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negneq-2.c b/gcc/testsuite/gcc.dg/tree-ssa/negneq-2.c
new file mode 100644
index 00000000000..9a3bb481ce4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/negneq-2.c
@@ -0,0 +1,17 @@
+/* PR tree-optimization/110134 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fu(unsigned a)
+{
+  a = -a;
+  return a == 1;
+}
+int fs(signed a)
+{
+  a = -a;
+  return a == 1;
+}
+
+/* We should have optimized out the a = -a; statements. */
+/* { dg-final { scan-tree-dump-not "= -a" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negneq-3.c b/gcc/testsuite/gcc.dg/tree-ssa/negneq-3.c
new file mode 100644
index 00000000000..14546a606c3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/negneq-3.c
@@ -0,0 +1,20 @@
+/* PR tree-optimization/110134 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fu(unsigned a, unsigned b)
+{
+  a = -a;
+  b = -b;
+  return a == b;
+}
+int fs(signed a, signed b)
+{
+  a = -a;
+  b = -b;
+  return a == b;
+}
+
+/* We should have optimized out the a = -; statements. */
+/* { dg-final { scan-tree-dump-not "= -a" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "= -b" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negneq-4.c b/gcc/testsuite/gcc.dg/tree-ssa/negneq-4.c
new file mode 100644
index 00000000000..1b66854acae
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/negneq-4.c
@@ -0,0 +1,20 @@
+/* PR tree-optimization/110134 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fu(unsigned a, unsigned b)
+{
+  a = -a;
+  b = -b;
+  return a != b;
+}
+int fs(signed a, signed b)
+{
+  a = -a;
+  b = -b;
+  return a != b;
+}
+
+/* We should have optimized out the a = -; statements. */
+/* { dg-final { scan-tree-dump-not "= -a" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "= -b" "optimized" } } */
-- 
2.31.1


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

* Re: [PATCH] For the `-A CMP -B -> B CMP A` pattern allow EQ/NE for all integer types
  2023-06-06 21:07 [PATCH] For the `-A CMP -B -> B CMP A` pattern allow EQ/NE for all integer types Andrew Pinski
@ 2023-06-07  2:53 ` Jeff Law
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2023-06-07  2:53 UTC (permalink / raw)
  To: Andrew Pinski, gcc-patches



On 6/6/23 15:07, Andrew Pinski via Gcc-patches wrote:
> I noticed while looking at some code generation issue, that forwprop
> was not handling `-a == 0` for unsigned types and I was confused why
> it was not.
> r6-1814-g66e1cacf608045 removed these from fold because they
> were supposed to be already handled by the match.pd patterns
> but it was missed that the match.pd patterns checked
> TYPE_OVERFLOW_UNDEFINED while fold didn't do that for NE/EQ.
> This patch removes the restriction on NE/EQ on TYPE_OVERFLOW_UNDEFINED.
> 
> OK? Bootstrapped and tested on x86_64-linux-gnu.
> 
> gcc/ChangeLog:
> 
> 	PR tree-optimization/110134
> 	* match.pd (-A CMP -B -> B CMP A): Allow EQ/NE for all integer
> 	types.
> 	(-A CMP CST -> B CMP (-CST)): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR tree-optimization/110134
> 	* gcc.dg/tree-ssa/negneq-1.c: New test.
> 	* gcc.dg/tree-ssa/negneq-2.c: New test.
> 	* gcc.dg/tree-ssa/negneq-3.c: New test.
> 	* gcc.dg/tree-ssa/negneq-4.c: New test.
OK.
jeff

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

end of thread, other threads:[~2023-06-07  2:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-06 21:07 [PATCH] For the `-A CMP -B -> B CMP A` pattern allow EQ/NE for all integer types Andrew Pinski
2023-06-07  2:53 ` Jeff Law

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).