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