aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/icode/hipe_icode_exceptions.erl
blob: 00caffb24ba60bcc33d48e2eb5def6ac6aa5d98f (plain) (blame)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2004-2011. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
%% ====================================================================
%%  Filename : 	hipe_icode_exceptions.erl
%%  Module   :	hipe_icode_exceptions
%%  Purpose  :  Rewrite calls in intermediate code to use Continuation
%%              and Fail-To labels.
%%
%%		Catch-instructions work as follows:
%%                - A begin_try(FailLabel) starts a catch-region which
%%                  is ended by a corresponding end_try(FailLabel).
%%                - The handler begins with a begin_handler(FailLabel).
%%
%%		However, the begin/end instructions do not always appear
%%		as parentheses around the section that they protect (in
%%		linear Beam/Icode). Also, different begin_catch
%%		instructions can reach the same basic blocks (which may
%%		raise exceptions), due to code compation optimizations
%%		in the Beam compiler, even though they have different
%%		handlers. Because of this, a data flow analysis is
%%		necessary to find out which catches may reach which
%%		basic blocks. After that, we clone basic blocks as
%%		needed to ensure that each block belongs to at most one
%%		unique begin_catch. The Beam does not have this problem,
%%		since it will find the correct catch-handler frame
%%		pushed on the stack. (Note that since there can be no
%%		tail-calls within a catch region, our dataflow analysis
%%		for finding all catch-stacks is sure to terminate.)
%%
%%		Finally, we can remove all special catch instructions
%%		and rewrite calls within catch regions to use explicit
%%		fail-to labels, which is the main point of all this.
%%		Fail labels that were set before this pass are kept.
%%		(Note that calls that have only a continuation label do
%%		not always end their basic blocks. Adding a fail label
%%		to such a call can thus force us to split the block.)
%%
%%  Notes    :  As of November 2003, primops that do not fail in the 
%%              normal sense are allowed to have a fail-label even
%%              before this pass. (Used for the mbox-empty + get_msg
%%              primitive in receives.)
%%
%%              Native floating point operations cannot fail in the
%%              normal sense. Instead they throw a hardware exception
%%              which will be caught by a special fp check error
%%              instruction. These primops do not need a fail label even
%%              in a catch. This pass checks for this with
%%              hipe_icode_primops:fails/1. If a call cannot fail, no
%%              fail label is added.
%%
%%              Explicit fails (exit, error and throw) inside
%%              a catch have to be handled. They have to build their
%%              exit value and jump directly to the catch handler. An
%%              alternative solution would be to have a new type of
%%              fail instruction that takes a fail-to label...
%%
%%  CVS:
%%    $Id$
%% ====================================================================

-module(hipe_icode_exceptions).

-export([fix_catches/1]).

-include("hipe_icode.hrl").
-include("../flow/cfg.hrl").

%%----------------------------------------------------------------------------

-spec fix_catches(#cfg{}) -> #cfg{}.

fix_catches(CFG) ->
  {Map, State} = build_mapping(find_catches(init_state(CFG))),
  hipe_icode_cfg:remove_unreachable_code(get_cfg(rewrite(State, Map))).

%% This finds the set of possible catch-stacks for each basic block

find_catches(State) ->
  find_catches(get_start_labels(State),
	       clear_visited(clear_changed(State))).

find_catches([L|Ls], State0) ->
  case is_visited(L, State0) of
    true ->
      find_catches(Ls, State0);
    false ->
      State1 = set_visited(L, State0),
      Code = get_bb_code(L, State1),
      Cs = get_new_catches_in(L, State1),
      State2 = set_catches_in(L, Cs, State1),  % memorize
      Cs1 = catches_out(Code, Cs),
      Ls1 = get_succ(L, State2) ++ Ls,
      Cs0 = get_catches_out(L, State2),
      if Cs1 =:= Cs0 ->
	  find_catches(Ls1, State2);
	 true ->
	  State3 = set_catches_out(L, Cs1, State2),
	  find_catches(Ls1, set_changed(State3))
      end
  end;
find_catches([], State) ->
  case is_changed(State) of
    true ->
      find_catches(State);
    false ->
      State
  end.

catches_out([I|Is], Cs) ->
  catches_out(Is, catches_out_instr(I, Cs));
catches_out([], Cs) ->
  Cs.

catches_out_instr(I, Cs) ->
  case I of
    #icode_begin_try{} ->
      Id = hipe_icode:begin_try_label(I),
      push_catch(Id, Cs);
    #icode_end_try{} ->
      pop_catch(Cs);
    #icode_begin_handler{} ->
      pop_catch(Cs);
    _ ->
      Cs
  end.


%% This builds the mapping used for cloning

build_mapping(State) ->
  build_mapping(get_start_labels(State), clear_visited(State),
		new_mapping()).

build_mapping([L|Ls], State0, Map) ->
  case is_visited(L, State0) of
    true ->
      build_mapping(Ls, State0, Map);
    false ->
      State1 = set_visited(L, State0),
      Cs = list_of_catches(get_catches_in(L, State1)),  % get memorized
      {Map1, State2} = map_bb(L, Cs, State1, Map),
      Ls1 = get_succ(L, State2) ++ Ls,
      build_mapping(Ls1, State2, Map1)
  end;
build_mapping([], State, Map) ->
  {Map, State}.

map_bb(_L, [_C], State, Map) ->
  {Map, State};
map_bb(L, [C | Cs], State, Map) ->
  %% This block will be cloned - we need to create N-1 new labels.
  %% The identity mapping will be used for the first element.
  Map1 = new_catch_labels(Cs, L, Map),
  State1 = set_catches_in(L, single_catch(C), State),  % update catches in
  Code = get_bb_code(L, State1),
  State2 = clone(Cs, L, Code, State1, Map1),
  {Map1, State2}.

clone([C | Cs], L, Code, State, Map) ->
  Ren = get_renaming(C, Map),
  L1 = Ren(L),
  State1 = set_bb_code(L1, Code, State),
  State2 = set_catches_in(L1, single_catch(C), State1),  % set catches in
  clone(Cs, L, Code, State2, Map);
clone([], _L, _Code, State, _Map) ->
  State.

new_catch_labels([C | Cs], L, Map) ->
  L1 = hipe_icode:label_name(hipe_icode:mk_new_label()),
  Map1 = set_mapping(C, L, L1, Map),
  new_catch_labels(Cs, L, Map1);
new_catch_labels([], _L, Map) ->
  Map.


%% This does all the actual rewriting and cloning.

rewrite(State, Map) ->
  rewrite(get_start_labels(State), clear_visited(State), Map).

rewrite([L|Ls], State0, Map) ->
  case is_visited(L, State0) of
    true ->
      rewrite(Ls, State0, Map);
    false ->
      State1 = set_visited(L, State0),
      Code = get_bb_code(L, State1),
      Cs = list_of_catches(get_catches_in(L, State1)),  % get memorized
      State2 = rewrite_bb(L, Cs, Code, State1, Map),
      Ls1 = get_succ(L, State2) ++ Ls,
      rewrite(Ls1, State2, Map)
  end;
rewrite([], State, _Map) ->
  State.

rewrite_bb(L, [C], Code, State, Map) ->
  {Code1, State1} = rewrite_code(Code, C, State, Map),
  set_bb_code(L, Code1, State1).

rewrite_code(Is, C, State, Map) ->
  rewrite_code(Is, C, State, Map, []).

rewrite_code([I|Is], C, State, Map, As) ->
  [C1] = list_of_catches(catches_out_instr(I, single_catch(C))),
  case I of
    #icode_begin_try{} ->
      {I1, Is1, State1} = update_begin_try(I, Is, C, State, Map),
      I2 = redirect_instr(I1, C, Map),
      rewrite_code(Is1, C1, State1, Map, [I2 | As]);
    #icode_end_try{} ->
      rewrite_code(Is, C1, State, Map, As);
    #icode_call{} ->
      {I1, Is1, State1} = update_call(I, Is, C, State, Map),
      I2 = redirect_instr(I1, C, Map),
      rewrite_code(Is1, C1, State1, Map, [I2 | As]);
    #icode_fail{} ->
      {I1, Is1, State1} = update_fail(I, Is, C, State, Map),
      I2 = redirect_instr(I1, C, Map),
      rewrite_code(Is1, C1, State1, Map, [I2 | As]);
    _ ->
      I1 = redirect_instr(I, C, Map),
      rewrite_code(Is, C1, State, Map, [I1 | As])
  end;
rewrite_code([], _C, State, _Map, As) ->
  {lists:reverse(As), State}.

redirect_instr(I, C, Map) ->
  redirect_instr_1(I, hipe_icode:successors(I), get_renaming(C, Map)).

redirect_instr_1(I, [L0 | Ls], Ren) ->
  I1 = hipe_icode:redirect_jmp(I, L0, Ren(L0)),
  redirect_instr_1(I1, Ls, Ren);
redirect_instr_1(I, [], _Ren) ->
  I.

update_begin_try(I, Is, _C, State0, _Map) ->
  L = hipe_icode:begin_try_successor(I),
  I1 = hipe_icode:mk_goto(L),
  {I1, Is, State0}.

update_call(I, Is, C, State0, Map) ->
  case top_of_stack(C) of
    [] ->
      %% No active catch. Assume cont./fail labels are correct as is.
      {I, Is, State0};
    L ->
      %% Only update the fail label if the call *can* fail.
      case hipe_icode_primops:fails(hipe_icode:call_fun(I)) of
	true ->
	  %% We only update the fail label if it is not already set.
	  case hipe_icode:call_fail_label(I) of
	    [] ->
	      I1 = hipe_icode:call_set_fail_label(I, L),
	      %% Now the call will end the block, so we must put the rest of
	      %% the code (if nonempty) in a new block!
	      if Is =:= [] ->
		  {I1, Is, State0};
		 true ->
		  L1 = hipe_icode:label_name(hipe_icode:mk_new_label()),
		  I2 = hipe_icode:call_set_continuation(I1, L1),
		  State1 = set_bb_code(L1, Is, State0),
		  State2 = set_catches_in(L1, single_catch(C), State1),
		  State3 = rewrite_bb(L1, [C], Is, State2, Map),
		  {I2, [], State3}
	      end;
	    _ when Is =:= [] ->
	      %% Something is very wrong if Is is not empty here. A call
	      %% with a fail label should have ended its basic block.
	      {I, Is, State0}
	  end;
	false ->
	  %% Make sure that the fail label is not set.
	  I1 = hipe_icode:call_set_fail_label(I, []),
	  {I1, Is, State0}
      end
  end.

update_fail(I, Is, C, State, _Map) ->
  case hipe_icode:fail_label(I) of
    [] -> 
      {hipe_icode:fail_set_label(I, top_of_stack(C)), Is, State};
    _ ->
      {I, Is, State}
  end.


%%---------------------------------------------------------------------
%% Abstraction for sets of catch stacks.

%% This is the bottom element
no_catches() -> [].

%% A singleton set
single_catch(C) -> [C].

%% A single, empty stack
empty_stack() -> [].

%% Getting the label to fail to
top_of_stack([C|_]) -> C;
top_of_stack([]) -> [].    % nil is used in Icode for "no label"

join_catches(Cs1, Cs2) ->
  ordsets:union(Cs1, Cs2).

list_of_catches(Cs) -> Cs.

%% Note that prepending an element to all elements in the list will
%% preserve the ordering of the list, and will never make two existing
%% elements become identical, so the list is still an ordset. 

push_catch(L, []) ->
  [[L]];
push_catch(L, Cs) ->
  push_catch_1(L, Cs).

push_catch_1(L, [C|Cs]) ->
  [[L|C] | push_catch_1(L, Cs)];
push_catch_1(_L, []) ->
  [].

%% However, after discarding the head of all elements, the list
%% is no longer an ordset, and must be processed.

pop_catch(Cs) ->
  ordsets:from_list(pop_catch_1(Cs)).

pop_catch_1([[_|C] | Cs]) ->
  [C | pop_catch_1(Cs)];
pop_catch_1([[] | Cs]) ->
  %% The elements in the list represent different possible incoming
  %% stacks of catch handlers to this BB.  Before the fixpoint has
  %% been found these elements are underapproximations of the true
  %% stacks, therefore it's possible for these elements to be too
  %% short for the number of pops implied by the code in the BB.
  %% We must not fail in that case, so we set pop([]) = [].
  %% This fixes find_catches_crash.erl and compiler_tests in the
  %% HiPE test suite.
  [[] | pop_catch_1(Cs)];
pop_catch_1([]) ->
  [].


%%---------------------------------------------------------------------
%% Mapping from catch-stacks to renamings on labels.

new_mapping() ->
  gb_trees:empty().

set_mapping(C, L0, L1, Map) ->
  Dict = case gb_trees:lookup(C, Map) of
	   {value, Dict0} ->
	     gb_trees:enter(L0, L1, Dict0);
	   none ->
	     gb_trees:insert(L0, L1, gb_trees:empty())
	 end,
  gb_trees:enter(C, Dict, Map).

%% Return a label renaming function for a particular catch-stack

get_renaming(C, Map) ->
  case gb_trees:lookup(C, Map) of
    {value, Dict} ->
      fun (L0) ->
	  case gb_trees:lookup(L0, Dict) of
	    {value, L1} -> L1;
	    none -> L0
	  end
      end;
    none ->
      fun (L0) -> L0 end
  end.


%%---------------------------------------------------------------------
%% State abstraction

-record(state, {cfg					:: #cfg{},
		changed = false				:: boolean(),
		succ					:: #cfg{},
		pred					:: #cfg{},
		start_labels				:: [icode_lbl(),...],
		visited = hipe_icode_cfg:none_visited()	:: gb_set(),
		out     = gb_trees:empty()		:: gb_tree(),
		in      = gb_trees:empty()		:: gb_tree()
	       }).

init_state(CFG) ->
  State = #state{cfg = CFG},
  refresh_state_cache(State).

refresh_state_cache(State) ->
  CFG = State#state.cfg,
  SLs = [hipe_icode_cfg:start_label(CFG)],
  State#state{succ = CFG, pred = CFG, start_labels = SLs}.

get_cfg(State) ->
  State#state.cfg.

get_start_labels(State) ->
  State#state.start_labels.

get_pred(L, State) ->
  hipe_icode_cfg:pred(State#state.pred, L).

get_succ(L, State) ->
  hipe_icode_cfg:succ(State#state.succ, L).

set_changed(State) ->
  State#state{changed = true}.

is_changed(State) ->
  State#state.changed.

clear_changed(State) ->
  State#state{changed = false}.

set_catches_out(L, Cs, State) ->
  State#state{out = gb_trees:enter(L, Cs, State#state.out)}.

get_catches_out(L, State) ->
  case gb_trees:lookup(L, State#state.out) of
    {value, Cs} -> Cs;
    none -> no_catches()
  end.

set_catches_in(L, Cs, State) ->
  State#state{in = gb_trees:enter(L, Cs, State#state.in)}.

get_catches_in(L, State) ->
  case gb_trees:lookup(L, State#state.in) of
    {value, Cs} -> Cs;
    none -> no_catches()
  end.

set_visited(L, State) ->
  State#state{visited = hipe_icode_cfg:visit(L, State#state.visited)}.

is_visited(L, State) ->
  hipe_icode_cfg:is_visited(L, State#state.visited).

clear_visited(State) ->
  State#state{visited = hipe_icode_cfg:none_visited()}.

get_bb_code(L, State) ->
  hipe_bb:code(hipe_icode_cfg:bb(State#state.cfg, L)).

set_bb_code(L, Code, State) ->
  CFG = State#state.cfg,
  CFG1 = hipe_icode_cfg:bb_add(CFG, L, hipe_bb:mk_bb(Code)),
  refresh_state_cache(State#state{cfg = CFG1}).

get_new_catches_in(L, State) ->
  Ps = get_pred(L, State),
  Cs = case lists:member(L, get_start_labels(State)) of
	 true -> single_catch(empty_stack());
	 false -> no_catches()
       end,
  get_new_catches_in(Ps, Cs, State).

get_new_catches_in([P | Ps], Cs, State) ->
  Cs1 = join_catches(Cs, get_catches_out(P, State)),
  get_new_catches_in(Ps, Cs1, State);
get_new_catches_in([], Cs, _) ->
  Cs.

%%---------------------------------------------------------------------