aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/opt/hipe_ultra_prio.erl
blob: 6dd240a33aa2808687c5d8e8fbfb070444564946 (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
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
%% 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.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%	      PRIORITY HANDLING AND PRIORITY CALCULATION
%%
%% Handling of ready nodes and priorities.
%% Priorities are mainly from the critical path. More priorities are added.
%%  * One version is adding priorities just depending on the instr, so
%%    for example loads get higher priority than stores, and ordered
%%    after reg's and offset for better cache performance.
%%  * The other version gives higher priority to a node that adds more new
%%    nodes to the ready list. This one is maybe not so effectively
%%    implemented, but was added too late for smarter solutions.
%% One version is commented away

-module(hipe_ultra_prio).
-export([init_ready/2,
	 init_instr_prio/2,
	 %% initial_ready_set/4,
	 next_ready/7,
	 add_ready_nodes/2,
	 insert_node/3
	]).

-include("../sparc/hipe_sparc.hrl").

% At first, only nodes with no predecessors are selected.
% - if R is empty, there is an error (unless BB itself is empty)

%% Arguments : Size  - size of ready-array
%%             Preds - array with number of predecessors for each node
%% Returns   : An array with list of ready-nodes for each cycle.

init_ready(Size, Preds) ->
    P = hipe_vectors:size(Preds),
    Ready = hipe_vectors:new(Size, []),
    R = initial_ready_set(1, P, Preds, []),
    hipe_vectors:set(Ready, 0, R).

init_instr_prio(N, DAG) ->
    critical_path(N, DAG).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function    : initial_ready_set
%% Argument    : M     - current node-index
%%               N     - where to stop
%%               Preds - array with number of predecessors for each node
%%               Ready - list with ready-nodes
%% Returns     : Ready - list with ready-nodes
%% Description : Finds all nodes with no predecessors and adds them to ready.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

initial_ready_set(M, N, Preds, Ready) ->
    if
	M > N ->
	    Ready;
	true ->
	    case hipe_vectors:get(Preds, M-1) of
		0 ->
		    initial_ready_set(M+1, N, Preds, [M|Ready]);
		V when is_integer(V), V > 0 ->
		    initial_ready_set(M+1, N, Preds, Ready)
	    end
    end.

%% The following handles the nodes ready to schedule:
%% 1. select the ready queue of given cycle
%% 2. if queue empty, return none
%% 3. otherwise, remove entry with highest priority
%%    and return {next,Highest_Prio,NewReady}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function    : next_ready
%% Argument    : C     - current cycle
%%               Ready - array with ready nodes
%%               Prio  - array with cpath-priorities for all nodes
%%               Nodes - indexed list [{N, Instr}]
%% Returns     : none / {next,Highest_Prio,NewReady}
%% Description :  1. select the ready queue of given cycle
%%                2. if queue empty, return none
%%                3. otherwise, remove entry with highest priority
%%                   and return {next,Highest_Prio,NewReady} where Highest_Prio
%%                   = Id of instr and NewReady = updated ready-array.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

next_ready(C, Ready, Prio, Nodes, DAG, Preds, Earl) ->
    Curr = hipe_vectors:get(Ready, C-1),
    case Curr of
	[] -> 
	    none;
	Instrs ->
	    {BestI,RestIs} = 
		get_best_instr(Instrs, Prio, Nodes, DAG, Preds, Earl, C),
	    {next,BestI,hipe_vectors:set(Ready,C-1,RestIs)}
    end.

% next_ready(C,Ready,Prio,Nodes) ->
%     Curr = hipe_vectors:get(Ready,C-1),
%     case Curr of
% 	[] ->   
% 	    none;
% 	Instrs ->
% 	    {BestInstr,RestInstrs} = get_best_instr(Instrs, Prio, Nodes),
% 	    {next,BestInstr,hipe_vectors:set(Ready,C-1,RestInstrs)}
%     end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function    : get_best_instr
%% Argument    : Instrs - list of node-id's
%%               Prio   - array with cpath-priorities for the nodes
%%               Nodes  - indexed list [{Id, Instr}]
%% Returns     : {BestSoFar, Rest} - Id of best instr and the rest of id's
%% Description : Returns the id of the instr that is the best choice.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

get_best_instr([Instr|Instrs], Prio, Nodes, DAG, Preds, Earl, C) ->
    get_best_instr(Instrs, [], Instr, Prio, Nodes, DAG, Preds, Earl, C).

get_best_instr([], Rest, BestSoFar, _Prio, _Nodes, _DAG, _Preds, _Earl, _C) -> 
    {BestSoFar, Rest};
get_best_instr([Instr|Instrs], PassedInstrs, BestSoFar, Prio, Nodes,
	       DAG, Preds, Earl, C) ->
    case better(Instr, BestSoFar, Prio, Nodes, DAG, Preds, Earl, C) of
        true ->
	    get_best_instr(Instrs, [BestSoFar|PassedInstrs],
			   Instr, Prio, Nodes, DAG, Preds, Earl, C);
	false -> 
	    get_best_instr(Instrs, [Instr|PassedInstrs], BestSoFar, Prio, 
			   Nodes, DAG, Preds, Earl, C)
    end.

% get_best_instr([Instr|Instrs], Prio, Nodes) ->
%     get_best_instr(Instrs, [], Instr, Prio, Nodes).

% get_best_instr([], Rest, BestSoFar, Prio, Nodes) -> {BestSoFar, Rest};
% get_best_instr([Instr|Instrs], PassedInstrs, BestSoFar, Prio, Nodes) ->
%     case better(Instr, BestSoFar, Prio, Nodes) of
%         true ->
% 	    get_best_instr(Instrs, [BestSoFar|PassedInstrs], 
% 			   Instr, Prio, Nodes);
% 	false -> 
% 	    get_best_instr(Instrs, [Instr|PassedInstrs],BestSoFar, Prio, Nodes)
%     end.
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function    : better
%% Argument    : Instr1 - Id of instr 1
%%               Instr2 - Id of instr 2
%%               Prio   - array with cpath-priorities for the nodes
%%               Nodes  - indexed list [{Id, Instr}]
%% Returns     : true if Instr1 has higher priority than Instr2
%% Description : Checks if Instr1 is a better choice than Instr2 for scheduling
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

better(Instr1, Instr2, Prio, Nodes, DAG, Preds, Earl, C) ->
    better_hlp(priority(Instr1, Prio, Nodes, DAG, Preds, Earl, C), 
	       priority(Instr2, Prio, Nodes, DAG, Preds, Earl, C)).

better_hlp([], []) -> false;
better_hlp([], [_|_]) -> false;
better_hlp([_|_], []) -> true;
better_hlp([X|Xs], [Y|Ys]) -> (X > Y) or ((X =:= Y) and better_hlp(Xs,Ys)).

%%
%% Returns the instr corresponding to id
%%
get_instr(InstrId, [{InstrId,Instr}|_]) -> Instr;
get_instr(InstrId, [_|Xs]) -> get_instr(InstrId, Xs).
				      
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function    : priority
%% Argument    : InstrId - Id
%%               Prio    - array with cpath-priorities for the nodes
%%               Nodes   - indexed list [{Id, Instr}]
%% Returns     : PrioList - list of priorities [MostSignificant, LessSign, ...]
%% Description : Returns a list of priorities where the first element is the
%%               cpath-priority and the rest are added depending on what kind
%%               of instr it is. Used to order loads/stores sequentially and
%%               there is possibility to add whatever stuff...  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

priority(InstrId, Prio, Nodes, DAG, Preds, Earl, C) ->
    {ReadyNodes,_,_,_} = hipe_schedule:delete_node(C,InstrId,DAG,Preds,Earl),
    Instr = get_instr(InstrId, Nodes),
    Prio1 = hipe_vectors:get(Prio, InstrId-1),
    Prio2 = length(ReadyNodes),
    PrioRest =
	case Instr of
	 #load_atom{} ->
	    [3];
	 #move{} ->
	    [3];
	 #load{} -> 
  	    Src = hipe_sparc:load_src(Instr),
  	    Off = hipe_sparc:load_off(Instr),
	    case hipe_sparc:is_reg(Off) of
		false -> [3, 
			  -(hipe_sparc:reg_nr(Src)), 
			  -(hipe_sparc:imm_value(Off))];
		true -> [1]
	    end;
	 #store{} -> 
	     Src = hipe_sparc:store_dest(Instr),
	     Off = hipe_sparc:store_off(Instr),
	     case hipe_sparc:is_reg(Off) of
		 false -> [2, 
			   -(hipe_sparc:reg_nr(Src)), 
			   -(hipe_sparc:imm_value(Off))];
		 true -> [1]
	     end;
	 _ -> [0]
	end,
    [Prio1,Prio2|PrioRest].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function    : add_ready_nodes
%% Argument    : Nodes - list of [{Cycle,Id}]
%%               Ready - array of ready nodes for all cycles
%% Returns     : NewReady - updated ready-array
%% Description : Gets a list of instrs and adds them to the ready-array
%%               to the corresponding cycle.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

add_ready_nodes([], Ready) -> Ready;
add_ready_nodes([{C,I}|Xs], Ready) ->
    add_ready_nodes(Xs, insert_node(C, I, Ready)).

insert_node(C, I, Ready) ->
    Old = hipe_vectors:get(Ready, C-1),
    hipe_vectors:set(Ready, C-1, [I|Old]).

%%
%% Computes the latency for the "most expensive" way through the graph
%% for all nodes.  Returns an array of priorities for all nodes.
%%
critical_path(N, DAG) ->
    critical_path(1, N, DAG, hipe_vectors:new(N, -1)).

critical_path(M, N, DAG, Prio) ->
    if
	M > N ->
	    Prio;
	true ->
	    critical_path(M+1, N, DAG, cpath(M, DAG, Prio))
    end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function    : cpath
%% Argument    : M    - current node id
%%               DAG  - the dependence graph
%%               Prio - array of priorities for all nodes
%% Returns     : Prio - updated prio array
%% Description : If node has prio -1, it has not been visited
%%                - otherwise, compute priority as max of priorities of 
%%                  successors (+ latency)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

cpath(M, DAG, Prio) ->
    InitPrio = hipe_vectors:get(Prio, M-1),
    if
	InitPrio =:= -1 ->
	    cpath_node(M, DAG, Prio);
	true ->
	    Prio
    end.

cpath_node(N, DAG, Prio) ->
    SuccL = dag_succ(DAG, N),
    {Max, NewPrio} = cpath_succ(SuccL, DAG, Prio),
    hipe_vectors:set(NewPrio, N-1, Max).

cpath_succ(SuccL, DAG, Prio) ->
    cpath_succ(SuccL, DAG, Prio, 0).

%% performs an unnecessary lookup of priority of Succ, but that might
%% not be such a big deal

cpath_succ([], _DAG, Prio, NodePrio) -> {NodePrio,Prio};
cpath_succ([{Lat,Succ}|Xs], DAG, Prio, NodePrio) ->
    NewPrio = cpath(Succ, DAG, Prio),
    NewNodePrio = erlang:max(hipe_vectors:get(NewPrio, Succ - 1) + Lat, NodePrio),
    cpath_succ(Xs, DAG, NewPrio, NewNodePrio).

dag_succ(DAG, N) when is_integer(N) ->
    hipe_vectors:get(DAG, N-1).