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