added Min, Max, Div, DivMod and Mulh nodes
[libfirm] / ir / be / bechordal.c
1 /**
2  * Chordal register allocation.
3  * @author Sebastian Hack
4  * @date 8.12.2004
5  *
6  * Copyright (C) Universitaet Karlsruhe
7  * Released under the GPL
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <ctype.h>
15
16 #include "obst.h"
17 #include "pset.h"
18 #include "list.h"
19 #include "bitset.h"
20 #include "iterator.h"
21
22 #include "irmode_t.h"
23 #include "irgraph_t.h"
24 #include "irprintf_t.h"
25 #include "irgwalk.h"
26 #include "irdump.h"
27 #include "irdom.h"
28 #include "debug.h"
29 #include "xmalloc.h"
30
31 #include "beutil.h"
32 #include "besched.h"
33 #include "benumb_t.h"
34 #include "besched_t.h"
35 #include "belive_t.h"
36 #include "bearch.h"
37
38 #include "bechordal_t.h"
39 #include "bechordal_draw.h"
40
41 #define DBG_LEVEL SET_LEVEL_0
42 #define DBG_LEVEL_CHECK SET_LEVEL_0
43
44 #define NO_COLOR (-1)
45
46 #undef DUMP_INTERVALS
47 #undef DUMP_PRESSURE
48 #undef DUMP_IFG
49
50 #if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
51 #error Must define BUILD_GRAPH to be able to dump it.
52 #endif
53
54
55 #include "fourcc.h"
56
57 /* Make a fourcc for border checking. */
58 #define BORDER_FOURCC                           FOURCC('B', 'O', 'R', 'D')
59
60 static firm_dbg_module_t *dbg;
61
62 #ifdef BUILD_GRAPH
63
64 #define IF_EDGE_HASH(e) ((e)->src ^ (e)->tgt)
65 #define IF_NODE_HASH(n) ((n)->nnr)
66
67 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
68 {
69         const if_edge_t *e1 = p1;
70         const if_edge_t *e2 = p2;
71
72         return !(e1->src == e2->src && e1->tgt == e2->tgt);
73 }
74
75 static int if_node_cmp(const void *p1, const void *p2, size_t size)
76 {
77         const if_node_t *n1 = p1;
78         const if_node_t *n2 = p2;
79
80         return n1->nnr != n2->nnr;
81 }
82
83 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
84 {
85         /* Bring the smaller entry to src. */
86         if(src > tgt) {
87                 edge->src = tgt;
88                 edge->tgt = src;
89         } else {
90                 edge->src = src;
91                 edge->tgt = tgt;
92         }
93
94         return edge;
95 }
96
97 static INLINE void add_if(const be_chordal_env_t *env, int src, int tgt)
98 {
99         if_edge_t edge;
100         if_node_t node, *src_node, *tgt_node;
101         /* insert edge */
102         edge_init(&edge, src, tgt);
103         set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
104
105         /* insert nodes */
106         node.nnr = src;
107         node.neighb = pset_new_ptr(8);
108         src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
109         node.nnr = tgt;
110         node.neighb = pset_new_ptr(8);
111         tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
112
113         /* insert neighbors into nodes */
114         pset_insert_ptr(src_node->neighb, tgt_node);
115         pset_insert_ptr(tgt_node->neighb, src_node);
116 }
117
118 static INLINE int are_connected(const be_chordal_env_t *env, int src, int tgt)
119 {
120         if_edge_t edge;
121         edge_init(&edge, src, tgt);
122         return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
123 }
124
125 int ifg_has_edge(const be_chordal_env_t *env, const if_node_t *n1, const if_node_t* n2) {
126         return are_connected(env, n1->nnr, n2->nnr);
127 }
128
129 #ifdef DUMP_IFG
130
131 static void dump_ifg(const be_chordal_env_t *env)
132 {
133         FILE *f;
134         set *edges = env->edges;
135         ir_graph *irg = env->irg;
136         char filename[128];
137
138         ir_snprintf(filename, sizeof(filename), "ifg_%s_%F.dot", env->cls->name, irg);
139
140         if((f = fopen(filename, "wt")) != NULL) {
141                 bitset_pos_t pos;
142                 int n_edges = 0;
143                 if_edge_t *edge;
144                 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
145
146                 ir_fprintf(f, "graph \"%F\" {\n", irg);
147                 fprintf(f, "\tnode [shape=box,style=filled]\n");
148
149                 for(edge = set_first(edges); edge; edge = set_next(edges)) {
150                         bitset_set(bs, edge->src);
151                         bitset_set(bs, edge->tgt);
152                         n_edges++;
153                 }
154
155                 fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
156
157                 bitset_foreach(bs, pos) {
158                         int nr = (int) pos;
159                         ir_node *irn = get_irn_for_graph_nr(irg, nr);
160
161                         ir_fprintf(f, "\tn%d [label=\"%+F\"]\n", nr, irn);
162                 }
163
164                 for(edge = set_first(edges); edge; edge = set_next(edges)) {
165                         fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
166                 }
167
168                 fprintf(f, "}\n");
169                 fclose(f);
170
171                 bitset_free(bs);
172         }
173
174 }
175
176 #endif /* DUMP_IFG */
177
178 #endif /* BUILD_GRAPH */
179
180 static void check_border_list(struct list_head *head)
181 {
182   border_t *x;
183   list_for_each_entry(border_t, x, head, list) {
184     assert(x->magic == BORDER_FOURCC);
185   }
186 }
187
188 static void check_heads(be_chordal_env_t *env)
189 {
190   pmap_entry *ent;
191   for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
192     /* ir_printf("checking border list of block %+F\n", ent->key); */
193     check_border_list(ent->value);
194   }
195 }
196
197
198 /**
199  * Add an interval border to the list of a block's list
200  * of interval border.
201  * @note You always have to create the use before the def.
202  * @param env The environment.
203  * @param head The list head to enqueue the borders.
204  * @param irn The node (value) the border belongs to.
205  * @param pressure The pressure at this point in time.
206  * @param step A time step for the border.
207  * @param is_def Is the border a use or a def.
208  * @return The created border.
209  */
210 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
211                         ir_node *irn, unsigned step, unsigned pressure,
212                         unsigned is_def, unsigned is_real)
213 {
214         border_t *b;
215
216         if(!is_def) {
217                 border_t *def;
218
219                 b = obstack_alloc(&env->obst, sizeof(*b));
220
221                 /* also allocate the def and tie it to the use. */
222                 def = obstack_alloc(&env->obst, sizeof(*def));
223     memset(def, 0, sizeof(*def));
224                 b->other_end = def;
225                 def->other_end = b;
226
227                 /*
228                  * Set the link field of the irn to the def.
229                  * This strongly relies on the fact, that the use is always
230                  * made before the def.
231                  */
232                 set_irn_link(irn, def);
233
234                 b->magic = BORDER_FOURCC;
235                 def->magic = BORDER_FOURCC;
236         }
237
238         /*
239          * If the def is encountered, the use was made and so was the
240          * the def node (see the code above). It was placed into the
241          * link field of the irn, so we can get it there.
242          */
243         else {
244                 b = get_irn_link(irn);
245
246                 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
247         }
248
249         b->pressure = pressure;
250         b->is_def = is_def;
251         b->is_real = is_real;
252         b->irn = irn;
253         b->step = step;
254         list_add_tail(&b->list, head);
255         DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n",
256                                 is_def ? "def" : "use", irn, step));
257
258
259         return b;
260 }
261
262 static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
263 {
264   return arch_irn_has_reg_class(env->session_env->main_env->arch_env,
265                         irn, arch_pos_make_out(0), env->cls);
266 }
267
268 /**
269  * Annotate the register pressure to the nodes and compute
270  * the liveness intervals.
271  * @param block The block to do it for.
272  * @param env_ptr The environment.
273  */
274 static void pressure(ir_node *block, void *env_ptr)
275 {
276 /* Convenience macro for a def */
277 #define border_def(irn, step, real) \
278         border_add(env, head, irn, step, pressure--, 1, real)
279
280 /* Convenience macro for a use */
281 #define border_use(irn, step, real) \
282         border_add(env, head, irn, step, ++pressure, 0, real)
283
284         be_chordal_env_t *env = env_ptr;
285         bitset_t *live = env->live;
286         ir_node *irn;
287
288         int i, n;
289         unsigned step = 0;
290         unsigned pressure = 0;
291         struct list_head *head;
292         pset *live_in = put_live_in(block, pset_new_ptr_default());
293         pset *live_end = put_live_end(block, pset_new_ptr_default());
294
295         DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
296         bitset_clear_all(live);
297
298         /* Set up the border list in the block info */
299         head = obstack_alloc(&env->obst, sizeof(*head));
300         INIT_LIST_HEAD(head);
301   assert(pmap_get(env->border_heads, block) == NULL);
302         pmap_insert(env->border_heads, block, head);
303
304         /*
305          * Make final uses of all values live out of the block.
306          * They are necessary to build up real intervals.
307          */
308         for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
309                 if(has_reg_class(env, irn)) {
310                         DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
311                         bitset_set(live, get_irn_graph_nr(irn));
312                         border_use(irn, step, 0);
313                 }
314         }
315         ++step;
316
317         /*
318          * Determine the last uses of a value inside the block, since they are
319          * relevant for the interval borders.
320          */
321         sched_foreach_reverse(block, irn) {
322                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
323                 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
324
325             /*
326              * If the node defines some value, which can put into a
327              * register of the current class, make a border for it.
328              */
329                 if(has_reg_class(env, irn)) {
330                         bitset_pos_t elm;
331                         int nr = get_irn_graph_nr(irn);
332
333                         bitset_clear(live, nr);
334                         border_def(irn, step, 1);
335
336 #ifdef BUILD_GRAPH
337                         bitset_foreach(live, elm)
338                                 add_if(env, nr, (int) elm);
339 #endif
340                 }
341
342                 /*
343                  * If the node is no phi node we can examine the uses.
344                  */
345                 if(!is_Phi(irn)) {
346                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
347                                 ir_node *op = get_irn_n(irn, i);
348
349                                 if(has_reg_class(env, op)) {
350                                         int nr = get_irn_graph_nr(op);
351
352                                         DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
353
354                                         if(!bitset_is_set(live, nr)) {
355                                                 border_use(op, step, 1);
356                                                 bitset_set(live, nr);
357                                         }
358                                 }
359                         }
360                 }
361                 ++step;
362         }
363
364         /*
365          * Add initial defs for all values live in.
366          */
367         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
368                 if(has_reg_class(env, irn)) {
369
370                         /* Mark the value live in. */
371                         bitset_set(live, get_irn_graph_nr(irn));
372
373                         /* Add the def */
374                         border_def(irn, step, 0);
375                 }
376         }
377
378
379   del_pset(live_in);
380   del_pset(live_end);
381 }
382
383 static void assign(ir_node *block, void *env_ptr)
384 {
385         be_chordal_env_t *env = env_ptr;
386         bitset_t *live = env->live;
387         bitset_t *colors = env->colors;
388         bitset_t *in_colors = env->in_colors;
389         const arch_env_t *arch_env = env->session_env->main_env->arch_env;
390
391         const ir_node *irn;
392         border_t *b;
393         struct list_head *head = get_block_border_head(env, block);
394         pset *live_in = put_live_in(block, pset_new_ptr_default());
395
396         bitset_clear_all(live);
397         bitset_clear_all(colors);
398         bitset_clear_all(in_colors);
399
400         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
401         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
402         list_for_each_entry(border_t, b, head, list) {
403                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
404                                         b->irn, get_irn_graph_nr(b->irn)));
405         }
406
407         /*
408          * Add initial defs for all values live in.
409          * Since their colors have already been assigned (The dominators were
410          * allocated before), we have to mark their colors as used also.
411          */
412         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
413                 if(has_reg_class(env, irn)) {
414                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn, 0);
415                         int col;
416
417                         assert(reg && "Node must have been assigned a register");
418                         col = arch_register_get_index(reg);
419
420                         /* Mark the color of the live in value as used. */
421                         bitset_set(colors, col);
422                         bitset_set(in_colors, col);
423
424                         /* Mark the value live in. */
425                         bitset_set(live, get_irn_graph_nr(irn));
426                 }
427         }
428
429         /*
430          * Mind that the sequence of defs from back to front defines a perfect
431          * elimination order. So, coloring the definitions from first to last
432          * will work.
433          */
434         list_for_each_entry_reverse(border_t, b, head, list) {
435                 ir_node *irn = b->irn;
436                 int nr = get_irn_graph_nr(irn);
437
438                 /*
439                  * Assign a color, if it is a local def. Global defs already have a
440                  * color.
441                  */
442                 if(b->is_def && !is_live_in(block, irn)) {
443                         const arch_register_t *reg;
444                         int col = NO_COLOR;
445
446                         DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
447
448                         col = bitset_next_clear(colors, 0);
449                         reg = arch_register_for_index(env->cls, col);
450
451                         assert(arch_get_irn_register(arch_env, irn, 0) == NULL && "This node must not have been assigned a register yet");
452                         assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
453
454                         bitset_set(colors, col);
455                         bitset_set(live, nr);
456
457                         arch_set_irn_register(arch_env, irn, 0, reg);
458                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
459             arch_register_get_name(reg), col, irn));
460                 }
461
462                 /* Clear the color upon a use. */
463                 else if(!b->is_def) {
464                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn, 0);
465                         int col;
466
467                         assert(reg && "Register must have been assigned");
468
469                         col = arch_register_get_index(reg);
470                         assert(bitset_is_set(live, nr) && "Cannot have a non live use");
471
472                         bitset_clear(colors, col);
473                         bitset_clear(live, nr);
474                 }
475         }
476
477         del_pset(live_in);
478 }
479
480 void be_ra_chordal_init(void)
481 {
482         dbg = firm_dbg_register(DBG_CHORDAL);
483         firm_dbg_set_mask(dbg, DBG_LEVEL);
484 }
485
486 be_chordal_env_t *be_ra_chordal(
487     const be_main_session_env_t *session,
488     const arch_register_class_t *cls)
489 {
490   ir_graph *irg = session->irg;
491         int node_count = get_graph_node_count(irg);
492         int colors_n = arch_register_class_n_regs(cls);
493         be_chordal_env_t *env = malloc(sizeof(*env));
494
495         if(get_irg_dom_state(irg) != dom_consistent)
496                 compute_doms(irg);
497
498         obstack_init(&env->obst);
499
500 #ifdef BUILD_GRAPH
501         env->edges = new_set(if_edge_cmp, node_count);
502         env->nodes = new_set(if_node_cmp, node_count);
503 #endif
504
505   env->session_env = session;
506         env->live = bitset_obstack_alloc(&env->obst, node_count);
507         env->colors = bitset_obstack_alloc(&env->obst, colors_n);
508         env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
509         env->colors_n = colors_n;
510         env->cls = cls;
511         env->border_heads = pmap_create();
512
513         /* First, determine the pressure */
514         dom_tree_walk_irg(irg, pressure, NULL, env);
515
516         /* Insert probable spills */
517         be_ra_chordal_spill(env);
518
519         /* Assign the colors */
520         dom_tree_walk_irg(irg, assign, NULL, env);
521
522 #ifdef DUMP_IFG
523         dump_ifg(env);
524 #endif
525
526 #ifdef DUMP_INTERVALS
527         {
528                 char buf[128];
529         plotter_t *plotter;
530
531                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
532         plotter = new_plotter_ps(buf);
533
534         draw_interval_tree(&draw_chordal_def_opts, env, plotter, env->session_env->main_env->arch_env, cls);
535         plotter_free(plotter);
536         }
537 #endif
538         return env;
539 }
540
541 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
542         const arch_env_t *arch_env = chordal_env->session_env->main_env->arch_env;
543         struct obstack ob;
544         pmap_entry *pme;
545         ir_node **nodes, *n1, *n2;
546         int i, o;
547
548         /* Collect all irns */
549         obstack_init(&ob);
550         pmap_foreach(chordal_env->border_heads, pme) {
551                 border_t *curr;
552                 struct list_head *head = pme->value;
553                 list_for_each_entry(border_t, curr, head, list)
554                         if (curr->is_def && curr->is_real)
555                                 if (arch_get_irn_reg_class(arch_env, curr->irn, arch_pos_make_out(0)) == chordal_env->cls)
556                                         obstack_ptr_grow(&ob, curr->irn);
557         }
558         obstack_ptr_grow(&ob, NULL);
559         nodes = (ir_node **) obstack_finish(&ob);
560
561         /* Check them */
562         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
563                 const arch_register_t *n1_reg, *n2_reg;
564
565                 n1_reg = arch_get_irn_register(arch_env, n1, 0);
566                 if (!arch_reg_is_allocatable(arch_env, n1, arch_pos_make_out(0), n1_reg)) {
567                         DBG((dbg, 0, "Register assigned to %+F is not allowed\n", n1));
568                         assert(0 && "Register constraint does not hold");
569                 }
570                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
571                         n2_reg = arch_get_irn_register(arch_env, n2, 0);
572                         if (nodes_interfere(chordal_env, n1, n2) && n1_reg == n2_reg) {
573                                 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
574                                 assert(0 && "Interfering values have the same color!");
575                         }
576                 }
577         }
578         obstack_free(&ob, NULL);
579 }
580
581 /* BETTER #ifdef BUILD_GRAPH --> faster version of checker with edges */
582
583 void be_ra_chordal_done(be_chordal_env_t *env)
584 {
585 #ifdef BUILD_GRAPH
586         {
587                 if_node_t *ifn;
588                 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
589                         free(ifn->neighb);
590                 free(env->nodes);
591                 free(env->edges);
592         }
593 #endif
594
595   pmap_destroy(env->border_heads);
596         obstack_free(&env->obst, NULL);
597         free(env);
598 }
599
600
601
602 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
603 {
604 #ifdef BUILD_GRAPH
605         return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
606 #else
607         return values_interfere(a, b);
608 #endif /* BUILD_GRAPH */
609 }
610
611 #ifdef BUILD_GRAPH
612
613 set *be_ra_get_ifg_edges(const be_chordal_env_t *env) {
614         return env->edges;
615 }
616
617 set *be_ra_get_ifg_nodes(const be_chordal_env_t *env) {
618         return env->nodes;
619 }
620
621 #endif
622
623 typedef struct {
624         const be_main_session_env_t *env;
625         const arch_register_class_t *cls;
626 } check_pressure_info_t;
627
628
629 static int check_pressure_has_class(const check_pressure_info_t *i, const ir_node *irn)
630 {
631   return arch_irn_has_reg_class(i->env->main_env->arch_env,
632       irn, arch_pos_make_out(0), i->cls);
633 }
634
635 static void check_pressure_walker(ir_node *bl, void *data)
636 {
637         firm_dbg_module_t *dbg = firm_dbg_register("be.ra.pressure");
638         check_pressure_info_t *info = data;
639         int n_regs = arch_register_class_n_regs(info->cls);
640
641         pset *live = pset_new_ptr_default();
642         int step = 0;
643         ir_node *irn;
644         irn_live_t *li;
645
646         firm_dbg_set_mask(dbg, DBG_LEVEL_CHECK);
647
648         live_foreach(bl, li) {
649                 if(live_is_end(li) && check_pressure_has_class(info, li->irn)) {
650                         ir_node *irn = (ir_node *) li->irn;
651                         pset_insert_ptr(live, irn);
652                 }
653         }
654
655         DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
656         for(irn = pset_first(live); irn; irn = pset_next(live))
657                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
658
659         sched_foreach_reverse(bl, irn) {
660                 int i, n;
661                 int pressure = pset_count(live);
662
663                 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
664
665                 if(pressure > n_regs) {
666                         ir_node *x;
667                         ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
668                         for(x = pset_first(live); x; x = pset_next(live))
669                                 ir_printf("\t%+10F\n", x);
670                 }
671
672                 if(check_pressure_has_class(info, irn))
673                         pset_remove_ptr(live, irn);
674
675                 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
676                         ir_node *op = get_irn_n(irn, i);
677                         if(check_pressure_has_class(info, op) && !is_Phi(irn))
678                                 pset_insert_ptr(live, op);
679                 }
680                 step++;
681         }
682 }
683
684 void be_check_pressure(const be_main_session_env_t *env, const arch_register_class_t *cls)
685 {
686         check_pressure_info_t i;
687         i.env = env;
688         i.cls = cls;
689         irg_block_walk_graph(env->irg, check_pressure_walker, NULL, &i);
690 }