bffc8855ad5bab2fc37ce80523dc865a805abb47
[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 "array.h"
30 #include "debug.h"
31
32 #include "besched_t.h"
33 #include "beutil.h"
34 #include "belive_t.h"
35 #include "belistsched.h"
36 #include "beschedmris.h"
37 #include "bearch.h"
38 #include "bestat.h"
39
40 #define MAX(x,y) ((x) > (y) ? (x) : (y))
41 #define MIN(x,y) ((x) < (y) ? (x) : (y))
42
43 /**
44  * All scheduling info needed per node.
45  */
46 typedef struct _sched_irn_t {
47         sched_timestep_t delay;     /**< The delay for this node if already calculated, else 0. */
48         sched_timestep_t etime;     /**< The earliest time of this node. */
49         unsigned already_sched : 1; /**< Set if this node is already scheduled */
50         unsigned is_root       : 1; /**< is a root node of a block */
51 } sched_irn_t;
52
53 /**
54  * Scheduling environment for the whole graph.
55  */
56 typedef struct _sched_env_t {
57         sched_irn_t *sched_info;                    /**< scheduling info per node */
58         const list_sched_selector_t *selector;      /**< The node selector. */
59         const arch_env_t *arch_env;                 /**< The architecture environment. */
60         const ir_graph *irg;                        /**< The graph to schedule. */
61         void *selector_env;                         /**< A pointer to give to the selector. */
62 } sched_env_t;
63
64 #if 0
65 /*
66  * Ugly global variable for the compare function
67  * since qsort(3) does not pass an extra pointer.
68  */
69 static ir_node *curr_bl = NULL;
70
71 static int cmp_usage(const void *a, const void *b)
72 {
73         struct trivial_sched_env *env;
74         const ir_node *p = a;
75         const ir_node *q = b;
76         int res = 0;
77
78         res = is_live_end(env->curr_bl, a) - is_live_end(env->curr_bl, b);
79
80         /*
81          * One of them is live at the end of the block.
82          * Then, that one shall be scheduled at after the other
83          */
84         if(res != 0)
85                 return res;
86
87
88         return res;
89 }
90 #endif
91
92 /**
93  * The trivial selector:
94  * Just assure that branches are executed last, otherwise select
95  * the first node ready.
96  */
97 static ir_node *trivial_select(void *block_env, nodeset *ready_set)
98 {
99         const arch_env_t *arch_env = block_env;
100         ir_node *irn = NULL;
101         int const_last = 0;
102
103         /* assure that branches and constants are executed last */
104         for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) {
105                 arch_irn_class_t irn_class = arch_irn_classify(arch_env, irn);
106
107                 if (irn_class != arch_irn_class_branch && (const_last ? (irn_class != arch_irn_class_const) : 1)) {
108                         nodeset_break(ready_set);
109                         return irn;
110                 }
111         }
112
113         /* assure that constants are executed before branches */
114         if (const_last) {
115                 for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) {
116                         if (arch_irn_classify(arch_env, irn) != arch_irn_class_branch) {
117                                 nodeset_break(ready_set);
118                                 return irn;
119                         }
120                 }
121         }
122
123
124         /* at last: schedule branches */
125         irn = nodeset_first(ready_set);
126         nodeset_break(ready_set);
127
128         return irn;
129 }
130
131 static void *trivial_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
132 {
133         return (void *) arch_env;
134 }
135
136 static void *trivial_init_block(void *graph_env, ir_node *bl)
137 {
138         return graph_env;
139 }
140
141 static INLINE int must_appear_in_schedule(const list_sched_selector_t *sel, void *block_env, const ir_node *irn)
142 {
143         int res = 0;
144
145         if(sel->to_appear_in_schedule)
146                 res = sel->to_appear_in_schedule(block_env, irn);
147
148         return res || to_appear_in_schedule(irn) || be_is_Keep(irn) || be_is_RegParams(irn);
149 }
150
151 static const list_sched_selector_t trivial_selector_struct = {
152         trivial_init_graph,
153         trivial_init_block,
154         trivial_select,
155         NULL,                /* to_appear_in_schedule */
156         NULL,                /* exectime */
157         NULL,                /* latency */
158         NULL,                /* finish_block */
159         NULL                 /* finish_graph */
160 };
161
162 const list_sched_selector_t *trivial_selector = &trivial_selector_struct;
163
164 typedef struct _usage_stats_t {
165         ir_node *irn;
166         struct _usage_stats_t *next;
167         int max_hops;
168         int uses_in_block;      /**< Number of uses inside the current block. */
169         int already_consumed;   /**< Number of insns using this value already
170                                                           scheduled. */
171 } usage_stats_t;
172
173 typedef struct {
174         const list_sched_selector_t *vtab;
175         const arch_env_t *arch_env;
176 } reg_pressure_main_env_t;
177
178 typedef struct {
179         struct obstack obst;
180         const reg_pressure_main_env_t *main_env;
181         usage_stats_t *root;
182         nodeset *already_scheduled;
183 } reg_pressure_selector_env_t;
184
185 static INLINE usage_stats_t *get_or_set_usage_stats(reg_pressure_selector_env_t *env, ir_node *irn)
186 {
187         usage_stats_t *us = get_irn_link(irn);
188
189         if(!us) {
190                 us                   = obstack_alloc(&env->obst, sizeof(us[0]));
191                 us->irn              = irn;
192                 us->already_consumed = 0;
193                 us->max_hops         = INT_MAX;
194                 us->next             = env->root;
195                 env->root            = us;
196                 set_irn_link(irn, us);
197         }
198
199         return us;
200 }
201
202 static INLINE usage_stats_t *get_usage_stats(ir_node *irn)
203 {
204         usage_stats_t *us = get_irn_link(irn);
205         assert(us && "This node must have usage stats");
206         return us;
207 }
208
209 static int max_hops_walker(reg_pressure_selector_env_t *env, ir_node *irn, ir_node *curr_bl, int depth, unsigned visited_nr)
210 {
211         ir_node *bl = get_nodes_block(irn);
212         /*
213          * If the reached node is not in the block desired,
214          * return the value passed for this situation.
215          */
216         if(get_nodes_block(irn) != bl)
217                 return block_dominates(bl, curr_bl) ? 0 : INT_MAX;
218
219         /*
220          * If the node is in the current block but not
221          * yet scheduled, we keep on searching from that node.
222          */
223         if(!nodeset_find(env->already_scheduled, irn)) {
224                 int i, n;
225                 int res = 0;
226                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
227                         ir_node *operand = get_irn_n(irn, i);
228
229                         if(get_irn_visited(operand) < visited_nr) {
230                                 int tmp;
231
232                                 set_irn_visited(operand, visited_nr);
233                                 tmp = max_hops_walker(env, operand, bl, depth + 1, visited_nr);
234                                 res = MAX(tmp, res);
235                         }
236                 }
237
238                 return res;
239         }
240
241         /*
242          * If the node is in the current block and scheduled, return
243          * the depth which indicates the number of steps to the
244          * region of scheduled nodes.
245          */
246         return depth;
247 }
248
249 static int compute_max_hops(reg_pressure_selector_env_t *env, ir_node *irn)
250 {
251         ir_node *bl   = get_nodes_block(irn);
252         ir_graph *irg = get_irn_irg(bl);
253         int res       = 0;
254
255         const ir_edge_t *edge;
256
257         foreach_out_edge(irn, edge) {
258                 ir_node *user       = get_edge_src_irn(edge);
259                 unsigned visited_nr = get_irg_visited(irg) + 1;
260                 int max_hops;
261
262                 set_irg_visited(irg, visited_nr);
263                 max_hops = max_hops_walker(env, user, irn, 0, visited_nr);
264                 res      = MAX(res, max_hops);
265         }
266
267         return res;
268 }
269
270 static void *reg_pressure_graph_init(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
271 {
272         reg_pressure_main_env_t *main_env = xmalloc(sizeof(main_env[0]));
273
274         main_env->arch_env = arch_env;
275         main_env->vtab     = vtab;
276         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
277
278         return main_env;
279 }
280
281 static void *reg_pressure_block_init(void *graph_env, ir_node *bl)
282 {
283         ir_node *irn;
284         reg_pressure_selector_env_t *env  = xmalloc(sizeof(env[0]));
285
286         obstack_init(&env->obst);
287         env->already_scheduled = new_nodeset(32);
288         env->root              = NULL;
289         env->main_env          = graph_env;
290
291         /*
292          * Collect usage statistics.
293          */
294         sched_foreach(bl, irn) {
295                 if(must_appear_in_schedule(env->main_env->vtab, env, irn)) {
296                         int i, n;
297
298                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
299                                 ir_node *op = get_irn_n(irn, i);
300                                 if(must_appear_in_schedule(env->main_env->vtab, env, irn)) {
301                                         usage_stats_t *us = get_or_set_usage_stats(env, irn);
302                                         if(is_live_end(bl, op))
303                                                 us->uses_in_block = 99999;
304                                         else
305                                                 us->uses_in_block++;
306                                 }
307                         }
308                 }
309         }
310
311         return env;
312 }
313
314 static void reg_pressure_block_free(void *block_env)
315 {
316         reg_pressure_selector_env_t *env = block_env;
317         usage_stats_t *us;
318
319         for(us = env->root; us; us = us->next)
320                 set_irn_link(us->irn, NULL);
321
322         obstack_free(&env->obst, NULL);
323         del_nodeset(env->already_scheduled);
324         free(env);
325 }
326
327 static int get_result_hops_sum(reg_pressure_selector_env_t *env, ir_node *irn)
328 {
329         int res = 0;
330         if(get_irn_mode(irn) == mode_T) {
331                 const ir_edge_t *edge;
332
333                 foreach_out_edge(irn, edge)
334                         res += get_result_hops_sum(env, get_edge_src_irn(edge));
335         }
336
337         else if(mode_is_data(get_irn_mode(irn)))
338                 res = compute_max_hops(env, irn);
339
340
341         return res;
342 }
343
344 static INLINE int reg_pr_costs(reg_pressure_selector_env_t *env, ir_node *irn)
345 {
346         int i, n;
347         int sum = 0;
348
349         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
350                 ir_node *op = get_irn_n(irn, i);
351
352                 if(must_appear_in_schedule(env->main_env->vtab, env, op))
353                         sum += compute_max_hops(env, op);
354         }
355
356         sum += get_result_hops_sum(env, irn);
357
358         return sum;
359 }
360
361 static ir_node *reg_pressure_select(void *block_env, nodeset *ready_set)
362 {
363         reg_pressure_selector_env_t *env = block_env;
364         ir_node *irn, *res     = NULL;
365         int curr_cost          = INT_MAX;
366
367         assert(nodeset_count(ready_set) > 0);
368
369         for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) {
370                 /*
371                         Ignore branch instructions for the time being.
372                         They should only be scheduled if there is nothing else.
373                 */
374                 if (arch_irn_classify(env->main_env->arch_env, irn) != arch_irn_class_branch) {
375                         int costs = reg_pr_costs(env, irn);
376                         if (costs <= curr_cost) {
377                                 res       = irn;
378                                 curr_cost = costs;
379                         }
380                 }
381         }
382
383         /*
384                 There was no result so we only saw a branch.
385                 Take it and finish.
386         */
387
388         if(!res) {
389                 res = nodeset_first(ready_set);
390                 nodeset_break(ready_set);
391
392                 assert(res && "There must be a node scheduled.");
393         }
394
395         nodeset_insert(env->already_scheduled, res);
396         return res;
397 }
398
399 /**
400  * Environment for a block scheduler.
401  */
402 typedef struct _block_sched_env_t {
403         sched_irn_t *sched_info;                    /**< scheduling info per node, copied from the global scheduler object */
404         sched_timestep_t curr_time;                 /**< current time of the scheduler */
405         nodeset *cands;                             /**< the set of candidates */
406         ir_node *block;                             /**< the current block */
407         sched_env_t *sched_env;                     /**< the scheduler environment */
408         const list_sched_selector_t *selector;
409         void *selector_block_env;
410         DEBUG_ONLY(firm_dbg_module_t *dbg;)
411 } block_sched_env_t;
412
413 /**
414  * Returns non-zero if the node is already scheduled
415  */
416 static INLINE int is_already_scheduled(block_sched_env_t *env, ir_node *n)
417 {
418         int idx = get_irn_idx(n);
419
420         assert(idx < ARR_LEN(env->sched_info));
421         return env->sched_info[idx].already_sched;
422 }
423
424 /**
425  * Mark a node as already scheduled
426  */
427 static INLINE void mark_already_scheduled(block_sched_env_t *env, ir_node *n)
428 {
429         int idx = get_irn_idx(n);
430
431         assert(idx < ARR_LEN(env->sched_info));
432         env->sched_info[idx].already_sched = 1;
433 }
434
435 /**
436  * Returns non-zero if the node is a root node
437  */
438 static INLINE unsigned is_root_node(block_sched_env_t *env, ir_node *n)
439 {
440         int idx = get_irn_idx(n);
441
442         assert(idx < ARR_LEN(env->sched_info));
443         return env->sched_info[idx].is_root;
444 }
445
446 /**
447  * Mark a node as roto node
448  */
449 static INLINE void mark_root_node(block_sched_env_t *env, ir_node *n)
450 {
451         int idx = get_irn_idx(n);
452
453         assert(idx < ARR_LEN(env->sched_info));
454         env->sched_info[idx].is_root = 1;
455 }
456
457 /**
458  * Get the current delay.
459  */
460 static sched_timestep_t get_irn_delay(block_sched_env_t *env, ir_node *n) {
461         int idx = get_irn_idx(n);
462
463         assert(idx < ARR_LEN(env->sched_info));
464         return env->sched_info[idx].delay;
465 }
466
467 /**
468  * Set the current delay.
469  */
470 static void set_irn_delay(block_sched_env_t *env, ir_node *n, sched_timestep_t delay) {
471         int idx = get_irn_idx(n);
472
473         assert(idx < ARR_LEN(env->sched_info));
474         env->sched_info[idx].delay = delay;
475 }
476
477 /**
478  * Get the current etime.
479  */
480 static sched_timestep_t get_irn_etime(block_sched_env_t *env, ir_node *n) {
481         int idx = get_irn_idx(n);
482
483         assert(idx < ARR_LEN(env->sched_info));
484         return env->sched_info[idx].etime;
485 }
486
487 /**
488  * Set the current etime.
489  */
490 static void set_irn_etime(block_sched_env_t *env, ir_node *n, sched_timestep_t etime) {
491         int idx = get_irn_idx(n);
492
493         assert(idx < ARR_LEN(env->sched_info));
494         env->sched_info[idx].etime = etime;
495 }
496
497 /**
498  * returns the exec-time for node n.
499  */
500 static sched_timestep_t exectime(sched_env_t *env, ir_node *n) {
501   if (be_is_Keep(n) || is_Proj(n))
502     return 0;
503         if (env->selector->exectime)
504                 return env->selector->exectime(env->selector_env, n);
505         return 1;
506 }
507
508 /**
509  * Calculates the latency for between two ops
510  */
511 static sched_timestep_t latency(sched_env_t *env, ir_node *pred, int pred_cycle, ir_node *curr, int curr_cycle) {
512         /* a Keep hides a root */
513   if (be_is_Keep(curr))
514                 return exectime(env, pred);
515
516         /* Proj's are executed immediately */
517         if (is_Proj(curr))
518     return 0;
519
520         /* predecessors Proj's must be skipped */
521   if (is_Proj(pred))
522     pred = get_Proj_pred(pred);
523
524         if (env->selector->latency)
525                 return env->selector->latency(env->selector_env, pred, pred_cycle, curr, curr_cycle);
526         return 1;
527 }
528
529 /**
530  * Try to put a node in the ready set.
531  * @param env   The block scheduler environment.
532  * @param pred  The previous scheduled node.
533  * @param irn   The node to make ready.
534  * @return 1, if the node could be made ready, 0 else.
535  */
536 static INLINE int make_ready(block_sched_env_t *env, ir_node *pred, ir_node *irn)
537 {
538     int i, n;
539                 sched_timestep_t etime_p, etime;
540
541     /* Blocks cannot be scheduled. */
542     if (is_Block(irn))
543         return 0;
544
545     /*
546      * Check, if the given ir node is in a different block as the
547      * currently scheduled one. If that is so, don't make the node ready.
548      */
549     if (env->block != get_nodes_block(irn))
550         return 0;
551
552     for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
553         ir_node *op = get_irn_n(irn, i);
554
555         /* if irn is an End we have keep-alives and op might be a block, skip that */
556         if (is_Block(op)) {
557           assert(get_irn_op(irn) == op_End);
558           continue;
559         }
560
561         /* If the operand is local to the scheduled block and not yet
562          * scheduled, this nodes cannot be made ready, so exit. */
563         if (!is_already_scheduled(env, op) && get_nodes_block(op) == env->block)
564             return 0;
565     }
566
567     nodeset_insert(env->cands, irn);
568
569                 /* calculate the etime of this node */
570                 etime = env->curr_time;
571                 if (pred) {
572                         etime_p  = get_irn_etime(env, pred);
573                         etime   += latency(env->sched_env, pred, 1, irn, 0);
574
575                         etime = etime_p > etime ? etime_p : etime;
576                 }
577
578                 set_irn_etime(env, irn, etime);
579
580     DB((env->dbg, LEVEL_2, "\tmaking ready: %+F etime %u\n", irn, etime));
581
582     return 1;
583 }
584
585 /**
586  * Try, to make all users of a node ready.
587  * In fact, a usage node can only be made ready, if all its operands
588  * have already been scheduled yet. This is checked my make_ready().
589  * @param env The block schedule environment.
590  * @param irn The node, which usages (successors) are to be made ready.
591  */
592 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
593 {
594         const ir_edge_t *edge;
595
596         foreach_out_edge(irn, edge) {
597                 ir_node *user = edge->src;
598                 if(!is_Phi(user))
599                         make_ready(env, irn, user);
600         }
601 }
602
603 /**
604  * Compare to nodes using pointer equality.
605  * @param p1 Node one.
606  * @param p2 Node two.
607  * @return 0 if they are identical.
608  */
609 static int node_cmp_func(const void *p1, const void *p2)
610 {
611     return p1 != p2;
612 }
613
614 /**
615  * Append an instruction to a schedule.
616  * @param env The block scheduling environment.
617  * @param irn The node to add to the schedule.
618  * @return    The given node.
619  */
620 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
621 {
622     /* If the node consumes/produces data, it is appended to the schedule
623      * list, otherwise, it is not put into the list */
624     if(must_appear_in_schedule(env->selector, env->selector_block_env, irn)) {
625         sched_info_t *info = get_irn_sched_info(irn);
626         INIT_LIST_HEAD(&info->list);
627         info->scheduled = 1;
628         sched_add_before(env->block, irn);
629
630         DBG((env->dbg, LEVEL_2, "\tadding %+F\n", irn));
631     }
632
633     /* Insert the node in the set of all already scheduled nodes. */
634     mark_already_scheduled(env, irn);
635
636     /* Remove the node from the ready set */
637     if(nodeset_find(env->cands, irn))
638         nodeset_remove(env->cands, irn);
639
640     return irn;
641 }
642
643 /**
644  * Add the proj nodes of a tuple-mode irn to the schedule immediately
645  * after the tuple-moded irn. By pinning the projs after the irn, no
646  * other nodes can create a new lifetime between the tuple-moded irn and
647  * one of its projs. This should render a realistic image of a
648  * tuple-moded irn, which in fact models a node which defines multiple
649  * values.
650  *
651  * @param irn The tuple-moded irn.
652  */
653 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
654 {
655         const ir_edge_t *edge;
656
657         assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
658
659         foreach_out_edge(irn, edge) {
660                 ir_node *out = edge->src;
661
662                 assert(is_Proj(out) && "successor of a modeT node must be a proj");
663
664                 if (get_irn_mode(out) == mode_T)
665                         add_tuple_projs(env, out);
666                 else {
667                         add_to_sched(env, out);
668                         make_users_ready(env, out);
669                 }
670         }
671 }
672
673 /**
674  * Execute the heuristic function,
675  */
676 static ir_node *select_node_heuristic(block_sched_env_t *be, nodeset *ns)
677 {
678         ir_node *irn;
679
680         for (irn = nodeset_first(ns); irn; irn = nodeset_next(ns)) {
681                 if (be_is_Keep(irn)) {
682                         nodeset_break(ns);
683                         return irn;
684                 }
685         }
686
687         return be->selector->select(be->selector_block_env, ns);
688 }
689
690 /**
691  * Returns non-zero if root is a root in the block block.
692  */
693 static int is_root(ir_node *root, ir_node *block) {
694         const ir_edge_t *edge;
695
696         foreach_out_edge(root, edge) {
697                 ir_node *succ = get_edge_src_irn(edge);
698
699                 if (is_Block(succ))
700                         continue;
701                 if (get_nodes_block(succ) == block)
702                         return 0;
703         }
704         return 1;
705 }
706
707 /* we need a special mark */
708 static char _mark;
709 #define MARK    &_mark
710
711 /**
712  * descent into a dag and create a pre-order list.
713  */
714 static void descent(ir_node *root, ir_node *block, ir_node **list) {
715         int i;
716
717         if (! is_Phi(root)) {
718                 /* Phi nodes always leave the block */
719                 for (i = get_irn_arity(root) - 1; i >= 0; --i) {
720                         ir_node *pred = get_irn_n(root, i);
721
722                         /* Blocks may happen as predecessors of End nodes */
723                         if (is_Block(pred))
724                                 continue;
725
726                         /* already seen nodes are not marked */
727                         if (get_irn_link(pred) != MARK)
728                                 continue;
729
730                         /* don't leave our block */
731                         if (get_nodes_block(pred) != block)
732                                 continue;
733
734                         descent(pred, block, list);
735                 }
736         }
737         set_irn_link(root, *list);
738         *list = root;
739 }
740
741 /**
742  * Perform list scheduling on a block.
743  *
744  * Note, that the caller must compute a linked list of nodes in the block
745  * using the link field before calling this function.
746  *
747  * Also the outs must have been computed.
748  *
749  * @param block The block node.
750  * @param env Scheduling environment.
751  */
752 static void list_sched_block(ir_node *block, void *env_ptr)
753 {
754         sched_env_t *env                      = env_ptr;
755         const list_sched_selector_t *selector = env->selector;
756         ir_node *start_node                   = get_irg_start(get_irn_irg(block));
757         sched_info_t *info                    = get_irn_sched_info(block);
758
759         block_sched_env_t be;
760         const ir_edge_t *edge;
761         ir_node *irn;
762         int j, m;
763
764         ir_node *root = NULL, *preord = NULL;
765         ir_node *curr;
766
767         /* Initialize the block's list head that will hold the schedule. */
768         INIT_LIST_HEAD(&info->list);
769
770         /* Initialize the block scheduling environment */
771         be.sched_info        = env->sched_info;
772         be.block             = block;
773         be.curr_time         = 0;
774         be.cands             = new_nodeset(get_irn_n_edges(block));
775         be.selector          = selector;
776         be.sched_env         = env;
777         FIRM_DBG_REGISTER(be.dbg, "firm.be.sched");
778
779 //      firm_dbg_set_mask(be.dbg, SET_LEVEL_3);
780
781         if (selector->init_block)
782                 be.selector_block_env = selector->init_block(env->selector_env, block);
783
784         DBG((be.dbg, LEVEL_1, "scheduling %+F\n", block));
785
786         /* First step: Find the root set. */
787         foreach_out_edge(block, edge) {
788                 ir_node *succ = get_edge_src_irn(edge);
789
790                 if (is_root(succ, block)) {
791                         mark_root_node(&be, succ);
792                         set_irn_link(succ, root);
793                         root = succ;
794                 }
795                 else
796                         set_irn_link(succ, MARK);
797         }
798
799         /* Second step: calculate the pre-order list. */
800         preord = NULL;
801         for (curr = root; curr; curr = irn) {
802                 irn = get_irn_link(curr);
803                 descent(curr, block, &preord);
804         }
805         root = preord;
806
807         /* Third step: calculate the Delay. Note that our
808          * list is now in pre-order, starting at root
809          */
810         for (curr = root; curr; curr = get_irn_link(curr)) {
811                 sched_timestep_t d;
812
813                 if (arch_irn_classify(env->arch_env, curr) == arch_irn_class_branch) {
814                         /* assure, that branches can be executed last */
815                         d = 0;
816                 }
817                 else {
818                         if (is_root_node(&be, curr))
819                                 d = exectime(env, curr);
820                         else {
821                                 d = 0;
822                                 foreach_out_edge(curr, edge) {
823                                         ir_node *n = get_edge_src_irn(edge);
824
825                                         if (get_nodes_block(n) == block) {
826                                                 sched_timestep_t ld;
827
828                                                 ld = latency(env, curr, 1, n, 0) + get_irn_delay(&be, n);
829                                                 d = ld > d ? ld : d;
830                                         }
831                                 }
832                         }
833                 }
834                 set_irn_delay(&be, curr, d);
835                 DB((be.dbg, LEVEL_2, "\t%+F delay %u\n", curr, d));
836
837                 /* set the etime of all nodes to 0 */
838                 set_irn_etime(&be, curr, 0);
839         }
840
841
842         /* Then one can add all nodes are ready to the set. */
843         foreach_out_edge(block, edge) {
844                 ir_node *irn = get_edge_src_irn(edge);
845
846                 /* Skip the end node because of keepalive edges. */
847                 if (get_irn_opcode(irn) == iro_End)
848                         continue;
849
850                 if (is_Phi(irn)) {
851                         /* Phi functions are scheduled immediately, since they only transfer
852                          * data flow from the predecessors to this block. */
853
854                         /* Increase the time step. */
855                         be.curr_time += get_irn_etime(&be, irn);
856                         add_to_sched(&be, irn);
857                         make_users_ready(&be, irn);
858                 }
859                 else if (irn == start_node) {
860                         /* The start block will be scheduled as the first node */
861                         be.curr_time += get_irn_etime(&be, irn);
862
863                         add_to_sched(&be, irn);
864                         add_tuple_projs(&be, irn);
865                 }
866                 else {
867                         /* Other nodes must have all operands in other blocks to be made
868                          * ready */
869                         int ready = 1;
870
871                         /* Check, if the operands of a node are not local to this block */
872                         for (j = 0, m = get_irn_arity(irn); j < m; ++j) {
873                                 ir_node *operand = get_irn_n(irn, j);
874
875                                 if (get_nodes_block(operand) == block) {
876                                         ready = 0;
877                                         break;
878                                 }
879                         }
880
881                         /* Make the node ready, if all operands live in a foreign block */
882                         if (ready) {
883                                 DBG((be.dbg, LEVEL_2, "\timmediately ready: %+F\n", irn));
884                                 make_ready(&be, NULL, irn);
885                         }
886                 }
887         }
888
889         while (nodeset_count(be.cands) > 0) {
890                 nodeset *mcands;                            /**< the set of candidates with maximum delay time */
891                 nodeset *ecands;                            /**< the set of nodes in mcands whose etime <= curr_time  */
892                 sched_timestep_t max_delay = 0;
893
894                 /* collect statistics about amount of ready nodes */
895                 be_do_stat_sched_ready(block, be.cands);
896
897                 /* calculate the max delay of all candidates */
898                 foreach_nodeset(be.cands, irn) {
899                         sched_timestep_t d = get_irn_delay(&be, irn);
900
901                         max_delay = d > max_delay ? d : max_delay;
902                 }
903                 mcands = new_nodeset(8);
904                 ecands = new_nodeset(8);
905
906                 /* calculate mcands and ecands */
907                 foreach_nodeset(be.cands, irn) {
908       if (be_is_Keep(irn)) {
909         nodeset_break(be.cands);
910         break;
911       }
912                         if (get_irn_delay(&be, irn) == max_delay) {
913                                 nodeset_insert(mcands, irn);
914                                 if (get_irn_etime(&be, irn) <= be.curr_time)
915                                         nodeset_insert(ecands, irn);
916                         }
917                 }
918
919     if (irn) {
920       /* Keeps must be immediately scheduled */
921     }
922     else {
923                   DB((be.dbg, LEVEL_2, "\tbe.curr_time = %u\n", be.curr_time));
924
925                   /* select a node to be scheduled and check if it was ready */
926                   if (nodeset_count(mcands) == 1) {
927                           DB((be.dbg, LEVEL_3, "\tmcand = 1, max_delay = %u\n", max_delay));
928                           irn = nodeset_first(mcands);
929                   }
930                   else {
931                           int cnt = nodeset_count(ecands);
932                           if (cnt == 1) {
933                                         arch_irn_class_t irn_class;
934
935                                   irn = nodeset_first(ecands);
936                                         irn_class = arch_irn_classify(env->arch_env, irn);
937
938                                         if (irn_class == arch_irn_class_branch) {
939                                                 /* BEWARE: don't select a JUMP if others are still possible */
940                                                 goto force_mcands;
941                                         }
942                                   DB((be.dbg, LEVEL_3, "\tecand = 1, max_delay = %u\n", max_delay));
943                           }
944                           else if (cnt > 1) {
945                                   DB((be.dbg, LEVEL_3, "\tecand = %d, max_delay = %u\n", cnt, max_delay));
946                                   irn = select_node_heuristic(&be, ecands);
947                           }
948                           else {
949 force_mcands:
950                                   DB((be.dbg, LEVEL_3, "\tmcand = %d\n", nodeset_count(mcands)));
951                                   irn = select_node_heuristic(&be, mcands);
952                           }
953                   }
954     }
955                 del_nodeset(mcands);
956                 del_nodeset(ecands);
957
958                 DB((be.dbg, LEVEL_2, "\tpicked node %+F\n", irn));
959
960                 /* Increase the time step. */
961                 be.curr_time += exectime(env, irn);
962
963                 /* Add the node to the schedule. */
964                 add_to_sched(&be, irn);
965
966                 if (get_irn_mode(irn) == mode_T)
967                         add_tuple_projs(&be, irn);
968                 else
969                         make_users_ready(&be, irn);
970
971                 /* remove the scheduled node from the ready list. */
972                 if (nodeset_find(be.cands, irn))
973                         nodeset_remove(be.cands, irn);
974         }
975
976         if (selector->finish_block)
977                 selector->finish_block(be.selector_block_env);
978
979         del_nodeset(be.cands);
980 }
981
982 static const list_sched_selector_t reg_pressure_selector_struct = {
983         reg_pressure_graph_init,
984         reg_pressure_block_init,
985         reg_pressure_select,
986         NULL,                    /* to_appear_in_schedule */
987         NULL,                    /* exectime */
988         NULL,                    /* latency */
989         reg_pressure_block_free,
990         free
991 };
992
993 const list_sched_selector_t *reg_pressure_selector = &reg_pressure_selector_struct;
994
995 /* List schedule a graph. */
996 void list_sched(const be_irg_t *birg, int disable_mris)
997 {
998         const arch_env_t *arch_env = birg->main_env->arch_env;
999         ir_graph *irg              = birg->irg;
1000
1001         int num_nodes;
1002         sched_env_t env;
1003         mris_env_t *mris;
1004
1005         /* Assure, that the out edges are computed */
1006         edges_assure(irg);
1007
1008         if(!disable_mris)
1009                 mris = be_sched_mris_preprocess(birg);
1010
1011         num_nodes = get_irg_last_idx(irg);
1012
1013         memset(&env, 0, sizeof(env));
1014         env.selector   = arch_env->isa->impl->get_list_sched_selector(arch_env->isa);
1015         env.arch_env   = arch_env;
1016         env.irg        = irg;
1017         env.sched_info = NEW_ARR_F(sched_irn_t, num_nodes);
1018
1019         memset(env.sched_info, 0, num_nodes * sizeof(*env.sched_info));
1020
1021         if (env.selector->init_graph)
1022                 env.selector_env = env.selector->init_graph(env.selector, arch_env, irg);
1023
1024         /* Schedule each single block. */
1025         irg_block_walk_graph(irg, list_sched_block, NULL, &env);
1026
1027         if (env.selector->finish_graph)
1028                 env.selector->finish_graph(env.selector_env);
1029
1030         if(!disable_mris)
1031                 be_sched_mris_free(mris);
1032
1033         DEL_ARR_F(env.sched_info);
1034 }