From 3060758b3c88994eee5a4be8d3f29eded81eb201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Fri, 19 Aug 2016 14:47:40 +0200 Subject: Strengthen test case added in 8b83bc0b Don't only test the case that failed; test it exhaustively. --- lib/compiler/test/match_SUITE.erl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/compiler/test/match_SUITE.erl b/lib/compiler/test/match_SUITE.erl index 127679ba69..52b2da05f7 100644 --- a/lib/compiler/test/match_SUITE.erl +++ b/lib/compiler/test/match_SUITE.erl @@ -576,7 +576,15 @@ grab_bag_remove_failure([{stretch,_,Mi}=Stretch | Specs], Unit, _MaxFailure) -> %% Regression in 19.0, reported by Alexei Sholik literal_binary(_Config) -> - 3 = literal_binary_match(bar,<<"y">>), + 3 = literal_binary_match(bar, <<"y">>), + + %% While we are at it, also test the remaining code paths + %% in literal_binary_match/2. + 1 = literal_binary_match(bar, <<"x">>), + 2 = literal_binary_match(foo, <<"x">>), + 3 = literal_binary_match(foo, <<"y">>), + fail = literal_binary_match(bar, <<"z">>), + fail = literal_binary_match(foo, <<"z">>), ok. literal_binary_match(bar, <<"x">>) -> 1; -- cgit v1.2.3 From a66a2e84ec4c6fb7c19affe43134570caabe8569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Fri, 19 Aug 2016 14:07:38 +0200 Subject: Reinstate optimization of binary literal matching 105c5b0071 was reverted in dd1162846e because clauses that were supposed to match would not match. (See 8b83bc0b.) Reintroduce the optimization, but make sure that we only shortcut bs_context_to_binary instructions and not bs_start_match2 instructions. --- lib/compiler/src/beam_dead.erl | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/compiler/src/beam_dead.erl b/lib/compiler/src/beam_dead.erl index 6f6d742293..3606af9d75 100644 --- a/lib/compiler/src/beam_dead.erl +++ b/lib/compiler/src/beam_dead.erl @@ -272,14 +272,18 @@ backward([{jump,{f,To}}=J|[{bif,Op,_,Ops,Reg}|Is]=Is0], D, Acc) -> catch throw:not_possible -> backward(Is0, D, [J|Acc]) end; -backward([{test,bs_start_match2,F,_,[R,_],Ctxt}=I|Is], D, +backward([{test,bs_start_match2,F,Live,[R,_]=Args,Ctxt}|Is], D, [{test,bs_match_string,F,[Ctxt,Bs]}, {test,bs_test_tail2,F,[Ctxt,0]}|Acc0]=Acc) -> + {f,To0} = F, case beam_utils:is_killed(Ctxt, Acc0, D) of true -> - Eq = {test,is_eq_exact,F,[R,{literal,Bs}]}, + To = shortcut_bs_context_to_binary(To0, R, D), + Eq = {test,is_eq_exact,{f,To},[R,{literal,Bs}]}, backward(Is, D, [Eq|Acc0]); false -> + To = shortcut_bs_start_match(To0, R, D), + I = {test,bs_start_match2,{f,To},Live,Args,Ctxt}, backward(Is, D, [I|Acc]) end; backward([{test,bs_start_match2,{f,To0},Live,[Src|_]=Info,Dst}|Is], D, Acc) -> @@ -551,6 +555,21 @@ shortcut_bs_start_match_1([{test,bs_start_match2,{f,To},_,[Reg|_],_}|_], shortcut_bs_start_match_1(_, _, To, _) -> To. +%% shortcut_bs_context_to_binary(TargetLabel, Reg) -> TargetLabel +%% If a bs_start_match2 instruction has been eliminated, the +%% bs_context_to_binary instruction can be eliminated too. + +shortcut_bs_context_to_binary(To, Reg, D) -> + shortcut_bs_ctb_1(beam_utils:code_at(To, D), Reg, To, D). + +shortcut_bs_ctb_1([{bs_context_to_binary,Reg}|Is], Reg, To, D) -> + shortcut_bs_ctb_1(Is, Reg, To, D); +shortcut_bs_ctb_1([{jump,{f,To}}|_], Reg, _, D) -> + Code = beam_utils:code_at(To, D), + shortcut_bs_ctb_1(Code, Reg, To, D); +shortcut_bs_ctb_1(_, _, To, _) -> + To. + %% shortcut_rel_op(FailLabel, Operator, [Operand], D) -> FailLabel' %% Try to shortcut the given test instruction. Example: %% -- cgit v1.2.3