adapted to new callback
[libfirm] / ir / be / bechordal.c
1 /**
2  * Chordal register allocation.
3  * @author Sebastian Hack
4  * @date   8.12.2004
5  * @cvs-id $Id$
6  *
7  * Copyright (C) Universitaet Karlsruhe
8  * Released under the GPL
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #ifdef HAVE_MALLOC_H
16 #include <malloc.h>
17 #endif
18
19 #ifdef HAVE_ALLOCA_H
20 #include <alloca.h>
21 #endif
22
23 #include <ctype.h>
24
25 #include "obst.h"
26 #include "pset.h"
27 #include "list.h"
28 #include "bitset.h"
29 #include "iterator.h"
30 #include "bipartite.h"
31
32 #include "irmode_t.h"
33 #include "irgraph_t.h"
34 #include "irprintf_t.h"
35 #include "irgwalk.h"
36 #include "irdump.h"
37 #include "irdom.h"
38 #include "irtools.h"
39 #include "debug.h"
40 #include "xmalloc.h"
41
42 #include "beutil.h"
43 #include "besched.h"
44 #include "benumb_t.h"
45 #include "besched_t.h"
46 #include "belive_t.h"
47 #include "benode_t.h"
48 #include "bearch.h"
49 #include "beirgmod.h"
50 #include "beifg.h"
51 #include "beinsn_t.h"
52
53 #include "bechordal_t.h"
54 #include "bechordal_draw.h"
55
56 #define DBG_LEVEL SET_LEVEL_0
57 #define DBG_LEVEL_CHECK SET_LEVEL_0
58
59 #define NO_COLOR (-1)
60
61 #define DUMP_INTERVALS
62
63 typedef struct _be_chordal_alloc_env_t {
64         be_chordal_env_t *chordal_env;
65
66         pset *pre_colored;              /**< Set of precolored nodes. */
67         bitset_t *live;                             /**< A liveness bitset. */
68         bitset_t *tmp_colors;           /**< An auxiliary bitset which is as long as the number of colors in the class. */
69         bitset_t *colors;                           /**< The color mask. */
70         bitset_t *in_colors;            /**< Colors used by live in values. */
71         int colors_n;                   /**< The number of colors. */
72         DEBUG_ONLY(firm_dbg_module_t *constr_dbg;)  /**< Debug output for the constraint handler. */
73 } be_chordal_alloc_env_t;
74
75 #include "fourcc.h"
76
77 /* Make a fourcc for border checking. */
78 #define BORDER_FOURCC                           FOURCC('B', 'O', 'R', 'D')
79
80 #if 0
81 static void check_border_list(struct list_head *head)
82 {
83   border_t *x;
84   list_for_each_entry(border_t, x, head, list) {
85     assert(x->magic == BORDER_FOURCC);
86   }
87 }
88
89 static void check_heads(be_chordal_env_t *env)
90 {
91   pmap_entry *ent;
92   for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
93     /* ir_printf("checking border list of block %+F\n", ent->key); */
94     check_border_list(ent->value);
95   }
96 }
97 #endif
98
99 /**
100  * Add an interval border to the list of a block's list
101  * of interval border.
102  * @note You always have to create the use before the def.
103  * @param env The environment.
104  * @param head The list head to enqueue the borders.
105  * @param irn The node (value) the border belongs to.
106  * @param pressure The pressure at this point in time.
107  * @param step A time step for the border.
108  * @param is_def Is the border a use or a def.
109  * @return The created border.
110  */
111 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
112                         ir_node *irn, unsigned step, unsigned pressure,
113                         unsigned is_def, unsigned is_real)
114 {
115         border_t *b;
116
117         if(!is_def) {
118                 border_t *def;
119
120                 b = obstack_alloc(&env->obst, sizeof(*b));
121
122                 /* also allocate the def and tie it to the use. */
123                 def = obstack_alloc(&env->obst, sizeof(*def));
124                 memset(def, 0, sizeof(*def));
125                 b->other_end = def;
126                 def->other_end = b;
127
128                 /*
129                  * Set the link field of the irn to the def.
130                  * This strongly relies on the fact, that the use is always
131                  * made before the def.
132                  */
133                 set_irn_link(irn, def);
134
135                 b->magic = BORDER_FOURCC;
136                 def->magic = BORDER_FOURCC;
137         }
138
139         /*
140          * If the def is encountered, the use was made and so was the
141          * the def node (see the code above). It was placed into the
142          * link field of the irn, so we can get it there.
143          */
144         else {
145                 b = get_irn_link(irn);
146
147                 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
148         }
149
150         b->pressure = pressure;
151         b->is_def = is_def;
152         b->is_real = is_real;
153         b->irn = irn;
154         b->step = step;
155         list_add_tail(&b->list, head);
156         DBG((env->dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
157
158
159         return b;
160 }
161
162 /**
163  * Check, if an irn is of the register class currently under processing.
164  * @param env The chordal environment.
165  * @param irn The node.
166  * @return 1, if the node is of that register class, 0 if not.
167  */
168 static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
169 {
170         return arch_irn_has_reg_class(env->birg->main_env->arch_env, irn, -1, env->cls);
171         // return arch_irn_consider_in_reg_alloc(env->birg->main_env->arch_env, env->cls, irn);
172 }
173
174 #define has_limited_constr(req, irn) \
175         (arch_get_register_req(arch_env, (req), irn, -1) && (req)->type == arch_register_req_type_limited)
176
177 static int get_next_free_reg(const be_chordal_alloc_env_t *alloc_env, bitset_t *colors)
178 {
179         bitset_t *tmp = alloc_env->tmp_colors;
180         bitset_copy(tmp, colors);
181         bitset_or(tmp, alloc_env->chordal_env->ignore_colors);
182         return bitset_next_clear(tmp, 0);
183 }
184
185 static bitset_t *get_decisive_partner_regs(bitset_t *bs, const be_operand_t *o1, const be_operand_t *o2)
186 {
187         bitset_t *res = bs;
188
189         if(!o1) {
190                 bitset_copy(bs, o2->regs);
191                 return bs;
192         }
193
194         if(!o2) {
195                 bitset_copy(bs, o1->regs);
196                 return bs;
197         }
198
199         assert(o1->req.cls == o2->req.cls);
200
201         if(bitset_contains(o1->regs, o2->regs))
202                 bitset_copy(bs, o1->regs);
203         else if(bitset_contains(o2->regs, o1->regs))
204                 bitset_copy(bs, o2->regs);
205         else
206                 res = NULL;
207
208         return res;
209 }
210
211 static be_insn_t *chordal_scan_insn(be_chordal_env_t *env, ir_node *irn)
212 {
213         be_insn_env_t ie;
214
215         ie.ignore_colors = env->ignore_colors;
216         ie.aenv          = env->birg->main_env->arch_env;
217         ie.obst          = &env->obst;
218         ie.cls           = env->cls;
219         return be_scan_insn(&ie, irn);
220 }
221
222 static ir_node *prepare_constr_insn(be_chordal_env_t *env, ir_node *irn)
223 {
224         be_insn_t *insn      = chordal_scan_insn(env, irn);
225         bitset_t *def_constr = bitset_alloca(env->cls->n_regs);
226         bitset_t *tmp        = bitset_alloca(env->cls->n_regs);
227         ir_node *bl          = get_nodes_block(insn->irn);
228         int i, j;
229
230         if(!insn->has_constraints)
231                 goto end;
232
233         /* insert copies for nodes that occur constrained more than once. */
234         for(i = insn->use_start; i < insn->n_ops; ++i) {
235                 be_operand_t *op = &insn->ops[i];
236
237                 if(op->has_constraints) {
238                         for(j = i + 1; j < insn->n_ops; ++j) {
239                                 be_operand_t *a_op = &insn->ops[j];
240
241                                 if(a_op->carrier == op->carrier && a_op->has_constraints) {
242                                         ir_node *copy = be_new_Copy(env->cls, env->irg, bl, op->carrier);
243
244                                         sched_add_before(insn->irn, copy);
245                                         set_irn_n(insn->irn, a_op->pos, copy);
246                                         DBG((env->dbg, LEVEL_3, "inserting multiple constr copy %+F for %+F pos %d\n", copy, insn->irn, a_op->pos));
247                                 }
248                         }
249                 }
250         }
251
252         /* collect all registers occuring in out constraints. */
253         for(i = 0; i < insn->use_start; ++i) {
254                 be_operand_t *op = &insn->ops[i];
255                 if(op->has_constraints)
256                         bitset_or(def_constr, op->regs);
257         }
258
259         /*
260                 insert copies for all constrained arguments living through the node
261                 and being constrained to a register which also occurs in out constraints.
262         */
263         for(i = insn->use_start; i < insn->n_ops; ++i) {
264                 be_operand_t *op = &insn->ops[i];
265
266                 bitset_copy(tmp, op->regs);
267                 bitset_and(tmp, def_constr);
268
269                 /*
270                         Check, if
271                         1) the operand is constrained.
272                         2) lives through the node.
273                         3) is constrained to a register occuring in out constraints.
274                 */
275                 if(op->has_constraints && values_interfere(env->lv, insn->irn, op->carrier) && bitset_popcnt(tmp) > 0) {
276                         /*
277                                 only create the copy if the operand is no copy.
278                                 this is necessary since the assure constraints phase inserts
279                                 Copies and Keeps for operands which must be different from the results.
280                                 Additional copies here would destroy this.
281                         */
282                         if(!be_is_Copy(op->carrier)) {
283                                 ir_node *copy = be_new_Copy(env->cls, env->irg, bl, op->carrier);
284
285                                 sched_add_before(insn->irn, copy);
286                                 set_irn_n(insn->irn, op->pos, copy);
287                                 DBG((env->dbg, LEVEL_3, "inserting constr copy %+F for %+F pos %d\n", copy, insn->irn, op->pos));
288                                 be_liveness_update(env->lv, op->carrier);
289                         }
290                 }
291         }
292
293 end:
294         obstack_free(&env->obst, insn);
295         return insn->next_insn;
296 }
297
298 static void pre_spill_prepare_constr_walker(ir_node *bl, void *data)
299 {
300         be_chordal_env_t *env = data;
301         ir_node *irn;
302         for(irn = sched_first(bl); !sched_is_end(irn);) {
303                 irn = prepare_constr_insn(env, irn);
304         }
305 }
306
307 void be_pre_spill_prepare_constr(be_chordal_env_t *cenv) {
308         irg_block_walk_graph(cenv->irg, pre_spill_prepare_constr_walker, NULL, (void *) cenv);
309 }
310
311 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
312 {
313         const be_chordal_env_t *env = alloc_env->chordal_env;
314
315         int n_uses   = be_insn_n_uses(insn);
316         int n_defs   = be_insn_n_defs(insn);
317         bitset_t *bs = bitset_alloca(env->cls->n_regs);
318         int *pairing = alloca(MAX(n_defs, n_uses) * sizeof(pairing[0]));
319
320         int i, j;
321
322         /*
323                 For each out operand, try to find an in operand which can be assigned the
324                 same register as the out operand.
325         */
326         for (j = 0; j < insn->use_start; ++j) {
327                 int smallest         = -1;
328                 int smallest_n_regs  = 2 * env->cls->n_regs + 1;
329                 be_operand_t *out_op = &insn->ops[j];
330
331                 /* Try to find an in operand which has ... */
332                 for(i = insn->use_start; i < insn->n_ops; ++i) {
333                         int n_total;
334                         const be_operand_t *op = &insn->ops[i];
335
336                         if (! values_interfere(env->lv, op->irn, op->carrier) && ! op->partner) {
337                                 bitset_clear_all(bs);
338                                 bitset_copy(bs, op->regs);
339                                 bitset_and(bs, out_op->regs);
340                                 n_total = bitset_popcnt(op->regs) + bitset_popcnt(out_op->regs);
341
342                                 if (bitset_popcnt(bs) > 0 && n_total < smallest_n_regs) {
343                                         smallest = i;
344                                         smallest_n_regs = n_total;
345                                 }
346                         }
347                 }
348
349                 if (smallest >= 0) {
350                         be_operand_t *partner = &insn->ops[smallest];
351                         out_op->partner  = partner;
352                         partner->partner = out_op;
353                 }
354         }
355 }
356
357
358 static ir_node *pre_process_constraints(be_chordal_alloc_env_t *alloc_env, be_insn_t **the_insn)
359 {
360         be_chordal_env_t *env       = alloc_env->chordal_env;
361         const arch_env_t *aenv      = env->birg->main_env->arch_env;
362         be_insn_t *insn             = *the_insn;
363         ir_node *bl                 = get_nodes_block(insn->irn);
364         ir_node *copy               = NULL;
365         ir_node *perm               = NULL;
366         bitset_t *out_constr        = bitset_alloca(env->cls->n_regs);
367         bitset_t *bs                = bitset_alloca(env->cls->n_regs);
368         DEBUG_ONLY(firm_dbg_module_t *dbg      = alloc_env->constr_dbg;)
369
370         int i;
371
372         assert(insn->has_constraints && "only do this for constrained nodes");
373
374         /*
375                 Collect all registers that occur in output constraints.
376                 This is necessary, since if the insn has one of these as an input constraint
377                 and the corresponding operand interferes with the insn, the operand must
378                 be copied.
379         */
380         for(i = 0; i < insn->use_start; ++i) {
381                 be_operand_t *op = &insn->ops[i];
382                 if(op->has_constraints)
383                         bitset_or(out_constr, op->regs);
384         }
385
386         /*
387                 Now, figure out which input operand must be copied since it has input
388                 constraints which are also output constraints.
389         */
390         (void) bl;
391         (void) copy;
392         (void) bs;
393         (void) dbg;
394 #if 0
395         for(i = insn->use_start; i < insn->n_ops; ++i) {
396                 be_operand_t *op = &insn->ops[i];
397                 if(op->has_constraints && (values_interfere(env->lv, op->carrier, insn->irn) || arch_irn_is(aenv, op->carrier, ignore))) {
398                         bitset_copy(bs, op->regs);
399                         bitset_and(bs, out_constr);
400
401                         /*
402                                 The operand (interfering with the node) has input constraints
403                                 which also occur as output constraints, so insert a copy.
404                         */
405                         if(bitset_popcnt(bs) > 0) {
406                                 copy        = be_new_Copy(op->req.cls, env->irg, bl, op->carrier);
407                                 op->carrier = copy;
408                                 sched_add_before(insn->irn, copy);
409                                 set_irn_n(insn->irn, op->pos, op->carrier);
410
411                                 DBG((dbg, LEVEL_2, "adding copy for interfering and constrained op %+F\n", op->carrier));
412                         }
413                 }
414         }
415 #endif
416
417         /*
418                 Make the Perm, recompute liveness and re-scan the insn since the
419                 in operands are now the Projs of the Perm.
420         */
421         perm = insert_Perm_after(aenv, env->lv, env->cls, env->dom_front, sched_prev(insn->irn));
422
423         /* Registers are propagated by insert_Perm_after(). Clean them here! */
424         if(perm) {
425                 const ir_edge_t *edge;
426
427                 foreach_out_edge(perm, edge) {
428                         ir_node *proj = get_edge_src_irn(edge);
429                         arch_set_irn_register(aenv, proj, NULL);
430                 }
431
432                 /*
433                         We also have to re-build the insn since the input operands are now the Projs of
434                         the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
435                         the live sets may change.
436                 */
437                 // be_liveness_recompute(env->lv);
438                 obstack_free(&env->obst, insn);
439                 *the_insn = insn = chordal_scan_insn(env, insn->irn);
440
441                 /*
442                         Copy the input constraints of the insn to the Perm as output
443                         constraints. Succeeding phases (coalescing will need that).
444                 */
445                 for(i = insn->use_start; i < insn->n_ops; ++i) {
446                         be_operand_t *op = &insn->ops[i];
447                         ir_node *proj = op->carrier;
448                         /*
449                                 Note that the predecessor must not be a Proj of the Perm,
450                                 since ignore-nodes are not Perm'ed.
451                         */
452                         if(op->has_constraints &&  is_Proj(proj) && get_Proj_pred(proj) == perm) {
453                                 be_set_constr_limited(perm, BE_OUT_POS(get_Proj_proj(proj)), &op->req);
454                         }
455                 }
456         }
457
458         return perm;
459 }
460
461 static ir_node *handle_constraints(be_chordal_alloc_env_t *alloc_env, ir_node *irn, int *silent)
462 {
463         be_chordal_env_t *env  = alloc_env->chordal_env;
464         void *base             = obstack_base(&env->obst);
465         be_insn_t *insn        = chordal_scan_insn(env, irn);
466         ir_node *res           = insn->next_insn;
467         int be_silent          = *silent;
468
469         if(insn->pre_colored) {
470                 int i;
471                 for(i = 0; i < insn->use_start; ++i)
472                         pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
473         }
474
475         /*
476                 If the current node is a barrier toggle the silent flag.
477                 If we are in the start block, we are ought to be silent at the beginning,
478                 so the toggling activates the constraint handling but skips the barrier.
479                 If we are in the end block we handle the in requirements of the barrier
480                 and set the rest to silent.
481         */
482         if(be_is_Barrier(irn))
483                 *silent = !*silent;
484
485         if(be_silent)
486                 goto end;
487
488         /*
489                 Perms inserted before the constraint handling phase are considered to be
490                 correctly precolored. These Perms arise during the ABI handling phase.
491         */
492         if(insn->has_constraints) {
493                 const arch_env_t *aenv = env->birg->main_env->arch_env;
494                 int n_regs             = env->cls->n_regs;
495                 bitset_t *bs           = bitset_alloca(n_regs);
496                 ir_node **alloc_nodes  = alloca(n_regs * sizeof(alloc_nodes[0]));
497                 bipartite_t *bp        = bipartite_new(n_regs, n_regs);
498                 int *assignment        = alloca(n_regs * sizeof(assignment[0]));
499                 pmap *partners         = pmap_create();
500                 DEBUG_ONLY(firm_dbg_module_t *dbg = alloc_env->constr_dbg;)
501
502                 int i, n_alloc;
503                 long col;
504                 const ir_edge_t *edge;
505                 ir_node *perm = NULL;
506
507                 /*
508                         prepare the constraint handling of this node.
509                         Perms are constructed and Copies are created for constrained values
510                         interfering with the instruction.
511                 */
512                 perm = pre_process_constraints(alloc_env, &insn);
513
514                 /* find suitable in operands to the out operands of the node. */
515                 pair_up_operands(alloc_env, insn);
516
517                 /*
518                         look at the in/out operands and add each operand (and its possible partner)
519                         to a bipartite graph (left: nodes with partners, right: admissible colors).
520                 */
521                 for(i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
522                         be_operand_t *op = &insn->ops[i];
523
524                         /*
525                                 If the operand has no partner or the partner has not been marked
526                                 for allocation, determine the admissible registers and mark it
527                                 for allocation by associating the node and its partner with the
528                                 set of admissible registers via a bipartite graph.
529                         */
530                         if(!op->partner || !pmap_contains(partners, op->partner->carrier)) {
531
532                                 pmap_insert(partners, op->carrier, op->partner ? op->partner->carrier : NULL);
533                                 alloc_nodes[n_alloc] = op->carrier;
534
535                                 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, op->partner ? op->partner->carrier : NULL));
536
537                                 bitset_clear_all(bs);
538                                 get_decisive_partner_regs(bs, op, op->partner);
539
540                                 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
541
542                                 bitset_foreach(bs, col)
543                                         bipartite_add(bp, n_alloc, col);
544
545                                 n_alloc++;
546                         }
547                 }
548
549                 /*
550                         Put all nodes which live by the constrained instruction also to the
551                         allocation bipartite graph. They are considered unconstrained.
552                 */
553                 if(perm) {
554                         foreach_out_edge(perm, edge) {
555                                 ir_node *proj = get_edge_src_irn(edge);
556
557                                 assert(is_Proj(proj));
558
559                                 if(values_interfere(env->lv, proj, irn) && !pmap_contains(partners, proj)) {
560                                         assert(n_alloc < n_regs);
561                                         alloc_nodes[n_alloc] = proj;
562                                         pmap_insert(partners, proj, NULL);
563
564                                         bitset_clear_all(bs);
565                                         arch_put_non_ignore_regs(aenv, env->cls, bs);
566                                         bitset_andnot(bs, env->ignore_colors);
567                                         bitset_foreach(bs, col)
568                                                 bipartite_add(bp, n_alloc, col);
569
570                                         n_alloc++;
571                                 }
572                         }
573                 }
574
575                 /* Compute a valid register allocation. */
576                 bipartite_matching(bp, assignment);
577
578                 /* Assign colors obtained from the matching. */
579                 for(i = 0; i < n_alloc; ++i) {
580                         const arch_register_t *reg;
581                         ir_node *nodes[2];
582                         int j;
583
584                         assert(assignment[i] >= 0 && "there must have been a register assigned");
585                         reg = arch_register_for_index(env->cls, assignment[i]);
586
587                         nodes[0] = alloc_nodes[i];
588                         nodes[1] = pmap_get(partners, alloc_nodes[i]);
589
590                         for(j = 0; j < 2; ++j) {
591                                 if(!nodes[j])
592                                         continue;
593
594                                 arch_set_irn_register(aenv, nodes[j], reg);
595                                 pset_hinsert_ptr(alloc_env->pre_colored, nodes[j]);
596                                 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", nodes[j], reg->name));
597                         }
598                 }
599
600
601                 /* Allocate the non-constrained Projs of the Perm. */
602                 if(perm) {
603
604                         bitset_clear_all(bs);
605
606                         /* Put the colors of all Projs in a bitset. */
607                         foreach_out_edge(perm, edge) {
608                                 ir_node *proj              = get_edge_src_irn(edge);
609                                 const arch_register_t *reg = arch_get_irn_register(aenv, proj);
610
611                                 if(reg != NULL)
612                                         bitset_set(bs, reg->index);
613                         }
614
615                         /* Assign the not yet assigned Projs of the Perm a suitable color. */
616                         foreach_out_edge(perm, edge) {
617                                 ir_node *proj              = get_edge_src_irn(edge);
618                                 const arch_register_t *reg = arch_get_irn_register(aenv, proj);
619
620                                 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
621
622                                 if(reg == NULL) {
623                                         col = get_next_free_reg(alloc_env, bs);
624                                         reg = arch_register_for_index(env->cls, col);
625                                         bitset_set(bs, reg->index);
626                                         arch_set_irn_register(aenv, proj, reg);
627                                         pset_insert_ptr(alloc_env->pre_colored, proj);
628                                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
629                                 }
630                         }
631                 }
632
633                 bipartite_free(bp);
634                 pmap_destroy(partners);
635         }
636
637 end:
638         obstack_free(&env->obst, base);
639         return res;
640 }
641
642 /**
643  * Handle constraint nodes in each basic block.
644  * handle_constraints() inserts Perm nodes which perm
645  * over all values live at the constrained node right in front
646  * of the constrained node. These Perms signal a constrained node.
647  * For further comments, refer to handle_constraints().
648  */
649 static void constraints(ir_node *bl, void *data)
650 {
651         be_chordal_alloc_env_t *env = data;
652
653         /*
654                 Start silent in the start block.
655                 The silence remains until the first barrier is seen.
656                 Each other block is begun loud.
657         */
658         int silent                  = bl == get_irg_start_block(get_irn_irg(bl));
659         ir_node *irn;
660
661         /*
662                 If the block is the start block search the barrier and
663                 start handling constraints from there.
664         */
665
666         for(irn = sched_first(bl); !sched_is_end(irn);) {
667                 irn = handle_constraints(env, irn, &silent);
668         }
669 }
670
671 /**
672  * Annotate the register pressure to the nodes and compute
673  * the liveness intervals.
674  * @param block The block to do it for.
675  * @param env_ptr The environment.
676  */
677 static void pressure(ir_node *block, void *env_ptr)
678 {
679 /* Convenience macro for a def */
680 #define border_def(irn, step, real) \
681         border_add(env, head, irn, step, pressure--, 1, real)
682
683 /* Convenience macro for a use */
684 #define border_use(irn, step, real) \
685         border_add(env, head, irn, step, ++pressure, 0, real)
686
687         be_chordal_alloc_env_t *alloc_env = env_ptr;
688         be_chordal_env_t *env             = alloc_env->chordal_env;
689         bitset_t *live                    = alloc_env->live;
690         ir_node *irn;
691         DEBUG_ONLY(firm_dbg_module_t *dbg            = env->dbg;)
692
693         int i, n;
694         unsigned step = 0;
695         unsigned pressure = 0;
696         struct list_head *head;
697         pset *live_in  = be_lv_pset_put_in(env->lv, block, pset_new_ptr_default());
698         pset *live_end = be_lv_pset_put_end(env->lv, block, pset_new_ptr_default());
699
700         DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
701         bitset_clear_all(live);
702
703         /* Set up the border list in the block info */
704         head = obstack_alloc(&env->obst, sizeof(*head));
705         INIT_LIST_HEAD(head);
706         assert(pmap_get(env->border_heads, block) == NULL);
707         pmap_insert(env->border_heads, block, head);
708
709         /*
710          * Make final uses of all values live out of the block.
711          * They are necessary to build up real intervals.
712          */
713         foreach_pset(live_end, irn) {
714                 if(has_reg_class(env, irn)) {
715                         DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
716                         bitset_set(live, get_irn_idx(irn));
717                         border_use(irn, step, 0);
718                 }
719         }
720         ++step;
721
722         /*
723          * Determine the last uses of a value inside the block, since they are
724          * relevant for the interval borders.
725          */
726         sched_foreach_reverse(block, irn) {
727                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
728                 DBG((dbg, LEVEL_2, "\tlive: %B\n", live));
729
730                 /*
731                  * If the node defines some value, which can put into a
732                  * register of the current class, make a border for it.
733                  */
734                 if(has_reg_class(env, irn)) {
735                         int nr = get_irn_idx(irn);
736
737                         bitset_clear(live, nr);
738                         border_def(irn, step, 1);
739                 }
740
741                 /*
742                  * If the node is no phi node we can examine the uses.
743                  */
744                 if(!is_Phi(irn)) {
745                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
746                                 ir_node *op = get_irn_n(irn, i);
747
748                                 if(has_reg_class(env, op)) {
749                                         int nr = get_irn_idx(op);
750                                         const char *msg = "-";
751
752                                         if(!bitset_is_set(live, nr)) {
753                                                 border_use(op, step, 1);
754                                                 bitset_set(live, nr);
755                                                 msg = "X";
756                                         }
757
758                                         DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
759                                 }
760                         }
761                 }
762                 ++step;
763         }
764
765         /*
766          * Add initial defs for all values live in.
767          */
768         foreach_pset(live_in, irn) {
769                 if(has_reg_class(env, irn)) {
770
771                         /* Mark the value live in. */
772                         bitset_set(live, get_irn_idx(irn));
773
774                         /* Add the def */
775                         border_def(irn, step, 0);
776                 }
777         }
778
779         del_pset(live_in);
780         del_pset(live_end);
781 }
782
783 static void assign(ir_node *block, void *env_ptr)
784 {
785         be_chordal_alloc_env_t *alloc_env = env_ptr;
786         be_chordal_env_t *env       = alloc_env->chordal_env;
787         bitset_t *live              = alloc_env->live;
788         bitset_t *colors            = alloc_env->colors;
789         bitset_t *in_colors         = alloc_env->in_colors;
790         const arch_env_t *arch_env  = env->birg->main_env->arch_env;
791         struct list_head *head      = get_block_border_head(env, block);
792         pset *live_in               = be_lv_pset_put_in(env->lv, block, pset_new_ptr_default());
793
794         const ir_node *irn;
795         border_t *b;
796         DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
797
798         bitset_clear_all(colors);
799         bitset_clear_all(live);
800         bitset_clear_all(in_colors);
801
802         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
803         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
804         list_for_each_entry(border_t, b, head, list) {
805                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
806                                         b->irn, get_irn_idx(b->irn)));
807         }
808
809         /*
810          * Add initial defs for all values live in.
811          * Since their colors have already been assigned (The dominators were
812          * allocated before), we have to mark their colors as used also.
813          */
814         foreach_pset(live_in, irn) {
815                 if(has_reg_class(env, irn)) {
816                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
817                         int col;
818
819                         assert(reg && "Node must have been assigned a register");
820                         col = arch_register_get_index(reg);
821
822                         DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
823
824                         /* Mark the color of the live in value as used. */
825                         bitset_set(colors, col);
826                         bitset_set(in_colors, col);
827
828                         /* Mark the value live in. */
829                         bitset_set(live, get_irn_idx(irn));
830                 }
831         }
832
833         /*
834          * Mind that the sequence
835          * of defs from back to front defines a perfect
836          * elimination order. So, coloring the definitions from first to last
837          * will work.
838          */
839         list_for_each_entry_reverse(border_t, b, head, list) {
840                 ir_node *irn = b->irn;
841                 int nr       = get_irn_idx(irn);
842                 int ignore   = arch_irn_is(arch_env, irn, ignore);
843
844                 /*
845                  * Assign a color, if it is a local def. Global defs already have a
846                  * color.
847                  */
848                 if(b->is_def && !be_is_live_in(env->lv, block, irn)) {
849                         const arch_register_t *reg;
850                         int col = NO_COLOR;
851
852                         if(pset_find_ptr(alloc_env->pre_colored, irn) || ignore) {
853                                 reg = arch_get_irn_register(arch_env, irn);
854                                 col = reg->index;
855                                 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
856                         }
857
858                         else {
859                                 col = get_next_free_reg(alloc_env, colors);
860                                 reg = arch_register_for_index(env->cls, col);
861                                 assert(arch_get_irn_register(arch_env, irn) == NULL && "This node must not have been assigned a register yet");
862                                 assert(!arch_register_type_is(reg, ignore) && "Must not assign ignore register");
863                         }
864
865                         bitset_set(colors, col);
866                         arch_set_irn_register(arch_env, irn, reg);
867
868                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", arch_register_get_name(reg), col, irn));
869
870                         assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
871                         bitset_set(live, nr);
872                 }
873
874                 /* Clear the color upon a use. */
875                 else if(!b->is_def) {
876                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
877                         int col;
878
879                         assert(reg && "Register must have been assigned");
880
881                         col = arch_register_get_index(reg);
882                         assert(bitset_is_set(live, nr) && "Cannot have a non live use");
883
884                         bitset_clear(colors, col);
885                         bitset_clear(live, nr);
886                 }
887         }
888
889         del_pset(live_in);
890 }
891
892 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
893 {
894         be_chordal_alloc_env_t env;
895         char buf[256];
896
897         int colors_n          = arch_register_class_n_regs(chordal_env->cls);
898         ir_graph *irg         = chordal_env->irg;
899
900
901         assure_doms(irg);
902
903         env.chordal_env   = chordal_env;
904         env.colors_n      = colors_n;
905         env.colors        = bitset_alloca(colors_n);
906         env.tmp_colors    = bitset_alloca(colors_n);
907         env.in_colors     = bitset_alloca(colors_n);
908         env.pre_colored   = pset_new_ptr_default();
909         FIRM_DBG_REGISTER(env.constr_dbg, "firm.be.chordal.constr");
910
911
912         /* Handle register targeting constraints */
913         dom_tree_walk_irg(irg, constraints, NULL, &env);
914
915         if(chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
916                 snprintf(buf, sizeof(buf), "-%s-constr", chordal_env->cls->name);
917                 be_dump(chordal_env->irg, buf, dump_ir_block_graph_sched);
918         }
919
920         be_numbering(irg);
921         env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
922
923         /* First, determine the pressure */
924         dom_tree_walk_irg(irg, pressure, NULL, &env);
925
926         /* Assign the colors */
927         dom_tree_walk_irg(irg, assign, NULL, &env);
928
929         be_numbering_done(irg);
930
931         if(chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
932                 plotter_t *plotter;
933                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
934                 plotter = new_plotter_ps(buf);
935                 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
936                 plotter_free(plotter);
937         }
938
939         bitset_free(env.live);
940         del_pset(env.pre_colored);
941 }