aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/opt/hipe_spillmin.erl
blob: a2efd35d2653d1f10c45478ee928663945b96dd6 (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
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2005-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%
%%
%% ==========================================================================
%%  Module  :  hipe_spillmin
%%  Purpose :  Driver module for minimizing the number of stack slots used
%%	       by a function. This is done using an algorithm for register
%%             allocation. The implementation is target-independent and
%%             requires a target-specific interface module as argument.  
%% 
%% $Id$
%% ==========================================================================
%% Exported functions (short description):
%%
%%  stackalloc(CFG, StackSlots, SpillIndex, Options, TgtMod, TgtCtx,
%%             TempMap) ->
%%      {Coloring, NumberOfSpills}
%%    Takes a CFG and the TempMap from register allocation and returns 
%%    a coloring of stack slots.  
%%    StackSlots should be a list of used stack slots, usually empty at
%%    first call to function.
%%    SpillIndex is the the first position we will spill to, usually 0.
%%    TempMap is the TempMap from the register allocation
%%
%%    The Coloring will be in the form of the "allocation datastructure"
%%    described below, that is, a list of tuples on the form
%%      {Name, {spill, SpillIndex}}
%%    The NumberOfSpills is either 0 indicating no spill or the 
%%    SpillIndex of the last spilled register.
%%
%%  mapmerge(Map, SpillMap) -> NewMap
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(hipe_spillmin).
-export([stackalloc/7, stackalloc/8, mapmerge/2]).

%%-define(DEBUG, 1).
-define(HIPE_INSTRUMENT_COMPILER, true).

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

-include("../main/hipe.hrl").
-include("../flow/cfg.hrl").

-type target_context() :: any().

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% stackalloc(CFG, StackSlots, SpillIndex, Options, Target, TempMap) 
%%   Calculates an allocation of stack slots using either a linear scan
%%   or a graph coloring allocation algorithm.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec stackalloc(#cfg{}, [_], non_neg_integer(),
		 comp_options(), module(), target_context(), hipe_temp_map()) ->
		    {hipe_spill_map(), non_neg_integer()}.
 
stackalloc(CFG, StackSlots, SpillIndex, Options, TgtMod, TgtCtx, TempMap) ->
  Liveness = TgtMod:analyze(CFG,TgtCtx),
  stackalloc(CFG, Liveness, StackSlots, SpillIndex, Options, TgtMod, TgtCtx, TempMap).

-spec stackalloc(#cfg{}, _, [_], non_neg_integer(),
		 comp_options(), module(), target_context(), hipe_temp_map()) ->
		    {hipe_spill_map(), non_neg_integer()}.

stackalloc(CFG, Liveness, StackSlots, SpillIndex, Options, TgtMod, TgtCtx,
	   TempMap) ->
  case proplists:get_bool(spillmin_color, Options) of
    false ->
      ?option_time(hipe_spillmin_scan:stackalloc(
		     CFG, Liveness, StackSlots, SpillIndex, Options, TgtMod,
		     TgtCtx, TempMap),
		   "Spill minimize, linear scan", Options);
    true ->
      ?option_time(hipe_spillmin_color:stackalloc(
		     CFG, Liveness, StackSlots, SpillIndex, Options, TgtMod,
		     TgtCtx, TempMap),
		   "Spill minimize, graph coloring", Options)
  end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% mapmerge(Map, SpillMap)
%%
%% stackalloc/6 will only return the subset of the tempmap that contains 
%% the spilled temporaries. This function is used to merge the old 
%% complete tempmap with the new spill information.
%% Map is the old map (a list of [{R0, C1}, {R1, C2}, ...]).
%% SpillMap is the new "spill" map.
%% !! Warning, the function does not work with the maps in another order !! 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Combines the map with allocated spills with a map from the register
%% allocator

-spec mapmerge(hipe_map(), hipe_spill_map()) -> hipe_map().

mapmerge(TempMap, SpillMap) ->
  mapmerge(TempMap, SpillMap, []).

mapmerge([], _, Ack) ->
  lists:reverse(Ack);
mapmerge([{T1, _}|T1s], [{T2, C}|T2s], Ack) when T1 =:= T2 -> 
  mapmerge(T1s, T2s, [{T1, C}|Ack]);
mapmerge([{_, unknown}|T1s], T2s, Ack) ->
  mapmerge(T1s, T2s, Ack);
mapmerge([T1|T1s], T2s, Ack) -> 
  mapmerge(T1s, T2s, [T1|Ack]).