This commit is contained in:
radrow 2022-07-03 18:44:46 +02:00
parent 03e53f60cd
commit cf7830e4f5

View File

@ -819,6 +819,9 @@ ann_live(LiveTop, [I | Is], LiveOut) ->
ann_live1(_LiveTop, switch_body, LiveOut) -> ann_live1(_LiveTop, switch_body, LiveOut) ->
Ann = #{ live_in => LiveOut, live_out => LiveOut }, Ann = #{ live_in => LiveOut, live_out => LiveOut },
{{i, Ann, switch_body}, LiveOut}; {{i, Ann, switch_body}, LiveOut};
ann_live1(LiveTop, {jump, Ref}, _LiveOut) ->
Ann = #{ live_in => LiveTop, live_out => [] },
{{i, Ann, {jump, Ref}}, LiveTop};
ann_live1(LiveTop, loop, _LiveOut) -> ann_live1(LiveTop, loop, _LiveOut) ->
Ann = #{ live_in => LiveTop, live_out => [] }, Ann = #{ live_in => LiveTop, live_out => [] },
{{i, Ann, loop}, LiveTop}; {{i, Ann, loop}, LiveTop};
@ -1104,6 +1107,8 @@ simplify([I | Code], Options) ->
simpl_s({switch, Arg, Type, Alts, Def}, Options) -> simpl_s({switch, Arg, Type, Alts, Def}, Options) ->
{switch, Arg, Type, [simplify(A, Options) || A <- Alts], simplify(Def, Options)}; {switch, Arg, Type, [simplify(A, Options) || A <- Alts], simplify(Def, Options)};
simpl_s({loop, Init, Var, Expr, ContRef, BreakRef}, Options) ->
{loop, simplify(Init, Options), Var, simplify(Expr, Options), ContRef, BreakRef};
simpl_s(I, _) -> I. simpl_s(I, _) -> I.
%% Safe-guard against loops in the rewriting. Shouldn't happen so throw an %% Safe-guard against loops in the rewriting. Shouldn't happen so throw an
@ -1705,7 +1710,6 @@ optimize_blocks(Blocks) ->
Rev = fun(Bs) -> [ {Ref, lists:reverse(Code)} || {Ref, Code} <- Bs ] end, Rev = fun(Bs) -> [ {Ref, lists:reverse(Code)} || {Ref, Code} <- Bs ] end,
RBlocks = [{Ref, crop_jumps(Code)} || {Ref, Code} <- Blocks], RBlocks = [{Ref, crop_jumps(Code)} || {Ref, Code} <- Blocks],
RBlockMap = maps:from_list(RBlocks), RBlockMap = maps:from_list(RBlocks),
io:format("REORDERING ~p\n\n", [RBlocks]),
RBlocks1 = reorder_blocks(RBlocks, []), RBlocks1 = reorder_blocks(RBlocks, []),
RBlocks2 = [ {Ref, inline_block(RBlockMap, Ref, Code)} || {Ref, Code} <- RBlocks1 ], RBlocks2 = [ {Ref, inline_block(RBlockMap, Ref, Code)} || {Ref, Code} <- RBlocks1 ],
RBlocks3 = shortcut_jump_chains(RBlocks2), RBlocks3 = shortcut_jump_chains(RBlocks2),
@ -1786,13 +1790,15 @@ tweak_returns(['RETURN' | Code = [{'EXIT', _} | _]]) -> Code;
tweak_returns(['RETURN' | Code = [loop | _]]) -> Code; tweak_returns(['RETURN' | Code = [loop | _]]) -> Code;
tweak_returns(Code) -> Code. tweak_returns(Code) -> Code.
%% Remove instructions that appear after jumps. Returns reversed code. %% -- Remove instructions that appear after jumps. Returns reversed code.
%% This is useful for example when bb emitter adds continuation jumps
%% for switch expressions, but some of the branches
crop_jumps(Code) -> crop_jumps(Code) ->
crop_jumps(Code, []). crop_jumps(Code, []).
crop_jumps([], Acc) -> crop_jumps([], Acc) ->
Acc; Acc;
crop_jumps([I = {jump, _}|_], Acc) -> %% crop_jumps([I = {jump, _}|_], Acc) ->
[I|Acc]; %% [I|Acc];
crop_jumps([I|Code], Acc) -> crop_jumps([I|Code], Acc) ->
crop_jumps(Code, [I|Acc]). crop_jumps(Code, [I|Acc]).