5d90ef087f8bca8505f795e9ec8850cbe83d68ef
[libfirm] / ir / be / beschednormal.c
1 /*
2  * Copyright (C) 1995-2008 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$
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 #include "benode_t.h"
38
39
40 // XXX there is no one time init for schedulers
41 //#define NORMAL_DBG
42
43
44 static const arch_env_t *cur_arch_env;
45
46
47 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
48                               ir_nodeset_t *live_set)
49 {
50         ir_nodeset_iterator_t iter;
51         ir_node*  block;
52         ir_node*  irn;
53         ir_node** sched;
54         int sched_count;
55
56         (void)block_env;
57         (void)live_set;
58
59         ir_nodeset_iterator_init(&iter, ready_set);
60         irn = ir_nodeset_iterator_next(&iter);
61         block = get_nodes_block(irn);
62         sched = get_irn_link(block);
63         sched_count = ARR_LEN(sched);
64         for (; sched_count-- != 0; ++sched) {
65                 ir_node* irn = *sched;
66                 if (ir_nodeset_contains(ready_set, irn) &&
67                                 !arch_irn_class_is(cur_arch_env, irn, branch)) {
68 #if defined NORMAL_DBG
69                         ir_fprintf(stderr, "scheduling %+F\n", irn);
70 #endif
71                         return irn;
72                 }
73         }
74
75         return irn;
76 }
77
78
79 typedef struct irn_cost_pair {
80         ir_node* irn;
81         int      cost;
82 } irn_cost_pair;
83
84
85 static int cost_cmp(const void* a, const void* b)
86 {
87         const irn_cost_pair* a1 = a;
88         const irn_cost_pair* b1 = b;
89         return b1->cost - a1->cost;
90 }
91
92
93 typedef struct flag_and_cost {
94         int no_root;
95         irn_cost_pair costs[];
96 } flag_and_cost;
97
98
99 static int count_result(const ir_node* irn)
100 {
101         const ir_mode* mode = get_irn_mode(irn);
102         return
103                 mode != mode_M &&
104                 mode != mode_X &&
105                 !arch_irn_is(cur_arch_env, irn, ignore);
106 }
107
108
109 /* TODO high cost for store trees
110  */
111
112
113 static int normal_tree_cost(ir_node* irn)
114 {
115         flag_and_cost* fc    = get_irn_link(irn);
116         ir_node*       block = get_nodes_block(irn);
117         int            arity = get_irn_arity(irn);
118         int            cost_max  = 0;
119         int            count_max = 0;
120         int            n_res;
121         int            cost;
122         int            n_op_res = 0;
123         int            i;
124
125         if (fc == NULL) {
126                 irn_cost_pair* costs;
127                 int            i;
128
129                 fc = malloc(sizeof(*fc) + sizeof(*fc->costs) * arity);
130                 fc->no_root = 0;
131                 costs = fc->costs;
132
133                 for (i = 0; i < arity; ++i) {
134                         ir_node* pred = get_irn_n(irn, i);
135                         int cost;
136
137                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
138                                 cost = 0;
139                         } else if (get_nodes_block(pred) != block) {
140                                 cost = 1;
141                         } else {
142                                 flag_and_cost* pred_fc;
143
144                                 cost = normal_tree_cost(pred);
145                                 if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
146                                 pred_fc = get_irn_link(pred);
147                                 pred_fc->no_root = 1;
148 #if defined NORMAL_DBG
149                                 ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, pred);
150 #endif
151                         }
152
153                         costs[i].irn  = pred;
154                         costs[i].cost = cost;
155
156                         if (cost > cost_max) {
157                                 cost_max  = cost;
158                                 count_max = 1;
159                         } else if (cost == cost_max) {
160                                 ++count_max;
161                         }
162                 }
163
164                 qsort(costs, arity, sizeof(*costs), cost_cmp);
165                 set_irn_link(irn, fc);
166         } else {
167                 irn_cost_pair* costs = fc->costs;
168                 int            i;
169
170                 if (arity > 0) {
171                         cost_max = costs[0].cost;
172
173                         for (i = 0; i < arity; ++i) {
174                                 if (costs[i].cost < cost_max) break;
175                                 ++count_max;
176                         }
177                 }
178         }
179
180         cost = 0;
181         for (i = 0; i < arity; ++i) {
182                 if (get_irn_mode(fc->costs[i].irn) == mode_M) continue;
183                 if (arch_irn_is(cur_arch_env, fc->costs[i].irn, ignore)) continue;
184                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
185                 ++n_op_res;
186         }
187         n_res = count_result(irn);
188         cost = MAX(n_res, cost);
189
190 #if defined NORMAL_DBG
191         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
192 #endif
193
194         return cost;
195 }
196
197
198 static void normal_cost_walker(ir_node* irn, void* env)
199 {
200         (void)env;
201
202 #if defined NORMAL_DBG
203         ir_fprintf(stderr, "cost walking node %+F\n", irn);
204 #endif
205         if (is_Block(irn)) return;
206         normal_tree_cost(irn);
207 }
208
209
210 static void collect_roots(ir_node* irn, void* env)
211 {
212         flag_and_cost* fc;
213
214         (void)env;
215
216         if (is_Block(irn)) return;
217
218         fc = get_irn_link(irn);
219
220 #if defined NORMAL_DBG
221         ir_fprintf(stderr, "%+F is %sroot\n", irn, fc->no_root ? "no " : "");
222 #endif
223
224         if (!fc->no_root) {
225                 ir_node* block = get_nodes_block(irn);
226                 ir_node** roots = get_irn_link(block);
227                 if (roots == NULL) {
228                         roots = NEW_ARR_F(ir_node*, 0);
229                 }
230                 ARR_APP1(ir_node*, roots, irn);
231                 set_irn_link(block, roots);
232         }
233 }
234
235
236 static ir_node** sched_node(ir_node** sched, ir_node* irn)
237 {
238         ir_node*       block = get_nodes_block(irn);
239         flag_and_cost* fc    = get_irn_link(irn);
240         irn_cost_pair* irns  = fc->costs;
241         int            arity = get_irn_arity(irn);
242         int            i;
243
244         if (irn_visited(irn)) return sched;
245
246         if (is_End(irn)) return sched;
247
248         if (!is_Phi(irn)) {
249                 for (i = 0; i < arity; ++i) {
250                         ir_node* pred = irns[i].irn;
251                         if (get_nodes_block(pred) != block) continue;
252                         if (get_irn_mode(pred) == mode_M) continue;
253                         sched = sched_node(sched, pred);
254                 }
255         }
256
257         mark_irn_visited(irn);
258         ARR_APP1(ir_node*, sched, irn);
259         return sched;
260 }
261
262
263 static void normal_sched_block(ir_node* block, void* env)
264 {
265         ir_node** roots = get_irn_link(block);
266         int            root_count;
267         irn_cost_pair* root_costs;
268         int i;
269         ir_node**      sched;
270
271         (void)env;
272
273 #if defined NORMAL_DBG
274         ir_fprintf(stderr, "sched walking block %+F\n", block);
275 #endif
276
277         if (roots == NULL) {
278 #if defined NORMAL_DBG
279                 fprintf(stderr, "has no roots\n");
280 #endif
281                 return;
282         }
283
284         root_count = ARR_LEN(roots);
285         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
286         for (i = 0; i < root_count; ++i) {
287                 root_costs[i].irn  = roots[i];
288                 root_costs[i].cost = normal_tree_cost(roots[i]);
289         }
290         qsort(root_costs, root_count, sizeof(*root_costs), cost_cmp);
291
292         sched = NEW_ARR_F(ir_node*, 0);
293         for (i = 0; i < root_count; ++i) {
294                 ir_node* irn = root_costs[i].irn;
295                 sched = sched_node(sched, irn);
296         }
297         set_irn_link(block, sched);
298         DEL_ARR_F(roots);
299
300 #if defined NORMAL_DBG
301         {
302                 int n = ARR_LEN(sched);
303                 int i;
304
305                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
306                 for (i = 0; i < n; ++i) {
307                         ir_fprintf(stderr, "  %+F\n", sched[i]);
308                 }
309                 fprintf(stderr, "\n");
310         }
311 #endif
312 }
313
314
315 static void *normal_init_graph(const list_sched_selector_t *vtab,
316                                const be_irg_t *birg)
317 {
318         ir_graph* irg = be_get_birg_irg(birg);
319
320         (void)vtab;
321
322         cur_arch_env = be_get_birg_arch_env(birg);
323
324         be_clear_links(irg);
325
326         irg_walk_graph(irg, normal_cost_walker,  NULL, NULL);
327         irg_walk_graph(irg, collect_roots, NULL, NULL);
328         inc_irg_visited(irg);
329         irg_block_walk_graph(irg, normal_sched_block, NULL, NULL);
330
331         return NULL;
332 }
333
334
335 static void *normal_init_block(void *graph_env, ir_node *block)
336 {
337         (void)graph_env;
338         (void)block;
339
340         return NULL;
341 }
342
343
344 const list_sched_selector_t normal_selector = {
345         normal_init_graph,
346         normal_init_block,
347         normal_select,
348         NULL,              /* to_appear_in_schedule */
349         NULL,              /* node_ready */
350         NULL,              /* node_selected */
351         NULL,              /* exectime */
352         NULL,              /* latency */
353         NULL,              /* finish_block */
354         NULL               /* finish_graph */
355 };