This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Fold ~(X ^ Y) if ~X or ~Y simplify


 Hi,

  This patch folds ~(X^Y) into ~X^Y or X^~Y if ~X or ~Y simplify.  Bootstrapped
and regtested on ia64-linux.  Ok for mainline?

-- 
Thanks,
Jim

http://www.csclub.uwaterloo.ca/~ja2morri/
http://phython.blogspot.com
http://open.nit.ca/wiki/?page=jim

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.

Index: fold-const.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.564
diff -u -p -r1.564 fold-const.c
--- fold-const.c	19 Apr 2005 02:35:52 -0000	1.564
+++ fold-const.c	20 Apr 2005 02:13:10 -0000
@@ -7015,6 +7015,20 @@ fold_unary (enum tree_code code, tree ty
 		   || (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:
testsuite:
        * gcc.dg/fold-xor-2.c: New test.

/* { 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" } } */

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]