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