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