-
Notifications
You must be signed in to change notification settings - Fork 3k
optimize beam_trim #10272
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
optimize beam_trim #10272
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -24,12 +24,12 @@ | |||||
| -moduledoc false. | ||||||
| -export([module/2]). | ||||||
|
|
||||||
| -import(lists, [any/2,reverse/1,reverse/2,seq/2,sort/1]). | ||||||
| -import(lists, [any/2,merge/1,reverse/2,seq/2,sort/1]). | ||||||
|
|
||||||
| -include("beam_asm.hrl"). | ||||||
|
|
||||||
| -record(st, | ||||||
| {safe :: sets:set(beam_asm:label()), %Safe labels. | ||||||
| {safe :: gb_sets:set(beam_asm:label()), %Safe labels. | ||||||
| fsz :: non_neg_integer() | ||||||
| }). | ||||||
|
|
||||||
|
|
@@ -48,8 +48,8 @@ function({function,Name,Arity,CLabel,Is0}) -> | |||||
| {function,Name,Arity,CLabel,Is} | ||||||
| catch | ||||||
| Class:Error:Stack -> | ||||||
| io:fwrite("Function: ~w/~w\n", [Name,Arity]), | ||||||
| erlang:raise(Class, Error, Stack) | ||||||
| io:fwrite("Function: ~w/~w\n", [Name,Arity]), | ||||||
| erlang:raise(Class, Error, Stack) | ||||||
| end. | ||||||
|
|
||||||
| trim([{init_yregs,_}=I|Is], none, St0) -> | ||||||
|
|
@@ -116,7 +116,7 @@ is_not_recursive(_) -> false. | |||||
| %% Y registers. Return the recipe that trims the most. | ||||||
|
|
||||||
| trim_recipe(Layout, IsNotRecursive, {Us,Ns}) -> | ||||||
| UsedRegs = ordsets:union(Us, Ns), | ||||||
| UsedRegs = gb_sets:union(Us, Ns), | ||||||
| Recipes = construct_recipes(Layout, 0, [], []), | ||||||
| NumOrigKills = length([I || {kill,_}=I <- Layout]), | ||||||
| IsTooExpensive = is_too_expensive_fun(IsNotRecursive), | ||||||
|
|
@@ -151,15 +151,18 @@ construct_recipes(_, _, _, Acc) -> | |||||
| Acc. | ||||||
|
|
||||||
| take_last_dead(L) -> | ||||||
| take_last_dead_1(reverse(L)). | ||||||
|
|
||||||
| take_last_dead_1([{live,_}|Is]) -> | ||||||
| take_last_dead_1(Is); | ||||||
| take_last_dead_1([{kill,Reg}|Is]) -> | ||||||
| {Reg,reverse(Is)}; | ||||||
| take_last_dead_1([{dead,Reg}|Is]) -> | ||||||
| {Reg,reverse(Is)}; | ||||||
| take_last_dead_1(_) -> none. | ||||||
| take_last_dead_1(L, []). | ||||||
|
|
||||||
| take_last_dead_1([{live,_}|T], Acc) -> | ||||||
| take_last_dead_1(T, Acc); | ||||||
| take_last_dead_1([{kill,Reg}|T], Acc) -> | ||||||
| {Reg,reverse(T, Acc)}; | ||||||
| take_last_dead_1([{dead,Reg}|T], Acc) -> | ||||||
| {Reg,reverse(T, Acc)}; | ||||||
| take_last_dead_1([], _) -> | ||||||
| none; | ||||||
| take_last_dead_1([H|T], Acc) -> | ||||||
| take_last_dead_1(T, [H|Acc]). | ||||||
|
||||||
|
|
||||||
| %% Is trimming too expensive? | ||||||
| is_too_expensive({Ks,_,Moves}, NumOrigKills, IsTooExpensive) -> | ||||||
|
|
@@ -199,13 +202,12 @@ is_too_expensive_fun(false) -> | |||||
| end. | ||||||
|
|
||||||
| is_recipe_viable({_,Trim,Moves}, UsedRegs) -> | ||||||
| Moved = ordsets:from_list([Src || {move,Src,_} <- Moves]), | ||||||
| Illegal = ordsets:from_list([Dst || {move,_,Dst} <- Moves]), | ||||||
| Moved = gb_sets:from_list([Src || {move,Src,_} <- Moves]), | ||||||
| Illegal = gb_sets:from_list([Dst || {move,_,Dst} <- Moves]), | ||||||
| Eliminated = [{y,N} || N <- seq(0, Trim - 1)], | ||||||
| %% All eliminated registers that are also in the used set must be moved. | ||||||
| UsedEliminated = ordsets:intersection(Eliminated, UsedRegs), | ||||||
| case ordsets:is_subset(UsedEliminated, Moved) andalso | ||||||
| ordsets:is_disjoint(Illegal, UsedRegs) of | ||||||
| UsedEliminated = gb_sets:intersection(gb_sets:from_list(Eliminated), UsedRegs), | ||||||
| case gb_sets:is_subset(UsedEliminated, Moved) andalso gb_sets:is_disjoint(Illegal, UsedRegs) of | ||||||
| true -> | ||||||
| UsedEliminated = Moved, %Assertion. | ||||||
|
||||||
| UsedEliminated = Moved, %Assertion. | |
| true = gb_sets:is_equal(UsedEliminated, Moved), %Assertion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing from
setstogb_setsis unnecessary since the only operation performed on this set is checking for membership (inis_safe_branch/2). Generally, checking for membership is faster forsetsthan forgb_sets.