Skip to content

Commit

Permalink
Merge pull request erlang#8695 from frej/frej/speedup
Browse files Browse the repository at this point in the history
Speed up alias analysis
  • Loading branch information
bjorng authored Oct 4, 2024
2 parents 0121211 + b83561a commit cc9f43e
Show file tree
Hide file tree
Showing 15 changed files with 973 additions and 340 deletions.
6 changes: 4 additions & 2 deletions lib/compiler/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ BEAM_H = $(wildcard ../priv/beam_h/*.h)
HRL_FILES= \
beam_asm.hrl \
beam_disasm.hrl \
beam_ssa_alias_debug.hrl \
beam_ssa_opt.hrl \
beam_ssa.hrl \
beam_types.hrl \
Expand Down Expand Up @@ -216,7 +217,8 @@ $(EBIN)/beam_jump.beam: beam_asm.hrl
$(EBIN)/beam_listing.beam: core_parse.hrl beam_ssa.hrl \
beam_asm.hrl beam_types.hrl
$(EBIN)/beam_ssa.beam: beam_ssa.hrl
$(EBIN)/beam_ssa_alias.beam: beam_ssa_opt.hrl beam_types.hrl
$(EBIN)/beam_ssa_alias.beam: beam_ssa_opt.hrl beam_ssa_alias_debug.hrl \
beam_types.hrl
$(EBIN)/beam_ssa_bsm.beam: beam_ssa.hrl
$(EBIN)/beam_ssa_bool.beam: beam_ssa.hrl
$(EBIN)/beam_ssa_check.beam: beam_ssa.hrl beam_types.hrl
Expand All @@ -229,7 +231,7 @@ $(EBIN)/beam_ssa_pp.beam: beam_ssa.hrl beam_types.hrl
$(EBIN)/beam_ssa_pre_codegen.beam: beam_ssa.hrl beam_asm.hrl
$(EBIN)/beam_ssa_recv.beam: beam_ssa.hrl
$(EBIN)/beam_ssa_share.beam: beam_ssa.hrl
$(EBIN)/beam_ssa_ss.beam: beam_ssa.hrl beam_types.hrl
$(EBIN)/beam_ssa_ss.beam: beam_ssa.hrl beam_ssa_alias_debug.hrl beam_types.hrl
$(EBIN)/beam_ssa_throw.beam: beam_ssa.hrl beam_types.hrl
$(EBIN)/beam_ssa_type.beam: beam_ssa.hrl beam_types.hrl
$(EBIN)/beam_trim.beam: beam_asm.hrl
Expand Down
28 changes: 27 additions & 1 deletion lib/compiler/src/beam_digraph.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
-export([new/0,
add_vertex/2, add_vertex/3, add_edge/3, add_edge/4,
del_edge/2, del_edges/2,
del_vertex/2,
edges/1,
foldv/3,
has_vertex/2,
is_path/3,
in_degree/2, in_edges/2, in_neighbours/2,
no_vertices/1,
out_degree/2, out_edges/2, out_neighbours/2,
vertex/2, vertices/1,
vertex/2, vertex/3, vertices/1,
reverse_postorder/2,
roots/1,
topsort/1,
Expand Down Expand Up @@ -76,6 +78,18 @@ add_vertex(Dg, V, Label) ->
Vs = Vs0#{V=>Label},
Dg#dg{vs=Vs}.

-spec del_vertex(graph(), vertex()) -> graph().
del_vertex(Dg, V) ->
#dg{vs=Vs0,in_es=InEsMap0,out_es=OutEsMap0} = Dg,
InEs = maps:get(V, InEsMap0, []),
OutEsMap = foldl(fun({From,_,_}=E, A) -> edge_map_del(From, E, A) end,
maps:remove(V, OutEsMap0), InEs),
OutEs = maps:get(V, OutEsMap0, []),
InEsMap = foldl(fun({_,To,_}=E, A) -> edge_map_del(To, E, A) end,
maps:remove(V, InEsMap0), OutEs),
Vs = maps:remove(V, Vs0),
Dg#dg{vs=Vs,in_es=InEsMap,out_es=OutEsMap}.

-spec add_edge(graph(), vertex(), vertex()) -> graph().
add_edge(Dg, From, To) ->
add_edge(Dg, From, To, edge).
Expand Down Expand Up @@ -176,6 +190,12 @@ no_vertices(#dg{vs=Vs}) ->
vertex(#dg{vs=Vs}, V) ->
map_get(V, Vs).

%% As vertex/2 but if the vertex does not exist a default value is
%% returned.
-spec vertex(graph(), vertex(), label()) -> label().
vertex(#dg{vs=Vs}, V, Default) ->
maps:get(V, Vs, Default).

-spec vertices(graph()) -> [{vertex(), label()}].
vertices(#dg{vs=Vs}) ->
maps:to_list(Vs).
Expand Down Expand Up @@ -217,6 +237,12 @@ topsort(G) ->
Seen = roots(G),
reverse_postorder(G, Seen).

-spec edges(graph()) -> [edge()].
edges(#dg{out_es=OutEsMap}) ->
maps:fold(fun(_, Es, Acc) ->
Es ++ Acc
end, [], OutEsMap).

%%
%% Kosaraju's algorithm
%%
Expand Down
Loading

0 comments on commit cc9f43e

Please sign in to comment.