fold-const.c (fold_binary): Fold ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.
authorJames A. Morrison <phython@gcc.gnu.org>
Wed, 20 Apr 2005 04:32:41 +0000 (04:32 +0000)
committerJames A. Morrison <phython@gcc.gnu.org>
Wed, 20 Apr 2005 04:32:41 +0000 (04:32 +0000)
2005-04-19  James A. Morrison  <phython@gcc.gnu.org>

* fold-const.c (fold_binary): Fold ~(X ^ Y) to ~X ^ Y or X ^ ~Y if
~X or ~Y simplify.

From-SVN: r98435

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

index 237ebcd4e88e607a891c910faa0bd89a15639167..f4737d780d3805f282f3dcda9f917f004bd64326 100644 (file)
@@ -1,3 +1,8 @@
+2005-04-19  James A. Morrison  <phython@gcc.gnu.org>
+
+       * fold-const.c (fold_binary): Fold ~(X ^ Y) to ~X ^ Y or X ^ ~Y if
+       ~X or ~Y simplify.
+
 2005-04-19  James A. Morrison  <phython@gcc.gnu.org>
 
        * fold-const (fold_binary):  Fold ~X ^ ~ Y to X ^ Y.
index 2c90d0c80deac47b961f4461231125f0d1e2144a..2ed556b27307ffbd40d2b50fc1c22b03bec0f11e 100644 (file)
@@ -7015,6 +7015,20 @@ fold_unary (enum tree_code code, tree type, tree op0)
                   || (TREE_CODE (arg0) == PLUS_EXPR
                       && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
        return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
+      /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
+      else if (TREE_CODE (arg0) == BIT_XOR_EXPR
+              && (tem = fold_unary (BIT_NOT_EXPR, type,
+                                    fold_convert (type,
+                                                  TREE_OPERAND (arg0, 0)))))
+       return fold_build2 (BIT_XOR_EXPR, type, tem,
+                           fold_convert (type, TREE_OPERAND (arg0, 1)));
+      else if (TREE_CODE (arg0) == BIT_XOR_EXPR
+              && (tem = fold_unary (BIT_NOT_EXPR, type,
+                                    fold_convert (type,
+                                                  TREE_OPERAND (arg0, 1)))))
+       return fold_build2 (BIT_XOR_EXPR, type,
+                           fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
+
       return NULL_TREE;
 
     case TRUTH_NOT_EXPR:
index 1782fed8405d61c24875dd09fb04d9edcb323c8d..c7d9f2ec93395f57c67347f0a8a11f6a2680e572 100644 (file)
@@ -1,6 +1,7 @@
 2005-04-19  James A. Morrison  <phython@gcc.gnu.org>
 
        * gcc.dg/fold-xor-1.c: New test.
+       * gcc.dg/fold-xor-2.c: New test.
 
 2005-04-19  James E. Wilson  <wilson@specifixinc.com>
 
diff --git a/gcc/testsuite/gcc.dg/fold-xor-2.c b/gcc/testsuite/gcc.dg/fold-xor-2.c
new file mode 100644 (file)
index 0000000..40c2c5d
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-generic" } */
+int f (int a, int b) {
+       return ~(a ^ -(b + 1));
+}
+
+int g (int a, int b) {
+       return b ^ a;
+}
+
+unsigned int h (unsigned int a, unsigned int b) {
+       return ~(-(b + 1) ^ a);
+}
+
+/* { dg-final { scan-tree-dump-times "b \\^ a" 3 "generic" } } */
+/* { dg-final { cleanup-tree-dump "generic" } } */