2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief PBQP based register allocation.
23 * @author Thomas Bersch
25 * @version $Id: bechordal.c 26750 2009-11-27 09:37:43Z bersch $
28 /* miscellaneous includes */
37 #include "iredges_t.h"
42 /* libfirm/ir/be includes */
47 #include "bechordal_common.h"
48 #include "bechordal.h"
49 #include "bechordal_t.h"
57 #include "becopyopt.h"
64 #include "heuristical_co.h"
66 #include "html_dumper.h"
67 #include "pbqp_node_t.h"
68 #include "pbqp_node.h"
73 static bool use_exec_freq = true;
75 typedef struct _be_pbqp_alloc_env_t {
76 pbqp *pbqp_inst; /**< PBQP instance for register allocation */
77 be_irg_t *birg; /**< Back-end IRG session. */
78 ir_graph *irg; /**< The graph under examination. */
79 const arch_register_class_t *cls; /**< Current processed register class */
81 bitset_t *ignored_regs;
82 pbqp_matrix *ife_matrix_template;
83 pbqp_matrix *aff_matrix_template;
85 unsigned *restr_nodes;
86 unsigned *ife_edge_num;
87 be_chordal_env_t *env;
88 } be_pbqp_alloc_env_t;
91 #define is_Reg_Phi(irn) (is_Phi(irn) && mode_is_data(get_irn_mode(irn)))
92 #define get_Perm_src(irn) (get_irn_n(get_Proj_pred(irn), get_Proj_proj(irn)))
93 #define is_Perm_Proj(irn) (is_Proj(irn) && be_is_Perm(get_Proj_pred(irn)))
94 #define insert_edge(pbqp, src_node, trg_node, template_matrix) (add_edge_costs(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node), pbqp_matrix_copy(pbqp, template_matrix)))
95 #define get_free_regs(restr_nodes, cls, irn) (arch_register_class_n_regs(cls) - restr_nodes[get_irn_idx(irn)])
97 static inline int is_2addr_code(const arch_register_req_t *req)
99 return (req->type & arch_register_req_type_should_be_same) != 0;
102 static const lc_opt_table_entry_t options[] = {
103 LC_OPT_ENT_BOOL ("exec_freq", "use exec_freq", &use_exec_freq),
108 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
115 n = strlen(env->birg->main_env->cup_name);
116 tu_name = XMALLOCN(char, n + 1);
117 strcpy(tu_name, env->birg->main_env->cup_name);
118 for (i = 0; i < n; ++i)
119 if (tu_name[i] == '.')
122 ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
124 result = fopen(buf, "wt");
126 panic("Couldn't open '%s' for writing.", buf);
134 static void create_pbqp_node(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *irn)
136 const arch_register_class_t *cls = pbqp_alloc_env->cls;
137 pbqp *pbqp_inst = pbqp_alloc_env->pbqp_inst;
138 bitset_t *ignored_regs = pbqp_alloc_env->ignored_regs;
139 unsigned colors_n = arch_register_class_n_regs(cls);
140 unsigned cntConstrains = 0;
142 /* create costs vector depending on register constrains */
143 struct vector *costs_vector = vector_alloc(pbqp_inst, colors_n);
145 /* set costs depending on register constrains */
147 for(idx = 0; idx < colors_n; idx++) {
148 if(bitset_is_set(ignored_regs, idx) || !arch_reg_out_is_allocatable(irn, arch_register_for_index(cls, idx))) {
149 vector_set(costs_vector, idx, INF_COSTS);
154 /* add vector to pbqp node */
155 add_node_costs(pbqp_inst, get_irn_idx(irn), costs_vector);
156 pbqp_alloc_env->restr_nodes[get_irn_idx(irn)] = cntConstrains;
159 static void insert_ife_edge(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *src_node, ir_node *trg_node)
161 pbqp *pbqp = pbqp_alloc_env->pbqp_inst;
162 const arch_register_class_t *cls = pbqp_alloc_env->cls;
163 pbqp_matrix *ife_matrix_template = pbqp_alloc_env->ife_matrix_template;
164 unsigned *restr_nodes = pbqp_alloc_env->restr_nodes;
166 if(get_edge(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node)) == NULL) {
168 /* increase ife edge counter */
169 pbqp_alloc_env->ife_edge_num[get_irn_idx(src_node)]++;
170 pbqp_alloc_env->ife_edge_num[get_irn_idx(trg_node)]++;
172 /* do useful optimization to speed up pbqp solving (we can do this because we know our matrix) */
173 if(get_free_regs(restr_nodes, cls, src_node) == 1 && get_free_regs(restr_nodes, cls, trg_node) == 1) {
174 unsigned src_idx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
175 unsigned trg_idx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
176 assert(src_idx != trg_idx && "Interfering nodes could not have the same register!");
179 if(get_free_regs(restr_nodes, cls, src_node) == 1 || get_free_regs(restr_nodes, cls, trg_node) == 1) {
180 if(get_free_regs(restr_nodes, cls, src_node) == 1) {
181 unsigned idx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
182 vector_set(get_node(pbqp, get_irn_idx(trg_node))->costs, idx, INF_COSTS);
185 unsigned idx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
186 vector_set(get_node(pbqp, get_irn_idx(src_node))->costs, idx, INF_COSTS);
191 /* insert interference edge */
192 insert_edge(pbqp, src_node, trg_node, ife_matrix_template);
196 static void inser_afe_edge(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *src_node, ir_node *trg_node, int pos)
198 pbqp *pbqp = pbqp_alloc_env->pbqp_inst;
199 const arch_register_class_t *cls = pbqp_alloc_env->cls;
200 unsigned *restr_nodes = pbqp_alloc_env->restr_nodes;
201 pbqp_matrix *afe_matrix = pbqp_matrix_alloc(pbqp, arch_register_class_n_regs(cls), arch_register_class_n_regs(cls));
202 unsigned colors_n = arch_register_class_n_regs(cls);
204 if(get_edge(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node)) == NULL) {
206 /* get exec_freq for copy_block */
207 ir_node *root_bl = get_nodes_block(src_node);
208 ir_node *copy_bl = is_Phi(src_node) ? get_Block_cfgpred_block(root_bl, pos) : root_bl;
209 unsigned long res = get_block_execfreq_ulong(pbqp_alloc_env->birg->exec_freq, copy_bl);
211 /* create afe-matrix */
213 for(row = 0; row < colors_n; row++) {
214 for(col = 0; col < colors_n; col++) {
216 pbqp_matrix_set(afe_matrix, row, col, (num)res);
221 afe_matrix = pbqp_alloc_env->aff_matrix_template;
224 /* do useful optimization to speed up pbqp solving */
225 if(get_free_regs(restr_nodes, cls, src_node) == 1 && get_free_regs(restr_nodes, cls, trg_node) == 1) {
228 if(get_free_regs(restr_nodes, cls, src_node) == 1 || get_free_regs(restr_nodes, cls, trg_node) == 1) {
229 if(get_free_regs(restr_nodes, cls, src_node) == 1) {
230 unsigned regIdx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
231 vector_add_matrix_col(get_node(pbqp, get_irn_idx(trg_node))->costs, afe_matrix, regIdx);
234 unsigned regIdx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
235 vector_add_matrix_col(get_node(pbqp, get_irn_idx(src_node))->costs, afe_matrix, regIdx);
240 /* insert interference edge */
241 insert_edge(pbqp, src_node, trg_node, afe_matrix);
245 static void create_affinity_edges(ir_node *irn, void *env)
247 be_pbqp_alloc_env_t *pbqp_alloc_env = env;
248 const arch_register_class_t *cls = pbqp_alloc_env->cls;
249 const arch_register_req_t *req = arch_get_register_req_out(irn);
252 if (is_Reg_Phi(irn)) { /* Phis */
253 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
254 ir_node *arg = get_irn_n(irn, pos);
256 if (!arch_irn_consider_in_reg_alloc(cls, arg))
259 /* no edges to itself */
264 inser_afe_edge(pbqp_alloc_env, irn, arg, pos);
267 else if (is_Perm_Proj(irn)) { /* Perms */
268 ir_node *arg = get_Perm_src(irn);
269 if (!arch_irn_consider_in_reg_alloc(cls, arg))
272 inser_afe_edge(pbqp_alloc_env, irn, arg, -1);
274 else { /* 2-address code */
275 if (is_2addr_code(req)) {
276 const unsigned other = req->other_same;
279 for (i = 0; 1U << i <= other; ++i) {
280 if (other & (1U << i)) {
281 ir_node *other = get_irn_n(skip_Proj(irn), i);
282 if (!arch_irn_consider_in_reg_alloc(cls, other))
285 /* no edges to itself */
290 inser_afe_edge(pbqp_alloc_env, irn, other, i);
297 static void create_pbqp_coloring_instance(ir_node *block, void *data)
299 be_pbqp_alloc_env_t *pbqp_alloc_env = data;
300 be_lv_t *lv = pbqp_alloc_env->lv;
301 const arch_register_class_t *cls = pbqp_alloc_env->cls;
302 plist_t *rpeo = pbqp_alloc_env->rpeo;
303 pbqp *pbqp_inst = pbqp_alloc_env->pbqp_inst;
304 unsigned *restr_nodes = pbqp_alloc_env->restr_nodes;
305 pqueue_t *queue = new_pqueue();
306 pqueue_t *restr_nodes_queue = new_pqueue();
307 plist_t *temp_list = plist_new();
309 ir_nodeset_t live_nodes;
311 /* first, determine the pressure */
312 /* (this is only for compatibility with copymin optimization, it's not needed for pbqp coloring) */
313 create_borders(block, pbqp_alloc_env->env);
315 /* calculate living nodes for the first step */
316 ir_nodeset_init(&live_nodes);
317 be_liveness_end_of_block(lv, cls, block, &live_nodes);
319 /* create pbqp nodes, interference edges and reverse perfect elimination order */
320 sched_foreach_reverse(block, irn) {
322 ir_nodeset_iterator_t iter;
324 if (get_irn_mode(irn) == mode_T) {
325 const ir_edge_t *edge;
326 foreach_out_edge(irn, edge) {
327 ir_node *proj = get_edge_src_irn(edge);
328 if (!arch_irn_consider_in_reg_alloc(cls, proj))
331 /* create pbqp source node if it dosn't exist */
332 if(get_node(pbqp_inst, get_irn_idx(proj)) == NULL) {
333 create_pbqp_node(pbqp_alloc_env, proj);
336 /* create nodes and interference edges */
337 foreach_ir_nodeset(&live_nodes, live, iter) {
338 /* create pbqp source node if it dosn't exist */
339 if(get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
340 create_pbqp_node(pbqp_alloc_env, live);
343 /* no edges to itself */
348 insert_ife_edge(pbqp_alloc_env, proj, live);
353 if (arch_irn_consider_in_reg_alloc(cls, irn)) {
354 /* create pbqp source node if it dosn't exist */
355 if(get_node(pbqp_inst, get_irn_idx(irn)) == NULL) {
356 create_pbqp_node(pbqp_alloc_env, irn);
359 /* create nodes and interference edges */
360 foreach_ir_nodeset(&live_nodes, live, iter) {
361 /* create pbqp source node if it dosn't exist */
362 if(get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
363 create_pbqp_node(pbqp_alloc_env, live);
366 /* no edges to itself */
371 /* insert interference edge */
372 insert_ife_edge(pbqp_alloc_env, irn, live);
377 /* get living nodes for next step */
379 be_liveness_transfer(cls, irn, &live_nodes);
382 /* order nodes for perfect elimination order */
383 if (get_irn_mode(irn) == mode_T) {
384 plist_element_t *first = plist_first(temp_list);
385 const ir_edge_t *edge;
387 foreach_out_edge(irn, edge) {
388 ir_node *proj = get_edge_src_irn(edge);
389 if (!arch_irn_consider_in_reg_alloc(cls, proj))
392 // insert proj node into priority queue (descending by the number of interference edges)
393 if(get_free_regs(restr_nodes, cls, proj) <= 4/*bitset_is_set(restr_nodes, get_irn_idx(proj))*/) {
394 pqueue_put(restr_nodes_queue, proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
397 pqueue_put(queue,proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
401 /* first insert all restricted nodes */
402 while(!pqueue_empty(restr_nodes_queue)) {
404 plist_insert_back(temp_list, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(restr_nodes_queue))));
405 first = plist_first(temp_list);
407 plist_insert_before(temp_list, first, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(restr_nodes_queue))));
411 /* insert proj nodes descending by their number of interference edges */
412 while(!pqueue_empty(queue)) {
414 plist_insert_back(temp_list, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(queue))));
415 first = plist_first(temp_list);
417 plist_insert_before(temp_list, first, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(queue))));
422 if (arch_irn_consider_in_reg_alloc(cls, irn)) {
423 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(irn)));
428 /* insert nodes into reverse perfect elimination order */
430 foreach_plist(temp_list, el) {
431 plist_insert_back(rpeo, el->data);
434 /* free reserved memory */
435 ir_nodeset_destroy(&live_nodes);
436 plist_free(temp_list);
438 del_pqueue(restr_nodes_queue);
441 static void insert_perms(ir_node *block, void *data)
444 * Start silent in the start block.
445 * The silence remains until the first barrier is seen.
446 * Each other block is begun loud.
448 be_chordal_env_t *env = data;
450 int silent = block == get_irg_start_block(get_irn_irg(block));
453 * If the block is the start block search the barrier and
454 * start handling constraints from there.
456 for (irn = sched_first(block); !sched_is_end(irn);) {
457 int silent_old = silent; /* store old silent value */
458 if (be_is_Barrier(irn))
459 silent = !silent; /* toggle silent flag */
461 be_insn_t *insn = chordal_scan_insn(env, irn);
462 irn = insn->next_insn;
467 if (!insn->has_constraints)
470 pre_process_constraints(env, &insn);
474 void be_pbqp_coloring(be_chordal_env_t *env)
476 ir_graph *irg = env->irg;
477 be_irg_t *birg = env->birg;
478 const arch_register_class_t *cls = env->cls;
479 unsigned colors_n = arch_register_class_n_regs(cls);
480 be_pbqp_alloc_env_t pbqp_alloc_env;
481 unsigned idx, row, col;
485 ir_timer_t *t_ra_pbqp_alloc_create = ir_timer_new();
486 ir_timer_t *t_ra_pbqp_alloc_solve = ir_timer_new();
487 ir_timer_t *t_ra_pbqp_alloc_create_aff = ir_timer_new();
489 printf("#### ----- === Allocating registers of %s (%s) ===\n", cls->name, get_entity_name(get_irg_entity(irg)));
491 lv = be_assure_liveness(birg);
492 be_liveness_assure_sets(lv);
493 be_liveness_assure_chk(lv);
497 dom_tree_walk_irg(irg, insert_perms, NULL, env);
499 /* dump graph after inserting perms */
500 if (env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
502 snprintf(buf, sizeof(buf), "-%s-constr", cls->name);
503 be_dump(irg, buf, dump_ir_block_graph_sched);
507 /* initialize pbqp allocation data structure */
508 pbqp_alloc_env.pbqp_inst = alloc_pbqp(get_irg_last_idx(irg)); /* initialize pbqp instance */
509 pbqp_alloc_env.birg = birg;
510 pbqp_alloc_env.cls = cls;
511 pbqp_alloc_env.irg = irg;
512 pbqp_alloc_env.lv = lv;
513 pbqp_alloc_env.ignored_regs = bitset_malloc(colors_n);
514 pbqp_alloc_env.rpeo = plist_new();
515 pbqp_alloc_env.restr_nodes = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
516 pbqp_alloc_env.ife_edge_num = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
517 pbqp_alloc_env.env = env;
518 be_put_ignore_regs(birg, cls, pbqp_alloc_env.ignored_regs); /* get ignored registers */
521 /* create costs matrix template for interference edges */
522 struct pbqp_matrix *ife_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
524 for(row = 0, col=0; row < colors_n; row++, col++)
525 pbqp_matrix_set(ife_matrix, row, col, INF_COSTS);
527 pbqp_alloc_env.ife_matrix_template = ife_matrix;
531 /* create costs matrix template for affinity edges */
532 struct pbqp_matrix *afe_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
534 for(row = 0; row < colors_n; row++) {
535 for(col = 0; col < colors_n; col++) {
537 pbqp_matrix_set(afe_matrix, row, col, 2);
540 pbqp_alloc_env.aff_matrix_template = afe_matrix;
544 /* create pbqp instance */
546 ir_timer_reset_and_start(t_ra_pbqp_alloc_create);
549 dom_tree_walk_irg(irg, create_pbqp_coloring_instance , NULL, &pbqp_alloc_env);
551 ir_timer_stop(t_ra_pbqp_alloc_create);
555 /* set up affinity edges */
557 ir_timer_reset_and_start(t_ra_pbqp_alloc_create_aff);
560 foreach_plist(pbqp_alloc_env.rpeo, el) {
561 pbqp_node *node = el->data;
563 ir_node *irn = get_idx_irn(irg, idx);
564 create_affinity_edges(irn, &pbqp_alloc_env);
567 ir_timer_stop(t_ra_pbqp_alloc_create_aff);
572 // dump graph before solving pbqp
573 FILE *file_before = my_open(env, "", "-pbqp_coloring.html");
574 set_dumpfile(pbqp_alloc_env.pbqp_inst, file_before);
578 /* solve pbqp instance */
580 ir_timer_reset_and_start(t_ra_pbqp_alloc_solve);
582 solve_pbqp_heuristical_co(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
584 ir_timer_stop(t_ra_pbqp_alloc_solve);
586 num solution = get_solution(pbqp_alloc_env.pbqp_inst);
587 assert(solution != INF_COSTS && "No PBQP solution found");
591 plist_element_t *element;
592 foreach_plist(pbqp_alloc_env.rpeo, element) {
593 pbqp_node *node = element->data;
595 ir_node *irn = get_idx_irn(irg, idx);
596 num color = get_node_solution(pbqp_alloc_env.pbqp_inst, idx);
597 const arch_register_t *reg = arch_register_for_index(cls, color);
599 arch_set_irn_register(irn, reg);
604 printf("%-20s: %8.3lf msec\n", "pbqp alloc create",
605 (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create) / 1000.0);
606 printf("%-20s: %8.3lf msec\n", "pbqp alloc solve",
607 (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_solve) / 1000.0);
608 printf("%-20s: %8.3lf msec\n", "pbqp alloc create aff",
609 (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create_aff) / 1000.0);
613 /* free reserved memory */
617 bitset_free(pbqp_alloc_env.ignored_regs);
618 free_pbqp(pbqp_alloc_env.pbqp_inst);
619 plist_free(pbqp_alloc_env.rpeo);
620 xfree(pbqp_alloc_env.restr_nodes);
621 xfree(pbqp_alloc_env.ife_edge_num);
626 * Initializes this module.
628 void be_init_pbqp_coloring(void)
630 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
631 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
632 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
633 lc_opt_entry_t *coloring_grp = lc_opt_get_grp(chordal_grp, "coloring");
634 lc_opt_entry_t *pbqp_grp = lc_opt_get_grp(coloring_grp, "pbqp");
636 static be_ra_chordal_coloring_t coloring = {
640 lc_opt_add_table(pbqp_grp, options);
641 be_register_chordal_coloring("pbqp", &coloring);
644 BE_REGISTER_MODULE_CONSTRUCTOR(be_pbqp_alloc);