74cffb9fcf7f9c2fdfb84f679b6d2cf67e62f682
[libfirm] / ir / be / beschednormal.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @brief   Use the strong normal form theorem (though it does not hold)
22  * @author  Christoph Mallon
23  * @version $Id: beschedrand.c 14604 2007-06-18 14:07:07Z matze $
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <stdlib.h>
30
31 #include "besched_t.h"
32 #include "belistsched.h"
33 #include "belive_t.h"
34 #include "beutil.h"
35 #include "irtools.h"
36 #include "irgwalk.h"
37
38
39 // XXX there is no one time init for schedulers
40 //#define NORMAL_DBG
41
42
43 static const arch_env_t *cur_arch_env;
44
45
46 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
47                               ir_nodeset_t *live_set)
48 {
49         ir_nodeset_iterator_t iter;
50         ir_node*  block;
51         ir_node*  irn;
52         ir_node** sched;
53         int sched_count;
54
55         (void)block_env;
56         (void)live_set;
57
58         ir_nodeset_iterator_init(&iter, ready_set);
59         irn = ir_nodeset_iterator_next(&iter);
60         block = get_nodes_block(irn);
61         sched = get_irn_link(block);
62         sched_count = ARR_LEN(sched);
63         for (; sched_count-- != 0; ++sched) {
64                 ir_node* irn = *sched;
65                 if (ir_nodeset_contains(ready_set, irn) &&
66                                 !arch_irn_class_is(cur_arch_env, irn, branch)) {
67 #if defined NORMAL_DBG
68                         ir_fprintf(stderr, "scheduling %+F\n", irn);
69 #endif
70                         return irn;
71                 }
72         }
73
74         return irn;
75 }
76
77
78 typedef struct irn_cost_pair {
79         ir_node* irn;
80         int      cost;
81 } irn_cost_pair;
82
83
84 static int cost_cmp(const void* a, const void* b)
85 {
86         const irn_cost_pair* a1 = a;
87         const irn_cost_pair* b1 = b;
88         return b1->cost - a1->cost;
89 }
90
91
92 typedef struct flag_and_cost {
93         int no_root;
94         irn_cost_pair costs[];
95 } flag_and_cost;
96
97
98 static int count_result(const ir_node* irn)
99 {
100         const ir_mode* mode = get_irn_mode(irn);
101         return
102                 mode != mode_M &&
103                 mode != mode_X &&
104                 !arch_irn_is(cur_arch_env, irn, ignore);
105 }
106
107
108 static int normal_tree_cost(ir_node* irn)
109 {
110         flag_and_cost* fc    = get_irn_link(irn);
111         ir_node*       block = get_nodes_block(irn);
112         int            arity = get_irn_arity(irn);
113         int            cost_max  = 0;
114         int            count_max = 0;
115         int            n_res;
116         int            cost;
117
118         if (fc == NULL) {
119                 irn_cost_pair* costs;
120                 int            i;
121
122                 fc = malloc(sizeof(*fc) + sizeof(*fc->costs) * arity);
123                 fc->no_root = 0;
124                 costs = fc->costs;
125
126                 for (i = 0; i < arity; ++i) {
127                         ir_node* pred = get_irn_n(irn, i);
128                         int cost;
129
130                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M) {
131                                 cost = 0;
132                         } else if (get_nodes_block(pred) != block) {
133                                 cost = 1;
134                         } else {
135                                 cost = normal_tree_cost(pred);
136                                 flag_and_cost* pred_fc = get_irn_link(pred);
137                                 pred_fc->no_root = 1;
138 #if defined NORMAL_DBG
139                                 ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, pred);
140 #endif
141                         }
142
143                         costs[i].irn  = pred;
144                         costs[i].cost = cost;
145
146                         if (cost > cost_max) {
147                                 cost_max  = cost;
148                                 count_max = 1;
149                         } else if (cost == cost_max) {
150                                 ++count_max;
151                         }
152                 }
153
154                 qsort(costs, arity, sizeof(*costs), cost_cmp);
155                 set_irn_link(irn, fc);
156         } else {
157                 irn_cost_pair* costs = fc->costs;
158                 int            i;
159
160                 if (arity > 0) {
161                         cost_max = costs[0].cost;
162
163                         for (i = 0; i < arity; ++i) {
164                                 if (costs[i].cost < cost_max) break;
165                                 ++count_max;
166                         }
167                 }
168         }
169
170         n_res = count_result(irn);
171         if (cost_max == 0) {
172                 cost = n_res;
173         } else {
174                 cost = MAX(n_res, cost_max + count_max - 1);
175         }
176
177 #if defined NORMAL_DBG
178         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
179 #endif
180
181         return cost;
182 }
183
184
185 static void normal_tree_sched(ir_node* irn)
186 {
187         irn_cost_pair* irns  = get_irn_link(irn);
188         int            arity = get_irn_arity(irn);
189         int            i;
190
191         if (irns == NULL) return;
192
193         for (i = 0; i < arity; ++i) {
194                 normal_tree_sched(irns[i].irn);
195         }
196
197         if (1) { // TODO check if node needs to be scheduled
198                 ir_node*  block = get_nodes_block(irn);
199                 ir_node** sched = get_irn_link(block);
200
201 #if defined NORMAL_DBG
202                 ir_fprintf(stderr, "scheduling %+F in array %p\n", irn, sched);
203 #endif
204
205                 if (sched == NULL) {
206                         sched = NEW_ARR_F(ir_node*, 0);
207                 }
208                 ARR_APP1(ir_node*, sched, irn);
209                 set_irn_link(block, sched);
210         }
211
212         free(irns);
213         set_irn_link(irn, NULL);
214 }
215
216
217 static void normal_cost_walker(ir_node* irn, void* env)
218 {
219         (void)env;
220
221 #if defined NORMAL_DBG
222         ir_fprintf(stderr, "cost walking node %+F\n", irn);
223 #endif
224         if (is_Block(irn)) return;
225         normal_tree_cost(irn);
226 }
227
228
229 static void collect_roots(ir_node* irn, void* env)
230 {
231         flag_and_cost* fc;
232
233         (void)env;
234
235         if (is_Block(irn)) return;
236
237         fc = get_irn_link(irn);
238
239 #if defined NORMAL_DBG
240         ir_fprintf(stderr, "%+F is %sroot\n", irn, fc->no_root ? "no " : "");
241 #endif
242
243         if (!fc->no_root) {
244                 ir_node* block = get_nodes_block(irn);
245                 ir_node** roots = get_irn_link(block);
246                 if (roots == NULL) {
247                         roots = NEW_ARR_F(ir_node*, 0);
248                 }
249                 ARR_APP1(ir_node*, roots, irn);
250                 set_irn_link(block, roots);
251         }
252 }
253
254
255 static ir_node** sched_node(ir_node** sched, ir_node* irn)
256 {
257         ir_node* block = get_nodes_block(irn);
258         int arity = get_irn_arity(irn);
259         int i;
260
261         if (!is_Phi(irn)) {
262                 for (i = 0; i < arity; ++i) {
263                         ir_node* pred = get_irn_n(irn, i);
264                         if (get_nodes_block(pred) != block) continue;
265                         sched = sched_node(sched, pred);
266                 }
267         }
268
269         ARR_APP1(ir_node*, sched, irn);
270         return sched;
271 }
272
273
274 static void normal_sched_block(ir_node* block, void* env)
275 {
276         ir_node** roots = get_irn_link(block);
277         int            root_count;
278         irn_cost_pair* root_costs;
279         int i;
280         ir_node**      sched;
281
282         (void)env;
283
284 #if defined NORMAL_DBG
285         ir_fprintf(stderr, "sched walking block %+F\n", block);
286 #endif
287
288         if (roots == NULL) {
289 #if defined NORMAL_DBG
290                 fprintf(stderr, "has no roots\n");
291 #endif
292                 return;
293         }
294
295         root_count = ARR_LEN(roots);
296         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
297         for (i = 0; i < root_count; ++i) {
298                 root_costs[i].irn  = roots[i];
299                 root_costs[i].cost = normal_tree_cost(roots[i]);
300         }
301         qsort(root_costs, root_count, sizeof(*root_costs), cost_cmp);
302
303         sched = NEW_ARR_F(ir_node*, 0);
304         for (i = 0; i < root_count; ++i) {
305                 ir_node* irn = root_costs[i].irn;
306                 sched = sched_node(sched, irn);
307         }
308         set_irn_link(block, sched);
309         DEL_ARR_F(roots);
310
311 #if defined NORMAL_DBG
312         {
313                 int n = ARR_LEN(sched);
314                 int i;
315
316                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
317                 for (i = 0; i < n; ++i) {
318                         int j;
319                         for (j = 0; j < i; ++j) {
320                                 if (sched[i] == sched[j]) goto skip;
321                         }
322                         ir_fprintf(stderr, "  %+F\n", sched[i]);
323 skip:;
324                 }
325                 fprintf(stderr, "\n");
326         }
327 #endif
328 }
329
330
331 static void *normal_init_graph(const list_sched_selector_t *vtab,
332                                const be_irg_t *birg)
333 {
334         ir_graph* irg = be_get_birg_irg(birg);
335
336         (void)vtab;
337
338         cur_arch_env = be_get_birg_arch_env(birg);
339
340         be_clear_links(irg);
341
342         irg_walk_graph(irg, normal_cost_walker,  NULL, NULL);
343         irg_walk_graph(irg, collect_roots, NULL, NULL);
344         irg_block_walk_graph(irg, normal_sched_block, NULL, NULL);
345
346         return NULL;
347 }
348
349
350 static void *normal_init_block(void *graph_env, ir_node *block)
351 {
352         (void)graph_env;
353         (void)block;
354
355         return NULL;
356 }
357
358
359 static const list_sched_selector_t normal_selector_struct = {
360         normal_init_graph,
361         normal_init_block,
362         normal_select,
363         NULL,              /* to_appear_in_schedule */
364         NULL,              /* node_ready */
365         NULL,              /* node_selected */
366         NULL,              /* exectime */
367         NULL,              /* latency */
368         NULL,              /* finish_block */
369         NULL               /* finish_graph */
370 };
371
372 const list_sched_selector_t *normal_selector = &normal_selector_struct;