From: Marek Polacek Date: Tue, 16 Dec 2014 18:29:01 +0000 (+0000) Subject: re PR middle-end/64309 (if (1 & (1 << n)) not simplified to if (n == 0)) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=01ada710782bb14c86d7caed1813efa593740eab;p=gcc.git re PR middle-end/64309 (if (1 & (1 << n)) not simplified to if (n == 0)) PR middle-end/64309 * match.pd: Add ((1 << A) & 1) != 0 -> A == 0 and ((1 << A) & 1) == 0 -> A != 0. * gcc.dg/pr64309.c: New test. From-SVN: r218787 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 609b8d1c6d4..b399b6ebade 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2014-12-16 Marek Polacek + + PR middle-end/64309 + * match.pd: Add ((1 << A) & 1) != 0 -> A == 0 and + ((1 << A) & 1) == 0 -> A != 0. + 2014-12-16 Richard Biener * genmatch.c (parser::parser): Initialize capture_ids. diff --git a/gcc/match.pd b/gcc/match.pd index 083d65f9ee1..dbca99efb01 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -599,6 +599,13 @@ along with GCC; see the file COPYING3. If not see build_int_cst (TREE_TYPE (@1), element_precision (type)), @1); })) +/* ((1 << A) & 1) != 0 -> A == 0 + ((1 << A) & 1) == 0 -> A != 0 */ +(for cmp (ne eq) + icmp (eq ne) + (simplify + (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop) + (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))) /* Simplifications of conversions. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 47565a2f465..a21146276be 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2014-12-16 Marek Polacek + + PR middle-end/64309 + * gcc.dg/pr64309.c: New test. + 2014-12-16 Felix Yang PR rtl-optimization/64240 diff --git a/gcc/testsuite/gcc.dg/pr64309.c b/gcc/testsuite/gcc.dg/pr64309.c new file mode 100644 index 00000000000..710a7624e21 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr64309.c @@ -0,0 +1,66 @@ +/* PR middle-end/64309 */ +/* { dg-do run } */ +/* { dg-options "-fdump-tree-original" } */ + +int +fn1 (int n) +{ + return ((1 << n) & 1) != 0; +} + +int +fn2 (int n) +{ + return (1 & (1 << n)) != 0; +} + +int +fn3 (int n) +{ + return ((1 << n) & 1) == 0; +} + +int +fn4 (int n) +{ + return (1 & (1 << n)) == 0; +} + +int +main (void) +{ + if (fn1 (0) != 1 + || fn1 (1) != 0 + || fn1 (2) != 0 + || fn1 (3) != 0 + || fn1 (4) != 0 + || fn1 (5) != 0) + __builtin_abort (); + + if (fn2 (0) != 1 + || fn2 (1) != 0 + || fn2 (2) != 0 + || fn2 (3) != 0 + || fn2 (4) != 0 + || fn2 (5) != 0) + __builtin_abort (); + + if (fn3 (0) != 0 + || fn3 (1) != 1 + || fn3 (2) != 1 + || fn3 (3) != 1 + || fn3 (4) != 1 + || fn3 (5) != 1) + __builtin_abort (); + + if (fn4 (0) != 0 + || fn4 (1) != 1 + || fn4 (2) != 1 + || fn4 (3) != 1 + || fn4 (4) != 1 + || fn4 (5) != 1) + __builtin_abort (); +} + +/* { dg-final { scan-tree-dump-not "(<<|>>)" "original" } } */ +/* { dg-final { cleanup-tree-dump "original" } } */