From: Marc Glisse Date: Tue, 7 Nov 2017 11:08:06 +0000 (+0100) Subject: More bitop simplifications in match.pd X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=e268a77b59cb788637d6db4829f0fd1ddf63f6f2;p=gcc.git More bitop simplifications in match.pd 2017-11-07 Marc Glisse gcc/ * match.pd ((a&~b)|(a^b),(a&~b)^~a,(a|b)&~(a^b),a|~(a^b), (a|b)|(a&^b),(a&b)|~(a^b),~(~a&b),~X^Y): New transformations. gcc/testsuite/ * gcc.dg/tree-ssa/bitops-1.c: New file. From-SVN: r254495 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index d06fcee491e..bbb51e3fc00 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2017-11-07 Marc Glisse + + * match.pd ((a&~b)|(a^b),(a&~b)^~a,(a|b)&~(a^b),a|~(a^b), + (a|b)|(a&^b),(a&b)|~(a^b),~(~a&b),~X^Y): New transformations. + 2017-11-07 Marc Glisse * fold-const.c (negate_expr_p) [PLUS_EXPR, MINUS_EXPR]: Handle diff --git a/gcc/match.pd b/gcc/match.pd index 9113fd183ac..40ac5dae8c0 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -685,6 +685,42 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bit_ior:c (bit_xor:c @0 @1) @0) (bit_ior @0 @1)) +/* (a & ~b) | (a ^ b) --> a ^ b */ +(simplify + (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1)) + @2) + +/* (a & ~b) ^ ~a --> ~(a & b) */ +(simplify + (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0)) + (bit_not (bit_and @0 @1))) + +/* (a | b) & ~(a ^ b) --> a & b */ +(simplify + (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1))) + (bit_and @0 @1)) + +/* a | ~(a ^ b) --> a | ~b */ +(simplify + (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1))) + (bit_ior @0 (bit_not @1))) + +/* (a | b) | (a &^ b) --> a | b */ +(for op (bit_and bit_xor) + (simplify + (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1)) + @2)) + +/* (a & b) | ~(a ^ b) --> ~(a ^ b) */ +(simplify + (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1))) + @2) + +/* ~(~a & b) --> a | ~b */ +(simplify + (bit_not (bit_and:cs (bit_not @0) @1)) + (bit_ior @0 (bit_not @1))) + /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */ #if GIMPLE (simplify @@ -1124,6 +1160,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) (convert (bit_xor @0 @1)))) +/* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */ +(simplify + (bit_xor:c (nop_convert:s (bit_not:s @0)) @1) + (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) + (bit_not (bit_xor (view_convert @0) @1)))) + /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ (simplify (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2)) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7f33b7c7337..bb0eb9bfa64 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2017-11-07 Marc Glisse + + * gcc.dg/tree-ssa/bitops-1.c: New file. + 2017-11-07 Marc Glisse * gcc.dg/tree-ssa/negminus.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-1.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-1.c new file mode 100644 index 00000000000..cf2823deb62 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-1.c @@ -0,0 +1,72 @@ +/* { dg-do run } */ +/* { dg-options "-O -fdump-tree-optimized-raw" } */ + +#define DECLS(n,VOL) \ +__attribute__((noinline,noclone)) \ +int f##n(int A,int B){ \ + VOL int C = A & ~B; \ + VOL int D = A ^ B; \ + return C | D; \ +} \ +__attribute__((noinline,noclone)) \ +int g##n(int A,int B){ \ + VOL int C = A & ~B; \ + return C ^ ~A; \ +} \ +__attribute__((noinline,noclone)) \ +int h##n(int A,int B){ \ + VOL int C = A | B; \ + VOL int D = A ^ B; \ + return C & ~D; \ +} \ +__attribute__((noinline,noclone)) \ +int i##n(int A,int B){ \ + VOL int C = A ^ B; \ + return A | ~C; \ +} \ +__attribute__((noinline,noclone)) \ +int J##n(int A,int B){ \ + VOL int C = A | B; \ + VOL int D = A & B; \ + return C | D; \ +} \ +__attribute__((noinline,noclone)) \ +int k##n(int A,int B){ \ + VOL int C = A & B; \ + VOL int D = A ^ B; \ + return C | ~D; \ +} \ +__attribute__((noinline,noclone)) \ +int l##n(int A,int B){ \ + VOL int C = A & ~B; \ + return ~C; \ +} \ +__attribute__((noinline,noclone)) \ +int m##n(int A,int B){ \ + VOL int C = A & B; \ + VOL int D = A ^ B; \ + return C ^ D; \ +} + +DECLS(0,) +DECLS(1,volatile) + +int main(){ + for(int A = 0; A <= 1; ++A) + for(int B = 0; B <= 1; ++B) + { + if (f0 (A, B) != f1 (A, B)) __builtin_abort(); + if (g0 (A, B) != g1 (A, B)) __builtin_abort(); + if (h0 (A, B) != h1 (A, B)) __builtin_abort(); + if (i0 (A, B) != i1 (A, B)) __builtin_abort(); + if (J0 (A, B) != J1 (A, B)) __builtin_abort(); + if (k0 (A, B) != k1 (A, B)) __builtin_abort(); + if (l0 (A, B) != l1 (A, B)) __builtin_abort(); + if (m0 (A, B) != m1 (A, B)) __builtin_abort(); + } +} + +/* { dg-final { scan-tree-dump-times "bit_not_expr" 12 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_and_expr" 9 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr" 10 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr" 9 "optimized"} } */