ruby-changes:51658
From: nobu <ko1@a...>
Date: Fri, 6 Jul 2018 17:02:03 +0900 (JST)
Subject: [ruby-changes:51658] nobu:r63870 (trunk): compile.c: remove unreachable jump only
nobu 2018-07-06 17:01:58 +0900 (Fri, 06 Jul 2018) New Revision: 63870 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63870 Log: compile.c: remove unreachable jump only * compile.c (iseq_peephole_optimize): remove unreachable jump instruction only. if it is labeled and referred from other instructions, it is reachable and must not be removed. [ruby-core:87830] [Bug #14897] Modified files: trunk/compile.c trunk/test/ruby/test_optimization.rb Index: compile.c =================================================================== --- compile.c (revision 63869) +++ compile.c (revision 63870) @@ -2730,12 +2730,13 @@ iseq_peephole_optimize(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/compile.c#L2730 ELEM_INSERT_NEXT(&dniobj->link, &pop->link); goto again; } - else if (IS_INSN(iobj->link.prev) && - (piobj = (INSN *)iobj->link.prev) && + else if ((piobj = (INSN *)get_prev_insn(iobj)) != 0 && (IS_INSN_ID(piobj, branchif) || IS_INSN_ID(piobj, branchunless))) { INSN *pdiobj = (INSN *)get_destination_insn(piobj); if (niobj == pdiobj) { + int refcnt = IS_LABEL(piobj->link.next) ? + ((LABEL *)piobj->link.next)->refcnt : 0; /* * useless jump elimination (if/unless destination): * if L1 @@ -2753,7 +2754,12 @@ iseq_peephole_optimize(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/compile.c#L2754 piobj->insn_id = (IS_INSN_ID(piobj, branchif)) ? BIN(branchunless) : BIN(branchif); replace_destination(piobj, iobj); - ELEM_REMOVE(&iobj->link); + if (refcnt <= 1) { + ELEM_REMOVE(&iobj->link); + } + else { + /* TODO: replace other branch destinations too */ + } return COMPILE_OK; } else if (diobj == pdiobj) { Index: test/ruby/test_optimization.rb =================================================================== --- test/ruby/test_optimization.rb (revision 63869) +++ test/ruby/test_optimization.rb (revision 63870) @@ -771,4 +771,17 @@ class TestRubyOptimization < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_optimization.rb#L771 tap {true || tap {}} end; end + + def test_jump_elimination_with_optimized_out_block + x = Object.new + def x.bug(obj) + if obj || obj + obj = obj + else + raise "[ruby-core:87830] [Bug #14897]" + end + obj + end + assert_equal(:ok, x.bug(:ok)) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/