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"
63 #include "heuristical.h"
65 #include "html_dumper.h"
66 #include "pbqp_node_t.h"
67 #include "pbqp_node.h"
70 typedef struct _be_pbqp_alloc_env_t {
71 pbqp *pbqp_inst; /**< PBQP instance for register allocation */
72 be_irg_t *birg; /**< Back-end IRG session. */
73 ir_graph *irg; /**< The graph under examination. */
74 const arch_register_class_t *cls; /**< Current processed register class */
76 bitset_t *ignored_regs;
77 pbqp_matrix *ife_matrix_dummy;
78 pbqp_matrix *aff_matrix_dummy;
80 unsigned *restr_nodes;
81 be_chordal_env_t *env;
82 } be_pbqp_alloc_env_t;
85 #define is_Reg_Phi(irn) (is_Phi(irn) && mode_is_data(get_irn_mode(irn)))
86 #define get_Perm_src(irn) (get_irn_n(get_Proj_pred(irn), get_Proj_proj(irn)))
87 #define is_Perm_Proj(irn) (is_Proj(irn) && be_is_Perm(get_Proj_pred(irn)))
89 static inline int is_2addr_code(const arch_register_req_t *req)
91 return (req->type & arch_register_req_type_should_be_same) != 0;
96 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
103 n = strlen(env->birg->main_env->cup_name);
104 tu_name = XMALLOCN(char, n + 1);
105 strcpy(tu_name, env->birg->main_env->cup_name);
106 for (i = 0; i < n; ++i)
107 if (tu_name[i] == '.')
110 ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
112 result = fopen(buf, "wt");
114 panic("Couldn't open '%s' for writing.", buf);
122 static unsigned create_pbqp_node(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *irn) {
123 const arch_register_class_t *cls = pbqp_alloc_env->cls;
124 pbqp *pbqp_inst = pbqp_alloc_env->pbqp_inst;
125 bitset_t *ignored_regs = pbqp_alloc_env->ignored_regs;
126 unsigned colors_n = arch_register_class_n_regs(cls);
127 unsigned cntConstrains = 0;
129 /* create costs vector depending on register constrains */
130 struct vector *costs_vector = vector_alloc(pbqp_inst, colors_n);
132 /* set costs depending on register constrains */
134 for(idx = 0; idx < colors_n; idx++) {
135 if(bitset_is_set(ignored_regs, idx) || !arch_reg_out_is_allocatable(irn, arch_register_for_index(cls, idx))) {
136 vector_set(costs_vector, idx, INF_COSTS);
141 /* add vector to pbqp node */
142 add_node_costs(pbqp_inst, get_irn_idx(irn), costs_vector);
144 /* return number of free selectable registers */
145 return (colors_n - cntConstrains);
148 static void build_graph_walker(ir_node *irn, void *env) {
149 be_pbqp_alloc_env_t *pbqp_alloc_env = env;
150 pbqp *pbqp_inst = pbqp_alloc_env->pbqp_inst;
151 const arch_register_class_t *cls = pbqp_alloc_env->cls;
152 const arch_register_req_t *req = arch_get_register_req_out(irn);
155 if (arch_irn_consider_in_reg_alloc(cls, irn))
158 if (is_Reg_Phi(irn)) { /* Phis */
159 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
160 ir_node *arg = get_irn_n(irn, pos);
161 //add_edges(co, irn, arg, co->get_costs(co, irn, arg, pos));
163 if (!arch_irn_consider_in_reg_alloc(cls, arg))
166 /* no edges to itself */
171 if(get_edge(pbqp_inst, get_irn_idx(irn), get_irn_idx(arg)) == NULL) {
173 struct pbqp_matrix *matrix = pbqp_matrix_copy(pbqp_inst, pbqp_alloc_env->aff_matrix_dummy);
174 /* add costs matrix to affinity edge */
175 add_edge_costs(pbqp_inst, get_irn_idx(irn), get_irn_idx(arg) , matrix);
179 else if (is_Perm_Proj(irn)) { /* Perms */
180 ir_node *arg = get_Perm_src(irn);
181 //add_edges(co, irn, arg, co->get_costs(co, irn, arg, 0));
183 if (!arch_irn_consider_in_reg_alloc(cls, arg))
186 if(get_edge(pbqp_inst, get_irn_idx(irn), get_irn_idx(arg)) == NULL) {
188 struct pbqp_matrix *matrix = pbqp_matrix_copy(pbqp_inst, pbqp_alloc_env->aff_matrix_dummy);
189 /* add costs matrix to affinity edge */
190 add_edge_costs(pbqp_inst, get_irn_idx(irn), get_irn_idx(arg) , matrix);
193 else { /* 2-address code */
194 if (is_2addr_code(req)) {
195 const unsigned other = req->other_same;
198 for (i = 0; 1U << i <= other; ++i) {
199 if (other & (1U << i)) {
200 ir_node *other = get_irn_n(skip_Proj(irn), i);
201 // if (!arch_irn_is_ignore(other)) {
202 //add_edges(co, irn, other, co->get_costs(co, irn, other, 0));
203 if (!arch_irn_consider_in_reg_alloc(cls, other))
206 /* no edges to itself */
211 if(get_edge(pbqp_inst, get_irn_idx(irn), get_irn_idx(other)) == NULL) {
213 struct pbqp_matrix *matrix = pbqp_matrix_copy(pbqp_inst, pbqp_alloc_env->aff_matrix_dummy);
214 /* add costs matrix to affinity edge */
215 add_edge_costs(pbqp_inst, get_irn_idx(irn), get_irn_idx(other) , matrix);
224 static void create_pbqp_coloring_inst(ir_node *block, void *data) {
225 be_pbqp_alloc_env_t *pbqp_alloc_env = data;
226 be_lv_t *lv = pbqp_alloc_env->lv;
227 const arch_register_class_t *cls = pbqp_alloc_env->cls;
228 plist_t *rpeo = pbqp_alloc_env->rpeo;
229 pbqp *pbqp_inst = pbqp_alloc_env->pbqp_inst;
230 unsigned *restr_nodes = pbqp_alloc_env->restr_nodes;
231 pbqp_matrix *ife_matrix_dummy = pbqp_alloc_env->ife_matrix_dummy;
232 pqueue_t *queue = new_pqueue();
233 pqueue_t *restr_nodes_queue = new_pqueue();
234 plist_t *temp_list = plist_new();
236 ir_nodeset_t live_nodes;
238 /* first, determine the pressure */
239 /* (this is only for compatibility with copymin optimization, it's not needed for pbqp coloring) */
240 create_borders(block, pbqp_alloc_env->env);
242 /* calculate living nodes for the first step */
243 ir_nodeset_init(&live_nodes);
244 be_liveness_end_of_block(lv, cls, block, &live_nodes);
246 /* create pbqp nodes, interference edges and reverse perfect elimination order */
247 sched_foreach_reverse(block, irn) {
248 ir_node *live, *if_live;
249 ir_nodeset_iterator_t iter, iter2;
251 /* create nodes and interference edges */
252 foreach_ir_nodeset(&live_nodes, live, iter) {
253 /* create pbqp source node if it dosn't exist */
254 if(get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
255 restr_nodes[get_irn_idx(live)] = create_pbqp_node(pbqp_alloc_env, live);
259 for(if_live = ir_nodeset_iterator_next(&iter2); if_live != NULL; if_live = ir_nodeset_iterator_next(&iter2)) {
260 /* create pbqp target node if it dosn't exist */
261 if(get_node(pbqp_inst, get_irn_idx(if_live)) == NULL) {
262 restr_nodes[get_irn_idx(if_live)] = create_pbqp_node(pbqp_alloc_env, if_live);
265 /* no edges to itself */
268 /* only one interference edge between two nodes */
269 if(get_edge(pbqp_inst, get_irn_idx(live), get_irn_idx(if_live)))
273 /* do useful optimization to improve pbqp solving (we can do this because we know our matrix) */
274 if(restr_nodes[get_irn_idx(live)] == 1 && restr_nodes[get_irn_idx(if_live)] == 1) {
275 unsigned src_idx = vector_get_min_index(get_node(pbqp_inst, get_irn_idx(live))->costs);
276 unsigned trg_idx = vector_get_min_index(get_node(pbqp_inst, get_irn_idx(if_live))->costs);
277 assert(src_idx != trg_idx && "Interfering nodes could not have the same register!");
280 if(restr_nodes[get_irn_idx(live)] == 1 || restr_nodes[get_irn_idx(if_live)] == 1) {
281 if(restr_nodes[get_irn_idx(live)] == 1) {
282 unsigned idx = vector_get_min_index(get_node(pbqp_inst, get_irn_idx(live))->costs);
283 vector_set(get_node(pbqp_inst, get_irn_idx(if_live))->costs, idx, INF_COSTS);
286 unsigned idx = vector_get_min_index(get_node(pbqp_inst, get_irn_idx(if_live))->costs);
287 vector_set(get_node(pbqp_inst, get_irn_idx(live))->costs, idx, INF_COSTS);
293 struct pbqp_matrix *matrix = pbqp_matrix_copy(pbqp_inst, ife_matrix_dummy);
294 /* add costs matrix to interference edge */
295 add_edge_costs(pbqp_inst, get_irn_idx(live), get_irn_idx(if_live) , matrix);
299 /* order nodes for perfect elimination order */
300 if (get_irn_mode(irn) == mode_T) {
301 plist_element_t *first = plist_first(temp_list);
302 const ir_edge_t *edge;
304 foreach_out_edge(irn, edge) {
305 ir_node *proj = get_edge_src_irn(edge);
306 if (!arch_irn_consider_in_reg_alloc(cls, proj))
309 // insert proj node into priority queue (descending by the number of interference edges)
310 if(restr_nodes[get_irn_idx(proj)] <= 4/*bitset_is_set(restr_nodes, get_irn_idx(proj))*/) {
311 pqueue_put(restr_nodes_queue, proj, pbqp_node_get_degree(get_node(pbqp_inst, get_irn_idx(proj))));
314 pqueue_put(queue,proj, pbqp_node_get_degree(get_node(pbqp_inst, get_irn_idx(proj))));
319 /* first insert all restricted nodes */
320 while(!pqueue_empty(restr_nodes_queue)) {
322 plist_insert_back(temp_list, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(restr_nodes_queue))));
323 first = plist_first(temp_list);
325 plist_insert_before(temp_list, first, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(restr_nodes_queue))));
329 /* insert proj nodes descending by their number of interference edges */
330 while(!pqueue_empty(queue)) {
332 plist_insert_back(temp_list, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(queue))));
333 first = plist_first(temp_list);
335 plist_insert_before(temp_list, first, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(queue))));
340 if (arch_irn_consider_in_reg_alloc(cls, irn)) {
341 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(irn)));
345 /* get living nodes for next step */
347 be_liveness_transfer(cls, irn, &live_nodes);
351 /* insert nodes into reverse perfect elimination order */
353 foreach_plist(temp_list, el) {
354 plist_insert_back(rpeo, el->data);
357 /* free reserved memory */
358 ir_nodeset_destroy(&live_nodes);
359 plist_free(temp_list);
361 del_pqueue(restr_nodes_queue);
364 static void insert_perms(ir_node *block, void *data) {
366 * Start silent in the start block.
367 * The silence remains until the first barrier is seen.
368 * Each other block is begun loud.
370 be_chordal_env_t *env = data;
372 int silent = block == get_irg_start_block(get_irn_irg(block));
375 * If the block is the start block search the barrier and
376 * start handling constraints from there.
378 for (irn = sched_first(block); !sched_is_end(irn);) {
379 int silent_old = silent; /* store old silent value */
380 if (be_is_Barrier(irn))
381 silent = !silent; /* toggle silent flag */
383 be_insn_t *insn = chordal_scan_insn(env, irn);
384 irn = insn->next_insn;
389 if (!insn->has_constraints)
392 pre_process_constraints(env, &insn);
397 void be_pbqp_coloring(be_chordal_env_t *env) {
398 ir_graph *irg = env->irg;
399 be_irg_t *birg = env->birg;
400 const arch_register_class_t *cls = env->cls;
401 unsigned colors_n = arch_register_class_n_regs(cls);
402 be_pbqp_alloc_env_t pbqp_alloc_env;
403 unsigned idx, row, col;
406 // ir_timer_t *t_ra_pbqp_alloc_create = ir_timer_register("be_pbqp_alloc_create", "pbqp alloc create");
407 // ir_timer_t *t_ra_pbqp_alloc_solve = ir_timer_register("be_pbqp_alloc_solve", "pbqp alloc solve");
408 // ir_timer_t *t_ra_pbqp_alloc_create_aff = ir_timer_register("be_pbqp_alloc_create_aff", "pbqp alloc create aff");
410 lv = be_assure_liveness(birg);
411 be_liveness_assure_sets(lv);
412 be_liveness_assure_chk(lv);
414 // printf("#### ----- === Allocating registers of %s (%s) ===\n", cls->name, get_entity_name(get_irg_entity(irg)));
418 dom_tree_walk_irg(irg, insert_perms, NULL, env);
420 /* dump graph after inserting perms */
421 if (env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
423 snprintf(buf, sizeof(buf), "-%s-constr", cls->name);
424 be_dump(irg, buf, dump_ir_block_graph_sched);
427 /* initialize pbqp allocation data structure */
428 pbqp_alloc_env.pbqp_inst = alloc_pbqp(get_irg_last_idx(irg)); /* initialize pbqp instance */
429 pbqp_alloc_env.birg = birg;
430 pbqp_alloc_env.cls = cls;
431 pbqp_alloc_env.irg = irg;
432 pbqp_alloc_env.lv = lv;
433 pbqp_alloc_env.ignored_regs = bitset_malloc(colors_n);
434 pbqp_alloc_env.rpeo = plist_new();
435 pbqp_alloc_env.restr_nodes = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
436 pbqp_alloc_env.env = env;
437 be_put_ignore_regs(birg, cls, pbqp_alloc_env.ignored_regs); /* get ignored registers */
439 /* create costs matrix for interference edges */
440 struct pbqp_matrix *ife_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
442 for(row = 0, col=0; row < colors_n; row++, col++)
443 pbqp_matrix_set(ife_matrix, row, col, INF_COSTS);
445 pbqp_alloc_env.ife_matrix_dummy = ife_matrix;
447 /* create costs matrix for affinity edges */
448 struct pbqp_matrix *afe_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
450 for(row = 0; row < colors_n; row++) {
451 for(col = 0; col < colors_n; col++) {
453 pbqp_matrix_set(afe_matrix, row, col, 2);
456 pbqp_alloc_env.aff_matrix_dummy = afe_matrix;
459 /* create pbqp instance */
460 // ir_timer_reset_and_start(t_ra_pbqp_alloc_create);
462 dom_tree_walk_irg(irg, create_pbqp_coloring_inst , NULL, &pbqp_alloc_env);
463 // ir_timer_stop(t_ra_pbqp_alloc_create);
465 /* set up affinity edges */
466 // ir_timer_reset_and_start(t_ra_pbqp_alloc_create_aff);
467 irg_walk_graph(irg, build_graph_walker, NULL, &pbqp_alloc_env);
468 // ir_timer_stop(t_ra_pbqp_alloc_create_aff);
471 // dump graph before solving pbqp
472 FILE *file_before = my_open(env, "", "-pbqp_coloring.html");
473 set_dumpfile(pbqp_alloc_env.pbqp_inst, file_before);
476 /* solve pbqp instance */
477 // ir_timer_reset_and_start(t_ra_pbqp_alloc_solve);
478 solve_pbqp_heuristical_co(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
479 // ir_timer_stop(t_ra_pbqp_alloc_solve);
480 num solution = get_solution(pbqp_alloc_env.pbqp_inst);
481 assert(solution != INF_COSTS && "No PBQP solution found");
483 plist_element_t *element;
484 foreach_plist(pbqp_alloc_env.rpeo, element) {
485 pbqp_node *node = element->data;
487 ir_node *irn = get_idx_irn(irg, idx);
488 num color = get_node_solution(pbqp_alloc_env.pbqp_inst, idx);
489 const arch_register_t *reg = arch_register_for_index(cls, color);
491 arch_set_irn_register(irn, reg);
494 // printf("%-20s: %8.3lf msec\n" , ir_timer_get_description(t_ra_pbqp_alloc_create), (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create) / 1000.0);
495 // printf("%-20s: %8.3lf msec\n" , ir_timer_get_description(t_ra_pbqp_alloc_solve), (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_solve) / 1000.0);
496 // printf("%-20s: %8.3lf msec\n" , ir_timer_get_description(t_ra_pbqp_alloc_create_aff), (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create_aff) / 1000.0);
499 /* free reserved memory */
503 bitset_free(pbqp_alloc_env.ignored_regs);
504 free_pbqp(pbqp_alloc_env.pbqp_inst);
505 plist_free(pbqp_alloc_env.rpeo);
506 xfree(pbqp_alloc_env.restr_nodes);
511 * Initializes this module.
513 void be_init_pbqp_coloring(void) {
515 static be_ra_chordal_coloring_t coloring = {
519 be_register_chordal_coloring("pbqp", &coloring);
522 BE_REGISTER_MODULE_CONSTRUCTOR(be_pbqp_alloc);