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 some comparisons of bit operations


Hello,

this patch adds two transformations to fold (descriptions see in
comments in patches).

Zdenek

Changelog:
	* fold-const.c (fold): Fold some comparisons of bit operations.

Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.226.2.8
diff -c -3 -p -r1.226.2.8 fold-const.c
*** fold-const.c	3 Jul 2003 16:33:20 -0000	1.226.2.8
--- fold-const.c	21 Jul 2003 18:27:33 -0000
*************** fold (expr)
*** 6504,6509 ****
--- 6504,6537 ----
  	  }
  	}
  
+       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
+ 	 Similarly for NE_EXPR.  */
+       if ((code == EQ_EXPR || code == NE_EXPR)
+ 	  && TREE_CODE (arg0) == BIT_AND_EXPR
+ 	  && TREE_CODE (arg1) == INTEGER_CST
+ 	  && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
+ 	{
+ 	  tree dandnotc = fold (build (BIT_ANDTC_EXPR, TREE_TYPE (arg0),
+ 				       arg1, TREE_OPERAND (arg0, 1)));
+ 	  tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
+ 	  if (!integer_zerop (dandnotc))
+ 	    return omit_one_operand (type, rslt, arg0);
+ 	}
+ 
+       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
+ 	 Similarly for NE_EXPR.  */
+       if ((code == EQ_EXPR || code == NE_EXPR)
+ 	  && TREE_CODE (arg0) == BIT_IOR_EXPR
+ 	  && TREE_CODE (arg1) == INTEGER_CST
+ 	  && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
+ 	{
+ 	  tree candnotd = fold (build (BIT_ANDTC_EXPR, TREE_TYPE (arg0),
+ 				       TREE_OPERAND (arg0, 1), arg1));
+ 	  tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
+ 	  if (!integer_zerop (candnotd))
+ 	    return omit_one_operand (type, rslt, arg0);
+ 	}
+ 
        /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
  	 and similarly for >= into !=.  */
        if ((code == LT_EXPR || code == GE_EXPR)


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