public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-optimization/103514 Missing XOR-EQ-AND Optimization
@ 2022-01-05 20:12 Navid Rahimi
  2022-01-28 22:14 ` Jeff Law
  0 siblings, 1 reply; 9+ messages in thread
From: Navid Rahimi @ 2022-01-05 20:12 UTC (permalink / raw)
  To: gcc-patches

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

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.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-tree-optimization-103514-Missing-XOR-EQ-AND-Opt-v4.patch --]
[-- Type: text/x-patch; name="0001-tree-optimization-103514-Missing-XOR-EQ-AND-Opt-v4.patch", Size: 2163 bytes --]

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


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

* Re: [PATCH] tree-optimization/103514 Missing XOR-EQ-AND Optimization
  2022-01-05 20:12 [PATCH] tree-optimization/103514 Missing XOR-EQ-AND Optimization Navid Rahimi
@ 2022-01-28 22:14 ` Jeff Law
  2022-01-29 16:46   ` [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514] Jakub Jelinek
  0 siblings, 1 reply; 9+ messages in thread
From: Jeff Law @ 2022-01-28 22:14 UTC (permalink / raw)
  To: Navid Rahimi, gcc-patches



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


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

* [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514]
  2022-01-28 22:14 ` Jeff Law
@ 2022-01-29 16:46   ` Jakub Jelinek
  2022-01-30 10:16     ` [EXTERNAL] " Navid Rahimi
  2022-01-31  8:30     ` Richard Biener
  0 siblings, 2 replies; 9+ messages in thread
From: Jakub Jelinek @ 2022-01-29 16:46 UTC (permalink / raw)
  To: Richard Biener, Jeff Law; +Cc: Navid Rahimi, gcc-patches

On Fri, Jan 28, 2022 at 03:14:16PM -0700, Jeff Law via Gcc-patches wrote:
> > 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.

The testcase FAILs on short-circuit targets like powerpc64le-linux.
While the first 2 functions are identical, the last two look like:
  <bb 2> :
  if (a_5(D) != 0)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  if (b_6(D) != 0)
    goto <bb 5>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 4> :

  <bb 5> :
  # iftmp.1_4 = PHI <1(3), 0(4)>
  _1 = a_5(D) == b_6(D);
  _2 = (int) _1;
  _3 = _2 ^ iftmp.1_4;
  _9 = _2 != iftmp.1_4;
  return _9;
instead of the expected:
  <bb 2> :
  _3 = a_8(D) & b_9(D);
  _4 = (int) _3;
  _5 = a_8(D) == b_9(D);
  _6 = (int) _5;
  _1 = a_8(D) | b_9(D);
  _2 = ~_1;
  _7 = (int) _2;
  _10 = ~_1;
  return _10;
so no wonder it doesn't match.  E.g. x86_64-linux will also use jumps
if it isn't just a && b but a && b && c && d (will do
a & b and c & d tests and jump based on those.

As it is too late to implement this optimization even for the short
circuiting targets this late (not even sure which pass would be best),
this patch just forces non-short-circuiting for the test.

Tested on x86_64-linux -m32/-m64 and powerpc64le-linux, ok for trunk?

2022-01-29  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/103514
	* gcc.dg/tree-ssa/pr103514.c: Add
	--param logical-op-non-short-circuit=1 to dg-options.

--- gcc/testsuite/gcc.dg/tree-ssa/pr103514.c.jj	2022-01-29 11:11:39.338627697 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr103514.c	2022-01-29 17:37:18.255237211 +0100
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O -fdump-tree-optimized" } */
+/* { dg-options "-O --param logical-op-non-short-circuit=1 -fdump-tree-optimized" } */
 #include <stdbool.h>
 
 bool
@@ -30,4 +30,4 @@ h (bool a, bool 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
+/* { dg-final { scan-tree-dump-not "==" "optimized"} } */


	Jakub


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

* Re: [EXTERNAL] [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514]
  2022-01-29 16:46   ` [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514] Jakub Jelinek
@ 2022-01-30 10:16     ` Navid Rahimi
  2022-01-31 10:12       ` Jakub Jelinek
  2022-01-31  8:30     ` Richard Biener
  1 sibling, 1 reply; 9+ messages in thread
From: Navid Rahimi @ 2022-01-30 10:16 UTC (permalink / raw)
  To: Jakub Jelinek, Richard Biener, Jeff Law; +Cc: gcc-patches

Thanks Jakob for the correction. Sadly, I didn’t have any access to any non x86 architecture. But x86 was fully tested and there was no regression.

In my spare time I will look at implementation of this for short-circuit targets.

Best wishes,
Navid.
________________________________
From: Jakub Jelinek <jakub@redhat.com>
Sent: Saturday, January 29, 2022 8:46:09 AM
To: Richard Biener <rguenther@suse.de>; Jeff Law <jeffreyalaw@gmail.com>
Cc: Navid Rahimi <navidrahimi@microsoft.com>; gcc-patches@gcc.gnu.org <gcc-patches@gcc.gnu.org>
Subject: [EXTERNAL] [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514]

[You don't often get email from jakub@redhat.com. Learn why this is important at http://aka.ms/LearnAboutSenderIdentification.]

On Fri, Jan 28, 2022 at 03:14:16PM -0700, Jeff Law via Gcc-patches wrote:
> > 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://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fbugzilla%2Fshow_bug.cgi%3Fid%3D103514&amp;data=04%7C01%7Cnavidrahimi%40microsoft.com%7C712766ef9fc24c7ffeda08d9e346e086%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637790716153978385%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&amp;sdata=CGslhJuy%2BTSrPpYbALD9pBh9945Hl6lINeHKmTEWqK0%3D&amp;reserved=0
> 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.

The testcase FAILs on short-circuit targets like powerpc64le-linux.
While the first 2 functions are identical, the last two look like:
  <bb 2> :
  if (a_5(D) != 0)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  if (b_6(D) != 0)
    goto <bb 5>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 4> :

  <bb 5> :
  # iftmp.1_4 = PHI <1(3), 0(4)>
  _1 = a_5(D) == b_6(D);
  _2 = (int) _1;
  _3 = _2 ^ iftmp.1_4;
  _9 = _2 != iftmp.1_4;
  return _9;
instead of the expected:
  <bb 2> :
  _3 = a_8(D) & b_9(D);
  _4 = (int) _3;
  _5 = a_8(D) == b_9(D);
  _6 = (int) _5;
  _1 = a_8(D) | b_9(D);
  _2 = ~_1;
  _7 = (int) _2;
  _10 = ~_1;
  return _10;
so no wonder it doesn't match.  E.g. x86_64-linux will also use jumps
if it isn't just a && b but a && b && c && d (will do
a & b and c & d tests and jump based on those.

As it is too late to implement this optimization even for the short
circuiting targets this late (not even sure which pass would be best),
this patch just forces non-short-circuiting for the test.

Tested on x86_64-linux -m32/-m64 and powerpc64le-linux, ok for trunk?

2022-01-29  Jakub Jelinek  <jakub@redhat.com>

        PR tree-optimization/103514
        * gcc.dg/tree-ssa/pr103514.c: Add
        --param logical-op-non-short-circuit=1 to dg-options.

--- gcc/testsuite/gcc.dg/tree-ssa/pr103514.c.jj 2022-01-29 11:11:39.338627697 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/pr103514.c    2022-01-29 17:37:18.255237211 +0100
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O -fdump-tree-optimized" } */
+/* { dg-options "-O --param logical-op-non-short-circuit=1 -fdump-tree-optimized" } */
 #include <stdbool.h>

 bool
@@ -30,4 +30,4 @@ h (bool a, bool 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
+/* { dg-final { scan-tree-dump-not "==" "optimized"} } */


        Jakub


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

* Re: [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514]
  2022-01-29 16:46   ` [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514] Jakub Jelinek
  2022-01-30 10:16     ` [EXTERNAL] " Navid Rahimi
@ 2022-01-31  8:30     ` Richard Biener
  1 sibling, 0 replies; 9+ messages in thread
From: Richard Biener @ 2022-01-31  8:30 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Jeff Law, Navid Rahimi, gcc-patches

On Sat, 29 Jan 2022, Jakub Jelinek wrote:

> On Fri, Jan 28, 2022 at 03:14:16PM -0700, Jeff Law via Gcc-patches wrote:
> > > 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.
> 
> The testcase FAILs on short-circuit targets like powerpc64le-linux.
> While the first 2 functions are identical, the last two look like:
>   <bb 2> :
>   if (a_5(D) != 0)
>     goto <bb 3>; [INV]
>   else
>     goto <bb 4>; [INV]
> 
>   <bb 3> :
>   if (b_6(D) != 0)
>     goto <bb 5>; [INV]
>   else
>     goto <bb 4>; [INV]
> 
>   <bb 4> :
> 
>   <bb 5> :
>   # iftmp.1_4 = PHI <1(3), 0(4)>
>   _1 = a_5(D) == b_6(D);
>   _2 = (int) _1;
>   _3 = _2 ^ iftmp.1_4;
>   _9 = _2 != iftmp.1_4;
>   return _9;
> instead of the expected:
>   <bb 2> :
>   _3 = a_8(D) & b_9(D);
>   _4 = (int) _3;
>   _5 = a_8(D) == b_9(D);
>   _6 = (int) _5;
>   _1 = a_8(D) | b_9(D);
>   _2 = ~_1;
>   _7 = (int) _2;
>   _10 = ~_1;
>   return _10;
> so no wonder it doesn't match.  E.g. x86_64-linux will also use jumps
> if it isn't just a && b but a && b && c && d (will do
> a & b and c & d tests and jump based on those.
> 
> As it is too late to implement this optimization even for the short
> circuiting targets this late (not even sure which pass would be best),
> this patch just forces non-short-circuiting for the test.
> 
> Tested on x86_64-linux -m32/-m64 and powerpc64le-linux, ok for trunk?

OK.

> 2022-01-29  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR tree-optimization/103514
> 	* gcc.dg/tree-ssa/pr103514.c: Add
> 	--param logical-op-non-short-circuit=1 to dg-options.
> 
> --- gcc/testsuite/gcc.dg/tree-ssa/pr103514.c.jj	2022-01-29 11:11:39.338627697 +0100
> +++ gcc/testsuite/gcc.dg/tree-ssa/pr103514.c	2022-01-29 17:37:18.255237211 +0100
> @@ -1,5 +1,5 @@
>  /* { dg-do compile } */
> -/* { dg-options "-O -fdump-tree-optimized" } */
> +/* { dg-options "-O --param logical-op-non-short-circuit=1 -fdump-tree-optimized" } */
>  #include <stdbool.h>
>  
>  bool
> @@ -30,4 +30,4 @@ h (bool a, bool 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
> +/* { dg-final { scan-tree-dump-not "==" "optimized"} } */
> 
> 
> 	Jakub
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg,
Germany; GF: Ivo Totev; HRB 36809 (AG Nuernberg)

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

* Re: [EXTERNAL] [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514]
  2022-01-30 10:16     ` [EXTERNAL] " Navid Rahimi
@ 2022-01-31 10:12       ` Jakub Jelinek
  2022-02-01  5:31         ` Andrew Pinski
  0 siblings, 1 reply; 9+ messages in thread
From: Jakub Jelinek @ 2022-01-31 10:12 UTC (permalink / raw)
  To: Navid Rahimi, Andrew Pinski; +Cc: Richard Biener, Jeff Law, gcc-patches

On Sun, Jan 30, 2022 at 10:16:44AM +0000, Navid Rahimi via Gcc-patches wrote:
> Thanks Jakob for the correction. Sadly, I didn’t have any access to any non x86 architecture. But x86 was fully tested and there was no regression.
> 
> In my spare time I will look at implementation of this for short-circuit targets.

Note, it isn't just about those targets.
If you write the code as:
_Bool
g (_Bool a, _Bool b)
{
  _Bool c;
  if (!a)
    c = 0;
  else if (!b)
    c = 0;
  else
    c = 1;
  return c == (a ^ b); 
}
instead, it will not match either, not even on x86, even when it is
equivalent.

Though, maybe for non-short-circuiting targets we should recognize this
somewhere and turn into c = a & b;

Since phiopt2 it is:
  <bb 2> [local count: 1073741824]:
  if (a_4(D) != 0)
    goto <bb 3>; [50.00%]
  else
    goto <bb 4>; [50.00%]

  <bb 3> [local count: 536870913]:
  _8 = (int) b_5(D);

  <bb 4> [local count: 1073741824]:
  # iftmp.0_3 = PHI <_8(3), 0(2)>
and phiopt3 makes
  <bb 2> [local count: 1073741824]:
  if (a_4(D) != 0)
    goto <bb 3>; [50.00%]
  else
    goto <bb 4>; [50.00%]

  <bb 3> [local count: 536870913]:

  <bb 4> [local count: 1073741824]:
  # _9 = PHI <b_5(D)(3), 0(2)>
  iftmp.0_3 = (int) _9;
out of that.

CCing Andrew if he'd like to have a look for GCC 13.

	Jakub


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

* Re: [EXTERNAL] [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514]
  2022-01-31 10:12       ` Jakub Jelinek
@ 2022-02-01  5:31         ` Andrew Pinski
  0 siblings, 0 replies; 9+ messages in thread
From: Andrew Pinski @ 2022-02-01  5:31 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Navid Rahimi, Richard Biener, Jeff Law, gcc-patches

On Mon, Jan 31, 2022 at 2:13 AM Jakub Jelinek <jakub@redhat.com> wrote:
>
> On Sun, Jan 30, 2022 at 10:16:44AM +0000, Navid Rahimi via Gcc-patches wrote:
> > Thanks Jakob for the correction. Sadly, I didn’t have any access to any non x86 architecture. But x86 was fully tested and there was no regression.
> >
> > In my spare time I will look at implementation of this for short-circuit targets.
>
> Note, it isn't just about those targets.
> If you write the code as:
> _Bool
> g (_Bool a, _Bool b)
> {
>   _Bool c;
>   if (!a)
>     c = 0;
>   else if (!b)
>     c = 0;
>   else
>     c = 1;
>   return c == (a ^ b);
> }
> instead, it will not match either, not even on x86, even when it is
> equivalent.
>
> Though, maybe for non-short-circuiting targets we should recognize this
> somewhere and turn into c = a & b;
>
> Since phiopt2 it is:
>   <bb 2> [local count: 1073741824]:
>   if (a_4(D) != 0)
>     goto <bb 3>; [50.00%]
>   else
>     goto <bb 4>; [50.00%]
>
>   <bb 3> [local count: 536870913]:
>   _8 = (int) b_5(D);
>
>   <bb 4> [local count: 1073741824]:
>   # iftmp.0_3 = PHI <_8(3), 0(2)>
> and phiopt3 makes
>   <bb 2> [local count: 1073741824]:
>   if (a_4(D) != 0)
>     goto <bb 3>; [50.00%]
>   else
>     goto <bb 4>; [50.00%]
>
>   <bb 3> [local count: 536870913]:
>
>   <bb 4> [local count: 1073741824]:
>   # _9 = PHI <b_5(D)(3), 0(2)>
>   iftmp.0_3 = (int) _9;
> out of that.
>
> CCing Andrew if he'd like to have a look for GCC 13.

Yes I have a patch to recognize:
  if (a_3(D) != 0)
    goto <bb 4>; [50.00%]
  else
    goto <bb 3>; [50.00%]

  <bb 3> [local count: 536870912]:

  <bb 4> [local count: 1073741824]:
  # c_2 = PHI <0(3), b_4(D)(2)>

already (a ? b : 0) into a & b.

This is already recorded as PR 89263.

Thanks,
Andrew Pinski

>
>         Jakub
>

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

* Re: [PATCH] tree-optimization/103514 Missing XOR-EQ-AND Optimization
@ 2021-12-04 21:22 Marc Glisse
  0 siblings, 0 replies; 9+ messages in thread
From: Marc Glisse @ 2021-12-04 21:22 UTC (permalink / raw)
  To: gcc-patches

+/* (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 @0 @1) (second_op:c @0 @1))
+   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+    (convert (bit_not (bit_ior @0 @1))))))

I don't think you need types_match, if both are operands of bit_and, their 
types must already match.

It isn't clear what the INTEGRAL_TYPE_P test is for. Your 2 
transformations don't seem that similar to me. The first one requires that 
a and b have the same type as the result of ==, so they are boolean-like. 
The second one makes sense for more general integers, but then it looks 
like it should produce (a|b)==0.

It doesn't look like we have a canonical representation between a^b and 
a!=b for booleans :-(

(sorry for the broken thread, I was automatically unsubscribed because 
mailman doesn't like greylisting)

-- 
Marc Glisse

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

* [PATCH] tree-optimization/103514 Missing XOR-EQ-AND Optimization
@ 2021-12-03  7:01 Navid Rahimi
  0 siblings, 0 replies; 9+ messages in thread
From: Navid Rahimi @ 2021-12-03  7:01 UTC (permalink / raw)
  To: Navid Rahimi via Gcc-patches

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

Hi GCC community,

This patch will add the missed pattern described in bug 103514 [1] to the match.pd. Tested on x86_64 Linux. 

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/pr102232.c: Testcase for this optimization.

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

Best wishes,
Navid.

[-- Attachment #2: 0001-tree-optimization-103514-Missing-XOR-EQ-AND-Opt-v3.patch --]
[-- Type: application/octet-stream, Size: 2587 bytes --]

From 8de3e8ee91dd3a31d5b71b4b001a1ca5ed4cd9bd 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/pr102232.c: Testcase for this optimization.
---
 gcc/match.pd                             | 10 +++++
 gcc/testsuite/gcc.dg/tree-ssa/pr103514.c | 56 ++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr103514.c

diff --git a/gcc/match.pd b/gcc/match.pd
index d467a1c4e45..2e2958f608e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1768,6 +1768,16 @@ 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 @0 @1) (second_op:c @0 @1))
+   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+    (convert (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..b46d033a36c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr103514.c
@@ -0,0 +1,56 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+#include <stdbool.h>
+
+bool
+b (bool a, bool b)
+{
+    return (a && b) == (a ^ b);
+}
+
+bool
+b_reverse (bool a, bool b)
+{
+     return (a && b) ^ (a == b);
+}
+
+int
+i (int a, int b)
+{
+     return (a & b) ^ (a == b);
+}
+
+int
+i_reverse (int a, int b)
+{
+     return (a & b) == (a ^ b);
+}
+
+long
+l (long a, long b)
+{
+     return (a & b) ^ (a == b);
+}
+
+long
+l_reverse (long a, long b)
+{
+     return (a & b) == (a ^ b);
+}
+
+unsigned int
+ui (unsigned int a, unsigned int b)
+{
+     return (a & b) ^ (a == b);
+}
+
+unsigned int
+ui_reverse (unsigned int a, unsigned int 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


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

end of thread, other threads:[~2022-02-01  5:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-05 20:12 [PATCH] tree-optimization/103514 Missing XOR-EQ-AND Optimization Navid Rahimi
2022-01-28 22:14 ` Jeff Law
2022-01-29 16:46   ` [PATCH] testsuite: Fix up tree-ssa/pr103514.c testcase [PR103514] Jakub Jelinek
2022-01-30 10:16     ` [EXTERNAL] " Navid Rahimi
2022-01-31 10:12       ` Jakub Jelinek
2022-02-01  5:31         ` Andrew Pinski
2022-01-31  8:30     ` Richard Biener
  -- strict thread matches above, loose matches on Subject: below --
2021-12-04 21:22 [PATCH] tree-optimization/103514 Missing XOR-EQ-AND Optimization Marc Glisse
2021-12-03  7:01 Navid Rahimi

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