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