aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/x86/hipe_x86_ra.erl
blob: 2e97b07e5d240084f96ff1a7ef7079bd4c1e20f8 (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
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2004-2016. All Rights Reserved.
%% 
%% 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.
%% 
%% %CopyrightEnd%
%%

-ifdef(HIPE_AMD64).
-define(HIPE_X86_RA, hipe_amd64_ra).
-define(HIPE_X86_PP, hipe_amd64_pp).
-define(HIPE_X86_RA_LS, hipe_amd64_ra_ls).
-define(HIPE_X86_RA_NAIVE, hipe_amd64_ra_naive).
-define(HIPE_X86_RA_FINALISE, hipe_amd64_ra_finalise).
-define(HIPE_X86_SPECIFIC, hipe_amd64_specific).
-else.
-define(HIPE_X86_RA, hipe_x86_ra).
-define(HIPE_X86_PP, hipe_x86_pp).
-define(HIPE_X86_RA_LS, hipe_x86_ra_ls).
-define(HIPE_X86_RA_NAIVE, hipe_x86_ra_naive).
-define(HIPE_X86_RA_FINALISE, hipe_x86_ra_finalise).
-define(HIPE_X86_SPECIFIC, hipe_x86_specific).
-endif.

-module(?HIPE_X86_RA).
-export([ra/2]).

%%-define(HIPE_INSTRUMENT_COMPILER, true). %% Turn on instrumentation.
-include("../main/hipe.hrl").

-ifdef(HIPE_INSTRUMENT_COMPILER).
code_size(CFG) ->
  hipe_x86_cfg:fold_bbs(fun(_, BB, Acc) -> Acc + length(hipe_bb:code(BB)) end,
			0, CFG).
-endif. %% ifdef(HIPE_INSTRUMENT_COMPILER)

ra(CFG0, Options) ->
  %% hipe_x86_cfg:pp(CFG0),
  {CFG1, Coloring_fp, SpillIndex} = ra_fp(CFG0, Options),
  %% hipe_x86_cfg:pp(CFG1),
  ?start_ra_instrumentation(Options,
			    code_size(CFG1),
			    element(2,hipe_gensym:var_range(x86))),
  {CFG2, Coloring}
    = case proplists:get_value(regalloc, Options, coalescing) of
	coalescing ->
	  ra(CFG1, SpillIndex, Options, hipe_coalescing_regalloc);
	optimistic ->
	  ra(CFG1, SpillIndex, Options, hipe_optimistic_regalloc);
	graph_color ->
	  ra(CFG1, SpillIndex, Options, hipe_graph_coloring_regalloc);
	linear_scan ->
	  ?HIPE_X86_RA_LS:ra(CFG1, SpillIndex, Options);
	naive ->
	  ?HIPE_X86_RA_NAIVE:ra(CFG1, Coloring_fp, Options);
        _ ->
	  exit({unknown_regalloc_compiler_option,
		proplists:get_value(regalloc,Options)})
      end,
  ?stop_ra_instrumentation(Options,
			   code_size(CFG2),
  			   element(2,hipe_gensym:var_range(x86))),
  %% hipe_x86_cfg:pp(CFG2),
  ?HIPE_X86_RA_FINALISE:finalise(CFG2, Coloring, Coloring_fp, Options).

ra(CFG, SpillIndex, Options, RegAllocMod) ->
  hipe_regalloc_loop:ra(CFG, SpillIndex, Options, RegAllocMod, ?HIPE_X86_SPECIFIC).

-ifdef(HIPE_AMD64).
ra_fp(CFG, Options) ->
  Regalloc0 = proplists:get_value(regalloc, Options),
  {Regalloc, TargetMod} =
    case proplists:get_bool(inline_fp, Options) and (Regalloc0 =/= naive) of
      false -> {naive, undefined};
      true ->
	case proplists:get_bool(x87, Options) of
	  true ->  {linear_scan, hipe_amd64_specific_x87};
	  false -> {Regalloc0,   hipe_amd64_specific_sse2}
	end
    end,
  case Regalloc of
    coalescing  -> ra_fp(CFG, Options, hipe_coalescing_regalloc, TargetMod);
    optimistic  -> ra_fp(CFG, Options, hipe_optimistic_regalloc, TargetMod);
    graph_color -> ra_fp(CFG, Options, hipe_graph_coloring_regalloc,
			 TargetMod);
    linear_scan -> hipe_amd64_ra_ls:ra_fp(CFG, Options, TargetMod);
    naive -> {CFG,[],0};
    _ ->
      exit({unknown_regalloc_compiler_option,
	    proplists:get_value(regalloc,Options)})
  end.

ra_fp(CFG, Options, RegAllocMod, TargetMod) ->
  hipe_regalloc_loop:ra_fp(CFG, Options, RegAllocMod, TargetMod).
-else.
ra_fp(CFG, Options) ->
  case proplists:get_bool(inline_fp, Options) of
    true ->
      hipe_x86_ra_ls:ra_fp(CFG, Options, hipe_x86_specific_x87);
    false ->
      {CFG,[],0}
  end.
-endif.