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