1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add peephole optimizer for newarray(X)/expandarray(X, 0) -> opt_reverse(X)

This renames the reverse instruction to opt_reverse, since now it
is only added by the optimizer.  Then it uses as a more general
form of swap.  This optimizes multiple assignment in the popped
case with more than two elements.
This commit is contained in:
Jeremy Evans 2022-07-20 12:28:48 -07:00
parent d9167491db
commit 5089b6acc7
Notes: git 2022-08-10 14:20:08 +09:00
2 changed files with 11 additions and 2 deletions

View file

@ -3337,6 +3337,7 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal
if (IS_INSN(next) && IS_INSN_ID(next, expandarray) && if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
OPERAND_AT(iobj, 0) == OPERAND_AT(next, 0) && OPERAND_AT(iobj, 0) == OPERAND_AT(next, 0) &&
OPERAND_AT(next, 1) == INT2FIX(0)) { OPERAND_AT(next, 1) == INT2FIX(0)) {
ELEM_REMOVE(next);
/* /*
* newarray 2 * newarray 2
* expandarray 2, 0 * expandarray 2, 0
@ -3344,10 +3345,18 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal
* swap * swap
*/ */
if (OPERAND_AT(iobj, 0) == INT2FIX(2)) { if (OPERAND_AT(iobj, 0) == INT2FIX(2)) {
ELEM_REMOVE(next);
INSN_OF(iobj) = BIN(swap); INSN_OF(iobj) = BIN(swap);
iobj->operand_size = 0; iobj->operand_size = 0;
} }
/*
* newarray X
* expandarray X, 0
* =>
* opt_reverse X
*/
else {
INSN_OF(iobj) = BIN(opt_reverse);
}
} }
} }

View file

@ -599,7 +599,7 @@ swap
/* reverse stack top N order. */ /* reverse stack top N order. */
DEFINE_INSN DEFINE_INSN
reverse opt_reverse
(rb_num_t n) (rb_num_t n)
(...) (...)
(...) (...)