bf6d1b81ee56224bafd413fc96f480f0954820fa
[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 #include "config.h"
26
27 #include <stdlib.h>
28
29 #include "besched_t.h"
30 #include "belistsched.h"
31 #include "belive_t.h"
32 #include "beutil.h"
33 #include "height.h"
34 #include "irtools.h"
35 #include "irgwalk.h"
36 #include "benode_t.h"
37 #include "array_t.h"
38
39 // XXX there is no one time init for schedulers
40 //#define NORMAL_DBG
41 #include "irprintf.h"
42
43
44 static int must_be_scheduled(const ir_node* const irn)
45 {
46         return !is_Proj(irn) && !is_Sync(irn);
47 }
48
49
50 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
51                               ir_nodeset_t *live_set)
52 {
53         ir_nodeset_iterator_t iter;
54         ir_node*  block;
55         ir_node*  irn;
56         ir_node** sched;
57         int sched_count;
58
59         (void)block_env;
60         (void)live_set;
61
62         ir_nodeset_iterator_init(&iter, ready_set);
63         irn = ir_nodeset_iterator_next(&iter);
64         block = get_nodes_block(irn);
65         sched = get_irn_link(block);
66         sched_count = ARR_LEN(sched);
67         for (; sched_count-- != 0; ++sched) {
68                 ir_node* irn = *sched;
69                 if (ir_nodeset_contains(ready_set, irn) &&
70                                 !arch_irn_class_is(irn, branch)) {
71 #if defined NORMAL_DBG
72                         ir_fprintf(stderr, "scheduling %+F\n", irn);
73 #endif
74                         return irn;
75                 }
76         }
77
78         return irn;
79 }
80
81
82 typedef struct irn_cost_pair {
83         ir_node* irn;
84         int      cost;
85 } irn_cost_pair;
86
87 static int cost_cmp(const void* a, const void* b)
88 {
89         const irn_cost_pair* const a1 = a;
90         const irn_cost_pair* const b1 = b;
91         int ret = b1->cost - a1->cost;
92         if (ret == 0)
93                 ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
94 #if defined NORMAL_DBG
95         ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
96 #endif
97         return ret;
98 }
99
100
101 typedef struct flag_and_cost {
102         int no_root;
103         irn_cost_pair costs[];
104 } flag_and_cost;
105
106 #define get_irn_fc(irn)     ((flag_and_cost*)get_irn_link(irn))
107 #define set_irn_fc(irn, fc) set_irn_link(irn, fc)
108
109
110 static int count_result(const ir_node* irn)
111 {
112         const ir_mode* mode = get_irn_mode(irn);
113
114         if (mode == mode_M || mode == mode_X)
115                 return 0;
116
117         if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
118                 return 0;
119
120         return 1;
121 }
122
123
124 /* TODO high cost for store trees
125  */
126
127 static int normal_tree_cost(ir_node* irn)
128 {
129         flag_and_cost* fc;
130         int            arity;
131         ir_node*       last;
132         int            n_res;
133         int            cost;
134         int            n_op_res = 0;
135         int            i;
136
137         if (be_is_Keep(irn))
138                 return 0;
139
140         if (is_Proj(irn)) {
141                 return normal_tree_cost(get_Proj_pred(irn));
142         }
143
144         arity = get_irn_arity(irn);
145         fc    = get_irn_fc(irn);
146
147         if (fc == NULL) {
148                 irn_cost_pair* costs;
149                 int            i;
150                 ir_node*       block = get_nodes_block(irn);
151
152                 fc = xmalloc(sizeof(*fc) + sizeof(*fc->costs) * arity);
153                 fc->no_root = 0;
154                 costs = fc->costs;
155
156                 for (i = 0; i < arity; ++i) {
157                         ir_node* pred = get_irn_n(irn, i);
158                         int cost;
159
160                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
161                                 cost = 0;
162                         } else if (get_nodes_block(pred) != block) {
163                                 cost = 1;
164                         } else {
165                                 flag_and_cost* pred_fc;
166                                 ir_node*       real_pred;
167
168                                 cost = normal_tree_cost(pred);
169                                 if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
170                                 if (!arch_irn_is_ignore(pred)) {
171                                         real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
172                                         pred_fc = get_irn_fc(real_pred);
173                                         pred_fc->no_root = 1;
174 #if defined NORMAL_DBG
175                                         ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
176 #endif
177                                 }
178                         }
179
180                         costs[i].irn  = pred;
181                         costs[i].cost = cost;
182                 }
183
184                 qsort(costs, arity, sizeof(*costs), cost_cmp);
185                 set_irn_link(irn, fc);
186         }
187
188         cost = 0;
189         last = 0;
190         for (i = 0; i < arity; ++i) {
191                 ir_node* op = fc->costs[i].irn;
192                 if (op == last)                 continue;
193                 if (get_irn_mode(op) == mode_M) continue;
194                 if (arch_irn_is_ignore(op))     continue;
195                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
196                 last = op;
197                 ++n_op_res;
198         }
199         n_res = count_result(irn);
200         cost = MAX(n_res, cost);
201
202 #if defined NORMAL_DBG
203         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
204 #endif
205
206         return cost;
207 }
208
209
210 static void normal_cost_walker(ir_node* irn, void* env)
211 {
212         (void)env;
213
214 #if defined NORMAL_DBG
215         ir_fprintf(stderr, "cost walking node %+F\n", irn);
216 #endif
217         if (is_Block(irn)) return;
218         if (!must_be_scheduled(irn)) return;
219         normal_tree_cost(irn);
220 }
221
222
223 static void collect_roots(ir_node* irn, void* env)
224 {
225         int is_root;
226
227         (void)env;
228
229         if (is_Block(irn)) return;
230         if (!must_be_scheduled(irn)) return;
231
232         is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
233
234 #if defined NORMAL_DBG
235         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
236 #endif
237
238         if (is_root) {
239                 ir_node* block = get_nodes_block(irn);
240                 ir_node** roots = get_irn_link(block);
241                 if (roots == NULL) {
242                         roots = NEW_ARR_F(ir_node*, 0);
243                 }
244                 ARR_APP1(ir_node*, roots, irn);
245                 set_irn_link(block, roots);
246         }
247 }
248
249
250 static ir_node** sched_node(ir_node** sched, ir_node* irn)
251 {
252         if (irn_visited_else_mark(irn)) return sched;
253         if (is_End(irn))                return sched;
254
255         if (!is_Phi(irn) && !be_is_Keep(irn)) {
256                 ir_node*       block = get_nodes_block(irn);
257                 int            arity = get_irn_arity(irn);
258                 flag_and_cost* fc    = get_irn_fc(irn);
259                 irn_cost_pair* irns  = fc->costs;
260                 int            i;
261
262                 for (i = 0; i < arity; ++i) {
263                         ir_node* pred = irns[i].irn;
264                         if (get_nodes_block(pred) != block) continue;
265                         if (get_irn_mode(pred) == mode_M) continue;
266                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
267                         sched = sched_node(sched, pred);
268                 }
269         }
270
271         ARR_APP1(ir_node*, sched, irn);
272         return sched;
273 }
274
275
276 static int root_cmp(const void* a, const void* b)
277 {
278         const irn_cost_pair* const a1 = a;
279         const irn_cost_pair* const b1 = b;
280         int ret;
281         if (is_irn_forking(a1->irn)) {
282                 ret = 1;
283         } else if (is_irn_forking(b1->irn)) {
284                 ret = -1;
285         } else {
286                 ret = b1->cost - a1->cost;
287                 if (ret == 0) {
288                         /* place live-out nodes later */
289                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
290                 }
291         }
292 #if defined NORMAL_DBG
293         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
294 #endif
295         return ret;
296 }
297
298
299 static void normal_sched_block(ir_node* block, void* env)
300 {
301         heights_t*     heights = env;
302         ir_node**      roots   = get_irn_link(block);
303         int            root_count;
304         irn_cost_pair* root_costs;
305         int i;
306         ir_node**      sched;
307
308 #if defined NORMAL_DBG
309         ir_fprintf(stderr, "sched walking block %+F\n", block);
310 #endif
311
312         if (roots == NULL) {
313 #if defined NORMAL_DBG
314                 fprintf(stderr, "has no roots\n");
315 #endif
316                 return;
317         }
318
319         root_count = ARR_LEN(roots);
320         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
321         for (i = 0; i < root_count; ++i) {
322                 root_costs[i].irn  = roots[i];
323                 root_costs[i].cost = get_irn_height(heights, roots[i]);
324 #if defined NORMAL_DBG
325                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
326 #endif
327         }
328         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
329 #if defined NORMAL_DBG
330         {
331                 int n = root_count;
332                 int i;
333
334                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
335                 for (i = 0; i < n; ++i) {
336                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
337                 }
338                 fprintf(stderr, "\n");
339         }
340 #endif
341
342         sched = NEW_ARR_F(ir_node*, 0);
343         for (i = 0; i < root_count; ++i) {
344                 ir_node* irn = root_costs[i].irn;
345                 assert(must_be_scheduled(irn));
346                 sched = sched_node(sched, irn);
347         }
348         set_irn_link(block, sched);
349         DEL_ARR_F(roots);
350
351 #if defined NORMAL_DBG
352         {
353                 int n = ARR_LEN(sched);
354                 int i;
355
356                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
357                 for (i = 0; i < n; ++i) {
358                         ir_fprintf(stderr, "  %+F\n", sched[i]);
359                 }
360                 fprintf(stderr, "\n");
361         }
362 #endif
363 }
364
365
366 static void *normal_init_graph(const list_sched_selector_t *vtab,
367                                const be_irg_t *birg)
368 {
369         ir_graph  *irg = be_get_birg_irg(birg);
370         heights_t *heights;
371
372         (void)vtab;
373
374         be_clear_links(irg);
375
376         heights = heights_new(irg);
377
378         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
379         irg_walk_graph(irg, normal_cost_walker,  NULL, NULL);
380         irg_walk_graph(irg, collect_roots, NULL, NULL);
381         inc_irg_visited(irg);
382         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
383         irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
384         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
385
386         heights_free(heights);
387
388         return NULL;
389 }
390
391
392 static void *normal_init_block(void *graph_env, ir_node *block)
393 {
394         (void)graph_env;
395         (void)block;
396
397         return NULL;
398 }
399
400
401 const list_sched_selector_t normal_selector = {
402         normal_init_graph,
403         normal_init_block,
404         normal_select,
405         NULL,              /* to_appear_in_schedule */
406         NULL,              /* node_ready */
407         NULL,              /* node_selected */
408         NULL,              /* exectime */
409         NULL,              /* latency */
410         NULL,              /* finish_block */
411         NULL               /* finish_graph */
412 };