9a7066d15e16107166853c20eb5352f59c70b9be
[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_has_reg_class(arch_env, irn, i, env->cls)) {
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                         ir_node *proj = insn->ops[i].carrier;
459                         /*
460                                 Note that the predecessor must not be a Proj of the Perm,
461                                 since ignore-nodes are not Perm'ed.
462                         */
463                         if(is_Proj(proj) && get_Proj_pred(proj) == perm) {
464                                 be_set_constr_limited(perm, BE_OUT_POS(get_Proj_proj(proj)), &insn->ops[i].req);
465                         }
466                 }
467         }
468
469         return perm;
470 }
471
472 static ir_node *handle_constraints(be_chordal_alloc_env_t *alloc_env, ir_node *irn)
473 {
474         be_chordal_env_t *env  = alloc_env->chordal_env;
475         void *base             = obstack_base(&env->obst);
476         insn_t *insn           = scan_insn(env, irn, &env->obst);
477         ir_node *res           = insn->next_insn;
478
479         if(insn->pre_colored) {
480                 int i;
481                 for(i = 0; i < insn->use_start; ++i)
482                         pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
483         }
484
485         if(be_is_Perm(irn) || be_is_RegParams(irn) || (be_is_Barrier(irn) && !insn->in_constraints))
486                 goto end;
487
488         /*
489                 Perms inserted before the constraint handling phase are considered to be
490                 correctly precolored. These Perms arise during the ABI handling phase.
491         */
492         if(insn->has_constraints) {
493                 firm_dbg_module_t *dbg = alloc_env->constr_dbg;
494                 const arch_env_t *aenv = env->birg->main_env->arch_env;
495                 int n_regs             = env->cls->n_regs;
496                 bitset_t *bs           = bitset_alloca(n_regs);
497                 bitset_t *non_ignore   = bitset_alloca(n_regs);
498                 ir_node **alloc_nodes  = alloca(n_regs * sizeof(alloc_nodes[0]));
499                 bipartite_t *bp        = bipartite_new(n_regs, n_regs);
500                 int *assignment        = alloca(n_regs * sizeof(assignment[0]));
501                 pmap *partners         = pmap_create();
502
503                 int i, n_alloc;
504                 long col;
505                 const ir_edge_t *edge;
506                 ir_node *perm = NULL;
507
508                 /*
509                         prepare the constraint handling of this node.
510                         Perms are constructed and Copies are created for constrained values
511                         interfering with the instruction.
512                 */
513                 perm = pre_process_constraints(alloc_env, &insn);
514
515                 /* find suitable in operands to the out operands of the node. */
516                 pair_up_operands(alloc_env, insn);
517
518                 /*
519                         look at the in/out operands and add each operand (and its possible partner)
520                         to a bipartite graph (left: nodes with partners, right: admissible colors).
521                 */
522                 for(i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
523                         operand_t *op = &insn->ops[i];
524
525                         /*
526                                 If the operand has no partner or the partner has not been marked
527                                 for allocation, determine the admissible registers and mark it
528                                 for allocation by associating the node and its partner with the
529                                 set of admissible registers via a bipartite graph.
530                         */
531                         if(!op->partner || !pmap_contains(partners, op->partner->carrier)) {
532
533                                 pmap_insert(partners, op->carrier, op->partner ? op->partner->carrier : NULL);
534                                 alloc_nodes[n_alloc] = op->carrier;
535
536                                 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, op->partner ? op->partner->carrier : NULL));
537
538                                 bitset_clear_all(bs);
539                                 get_decisive_partner_regs(bs, op, op->partner);
540
541                                 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
542
543                                 bitset_foreach(bs, col)
544                                         bipartite_add(bp, n_alloc, col);
545
546                                 n_alloc++;
547                         }
548                 }
549
550                 /*
551                         Put all nodes which live by the constrained instruction also to the
552                         allocation bipartite graph. They are considered unconstrained.
553                 */
554                 if(perm) {
555                         foreach_out_edge(perm, edge) {
556                                 ir_node *proj = get_edge_src_irn(edge);
557
558                                 assert(is_Proj(proj));
559
560                                 if(values_interfere(proj, irn)) {
561                                         assert(n_alloc < n_regs);
562                                         alloc_nodes[n_alloc] = proj;
563                                         pmap_insert(partners, proj, NULL);
564
565                                         bitset_clear_all(bs);
566                                         arch_put_non_ignore_regs(aenv, env->cls, bs);
567                                         bitset_foreach(bs, col)
568                                                 bipartite_add(bp, n_alloc, col);
569
570                                         n_alloc++;
571                                 }
572                         }
573                 }
574
575                 /* Compute a valid register allocation. */
576                 bipartite_matching(bp, assignment);
577
578                 /* Assign colors obtained from the matching. */
579                 for(i = 0; i < n_alloc; ++i) {
580                         const arch_register_t *reg;
581                         ir_node *nodes[2];
582                         int j;
583
584                         assert(assignment[i] >= 0 && "there must have been a register assigned");
585                         reg = arch_register_for_index(env->cls, assignment[i]);
586
587                         nodes[0] = alloc_nodes[i];
588                         nodes[1] = pmap_get(partners, alloc_nodes[i]);
589
590                         for(j = 0; j < 2; ++j) {
591                                 if(!nodes[j])
592                                         continue;
593
594                                 arch_set_irn_register(aenv, nodes[j], reg);
595                                 pset_hinsert_ptr(alloc_env->pre_colored, nodes[j]);
596                                 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", nodes[j], reg->name));
597                         }
598                 }
599
600
601                 /* Allocate the non-constrained Projs of the Perm. */
602                 if(perm) {
603
604                         bitset_clear_all(bs);
605
606                         /* Put the colors of all Projs in a bitset. */
607                         foreach_out_edge(perm, edge) {
608                                 ir_node *proj              = get_edge_src_irn(edge);
609                                 const arch_register_t *reg = arch_get_irn_register(aenv, proj);
610
611                                 if(reg != NULL)
612                                         bitset_set(bs, reg->index);
613                         }
614
615                         /* Assign the not yet assigned Projs of the Perm a suitable color. */
616                         foreach_out_edge(perm, edge) {
617                                 ir_node *proj              = get_edge_src_irn(edge);
618                                 const arch_register_t *reg = arch_get_irn_register(aenv, proj);
619
620                                 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
621
622                                 if(reg == NULL) {
623                                         col = get_next_free_reg(alloc_env, bs);
624                                         reg = arch_register_for_index(env->cls, col);
625                                         bitset_set(bs, reg->index);
626                                         arch_set_irn_register(aenv, proj, reg);
627                                         pset_insert_ptr(alloc_env->pre_colored, proj);
628                                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
629                                 }
630                         }
631                 }
632
633                 pmap_destroy(partners);
634         }
635
636 end:
637         obstack_free(&env->obst, base);
638         return res;
639 }
640
641 /**
642  * Handle constraint nodes in each basic block.
643  * be_insert_constr_perms() inserts Perm nodes which perm
644  * over all values live at the constrained node right in front
645  * of the constrained node. These Perms signal a constrained node.
646  * For further comments, refer to handle_constraints_at_perm().
647  */
648 static void constraints(ir_node *bl, void *data)
649 {
650         firm_dbg_module_t *dbg      = firm_dbg_register("firm.be.chordal.constr");
651         be_chordal_alloc_env_t *env = data;
652         arch_env_t *arch_env        = env->chordal_env->birg->main_env->arch_env;
653         ir_node *irn;
654
655         for(irn = sched_first(bl); !sched_is_end(irn);) {
656                 irn = handle_constraints(env, irn);
657         }
658 }
659
660 /**
661  * Annotate the register pressure to the nodes and compute
662  * the liveness intervals.
663  * @param block The block to do it for.
664  * @param env_ptr The environment.
665  */
666 static void pressure(ir_node *block, void *env_ptr)
667 {
668 /* Convenience macro for a def */
669 #define border_def(irn, step, real) \
670         border_add(env, head, irn, step, pressure--, 1, real)
671
672 /* Convenience macro for a use */
673 #define border_use(irn, step, real) \
674         border_add(env, head, irn, step, ++pressure, 0, real)
675
676         be_chordal_alloc_env_t *alloc_env = env_ptr;
677         be_chordal_env_t *env             = alloc_env->chordal_env;
678         const arch_env_t *arch_env        = env->birg->main_env->arch_env;
679         bitset_t *live                    = alloc_env->live;
680         firm_dbg_module_t *dbg            = env->dbg;
681         ir_node *irn;
682
683         int i, n;
684         unsigned step = 0;
685         unsigned pressure = 0;
686         struct list_head *head;
687         pset *live_in = put_live_in(block, pset_new_ptr_default());
688         pset *live_end = put_live_end(block, pset_new_ptr_default());
689
690         DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
691         bitset_clear_all(live);
692
693         /* Set up the border list in the block info */
694         head = obstack_alloc(&env->obst, sizeof(*head));
695         INIT_LIST_HEAD(head);
696         assert(pmap_get(env->border_heads, block) == NULL);
697         pmap_insert(env->border_heads, block, head);
698
699         /*
700          * Make final uses of all values live out of the block.
701          * They are necessary to build up real intervals.
702          */
703         for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
704                 if(has_reg_class(env, irn)) {
705                         DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
706                         bitset_set(live, get_irn_graph_nr(irn));
707                         border_use(irn, step, 0);
708                 }
709         }
710         ++step;
711
712         /*
713          * Determine the last uses of a value inside the block, since they are
714          * relevant for the interval borders.
715          */
716         sched_foreach_reverse(block, irn) {
717                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
718                 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
719
720                 /*
721                  * If the node defines some value, which can put into a
722                  * register of the current class, make a border for it.
723                  */
724                 if(has_reg_class(env, irn)) {
725                         int nr = get_irn_graph_nr(irn);
726
727                         bitset_clear(live, nr);
728                         border_def(irn, step, 1);
729                 }
730
731                 /*
732                  * If the node is no phi node we can examine the uses.
733                  */
734                 if(!is_Phi(irn)) {
735                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
736                                 ir_node *op = get_irn_n(irn, i);
737
738                                 if(has_reg_class(env, op)) {
739                                         int nr = get_irn_graph_nr(op);
740
741                                         DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
742
743                                         if(!bitset_is_set(live, nr)) {
744                                                 border_use(op, step, 1);
745                                                 bitset_set(live, nr);
746                                         }
747                                 }
748                         }
749                 }
750                 ++step;
751         }
752
753         /*
754          * Add initial defs for all values live in.
755          */
756         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
757                 if(has_reg_class(env, irn)) {
758
759                         /* Mark the value live in. */
760                         bitset_set(live, get_irn_graph_nr(irn));
761
762                         /* Add the def */
763                         border_def(irn, step, 0);
764                 }
765         }
766
767
768   del_pset(live_in);
769   del_pset(live_end);
770 }
771
772 static void assign(ir_node *block, void *env_ptr)
773 {
774         be_chordal_alloc_env_t *alloc_env = env_ptr;
775         be_chordal_env_t *env       = alloc_env->chordal_env;
776         firm_dbg_module_t *dbg      = env->dbg;
777         bitset_t *live              = alloc_env->live;
778         bitset_t *colors            = alloc_env->colors;
779         bitset_t *in_colors         = alloc_env->in_colors;
780         const arch_env_t *arch_env  = env->birg->main_env->arch_env;
781
782         const ir_node *irn;
783         border_t *b;
784         struct list_head *head = get_block_border_head(env, block);
785         pset *live_in = put_live_in(block, pset_new_ptr_default());
786
787         bitset_clear_all(colors);
788         bitset_clear_all(live);
789         bitset_clear_all(in_colors);
790
791         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
792         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
793         list_for_each_entry(border_t, b, head, list) {
794                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
795                                         b->irn, get_irn_graph_nr(b->irn)));
796         }
797
798         /*
799          * Add initial defs for all values live in.
800          * Since their colors have already been assigned (The dominators were
801          * allocated before), we have to mark their colors as used also.
802          */
803         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
804                 if(has_reg_class(env, irn)) {
805                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
806                         int col;
807
808                         assert(reg && "Node must have been assigned a register");
809                         col = arch_register_get_index(reg);
810
811                         /* Mark the color of the live in value as used. */
812                         bitset_set(colors, col);
813                         bitset_set(in_colors, col);
814
815                         /* Mark the value live in. */
816                         bitset_set(live, get_irn_graph_nr(irn));
817                 }
818         }
819
820         /*
821          * Mind that the sequence
822          * of defs from back to front defines a perfect
823          * elimination order. So, coloring the definitions from first to last
824          * will work.
825          */
826         list_for_each_entry_reverse(border_t, b, head, list) {
827                 ir_node *irn = b->irn;
828                 int nr = get_irn_graph_nr(irn);
829
830                 /*
831                  * Assign a color, if it is a local def. Global defs already have a
832                  * color.
833                  */
834                 if(b->is_def && !is_live_in(block, irn)) {
835                         const arch_register_t *reg;
836                         int col = NO_COLOR;
837
838                         if(pset_find_ptr(alloc_env->pre_colored, irn)) {
839                                 reg = arch_get_irn_register(arch_env, irn);
840                                 col = reg->index;
841                                 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
842                         }
843
844                         else {
845                                 col = get_next_free_reg(alloc_env, colors);
846                                 reg = arch_register_for_index(env->cls, col);
847                                 assert(arch_get_irn_register(arch_env, irn) == NULL && "This node must not have been assigned a register yet");
848                         }
849
850                         bitset_set(colors, col);
851
852                         assert(!arch_register_type_is(reg, ignore) && "Must not assign ignore register");
853                         arch_set_irn_register(arch_env, irn, reg);
854
855                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
856             arch_register_get_name(reg), col, irn));
857
858                         assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
859                         bitset_set(live, nr);
860                 }
861
862                 /* Clear the color upon a use. */
863                 else if(!b->is_def) {
864                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
865                         int col;
866
867                         assert(reg && "Register must have been assigned");
868
869                         col = arch_register_get_index(reg);
870                         assert(bitset_is_set(live, nr) && "Cannot have a non live use");
871
872                         bitset_clear(colors, col);
873                         bitset_clear(live, nr);
874                 }
875         }
876
877         del_pset(live_in);
878 }
879
880 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
881 {
882         be_chordal_alloc_env_t env;
883         char buf[256];
884         int i;
885
886         int colors_n          = arch_register_class_n_regs(chordal_env->cls);
887         ir_graph *irg         = chordal_env->irg;
888
889
890         if(get_irg_dom_state(irg) != dom_consistent)
891                 compute_doms(irg);
892
893         env.chordal_env   = chordal_env;
894         env.colors_n      = colors_n;
895         env.colors        = bitset_malloc(colors_n);
896         env.in_colors     = bitset_malloc(colors_n);
897         env.ignore_regs   = bitset_malloc(colors_n);
898         env.pre_colored   = pset_new_ptr_default();
899         env.constr_dbg    = firm_dbg_register("firm.be.chordal.constr");
900
901         for(i = 0; i < colors_n; ++i)
902                 if(arch_register_type_is(&chordal_env->cls->regs[i], ignore))
903                         bitset_set(env.ignore_regs, i);
904
905         /* Handle register targeting constraints */
906         dom_tree_walk_irg(irg, constraints, NULL, &env);
907
908         if(chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
909                 snprintf(buf, sizeof(buf), "-%s-constr", chordal_env->cls->name);
910                 be_dump(chordal_env->irg, buf, dump_ir_block_graph_sched);
911         }
912
913         be_numbering(irg);
914         env.live = bitset_malloc(get_graph_node_count(chordal_env->irg));
915
916         /* First, determine the pressure */
917         dom_tree_walk_irg(irg, pressure, NULL, &env);
918
919         /* Assign the colors */
920         dom_tree_walk_irg(irg, assign, NULL, &env);
921
922         be_numbering_done(irg);
923
924         if(chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
925         plotter_t *plotter;
926                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
927         plotter = new_plotter_ps(buf);
928         draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
929         plotter_free(plotter);
930         }
931
932         free(env.live);
933         free(env.colors);
934         free(env.in_colors);
935         free(env.ignore_regs);
936         del_pset(env.pre_colored);
937 }