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

