From: Paolo Bonzini Date: Mon, 5 Dec 2016 13:19:34 +0000 (+0000) Subject: match.pd: Simplify X ? C : 0 where C is a power of 2 and X tests a single bit. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=519e0faa00bda70c9ffd66b7e7a6011c5e742d2b;p=gcc.git match.pd: Simplify X ? C : 0 where C is a power of 2 and X tests a single bit. gcc: * match.pd: Simplify X ? C : 0 where C is a power of 2 and X tests a single bit. gcc/testsuite: * gcc.dg/fold-and-lshift.c, gcc.dg/fold-and-rshift-1.c, gcc.dg/fold-and-rshift-2.c: New testcases. From-SVN: r243255 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 9488b0f7d9e..2ba03023cb0 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2016-12-05 Paolo Bonzini + + * match.pd: Simplify X ? C : 0 where C is a power of 2 and + X tests a single bit. + 2016-12-05 Nathan Sidwell * diagnostic.c (diagnostic_check_max_errors): New, broken out of ... @@ -25,7 +30,7 @@ * config/arc/arc.md (ls_gd_load): Remove unused pattern. (tls_gd_dispatch): Likewise. -2016-12-025 Andre Vieira +2016-12-05 Andre Vieira * config/arm/arm.c (TARGET_ASM_INIT_SECTIONS): Fix wrong undef location. diff --git a/gcc/match.pd b/gcc/match.pd index dbb91034e95..1fe003ba158 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2737,6 +2737,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cmp (bit_and@2 @0 integer_pow2p@1) @1) (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) +/* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2, + convert this into a shift followed by ANDing with D. */ +(simplify + (cond + (ne (bit_and @0 integer_pow2p@1) integer_zerop) + integer_pow2p@2 integer_zerop) + (with { + int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1); + } + (if (shift > 0) + (bit_and + (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2) + (bit_and + (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2)))) + /* If we have (A & C) != 0 where C is the sign bit of A, convert this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ (for cmp (eq ne) @@ -2751,6 +2766,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (with { tree stype = signed_type_for (TREE_TYPE (@0)); } (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) +/* If we have A < 0 ? C : 0 where C is a power of 2, convert + this into a right shift followed by ANDing with C. */ +(simplify + (cond + (lt @0 integer_zerop) + integer_pow2p@1 integer_zerop) + (with { + int shift = element_precision (@0) - wi::exact_log2 (@1) - 1; + } + (bit_and + (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) + @1))) + /* When the addresses are not directly of decls compare base and offset. This implements some remaining parts of fold_comparison address comparisons but still no complete part of it. Still it is good diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c40ffd60b21..d9edb52d3b2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-12-05 Paolo Bonzini + + * gcc.dg/fold-and-lshift.c, gcc.dg/fold-and-rshift-1.c, + gcc.dg/fold-and-rshift-2.c: New testcases. + 2016-12-05 Nathan Sidwell * c-c++-common/fmax_errors.c: Check notes after last error are diff --git a/gcc/testsuite/gcc.dg/fold-and-lshift.c b/gcc/testsuite/gcc.dg/fold-and-lshift.c new file mode 100644 index 00000000000..2905095ff74 --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-and-lshift.c @@ -0,0 +1,35 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-original" } */ + +int f(int x) +{ + return (x << 2) & 128; +} + +int g(int x) +{ + return !!(x & 32) << 7; +} + +int h(int x) +{ + return ((x >> 5) & 1) << 7; +} + +int i(int x) +{ + return (x & 32) >> 5 << 7; +} + +int j(int x) +{ + return ((x >> 5) & 1) ? 128 : 0; +} + +int k(int x) +{ + return (x & 32) ? 128 : 0; +} + +/* { dg-final { scan-tree-dump-not " \\? " "original" } } */ +/* { dg-final { scan-assembler-not "sarl" { target i?86-*-* x86_64-*-* } } }" */ diff --git a/gcc/testsuite/gcc.dg/fold-and-rshift-1.c b/gcc/testsuite/gcc.dg/fold-and-rshift-1.c new file mode 100644 index 00000000000..11c13d582eb --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-and-rshift-1.c @@ -0,0 +1,35 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-original" } */ + +int f(int x) +{ + return (x >> 2) & 128; +} + +int g(int x) +{ + return !!(x & 512) << 7; +} + +int h(int x) +{ + return ((x >> 9) & 1) << 7; +} + +int i(int x) +{ + return (x & 512) >> 9 << 7; +} + +int j(int x) +{ + return ((x >> 9) & 1) ? 128 : 0; +} + +int k(int x) +{ + return (x & 512) ? 128 : 0; +} + +/* { dg-final { scan-tree-dump-not " \\? " "original" } } */ +/* { dg-final { scan-assembler-not "sall" { target i?86-*-* x86_64-*-* } } }" */ diff --git a/gcc/testsuite/gcc.dg/fold-and-rshift-2.c b/gcc/testsuite/gcc.dg/fold-and-rshift-2.c new file mode 100644 index 00000000000..f88d48dc34a --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-and-rshift-2.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-original" } */ + +unsigned f(unsigned x) +{ + return (x >> 29) & 32; +} + +unsigned g(unsigned x) +{ + return !!(x & 0x80000000) << 5; +} + +unsigned j(unsigned x) +{ + return ((x >> 31) & 1) ? 32 : 0; +} + +unsigned k(unsigned x) +{ + return (x & 0x80000000) ? 32 : 0; +} + +/* { dg-final { scan-tree-dump-not " \\? " "original" } } */ +/* { dg-final { scan-assembler-not "sall" { target i?86-*-* x86_64-*-* } } }" */