tree-optimization/103514 Missing XOR-EQ-AND Optimization

Message ID BN6PR21MB01629E8E6E8B71D7F1553530A34B9@BN6PR21MB0162.namprd21.prod.outlook.com
State New
Headers
Series tree-optimization/103514 Missing XOR-EQ-AND Optimization |

Commit Message

Navid Rahimi Jan. 5, 2022, 8:12 p.m. UTC
  Hi GCC community,

This patch will add the missed pattern described in bug 103514 [1] to the match.pd. [1] includes proof of correctness for the patch too. 

PR tree-optimization/103514
	* match.pd (a & b) ^ (a == b) -> !(a | b): New optimization.
	* match.pd (a & b) == (a ^ b) -> !(a | b): New optimization.
	* gcc.dg/tree-ssa/pr103514.c: Testcase for this optimization.

1) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103514

Best wishes,
Navid.
  

Comments

Jeff Law Jan. 28, 2022, 10:14 p.m. UTC | #1
On 1/5/2022 1:12 PM, Navid Rahimi via Gcc-patches wrote:
> Hi GCC community,
>
> This patch will add the missed pattern described in bug 103514 [1] to the match.pd. [1] includes proof of correctness for the patch too.
>
> PR tree-optimization/103514
> 	* match.pd (a & b) ^ (a == b) -> !(a | b): New optimization.
> 	* match.pd (a & b) == (a ^ b) -> !(a | b): New optimization.
> 	* gcc.dg/tree-ssa/pr103514.c: Testcase for this optimization.
>
> 1) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103514
Note the bug was filed an fixed during stage3, review just didn't happen 
in a reasonable timeframe.

I'm going to ACK this for the trunk and go ahead and commit it for you.

Thanks for your patience,
jeff
  

Patch

From 7bc34478cc3a494bb634625281b5f03e43f210a9 Mon Sep 17 00:00:00 2001
From: Navid Rahimi <navidrahimi@microsoft.com>
Date: Wed, 1 Dec 2021 00:00:54 -0800
Subject: [PATCH] tree-optimization/103514 Missing XOR-EQ-AND Optimization

	* match.pd (a & b) ^ (a == b) -> !(a | b): New optimization.
	* match.pd (a & b) == (a ^ b) -> !(a | b): New optimization.
	* gcc.dg/tree-ssa/pr103514.c: Testcase for this optimization.
---
 gcc/match.pd                             |  8 ++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr103514.c | 33 ++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr103514.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 0d7b8dd1334..df55206d2ec 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1768,6 +1768,14 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (negate (nop_convert? (bit_not @0)))
  (plus (view_convert @0) { build_each_one_cst (type); }))
 
+/* (a & b) ^ (a == b) -> !(a | b) */
+/* (a & b) == (a ^ b) -> !(a | b) */
+(for first_op (bit_xor eq)
+     second_op (eq bit_xor)
+ (simplify
+  (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
+    (bit_not (bit_ior @0 @1))))
+
 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
 (simplify
  (bit_not (convert? (minus @0 integer_each_onep)))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr103514.c b/gcc/testsuite/gcc.dg/tree-ssa/pr103514.c
new file mode 100644
index 00000000000..5942ad37bf0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr103514.c
@@ -0,0 +1,33 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+#include <stdbool.h>
+
+bool
+i (bool a, bool b)
+{
+     return (a & b) ^ (a == b);
+}
+
+bool
+j (bool a, bool b)
+{
+     return (a & b) == (a ^ b);
+}
+
+bool
+g (bool a, bool b)
+{
+    return (a && b) == (a ^ b); 
+}
+
+bool
+h (bool a, bool b)
+{
+     return (a && b) ^ (a == b);
+}
+
+
+/* Make sure we have removed "==" and "^" and "&". */
+/* { dg-final { scan-tree-dump-not "&" "optimized"} } */
+/* { dg-final { scan-tree-dump-not "\\^"  "optimized"} } */
+/* { dg-final { scan-tree-dump-not "==" "optimized"} } */
\ No newline at end of file
-- 
2.25.1