[前][次][番号順一覧][スレッド一覧]

ruby-changes:72884

From: Jeremy <ko1@a...>
Date: Wed, 10 Aug 2022 14:20:00 +0900 (JST)
Subject: [ruby-changes:72884] 5089b6acc7 (master): Add peephole optimizer for newarray(X)/expandarray(X, 0) -> opt_reverse(X)

https://git.ruby-lang.org/ruby.git/commit/?id=5089b6acc7

From 5089b6acc7b57605823704d28c82e286f49661e6 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 20 Jul 2022 12:28:48 -0700
Subject: 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.
---
 compile.c | 11 ++++++++++-
 insns.def |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/compile.c b/compile.c
index f38a320b76..9716f04374 100644
--- a/compile.c
+++ b/compile.c
@@ -3337,6 +3337,7 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal https://github.com/ruby/ruby/blob/trunk/compile.c#L3337
         if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
             OPERAND_AT(iobj, 0) == OPERAND_AT(next, 0) &&
             OPERAND_AT(next, 1) == INT2FIX(0)) {
+                ELEM_REMOVE(next);
                 /*
                  *  newarray 2
                  *  expandarray 2, 0
@@ -3344,10 +3345,18 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal https://github.com/ruby/ruby/blob/trunk/compile.c#L3345
                  *  swap
                  */
                 if (OPERAND_AT(iobj, 0) == INT2FIX(2)) {
-                    ELEM_REMOVE(next);
                     INSN_OF(iobj) = BIN(swap);
                     iobj->operand_size = 0;
                 }
+                /*
+                 *  newarray X
+                 *  expandarray X, 0
+                 * =>
+                 *  opt_reverse X
+                 */
+                else {
+                    INSN_OF(iobj) = BIN(opt_reverse);
+                }
         }
     }
 
diff --git a/insns.def b/insns.def
index ebdbed6237..15c4734b8b 100644
--- a/insns.def
+++ b/insns.def
@@ -599,7 +599,7 @@ swap https://github.com/ruby/ruby/blob/trunk/insns.def#L599
 
 /* reverse stack top N order. */
 DEFINE_INSN
-reverse
+opt_reverse
 (rb_num_t n)
 (...)
 (...)
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]