[JAVA] Optimize binary operators with equal operands.
Roger Sayle
roger@eyesopen.com
Fri Sep 19 21:12:00 GMT 2003
The following patch to gcj's bytecode generation routines optimizes
the case where a binary arithmetic or logical operation is applied
to equal operands without side-effects. Instead of expanding the
bytecode to evaluate the operand twice, we emit a "dup" to duplicate
the operand on the top of the stack. This use of operand_equal_p is
identical to that in GCC's RTL expansion (expand_operands in expr.c).
You'll also notice that this logic is a generalization of the existing
test in generate_bytecode_insns. Suffice to say that if arg0 == arg1
and TREE_CODE (arg0) == SAVE_EXPR, then operand_equal_p (arg0, arg1, 0)
will return true.
But the real underlying motivation for this patch is the change on the
second line that, as a side-effect, explicitly expands the SAVE_EXPR node.
With this tweak, all SAVE_EXPR nodes are now expanded through a single
case in generate_bytecode_insns, but without affecting the current
behaviour.
The following patch has been tested on i686-pc-linux-gnu with a complete
"make bootstrap", all languages except treelang, and regression tested
with a top-level "make -k check" with no new failures.
Ok for mainline?
2003-09-19 Roger Sayle <roger@eyesopen.com>
* jcf-write.c (generate_bytecode_insns): Optimize binary operations
with equal operands without side-effects.
Index: jcf-write.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/jcf-write.c,v
retrieving revision 1.132
diff -c -3 -p -r1.132 jcf-write.c
*** jcf-write.c 18 Sep 2003 14:55:43 -0000 1.132
--- jcf-write.c 19 Sep 2003 16:53:11 -0000
*************** generate_bytecode_insns (tree exp, int t
*** 2095,2104 ****
tree arg0 = TREE_OPERAND (exp, 0);
tree arg1 = TREE_OPERAND (exp, 1);
jopcode += adjust_typed_op (type, 3);
! if (arg0 == arg1 && TREE_CODE (arg0) == SAVE_EXPR)
{
/* fold may (e.g) convert 2*x to x+x. */
! generate_bytecode_insns (TREE_OPERAND (arg0, 0), target, state);
emit_dup (TYPE_PRECISION (TREE_TYPE (arg0)) > 32 ? 2 : 1, 0, state);
}
else
--- 2095,2104 ----
tree arg0 = TREE_OPERAND (exp, 0);
tree arg1 = TREE_OPERAND (exp, 1);
jopcode += adjust_typed_op (type, 3);
! if (arg0 != NULL_TREE && operand_equal_p (arg0, arg1, 0))
{
/* fold may (e.g) convert 2*x to x+x. */
! generate_bytecode_insns (arg0, target, state);
emit_dup (TYPE_PRECISION (TREE_TYPE (arg0)) > 32 ? 2 : 1, 0, state);
}
else
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
More information about the Java-patches
mailing list