public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: [PATCH] [tree-optimization] Optimize two patterns with three xors.
@ 2020-11-17  2:19 Eugene Rozenfeld
  2020-11-17  7:57 ` Richard Biener
  0 siblings, 1 reply; 6+ messages in thread
From: Eugene Rozenfeld @ 2020-11-17  2:19 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

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

Thank you for the review Richard!

I re-worked the patch based on your suggestions (attached).
I made the change to reuse the first bit_xor in both patterns and I added :s to the last xor in the first pattern.
For the second pattern I didn't add :s because I think the simplification is beneficial even if the second or third bit_xor has more than one use since we are simplifying them to just a single operand (@2). If that is incorrect, please explain why.

Eugene

-----Original Message-----
From: Richard Biener <richard.guenther@gmail.com> 
Sent: Monday, November 16, 2020 4:11 AM
To: Eugene Rozenfeld <Eugene.Rozenfeld@microsoft.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] [tree-optimization] Optimize two patterns with three xors.

On Thu, Nov 12, 2020 at 2:53 AM Eugene Rozenfeld via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
>
> Simplify (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c.
>
> int f(int a, int b, int c)
> {
>     return (a ^ b) & ((b ^ c) ^ a);
> }
>
> Code without the patch:
> mov    eax,edx
> xor    eax,esi
> xor    eax,edi
> xor    edi,esi
> and    eax,edi
> ret
>
> Code with the patch:
> xor    edi,esi
> andn   eax,edx,edi
> ret
>
> Simplify (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c.
> int g(int a, int b, int c)
> {
>     return (a ^ b) | ((b ^ c) ^ a);
> }
>
> Code without the patch:
> mov    eax,edx
> xor    eax,esi
> xor    eax,edi
> xor    edi,esi
> or     eax,edi
> ret
>
> Code with the patch:
> xor    edi,esi
> mov    eax,edi
> or     eax,edx
> ret
>
> This fixes PR96671.

+/* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */ (simplify  (bit_and:c 
+(bit_xor:c @0 @1) (bit_xor:cs (bit_xor:c @1 @2) @0))  (bit_and (bit_xor 
+@0 @1) (bit_not @2)))

you should be able to re-use the first bit_xor by doing

+/* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */ (simplify  (bit_and:c 
+(bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:c @1 @2) @0))  (bit_and @3 
+(bit_not @2)))

For completeness the last (bit_xor:c @1 @2) should also have a :s

+/* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */ (simplify  (bit_ior:c 
+(bit_xor:c @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))  (bit_ior (bit_xor 
+@0 @1) @2))

completely misses :s here and the same comment about the re-use of the existing xor applies.

Otherwise looks OK to me, sorry for the delay.

Thanks,
Richard.

> Tested on x86_64-pc-linux-gnu.
>

[-- Attachment #2: 0001-Optimize-two-patterns-with-three-xors.patch --]
[-- Type: application/octet-stream, Size: 1106 bytes --]

From 8769f7e720219457d7e708d04981231c0d4501f5 Mon Sep 17 00:00:00 2001
From: Eugene Rozenfeld <erozen@microsoft.com>
Date: Wed, 11 Nov 2020 17:24:48 -0800
Subject: [PATCH] Optimize two patterns with three xors.

Simplify (a ^ b) & ((b ^ c) ^ a) to (a ^ b) & ~c.
Simplify (a ^ b) | ((b ^ c) ^ a) to (a ^ b) | c.
This fixes PR96671.

gcc/
* match.pd (three xor patterns): New patterns.
---
 gcc/match.pd | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 349eab61d6b..1dd05c4c2a3 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -901,6 +901,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (bit_not (bit_ior:cs (bit_not @0) @1))
  (bit_and @0 (bit_not @1)))
 
+/* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
+(simplify
+ (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
+ (bit_and @3 (bit_not @2)))
+
+/* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
+(simplify
+ (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
+ (bit_ior @3 @2))
+
 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
 #if GIMPLE
 (simplify
-- 
2.17.1


^ permalink raw reply	[flat|nested] 6+ messages in thread
* [PATCH] [tree-optimization] Optimize two patterns with three xors.
@ 2020-11-12  1:52 Eugene Rozenfeld
  2020-11-16 12:10 ` Richard Biener
  0 siblings, 1 reply; 6+ messages in thread
From: Eugene Rozenfeld @ 2020-11-12  1:52 UTC (permalink / raw)
  To: gcc-patches

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

Simplify (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c.

int f(int a, int b, int c)
{
    return (a ^ b) & ((b ^ c) ^ a);
}

Code without the patch:
mov    eax,edx
xor    eax,esi
xor    eax,edi
xor    edi,esi
and    eax,edi
ret  

Code with the patch:
xor    edi,esi
andn   eax,edx,edi
ret  

Simplify (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c.
int g(int a, int b, int c)
{
    return (a ^ b) | ((b ^ c) ^ a);
}

Code without the patch:
mov    eax,edx
xor    eax,esi
xor    eax,edi
xor    edi,esi
or     eax,edi
ret    

Code with the patch:
xor    edi,esi
mov    eax,edi
or     eax,edx
ret

This fixes PR96671.

Tested on x86_64-pc-linux-gnu.


[-- Attachment #2: 0001-Optimize-two-patterns-with-three-xors.patch --]
[-- Type: application/octet-stream, Size: 1127 bytes --]

From 68f4c49b118c5bc110e890d45f1228f79a8a27e4 Mon Sep 17 00:00:00 2001
From: Eugene Rozenfeld <erozen@microsoft.com>
Date: Wed, 11 Nov 2020 17:24:48 -0800
Subject: [PATCH] Optimize two patterns with three xors.

Simplify (a ^ b) & ((b ^ c) ^ a) to (a ^ b) & ~c.
Simplify (a ^ b) | ((b ^ c) ^ a) to (a ^ b) | c.
This fixes PR96671.

gcc/
* match.pd (three xor patterns): New patterns.
---
 gcc/match.pd | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 349eab61d6b..63d1688e5b7 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -901,6 +901,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (bit_not (bit_ior:cs (bit_not @0) @1))
  (bit_and @0 (bit_not @1)))
 
+/* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
+(simplify
+ (bit_and:c (bit_xor:c @0 @1) (bit_xor:cs (bit_xor:c @1 @2) @0))
+ (bit_and (bit_xor @0 @1) (bit_not @2)))
+
+/* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
+(simplify
+ (bit_ior:c (bit_xor:c @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
+ (bit_ior (bit_xor @0 @1) @2))
+
 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
 #if GIMPLE
 (simplify
-- 
2.17.1


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

end of thread, other threads:[~2020-11-19 22:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-17  2:19 [PATCH] [tree-optimization] Optimize two patterns with three xors Eugene Rozenfeld
2020-11-17  7:57 ` Richard Biener
2020-11-18 19:32   ` Jeff Law
2020-11-19 22:04     ` [EXTERNAL] " Eugene Rozenfeld
  -- strict thread matches above, loose matches on Subject: below --
2020-11-12  1:52 Eugene Rozenfeld
2020-11-16 12: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).