fold-const.c (fold_binary_loc): Move ~X | X folding ...
authorMarek Polacek <polacek@redhat.com>
Tue, 30 Jun 2015 09:02:00 +0000 (09:02 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Tue, 30 Jun 2015 09:02:00 +0000 (09:02 +0000)
* fold-const.c (fold_binary_loc): Move ~X | X folding ...
* match.pd: ... here.

* gcc.dg/fold-ior-2.c: New test.

From-SVN: r225164

gcc/ChangeLog
gcc/fold-const.c
gcc/match.pd
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/fold-ior-2.c [new file with mode: 0644]

index 836b69cb8d62852e266574418509b5434acdeab8..5f2e14ee42aa60c257de0ff24bd83a8fdc366cd4 100644 (file)
@@ -1,3 +1,8 @@
+2015-06-30  Marek Polacek  <polacek@redhat.com>
+
+       * fold-const.c (fold_binary_loc): Move ~X | X folding ...
+       * match.pd: ... here.
+
 2015-06-30  Richard Biener  <rguenther@suse.de>
 
        * target-insns.def (canonicalize_funcptr_for_compare): Add.
index 5cdb6d1e13d143f0b0342e352fd4554cf55fbe4f..f330d78904acde2c76eefffcd6f88104e3859d7d 100644 (file)
@@ -10922,24 +10922,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
index 0cf3d21838953a2658416e83cf38035a64f0f35a..5dcbc1a47e08d9b5fcf64bf1e65e372f32f25b0c 100644 (file)
@@ -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)
index f249a7d7ded7cefc6de69e6e0ebe71f679ebbf0f..b006b4c021bff23c561af1ab6d3a00d515878497 100644 (file)
@@ -1,3 +1,7 @@
+2015-06-30  Marek Polacek  <polacek@redhat.com>
+
+       * gcc.dg/fold-ior-2.c: New test.
+
 2015-06-30  Tom de Vries  <tom@codesourcery.com>
 
        PR tree-optimization/66652
diff --git a/gcc/testsuite/gcc.dg/fold-ior-2.c b/gcc/testsuite/gcc.dg/fold-ior-2.c
new file mode 100644 (file)
index 0000000..6abac9e
--- /dev/null
@@ -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" } } */