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