becopyopt: Remove the unnecessary attribute name from struct copy_opt_t.
[libfirm] / ir / be / bechordal.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Chordal register allocation.
9  * @author      Sebastian Hack
10  * @date        08.12.2004
11  */
12 #include "config.h"
13
14 #include "bechordal_common.h"
15 #include "bechordal_draw.h"
16 #include "bechordal_t.h"
17 #include "beinsn_t.h"
18 #include "beintlive_t.h"
19 #include "beirg.h"
20 #include "bemodule.h"
21 #include "debug.h"
22 #include "irdump.h"
23
24 #define USE_HUNGARIAN 0
25
26 #if USE_HUNGARIAN
27 #include "hungarian.h"
28 #else
29 #include "bipartite.h"
30 #endif
31
32 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
33
34 static int get_next_free_reg(bitset_t *const available)
35 {
36         return bitset_next_set(available, 0);
37 }
38
39 static unsigned const *get_decisive_partner_regs(be_operand_t const *const o1, size_t const n_regs)
40 {
41         be_operand_t const *const o2 = o1->partner;
42         if (!o2 || rbitset_contains(o1->regs, o2->regs, n_regs)) {
43                 return o1->regs;
44         } else if (rbitset_contains(o2->regs, o1->regs, n_regs)) {
45                 return o2->regs;
46         } else {
47                 return NULL;
48         }
49 }
50
51 static void pair_up_operands(be_chordal_env_t const *const env, be_insn_t *const insn)
52 {
53         /* For each out operand, try to find an in operand which can be assigned the
54          * same register as the out operand. */
55         int       const n_regs = env->cls->n_regs;
56         unsigned *const bs     = rbitset_alloca(n_regs);
57         be_lv_t  *const lv     = be_get_irg_liveness(env->irg);
58         for (int j = 0; j < insn->use_start; ++j) {
59                 /* Try to find an in operand which has ... */
60                 be_operand_t       *smallest        = NULL;
61                 int                 smallest_n_regs = n_regs + 1;
62                 be_operand_t *const out_op          = &insn->ops[j];
63                 for (int i = insn->use_start; i < insn->n_ops; ++i) {
64                         be_operand_t *const op = &insn->ops[i];
65                         if (op->partner || be_values_interfere(lv, insn->irn, op->carrier))
66                                 continue;
67
68                         rbitset_copy(bs, op->regs, n_regs);
69                         rbitset_and(bs, out_op->regs, n_regs);
70                         int const n_total = rbitset_popcount(op->regs, n_regs);
71                         if (!rbitset_is_empty(bs, n_regs) && n_total < smallest_n_regs) {
72                                 smallest        = op;
73                                 smallest_n_regs = n_total;
74                         }
75                 }
76
77                 if (smallest != NULL) {
78                         for (int i = insn->use_start; i < insn->n_ops; ++i) {
79                                 if (insn->ops[i].carrier == smallest->carrier)
80                                         insn->ops[i].partner = out_op;
81                         }
82
83                         out_op->partner   = smallest;
84                         smallest->partner = out_op;
85                 }
86         }
87 }
88
89 static bool list_contains_irn(ir_node *const *const list, size_t const n, ir_node *const irn)
90 {
91         for (ir_node *const *i = list; i != list + n; ++i) {
92                 if (*i == irn)
93                         return true;
94         }
95         return false;
96 }
97
98 static void handle_constraints(be_chordal_env_t *const env, ir_node *const irn)
99 {
100         void *const base = obstack_base(&env->obst);
101         be_insn_t  *insn = be_scan_insn(env, irn);
102
103         /* Perms inserted before the constraint handling phase are considered to be
104          * correctly precolored. These Perms arise during the ABI handling phase. */
105         if (!insn || is_Phi(irn))
106                 goto end;
107
108         /* Prepare the constraint handling of this node.
109          * Perms are constructed and Copies are created for constrained values
110          * interfering with the instruction. */
111         ir_node *const perm = pre_process_constraints(env, &insn);
112
113         /* find suitable in operands to the out operands of the node. */
114         pair_up_operands(env, insn);
115
116         /* Look at the in/out operands and add each operand (and its possible partner)
117          * to a bipartite graph (left: nodes with partners, right: admissible colors). */
118         int                        n_alloc     = 0;
119         int                  const n_regs      = env->cls->n_regs;
120         ir_node            **const alloc_nodes = ALLOCAN(ir_node*, n_regs);
121         pmap                *const partners    = pmap_create();
122 #if USE_HUNGARIAN
123         hungarian_problem_t *const bp          = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
124 #else
125         bipartite_t         *const bp          = bipartite_new(n_regs, n_regs);
126 #endif
127         for (int i = 0; i < insn->n_ops; ++i) {
128                 /* If the operand has no partner or the partner has not been marked
129                  * for allocation, determine the admissible registers and mark it
130                  * for allocation by associating the node and its partner with the
131                  * set of admissible registers via a bipartite graph. */
132                 be_operand_t *const op = &insn->ops[i];
133                 if (op->partner && pmap_contains(partners, op->partner->carrier))
134                         continue;
135
136                 ir_node *const partner = op->partner ? op->partner->carrier : NULL;
137                 pmap_insert(partners, op->carrier, partner);
138                 if (partner != NULL)
139                         pmap_insert(partners, partner, op->carrier);
140
141                 /* Don't insert a node twice. */
142                 if (list_contains_irn(alloc_nodes, n_alloc, op->carrier))
143                         continue;
144
145                 alloc_nodes[n_alloc] = op->carrier;
146
147                 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, partner));
148
149                 unsigned const *const bs = get_decisive_partner_regs(op, n_regs);
150                 if (bs) {
151                         DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
152
153                         rbitset_foreach(bs, n_regs, col) {
154 #if USE_HUNGARIAN
155                                 hungarian_add(bp, n_alloc, col, 1);
156 #else
157                                 bipartite_add(bp, n_alloc, col);
158 #endif
159                         }
160                 } else {
161                         DBG((dbg, LEVEL_2, "\tallowed registers for %+F: none\n", op->carrier));
162                 }
163
164                 n_alloc++;
165         }
166
167         /* Put all nodes which live through the constrained instruction also to the
168          * allocation bipartite graph. They are considered unconstrained. */
169         if (perm != NULL) {
170                 be_lv_t *const lv = be_get_irg_liveness(env->irg);
171                 foreach_out_edge(perm, edge) {
172                         ir_node *const proj = get_edge_src_irn(edge);
173                         assert(is_Proj(proj));
174
175                         if (!be_values_interfere(lv, proj, irn) || pmap_contains(partners, proj))
176                                 continue;
177
178                         /* Don't insert a node twice. */
179                         if (list_contains_irn(alloc_nodes, n_alloc, proj))
180                                 continue;
181
182                         assert(n_alloc < n_regs);
183
184                         alloc_nodes[n_alloc] = proj;
185                         pmap_insert(partners, proj, NULL);
186
187                         bitset_foreach(env->allocatable_regs, col) {
188 #if USE_HUNGARIAN
189                                 hungarian_add(bp, n_alloc, col, 1);
190 #else
191                                 bipartite_add(bp, n_alloc, col);
192 #endif
193                         }
194
195                         n_alloc++;
196                 }
197         }
198
199         /* Compute a valid register allocation. */
200         int *const assignment = ALLOCAN(int, n_regs);
201 #if USE_HUNGARIAN
202         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
203         int const match_res = hungarian_solve(bp, assignment, NULL, 1);
204         assert(match_res == 0 && "matching failed");
205 #else
206         bipartite_matching(bp, assignment);
207 #endif
208
209         /* Assign colors obtained from the matching. */
210         for (int i = 0; i < n_alloc; ++i) {
211                 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
212                 arch_register_t const *const reg = arch_register_for_index(env->cls, assignment[i]);
213
214                 ir_node *const irn = alloc_nodes[i];
215                 if (irn != NULL) {
216                         arch_set_irn_register(irn, reg);
217                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
218                 }
219
220                 ir_node *const partner = pmap_get(ir_node, partners, alloc_nodes[i]);
221                 if (partner != NULL) {
222                         arch_set_irn_register(partner, reg);
223                         DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", partner, reg->name));
224                 }
225         }
226
227         /* Allocate the non-constrained Projs of the Perm. */
228         if (perm != NULL) {
229                 bitset_t *const available = bitset_alloca(n_regs);
230                 bitset_copy(available, env->allocatable_regs);
231
232                 /* Put the colors of all Projs in a bitset. */
233                 foreach_out_edge(perm, edge) {
234                         ir_node               *const proj = get_edge_src_irn(edge);
235                         arch_register_t const *const reg  = arch_get_irn_register(proj);
236                         if (reg != NULL)
237                                 bitset_clear(available, reg->index);
238                 }
239
240                 /* Assign the not yet assigned Projs of the Perm a suitable color. */
241                 foreach_out_edge(perm, edge) {
242                         ir_node               *const proj = get_edge_src_irn(edge);
243                         arch_register_t const *const reg  = arch_get_irn_register(proj);
244
245                         DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
246
247                         if (reg == NULL) {
248                                 size_t const col = get_next_free_reg(available);
249                                 arch_register_t const *const new_reg = arch_register_for_index(env->cls, col);
250                                 bitset_clear(available, new_reg->index);
251                                 arch_set_irn_register(proj, new_reg);
252                                 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, new_reg->name));
253                         }
254                 }
255         }
256
257 #if USE_HUNGARIAN
258         hungarian_free(bp);
259 #else
260         bipartite_free(bp);
261 #endif
262         pmap_destroy(partners);
263
264 end:
265         obstack_free(&env->obst, base);
266 }
267
268 /**
269  * Handle constraint nodes in each basic block.
270  * handle_constraints() inserts Perm nodes which perm
271  * over all values live at the constrained node right in front
272  * of the constrained node. These Perms signal a constrained node.
273  * For further comments, refer to handle_constraints().
274  */
275 static void constraints(ir_node *const bl, void *const data)
276 {
277         be_chordal_env_t *const env = (be_chordal_env_t*)data;
278         sched_foreach_safe(bl, irn) {
279                 handle_constraints(env, irn);
280         }
281 }
282
283 static void assign(ir_node *const block, void *const env_ptr)
284 {
285         be_chordal_env_t *const env  = (be_chordal_env_t*)env_ptr;
286         struct list_head *const head = get_block_border_head(env, block);
287         be_lv_t          *const lv   = be_get_irg_liveness(env->irg);
288
289         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
290         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
291         foreach_border_head(head, b) {
292                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
293                                         b->irn, get_irn_idx(b->irn)));
294         }
295
296         bitset_t *const available = bitset_alloca(env->allocatable_regs->size);
297         bitset_copy(available, env->allocatable_regs);
298
299         /* Add initial defs for all values live in.
300          * Since their colors have already been assigned (The dominators were
301          * allocated before), we have to mark their colors as used also. */
302         be_lv_foreach_cls(lv, block, be_lv_state_in, env->cls, irn) {
303                 arch_register_t const *const reg = arch_get_irn_register(irn);
304
305                 assert(reg && "Node must have been assigned a register");
306                 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
307
308                 /* Mark the color of the live in value as used. */
309                 bitset_clear(available, reg->index);
310         }
311
312         /* Mind that the sequence of defs from back to front defines a perfect
313          * elimination order. So, coloring the definitions from first to last
314          * will work. */
315         foreach_border_head(head, b) {
316                 ir_node *const irn = b->irn;
317
318                 /* Assign a color, if it is a local def. Global defs already have a
319                  * color. */
320                 if (!b->is_def) {
321                         /* Make the color available upon a use. */
322                         arch_register_t const *const reg = arch_get_irn_register(irn);
323                         assert(reg && "Register must have been assigned");
324                         bitset_set(available, reg->index);
325                 } else if (!be_is_live_in(lv, block, irn)) {
326                         int                    col;
327                         arch_register_t const *reg = arch_get_irn_register(irn);
328                         if (reg) {
329                                 col = reg->index;
330                                 assert(bitset_is_set(available, col) && "pre-colored register must be free");
331                         } else {
332                                 assert(!arch_irn_is_ignore(irn));
333                                 col = get_next_free_reg(available);
334                                 reg = arch_register_for_index(env->cls, col);
335                                 arch_set_irn_register(irn, reg);
336                         }
337                         bitset_clear(available, col);
338
339                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
340                 }
341         }
342 }
343
344 static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
345 {
346         char            buf[256];
347         ir_graph *const irg = chordal_env->irg;
348         be_assure_live_sets(irg);
349         assure_doms(irg);
350
351         be_timer_push(T_CONSTR);
352
353         /* Handle register targeting constraints */
354         dom_tree_walk_irg(irg, constraints, NULL, chordal_env);
355
356         if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
357                 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
358                 dump_ir_graph(irg, buf);
359         }
360
361         be_timer_pop(T_CONSTR);
362
363         /* First, determine the pressure */
364         dom_tree_walk_irg(irg, create_borders, NULL, chordal_env);
365
366         /* Assign the colors */
367         dom_tree_walk_irg(irg, assign, NULL, chordal_env);
368
369         if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
370                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
371                 plotter_t *const plotter = new_plotter_ps(buf);
372                 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
373                 plotter_free(plotter);
374         }
375 }
376
377 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
378 void be_init_chordal(void)
379 {
380         static be_ra_chordal_coloring_t coloring = {
381                 be_ra_chordal_color
382         };
383         FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
384
385         be_register_chordal_coloring("default", &coloring);
386 }