File: hipe_sparc_finalize.erl

package info (click to toggle)
erlang 1%3A11.b.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 54,332 kB
  • ctags: 138,013
  • sloc: erlang: 566,932; ansic: 185,450; makefile: 14,148; java: 7,835; sh: 7,307; lisp: 5,249; pascal: 3,225; perl: 2,290; asm: 1,325; cpp: 306; tcl: 245; csh: 29; python: 21; sed: 9
file content (246 lines) | stat: -rw-r--r-- 6,407 bytes parent folder | download | duplicates (2)
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
%%% -*- erlang-indent-level: 2 -*-
%%%----------------------------------------------------------------------
%%% File    : hipe_sparc_finalize.erl
%%% Author  : Christoffer Vikstrm <chvi3471@student.uu.se>
%%% Purpose : 
%%% Created : 16 Feb 2004 by Christoffer Vikstrm <chvi3471@student.uu.se>
%%%----------------------------------------------------------------------

-module(hipe_sparc_finalize).
-export([finalize/2]).

-include("hipe_sparc.hrl").

finalize(CFG,Options) ->
  OptCFG = case proplists:get_bool(sparc_peephole,Options) of
	     true  -> hipe_sparc_cfg:remove_trivial_bbs(CFG);
	     false -> CFG
	   end,
  LinearCode = hipe_sparc:sparc_code(hipe_sparc_cfg:linearize(OptCFG)),
  OptLinearCode = hipe_sparc_peephole:peep(LinearCode),
  OptLinearCode2 = fill_delay(OptLinearCode, Options),
  {OptCFG, lists:flatten(OptLinearCode2)}.


%% opt(Code) ->
%%     lists:reverse(opt(Code,[])).

%% opt([G,L|Code], Acc) ->
%%   case hipe_sparc:is_goto(G) of
%%       true ->
%% 	  case hipe_sparc:is_label(L) of
%% 	      true ->
%% 		  case hipe_sparc:goto_label(G) =:= hipe_sparc:label_name(L) of
%% 		      true ->
%% 			  opt(Code,[L|Acc]);
%% 		      false ->
%% 			  opt(Code,[L,G|Acc])
%% 		  end;
%% 	      false ->
%% 		  opt([L|Code], [G|Acc])
%% 	  end;
%%       false ->
%% 	  opt([L|Code], [G|Acc])
%%   end;
%% opt([I],Acc) ->
%%     [I|Acc];
%% opt([],Acc) ->
%%     Acc.

%% %%
%% %% 
%% %%

%% peephole([]) ->
%%   [];
%% peephole([I|Is]) ->
%%    case I of
%%      #move{} ->
%%        case hipe_sparc:move_src(I) =:= hipe_sparc:move_dest(I) of
%% 	 true ->
%% 	   peephole(Is);
%% 	 false ->
%% 	   [I | peephole(Is)]
%%        end;
%%       _ ->
%%        [I | peephole(Is)]
%%    end.

%%
%% Code is a list of instructions. 
%%

fill_delay(Code, Options) ->
  Codes =  split_at_branch(Code),
  case proplists:get_bool(fill_delayslot,Options) of
    true  -> [fill_delay0(Is) || Is <- Codes];
    false -> [nofill_delay(Is) || Is <- Codes]
  end.

nofill_delay(Code) ->
  [Code | [hipe_sparc:nop_create()]].

%%
%% Code is a list of instructions where a branch/jump 
%%

fill_delay0(Code) ->
  case catch find_delay(Code) of
    no_branch ->
      Code;
    {NewCode, _, _, _} ->
      [NewCode | [hipe_sparc:nop_create()]];
    {NewCode, Delay} ->
      [NewCode | [Delay]]
  end.

%%
%% Extracts a delay instruction from a list 
%%

find_delay([Jmp]) ->
  case hipe_sparc:has_delayslot(Jmp) of
    true ->
      {[Jmp], 
       ordsets:from_list(hipe_sparc:uses(Jmp)), 
       ordsets:from_list(hipe_sparc:defines(Jmp)),
       []};
    false ->
      throw(no_branch)
  end;
find_delay([I|Is]) ->
  case find_delay(Is) of
    {NewIs, Uses, Defs, Loads} ->
      IUses = ordsets:from_list(hipe_sparc:uses(I)),
      IDefs = ordsets:from_list(hipe_sparc:defines(I)),
      NewUses = ordsets:union(Uses, IUses),
      NewDefs = ordsets:union(Defs, IDefs),
      IsStore = hipe_sparc:is_store(I),
      NewLoads = case hipe_sparc:is_load(I) of 
		   true -> [I|Loads];
		   false -> Loads
		 end,
      case is_delay_instr(I) of
	true ->
	  %% Make sure the instruction isn't defining a reg that is 
	  %% used later or uses a reg that is defined later or
	  %% defines a reg that is defined later
	  X = {ordsets:intersection(Uses, IDefs), 
	       ordsets:intersection(Defs, IUses),
	       ordsets:intersection(Defs, IDefs)},
	  case X of
	    {[], [], []} ->  %% No register conflicts
	      case {IsStore,Loads} of
		{_,[]} -> % No following loads
		  {NewIs, I}; % found a delay instr.
		{false,_} -> % This is not a store
		  {NewIs, I}; % found a delay instr.
		_ -> %% A store with following loads.
		  %% XXX: TODO check whether the store 
		  %%      and the load REALY conflicts.
		  %%      (Stack and Heap does not conflict)
		  {[I|NewIs], NewUses, NewDefs, NewLoads} %% Search more
	      end;
	    _ -> %% Register conflicts.
	      {[I|NewIs], NewUses, NewDefs, NewLoads}
	  end;
	false ->
	  {[I|NewIs], NewUses, NewDefs, NewLoads}
      end;
    {NewIs, Delay} ->
      {[I|NewIs], Delay}
  end.

%%
%% true if I is an instruction that can be moved to a delay slot
%%

is_delay_instr(I) ->
  case I of
    #label{} -> false;
    #comment{} -> false;
    #load_address{} -> false;
    #load_atom{} -> false;
    #load_word_index{} -> false;
    #fop{} -> false;
    %%         in the delayslot can slow down code...
    %%         ... but it can also speed up code.
    %%         the impact is about 10 - 20 % on small bms
    %%         on the average you loose 1-2 % by not putting
    %%         loads in delayslots
    %% #load{} -> false;
    _ -> true
  end.

%%
%% Split a list of instructions to a list of lists of instructions
%% where each sublist ends with a branch.
%%

split_at_branch([]) ->
   [];
split_at_branch([I|Rest]) ->
  case Rest of
    [L|_] ->
      case hipe_sparc:is_label(L) of
	true -> 
	  Lname = hipe_sparc:label_name(L),
	  split(I,Lname,Rest);
	false ->
	  split(I,[],Rest)
      end;
    _ ->
      split(I,[],Rest)
  end.

split(I,Lname,Is)->
  case I of
    #b{} -> 
      case hipe_sparc:b_false_label(I) of
	Lname ->
	  [[hipe_sparc:b_false_label_update(I,[])] |
	   split_at_branch(Is)];
	FL ->
	  [[hipe_sparc:b_false_label_update(I,[])], 
	   [hipe_sparc:goto_create(FL)]|
	   split_at_branch(Is)]
      end;
%%  #br{} -> 
%%       case hipe_sparc:br_false_label(I) of
%% 	Lname ->
%% 	  [[hipe_sparc:br_false_label_update(I,[])] |
%% 	   split_at_branch(Is)];
%% 	FL ->
%% 	  [[hipe_sparc:br_false_label_update(I,[])], 
%% 	   [hipe_sparc:goto_create(FL)]|
%% 	   split_at_branch(Is)]
%%       end;
    #goto{} -> 
      [[I] | split_at_branch(Is)];
    #jmp{} -> 
      [[I] | split_at_branch(Is)];
    #jmp_link{} -> 
      [[I] | split_at_branch(Is)];
    #call_link{} ->
%%      [[I] | split_at_branch(Is)];
      case hipe_sparc:call_link_continuation(I) of
	[] -> 
%%	  NL = hipe_sparc:label_create_new(),
%%	  NLname = hipe_sparc:label_name(NL),
	 [[I] | split_at_branch(Is)];
%%	  [[hipe_sparc:goto_create(NLname)]|
%%	   split_at_branch([NL|Is])]];
	Lname -> 
	  [[hipe_sparc:call_link_continuation_update(I,[])] |
	   split_at_branch(Is)];
	CCL ->
	  [[hipe_sparc:call_link_continuation_update(I,[])],
	   [hipe_sparc:goto_create(CCL)]|
	   split_at_branch(Is)]
      end;
    #label{} ->
      [[I] | split_at_branch(Is)];
    _ -> 
      [Same|Lists] = split_at_branch(Is),
      [[I|Same]|Lists]
  end.