Corrected setting register classes of Return and Barrier nodes
[libfirm] / ir / be / belistsched.c
1 /**
2  * Scheduling algorithms.
3  * Just a simple list scheduling algorithm is here.
4  * @date 20.10.2004
5  * @author Sebastian Hack
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <stdio.h>
13 #include <stdarg.h>
14 #include <string.h>
15 #include <limits.h>
16
17 #include "benode_t.h"
18
19 #include "obst.h"
20 #include "list.h"
21 #include "iterator.h"
22
23 #include "iredges_t.h"
24 #include "irgwalk.h"
25 #include "irnode_t.h"
26 #include "irmode_t.h"
27 #include "irdump.h"
28 #include "irprintf_t.h"
29 #include "debug.h"
30
31 #include "besched_t.h"
32 #include "beutil.h"
33 #include "belive_t.h"
34 #include "belistsched.h"
35 #include "bearch.h"
36
37 #define MAX(x,y) ((x) > (y) ? (x) : (y))
38 #define MIN(x,y) ((x) < (y) ? (x) : (y))
39
40 /**
41  * Scheduling environment for the whole graph.
42  */
43 typedef struct _sched_env_t {
44     const list_sched_selector_t *selector;      /**< The node selector. */
45         const arch_env_t *arch_env;                 /**< The architecture enviromnent. */
46         const ir_graph *irg;                        /**< The graph to schedule. */
47     void *selector_env;                         /**< A pointer to give to the selector. */
48 } sched_env_t;
49
50 #if 0
51 /*
52  * Ugly global variable for the compare function
53  * since qsort(3) does not pass an extra pointer.
54  */
55 static ir_node *curr_bl = NULL;
56
57 static int cmp_usage(const void *a, const void *b)
58 {
59         struct trivial_sched_env *env;
60         const ir_node *p = a;
61         const ir_node *q = b;
62         int res = 0;
63
64         res = is_live_end(env->curr_bl, a) - is_live_end(env->curr_bl, b);
65
66         /*
67          * One of them is live at the end of the block.
68          * Then, that one shall be scheduled at after the other
69          */
70         if(res != 0)
71                 return res;
72
73
74         return res;
75 }
76 #endif
77
78 static ir_node *trivial_select(void *block_env, pset *ready_set)
79 {
80         const arch_env_t *arch_env = block_env;
81         ir_node *irn = NULL;
82
83         for(irn = pset_first(ready_set); irn; irn = pset_next(ready_set)) {
84                 if(arch_irn_classify(arch_env, irn) != arch_irn_class_branch) {
85                         pset_break(ready_set);
86                         return irn;
87                 }
88         }
89
90         irn = pset_first(ready_set);
91         pset_break(ready_set);
92
93         return irn;
94 }
95
96 static void *trivial_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
97 {
98         return (void *) arch_env;
99 }
100
101 static void *trivial_init_block(void *graph_env, ir_node *bl)
102 {
103         return graph_env;
104 }
105
106 static INLINE int must_appear_in_schedule(const list_sched_selector_t *sel, void *block_env, const ir_node *irn)
107 {
108         int res = 0;
109
110         if(sel->to_appear_in_schedule)
111                 res = sel->to_appear_in_schedule(block_env, irn);
112
113         return res || to_appear_in_schedule(irn) || be_is_Keep(irn) || be_is_RegParams(irn);
114 }
115
116 static const list_sched_selector_t trivial_selector_struct = {
117         trivial_init_graph,
118         trivial_init_block,
119         trivial_select,
120         NULL,
121         NULL,
122         NULL
123 };
124
125 const list_sched_selector_t *trivial_selector = &trivial_selector_struct;
126
127 typedef struct _usage_stats_t {
128         ir_node *irn;
129         struct _usage_stats_t *next;
130         int max_hops;
131         int uses_in_block;      /**< Number of uses inside the current block. */
132         int already_consumed;   /**< Number of insns using this value already
133                                                           scheduled. */
134 } usage_stats_t;
135
136 typedef struct {
137         const list_sched_selector_t *vtab;
138         const arch_env_t *arch_env;
139 } reg_pressure_main_env_t;
140
141 typedef struct {
142         struct obstack obst;
143         const reg_pressure_main_env_t *main_env;
144         usage_stats_t *root;
145         pset *already_scheduled;
146 } reg_pressure_selector_env_t;
147
148 static INLINE usage_stats_t *get_or_set_usage_stats(reg_pressure_selector_env_t *env, ir_node *irn)
149 {
150         usage_stats_t *us = get_irn_link(irn);
151
152         if(!us) {
153                 us                   = obstack_alloc(&env->obst, sizeof(us[0]));
154                 us->irn              = irn;
155                 us->already_consumed = 0;
156                 us->max_hops         = INT_MAX;
157                 us->next             = env->root;
158                 env->root            = us;
159                 set_irn_link(irn, us);
160         }
161
162         return us;
163 }
164
165 static INLINE usage_stats_t *get_usage_stats(ir_node *irn)
166 {
167         usage_stats_t *us = get_irn_link(irn);
168         assert(us && "This node must have usage stats");
169         return us;
170 }
171
172 static int max_hops_walker(reg_pressure_selector_env_t *env, ir_node *irn, ir_node *curr_bl, int depth, unsigned visited_nr)
173 {
174         ir_node *bl = get_nodes_block(irn);
175         /*
176          * If the reached node is not in the block desired,
177          * return the value passed for this situation.
178          */
179         if(get_nodes_block(irn) != bl)
180                 return block_dominates(bl, curr_bl) ? 0 : INT_MAX;
181
182         /*
183          * If the node is in the current block but not
184          * yet scheduled, we keep on searching from that node.
185          */
186         if(!pset_find_ptr(env->already_scheduled, irn)) {
187                 int i, n;
188                 int res = 0;
189                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
190                         ir_node *operand = get_irn_n(irn, i);
191
192                         if(get_irn_visited(operand) < visited_nr) {
193                                 int tmp;
194
195                                 set_irn_visited(operand, visited_nr);
196                                 tmp = max_hops_walker(env, operand, bl, depth + 1, visited_nr);
197                                 res = MAX(tmp, res);
198                         }
199                 }
200
201                 return res;
202         }
203
204         /*
205          * If the node is in the current block and scheduled, return
206          * the depth which indicates the number of steps to the
207          * region of scheduled nodes.
208          */
209         return depth;
210 }
211
212 static int compute_max_hops(reg_pressure_selector_env_t *env, ir_node *irn)
213 {
214         ir_node *bl   = get_nodes_block(irn);
215         ir_graph *irg = get_irn_irg(bl);
216         int res       = 0;
217
218         const ir_edge_t *edge;
219
220         foreach_out_edge(irn, edge) {
221                 ir_node *user       = get_edge_src_irn(edge);
222                 unsigned visited_nr = get_irg_visited(irg) + 1;
223                 int max_hops;
224
225                 set_irg_visited(irg, visited_nr);
226                 max_hops = max_hops_walker(env, user, irn, 0, visited_nr);
227                 res      = MAX(res, max_hops);
228         }
229
230         return res;
231 }
232
233 static void *reg_pressure_graph_init(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
234 {
235         reg_pressure_main_env_t *main_env = xmalloc(sizeof(main_env[0]));
236
237         main_env->arch_env = arch_env;
238         main_env->vtab     = vtab;
239         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
240
241         return main_env;
242 }
243
244 static void *reg_pressure_block_init(void *graph_env, ir_node *bl)
245 {
246         ir_node *irn;
247         reg_pressure_selector_env_t *env  = xmalloc(sizeof(env[0]));
248
249         obstack_init(&env->obst);
250         env->already_scheduled = pset_new_ptr(32);
251         env->root              = NULL;
252         env->main_env          = graph_env;
253
254         /*
255          * Collect usage statistics.
256          */
257         sched_foreach(bl, irn) {
258                 if(must_appear_in_schedule(env->main_env->vtab, env, irn)) {
259                         int i, n;
260
261                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
262                                 ir_node *op = get_irn_n(irn, i);
263                                 if(must_appear_in_schedule(env->main_env->vtab, env, irn)) {
264                                         usage_stats_t *us = get_or_set_usage_stats(env, irn);
265                                         if(is_live_end(bl, op))
266                                                 us->uses_in_block = 99999;
267                                         else
268                                                 us->uses_in_block++;
269                                 }
270                         }
271                 }
272         }
273
274         return env;
275 }
276
277 static void reg_pressure_block_free(void *block_env)
278 {
279         reg_pressure_selector_env_t *env = block_env;
280         usage_stats_t *us;
281
282         for(us = env->root; us; us = us->next)
283                 set_irn_link(us->irn, NULL);
284
285         obstack_free(&env->obst, NULL);
286         del_pset(env->already_scheduled);
287         free(env);
288 }
289
290 static int get_result_hops_sum(reg_pressure_selector_env_t *env, ir_node *irn)
291 {
292         int res = 0;
293         if(get_irn_mode(irn) == mode_T) {
294                 const ir_edge_t *edge;
295
296                 foreach_out_edge(irn, edge)
297                         res += get_result_hops_sum(env, get_edge_src_irn(edge));
298         }
299
300         else if(mode_is_data(get_irn_mode(irn)))
301                 res = compute_max_hops(env, irn);
302
303
304         return res;
305 }
306
307 static INLINE int reg_pr_costs(reg_pressure_selector_env_t *env, ir_node *irn)
308 {
309         int i, n;
310         int sum = 0;
311
312         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
313                 ir_node *op = get_irn_n(irn, i);
314
315                 if(must_appear_in_schedule(env->main_env->vtab, env, op))
316                         sum += compute_max_hops(env, op);
317         }
318
319         sum += get_result_hops_sum(env, irn);
320
321         return sum;
322 }
323
324 static ir_node *reg_pressure_select(void *block_env, pset *ready_set)
325 {
326         reg_pressure_selector_env_t *env = block_env;
327         ir_node *irn, *res     = NULL;
328         int curr_cost          = INT_MAX;
329
330         assert(pset_count(ready_set) > 0);
331
332         for(irn = pset_first(ready_set); irn; irn = pset_next(ready_set)) {
333                 /*
334                         Ignore branch instructions for the time being.
335                         They should only be scheduled if there is nothing else.
336                 */
337                 if(arch_irn_classify(env->main_env->arch_env, irn) != arch_irn_class_branch) {
338                         int costs = reg_pr_costs(env, irn);
339                         if(costs <= curr_cost) {
340                                 res       = irn;
341                                 curr_cost = costs;
342                         }
343                 }
344         }
345
346         /*
347                 There was no result so we only saw a branch.
348                 Take it and finish.
349         */
350
351         if(!res) {
352                 res = pset_first(ready_set);
353                 pset_break(ready_set);
354
355                 assert(res && "There must be a node scheduled.");
356         }
357
358         pset_insert_ptr(env->already_scheduled, res);
359         return res;
360 }
361
362 static const list_sched_selector_t reg_pressure_selector_struct = {
363         reg_pressure_graph_init,
364         reg_pressure_block_init,
365         reg_pressure_select,
366         NULL,
367         reg_pressure_block_free,
368         free
369 };
370
371 const list_sched_selector_t *reg_pressure_selector = &reg_pressure_selector_struct;
372
373 static void list_sched_block(ir_node *block, void *env_ptr);
374
375 void list_sched(const arch_env_t *arch_env, ir_graph *irg)
376 {
377         sched_env_t env;
378
379         memset(&env, 0, sizeof(env));
380         env.selector = arch_env->isa->impl->get_list_sched_selector(arch_env->isa);
381         env.arch_env = arch_env;
382         env.irg      = irg;
383
384         if(env.selector->init_graph)
385                 env.selector_env = env.selector->init_graph(env.selector, arch_env, irg);
386
387         /* Assure, that the out edges are computed */
388         edges_assure(irg);
389
390         /* Schedule each single block. */
391         irg_block_walk_graph(irg, list_sched_block, NULL, &env);
392
393         if(env.selector->finish_graph)
394                 env.selector->finish_graph(env.selector_env);
395 }
396
397
398 /**
399  * Environment for a block scheduler.
400  */
401 typedef struct _block_sched_env_t {
402         int curr_time;
403         pset *ready_set;
404         pset *already_scheduled;
405         ir_node *block;
406         firm_dbg_module_t *dbg;
407         const list_sched_selector_t *selector;
408         void *selector_block_env;
409 } block_sched_env_t;
410
411 /**
412  * Try to put a node in the ready set.
413  * @param env The block scheduler environment.
414  * @param irn The node to make ready.
415  * @return 1, if the node could be made ready, 0 else.
416  */
417 static INLINE int make_ready(block_sched_env_t *env, ir_node *irn)
418 {
419     int i, n;
420
421     /* Blocks cannot be scheduled. */
422     if(is_Block(irn))
423         return 0;
424
425     /*
426      * Check, if the given ir node is in a different block as the
427      * currently scheduled one. If that is so, don't make the node ready.
428      */
429     if(env->block != get_nodes_block(irn))
430         return 0;
431
432     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
433         ir_node *op = get_irn_n(irn, i);
434
435         /* If the operand is local to the scheduled block and not yet
436          * scheduled, this nodes cannot be made ready, so exit. */
437         if(!pset_find_ptr(env->already_scheduled, op) && get_nodes_block(op) == env->block)
438             return 0;
439     }
440
441     DBG((env->dbg, LEVEL_2, "\tmaking ready: %+F\n", irn));
442     pset_insert_ptr(env->ready_set, irn);
443
444     return 1;
445 }
446
447 /**
448  * Check, if a node is ready in a block schedule.
449  * @param env The block schedule environment.
450  * @param irn The node to check for.
451  * @return 1 if the node was ready, 0 if not.
452  */
453 #define is_ready(env,irn) \
454   (pset_find_ptr((env)->ready_set, irn) != NULL)
455
456 /**
457  * Check, if a node has already been schedules.
458  * @param env The block schedule environment.
459  * @param irn The node to check for.
460  * @return 1 if the node was already scheduled, 0 if not.
461  */
462 #define is_scheduled(env,irn) \
463   (pset_find_ptr((env)->already_scheduled, irn) != NULL)
464
465 /**
466  * Try, to make all users of a node ready.
467  * In fact, a usage node can only be made ready, if all its operands
468  * have already been scheduled yet. This is checked my make_ready().
469  * @param env The block schedule environment.
470  * @param irn The node, which usages (successors) are to be made ready.
471  */
472 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
473 {
474         const ir_edge_t *edge;
475
476         foreach_out_edge(irn, edge) {
477                 ir_node *user = edge->src;
478                 if(!is_Phi(user))
479                         make_ready(env, user);
480         }
481 }
482
483 /**
484  * Compare to nodes using pointer equality.
485  * @param p1 Node one.
486  * @param p2 Node two.
487  * @return 0 if they are identical.
488  */
489 static int node_cmp_func(const void *p1, const void *p2)
490 {
491     return p1 != p2;
492 }
493
494 /**
495  * Append an instruction to a schedule.
496  * @param env The block scheduling environment.
497  * @param irn The node to add to the schedule.
498  * @return    The given node.
499  */
500 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
501 {
502     /* If the node consumes/produces data, it is appended to the schedule
503      * list, otherwise, it is not put into the list */
504     if(must_appear_in_schedule(env->selector, env->selector_block_env, irn)) {
505         sched_info_t *info = get_irn_sched_info(irn);
506         INIT_LIST_HEAD(&info->list);
507         info->scheduled = 1;
508         sched_add_before(env->block, irn);
509
510         DBG((env->dbg, LEVEL_2, "\tadding %+F\n", irn));
511     }
512
513     /* Insert the node in the set of all already scheduled nodes. */
514     pset_insert_ptr(env->already_scheduled, irn);
515
516     /* Remove the node from the ready set */
517     if(pset_find_ptr(env->ready_set, irn))
518         pset_remove_ptr(env->ready_set, irn);
519
520     return irn;
521 }
522
523 /**
524  * Add the proj nodes of a tuple-mode irn to the schedule immediately
525  * after the tuple-moded irn. By pinning the projs after the irn, no
526  * other nodes can create a new lifetime between the tuple-moded irn and
527  * one of its projs. This should render a realistic image of a
528  * tuple-moded irn, which in fact models a node which defines multiple
529  * values.
530  *
531  * @param irn The tuple-moded irn.
532  * @param list The schedule list to append all the projs.
533  * @param time The time step to which the irn and all its projs are
534  * related to.
535  * @param obst The obstack the scheduling data structures shall be
536  * created upon.
537  * @param ready_set The ready set of the list scheduler.
538  * @param already_scheduled A set containing all nodes already
539  * scheduled.
540  */
541 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
542 {
543         const ir_edge_t *edge;
544
545         assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
546
547         foreach_out_edge(irn, edge) {
548                 ir_node *out = edge->src;
549
550                 assert(is_Proj(out) && "successor of a modeT node must be a proj");
551
552                 if(get_irn_mode(out) == mode_T)
553                         add_tuple_projs(env, out);
554                 else {
555                         add_to_sched(env, out);
556                         make_users_ready(env, out);
557                 }
558         }
559 }
560
561 static ir_node *select_node(block_sched_env_t *be)
562 {
563         ir_node *irn;
564
565         for(irn = pset_first(be->ready_set); irn; irn = pset_next(be->ready_set)) {
566                 if(be_is_Keep(irn)) {
567                         pset_break(be->ready_set);
568                         return irn;
569                 }
570         }
571
572         return be->selector->select(be->selector_block_env, be->ready_set);
573 }
574
575 /**
576  * Perform list scheduling on a block.
577  *
578  * Note, that the caller must compute a linked list of nodes in the block
579  * using the link field before calling this function.
580  *
581  * Also the outs must have been computed.
582  *
583  * @param block The block node.
584  * @param env Scheduling environment.
585  */
586 static void list_sched_block(ir_node *block, void *env_ptr)
587 {
588         sched_env_t *env                      = env_ptr;
589         const list_sched_selector_t *selector = env->selector;
590         ir_node *start_node                   = get_irg_start(get_irn_irg(block));
591         int phi_seen                          = 0;
592         sched_info_t *info                    = get_irn_sched_info(block);
593
594         block_sched_env_t be;
595         const ir_edge_t *edge;
596         ir_node *irn;
597         int j, m;
598
599         /* Initialize the block's list head that will hold the schedule. */
600         INIT_LIST_HEAD(&info->list);
601
602         /* Initialize the block scheduling environment */
603         be.dbg               = firm_dbg_register("firm.be.sched");
604         be.block             = block;
605         be.curr_time         = 0;
606         be.ready_set         = new_pset(node_cmp_func, get_irn_n_edges(block));
607         be.already_scheduled = new_pset(node_cmp_func, get_irn_n_edges(block));
608         be.selector          = selector;
609
610         if(selector->init_block)
611                 be.selector_block_env = selector->init_block(env->selector_env, block);
612
613         DBG((be.dbg, LEVEL_1, "scheduling %+F\n", block));
614
615         /* Then one can add all nodes are ready to the set. */
616         foreach_out_edge(block, edge) {
617                 ir_node *irn = get_edge_src_irn(edge);
618
619                 /* Skip the end node because of keepalive edges. */
620                 if(get_irn_opcode(irn) == iro_End)
621                         continue;
622
623                 /* Phi functions are scheduled immediately, since they only transfer
624                  * data flow from the predecessors to this block. */
625                 if(is_Phi(irn)) {
626                         add_to_sched(&be, irn);
627                         make_users_ready(&be, irn);
628                         phi_seen = 1;
629                 }
630
631                 /* The start block will be scheduled as the first node */
632                 else if(irn == start_node) {
633                         add_to_sched(&be, irn);
634                         add_tuple_projs(&be, irn);
635                 }
636
637
638                 /* Other nodes must have all operands in other blocks to be made
639                  * ready */
640                 else {
641                         int ready = 1;
642
643                         /* Check, if the operands of a node are not local to this block */
644                         for(j = 0, m = get_irn_arity(irn); j < m; ++j) {
645                                 ir_node *operand = get_irn_n(irn, j);
646
647                                 if(get_nodes_block(operand) == block) {
648                                         ready = 0;
649                                         break;
650                                 }
651                         }
652
653                         /* Make the node ready, if all operands live in a foreign block */
654                         if(ready) {
655                                 DBG((be.dbg, LEVEL_2, "\timmediately ready: %+F\n", irn));
656                                 make_ready(&be, irn);
657                         }
658                 }
659         }
660
661         /* Increase the time, if some phi functions have been scheduled */
662         be.curr_time += phi_seen;
663
664         while(pset_count(be.ready_set) > 0) {
665                 /* select a node to be scheduled and check if it was ready */
666                 irn = select_node(&be);
667
668                 DBG((be.dbg, LEVEL_3, "\tpicked node %+F\n", irn));
669
670                 /* Add the node to the schedule. */
671                 add_to_sched(&be, irn);
672
673                 if(get_irn_mode(irn) == mode_T)
674                         add_tuple_projs(&be, irn);
675                 else
676                         make_users_ready(&be, irn);
677
678                 /* Increase the time step. */
679                 be.curr_time += 1;
680
681                 /* remove the scheduled node from the ready list. */
682                 if(pset_find_ptr(be.ready_set, irn))
683                         pset_remove_ptr(be.ready_set, irn);
684         }
685
686         if(selector->finish_block)
687                 selector->finish_block(be.selector_block_env);
688
689         del_pset(be.ready_set);
690         del_pset(be.already_scheduled);
691 }