The following patch adds optimizing of
(x | y) & ~(x & y)
and
(x | y) & (~x ^ y)
to a single operation.

No +/-/* in those, so I think we don't have to worry about saturating types
this time around.

For some reason I had to use more :c's in the latter pattern than usual.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-06-26  Marek Polacek  <pola...@redhat.com>

        * match.pd ((x | y) & ~(x & y) -> x ^ y,
        (x | y) & (~x ^ y) -> x & y): New patterns.

        * gcc.dg/fold-and-1.c: New test.
        * gcc.dg/fold-and-2.c: New test.

diff --git gcc/match.pd gcc/match.pd
index 292ce6d..f242c99 100644
--- gcc/match.pd
+++ gcc/match.pd
@@ -367,6 +367,16 @@ along with GCC; see the file COPYING3.  If not see
  (minus (bit_ior @0 @1) (bit_and @0 @1))
  (bit_xor @0 @1))
 
+/* (x | y) & ~(x & y) -> x ^ y */
+(simplify
+ (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
+ (bit_xor @0 @1))
+
+/* (x | y) & (~x ^ y) -> x & y */
+(simplify
+ (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
+ (bit_and @0 @1))
+
 (simplify
  (abs (negate @0))
  (abs @0))
diff --git gcc/testsuite/gcc.dg/fold-and-1.c gcc/testsuite/gcc.dg/fold-and-1.c
index e69de29..d555bb4 100644
--- gcc/testsuite/gcc.dg/fold-and-1.c
+++ gcc/testsuite/gcc.dg/fold-and-1.c
@@ -0,0 +1,70 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+fn1 (int x, int y)
+{
+  int tem1 = x | y;
+  int tem2 = ~(x & y);
+  return tem1 & tem2;
+}
+
+int
+fn2 (int x, int y)
+{
+  int tem1 = y | x;
+  int tem2 = ~(x & y);
+  return tem1 & tem2;
+}
+
+int
+fn3 (int x, int y)
+{
+  int tem1 = x | y;
+  int tem2 = ~(y & x);
+  return tem1 & tem2;
+}
+
+int
+fn4 (int x, int y)
+{
+  int tem1 = y | x;
+  int tem2 = ~(y & x);
+  return tem1 & tem2;
+}
+
+int
+fn5 (int x, int y)
+{
+  int tem1 = ~(x & y);
+  int tem2 = x | y;
+  return tem1 & tem2;
+}
+
+int
+fn6 (int x, int y)
+{
+  int tem1 = ~(x & y);
+  int tem2 = y | x;
+  return tem1 & tem2;
+}
+
+int
+fn7 (int x, int y)
+{
+  int tem1 = ~(y & x);
+  int tem2 = x | y;
+  return tem1 & tem2;
+}
+
+int
+fn8 (int x, int y)
+{
+  int tem1 = ~(y & x);
+  int tem2 = y | x;
+  return tem1 & tem2;
+}
+
+/* { dg-final { scan-tree-dump-not " \\| " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not " \\& " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not "~" "cddce1" } } */
diff --git gcc/testsuite/gcc.dg/fold-and-2.c gcc/testsuite/gcc.dg/fold-and-2.c
index e69de29..3df2a0b 100644
--- gcc/testsuite/gcc.dg/fold-and-2.c
+++ gcc/testsuite/gcc.dg/fold-and-2.c
@@ -0,0 +1,70 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+fn1 (int x, int y)
+{
+  int tem1 = x | y;
+  int tem2 = ~x ^ y;
+  return tem1 & tem2;
+}
+
+int
+fn2 (int x, int y)
+{
+  int tem1 = y | x;
+  int tem2 = ~x ^ y;
+  return tem1 & tem2;
+}
+
+int
+fn3 (int x, int y)
+{
+  int tem1 = x | y;
+  int tem2 = y ^ ~x;
+  return tem1 & tem2;
+}
+
+int
+fn4 (int x, int y)
+{
+  int tem1 = y | x;
+  int tem2 = y ^ ~x;
+  return tem1 & tem2;
+}
+
+int
+fn5 (int x, int y)
+{
+  int tem1 = ~x ^ y;
+  int tem2 = x | y;
+  return tem1 & tem2;
+}
+
+int
+fn6 (int x, int y)
+{
+  int tem1 = ~x ^ y;
+  int tem2 = y | x;
+  return tem1 & tem2;
+}
+
+int
+fn7 (int x, int y)
+{
+  int tem1 = y ^ ~x;
+  int tem2 = x | y;
+  return tem1 & tem2;
+}
+
+int
+fn8 (int x, int y)
+{
+  int tem1 = y ^ ~x;
+  int tem2 = y | x;
+  return tem1 & tem2;
+}
+
+/* { dg-final { scan-tree-dump-not " \\| " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not " \\^ " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not "~" "cddce1" } } */

        Marek

Reply via email to