Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor type annotation handling for clarity #8586

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/compiler/src/beam_ssa_type.erl
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,30 @@ opt_is([I0 | Is], Ts0, Ds0, Ls, Fdb, Sub0, Meta, Acc) ->
opt_is([], Ts, Ds, _Ls, Fdb, Sub, _Meta, Acc) ->
{reverse(Acc), Ts, Ds, Fdb, Sub}.

opt_anno_types(#b_set{op=Op,args=Args}=I, Ts) ->
%% opt_anno_types(Instruction0, Types) -> Instruction.
%% Maintain the invariant that the `arg_types` annotation only
%% contains type annotations for variable arguments whose types are
%% more specific than `any`. Literal arguments must not have any type
%% annotation.
%%
%% Also, ensure that the `arg_types` annotation is only present
%% in instructions that can benefit from it.
opt_anno_types(#b_set{anno=Anno0,op=Op,args=Args}=I, Ts) ->
case benefits_from_type_anno(Op, Args) of
true -> opt_anno_types_1(I, Args, Ts, 0, #{});
false -> I
true ->
opt_anno_types_1(I, Args, Ts, 0, #{});
false ->
case Anno0 of
#{arg_types := _} ->
%% Remove `arg_types` from operations that don't
%% need them. This can happen, for example, when
%% the operation has been changed from
%% `{bif,is_list}` to `is_nonempty_list`.
Anno = maps:remove(arg_types, Anno0),
I#b_set{anno=Anno};
_ ->
I
end
end;
opt_anno_types(#b_switch{anno=Anno0,arg=Arg}=I, Ts) ->
case concrete_type(Arg, Ts) of
Expand Down Expand Up @@ -621,10 +641,6 @@ opt_anno_types_1(#b_set{anno=Anno0}=I, [], _Ts, _Index, Acc) ->
case Anno0 of
#{ arg_types := Acc } ->
I;
#{ arg_types := _ } when Acc =:= #{} ->
%% One or more arguments have been simplified to literal values.
Anno = maps:remove(arg_types, Anno0),
I#b_set{anno=Anno};
#{} ->
Anno = Anno0#{ arg_types => Acc },
I#b_set{anno=Anno}
Expand Down
Loading