Handle reads from undefined variables in liveness analysis

Doesn't affect well-formed code, but makes testing easier.
This commit is contained in:
Ulf Norell 2019-11-11 10:12:30 +01:00
parent eba4f1c79c
commit 13b196568b

View File

@ -759,8 +759,7 @@ ann_reads([{switch, Arg, Type, Alts, Def} | Code], Reads, Acc) ->
{Def1, ReadsDef} = ann_reads(Def, Reads, []), {Def1, ReadsDef} = ann_reads(Def, Reads, []),
Reads1 = ordsets:union([[Arg], Reads, ReadsDef | ReadsAlts]), Reads1 = ordsets:union([[Arg], Reads, ReadsDef | ReadsAlts]),
ann_reads(Code, Reads1, [{switch, Arg, Type, Alts1, Def1} | Acc]); ann_reads(Code, Reads1, [{switch, Arg, Type, Alts1, Def1} | Acc]);
ann_reads([{i, Ann, I} | Code], Reads, Acc) -> ann_reads([{i, _Ann, I} | Code], Reads, Acc) ->
#{ writes_in := WritesIn, writes_out := WritesOut } = Ann,
#{ read := Rs, write := W, pure := Pure } = attributes(I), #{ read := Rs, write := W, pure := Pure } = attributes(I),
%% If we write it here it's not live in (unless we also read it) %% If we write it here it's not live in (unless we also read it)
Reads1 = Reads -- [W], Reads1 = Reads -- [W],
@ -772,8 +771,8 @@ ann_reads([{i, Ann, I} | Code], Reads, Acc) ->
{{var, _}, true} -> Reads1; {{var, _}, true} -> Reads1;
_ -> ordsets:union(Reads1, Rs) _ -> ordsets:union(Reads1, Rs)
end, end,
LiveIn = ordsets:intersection(Reads2, WritesIn), LiveIn = Reads2, % For well-formed code this should be a subset of WritesIn
LiveOut = ordsets:intersection(Reads, WritesOut), LiveOut = Reads, % and this of WritesOut,
Ann1 = #{ live_in => LiveIn, live_out => LiveOut }, Ann1 = #{ live_in => LiveIn, live_out => LiveOut },
ann_reads(Code, Reads2, [{i, Ann1, I} | Acc]); ann_reads(Code, Reads2, [{i, Ann1, I} | Acc]);
ann_reads([], Reads, Acc) -> {Acc, Reads}. ann_reads([], Reads, Acc) -> {Acc, Reads}.