This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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

-- 
Richard Biener <rguenther@suse.de>
SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Dilip Upmanyu, Graham Norton, HRB 21284 (AG Nuernberg)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]