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