bechordal: Remove impossible case from get_decisive_partner_regs().
[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 #define DUMP_INTERVALS
70
71 typedef struct be_chordal_alloc_env_t {
72         be_chordal_env_t *chordal_env;
73
74         pset *pre_colored;     /**< Set of precolored nodes. */
75         bitset_t *live;        /**< A liveness bitset. */
76         bitset_t *tmp_colors;  /**< An auxiliary bitset which is as long as the number of colors in the class. */
77         bitset_t *colors;      /**< The color mask. */
78         bitset_t *in_colors;   /**< Colors used by live in values. */
79         int colors_n;          /**< The number of colors. */
80 } be_chordal_alloc_env_t;
81
82 static int get_next_free_reg(const be_chordal_alloc_env_t *alloc_env, bitset_t *colors)
83 {
84         bitset_t *tmp = alloc_env->tmp_colors;
85         bitset_copy(tmp, colors);
86         bitset_flip_all(tmp);
87         bitset_and(tmp, alloc_env->chordal_env->allocatable_regs);
88         return bitset_next_set(tmp, 0);
89 }
90
91 static bitset_t const *get_decisive_partner_regs(const be_operand_t *o1, const be_operand_t *o2)
92 {
93         if (!o2)
94                 return o1->regs;
95
96         assert(o1->req->cls == o2->req->cls);
97
98         if (bitset_contains(o1->regs, o2->regs)) {
99                 return o1->regs;
100         } else if (bitset_contains(o2->regs, o1->regs)) {
101                 return o2->regs;
102         } else {
103                 return NULL;
104         }
105 }
106
107 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
108 {
109         const be_chordal_env_t *env = alloc_env->chordal_env;
110         bitset_t               *bs  = bitset_alloca(env->cls->n_regs);
111         int                     i;
112         int                     j;
113
114         /*
115          * For each out operand, try to find an in operand which can be assigned the
116          * same register as the out operand.
117          */
118         for (j = 0; j < insn->use_start; ++j) {
119                 be_operand_t *smallest        = NULL;
120                 int           smallest_n_regs = env->cls->n_regs + 1;
121                 be_operand_t *out_op          = &insn->ops[j];
122
123                 /* Try to find an in operand which has ... */
124                 for (i = insn->use_start; i < insn->n_ops; ++i) {
125                         int           n_total;
126                         be_operand_t *op = &insn->ops[i];
127                         be_lv_t      *lv;
128
129                         if (op->partner != NULL)
130                                 continue;
131                         lv = be_get_irg_liveness(env->irg);
132                         if (be_values_interfere(lv, op->irn, op->carrier))
133                                 continue;
134
135                         bitset_copy(bs, op->regs);
136                         bitset_and(bs, out_op->regs);
137                         n_total = bitset_popcount(op->regs);
138
139                         if (!bitset_is_empty(bs) && n_total < smallest_n_regs) {
140                                 smallest        = op;
141                                 smallest_n_regs = n_total;
142                         }
143                 }
144
145                 if (smallest != NULL) {
146                         for (i = insn->use_start; i < insn->n_ops; ++i) {
147                                 if (insn->ops[i].carrier == smallest->carrier)
148                                         insn->ops[i].partner = out_op;
149                         }
150
151                         out_op->partner   = smallest;
152                         smallest->partner = out_op;
153                 }
154         }
155 }
156
157 static void handle_constraints(be_chordal_alloc_env_t *alloc_env,
158                                    ir_node *irn)
159 {
160         int n_regs;
161         ir_node **alloc_nodes;
162         //hungarian_problem_t *bp;
163         int *assignment;
164         pmap *partners;
165         int i, n_alloc;
166         ir_node *perm = NULL;
167         //int match_res, cost;
168         be_chordal_env_t *env  = alloc_env->chordal_env;
169         void *base             = obstack_base(env->obst);
170         be_insn_t *insn        = be_scan_insn(env, irn);
171         bipartite_t *bp;
172
173         if (insn->pre_colored) {
174                 int i;
175                 for (i = 0; i < insn->use_start; ++i)
176                         pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
177         }
178
179         /*
180          * Perms inserted before the constraint handling phase are considered to be
181          * correctly precolored. These Perms arise during the ABI handling phase.
182          */
183         if (!insn->has_constraints || is_Phi(irn))
184                 goto end;
185
186         n_regs      = env->cls->n_regs;
187         alloc_nodes = ALLOCAN(ir_node*, n_regs);
188         //bp          = hungarian_new(n_regs, n_regs, 2, HUNGARIAN_MATCH_PERFECT);
189         bp          = bipartite_new(n_regs, n_regs);
190         assignment  = ALLOCAN(int, n_regs);
191         partners    = pmap_create();
192
193         /*
194          * prepare the constraint handling of this node.
195          * Perms are constructed and Copies are created for constrained values
196          * interfering with the instruction.
197          */
198         perm = pre_process_constraints(alloc_env->chordal_env, &insn);
199
200         /* find suitable in operands to the out operands of the node. */
201         pair_up_operands(alloc_env, insn);
202
203         /*
204          * look at the in/out operands and add each operand (and its possible partner)
205          * to a bipartite graph (left: nodes with partners, right: admissible colors).
206          */
207         for (i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
208                 be_operand_t *op = &insn->ops[i];
209
210                 /*
211                  * If the operand has no partner or the partner has not been marked
212                  * for allocation, determine the admissible registers and mark it
213                  * for allocation by associating the node and its partner with the
214                  * set of admissible registers via a bipartite graph.
215                  */
216                 if (!op->partner || !pmap_contains(partners, op->partner->carrier)) {
217                         ir_node *partner = op->partner ? op->partner->carrier : NULL;
218                         int i;
219
220                         pmap_insert(partners, op->carrier, partner);
221                         if (partner != NULL)
222                                 pmap_insert(partners, partner, op->carrier);
223
224                         /* don't insert a node twice */
225                         for (i = 0; i < n_alloc; ++i) {
226                                 if (alloc_nodes[i] == op->carrier) {
227                                         break;
228                                 }
229                         }
230                         if (i < n_alloc)
231                                 continue;
232
233                         alloc_nodes[n_alloc] = op->carrier;
234
235                         DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier,
236                              partner));
237
238                         bitset_t const *const bs = get_decisive_partner_regs(op, op->partner);
239                         if (bs) {
240                                 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
241
242                                 bitset_foreach(bs, col) {
243                                         //hungarian_add(bp, n_alloc, col, 1);
244                                         bipartite_add(bp, n_alloc, col);
245                                 }
246                         } else {
247                                 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: none\n", op->carrier));
248                         }
249
250                         n_alloc++;
251                 }
252         }
253
254         /*
255          * Put all nodes which live through the constrained instruction also to the
256          * allocation bipartite graph. They are considered unconstrained.
257          */
258         if (perm != NULL) {
259                 foreach_out_edge(perm, edge) {
260                         int i;
261                         ir_node *proj = get_edge_src_irn(edge);
262                         be_lv_t *lv   = be_get_irg_liveness(env->irg);
263
264                         assert(is_Proj(proj));
265
266                         if (!be_values_interfere(lv, proj, irn)
267                             || pmap_contains(partners, proj))
268                                 continue;
269
270                         /* don't insert a node twice */
271                         for (i = 0; i < n_alloc; ++i) {
272                                 if (alloc_nodes[i] == proj) {
273                                         break;
274                                 }
275                         }
276                         if (i < n_alloc)
277                                 continue;
278
279
280                         assert(n_alloc < n_regs);
281
282                         alloc_nodes[n_alloc] = proj;
283                         pmap_insert(partners, proj, NULL);
284
285                         bitset_foreach(env->allocatable_regs, col) {
286                                 //hungarian_add(bp, n_alloc, col, 1);
287                                 bipartite_add(bp, n_alloc, col);
288                         }
289
290                         n_alloc++;
291                 }
292         }
293
294         /* Compute a valid register allocation. */
295 #if 0
296         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
297         match_res = hungarian_solve(bp, assignment, &cost, 1);
298         assert(match_res == 0 && "matching failed");
299 #else
300         /*bipartite_dump_f(stderr, bp);*/
301         bipartite_matching(bp, assignment);
302 #endif
303
304         /* Assign colors obtained from the matching. */
305         for (i = 0; i < n_alloc; ++i) {
306                 const arch_register_t *reg;
307                 ir_node *irn;
308
309                 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
310                 reg = arch_register_for_index(env->cls, assignment[i]);
311
312                 irn = alloc_nodes[i];
313                 if (irn != NULL) {
314                         arch_set_irn_register(irn, reg);
315                         (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
316                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
317                 }
318
319                 irn = pmap_get(ir_node, partners, alloc_nodes[i]);
320                 if (irn != NULL) {
321                         arch_set_irn_register(irn, reg);
322                         (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
323                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
324                 }
325         }
326
327         /* Allocate the non-constrained Projs of the Perm. */
328         if (perm != NULL) {
329                 /* Put the colors of all Projs in a bitset. */
330                 bitset_t *const bs = bitset_alloca(n_regs);
331                 foreach_out_edge(perm, edge) {
332                         ir_node *proj              = get_edge_src_irn(edge);
333                         const arch_register_t *reg = arch_get_irn_register(proj);
334
335                         if (reg != NULL)
336                                 bitset_set(bs, reg->index);
337                 }
338
339                 /* Assign the not yet assigned Projs of the Perm a suitable color. */
340                 foreach_out_edge(perm, edge) {
341                         ir_node *proj              = get_edge_src_irn(edge);
342                         const arch_register_t *reg = arch_get_irn_register(proj);
343
344                         DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
345
346                         if (reg == NULL) {
347                                 size_t const col = get_next_free_reg(alloc_env, bs);
348                                 reg = arch_register_for_index(env->cls, col);
349                                 bitset_set(bs, reg->index);
350                                 arch_set_irn_register(proj, reg);
351                                 pset_insert_ptr(alloc_env->pre_colored, proj);
352                                 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
353                         }
354                 }
355         }
356
357         bipartite_free(bp);
358         //hungarian_free(bp);
359         pmap_destroy(partners);
360
361 end:
362         obstack_free(env->obst, base);
363 }
364
365 /**
366  * Handle constraint nodes in each basic block.
367  * handle_constraints() inserts Perm nodes which perm
368  * over all values live at the constrained node right in front
369  * of the constrained node. These Perms signal a constrained node.
370  * For further comments, refer to handle_constraints().
371  */
372 static void constraints(ir_node *bl, void *data)
373 {
374         be_chordal_alloc_env_t *env    = (be_chordal_alloc_env_t*)data;
375         ir_node                *irn;
376
377         for (irn = sched_first(bl); !sched_is_end(irn);) {
378                 ir_node *const next = sched_next(irn);
379                 handle_constraints(env, irn);
380                 irn = next;
381         }
382 }
383
384 static void assign(ir_node *block, void *env_ptr)
385 {
386         be_chordal_alloc_env_t *alloc_env = (be_chordal_alloc_env_t*)env_ptr;
387         be_chordal_env_t *env       = alloc_env->chordal_env;
388         bitset_t *live              = alloc_env->live;
389         bitset_t *colors            = alloc_env->colors;
390         bitset_t *in_colors         = alloc_env->in_colors;
391         struct list_head *head      = get_block_border_head(env, block);
392         be_lv_t *lv                 = be_get_irg_liveness(env->irg);
393
394         bitset_clear_all(colors);
395         bitset_clear_all(live);
396         bitset_clear_all(in_colors);
397
398         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
399         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
400         foreach_border_head(head, b) {
401                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
402                                         b->irn, get_irn_idx(b->irn)));
403         }
404
405         /*
406          * Add initial defs for all values live in.
407          * Since their colors have already been assigned (The dominators were
408          * allocated before), we have to mark their colors as used also.
409          */
410         be_lv_foreach(lv, block, be_lv_state_in, irn) {
411                 if (has_reg_class(env, irn)) {
412                         const arch_register_t *reg = arch_get_irn_register(irn);
413
414                         assert(reg && "Node must have been assigned a register");
415                         DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
416
417                         /* Mark the color of the live in value as used. */
418                         int const col = reg->index;
419                         bitset_set(colors, col);
420                         bitset_set(in_colors, col);
421
422                         /* Mark the value live in. */
423                         bitset_set(live, get_irn_idx(irn));
424                 }
425         }
426
427         /*
428          * Mind that the sequence of defs from back to front defines a perfect
429          * elimination order. So, coloring the definitions from first to last
430          * will work.
431          */
432         foreach_border_head(head, b) {
433                 ir_node *irn = b->irn;
434                 int nr       = get_irn_idx(irn);
435                 int ignore   = arch_irn_is_ignore(irn);
436
437                 /*
438                  * Assign a color, if it is a local def. Global defs already have a
439                  * color.
440                  */
441                 if (b->is_def && !be_is_live_in(lv, block, irn)) {
442                         const arch_register_t *reg;
443                         int col;
444
445                         if (ignore || pset_find_ptr(alloc_env->pre_colored, irn)) {
446                                 reg = arch_get_irn_register(irn);
447                                 col = reg->index;
448                                 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
449                         } else {
450                                 col = get_next_free_reg(alloc_env, colors);
451                                 reg = arch_register_for_index(env->cls, col);
452                                 assert(arch_get_irn_register(irn) == NULL && "This node must not have been assigned a register yet");
453                         }
454
455                         bitset_set(colors, col);
456                         arch_set_irn_register(irn, reg);
457
458                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
459
460                         assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
461                         bitset_set(live, nr);
462                 } else if (!b->is_def) {
463                         /* Clear the color upon a use. */
464                         const arch_register_t *reg = arch_get_irn_register(irn);
465
466                         assert(reg && "Register must have been assigned");
467
468                         bitset_clear(colors, reg->index);
469                         bitset_clear(live, nr);
470                 }
471         }
472 }
473
474 static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
475 {
476         be_chordal_alloc_env_t env;
477         char buf[256];
478         const arch_register_class_t *cls = chordal_env->cls;
479
480         int       colors_n = arch_register_class_n_regs(cls);
481         ir_graph *irg      = chordal_env->irg;
482
483         be_assure_live_sets(irg);
484         assure_doms(irg);
485
486         env.chordal_env   = chordal_env;
487         env.colors_n      = colors_n;
488         env.colors        = bitset_alloca(colors_n);
489         env.tmp_colors    = bitset_alloca(colors_n);
490         env.in_colors     = bitset_alloca(colors_n);
491         env.pre_colored   = pset_new_ptr_default();
492
493         be_timer_push(T_SPLIT);
494
495         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPLIT) {
496                 snprintf(buf, sizeof(buf), "%s-split", chordal_env->cls->name);
497                 dump_ir_graph(chordal_env->irg, buf);
498         }
499
500         be_timer_pop(T_SPLIT);
501
502         be_timer_push(T_CONSTR);
503
504         /* Handle register targeting constraints */
505         dom_tree_walk_irg(irg, constraints, NULL, &env);
506
507         if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
508                 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
509                 dump_ir_graph(chordal_env->irg, buf);
510         }
511
512         be_timer_pop(T_CONSTR);
513
514         env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
515
516         /* First, determine the pressure */
517         dom_tree_walk_irg(irg, create_borders, NULL, env.chordal_env);
518
519         /* Assign the colors */
520         dom_tree_walk_irg(irg, assign, NULL, &env);
521
522         if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
523                 plotter_t *plotter;
524                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
525                 plotter = new_plotter_ps(buf);
526                 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
527                 plotter_free(plotter);
528         }
529
530         bitset_free(env.live);
531         del_pset(env.pre_colored);
532 }
533
534 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
535 void be_init_chordal(void)
536 {
537         static be_ra_chordal_coloring_t coloring = {
538                 be_ra_chordal_color
539         };
540         FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
541
542         be_register_chordal_coloring("default", &coloring);
543 }