On Thu, Jun 18, 2015 at 05:41:18PM +0200, Marek Polacek wrote: > > Again for symmetry, it seems like this comes with > > x + y - (x | y) -> x & y > > x + y - (x & y) -> x | y > > which seem fine when overflow is undefined or wraps, but not if for instance > > it saturates. > > I'll leave this as a follow-up.
...here. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2015-06-19 Marek Polacek <pola...@redhat.com> * match.pd (x + y - (x | y) -> x & y, (x + y) - (x & y) -> x | y): New patterns. * gcc.dg/fold-minus-4.c: New test. * gcc.dg/fold-minus-5.c: New test. diff --git gcc/match.pd gcc/match.pd index badb80a..61ff710 100644 --- gcc/match.pd +++ gcc/match.pd @@ -343,6 +343,18 @@ along with GCC; see the file COPYING3. If not see (plus:c (bit_and @0 @1) (bit_ior @0 @1)) (plus @0 @1)) +/* x + y - (x | y) -> x & y */ +(simplify + (minus (plus @0 @1) (bit_ior @0 @1)) + (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_SATURATING (type)) + (bit_and @0 @1))) + +/* (x + y) - (x & y) -> x | y */ +(simplify + (minus (plus @0 @1) (bit_and @0 @1)) + (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_SATURATING (type)) + (bit_ior @0 @1))) + /* (x | y) - (x ^ y) -> x & y */ (simplify (minus (bit_ior @0 @1) (bit_xor @0 @1)) diff --git gcc/testsuite/gcc.dg/fold-minus-4.c gcc/testsuite/gcc.dg/fold-minus-4.c index e69de29..2d76b4f 100644 --- gcc/testsuite/gcc.dg/fold-minus-4.c +++ gcc/testsuite/gcc.dg/fold-minus-4.c @@ -0,0 +1,37 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-cddce1" } */ + +int +fn1 (int a, int b) +{ + int tem1 = a + b; + int tem2 = a & b; + return tem1 - tem2; +} + +int +fn2 (int a, int b) +{ + int tem1 = b + a; + int tem2 = a & b; + return tem1 - tem2; +} + +int +fn3 (int a, int b) +{ + int tem1 = a + b; + int tem2 = b & a; + return tem1 - tem2; +} + +int +fn4 (int a, int b) +{ + int tem1 = b + a; + int tem2 = b & a; + return tem1 - tem2; +} + +/* { dg-final { scan-tree-dump-not " & " "cddce1" } } */ +/* { dg-final { scan-tree-dump-not " \\+ " "cddce1" } } */ diff --git gcc/testsuite/gcc.dg/fold-minus-5.c gcc/testsuite/gcc.dg/fold-minus-5.c index e69de29..a31e1cc 100644 --- gcc/testsuite/gcc.dg/fold-minus-5.c +++ gcc/testsuite/gcc.dg/fold-minus-5.c @@ -0,0 +1,37 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-cddce1" } */ + +int +fn1 (int a, int b) +{ + int tem1 = a + b; + int tem2 = a | b; + return tem1 - tem2; +} + +int +fn2 (int a, int b) +{ + int tem1 = b + a; + int tem2 = a | b; + return tem1 - tem2; +} + +int +fn3 (int a, int b) +{ + int tem1 = a + b; + int tem2 = b | a; + return tem1 - tem2; +} + +int +fn4 (int a, int b) +{ + int tem1 = b + a; + int tem2 = b | a; + return tem1 - tem2; +} + +/* { dg-final { scan-tree-dump-not " \\+ " "cddce1" } } */ +/* { dg-final { scan-tree-dump-not " \\| " "cddce1" } } */ Marek