fold-const.c (fold_binary_loc): Move A - (A & B) into ~B & A ...
authorMarek Polacek <polacek@redhat.com>
Fri, 17 Jul 2015 16:03:27 +0000 (16:03 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Fri, 17 Jul 2015 16:03:27 +0000 (16:03 +0000)
* fold-const.c (fold_binary_loc): Move A - (A & B) into ~B & A ...
* match.pd: ... here.

* gcc.dg/fold-minus-7.c: New test.

From-SVN: r225938

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

index d36d6b57b7ba817a5fdbecec08b8a4b30ae3ee99..f0d08e96e193bf4b18932e992deb81baf4e02c21 100644 (file)
@@ -1,3 +1,8 @@
+2015-07-17  Marek Polacek  <polacek@redhat.com>
+
+       * fold-const.c (fold_binary_loc): Move A - (A & B) into ~B & A ...
+       * match.pd: ... here.
+
 2015-07-17  Nathan Sidwell  <nathan@codesourcery.com>
 
        * config/nvptx/mkoffload.c (process): Constify target data.
index 93dd29d6729b2d445ea12ca87dd62980913d55a9..fa321f455d2ce430dcc6be642eac98f22380102d 100644 (file)
@@ -9777,30 +9777,6 @@ fold_binary_loc (location_t loc,
 
       if (! FLOAT_TYPE_P (type))
        {
-         /* Fold A - (A & B) into ~B & A.  */
-         if (!TREE_SIDE_EFFECTS (arg0)
-             && TREE_CODE (arg1) == BIT_AND_EXPR)
-           {
-             if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
-               {
-                 tree arg10 = fold_convert_loc (loc, type,
-                                                TREE_OPERAND (arg1, 0));
-                 return fold_build2_loc (loc, BIT_AND_EXPR, type,
-                                     fold_build1_loc (loc, BIT_NOT_EXPR,
-                                                  type, arg10),
-                                     fold_convert_loc (loc, type, arg0));
-               }
-             if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
-               {
-                 tree arg11 = fold_convert_loc (loc,
-                                                type, TREE_OPERAND (arg1, 1));
-                 return fold_build2_loc (loc, BIT_AND_EXPR, type,
-                                     fold_build1_loc (loc, BIT_NOT_EXPR,
-                                                  type, arg11),
-                                     fold_convert_loc (loc, type, arg0));
-               }
-           }
-
          /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
             any power of 2 minus 1.  */
          if (TREE_CODE (arg0) == BIT_AND_EXPR
index c335ada567ed82b164aa390cfdf0ddd2b075d0b2..700a692229537863b28220453390bb82c9545b61 100644 (file)
@@ -662,6 +662,12 @@ along with GCC; see the file COPYING3.  If not see
  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
 
+/* Fold A - (A & B) into ~B & A.  */
+(simplify
+ (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
+ (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
+      && tree_nop_conversion_p (type, TREE_TYPE (@1)))
+  (convert (bit_and (bit_not @1) @0))))
 
 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
 (simplify
index 5462c1fd02036491b15784c56b127f075524700e..8a30395e3486413f4380d3556fef377f94d31a8e 100644 (file)
@@ -1,3 +1,7 @@
+2015-07-17  Marek Polacek  <polacek@redhat.com>
+
+       * gcc.dg/fold-minus-7.c: New test.
+
 2015-07-17  Alessandro Fanfarillo  <fanfarillo.gcc@gmail.com>
 
        * gfortran.dg/co_reduce_1.f90: New file.
diff --git a/gcc/testsuite/gcc.dg/fold-minus-7.c b/gcc/testsuite/gcc.dg/fold-minus-7.c
new file mode 100644 (file)
index 0000000..7a49faa
--- /dev/null
@@ -0,0 +1,36 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+f1 (int a, int b)
+{
+  int tem = a & b;
+  return a - tem;
+}
+
+int
+f2 (int a, int b)
+{
+  int tem = b & a;
+  return a - tem;
+}
+
+int
+f3 (unsigned int a, int b)
+{
+  return a - (a & b);
+}
+
+int
+f4 (int a, unsigned int b)
+{
+  return a - (a & b);
+}
+
+int
+f5 (int a, int b)
+{
+  return a - (unsigned) (b & a);
+}
+
+/* { dg-final { scan-tree-dump-not " - " "cddce1" } } */