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]

[Committed] PR13827: Fold (X&C) != (Y&C) as ((X^Y)&C) != 0


The following patch resolves the enhancement request PR tree-opt/13827.
The optimization is to transform equality/inequality expressions such as
"(X & 0xf0) == (Y & 0xf0)" into "((X ^ Y) & 0xf0) == 0".  The belief is
that these forms of expressions are frequent in testing flags and
bitfields.

The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap", all default languages including Ada, and regression
tested with a top-level "make -k check" with no new failures.

Committed to mainline as revision 118727.


2006-11-12  Roger Sayle  <roger@eyesopen.com>

	PR tree-optimization/13827
	* fold-const.c (fold_binary) <EQ_EXPR, NE_EXPR>: Fold (X&C) op (Y&C)
	as ((X^Y)&C) op 0.

	* gcc.dg/fold-eqand-1.c: New test case.


Index: fold-const.c
===================================================================
*** fold-const.c	(revision 118685)
--- fold-const.c	(working copy)
*************** fold_binary (enum tree_code code, tree t
*** 10801,10806 ****
--- 10801,10849 ----
  			    TREE_OPERAND (arg0, 0),
  			    TREE_OPERAND (arg1, 0));

+       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
+       if (TREE_CODE (arg0) == BIT_AND_EXPR
+ 	  && TREE_CODE (arg1) == BIT_AND_EXPR)
+ 	{
+ 	  tree arg00 = TREE_OPERAND (arg0, 0);
+ 	  tree arg01 = TREE_OPERAND (arg0, 1);
+ 	  tree arg10 = TREE_OPERAND (arg1, 0);
+ 	  tree arg11 = TREE_OPERAND (arg1, 1);
+ 	  tree itype = TREE_TYPE (arg0);
+
+ 	  if (operand_equal_p (arg01, arg11, 0))
+ 	    return fold_build2 (code, type,
+ 				fold_build2 (BIT_AND_EXPR, itype,
+ 					     fold_build2 (BIT_XOR_EXPR, itype,
+ 							  arg00, arg10),
+ 					     arg01),
+ 				build_int_cst (itype, 0));
+
+ 	  if (operand_equal_p (arg01, arg10, 0))
+ 	    return fold_build2 (code, type,
+ 				fold_build2 (BIT_AND_EXPR, itype,
+ 					     fold_build2 (BIT_XOR_EXPR, itype,
+ 							  arg00, arg11),
+ 					     arg01),
+ 				build_int_cst (itype, 0));
+
+ 	  if (operand_equal_p (arg00, arg11, 0))
+ 	    return fold_build2 (code, type,
+ 				fold_build2 (BIT_AND_EXPR, itype,
+ 					     fold_build2 (BIT_XOR_EXPR, itype,
+ 							  arg01, arg10),
+ 					     arg00),
+ 				build_int_cst (itype, 0));
+
+ 	  if (operand_equal_p (arg00, arg10, 0))
+ 	    return fold_build2 (code, type,
+ 				fold_build2 (BIT_AND_EXPR, itype,
+ 					     fold_build2 (BIT_XOR_EXPR, itype,
+ 							  arg01, arg11),
+ 					     arg00),
+ 				build_int_cst (itype, 0));
+ 	}
+
        return NULL_TREE;

      case LT_EXPR:


/* PR tree-optimization/13827 */
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-original" } */

unsigned foo (unsigned a, unsigned b)
{
  return (a & 0xff00) != (b & 0xff00);
}

unsigned bar (unsigned c, unsigned d)
{
  return (c & 0xff00) == (d & 0xff00);
}

/* { dg-final { scan-tree-dump-times "a \\^ b" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "c \\^ d" 1 "original" } } */
/* { dg-final { cleanup-tree-dump "original" } } */


Roger
--


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