File: hipe_sparc_caller_saves.erl

package info (click to toggle)
erlang 1%3A12.b.3-dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 77,780 kB
  • ctags: 157,528
  • sloc: erlang: 664,178; ansic: 241,119; makefile: 15,725; xml: 8,378; java: 7,780; sh: 6,789; lisp: 5,396; pascal: 3,637; perl: 2,310; asm: 1,438; cpp: 975; tcl: 245; python: 21; sed: 20; awk: 15
file content (454 lines) | stat: -rw-r--r-- 14,123 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
%% -*- erlang-indent-level: 2 -*-
%% ====================================================================
%%  Filename : 	hipe_sparc_caller_saves.erl
%%  Module   :	hipe_sparc_caller_saves
%%  Purpose  :  To add save and restore code for caller save regs
%%              at call sites.
%%  Notes    :  LastSpillPos is a bad name in this module,
%%               since it denotes the postion after the last used
%%               position.
%%  History  :	* 2001-11-16 Erik Johansson (happi@csd.uu.se): 
%%               Created.
%%  CVS      :
%%              $Author: mikpe $
%%              $Date: 2007/12/18 09:18:21 $
%%              $Revision: 1.1 $
%% ====================================================================
%%  Exports  :
%%
%%  Description:
%%
%%   1. Calculate liveness. 
%%      (Consider reusing the liveness calculated by the regalloc.)
%%
%%   2. Go through each basic block bottom up.
%%      For each call c:
%%         For each live out temp t. (that is not a retval of c)
%%             If t is spilled: Do nothing.
%%             If t is not spilled:
%%                a) Get a spillposition p for t.
%%                b) Add code for spilling t to p before the call.
%%                c) add code for unspilling t from p after the call.
%%                d) If any spillcode was added rewrite the call
%%                     If there is an exception handler: add the unspill
%%                       code to both branches of the call.
%%
%%   3. Optimization pass: Place the spillcode "optimally".
%%                        Implemented in hipe_sparc_opt_frame
%%
%%  TODO: Either make this module completely target independent or
%%        completely target dependent.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(hipe_sparc_caller_saves).
-export([rewrite/5]).

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

%% XXX: Make target independent.
-define(TARGET, hipe_sparc_specific).

%% XXX: Make target independent.
rewrite(Cfg, TempMap, FpMap, LastSpillPos, Options) ->
  %% hipe_sparc_cfg:pp(Cfg),  
  ?opt_start_timer("Caller Saves"),
  ?start_time_caller_saves(Options),
  %% Get Info.
  Liveness = liveness(Cfg, ?TARGET),
  State = new_state(Liveness,  Cfg, TempMap, FpMap, LastSpillPos),
  BBs = hipe_sparc_cfg:predictionorder(Cfg),

  %% Rewrite.
  {NewCode, NextPos, NewBlocks} = traverse(BBs, State),

  %% Make new CFG.
  Sparc0 = hipe_sparc_cfg:linearize(Cfg),
  Sparc1 = hipe_sparc:sparc_code_update(Sparc0, NewCode),
  Sparc2 = hipe_sparc:sparc_var_range_update(
	     Sparc1, {0,hipe_gensym:get_var(sparc)}),
  Sparc3 = hipe_sparc:sparc_label_range_update(
	     Sparc2, {0,hipe_gensym:get_label(sparc)}),  
  Cfg1 = hipe_sparc_cfg:init(Sparc3),

  %% Minimize the number of loads and stores.
  ?opt_stop_timer("Caller Saves"),
  ?opt_start_timer("Opt Frame"),
  %% hipe_sparc_cfg:pp(Cfg1),
  Cfg2 = hipe_sparc_opt_frame:cfg(Cfg1),
  %% hipe_sparc_cfg:pp(Cfg2),
  ?opt_stop_timer("Opt Frame"),
  ?stop_time_caller_saves(Options),
  case proplists:get_value(regalloc,Options) of
    cs ->
      {Cfg2, NextPos, NewBlocks};
    _ ->
      {Cfg2, NextPos}
  end.

%% Go through each BasicBlock
traverse(BBs, State) ->
  State1 = lists:foldr(fun handle_bb/2,State,BBs),  
  {state__instrs(State1),state__spill_index(State1),state__newblocks(State1)}.
  
%% For each BasicBlock go through the code bottom-up.
handle_bb(BB,State) ->
  %% io:format("--------------\n",[]),
  Code =  hipe_bb:code(bb(state__cfg(State),BB, ?TARGET)),
  State1 = state__enter_bb(BB, State),
  State2 = bottom_up(Code, State1),
  %% io:format("-- Block ~w --\n",[BB]),
  state__add_instrs(hipe_sparc:label_create(BB), State2).

%% Go through each instruction from the last to the first.
%% (Collect liveness from live out and backwards.)
bottom_up(Is, State) ->
  lists:foldr(fun handle/2,State,Is).

%% For each instruction rewrite calls so that live temps are saved
handle(I,State) ->
  {FpDefs, Defs}  = defines(I, ?TARGET),
  {FpUses, Uses} = uses(I, ?TARGET),
  LiveOut = state__live(State)-- hipe_sparc_registers:global(),
  Live = (LiveOut--Defs), %% Remove killed
  LiveIn = ordsets:union(ordsets:from_list(Live),ordsets:from_list(Uses))
    -- hipe_sparc_registers:global(),

  FpLiveOut = state__live_fp(State),
  FpLive = (FpLiveOut--FpDefs), %% Remove killed
  FpLiveIn = ordsets:union(ordsets:from_list(FpLive),ordsets:from_list(FpUses)),


    %% io:format("~nLiveOut: ~w~n",[LiveOut-- hipe_sparc_registers:global()]),
    %% hipe_sparc_pp:pp_instr(I),
    %% io:format("LiveIn: ~w~n",[LiveIn]),

  %% Add save and restores if neccessary.
  State1 = update_call(I,State,Live, LiveOut, FpLive),

  %% Update the liveness information in the state.
  State2 = state__update_live(LiveIn, State1),
  state__update_live_fp(FpLiveIn, State2).


%% XXX: Make this target independent.
update_call(I,State,Live,_LiveOut,FpLive) ->
  case I of
    #call_link{} ->
      case {live_in_regs(Live), FpLive} of
	{[], []} -> 
	  %% All live temps are allready on the stack.
	  %% No need for extra saving.
	  %% Just add the live-info to the stack descriptor 
	  %%  of the call.
	  state__add_instrs(set_live_slots(I,State,Live),State);
	{ToSpill, ToSpillFp} ->
	  %% We have at least one live temp that is not on the stack
	  handle_call(I,State,ToSpill,Live, ToSpillFp)
      end;
    
    _ ->
      %% This is not a call, nothing to save.
      %% Just add the instruction to the state.
      state__add_instrs(I,State)
  end.



%% Ugly function that finds the stack slots of all live temps
%% and adds these to the stack descriptor of the call. 
set_live_slots(Call,State,LiveOut) ->
  Live =
    [stack_pos(T,State, reg) || T <- LiveOut],
  SD = hipe_sparc:call_link_stack_desc(Call),
  NewSD = hipe_sparc:sdesc_live_slots_update(SD, Live),
  hipe_sparc:call_link_stack_desc_update(Call,NewSD).

%% Helper function to set_live_slots.
%% Maps a Temp to its stack pos.
stack_pos(T,State, Type) ->
  Map = 
    case Type of
      reg -> state__tempmap(State);
      fp_reg-> state__fpmap(State)
    end,
  case hipe_temp_map:find(T, Map) of
    {spill, P} ->
      P;
    _ ->
      element(1,get_pos(T,state__extramap(State), Type))
  end.
      
%% XXX: Make target independent.
live_in_regs(Live) ->
  %% XXX: We have to save spilled regs with new call-split allocation
  %%      Make this modular so other allocators are unaffected.
  [T || T <- Live ] 
    -- hipe_sparc_registers:global().


handle_call(I,State,ToSpill,LiveOut, FpLive)->
  %% a) Get a spillposition p for t.
  %% b) Add code for spilling t to p before the call.
  %% c) add code for unspilling t from p after the call.
  %% d) If any spillcode was added rewrite the call
  %%      If there is an exception handler: add the unspill
  %%        code to both branches of the call.


  ExtraMap = state__extramap(State),
  {SpillPositions, NewExtraMap0} =
    get_spill_positions(ToSpill, ExtraMap, State, reg),
  {Saves0, Restores0} = gen_save_restore(SpillPositions, reg),
  {SpillPositions_fp, NewExtraMap} =
    get_spill_positions(FpLive, NewExtraMap0, State, fp_reg),
  {Saves1, Restores1} = gen_save_restore(SpillPositions_fp, fp_reg),

  State1 = state__update_extramap(NewExtraMap, State),

  ContLab = hipe_sparc:call_link_continuation(I),
  FailLab = hipe_sparc:call_link_fail(I),
  NewContLab = hipe_sparc:label_create_new(),
  GotoCont =  hipe_sparc:goto_create(ContLab),
  {NewI,State2} =
    case ContLab of
      [] -> {I, State1};
      _ ->
	{
	hipe_sparc:call_link_continuation_update(
	  I,
	  hipe_sparc:label_name(NewContLab)),
	state__add_block(hipe_sparc:label_name(NewContLab),ContLab, State1)}
    end,

   
  %% Update stack descriptor. Fp regs must not be marked as live.
  NewCall = set_live_slots(NewI,State2,LiveOut),

  case ContLab of
    [] ->
       NewCode = 
	[Saves0,
	 Saves1,
	 NewCall,
	 Restores0,
	 Restores1],
      state__add_instrs(NewCode,State2);
    _ ->
      case FailLab of
	[] ->
	  NewCode = 
	    [Saves0,
	     Saves1,
	     NewCall,
	     NewContLab,
	     Restores0,
	     Restores1,
	     GotoCont],
	  state__add_instrs(NewCode,State2);
	_ ->
	  NewFailLab = hipe_sparc:label_create_new(),
	  GotoFail =  hipe_sparc:goto_create(FailLab),
	  NewI2 = 
	    hipe_sparc:call_link_fail_update(
	      NewCall,
	      hipe_sparc:label_name(NewFailLab)),
	  State3 = 
	    state__add_block(hipe_sparc:label_name(NewFailLab),FailLab, State2),
	  NewCode = 
	    [Saves0, 
	     Saves1,
	     NewI2,
	     NewContLab,
	     Restores0,
	     Restores1,
	     GotoCont,
	     NewFailLab,
	     Restores0,
	     Restores1,
	     GotoFail],
	  state__add_instrs(NewCode,State3)
      end
  end.


get_spill_positions([T|Temps], ExtraMap, State, Type) ->
  TempMap = 
    case Type of
      reg -> state__tempmap(State);
      fp_reg -> state__fpmap(State)
    end,
  {Pos, NewMap} = 
    case hipe_temp_map:is_spilled(T, TempMap) of
      true ->
	{stack_pos(T, State, Type),ExtraMap};
      false ->
	get_pos(T,ExtraMap, Type)
    end,
  {Positions, Map} = get_spill_positions(Temps, NewMap, State, Type),
  {[{T,Pos}|Positions], Map};
get_spill_positions([],Map,_, _) ->
  {[],Map}.

gen_save_restore([{T,Pos}|Positions], Type) ->
  SparcTemp = 
    case Type of
      fp_reg -> hipe_sparc:mk_fpreg(T);
      reg -> hipe_sparc:mk_reg(T)
    end,
  SparcPos = hipe_sparc:mk_imm(Pos),
  {Saves, Restores} = gen_save_restore(Positions, Type),
  Save = hipe_sparc:pseudo_spill_create(SparcTemp,SparcPos),
  Restore = hipe_sparc:pseudo_unspill_create(SparcTemp,SparcPos),
  {[Save | Saves], [Restore | Restores]};
gen_save_restore([], _) -> {[],[]}.



%% ================================================================= %%
%%                             A  D  T s                             %%
%% ================================================================= %%


%% ================================================================= %%
%% The ExtraMap structure
%% Maps unspilled temps to spillpositions.
%% 

%% ____________________________________________________________________
%%  new_extramap(LastSpillPos)    
%% Returns: 	An empty ExtraMap.
%% Arguments:	LastSpillPos - The next spill will be to this pos.
%% Description:	Creates an empty ExtraMap 
%% ____________________________________________________________________
new_extramap(LastSpillPos) ->
  {LastSpillPos, empty()}.

index(Map) ->
  %% io:format("Map: ~w\n",[Map]),
  element(1,Map).

%% ____________________________________________________________________
%%  get_pos(T,ExtraMap, Type)    
%% Returns: 	
%% Arguments:	
%% Description:	Finds the spill position of the temp T,
%%              If T is not in the map T will be added. 
%%              Type is either reg or fp_reg 
%% ____________________________________________________________________
get_pos(T,{Next,Map}, Type) ->
  case lookup(T,Map) of
    none ->
      case Type of
	fp_reg -> {Next, {Next+2,insert(T,Next,Map)}};
	reg -> {Next, {Next+1,insert(T,Next,Map)}}
      end;
    {value, Pos} ->
      {Pos, {Next,Map}}
  end.


%% ____________________________________________________________________
%% 
%% The Map kernel.


empty() ->
  gb_trees:empty().
lookup(T,Map) ->
  gb_trees:lookup(T,Map).
insert(T,Next,Map) ->
  gb_trees:insert(T,Next,Map).



%% ================================================================= %%
%%
%% The state structure
%%
%% ================================================================= %%

%% ____________________________________________________________________
%% The state record.
-record(state, {instrs, extramap, live, live_fp, liveness, cfg, tempmap, fpmap, newblocks}).

%% Create a new state.
new_state(Liveness,  Cfg, TempMap, FpMap, LastSpillPos) ->
  #state{instrs = [], 
	 extramap = new_extramap(LastSpillPos), 
	 live = [],
	 live_fp = [],
	 liveness = Liveness,  
	 cfg = Cfg,
	 tempmap = TempMap,
	 fpmap = FpMap,
	 newblocks = []}.

%% ____________________________________________________________________
%% Selectors

state__instrs(#state{instrs=Instrs}) -> lists:flatten(Instrs).
state__extramap(#state{extramap=ExtraMap}) -> ExtraMap.
state__live(#state{live=Live}) -> Live.
state__live_fp(#state{live_fp=LiveFP}) -> LiveFP.
state__liveness(#state{liveness=Liveness}) -> Liveness.
state__cfg(#state{cfg=CFG}) -> CFG.
state__tempmap(#state{tempmap=TempMap}) -> TempMap.
state__fpmap(#state{fpmap=FpMap}) -> FpMap.
state__newblocks(#state{newblocks=NewBlocks}) -> NewBlocks.

state__spill_index(State) -> index(state__extramap(State)).

%% ____________________________________________________________________
%% Updates

state__enter_bb(BB, State) ->
  {FpReg, Reg} = liveout(state__liveness(State), BB, hipe_sparc_specific),
  State#state{live=ordsets:from_list(Reg), live_fp=ordsets:from_list(FpReg)}.
state__add_instrs(I,State) ->
  State#state{instrs=[I|state__instrs(State)]}.
state__update_extramap(NewExtraMap, State) ->
  State#state{extramap=NewExtraMap}.
state__update_live(NewLive, State) ->
  State#state{live=NewLive}.
state__update_live_fp(NewLive, State) ->
  State#state{live_fp=NewLive}.
state__add_block(New,Old, State) ->
   State#state{newblocks=[{New,Old}|state__newblocks(State)]}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Interface to target specific external functions.
%% XXX: Make this efficient somehow...
%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

liveness(CFG, Target) ->
   Target:analyze(CFG).

bb(CFG, L, Target) ->
  Target:bb(CFG,L).

liveout(Liveness,L, Target)->
  regnames(Target:liveout(Liveness,L), Target).

uses(I, Target)->
  regnames(Target:uses(I), Target).

defines(I, Target) ->
  regnames(Target:defines(I), Target).

regnames(Regs, _Target) ->
  regnames(Regs, [], []).

regnames([Reg|Rest], Fp_regs, Regs) ->
  case hipe_sparc:is_fpreg(Reg) of
    true->
      regnames(Rest, [hipe_sparc:fpreg_nr(Reg) | Fp_regs], Regs);
    _ ->
      regnames(Rest, Fp_regs, [hipe_sparc:reg_nr(Reg) | Regs])
  end;
regnames([], Fp_regs, Regs) ->
  {Fp_regs, Regs}.