aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/rtl/hipe_rtl_verify_gcsafe.erl
blob: 01d7e89ccdd2b2c300f9eaa72716e81627b8c305 (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
%% -*- mode: 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.
%%
-module(hipe_rtl_verify_gcsafe).

-export([check/1]).

-include("../flow/cfg.hrl").              %% needed for the specs
-include("hipe_rtl.hrl").

check(CFG) ->
  Liveness = hipe_rtl_liveness:analyze(CFG),
  put({?MODULE, 'fun'}, CFG#cfg.info#cfg_info.'fun'),
  lists:foreach(
    fun(Lb) ->
        put({?MODULE, label}, Lb),
        Liveout = hipe_rtl_liveness:liveout(Liveness, Lb),
        BB = hipe_rtl_cfg:bb(CFG, Lb),
        check_instrs(lists:reverse(hipe_bb:code(BB)), Liveout)
    end, hipe_rtl_cfg:labels(CFG)),
  erase({?MODULE, 'fun'}),
  erase({?MODULE, label}),
  erase({?MODULE, instr}),
  ok.

check_instrs([], _Livein) -> ok;
check_instrs([I|Is], LiveOut) ->
  Def = ordsets:from_list(hipe_rtl:defines(I)),
  Use = ordsets:from_list(hipe_rtl:uses(I)),
  LiveOver = ordsets:subtract(LiveOut, Def),
  LiveIn = ordsets:union(LiveOver, Use),
  case (hipe_rtl:is_call(I)
        andalso not safe_primop(hipe_rtl:call_fun(I)))
    orelse is_record(I, gctest)
  of
    false -> ok;
    true ->
      put({?MODULE, instr}, I),
      lists:foreach(fun verify_live/1, LiveOver)
  end,
  check_instrs(Is, LiveIn).

verify_live(T) ->
  case hipe_rtl:is_reg(T) of
    false -> ok;
    true ->
      case hipe_rtl:reg_is_gcsafe(T) of
        true -> ok;
        false ->
          error({gcunsafe_live_over_call,
                 get({?MODULE, 'fun'}),
                 {label, get({?MODULE, label})},
                 get({?MODULE, instr}),
                 T})
      end
  end.

%% Primops that can't gc
%% Note: This information is essentially duplicated from hipe_bif_list.m4
safe_primop(is_divisible) -> true;
safe_primop(is_unicode) -> true;
safe_primop(cmp_2) -> true;
safe_primop(eq_2) -> true;
safe_primop(bs_allocate) -> true;
safe_primop(bs_reallocate) -> true;
safe_primop(bs_utf8_size) -> true;
safe_primop(bs_get_utf8) -> true;
safe_primop(bs_put_utf8) -> true;
safe_primop(bs_utf16_size) -> true;
safe_primop(bs_get_utf16) -> true;
safe_primop(bs_validate_unicode_retract) -> true;
safe_primop(bs_put_small_float) -> true;
safe_primop(bs_put_bits) -> true;
safe_primop(emasculate_binary) -> true;
safe_primop(atomic_inc) -> true;
%% Not noproc but manually verified
safe_primop(bs_put_big_integer) -> true;
safe_primop(_) -> false.