summaryrefslogtreecommitdiff
path: root/lib/hipe/ssa/hipe_ssa_liveness.inc
blob: a1b49d5d356043c3d8bf68c578cf51117f81a8a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
%% -*- Erlang -*-
%% -*- erlang-indent-level: 2 -*-
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% GENERIC MODULE TO PERFORM LIVENESS ANALYSIS ON SSA FORM
%%
%% Exports:
%% ~~~~~~~
%% analyze(CFG) - returns a liveness analysis of CFG.
%% liveout(Liveness, Label) - returns the list of variables that are
%%      live at exit from basic block named Label.
%% livein(Liveness, Label) - returns the list of variables that are
%%      live on entry to the basic block named Label.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Uncomment the following if this is ever needed as an independent module
%%
-ifdef(LIVENESS_NEEDED).
-export([ssa_liveness__analyze/1,
	 ssa_liveness__livein/2]).
%%	 ssa_liveness__livein/3],
%%	 ssa_liveness__liveout/2]).
-type set(E) :: gb_sets:set(E).
-type liveness(Label, Var) ::
	#{Label => {{Gen          :: set(Var),
		     Kill         :: set(Var),
		     {TotalDirGen :: set(Var),
		      DirGen      :: gb_trees:tree(Label, set(Var))}},
		    LiveIn        :: set(Var),
		    LiveOut       :: set(Var),
		    Successors    :: [Label]}}.
-endif.
%% -ifdef(DEBUG_LIVENESS).
%% -export([pp_liveness/1]).
%% -endif.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Interface functions that MUST be implemented in the supporting files
%%
%% In the CFG file:
%% ----------------
%%  - bb(CFG, L) -> BasicBlock, extract a basic block from a cfg.
%%  - postorder(CFG) -> [Labels], the labels of the cfg in postorder
%%  - succ(CFG, L) -> [Labels], 
%%  - function(CFG) -> {M,F,A}
%%
%% In the CODE file:
%% ----------------- 
%%  - uses(Instr) ->
%%  - defines(Instr) ->
%%  - is_phi(Instr) -> Boolean
%%  - phi_arglist(Instr) -> [{Pred, Var}]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% The generic liveness analysis on SSA form
%%
ssa_liveness__analyze(CFG) ->
  PO = ?CFG:postorder(CFG),
  InitLiveness = liveness_init(init(PO, CFG)),
  merry_go_around(PO, InitLiveness).

%%
%% The fixpoint iteration
%%

merry_go_around(Labels, Liveness) ->
  case doit_once(Labels, Liveness) of
    {fixpoint, NewLiveness} -> 
      NewLiveness;
    {value, NewLiveness} -> 
      merry_go_around(Labels, NewLiveness)
  end.

%%
%% One iteration
%%

doit_once(Labels, Liveness) ->
  doit_once(Labels, Liveness, true).

doit_once([], Liveness, FixPoint) ->
  if FixPoint -> {fixpoint, Liveness};
     true -> {value, Liveness}
  end;
doit_once([L|Ls], Liveness, FixPoint) ->
  LiveOut = join_livein(Liveness, L),
  NewLiveness = update_liveout(L, LiveOut, Liveness),
  Kill = set_subtract(LiveOut, kill(L, NewLiveness)),
  LiveIn = set_union(Kill, gen(L, NewLiveness)),
  case update_livein(L, LiveIn, NewLiveness) of
    fixpoint -> doit_once(Ls, NewLiveness, FixPoint);
    {value, NewLiveness1} -> doit_once(Ls, NewLiveness1, false)
  end.
      
%%
%% updates liveness for a basic block
%%

update_livein(Label, NewLiveIn, Liveness) ->
  {GKD, LiveIn, LiveOut, Succ} = liveness_lookup(Label, Liveness),
  case LiveIn of
    NewLiveIn -> 
      fixpoint;
    _ -> 
      {value, liveness_update(Label, {GKD,NewLiveIn,LiveOut,Succ}, Liveness)}
  end.

update_liveout(Label, NewLiveOut, Liveness) ->
  {GKD, LiveIn, _LiveOut, Succ} = liveness_lookup(Label, Liveness),
  liveness_update(Label, {GKD,LiveIn,NewLiveOut,Succ}, Liveness).

%%
%% Join live in to get the new live out.
%%

join_livein(Liveness, L) ->
  Succ = successors(L, Liveness),
  case Succ of
    [] ->  % special case if no successors
      gb_sets:from_list(liveout_no_succ());
    _ ->
      join_livein1(L, Succ, Liveness)
  end.

join_livein1(Pred, Labels, Liveness) ->
  join_livein1(Pred, Labels, Liveness, new_set()).

join_livein1(_Pred, [], _Liveness, Live) ->
  Live;
join_livein1(Pred, [L|Ls], Liveness, Live) ->
  OldLivein = livein_set(Liveness, L, Pred),
  NewLive = set_union(OldLivein, Live),
  join_livein1(Pred, Ls, Liveness, NewLive).


ssa_liveness__liveout(Liveness, L) ->
  {_GKD, _LiveIn, LiveOut, Successors} = liveness_lookup(L, Liveness),
  case Successors of
    [] ->  % special case if no successors
      liveout_no_succ();
    _ ->
      set_to_list(LiveOut)
  end.  

-ifdef(LIVENESS_NEEDED).
ssa_liveness__livein(Liveness, L) ->
  set_to_list(livein_set(Liveness, L)).

%% ssa_liveness__livein(Liveness, L, Pred) ->
%%   set_to_list(livein_set(Liveness, L, Pred)).

livein_set(Liveness, L) ->
  {{_Gen,_Kill,{TotalDirGen, _DirGen}}, LiveIn, _LiveOut, _Successors} = 
    liveness_lookup(L, Liveness),
  set_union(TotalDirGen, LiveIn).
-endif.

livein_set(Liveness, L, Pred) ->
  {{_Gen,_Kill,{_TotalDirGen, DirGen}}, LiveIn, _LiveOut, _Successors} = 
    liveness_lookup(L, Liveness),
  case gb_trees:lookup(Pred, DirGen) of
    none ->
      LiveIn;
    {value, LiveInFromPred} ->
      set_union(LiveInFromPred, LiveIn)
  end.

successors(L, Liveness) ->
  {_GKD, _LiveIn, _LiveOut, Successors} = liveness_lookup(L, Liveness),
  Successors.

kill(L, Liveness) ->
  {{_Gen,Kill,_DirGen},_LiveIn,_LiveOut,_Successors} = 
    liveness_lookup(L, Liveness),
  Kill.

gen(L, Liveness) ->
  {{Gen,_Kill,_DirGen},_LiveIn,_LiveOut,_Successors} = 
    liveness_lookup(L, Liveness),
  Gen.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% init returns a list of: {Label, {{Gen, Kill}, LiveIn, Successors}}
%%    - Label is the name of the basic block.
%%    - Gen is the set of varables that are used by this block.
%%    - Kill is the set of varables that are defined by this block.
%%    - LiveIn is the set of variables that are alive at entry to the
%%      block (initially empty).
%%    - Successors is a list of the successors to the block.

init([], _) ->
  [];
init([L|Ls], CFG) ->
  BB = ?CFG:bb(CFG, L),
  Code = hipe_bb:code(BB),
  Succ = ?CFG:succ(CFG, L),
  {Gen, Kill} = make_bb_transfer(Code, Succ),
  DirectedGen = get_directed_gen(Code),
  [{L, {{Gen, Kill, DirectedGen}, new_set(), new_set(), Succ}} 
   | init(Ls, CFG)].

make_bb_transfer([], _Succ) ->
  {new_set(), new_set()};   % {Gen, Kill}
make_bb_transfer([I|Is], Succ) ->
  {Gen, Kill} = make_bb_transfer(Is, Succ),
  case ?CODE:is_phi(I) of
    true ->
      InstrKill = set_from_list(?CODE:defines(I)),
      Gen1 = set_subtract(Gen, InstrKill),
      Kill1 = set_union(Kill, InstrKill),
      {Gen1, Kill1};
    false ->
      InstrGen = set_from_list(?CODE:uses(I)),
      InstrKill = set_from_list(?CODE:defines(I)),
      Gen1 = set_subtract(Gen, InstrKill),
      Gen2 = set_union(Gen1, InstrGen),
      Kill1 = set_union(Kill, InstrKill),
      Kill2 = set_subtract(Kill1, InstrGen),
      {Gen2, Kill2}
  end.

get_directed_gen(Code) ->
  Map = get_directed_gen_1(Code),
  TotalGen = lists:foldl(fun({_Pred, Gen}, Acc) ->
			     set_union(Gen, Acc)
			 end, new_set(), gb_trees:to_list(Map)),
  {TotalGen, Map}.

get_directed_gen_1([I|Left])->
  case ?CODE:is_phi(I) of
    false -> 
      gb_trees:empty();
    true -> 
      Map = get_directed_gen_1(Left),
      ArgList = ?CODE:phi_arglist(I),
      lists:foldl(fun update_directed_gen/2, Map, ArgList)
  end.

update_directed_gen({Pred, Var}, Map)->      
  case gb_trees:lookup(Pred, Map) of
    none -> gb_trees:insert(Pred, set_from_list([Var]), Map);
    {value, Set} -> gb_trees:update(Pred, set_add(Var, Set), Map)
  end.
       

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% liveness
%%
-compile({inline, [liveness_lookup/2, liveness_update/3]}).

liveness_init(List) ->
  maps:from_list(List).

liveness_lookup(Label, Liveness) ->
  maps:get(Label, Liveness).
liveness_update(Label, Val, Liveness) ->
  maps:update(Label, Val, Liveness).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Sets
%%

new_set() ->
  gb_sets:empty().

set_union(S1, S2) ->
  gb_sets:union(S1, S2).

set_subtract(S1, S2) ->
  gb_sets:subtract(S1, S2).

set_from_list(List) ->
  gb_sets:from_list(List).

set_to_list(Set) ->
  gb_sets:to_list(Set).

set_add(Var, Set) ->
  gb_sets:add(Var, Set).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Pretty printer
%%

-ifdef(DEBUG_LIVENESS).

pp_liveness(CFG) ->
  io:format("Liveness for ~p:\n", [?CFG:function(CGF)]),
  Liveness = analyze(CFG),
  RevPostorder = lists:reverse(?CFG:postorder(CFG)),
  Edges = [{X, Y} || X <- RevPostorder, Y <- ?CFG:succ(CFG, X)],
  pp_liveness_edges(Edges, Liveness).

pp_liveness_edges([{From, To}|Left], Liveness)->
  LiveIn = livein(Liveness, To, From),
  io:format("Label ~w -> Label ~w: ~p\n", [From, To, LiveIn]),
  LiveOut = liveout(Liveness, From),
  io:format("Total live out from Label ~w: ~p\n", [From, LiveOut]),
  pp_liveness_edges(Left, Liveness);
pp_liveness_edges([], _Liveness) ->
  ok.

-endif.