This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[Committed] Constant fold x&x, x|x and x^x
- From: Roger Sayle <roger at eyesopen dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sun, 7 Mar 2004 12:00:35 -0700 (MST)
- Subject: [Committed] Constant fold x&x, x|x and x^x
The following patch improves constant folding of bit-wise operators
with duplicate operands. It folds x&x and x|x into x, and folds x^x
into zero. The use of "operand_equal_p" avoids potential problems
with side-effects, and the XOR_EXPR case also uses omit_one_operand.
These transformations are already performed at the RTL-level, but
adding them to fold should both benefit tree-ssa and reduce the
amount of RTL we initially generate.
The following patch has been tested on i686-pc-linux-gnu with a
bootstrap, and regression tested with no new failures.
Committed to mainline.
2004-03-07 Roger Sayle <roger@eyesopen.com>
* fold-const.c (fold) <IOR_EXPR>: Fold x | x as x.
<XOR_EXPR>: Fold x ^ x as zero.
<AND_EXPR>: Fold x & x as x.
Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.337
diff -c -3 -p -r1.337 fold-const.c
*** fold-const.c 28 Feb 2004 07:33:10 -0000 1.337
--- fold-const.c 7 Mar 2004 16:58:58 -0000
*************** fold (tree expr)
*** 6621,6626 ****
--- 6621,6628 ----
return omit_one_operand (type, arg1, arg0);
if (integer_zerop (arg1))
return non_lvalue (fold_convert (type, arg0));
+ if (operand_equal_p (arg0, arg1, 0))
+ return non_lvalue (fold_convert (type, arg0));
t1 = distribute_bit_expr (code, type, arg0, arg1);
if (t1 != NULL_TREE)
return t1;
*************** fold (tree expr)
*** 6649,6654 ****
--- 6651,6658 ----
return non_lvalue (fold_convert (type, arg0));
if (integer_all_onesp (arg1))
return fold (build1 (BIT_NOT_EXPR, type, arg0));
+ if (operand_equal_p (arg0, arg1, 0))
+ return omit_one_operand (type, integer_zero_node, arg0);
/* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
with a constant, and the two constants have no bits in common,
*************** fold (tree expr)
*** 6675,6680 ****
--- 6679,6686 ----
return non_lvalue (fold_convert (type, arg0));
if (integer_zerop (arg1))
return omit_one_operand (type, arg1, arg0);
+ if (operand_equal_p (arg0, arg1, 0))
+ return non_lvalue (fold_convert (type, arg0));
t1 = distribute_bit_expr (code, type, arg0, arg1);
if (t1 != NULL_TREE)
return t1;
Roger
--
Roger Sayle, E-mail: roger@eyesopen.com
OpenEye Scientific Software, WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road, Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507. Fax: (+1) 505-473-0833