beirgmod: Do not set registers for the Perm results in insert_Perm_before() just...
[libfirm] / ir / be / bechordal.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Chordal register allocation.
23  * @author      Sebastian Hack
24  * @date        08.12.2004
25  */
26 #include "config.h"
27
28 #include <ctype.h>
29
30 #include "obst.h"
31 #include "pset.h"
32 #include "list.h"
33 #include "bitset.h"
34 #include "raw_bitset.h"
35 #include "bipartite.h"
36 #include "hungarian.h"
37
38 #include "irmode_t.h"
39 #include "irgraph_t.h"
40 #include "irprintf_t.h"
41 #include "irgwalk.h"
42 #include "irdump.h"
43 #include "irdom.h"
44 #include "irtools.h"
45 #include "debug.h"
46 #include "iredges.h"
47
48 #include "beutil.h"
49 #include "besched.h"
50 #include "besched.h"
51 #include "belive_t.h"
52 #include "benode.h"
53 #include "bearch.h"
54 #include "beirgmod.h"
55 #include "beifg.h"
56 #include "beinsn_t.h"
57 #include "statev_t.h"
58 #include "beirg.h"
59 #include "beintlive_t.h"
60 #include "bera.h"
61 #include "bechordal_t.h"
62 #include "bechordal_draw.h"
63 #include "bemodule.h"
64 #include "bearch.h"
65 #include "bechordal_common.h"
66
67 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
68
69 typedef struct be_chordal_alloc_env_t {
70         be_chordal_env_t *chordal_env;
71
72         bitset_t *live;        /**< A liveness bitset. */
73         bitset_t *tmp_colors;  /**< An auxiliary bitset which is as long as the number of colors in the class. */
74         bitset_t *colors;      /**< The color mask. */
75 } be_chordal_alloc_env_t;
76
77 static int get_next_free_reg(const be_chordal_alloc_env_t *alloc_env, bitset_t *colors)
78 {
79         bitset_t *tmp = alloc_env->tmp_colors;
80         bitset_copy(tmp, colors);
81         bitset_flip_all(tmp);
82         bitset_and(tmp, alloc_env->chordal_env->allocatable_regs);
83         return bitset_next_set(tmp, 0);
84 }
85
86 static bitset_t const *get_decisive_partner_regs(be_operand_t const *const o1)
87 {
88         be_operand_t const *const o2 = o1->partner;
89         assert(!o2 || o1->req->cls == o2->req->cls);
90
91         if (!o2 || bitset_contains(o1->regs, o2->regs)) {
92                 return o1->regs;
93         } else if (bitset_contains(o2->regs, o1->regs)) {
94                 return o2->regs;
95         } else {
96                 return NULL;
97         }
98 }
99
100 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
101 {
102         const be_chordal_env_t *env = alloc_env->chordal_env;
103         bitset_t               *bs  = bitset_alloca(env->cls->n_regs);
104         int                     i;
105         int                     j;
106
107         /*
108          * For each out operand, try to find an in operand which can be assigned the
109          * same register as the out operand.
110          */
111         for (j = 0; j < insn->use_start; ++j) {
112                 be_operand_t *smallest        = NULL;
113                 int           smallest_n_regs = env->cls->n_regs + 1;
114                 be_operand_t *out_op          = &insn->ops[j];
115
116                 /* Try to find an in operand which has ... */
117                 for (i = insn->use_start; i < insn->n_ops; ++i) {
118                         int           n_total;
119                         be_operand_t *op = &insn->ops[i];
120                         be_lv_t      *lv;
121
122                         if (op->partner != NULL)
123                                 continue;
124                         lv = be_get_irg_liveness(env->irg);
125                         if (be_values_interfere(lv, op->irn, op->carrier))
126                                 continue;
127
128                         bitset_copy(bs, op->regs);
129                         bitset_and(bs, out_op->regs);
130                         n_total = bitset_popcount(op->regs);
131
132                         if (!bitset_is_empty(bs) && n_total < smallest_n_regs) {
133                                 smallest        = op;
134                                 smallest_n_regs = n_total;
135                         }
136                 }
137
138                 if (smallest != NULL) {
139                         for (i = insn->use_start; i < insn->n_ops; ++i) {
140                                 if (insn->ops[i].carrier == smallest->carrier)
141                                         insn->ops[i].partner = out_op;
142                         }
143
144                         out_op->partner   = smallest;
145                         smallest->partner = out_op;
146                 }
147         }
148 }
149
150 static void handle_constraints(be_chordal_alloc_env_t *alloc_env,
151                                    ir_node *irn)
152 {
153         int n_regs;
154         ir_node **alloc_nodes;
155         //hungarian_problem_t *bp;
156         int *assignment;
157         pmap *partners;
158         int i, n_alloc;
159         ir_node *perm = NULL;
160         //int match_res, cost;
161         be_chordal_env_t *env  = alloc_env->chordal_env;
162         void *base             = obstack_base(env->obst);
163         be_insn_t *insn        = be_scan_insn(env, irn);
164         bipartite_t *bp;
165
166         /*
167          * Perms inserted before the constraint handling phase are considered to be
168          * correctly precolored. These Perms arise during the ABI handling phase.
169          */
170         if (!insn->has_constraints || is_Phi(irn))
171                 goto end;
172
173         n_regs      = env->cls->n_regs;
174         alloc_nodes = ALLOCAN(ir_node*, n_regs);
175         //bp          = hungarian_new(n_regs, n_regs, 2, HUNGARIAN_MATCH_PERFECT);
176         bp          = bipartite_new(n_regs, n_regs);
177         assignment  = ALLOCAN(int, n_regs);
178         partners    = pmap_create();
179
180         /*
181          * prepare the constraint handling of this node.
182          * Perms are constructed and Copies are created for constrained values
183          * interfering with the instruction.
184          */
185         perm = pre_process_constraints(alloc_env->chordal_env, &insn);
186
187         /* find suitable in operands to the out operands of the node. */
188         pair_up_operands(alloc_env, insn);
189
190         /*
191          * look at the in/out operands and add each operand (and its possible partner)
192          * to a bipartite graph (left: nodes with partners, right: admissible colors).
193          */
194         for (i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
195                 be_operand_t *op = &insn->ops[i];
196
197                 /*
198                  * If the operand has no partner or the partner has not been marked
199                  * for allocation, determine the admissible registers and mark it
200                  * for allocation by associating the node and its partner with the
201                  * set of admissible registers via a bipartite graph.
202                  */
203                 if (!op->partner || !pmap_contains(partners, op->partner->carrier)) {
204                         ir_node *partner = op->partner ? op->partner->carrier : NULL;
205                         int i;
206
207                         pmap_insert(partners, op->carrier, partner);
208                         if (partner != NULL)
209                                 pmap_insert(partners, partner, op->carrier);
210
211                         /* don't insert a node twice */
212                         for (i = 0; i < n_alloc; ++i) {
213                                 if (alloc_nodes[i] == op->carrier) {
214                                         break;
215                                 }
216                         }
217                         if (i < n_alloc)
218                                 continue;
219
220                         alloc_nodes[n_alloc] = op->carrier;
221
222                         DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier,
223                              partner));
224
225                         bitset_t const *const bs = get_decisive_partner_regs(op);
226                         if (bs) {
227                                 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
228
229                                 bitset_foreach(bs, col) {
230                                         //hungarian_add(bp, n_alloc, col, 1);
231                                         bipartite_add(bp, n_alloc, col);
232                                 }
233                         } else {
234                                 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: none\n", op->carrier));
235                         }
236
237                         n_alloc++;
238                 }
239         }
240
241         /*
242          * Put all nodes which live through the constrained instruction also to the
243          * allocation bipartite graph. They are considered unconstrained.
244          */
245         if (perm != NULL) {
246                 foreach_out_edge(perm, edge) {
247                         int i;
248                         ir_node *proj = get_edge_src_irn(edge);
249                         be_lv_t *lv   = be_get_irg_liveness(env->irg);
250
251                         assert(is_Proj(proj));
252
253                         if (!be_values_interfere(lv, proj, irn)
254                             || pmap_contains(partners, proj))
255                                 continue;
256
257                         /* don't insert a node twice */
258                         for (i = 0; i < n_alloc; ++i) {
259                                 if (alloc_nodes[i] == proj) {
260                                         break;
261                                 }
262                         }
263                         if (i < n_alloc)
264                                 continue;
265
266
267                         assert(n_alloc < n_regs);
268
269                         alloc_nodes[n_alloc] = proj;
270                         pmap_insert(partners, proj, NULL);
271
272                         bitset_foreach(env->allocatable_regs, col) {
273                                 //hungarian_add(bp, n_alloc, col, 1);
274                                 bipartite_add(bp, n_alloc, col);
275                         }
276
277                         n_alloc++;
278                 }
279         }
280
281         /* Compute a valid register allocation. */
282 #if 0
283         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
284         match_res = hungarian_solve(bp, assignment, &cost, 1);
285         assert(match_res == 0 && "matching failed");
286 #else
287         /*bipartite_dump_f(stderr, bp);*/
288         bipartite_matching(bp, assignment);
289 #endif
290
291         /* Assign colors obtained from the matching. */
292         for (i = 0; i < n_alloc; ++i) {
293                 const arch_register_t *reg;
294                 ir_node *irn;
295
296                 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
297                 reg = arch_register_for_index(env->cls, assignment[i]);
298
299                 irn = alloc_nodes[i];
300                 if (irn != NULL) {
301                         arch_set_irn_register(irn, reg);
302                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
303                 }
304
305                 irn = pmap_get(ir_node, partners, alloc_nodes[i]);
306                 if (irn != NULL) {
307                         arch_set_irn_register(irn, reg);
308                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
309                 }
310         }
311
312         /* Allocate the non-constrained Projs of the Perm. */
313         if (perm != NULL) {
314                 /* Put the colors of all Projs in a bitset. */
315                 bitset_t *const bs = bitset_alloca(n_regs);
316                 foreach_out_edge(perm, edge) {
317                         ir_node *proj              = get_edge_src_irn(edge);
318                         const arch_register_t *reg = arch_get_irn_register(proj);
319
320                         if (reg != NULL)
321                                 bitset_set(bs, reg->index);
322                 }
323
324                 /* Assign the not yet assigned Projs of the Perm a suitable color. */
325                 foreach_out_edge(perm, edge) {
326                         ir_node *proj              = get_edge_src_irn(edge);
327                         const arch_register_t *reg = arch_get_irn_register(proj);
328
329                         DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
330
331                         if (reg == NULL) {
332                                 size_t const col = get_next_free_reg(alloc_env, bs);
333                                 reg = arch_register_for_index(env->cls, col);
334                                 bitset_set(bs, reg->index);
335                                 arch_set_irn_register(proj, reg);
336                                 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
337                         }
338                 }
339         }
340
341         bipartite_free(bp);
342         //hungarian_free(bp);
343         pmap_destroy(partners);
344
345 end:
346         obstack_free(env->obst, base);
347 }
348
349 /**
350  * Handle constraint nodes in each basic block.
351  * handle_constraints() inserts Perm nodes which perm
352  * over all values live at the constrained node right in front
353  * of the constrained node. These Perms signal a constrained node.
354  * For further comments, refer to handle_constraints().
355  */
356 static void constraints(ir_node *bl, void *data)
357 {
358         be_chordal_alloc_env_t *env    = (be_chordal_alloc_env_t*)data;
359         ir_node                *irn;
360
361         for (irn = sched_first(bl); !sched_is_end(irn);) {
362                 ir_node *const next = sched_next(irn);
363                 handle_constraints(env, irn);
364                 irn = next;
365         }
366 }
367
368 static void assign(ir_node *block, void *env_ptr)
369 {
370         be_chordal_alloc_env_t *alloc_env = (be_chordal_alloc_env_t*)env_ptr;
371         be_chordal_env_t *env       = alloc_env->chordal_env;
372         bitset_t *live              = alloc_env->live;
373         bitset_t *colors            = alloc_env->colors;
374         struct list_head *head      = get_block_border_head(env, block);
375         be_lv_t *lv                 = be_get_irg_liveness(env->irg);
376
377         bitset_clear_all(colors);
378         bitset_clear_all(live);
379
380         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
381         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
382         foreach_border_head(head, b) {
383                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
384                                         b->irn, get_irn_idx(b->irn)));
385         }
386
387         /*
388          * Add initial defs for all values live in.
389          * Since their colors have already been assigned (The dominators were
390          * allocated before), we have to mark their colors as used also.
391          */
392         be_lv_foreach(lv, block, be_lv_state_in, irn) {
393                 if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
394                         const arch_register_t *reg = arch_get_irn_register(irn);
395
396                         assert(reg && "Node must have been assigned a register");
397                         DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
398
399                         /* Mark the color of the live in value as used. */
400                         int const col = reg->index;
401                         bitset_set(colors, col);
402
403                         /* Mark the value live in. */
404                         bitset_set(live, get_irn_idx(irn));
405                 }
406         }
407
408         /*
409          * Mind that the sequence of defs from back to front defines a perfect
410          * elimination order. So, coloring the definitions from first to last
411          * will work.
412          */
413         foreach_border_head(head, b) {
414                 ir_node *irn = b->irn;
415                 int nr       = get_irn_idx(irn);
416
417                 /*
418                  * Assign a color, if it is a local def. Global defs already have a
419                  * color.
420                  */
421                 if (b->is_def && !be_is_live_in(lv, block, irn)) {
422                         int                    col;
423                         arch_register_t const *reg = arch_get_irn_register(irn);
424                         if (reg) {
425                                 col = reg->index;
426                                 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
427                         } else {
428                                 assert(!arch_irn_is_ignore(irn));
429                                 col = get_next_free_reg(alloc_env, colors);
430                                 reg = arch_register_for_index(env->cls, col);
431                                 arch_set_irn_register(irn, reg);
432                         }
433                         bitset_set(colors, col);
434
435                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
436
437                         assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
438                         bitset_set(live, nr);
439                 } else if (!b->is_def) {
440                         /* Clear the color upon a use. */
441                         const arch_register_t *reg = arch_get_irn_register(irn);
442
443                         assert(reg && "Register must have been assigned");
444
445                         bitset_clear(colors, reg->index);
446                         bitset_clear(live, nr);
447                 }
448         }
449 }
450
451 static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
452 {
453         be_chordal_alloc_env_t env;
454         char buf[256];
455         const arch_register_class_t *cls = chordal_env->cls;
456
457         int       colors_n = arch_register_class_n_regs(cls);
458         ir_graph *irg      = chordal_env->irg;
459
460         be_assure_live_sets(irg);
461         assure_doms(irg);
462
463         env.chordal_env   = chordal_env;
464         env.colors        = bitset_alloca(colors_n);
465         env.tmp_colors    = bitset_alloca(colors_n);
466
467         be_timer_push(T_SPLIT);
468
469         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPLIT) {
470                 snprintf(buf, sizeof(buf), "%s-split", chordal_env->cls->name);
471                 dump_ir_graph(chordal_env->irg, buf);
472         }
473
474         be_timer_pop(T_SPLIT);
475
476         be_timer_push(T_CONSTR);
477
478         /* Handle register targeting constraints */
479         dom_tree_walk_irg(irg, constraints, NULL, &env);
480
481         if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
482                 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
483                 dump_ir_graph(chordal_env->irg, buf);
484         }
485
486         be_timer_pop(T_CONSTR);
487
488         env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
489
490         /* First, determine the pressure */
491         dom_tree_walk_irg(irg, create_borders, NULL, env.chordal_env);
492
493         /* Assign the colors */
494         dom_tree_walk_irg(irg, assign, NULL, &env);
495
496         if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
497                 plotter_t *plotter;
498                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
499                 plotter = new_plotter_ps(buf);
500                 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
501                 plotter_free(plotter);
502         }
503
504         bitset_free(env.live);
505 }
506
507 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
508 void be_init_chordal(void)
509 {
510         static be_ra_chordal_coloring_t coloring = {
511                 be_ra_chordal_color
512         };
513         FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
514
515         be_register_chordal_coloring("default", &coloring);
516 }