ChangeLog gcc/
authorKai Tietz <ktietz@redhat.com>
Wed, 20 Apr 2011 16:16:28 +0000 (18:16 +0200)
committerKai Tietz <ktietz@gcc.gnu.org>
Wed, 20 Apr 2011 16:16:28 +0000 (18:16 +0200)
2011-04-20  Kai Tietz  <ktietz@redhat.com>

* fold-const.c (fold_binary_loc): Add handling for
(X & ~Y) | (~X & Y) and (X && !Y) | (!X && Y) optimization
to (X ^ Y).

ChangeLog gcc/testsuite

2011-04-20  Kai Tietz  <ktietz@redhat.com>

* gcc.dg/binio-xor1.c: New test.
* gcc.dg/binio-xor2.c: New test.
* gcc.dg/binio-xor3.c: New test.
* gcc.dg/binio-xor4.c: New test.
* gcc.dg/binio-xor5.c: New test.

From-SVN: r172776

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/binop-xor1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/binop-xor2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/binop-xor3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/binop-xor4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/binop-xor5.c [new file with mode: 0644]

index d64225c5a9d21dbf758decf9d126cf92dd606933..cdb4d3ba4968b56fb28b25962ee82531dd304462 100644 (file)
@@ -1,3 +1,9 @@
+2011-04-20  Kai Tietz  <ktietz@redhat.com>
+
+       * fold-const.c (fold_binary_loc): Add handling for
+       (X & ~Y) | (~X & Y) and (X && !Y) | (!X && Y) optimization
+       to (X ^ Y).
+
 2011-04-20  Andrew Stubbs  <ams@codesourcery.com>
 
        * config/arm/arm.c (arm_gen_constant): Remove redundant can_invert.
index 8d543c4080fb33ac28304939661ba6c5a0642cd8..c4bf08819b38002087f4619c5336768da7cdd4f9 100644 (file)
@@ -10660,6 +10660,28 @@ fold_binary_loc (location_t loc,
          && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
        return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
 
+      /* (X & ~Y) | (~X & Y) is X ^ Y */
+      if (TREE_CODE (arg0) == BIT_AND_EXPR
+         && TREE_CODE (arg1) == BIT_AND_EXPR)
+        {
+         tree a0, a1, l0, l1, n0, n1;
+
+         a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
+         a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
+
+         l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
+         l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
+         
+         n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
+         n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
+         
+         if ((operand_equal_p (n0, a0, 0)
+              && operand_equal_p (n1, a1, 0))
+             || (operand_equal_p (n0, a1, 0)
+                 && operand_equal_p (n1, a0, 0)))
+           return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
+       }
+
       t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
       if (t1 != NULL_TREE)
        return t1;
@@ -12039,6 +12061,27 @@ fold_binary_loc (location_t loc,
          && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
        return omit_one_operand_loc (loc, type, integer_one_node, arg0);
 
+      /* (X && !Y) || (!X && Y) is X ^ Y */
+      if (TREE_CODE (arg0) == TRUTH_AND_EXPR
+         && TREE_CODE (arg1) == TRUTH_AND_EXPR)
+        {
+         tree a0, a1, l0, l1, n0, n1;
+
+         a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
+         a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
+
+         l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
+         l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
+         
+         n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
+         n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
+         
+         if ((operand_equal_p (n0, a0, 0)
+              && operand_equal_p (n1, a1, 0))
+             || (operand_equal_p (n0, a1, 0)
+                 && operand_equal_p (n1, a0, 0)))
+           return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
+       }
       goto truth_andor;
 
     case TRUTH_XOR_EXPR:
index 5c792f1670a006beb67b5cf09a5433947f5728f6..95646de029a4d1cd2e2329adad5541def286bc05 100644 (file)
@@ -1,3 +1,11 @@
+2011-04-20  Kai Tietz  <ktietz@redhat.com>
+
+       * gcc.dg/binio-xor1.c: New test.
+       * gcc.dg/binio-xor2.c: New test.
+       * gcc.dg/binio-xor3.c: New test.
+       * gcc.dg/binio-xor4.c: New test.
+       * gcc.dg/binio-xor5.c: New test.
+
 2011-04-20  Richard Guenther  <rguenther@suse.de>
 
        PR tree-optimization/47892
diff --git a/gcc/testsuite/gcc.dg/binop-xor1.c b/gcc/testsuite/gcc.dg/binop-xor1.c
new file mode 100644 (file)
index 0000000..7e46286
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (int a, int b, int c)
+{
+  return ((a && !b && c) || (!a && b && c));
+}
+
+/* We expect to see "<bb N>"; confirm that, so that we know to count
+   it in the real test.  */
+/* { dg-final { scan-tree-dump-times "<bb\[^>\]*>" 5 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\^" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/binop-xor2.c b/gcc/testsuite/gcc.dg/binop-xor2.c
new file mode 100644 (file)
index 0000000..369b350
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (int a, int b)
+{
+  return ((a & ~b) | (~a & b));
+}
+
+/* We expect to see "<bb N>"; confirm that, so that we know to count
+   it in the real test.  */
+/* { dg-final { scan-tree-dump-times "<bb\[^>\]*>" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\^" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/binop-xor3.c b/gcc/testsuite/gcc.dg/binop-xor3.c
new file mode 100644 (file)
index 0000000..ef4b82f
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (int a, int b)
+{
+  return ((a && !b) || (!a && b));
+}
+
+/* We expect to see "<bb N>"; confirm that, so that we know to count
+   it in the real test.  */
+/* { dg-final { scan-tree-dump-times "<bb\[^>\]*>" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\^" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/binop-xor4.c b/gcc/testsuite/gcc.dg/binop-xor4.c
new file mode 100644 (file)
index 0000000..af228d9
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (int a, int b, int c)
+{
+  return ((a & ~b) | (~a & b)) & c;
+}
+
+/* We expect to see "<bb N>"; confirm that, so that we know to count
+   it in the real test.  */
+/* { dg-final { scan-tree-dump-times "<bb\[^>\]*>" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\^" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/binop-xor5.c b/gcc/testsuite/gcc.dg/binop-xor5.c
new file mode 100644 (file)
index 0000000..5f2b8df
--- /dev/null
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+foo (int a, int b, int c)
+{
+  return ((a & ~b & c) | (~a & b & c));
+}
+
+/* We expect to see "<bb N>"; confirm that, so that we know to count
+   it in the real test.  */
+/* { dg-final { scan-tree-dump-times "<bb\[^>\]*>" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\^" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\&" 1 "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */