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