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