aboutsummaryrefslogblamecommitdiffstats
path: root/lib/hipe/regalloc/hipe_regalloc_loop.erl
blob: 95574612443e8191b28725ce16a45d92ce1acf89 (plain) (tree)
1
2
3
4
5
6



                                  
                                                         
    










                                                                            





                                                               
                         



                                                                      



                                                                           
 

                                                               
 
                                                                          
                                    
                                                      


                                                                             

                                                                               
                                                    
                                                                 
                                
 
                                                                
                                
                                        
                                                                



                                                              
                                  
                                                                         








                                                                              
                                                 


                                                         


                                                                      



                                                                        

                                                                      


                                                  
                                                                                
            


                                                                           

      

                                                                           


                                                  
                                                                                
            

                                                                            
      
%%% -*- 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%
%%%
%%% Common wrapper for graph_coloring and coalescing regallocs.

-module(hipe_regalloc_loop).
-export([ra/6, ra_fp/5]).

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

ra(CFG, Liveness0, SpillIndex, Options, RegAllocMod, TargetMod) ->
  {NewCFG, Liveness, Coloring, _NewSpillIndex} =
    ra_common(CFG, Liveness0, SpillIndex, Options, RegAllocMod, TargetMod),
  {NewCFG, Liveness, Coloring}.

ra_fp(CFG, Liveness, Options, RegAllocMod, TargetMod) ->
  ra_common(CFG, Liveness, 0, Options, RegAllocMod, TargetMod).

ra_common(CFG0, Liveness0, SpillIndex, Options, RegAllocMod, TargetMod) ->
  ?inc_counter(ra_calls_counter, 1),
  SpillLimit0 = TargetMod:number_of_temporaries(CFG0),
  {Coloring, _, CFG, Liveness} =
    call_allocator_initial(CFG0, Liveness0, SpillLimit0, SpillIndex, Options,
			   RegAllocMod, TargetMod),
  %% The first iteration, the hipe_regalloc_prepass may create new temps, these
  %% should not end up above SpillLimit.
  SpillLimit = TargetMod:number_of_temporaries(CFG),
  alloc(Coloring, CFG, Liveness, SpillLimit, SpillIndex, Options,
	RegAllocMod, TargetMod).

alloc(Coloring, CFG0, Liveness, SpillLimit, SpillIndex, Options,
      RegAllocMod, TargetMod) ->
  ?inc_counter(ra_iteration_counter, 1),
  {CFG, DidSpill} = TargetMod:check_and_rewrite(CFG0, Coloring),
  case DidSpill of
    false -> %% No new temps, we are done.
      ?add_spills(Options, _NewSpillIndex),
      TempMap = hipe_temp_map:cols2tuple(Coloring, TargetMod),
      {TempMap2, NewSpillIndex2} =
	hipe_spillmin:stackalloc(CFG0, Liveness, [], SpillIndex, Options,
				 TargetMod, TempMap),
      Coloring2 = 
	hipe_spillmin:mapmerge(hipe_temp_map:to_substlist(TempMap), TempMap2),
      %% case proplists:get_bool(verbose_spills, Options) of
      %%   true ->
      %%     ?msg("Num spill slots used: ~p~n", [NewSpillIndex2-SpillIndex]);
      %%   false ->
      %%     ok
      %% end,
      {CFG, Liveness, Coloring2, NewSpillIndex2};
    _ ->
      %% Since SpillLimit is used as a low-water-mark
      %% the list of temps not to spill is uninteresting.
      {NewColoring, _NewSpillIndex} =
	call_allocator(CFG, Liveness, SpillLimit, SpillIndex, Options,
		       RegAllocMod, TargetMod),
      alloc(NewColoring, CFG, Liveness, SpillLimit, SpillIndex, Options,
	    RegAllocMod, TargetMod)
  end.

call_allocator_initial(CFG, Liveness, SpillLimit, SpillIndex, Options,
		       RegAllocMod, TargetMod) ->
  case proplists:get_bool(ra_prespill, Options) of
    true ->
      hipe_regalloc_prepass:regalloc_initial(
	RegAllocMod, CFG, Liveness, SpillIndex, SpillLimit, TargetMod, Options);
    false ->
      {C, SI} = RegAllocMod:regalloc(CFG, Liveness, SpillIndex, SpillLimit,
				     TargetMod, Options),
      {C, SI, CFG, Liveness}
  end.

call_allocator(CFG, Liveness, SpillLimit, SpillIndex, Options, RegAllocMod,
	       TargetMod) ->
  case proplists:get_bool(ra_prespill, Options) of
    true ->
      hipe_regalloc_prepass:regalloc(
	RegAllocMod, CFG, Liveness, SpillIndex, SpillLimit, TargetMod, Options);
    false ->
      RegAllocMod:regalloc(CFG, Liveness, SpillIndex, SpillLimit, TargetMod,
			   Options)
  end.