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