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