values may die at every use
[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 #ifdef HAVE_MALLOC_H
15 #include <malloc.h>
16 #endif
17
18 #ifdef HAVE_ALLOCA_H
19 #include <alloca.h>
20 #endif
21
22 #include <ctype.h>
23
24 #include "obst.h"
25 #include "pset.h"
26 #include "list.h"
27 #include "bitset.h"
28 #include "iterator.h"
29 #include "bipartite.h"
30
31 #include "irmode_t.h"
32 #include "irgraph_t.h"
33 #include "irprintf_t.h"
34 #include "irgwalk.h"
35 #include "irdump.h"
36 #include "irdom.h"
37 #include "debug.h"
38 #include "xmalloc.h"
39
40 #include "beutil.h"
41 #include "besched.h"
42 #include "benumb_t.h"
43 #include "besched_t.h"
44 #include "belive_t.h"
45 #include "benode_t.h"
46 #include "bearch.h"
47 #include "beirgmod.h"
48 #include "beifg.h"
49 #include "beinsn_t.h"
50
51 #include "bechordal_t.h"
52 #include "bechordal_draw.h"
53
54 #define DBG_LEVEL SET_LEVEL_0
55 #define DBG_LEVEL_CHECK SET_LEVEL_0
56
57 #define NO_COLOR (-1)
58
59 #define MAX(x, y) ((x) > (y) ? (x) : (y))
60 #define MIN(x, y) ((x) < (y) ? (x) : (y))
61
62 #define DUMP_INTERVALS
63
64 typedef struct _be_chordal_alloc_env_t {
65         be_chordal_env_t *chordal_env;
66
67         pset *pre_colored;              /**< Set of precolored nodes. */
68         bitset_t *live;                             /**< A liveness bitset. */
69         bitset_t *tmp_colors;           /**< An auxiliary bitset which is as long as the number of colors in the class. */
70         bitset_t *colors;                           /**< The color mask. */
71         bitset_t *in_colors;            /**< Colors used by live in values. */
72         int colors_n;                   /**< The number of colors. */
73         DEBUG_ONLY(firm_dbg_module_t *constr_dbg;)  /**< Debug output for the constraint handler. */
74 } be_chordal_alloc_env_t;
75
76 #include "fourcc.h"
77
78 /* Make a fourcc for border checking. */
79 #define BORDER_FOURCC                           FOURCC('B', 'O', 'R', 'D')
80
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
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_alloc_env_t *env, ir_node *irn)
212 {
213         be_insn_env_t ie;
214
215         ie.ignore_colors = env->chordal_env->ignore_colors;
216         ie.aenv          = env->chordal_env->birg->main_env->arch_env;
217         ie.obst          = &env->chordal_env->obst;
218         ie.cls           = env->chordal_env->cls;
219         return be_scan_insn(&ie, irn);
220 }
221
222 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
223 {
224         const be_chordal_env_t *env = alloc_env->chordal_env;
225
226         int n_uses         = be_insn_n_uses(insn);
227         int n_defs         = be_insn_n_defs(insn);
228         bitset_t *bs       = bitset_alloca(env->cls->n_regs);
229         bipartite_t *bp    = bipartite_new(n_defs, n_uses);
230         int *pairing       = alloca(MAX(n_defs, n_uses) * sizeof(pairing[0]));
231
232         int i, j;
233
234         /*
235                 For each out operand, try to find an in operand which can be assigned the
236                 same register as the out operand.
237         */
238         for(j = 0; j < insn->use_start; ++j) {
239                 be_operand_t *out_op = &insn->ops[j];
240
241                 /* Try to find an in operand which has ... */
242                 for(i = insn->use_start; i < insn->n_ops; ++i) {
243                         const be_operand_t *op = &insn->ops[i];
244
245                         /*
246                         The in operand can only be paired with a def, if the node defining the
247                         operand's value does not interfere with the instruction itself. That
248                         would mean, that it is live at the instruction, so no result of the instruction
249                         can have the same register as the operand.
250
251                         Furthermore, tow operands can be paired, if the admissible registers
252                         of one are a subset of the other's. We record the operand whose constraints
253                         count in the decisive array.
254                         */
255                         if(!values_interfere(op->irn, op->carrier)) {
256                                 if(get_decisive_partner_regs(bs, out_op, op))
257                                         bipartite_add(bp, j, i - insn->use_start);
258                         }
259                 }
260         }
261
262         /* Compute the pairing. */
263         bipartite_matching(bp, pairing);
264         for(i = 0; i < insn->use_start; ++i) {
265                 int p = pairing[i] + insn->use_start;
266
267                 if(p >= insn->use_start) {
268                         insn->ops[i].partner = &insn->ops[p];
269                         insn->ops[p].partner = &insn->ops[i];
270                 }
271         }
272
273         bipartite_free(bp);
274 }
275
276
277 static ir_node *pre_process_constraints(be_chordal_alloc_env_t *alloc_env, be_insn_t **the_insn)
278 {
279         be_chordal_env_t *env       = alloc_env->chordal_env;
280         const arch_env_t *aenv      = env->birg->main_env->arch_env;
281         be_insn_t *insn             = *the_insn;
282         ir_node *bl                 = get_nodes_block(insn->irn);
283         ir_node *copy               = NULL;
284         ir_node *perm               = NULL;
285         bitset_t *out_constr        = bitset_alloca(env->cls->n_regs);
286         bitset_t *bs                = bitset_alloca(env->cls->n_regs);
287         DEBUG_ONLY(firm_dbg_module_t *dbg      = alloc_env->constr_dbg;)
288
289         int i;
290
291         assert(insn->has_constraints && "only do this for constrained nodes");
292
293         /*
294                 Collect all registers that occur in output constraints.
295                 This is necessary, since if the insn has one of these as an input constraint
296                 and the corresponding operand interferes with the insn, the operand must
297                 be copied.
298         */
299         for(i = 0; i < insn->use_start; ++i) {
300                 be_operand_t *op = &insn->ops[i];
301                 if(op->has_constraints)
302                         bitset_or(out_constr, op->regs);
303         }
304
305         /*
306                 Now, figure out which input operand must be copied since it has input
307                 constraints which are also output constraints.
308         */
309         for(i = insn->use_start; i < insn->n_ops; ++i) {
310                 be_operand_t *op = &insn->ops[i];
311                 if(op->has_constraints && (values_interfere(op->carrier, insn->irn) || arch_irn_is(aenv, op->carrier, ignore))) {
312                         bitset_copy(bs, op->regs);
313                         bitset_and(bs, out_constr);
314
315                         /*
316                                 The operand (interfering with the node) has input constraints
317                                 which also occur as output constraints, so insert a copy.
318                         */
319                         if(bitset_popcnt(bs) > 0) {
320                                 copy        = be_new_Copy(op->req.cls, env->irg, bl, op->carrier);
321                                 op->carrier = copy;
322                                 sched_add_before(insn->irn, copy);
323                                 set_irn_n(insn->irn, op->pos, op->carrier);
324
325                                 DBG((dbg, LEVEL_2, "adding copy for interfering and constrained op %+F\n", op->carrier));
326                         }
327                 }
328         }
329
330         /*
331                 Make the Perm, recompute liveness and re-scan the insn since the
332                 in operands are now the Projs of the Perm.
333         */
334         perm = insert_Perm_after(aenv, env->cls, env->dom_front, sched_prev(insn->irn));
335
336         /* Registers are propagated by insert_Perm_after(). Clean them here! */
337         if(perm) {
338                 const ir_edge_t *edge;
339
340                 foreach_out_edge(perm, edge) {
341                         ir_node *proj = get_edge_src_irn(edge);
342                         arch_set_irn_register(aenv, proj, NULL);
343                 }
344
345                 /*
346                         We also have to re-build the insn since the input operands are now the Projs of
347                         the Perm. Recomputing liveness is also a good idea if a Perm is inserted, since
348                         the live sets may change.
349                 */
350                 be_liveness(env->irg);
351                 obstack_free(&env->obst, insn);
352                 *the_insn = insn = chordal_scan_insn(alloc_env, insn->irn);
353
354                 /*
355                         Copy the input constraints of the insn to the Perm as output
356                         constraints. Succeeding phases (coalescing will need that).
357                 */
358                 for(i = insn->use_start; i < insn->n_ops; ++i) {
359                         be_operand_t *op = &insn->ops[i];
360                         ir_node *proj = op->carrier;
361                         /*
362                                 Note that the predecessor must not be a Proj of the Perm,
363                                 since ignore-nodes are not Perm'ed.
364                         */
365                         if(op->has_constraints &&  is_Proj(proj) && get_Proj_pred(proj) == perm) {
366                                 be_set_constr_limited(perm, BE_OUT_POS(get_Proj_proj(proj)), &op->req);
367                         }
368                 }
369         }
370
371         return perm;
372 }
373
374 static ir_node *handle_constraints(be_chordal_alloc_env_t *alloc_env, ir_node *irn, int *silent)
375 {
376         be_chordal_env_t *env  = alloc_env->chordal_env;
377         void *base             = obstack_base(&env->obst);
378         be_insn_t *insn        = chordal_scan_insn(alloc_env, irn);
379         ir_node *res           = insn->next_insn;
380         int be_silent          = *silent;
381
382         if(insn->pre_colored) {
383                 int i;
384                 for(i = 0; i < insn->use_start; ++i)
385                         pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
386         }
387
388         /*
389                 If the current node is a barrier toggle the silent flag.
390                 If we are in the start block, we are ought to be silent at the beginning,
391                 so the toggling activates the constraint handling but skips the barrier.
392                 If we are in the end block we handle the in requirements of the barrier
393                 and set the rest to silent.
394         */
395         if(be_is_Barrier(irn))
396                 *silent = !*silent;
397
398         if(be_silent)
399                 goto end;
400
401         /*
402                 Perms inserted before the constraint handling phase are considered to be
403                 correctly precolored. These Perms arise during the ABI handling phase.
404         */
405         if(insn->has_constraints) {
406                 const arch_env_t *aenv = env->birg->main_env->arch_env;
407                 int n_regs             = env->cls->n_regs;
408                 bitset_t *bs           = bitset_alloca(n_regs);
409                 ir_node **alloc_nodes  = alloca(n_regs * sizeof(alloc_nodes[0]));
410                 bipartite_t *bp        = bipartite_new(n_regs, n_regs);
411                 int *assignment        = alloca(n_regs * sizeof(assignment[0]));
412                 pmap *partners         = pmap_create();
413                 DEBUG_ONLY(firm_dbg_module_t *dbg = alloc_env->constr_dbg;)
414
415                 int i, n_alloc;
416                 long col;
417                 const ir_edge_t *edge;
418                 ir_node *perm = NULL;
419
420                 /*
421                         prepare the constraint handling of this node.
422                         Perms are constructed and Copies are created for constrained values
423                         interfering with the instruction.
424                 */
425                 perm = pre_process_constraints(alloc_env, &insn);
426
427                 /* find suitable in operands to the out operands of the node. */
428                 pair_up_operands(alloc_env, insn);
429
430                 /*
431                         look at the in/out operands and add each operand (and its possible partner)
432                         to a bipartite graph (left: nodes with partners, right: admissible colors).
433                 */
434                 for(i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
435                         be_operand_t *op = &insn->ops[i];
436
437                         /*
438                                 If the operand has no partner or the partner has not been marked
439                                 for allocation, determine the admissible registers and mark it
440                                 for allocation by associating the node and its partner with the
441                                 set of admissible registers via a bipartite graph.
442                         */
443                         if(!op->partner || !pmap_contains(partners, op->partner->carrier)) {
444
445                                 pmap_insert(partners, op->carrier, op->partner ? op->partner->carrier : NULL);
446                                 alloc_nodes[n_alloc] = op->carrier;
447
448                                 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, op->partner ? op->partner->carrier : NULL));
449
450                                 bitset_clear_all(bs);
451                                 get_decisive_partner_regs(bs, op, op->partner);
452
453                                 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
454
455                                 bitset_foreach(bs, col)
456                                         bipartite_add(bp, n_alloc, col);
457
458                                 n_alloc++;
459                         }
460                 }
461
462                 /*
463                         Put all nodes which live by the constrained instruction also to the
464                         allocation bipartite graph. They are considered unconstrained.
465                 */
466                 if(perm) {
467                         foreach_out_edge(perm, edge) {
468                                 ir_node *proj = get_edge_src_irn(edge);
469
470                                 assert(is_Proj(proj));
471
472                                 if(values_interfere(proj, irn) && !pmap_contains(partners, proj)) {
473                                         assert(n_alloc < n_regs);
474                                         alloc_nodes[n_alloc] = proj;
475                                         pmap_insert(partners, proj, NULL);
476
477                                         bitset_clear_all(bs);
478                                         arch_put_non_ignore_regs(aenv, env->cls, bs);
479                                         bitset_foreach(bs, col)
480                                                 bipartite_add(bp, n_alloc, col);
481
482                                         n_alloc++;
483                                 }
484                         }
485                 }
486
487                 /* Compute a valid register allocation. */
488                 bipartite_matching(bp, assignment);
489
490                 /* Assign colors obtained from the matching. */
491                 for(i = 0; i < n_alloc; ++i) {
492                         const arch_register_t *reg;
493                         ir_node *nodes[2];
494                         int j;
495
496                         assert(assignment[i] >= 0 && "there must have been a register assigned");
497                         reg = arch_register_for_index(env->cls, assignment[i]);
498
499                         nodes[0] = alloc_nodes[i];
500                         nodes[1] = pmap_get(partners, alloc_nodes[i]);
501
502                         for(j = 0; j < 2; ++j) {
503                                 if(!nodes[j])
504                                         continue;
505
506                                 arch_set_irn_register(aenv, nodes[j], reg);
507                                 pset_hinsert_ptr(alloc_env->pre_colored, nodes[j]);
508                                 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", nodes[j], reg->name));
509                         }
510                 }
511
512
513                 /* Allocate the non-constrained Projs of the Perm. */
514                 if(perm) {
515
516                         bitset_clear_all(bs);
517
518                         /* Put the colors of all Projs in a bitset. */
519                         foreach_out_edge(perm, edge) {
520                                 ir_node *proj              = get_edge_src_irn(edge);
521                                 const arch_register_t *reg = arch_get_irn_register(aenv, proj);
522
523                                 if(reg != NULL)
524                                         bitset_set(bs, reg->index);
525                         }
526
527                         /* Assign the not yet assigned Projs of the Perm a suitable color. */
528                         foreach_out_edge(perm, edge) {
529                                 ir_node *proj              = get_edge_src_irn(edge);
530                                 const arch_register_t *reg = arch_get_irn_register(aenv, proj);
531
532                                 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
533
534                                 if(reg == NULL) {
535                                         col = get_next_free_reg(alloc_env, bs);
536                                         reg = arch_register_for_index(env->cls, col);
537                                         bitset_set(bs, reg->index);
538                                         arch_set_irn_register(aenv, proj, reg);
539                                         pset_insert_ptr(alloc_env->pre_colored, proj);
540                                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
541                                 }
542                         }
543                 }
544
545                 bipartite_free(bp);
546                 pmap_destroy(partners);
547         }
548
549 end:
550         obstack_free(&env->obst, base);
551         return res;
552 }
553
554 /**
555  * Handle constraint nodes in each basic block.
556  * handle_constraints() inserts Perm nodes which perm
557  * over all values live at the constrained node right in front
558  * of the constrained node. These Perms signal a constrained node.
559  * For further comments, refer to handle_constraints().
560  */
561 static void constraints(ir_node *bl, void *data)
562 {
563         be_chordal_alloc_env_t *env = data;
564
565         /*
566                 Start silent in the start block.
567                 The silence remains until the first barrier is seen.
568                 Each other block is begun loud.
569         */
570         int silent                  = bl == get_irg_start_block(get_irn_irg(bl));
571         ir_node *irn;
572
573         /*
574                 If the block is the start block search the barrier and
575                 start handling constraints from there.
576         */
577
578         for(irn = sched_first(bl); !sched_is_end(irn);) {
579                 irn = handle_constraints(env, irn, &silent);
580         }
581 }
582
583 /**
584  * Annotate the register pressure to the nodes and compute
585  * the liveness intervals.
586  * @param block The block to do it for.
587  * @param env_ptr The environment.
588  */
589 static void pressure(ir_node *block, void *env_ptr)
590 {
591 /* Convenience macro for a def */
592 #define border_def(irn, step, real) \
593         border_add(env, head, irn, step, pressure--, 1, real)
594
595 /* Convenience macro for a use */
596 #define border_use(irn, step, real) \
597         border_add(env, head, irn, step, ++pressure, 0, real)
598
599         be_chordal_alloc_env_t *alloc_env = env_ptr;
600         be_chordal_env_t *env             = alloc_env->chordal_env;
601         bitset_t *live                    = alloc_env->live;
602         ir_node *irn;
603         DEBUG_ONLY(firm_dbg_module_t *dbg            = env->dbg;)
604
605         int i, n;
606         unsigned step = 0;
607         unsigned pressure = 0;
608         struct list_head *head;
609         pset *live_in  = put_live_in(block, pset_new_ptr_default());
610         pset *live_end = put_live_end(block, pset_new_ptr_default());
611
612         DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
613         bitset_clear_all(live);
614
615         /* Set up the border list in the block info */
616         head = obstack_alloc(&env->obst, sizeof(*head));
617         INIT_LIST_HEAD(head);
618         assert(pmap_get(env->border_heads, block) == NULL);
619         pmap_insert(env->border_heads, block, head);
620
621         /*
622          * Make final uses of all values live out of the block.
623          * They are necessary to build up real intervals.
624          */
625         foreach_pset(live_end, irn) {
626                 if(has_reg_class(env, irn)) {
627                         DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_idx(irn)));
628                         bitset_set(live, get_irn_idx(irn));
629                         border_use(irn, step, 0);
630                 }
631         }
632         ++step;
633
634         /*
635          * Determine the last uses of a value inside the block, since they are
636          * relevant for the interval borders.
637          */
638         sched_foreach_reverse(block, irn) {
639                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
640                 DBG((dbg, LEVEL_2, "\tlive: %B\n", live));
641
642                 /*
643                  * If the node defines some value, which can put into a
644                  * register of the current class, make a border for it.
645                  */
646                 if(has_reg_class(env, irn)) {
647                         int nr = get_irn_idx(irn);
648
649                         bitset_clear(live, nr);
650                         border_def(irn, step, 1);
651                 }
652
653                 /*
654                  * If the node is no phi node we can examine the uses.
655                  */
656                 if(!is_Phi(irn)) {
657                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
658                                 ir_node *op = get_irn_n(irn, i);
659
660                                 if(has_reg_class(env, op)) {
661                                         int nr = get_irn_idx(op);
662                                         const char *msg = "-";
663
664                                         if(!bitset_is_set(live, nr)) {
665                                                 border_use(op, step, 1);
666                                                 bitset_set(live, nr);
667                                                 msg = "X";
668                                         }
669
670                                         DBG((dbg, LEVEL_4, "\t\t%s pos: %d, use: %+F\n", msg, i, op));
671                                 }
672                         }
673                 }
674                 ++step;
675         }
676
677         /*
678          * Add initial defs for all values live in.
679          */
680         foreach_pset(live_in, irn) {
681                 if(has_reg_class(env, irn)) {
682
683                         /* Mark the value live in. */
684                         bitset_set(live, get_irn_idx(irn));
685
686                         /* Add the def */
687                         border_def(irn, step, 0);
688                 }
689         }
690
691         del_pset(live_in);
692         del_pset(live_end);
693 }
694
695 static void assign(ir_node *block, void *env_ptr)
696 {
697         be_chordal_alloc_env_t *alloc_env = env_ptr;
698         be_chordal_env_t *env       = alloc_env->chordal_env;
699         bitset_t *live              = alloc_env->live;
700         bitset_t *colors            = alloc_env->colors;
701         bitset_t *in_colors         = alloc_env->in_colors;
702         const arch_env_t *arch_env  = env->birg->main_env->arch_env;
703         struct list_head *head      = get_block_border_head(env, block);
704         pset *live_in               = put_live_in(block, pset_new_ptr_default());
705
706         const ir_node *irn;
707         border_t *b;
708         DEBUG_ONLY(firm_dbg_module_t *dbg = env->dbg;)
709
710         bitset_clear_all(colors);
711         bitset_clear_all(live);
712         bitset_clear_all(in_colors);
713
714         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
715         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
716         list_for_each_entry(border_t, b, head, list) {
717                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
718                                         b->irn, get_irn_idx(b->irn)));
719         }
720
721         /*
722          * Add initial defs for all values live in.
723          * Since their colors have already been assigned (The dominators were
724          * allocated before), we have to mark their colors as used also.
725          */
726         foreach_pset(live_in, irn) {
727                 if(has_reg_class(env, irn)) {
728                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
729                         int col;
730
731                         assert(reg && "Node must have been assigned a register");
732                         col = arch_register_get_index(reg);
733
734                         DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
735
736                         /* Mark the color of the live in value as used. */
737                         bitset_set(colors, col);
738                         bitset_set(in_colors, col);
739
740                         /* Mark the value live in. */
741                         bitset_set(live, get_irn_idx(irn));
742                 }
743         }
744
745         /*
746          * Mind that the sequence
747          * of defs from back to front defines a perfect
748          * elimination order. So, coloring the definitions from first to last
749          * will work.
750          */
751         list_for_each_entry_reverse(border_t, b, head, list) {
752                 ir_node *irn = b->irn;
753                 int nr       = get_irn_idx(irn);
754                 int ignore   = arch_irn_is(arch_env, irn, ignore);
755
756                 /*
757                  * Assign a color, if it is a local def. Global defs already have a
758                  * color.
759                  */
760                 if(b->is_def && !is_live_in(block, irn)) {
761                         const arch_register_t *reg;
762                         int col = NO_COLOR;
763
764                         if(pset_find_ptr(alloc_env->pre_colored, irn) || ignore) {
765                                 reg = arch_get_irn_register(arch_env, irn);
766                                 col = reg->index;
767                                 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
768                         }
769
770                         else {
771                                 col = get_next_free_reg(alloc_env, colors);
772                                 reg = arch_register_for_index(env->cls, col);
773                                 assert(arch_get_irn_register(arch_env, irn) == NULL && "This node must not have been assigned a register yet");
774                                 assert(!arch_register_type_is(reg, ignore) && "Must not assign ignore register");
775                         }
776
777                         bitset_set(colors, col);
778                         arch_set_irn_register(arch_env, irn, reg);
779
780                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", arch_register_get_name(reg), col, irn));
781
782                         assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
783                         bitset_set(live, nr);
784                 }
785
786                 /* Clear the color upon a use. */
787                 else if(!b->is_def) {
788                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
789                         int col;
790
791                         assert(reg && "Register must have been assigned");
792
793                         col = arch_register_get_index(reg);
794                         assert(bitset_is_set(live, nr) && "Cannot have a non live use");
795
796                         bitset_clear(colors, col);
797                         bitset_clear(live, nr);
798                 }
799         }
800
801         del_pset(live_in);
802 }
803
804 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
805 {
806         be_chordal_alloc_env_t env;
807         char buf[256];
808
809         int colors_n          = arch_register_class_n_regs(chordal_env->cls);
810         ir_graph *irg         = chordal_env->irg;
811
812
813         assure_doms(irg);
814
815         env.chordal_env   = chordal_env;
816         env.colors_n      = colors_n;
817         env.colors        = bitset_alloca(colors_n);
818         env.tmp_colors    = bitset_alloca(colors_n);
819         env.in_colors     = bitset_alloca(colors_n);
820         env.pre_colored   = pset_new_ptr_default();
821         FIRM_DBG_REGISTER(env.constr_dbg, "firm.be.chordal.constr");
822
823
824         /* Handle register targeting constraints */
825         dom_tree_walk_irg(irg, constraints, NULL, &env);
826
827         if(chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
828                 snprintf(buf, sizeof(buf), "-%s-constr", chordal_env->cls->name);
829                 be_dump(chordal_env->irg, buf, dump_ir_block_graph_sched);
830         }
831
832         be_numbering(irg);
833         env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
834
835         /* First, determine the pressure */
836         dom_tree_walk_irg(irg, pressure, NULL, &env);
837
838         /* Assign the colors */
839         dom_tree_walk_irg(irg, assign, NULL, &env);
840
841         be_numbering_done(irg);
842
843         if(chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
844                 plotter_t *plotter;
845                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
846                 plotter = new_plotter_ps(buf);
847                 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
848                 plotter_free(plotter);
849         }
850
851         bitset_free(env.live);
852         del_pset(env.pre_colored);
853 }