diff mbox

match.pd: Optimize (x | y) & ~(x & y) and (x | y) & (~x ^ y)

Message ID 20150626075918.GC10139@redhat.com
State New
Headers show

Commit Message

Marek Polacek June 26, 2015, 7:59 a.m. UTC
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  <polacek@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.


	Marek

Comments

Richard Biener June 26, 2015, 9:28 a.m. UTC | #1
On Fri, 26 Jun 2015, Marek Polacek wrote:

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

Yeah, I double-checked and indeed you can't remove any there.

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

Ok.  (I suppose you always check whether the patterns are in fold-const.c
and remove them there if you spot them)

Thanks,
Richard.

> 2015-06-26  Marek Polacek  <polacek@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
> 
>
diff mbox

Patch

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" } } */