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-const.c: Fold (A & ~3) - (A & 3) into (A ^ 3) - 3.


Hi,

Attached is a patch to fold (A & ~B) - (A & B) into (A ^ B) - B, where
B is any power of 2 minus 1.

Here is a quick proof:

(A & ~3) - (A & 3) = (A & ~3) + ~(A & 3) + 1
                   = (A & ~3) + (-4) + (~A & 3) + 1
                   = (A & ~3) + (~A & 3) - 3
                   = (A ^ 3) - 3

This occurs in the code that swaps byte order like so

  array[(index & ~3) + (3 - (index & 3))]

With this patch, this is simplified down to

  array[index ^ 3]

Tested on h8300 port.  OK to apply?

Kazu Hirata

2003-09-29  Kazu Hirata  <kazu@cs.umass.edu>

	* fold-const.c (fold): Fold (A & ~B) - (A & B) into
	(A ^ B) - B, where B is any power of 2 minus 1.

Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.306
diff -u -r1.306 fold-const.c
--- fold-const.c	25 Sep 2003 02:12:13 -0000	1.306
+++ fold-const.c	29 Sep 2003 05:49:21 -0000
@@ -6017,6 +6017,29 @@
 						  TREE_OPERAND (arg1, 1))),
 				    arg0));
 	    }
+
+	  /* Fold (A & ~3) - (A & 3) into (A ^ 3) - 3.  */
+	  if (TREE_CODE (arg0) == BIT_AND_EXPR
+	      && TREE_CODE (arg1) == BIT_AND_EXPR
+	      && !TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 0))
+	      && operand_equal_p (TREE_OPERAND (arg0, 0),
+				  TREE_OPERAND (arg1, 0), 0)
+	      && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
+	      && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST)
+	    {
+	      tree mask0 = TREE_OPERAND (arg0, 1);
+	      tree mask1 = TREE_OPERAND (arg1, 1);
+	      tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
+	      
+	      if (operand_equal_p (tem, mask1, 0)
+		  && integer_pow2p (fold (build (PLUS_EXPR, type,
+						 mask1, integer_one_node))))
+		{
+		  tem = fold (build (BIT_XOR_EXPR, type,
+				     TREE_OPERAND (arg0, 0), mask1));
+		  return fold (build (MINUS_EXPR, type, tem, mask1));
+		}
+	    }
 	}
 
       /* See if ARG1 is zero and X - ARG1 reduces to X.  */


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