Skip to content

Commit

Permalink
Only convert "safe" BIFs to guard BIFs in try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorng committed Nov 7, 2024
1 parent 6d3c495 commit 9b41d4f
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/compiler/src/beam_ssa_opt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ reduce_try_is([#b_set{op={succeeded,body}}=I0|Is], Acc) ->
reduce_try_is(Is, [I|Acc]);
reduce_try_is([#b_set{op=call,args=[#b_remote{mod=#b_literal{val=M},
name=#b_literal{val=F},arity=A}|Args]}=I0|Is], Acc) ->
case erl_bifs:is_pure(M, F, A) of
case is_safe_as_guard_bif(M, F, A) of
true ->
I = I0#b_set{op={bif,F},args=Args},
reduce_try_is(Is, [I|Acc]);
Expand All @@ -1913,6 +1913,12 @@ reduce_try_is([#b_set{op=Op}=I|Is], Acc) ->
reduce_try_is([], Acc) ->
{safe,reverse(Acc)}.

is_safe_as_guard_bif(erlang, binary_to_atom, 1) -> true;
is_safe_as_guard_bif(erlang, binary_to_existing_atom, 1) -> true;
is_safe_as_guard_bif(erlang, list_to_atom, 1) -> true;
is_safe_as_guard_bif(erlang, list_to_existing_atom, 1) -> true;
is_safe_as_guard_bif(_, _, _) -> false.

%% Removes try/catch expressions whose expressions will never throw.
%%
%% We walk backwards through all blocks, maintaining a set of potentially
Expand Down

0 comments on commit 9b41d4f

Please sign in to comment.