1%% -*- erlang-indent-level: 2 -*-
2%%
3%% Licensed under the Apache License, Version 2.0 (the "License");
4%% you may not use this file except in compliance with the License.
5%% You may obtain a copy of the License at
6%%
7%%     http://www.apache.org/licenses/LICENSE-2.0
8%%
9%% Unless required by applicable law or agreed to in writing, software
10%% distributed under the License is distributed on an "AS IS" BASIS,
11%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12%% See the License for the specific language governing permissions and
13%% limitations under the License.
14%%
15%% ==========================================================================
16%%  Module  :  hipe_spillmin
17%%  Purpose :  Driver module for minimizing the number of stack slots used
18%%	       by a function. This is done using an algorithm for register
19%%             allocation. The implementation is target-independent and
20%%             requires a target-specific interface module as argument.
21%% ==========================================================================
22%% Exported functions (short description):
23%%
24%%  stackalloc(CFG, StackSlots, SpillIndex, Options, TgtMod, TgtCtx,
25%%             TempMap) ->
26%%      {Coloring, NumberOfSpills}
27%%    Takes a CFG and the TempMap from register allocation and returns
28%%    a coloring of stack slots.
29%%    StackSlots should be a list of used stack slots, usually empty at
30%%    first call to function.
31%%    SpillIndex is the the first position we will spill to, usually 0.
32%%    TempMap is the TempMap from the register allocation
33%%
34%%    The Coloring will be in the form of the "allocation datastructure"
35%%    described below, that is, a list of tuples on the form
36%%      {Name, {spill, SpillIndex}}
37%%    The NumberOfSpills is either 0 indicating no spill or the
38%%    SpillIndex of the last spilled register.
39%%
40%%  mapmerge(Map, SpillMap) -> NewMap
41%%
42%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43
44-module(hipe_spillmin).
45-export([stackalloc/7, stackalloc/8, mapmerge/2]).
46
47%%-define(DEBUG, 1).
48-define(HIPE_INSTRUMENT_COMPILER, true).
49
50%%---------------------------------------------------------------------------
51
52-include("../main/hipe.hrl").
53-include("../flow/cfg.hrl").
54
55-type target_context() :: any().
56
57%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58%%
59%% stackalloc(CFG, StackSlots, SpillIndex, Options, Target, TempMap)
60%%   Calculates an allocation of stack slots using either a linear scan
61%%   or a graph coloring allocation algorithm.
62%%
63%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64
65-spec stackalloc(#cfg{}, [_], non_neg_integer(),
66		 comp_options(), module(), target_context(), hipe_temp_map()) ->
67		    {hipe_spill_map(), non_neg_integer()}.
68
69stackalloc(CFG, StackSlots, SpillIndex, Options, TgtMod, TgtCtx, TempMap) ->
70  Liveness = TgtMod:analyze(CFG,TgtCtx),
71  stackalloc(CFG, Liveness, StackSlots, SpillIndex, Options, TgtMod, TgtCtx, TempMap).
72
73-spec stackalloc(#cfg{}, _, [_], non_neg_integer(),
74		 comp_options(), module(), target_context(), hipe_temp_map()) ->
75		    {hipe_spill_map(), non_neg_integer()}.
76
77stackalloc(CFG, Liveness, StackSlots, SpillIndex, Options, TgtMod, TgtCtx,
78	   TempMap) ->
79  case proplists:get_bool(spillmin_color, Options) of
80    false ->
81      ?option_time(hipe_spillmin_scan:stackalloc(
82		     CFG, Liveness, StackSlots, SpillIndex, Options, TgtMod,
83		     TgtCtx, TempMap),
84		   "Spill minimize, linear scan", Options);
85    true ->
86      ?option_time(hipe_spillmin_color:stackalloc(
87		     CFG, Liveness, StackSlots, SpillIndex, Options, TgtMod,
88		     TgtCtx, TempMap),
89		   "Spill minimize, graph coloring", Options)
90  end.
91
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93%% mapmerge(Map, SpillMap)
94%%
95%% stackalloc/6 will only return the subset of the tempmap that contains
96%% the spilled temporaries. This function is used to merge the old
97%% complete tempmap with the new spill information.
98%% Map is the old map (a list of [{R0, C1}, {R1, C2}, ...]).
99%% SpillMap is the new "spill" map.
100%% !! Warning, the function does not work with the maps in another order !!
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103%% Combines the map with allocated spills with a map from the register
104%% allocator
105
106-spec mapmerge(hipe_map(), hipe_spill_map()) -> hipe_map().
107
108mapmerge(TempMap, SpillMap) ->
109  mapmerge(TempMap, SpillMap, []).
110
111mapmerge([], _, Ack) ->
112  lists:reverse(Ack);
113mapmerge([{T1, _}|T1s], [{T2, C}|T2s], Ack) when T1 =:= T2 ->
114  mapmerge(T1s, T2s, [{T1, C}|Ack]);
115mapmerge([{_, unknown}|T1s], T2s, Ack) ->
116  mapmerge(T1s, T2s, Ack);
117mapmerge([T1|T1s], T2s, Ack) ->
118  mapmerge(T1s, T2s, [T1|Ack]).
119