4
%% Copyright Ericsson AB 1999-2010. All Rights Reserved.
4
%% Copyright Ericsson AB 1999-2011. All Rights Reserved.
6
6
%% The contents of this file are subject to the Erlang Public License,
7
7
%% Version 1.1, (the "License"); you may not use this file except in
37
37
%% Collect basic blocks and optimize them.
38
38
Is2 = blockify(Is1),
39
Is3 = move_allocates(Is2),
40
Is4 = beam_utils:live_opt(Is3),
41
Is5 = opt_blocks(Is4),
42
Is6 = beam_utils:delete_live_annos(Is5),
39
Is3 = embed_lines(Is2),
40
Is4 = move_allocates(Is3),
41
Is5 = beam_utils:live_opt(Is4),
42
Is6 = opt_blocks(Is5),
43
Is7 = beam_utils:delete_live_annos(Is6),
44
45
%% Optimize bit syntax.
45
{Is,Lc} = bsm_opt(Is6, Lc0),
46
{Is,Lc} = bsm_opt(Is7, Lc0),
48
49
{{function,Name,Arity,CLabel,Is},Lc}
148
149
collect({'catch',R,L}) -> {set,[R],[],{'catch',L}};
149
150
collect(_) -> error.
152
%% embed_lines([Instruction]) -> [Instruction]
153
%% Combine blocks that would be split by line/1 instructions.
154
%% Also move a line instruction before a block into the block,
155
%% but leave the line/1 instruction after a block outside.
158
embed_lines(reverse(Is), []).
160
embed_lines([{block,B2},{line,_}=Line,{block,B1}|T], Acc) ->
161
B = {block,B1++[{set,[],[],Line}]++B2},
162
embed_lines([B|T], Acc);
163
embed_lines([{block,B1},{line,_}=Line|T], Acc) ->
164
B = {block,[{set,[],[],Line}|B1]},
165
embed_lines([B|T], Acc);
166
embed_lines([I|Is], Acc) ->
167
embed_lines(Is, [I|Acc]);
168
embed_lines([], Acc) -> Acc.
151
170
opt_blocks([{block,Bl0}|Is]) ->
152
171
%% The live annotation at the beginning is not useful.
153
172
[{'%live',_}|Bl] = Bl0,
225
244
RevBif -> [{set,[Dst],As,{bif,RevBif,Fail}}|opt(Is)]
227
246
opt([{set,[X],[X],move}|Is]) -> opt(Is);
228
opt([{set,[D1],[{integer,Idx1},Reg],{bif,element,{f,0}}}=I1,
247
opt([{set,_,_,{line,_}}=Line1,
248
{set,[D1],[{integer,Idx1},Reg],{bif,element,{f,0}}}=I1,
249
{set,_,_,{line,_}}=Line2,
229
250
{set,[D2],[{integer,Idx2},Reg],{bif,element,{f,0}}}=I2|Is])
230
251
when Idx1 < Idx2, D1 =/= D2, D1 =/= Reg, D2 =/= Reg ->
252
opt([Line2,I2,Line1,I1|Is]);
232
253
opt([{set,Ds0,Ss,Op}|Is0]) ->
233
254
{Ds,Is} = opt_moves(Ds0, Is0),
234
255
[{set,Ds,Ss,Op}|opt(Is)];