This moves a simple optimization. Here it's plain to see how :c removes the need to duplicate code to handle commutativity.
I put some more converts into the pattern, but then it's turned out that I also need the tree_nop_conversion_p (otherwise we'd regress binop-notor2.c that uses booleans). I did a regtest with the patterns in fold-const.c removed to see whether we have some testing for this folding -- and there were no regressions, so I had to write a test. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2015-06-29 Marek Polacek <pola...@redhat.com> * fold-const.c (fold_binary_loc): Move ~X | X folding ... * match.pd: ... here. * gcc.dg/fold-ior-2.c: New test. diff --git gcc/fold-const.c gcc/fold-const.c index a447452..caba0cf 100644 --- gcc/fold-const.c +++ gcc/fold-const.c @@ -10928,24 +10928,6 @@ fold_binary_loc (location_t loc, case BIT_IOR_EXPR: bit_ior: - /* ~X | X is -1. */ - if (TREE_CODE (arg0) == BIT_NOT_EXPR - && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) - { - t1 = build_zero_cst (type); - t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); - return omit_one_operand_loc (loc, type, t1, arg1); - } - - /* X | ~X is -1. */ - if (TREE_CODE (arg1) == BIT_NOT_EXPR - && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) - { - t1 = build_zero_cst (type); - t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); - return omit_one_operand_loc (loc, type, t1, arg0); - } - /* Canonicalize (X & C1) | C2. */ if (TREE_CODE (arg0) == BIT_AND_EXPR && TREE_CODE (arg1) == INTEGER_CST diff --git gcc/match.pd gcc/match.pd index 0cf3d21..5dcbc1a 100644 --- gcc/match.pd +++ gcc/match.pd @@ -283,6 +283,12 @@ along with GCC; see the file COPYING3. If not see (bit_and @0 integer_zerop@1) @1) +/* ~x | x -> -1 */ +(simplify + (bit_ior:c (convert? @0) (convert? (bit_not @0))) + (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) + { build_all_ones_cst (type); })) + /* x ^ x -> 0 */ (simplify (bit_xor @0 @0) diff --git gcc/testsuite/gcc.dg/fold-ior-2.c gcc/testsuite/gcc.dg/fold-ior-2.c index e69de29..6abac9e 100644 --- gcc/testsuite/gcc.dg/fold-ior-2.c +++ gcc/testsuite/gcc.dg/fold-ior-2.c @@ -0,0 +1,47 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-cddce1" } */ + +int +fn1 (int x) +{ + return ~x | x; +} + +int +fn2 (int x) +{ + return x | ~x; +} + +unsigned int +fn3 (unsigned int x) +{ + return ~x | x; +} + +unsigned int +fn4 (unsigned int x) +{ + return ~x | x; +} + +int +fn5 (int x) +{ + return ~x | (unsigned) x; +} + +int +fn6 (int x) +{ + return (unsigned) ~x | x; +} + +int +fn7 (int x) +{ + return ~(unsigned) x | x; +} + +/* { dg-final { scan-tree-dump-not "~" "cddce1" } } */ +/* { dg-final { scan-tree-dump-not " \\| " "cddce1" } } */ Marek