aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/test/opt_verify_SUITE.erl
blob: 24f43af2750b305de9933ac0b92193efaf3b697a (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
-module(opt_verify_SUITE).

-export([all/0, groups/0,
	 init_per_suite/1, end_per_suite/1,
	 init_per_group/2, end_per_group/2,
	 call_elim/0, call_elim/1]).

all() ->
    [call_elim].

groups() ->
    [].

init_per_suite(Config) ->
    case erlang:system_info(hipe_architecture) of
        undefined -> {skip, "HiPE not available or enabled"};
        _ -> Config
    end.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.

call_elim() ->
    [{doc, "Test that the call elimination optimization pass is ok"}].
call_elim(Config) ->
    DataDir = test_server:lookup_config(data_dir, Config),
    F1 = filename:join(DataDir, "call_elim_test.erl"),
    Icode1 = call_elim_test_file(Config, F1, icode_call_elim),
    0 = substring_count(binary:bin_to_list(Icode1), "is_key"),
    Icode2 = call_elim_test_file(Config, F1, no_icode_call_elim),
    true = (0 /= substring_count(binary:bin_to_list(Icode2), "is_key")),
    F2 = filename:join(DataDir, "call_elim_test_branches_no_opt_poss.erl"),
    Icode3 = call_elim_test_file(Config, F2, icode_call_elim),
    3 = substring_count(binary:bin_to_list(Icode3), "is_key"),
    Icode4 = call_elim_test_file(Config, F2, no_icode_call_elim),
    3 = substring_count(binary:bin_to_list(Icode4), "is_key"),
    F3 = filename:join(DataDir, "call_elim_test_branches_opt_poss.erl"),
    Icode5 = call_elim_test_file(Config, F3, icode_call_elim),
    0 = substring_count(binary:bin_to_list(Icode5), "is_key"),
    Icode6 = call_elim_test_file(Config, F3, no_icode_call_elim),
    2 = substring_count(binary:bin_to_list(Icode6), "is_key"),
    ok.

call_elim_test_file(Config, FileName, Option) ->
    PrivDir = test_server:lookup_config(priv_dir, Config),
    TempOut = test_server:temp_name(filename:join(PrivDir, "call_elim_out")),
    {ok, TestCase} = compile:file(FileName),
    {ok, TestCase} = hipe:c(TestCase, [Option, {pp_range_icode, {file, TempOut}}]),
    {ok, Icode} = file:read_file(TempOut),
    ok = file:delete(TempOut),
    Icode.

substring_count(Icode, Substring) ->
    substring_count(Icode, Substring, 0).
substring_count(Icode, Substring, N) ->
    case string:find(Icode, Substring) of
        nomatch -> N;
        Prefix -> substring_count(string:prefix(Prefix, Substring), Substring, N+1)
    end.