Added checker for register allocation. Interference and constraints get checked.
[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 NO_COLOR (-1)
42
43 #undef DUMP_INTERVALS
44 #undef DUMP_PRESSURE
45 #undef DUMP_IFG
46
47 #if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
48 #error Must define BUILD_GRAPH to be able to dump it.
49 #endif
50
51
52 #ifdef DEBUG_libfirm
53 #include "fourcc.h"
54
55 /* Make a fourcc for border checking. */
56 #define BORDER_FOURCC                           FOURCC('B', 'O', 'R', 'D')
57
58 #endif /* DEBUG_libfirm */
59
60 static firm_dbg_module_t *dbg;
61
62 #ifdef BUILD_GRAPH
63
64 #define IF_EDGE_HASH(e) ((e)->src)
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
181 /**
182  * Add an interval border to the list of a block's list
183  * of interval border.
184  * @note You always have to create the use before the def.
185  * @param env The environment.
186  * @param head The list head to enqueue the borders.
187  * @param irn The node (value) the border belongs to.
188  * @param pressure The pressure at this point in time.
189  * @param step A time step for the border.
190  * @param is_def Is the border a use or a def.
191  * @return The created border.
192  */
193 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
194                         ir_node *irn, unsigned step, unsigned pressure,
195                         unsigned is_def, unsigned is_real)
196 {
197         border_t *b;
198
199         if(!is_def) {
200                 border_t *def;
201
202                 b = obstack_alloc(&env->obst, sizeof(*b));
203
204                 /* also allocate the def and tie it to the use. */
205                 def = obstack_alloc(&env->obst, sizeof(*def));
206                 b->other_end = def;
207                 def->other_end = b;
208
209                 /*
210                  * Set the link field of the irn to the def.
211                  * This strongly relies on the fact, that the use is always
212                  * made before the def.
213                  */
214                 set_irn_link(irn, def);
215
216 #ifdef DEBUG_libfirm
217                 b->magic = BORDER_FOURCC;
218                 def->magic = BORDER_FOURCC;
219 #endif
220         }
221
222         /*
223          * If the def is encountered, the use was made and so was the
224          * the def node (see the code above). It was placed into the
225          * link field of the irn, so we can get it there.
226          */
227         else {
228                 b = get_irn_link(irn);
229
230 #ifdef DEBUG_libfirm
231                 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
232 #endif
233         }
234
235         b->pressure = pressure;
236         b->is_def = is_def;
237         b->is_real = is_real;
238         b->irn = irn;
239         b->step = step;
240         list_add_tail(&b->list, head);
241         DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n",
242                                 is_def ? "def" : "use", irn, step));
243
244         return b;
245 }
246
247 /**
248  * Annotate the register pressure to the nodes and compute
249  * the liveness intervals.
250  * @param block The block to do it for.
251  * @param env_ptr The environment.
252  */
253 static void pressure(ir_node *block, void *env_ptr)
254 {
255 /* Convenience macro for a def */
256 #define border_def(irn, step, real) \
257         border_add(env, head, irn, step, pressure--, 1, real)
258
259 /* Convenience macro for a use */
260 #define border_use(irn, step, real) \
261         border_add(env, head, irn, step, ++pressure, 0, real)
262
263         be_chordal_env_t *env = env_ptr;
264         bitset_t *live = env->live;
265         ir_node *irn;
266
267         int i, n;
268         unsigned step = 0;
269         unsigned pressure = 0;
270         struct list_head *head;
271         pset *live_in = put_live_in(block, pset_new_ptr_default());
272         pset *live_end = put_live_end(block, pset_new_ptr_default());
273         const arch_register_class_t *cls = env->cls;
274
275         DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
276         bitset_clear_all(live);
277
278         /* Set up the border list in the block info */
279         head = obstack_alloc(&env->obst, sizeof(*head));
280         INIT_LIST_HEAD(head);
281         pmap_insert(env->border_heads, block, head);
282
283         /*
284          * Make final uses of all values live out of the block.
285          * They are necessary to build up real intervals.
286          */
287         for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
288                 DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
289                 bitset_set(live, get_irn_graph_nr(irn));
290                 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls))
291                         border_use(irn, step, 0);
292         }
293         ++step;
294
295         /*
296          * Determine the last uses of a value inside the block, since they are
297          * relevant for the interval borders.
298          */
299         sched_foreach_reverse(block, irn) {
300                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
301                 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
302
303             /*
304              * If the node defines some value, which can put into a
305              * register of the current class, make a border for it.
306              */
307                 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
308                         bitset_pos_t elm;
309                         int nr = get_irn_graph_nr(irn);
310
311                         bitset_clear(live, nr);
312                         border_def(irn, step, 1);
313
314 #ifdef BUILD_GRAPH
315                         bitset_foreach(live, elm)
316                         add_if(env, nr, (int) elm);
317 #endif
318                 }
319
320                 /*
321                  * If the node is no phi node we can examine the uses.
322                  */
323                 if(!is_Phi(irn)) {
324                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
325                                 ir_node *op = get_irn_n(irn, i);
326
327                                 if(arch_irn_has_reg_class(env->arch_env, op, 0, cls)) {
328                                         int nr = get_irn_graph_nr(op);
329
330                                         DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
331
332                                         if(!bitset_is_set(live, nr)) {
333                                                 border_use(op, step, 1);
334                                                 bitset_set(live, nr);
335                                         }
336                                 }
337                         }
338                 }
339                 ++step;
340         }
341
342         /*
343          * Add initial defs for all values live in.
344          */
345         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
346                 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
347
348                         /* Mark the value live in. */
349                         bitset_set(live, get_irn_graph_nr(irn));
350
351                         /* Add the def */
352                         border_def(irn, step, 0);
353                 }
354         }
355
356     del_pset(live_in);
357     del_pset(live_end);
358 }
359
360 static void assign(ir_node *block, void *env_ptr)
361 {
362         be_chordal_env_t *env = env_ptr;
363         struct obstack *obst = &env->obst;
364         bitset_t *live = env->live;
365         bitset_t *colors = env->colors;
366         bitset_t *in_colors = env->in_colors;
367         const arch_register_class_t *cls = env->cls;
368
369         /* Mark the obstack level and allocate the temporary tmp_colors */
370         void *obstack_level = obstack_base(obst);
371         /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
372
373         const ir_node *irn;
374         border_t *b;
375         struct list_head *head = get_block_border_head(env, block);
376         pset *live_in = put_live_in(block, pset_new_ptr_default());
377
378         bitset_clear_all(live);
379         bitset_clear_all(colors);
380         bitset_clear_all(in_colors);
381
382         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
383         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
384         list_for_each_entry(border_t, b, head, list) {
385                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
386                                         b->irn, get_irn_graph_nr(b->irn)));
387         }
388
389         /*
390          * Add initial defs for all values live in.
391          * Since their colors have already been assigned (The dominators were
392          * allocated before), we have to mark their colors as used also.
393          */
394         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
395                 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
396       const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
397       int col;
398
399       assert(reg && "Node must have been assigned a register");
400                         col = arch_register_get_index(reg);
401
402                         /* Mark the color of the live in value as used. */
403                         bitset_set(colors, col);
404                         bitset_set(in_colors, col);
405
406                         /* Mark the value live in. */
407                         bitset_set(live, get_irn_graph_nr(irn));
408                 }
409         }
410
411         /*
412          * Mind that the sequence of defs from back to front defines a perfect
413          * elimination order. So, coloring the definitions from first to last
414          * will work.
415          */
416         list_for_each_entry_reverse(border_t, b, head, list) {
417                 ir_node *irn = b->irn;
418                 int nr = get_irn_graph_nr(irn);
419
420                 /*
421                  * Assign a color, if it is a local def. Global defs already have a
422                  * color.
423                  */
424                 if(b->is_def && !is_live_in(block, irn)) {
425       const arch_register_t *reg;
426                         int col = NO_COLOR;
427
428                         DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
429
430       col = bitset_next_clear(colors, 0);
431       reg = arch_register_for_index(env->cls, col);
432
433       assert(arch_get_irn_register(env->arch_env, irn, 0) == NULL
434           && "This node must not have been assigned a register yet");
435                         assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
436
437                         bitset_set(colors, col);
438                         bitset_set(live, nr);
439
440                         arch_set_irn_register(env->arch_env, irn, 0, reg);
441                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
442             arch_register_get_name(reg), col, irn));
443                 }
444
445                 /* Clear the color upon a use. */
446                 else if(!b->is_def) {
447       const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
448                         int col;
449
450       assert(reg && "Register must have been assigned");
451
452       col = arch_register_get_index(reg);
453                         assert(bitset_is_set(live, nr) && "Cannot have a non live use");
454
455                         bitset_clear(colors, col);
456                         bitset_clear(live, nr);
457                 }
458         }
459
460         /* Free the auxillary data on the obstack. */
461         obstack_free(obst, obstack_level);
462
463   del_pset(live_in);
464 }
465
466 void be_ra_chordal_init(void)
467 {
468         dbg = firm_dbg_register(DBG_CHORDAL);
469         firm_dbg_set_mask(dbg, 0);
470 }
471
472 be_chordal_env_t *be_ra_chordal(ir_graph *irg,
473     const arch_env_t *arch_env,
474     const arch_register_class_t *cls)
475 {
476         int node_count = get_graph_node_count(irg);
477         int colors_n = arch_register_class_n_regs(cls);
478         be_chordal_env_t *env = malloc(sizeof(*env));
479
480         if(get_irg_dom_state(irg) != dom_consistent)
481                 compute_doms(irg);
482
483         obstack_init(&env->obst);
484
485 #ifdef BUILD_GRAPH
486         env->edges = new_set(if_edge_cmp, node_count);
487         env->nodes = new_set(if_node_cmp, node_count);
488 #endif
489
490         env->live = bitset_obstack_alloc(&env->obst, node_count);
491         env->colors = bitset_obstack_alloc(&env->obst, colors_n);
492         env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
493         env->colors_n = colors_n;
494         env->cls = cls;
495         env->arch_env = arch_env;
496         env->irg = irg;
497         env->border_heads = pmap_create();
498
499         /* First, determine the pressure */
500         dom_tree_walk_irg(irg, pressure, NULL, env);
501
502         /* Insert probable spills */
503         be_ra_chordal_spill(env);
504
505         /* Assign the colors */
506         dom_tree_walk_irg(irg, assign, NULL, env);
507
508 #ifdef DUMP_IFG
509         dump_ifg(env);
510 #endif
511
512 #ifdef DUMP_INTERVALS
513         {
514                 char buf[128];
515         plotter_t *plotter;
516
517                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
518         plotter = new_plotter_ps(buf);
519
520         draw_interval_tree(&draw_chordal_def_opts, env, plotter, arch_env, cls);
521         plotter_free(plotter);
522         }
523 #endif
524         return env;
525 }
526
527 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
528         const arch_env_t *arch_env;
529         struct obstack ob;
530         pmap_entry *pme;
531         ir_node **nodes, *n1, *n2;
532         int i, o;
533
534         arch_env = chordal_env->arch_env;
535
536         /* Collect all irns */
537         obstack_init(&ob);
538         pmap_foreach(chordal_env->border_heads, pme) {
539                 border_t *curr;
540                 struct list_head *head = pme->value;
541                 list_for_each_entry(border_t, curr, head, list)
542                         if (curr->is_def && curr->is_real)
543                                 if (arch_get_irn_reg_class(arch_env, curr->irn, arch_pos_make_out(0)) == chordal_env->cls)
544                                         obstack_ptr_grow(&ob, curr->irn);
545         }
546         obstack_ptr_grow(&ob, NULL);
547         nodes = (ir_node **) obstack_finish(&ob);
548
549         /* Check them */
550         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
551                 const arch_register_t *n1_reg, *n2_reg;
552
553                 n1_reg = arch_get_irn_register(arch_env, n1, 0);
554                 assert(arch_reg_is_allocatable(arch_env, n1, arch_pos_make_out(0), n1_reg) && "Register constraint does not hold");
555
556                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
557                         n2_reg = arch_get_irn_register(arch_env, n2, 0);
558                         assert(!(nodes_interfere(chordal_env, n1, n2) && n1_reg == n2_reg) && "Interfering values have the same color!");
559                 }
560         }
561         obstack_free(&ob, NULL);
562 }
563
564 /* TODO #ifdef BUILD_GRAPH --> faster version of checker with edges */
565
566 void be_ra_chordal_done(be_chordal_env_t *env)
567 {
568 #ifdef BUILD_GRAPH
569         {
570                 if_node_t *ifn;
571                 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
572                         free(ifn->neighb);
573                 free(env->nodes);
574                 free(env->edges);
575         }
576 #endif
577
578   pmap_destroy(env->border_heads);
579         obstack_free(&env->obst, NULL);
580         free(env);
581 }
582
583 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
584 {
585 #ifdef BUILD_GRAPH
586         return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
587 #else
588         return values_interfere(a, b);
589 #endif /* BUILD_GRAPH */
590 }
591
592 #ifdef BUILD_GRAPH
593
594 set *be_ra_get_ifg_edges(const be_chordal_env_t *env) {
595         return env->edges;
596 }
597
598 set *be_ra_get_ifg_nodes(const be_chordal_env_t *env) {
599         return env->nodes;
600 }
601
602 #endif