4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
26 #include "iredges_t.h"
29 #include "irphase_t.h"
30 #include "irprintf_t.h"
37 #include "becopyopt_t.h"
38 #include "becopystat.h"
41 #include "besched_t.h"
42 #include "benodesets.h"
43 #include "bejavacoal.h"
44 #include "bestatevent.h"
49 #include <libcore/lc_timing.h>
50 #include <libcore/lc_opts.h>
51 #endif /* WITH_LIBCORE */
56 #define DUMP_ALL 2 * DUMP_APPEL - 1
58 #define COST_FUNC_FREQ 1
59 #define COST_FUNC_LOOP 2
60 #define COST_FUNC_ALL_ONE 3
62 static unsigned dump_flags = 0;
63 static unsigned style_flags = 0;
64 static unsigned do_stats = 0;
65 static cost_fct_t cost_func = co_get_costs_exec_freq;
66 static unsigned algo = CO_ALGO_HEUR2;
67 static int improve = 1;
70 static const lc_opt_enum_mask_items_t dump_items[] = {
71 { "before", DUMP_BEFORE },
72 { "after", DUMP_AFTER },
73 { "appel", DUMP_APPEL },
78 static const lc_opt_enum_mask_items_t style_items[] = {
79 { "color", CO_IFG_DUMP_COLORS },
80 { "labels", CO_IFG_DUMP_LABELS },
81 { "constr", CO_IFG_DUMP_CONSTR },
82 { "shape", CO_IFG_DUMP_SHAPE },
83 { "full", 2 * CO_IFG_DUMP_SHAPE - 1 },
87 static const lc_opt_enum_mask_items_t algo_items[] = {
88 { "none", CO_ALGO_NONE },
89 { "heur", CO_ALGO_HEUR },
90 { "heur2", CO_ALGO_HEUR2 },
92 { "heur3", CO_ALGO_HEUR3 },
95 { "ilp", CO_ALGO_ILP },
100 typedef int (*opt_funcptr)(void);
102 static const lc_opt_enum_func_ptr_items_t cost_func_items[] = {
103 { "freq", (opt_funcptr) co_get_costs_exec_freq },
104 { "loop", (opt_funcptr) co_get_costs_loop_depth },
105 { "one", (opt_funcptr) co_get_costs_all_one },
109 static lc_opt_enum_mask_var_t dump_var = {
110 &dump_flags, dump_items
113 static lc_opt_enum_mask_var_t style_var = {
114 &style_flags, style_items
117 static lc_opt_enum_mask_var_t algo_var = {
121 static lc_opt_enum_func_ptr_var_t cost_func_var = {
122 (opt_funcptr*) &cost_func, cost_func_items
125 static const lc_opt_table_entry_t options[] = {
126 LC_OPT_ENT_ENUM_INT ("algo", "select copy optimization algo", &algo_var),
127 LC_OPT_ENT_ENUM_FUNC_PTR ("cost", "select a cost function", &cost_func_var),
128 LC_OPT_ENT_ENUM_MASK ("dump", "dump ifg before or after copy optimization", &dump_var),
129 LC_OPT_ENT_ENUM_MASK ("style", "dump style for ifg dumping", &style_var),
130 LC_OPT_ENT_BOOL ("stats", "dump statistics after each optimization", &do_stats),
131 LC_OPT_ENT_BOOL ("improve", "run heur3 before if algo can exploit start solutions", &improve),
135 /* Insert additional options registration functions here. */
136 extern void be_co_ilp_register_options(lc_opt_entry_t *grp);
137 extern void be_co2_register_options(lc_opt_entry_t *grp);
138 extern void be_co3_register_options(lc_opt_entry_t *grp);
140 void be_init_copycoal(void)
142 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
143 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
144 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
145 lc_opt_entry_t *co_grp = lc_opt_get_grp(chordal_grp, "co");
147 lc_opt_add_table(co_grp, options);
150 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copycoal);
154 #undef QUICK_AND_DIRTY_HACK
156 static int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
159 return be_ifg_connected(env->ifg, a, b);
161 return values_interfere(env->birg->lv, a, b);
165 /******************************************************************************
168 | | __ ___ _ __ ___ _ __ __ _| |
169 | | |_ |/ _ \ '_ \ / _ \ '__/ _` | |
170 | |__| | __/ | | | __/ | | (_| | |
171 \_____|\___|_| |_|\___|_| \__,_|_|
173 ******************************************************************************/
175 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
178 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, cost_fct_t get_costs)
180 const char *s1, *s2, *s3;
184 FIRM_DBG_REGISTER(dbg, "ir.be.copyopt");
186 co = xcalloc(1, sizeof(*co));
187 co->cenv = chordal_env;
188 co->aenv = chordal_env->birg->main_env->arch_env;
189 co->irg = chordal_env->irg;
190 co->cls = chordal_env->cls;
191 co->get_costs = get_costs;
193 s1 = get_irp_prog_name();
194 s2 = get_entity_name(get_irg_entity(co->irg));
195 s3 = chordal_env->cls->name;
196 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
197 co->name = xmalloc(len);
198 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
203 void free_copy_opt(copy_opt_t *co) {
208 int co_is_optimizable_root(const copy_opt_t *co, ir_node *irn) {
209 arch_register_req_t req;
210 const arch_register_t *reg;
212 if (arch_irn_is(co->aenv, irn, ignore))
215 reg = arch_get_irn_register(co->aenv, irn);
216 if (arch_register_type_is(reg, ignore))
219 if (is_Reg_Phi(irn) || is_Perm_Proj(co->aenv, irn) || is_2addr_code(co->aenv, irn, &req))
225 int co_is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
226 const ir_edge_t *edge;
227 const arch_register_t *reg;
229 assert(0 && "Is buggy and obsolete. Do not use");
231 if (arch_irn_is(co->aenv, irn, ignore))
234 reg = arch_get_irn_register(co->aenv, irn);
235 if (arch_register_type_is(reg, ignore))
238 foreach_out_edge(irn, edge) {
239 ir_node *n = edge->src;
241 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
242 arch_register_req_t req;
243 arch_get_register_req(co->aenv, &req, n, -1);
246 is_Perm(co->aenv, n) ||
247 (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
256 int co_get_costs_loop_depth(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos) {
259 ir_node *root_block = get_nodes_block(root);
262 /* for phis the copies are placed in the corresponding pred-block */
263 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
265 /* a perm places the copy in the same block as it resides */
266 loop = get_irn_loop(root_block);
269 int d = get_loop_depth(loop);
275 int co_get_costs_exec_freq(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos) {
277 ir_node *root_bl = get_nodes_block(root);
278 ir_node *copy_bl = is_Phi(root) ? get_Block_cfgpred_block(root_bl, pos) : root_bl;
279 res = get_block_execfreq_ulong(co->cenv->birg->exec_freq, copy_bl);
281 /* don't allow values smaller than one. */
282 return res < 1 ? 1 : res;
286 int co_get_costs_all_one(const copy_opt_t *co, ir_node *root, ir_node *arg, int pos) {
290 /******************************************************************************
291 ____ _ _ _ _ _ _____ _
292 / __ \ | | | | | | (_) | / ____| |
293 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
294 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
295 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
296 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
299 ******************************************************************************/
302 * Determines a maximum weighted independent set with respect to
303 * the interference and conflict edges of all nodes in a qnode.
305 static int ou_max_ind_set_costs(unit_t *ou) {
306 be_chordal_env_t *chordal_env = ou->co->cenv;
307 ir_node **safe, **unsafe;
308 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
310 int max, pos, curr_weight, best_weight = 0;
312 /* assign the nodes into two groups.
313 * safe: node has no interference, hence it is in every max stable set.
314 * unsafe: node has an interference
316 safe = alloca((ou->node_count-1) * sizeof(*safe));
319 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
320 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
322 for(i=1; i<ou->node_count; ++i) {
324 for(o=1; o<ou->node_count; ++o) {
327 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
328 unsafe_costs[unsafe_count] = ou->costs[i];
329 unsafe[unsafe_count] = ou->nodes[i];
336 safe_costs += ou->costs[i];
337 safe[safe_count++] = ou->nodes[i];
342 /* now compute the best set out of the unsafe nodes*/
343 if (unsafe_count > MIS_HEUR_TRIGGER) {
344 bitset_t *best = bitset_alloca(unsafe_count);
345 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
346 for (i=0; i<unsafe_count; ++i) {
348 /* check if it is a stable set */
349 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
350 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
351 bitset_clear(best, i); /* clear the bit and try next one */
355 /* compute the weight */
356 bitset_foreach(best, pos)
357 best_weight += unsafe_costs[pos];
359 /* Exact Algorithm: Brute force */
360 curr = bitset_alloca(unsafe_count);
361 bitset_set_all(curr);
362 while ((max = bitset_popcnt(curr)) != 0) {
363 /* check if curr is a stable set */
364 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
365 for (o=bitset_next_set(curr, i+1); o!=-1; o=bitset_next_set(curr, o+1)) /* !!!!! difference to qnode_max_ind_set(): NOT (curr, i) */
366 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
369 /* if we arrive here, we have a stable set */
370 /* compute the weigth of the stable set*/
372 bitset_foreach(curr, pos)
373 curr_weight += unsafe_costs[pos];
376 if (curr_weight > best_weight) {
377 best_weight = curr_weight;
385 return safe_costs+best_weight;
388 static void co_collect_units(ir_node *irn, void *env) {
389 copy_opt_t *co = env;
391 arch_register_req_t req;
393 if (!is_curr_reg_class(co, irn))
395 if (!co_is_optimizable_root(co, irn))
398 /* Init a new unit */
399 unit = xcalloc(1, sizeof(*unit));
401 unit->node_count = 1;
402 INIT_LIST_HEAD(&unit->queue);
404 /* Phi with some/all of its arguments */
405 if (is_Reg_Phi(irn)) {
409 arity = get_irn_arity(irn);
410 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
411 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
412 unit->nodes[0] = irn;
415 for (i=0; i<arity; ++i) {
417 ir_node *arg = get_irn_n(irn, i);
419 assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
422 if (nodes_interfere(co->cenv, irn, arg)) {
423 unit->inevitable_costs += co->get_costs(co, irn, arg, i);
427 /* Else insert the argument of the phi to the members of this ou */
428 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
430 /* Check if arg has occurred at a prior position in the arg/list */
432 for (o=0; o<unit->node_count; ++o)
433 if (unit->nodes[o] == arg) {
438 if (!arg_pos) { /* a new argument */
439 /* insert node, set costs */
440 unit->nodes[unit->node_count] = arg;
441 unit->costs[unit->node_count] = co->get_costs(co, irn, arg, i);
443 } else { /* arg has occured before in same phi */
444 /* increase costs for existing arg */
445 unit->costs[arg_pos] += co->get_costs(co, irn, arg, i);
448 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
449 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
452 /* Proj of a perm with corresponding arg */
453 if (is_Perm_Proj(co->aenv, irn)) {
454 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
455 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
456 unit->costs = xmalloc(2 * sizeof(*unit->costs));
457 unit->node_count = 2;
458 unit->nodes[0] = irn;
459 unit->nodes[1] = get_Perm_src(irn);
460 unit->costs[1] = co->get_costs(co, irn, unit->nodes[1], -1);
463 /* Src == Tgt of a 2-addr-code instruction */
464 if (is_2addr_code(co->aenv, irn, &req)) {
465 ir_node *other = req.other_same;
466 if (!nodes_interfere(co->cenv, irn, other)) {
467 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
468 unit->costs = xmalloc(2 * sizeof(*unit->costs));
469 unit->node_count = 2;
470 unit->nodes[0] = irn;
471 unit->nodes[1] = other;
472 unit->costs[1] = co->get_costs(co, irn, other, -1);
475 assert(0 && "This is not an optimizable node!");
477 /* Insert the new unit at a position according to its costs */
478 if (unit->node_count > 1) {
480 struct list_head *tmp;
482 /* Determine the maximum costs this unit can cause: all_nodes_cost */
483 for(i=1; i<unit->node_count; ++i) {
484 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
485 unit->all_nodes_costs += unit->costs[i];
488 /* Determine the minimal costs this unit will cause: min_nodes_costs */
489 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
490 /* Insert the new ou according to its sort_key */
492 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
494 list_add(&unit->units, tmp);
500 #ifdef QUICK_AND_DIRTY_HACK
502 static int compare_ous(const void *k1, const void *k2) {
503 const unit_t *u1 = *((const unit_t **) k1);
504 const unit_t *u2 = *((const unit_t **) k2);
505 int i, o, u1_has_constr, u2_has_constr;
506 arch_register_req_t req;
507 const arch_env_t *aenv = u1->co->aenv;
509 /* Units with constraints come first */
511 for (i=0; i<u1->node_count; ++i) {
512 arch_get_register_req(aenv, &req, u1->nodes[i], -1);
513 if (arch_register_req_is(&req, limited)) {
520 for (i=0; i<u2->node_count; ++i) {
521 arch_get_register_req(aenv, &req, u2->nodes[i], -1);
522 if (arch_register_req_is(&req, limited)) {
528 if (u1_has_constr != u2_has_constr)
529 return u2_has_constr - u1_has_constr;
531 /* Now check, whether the two units are connected */
533 for (i=0; i<u1->node_count; ++i)
534 for (o=0; o<u2->node_count; ++o)
535 if (u1->nodes[i] == u2->nodes[o])
539 /* After all, the sort key decides. Greater keys come first. */
540 return u2->sort_key - u1->sort_key;
545 * Sort the ou's according to constraints and their sort_key
547 static void co_sort_units(copy_opt_t *co) {
548 int i, count = 0, costs;
551 /* get the number of ous, remove them form the list and fill the array */
552 list_for_each_entry(unit_t, ou, &co->units, units)
554 ous = alloca(count * sizeof(*ous));
556 costs = co_get_max_copy_costs(co);
559 list_for_each_entry(unit_t, ou, &co->units, units)
562 INIT_LIST_HEAD(&co->units);
564 assert(count == i && list_empty(&co->units));
566 for (i=0; i<count; ++i)
567 ir_printf("%+F\n", ous[i]->nodes[0]);
569 qsort(ous, count, sizeof(*ous), compare_ous);
572 for (i=0; i<count; ++i)
573 ir_printf("%+F\n", ous[i]->nodes[0]);
575 /* reinsert into list in correct order */
576 for (i=0; i<count; ++i)
577 list_add_tail(&ous[i]->units, &co->units);
579 assert(costs == co_get_max_copy_costs(co));
583 void co_build_ou_structure(copy_opt_t *co) {
584 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
585 INIT_LIST_HEAD(&co->units);
586 irg_walk_graph(co->irg, co_collect_units, NULL, co);
587 #ifdef QUICK_AND_DIRTY_HACK
592 void co_free_ou_structure(copy_opt_t *co) {
595 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
600 co->units.next = NULL;
603 /* co_solve_heuristic() is implemented in becopyheur.c */
605 int co_get_max_copy_costs(const copy_opt_t *co) {
611 list_for_each_entry(unit_t, curr, &co->units, units) {
612 res += curr->inevitable_costs;
613 for (i=1; i<curr->node_count; ++i)
614 res += curr->costs[i];
619 int co_get_inevit_copy_costs(const copy_opt_t *co) {
625 list_for_each_entry(unit_t, curr, &co->units, units)
626 res += curr->inevitable_costs;
630 int co_get_copy_costs(const copy_opt_t *co) {
636 list_for_each_entry(unit_t, curr, &co->units, units) {
637 int root_col = get_irn_col(co, curr->nodes[0]);
638 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
639 res += curr->inevitable_costs;
640 for (i=1; i<curr->node_count; ++i) {
641 int arg_col = get_irn_col(co, curr->nodes[i]);
642 if (root_col != arg_col) {
643 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
644 res += curr->costs[i];
651 int co_get_lower_bound(const copy_opt_t *co) {
657 list_for_each_entry(unit_t, curr, &co->units, units)
658 res += curr->inevitable_costs + curr->min_nodes_costs;
662 void co_complete_stats(const copy_opt_t *co, co_complete_stats_t *stat)
664 bitset_t *seen = bitset_irg_malloc(co->irg);
667 memset(stat, 0, sizeof(stat[0]));
669 /* count affinity edges. */
670 co_gs_foreach_aff_node(co, an) {
672 stat->aff_nodes += 1;
673 bitset_add_irn(seen, an->irn);
674 co_gs_foreach_neighb(an, neigh) {
675 if(!bitset_contains_irn(seen, neigh->irn)) {
676 stat->aff_edges += 1;
677 stat->max_costs += neigh->costs;
679 if(get_irn_col(co, an->irn) != get_irn_col(co, neigh->irn)) {
680 stat->costs += neigh->costs;
681 stat->unsatisfied_edges += 1;
684 if(nodes_interfere(co->cenv, an->irn, neigh->irn)) {
686 stat->inevit_costs += neigh->costs;
696 /******************************************************************************
698 / ____| | | / ____| |
699 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
700 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
701 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
702 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
705 ******************************************************************************/
707 static int compare_affinity_node_t(const void *k1, const void *k2, size_t size) {
708 const affinity_node_t *n1 = k1;
709 const affinity_node_t *n2 = k2;
711 return (n1->irn != n2->irn);
714 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
715 affinity_node_t new_node, *node;
716 neighb_t new_nbr, *nbr;
721 new_node.neighbours = NULL;
722 node = set_insert(co->nodes, &new_node, sizeof(new_node), nodeset_hash(new_node.irn));
725 for (nbr = node->neighbours; nbr; nbr = nbr->next)
726 if (nbr->irn == n2) {
731 /* if we did not find n2 in n1's neighbourhood insert it */
733 obstack_grow(&co->obst, &new_nbr, sizeof(new_nbr));
734 nbr = obstack_finish(&co->obst);
737 nbr->next = node->neighbours;
738 node->neighbours = nbr;
742 /* now nbr points to n1's neighbour-entry of n2 */
746 static INLINE void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
747 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
748 add_edge(co, n1, n2, costs);
749 add_edge(co, n2, n1, costs);
753 static void build_graph_walker(ir_node *irn, void *env) {
754 copy_opt_t *co = env;
756 arch_register_req_t req;
757 const arch_register_t *reg;
759 if (!is_curr_reg_class(co, irn) || arch_irn_is(co->aenv, irn, ignore))
762 reg = arch_get_irn_register(co->aenv, irn);
763 if (arch_register_type_is(reg, ignore))
768 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
769 ir_node *arg = get_irn_n(irn, pos);
770 add_edges(co, irn, arg, co->get_costs(co, irn, arg, pos));
774 else if (is_Perm_Proj(co->aenv, irn)) {
775 ir_node *arg = get_Perm_src(irn);
776 add_edges(co, irn, arg, co->get_costs(co, irn, arg, 0));
780 else if (is_2addr_code(co->aenv, irn, &req))
781 add_edges(co, irn, req.other_same, co->get_costs(co, irn, req.other_same, 0));
784 void co_build_graph_structure(copy_opt_t *co) {
785 obstack_init(&co->obst);
786 co->nodes = new_set(compare_affinity_node_t, 32);
788 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
791 void co_free_graph_structure(copy_opt_t *co) {
795 obstack_free(&co->obst, NULL);
799 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
801 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn) {
802 affinity_node_t new_node, *n;
807 n = set_find(co->nodes, &new_node, sizeof(new_node), nodeset_hash(new_node.irn));
809 return (n->degree > 0);
814 void co_dump_appel_graph(const copy_opt_t *co, FILE *f)
816 be_ifg_t *ifg = co->cenv->ifg;
817 int *color_map = alloca(co->cls->n_regs * sizeof(color_map[0]));
818 bitset_t *adm = bitset_alloca(co->cls->n_regs);
825 for(i = 0; i < co->cls->n_regs; ++i) {
826 const arch_register_t *reg = &co->cls->regs[i];
827 color_map[i] = arch_register_type_is(reg, ignore) ? -1 : n_regs++;
831 * n contains the first node number.
832 * the values below n are the pre-colored register nodes
835 it = be_ifg_nodes_iter_alloca(ifg);
836 nit = be_ifg_neighbours_iter_alloca(ifg);
839 be_ifg_foreach_node(ifg, it, irn) {
840 if(!arch_irn_is(co->aenv, irn, ignore))
841 set_irn_link(irn, INT_TO_PTR(n++));
844 fprintf(f, "%d %d\n", n, n_regs);
846 be_ifg_foreach_node(ifg, it, irn) {
847 if(!arch_irn_is(co->aenv, irn, ignore)) {
848 int idx = PTR_TO_INT(get_irn_link(irn));
849 affinity_node_t *a = get_affinity_info(co, irn);
851 arch_register_req_t req;
854 arch_get_register_req(co->aenv, &req, irn, BE_OUT_POS(0));
855 if(arch_register_req_is(&req, limited)) {
856 bitset_clear_all(adm);
857 req.limited(req.limited_env, adm);
858 for(i = 0; i < co->cls->n_regs; ++i)
859 if(!bitset_is_set(adm, i) && color_map[i] >= 0)
860 fprintf(f, "%d %d -1\n", color_map[i], idx);
865 be_ifg_foreach_neighbour(ifg, nit, irn, adj) {
866 if(!arch_irn_is(co->aenv, adj, ignore)) {
867 int adj_idx = PTR_TO_INT(get_irn_link(adj));
869 fprintf(f, "%d %d -1\n", idx, adj_idx);
876 co_gs_foreach_neighb(a, n) {
877 if(!arch_irn_is(co->aenv, n->irn, ignore)) {
878 int n_idx = PTR_TO_INT(get_irn_link(n->irn));
880 fprintf(f, "%d %d %d\n", idx, n_idx, (int) n->costs);
888 typedef struct _appel_clique_walker_t {
890 const copy_opt_t *co;
897 } appel_clique_walker_t;
899 typedef struct _appel_block_info_t {
909 } appel_block_info_t;
911 static int appel_aff_weight(const appel_clique_walker_t *env, ir_node *bl)
914 double freq = get_block_execfreq(env->co->cenv->execfreq, bl);
915 int res = (int) freq;
916 return res == 0 ? 1 : res;
918 ir_loop *loop = get_irn_loop(bl);
920 int d = get_loop_depth(loop);
927 static void *appel_clique_walker_irn_init(phase_t *phase, ir_node *irn, void *old)
929 appel_block_info_t *res = NULL;
932 appel_clique_walker_t *d = (void *) phase;
933 res = phase_alloc(phase, sizeof(res[0]));
934 res->phi_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end_nr));
935 res->live_end_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end_nr));
936 res->live_in_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in_nr));
937 res->live_end = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end));
938 res->live_in = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in));
939 res->phi = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in));
945 typedef struct _insn_list_t {
947 struct list_head list;
950 static int appel_get_live_end_nr(appel_clique_walker_t *env, ir_node *bl, ir_node *irn)
952 appel_block_info_t *bli = phase_get_irn_data(&env->ph, bl);
955 for(i = 0; i < bli->n_live_end; ++i)
956 if(bli->live_end[i] == irn)
957 return bli->live_end_nr[i];
962 static int appel_dump_clique(appel_clique_walker_t *env, pset *live, ir_node *bl, int curr_nr, int start_nr)
964 ir_node **live_arr = alloca(env->co->cls->n_regs * sizeof(live_arr[0]));
970 foreach_pset(live, irn)
971 live_arr[n_live++] = irn;
973 /* dump the live after clique */
975 for(j = 0; j < n_live; ++j) {
978 for(k = j + 1; k < n_live; ++k) {
979 fprintf(env->f, "%d %d -1 ", curr_nr + j, curr_nr + k);
981 fprintf(env->f, "\n");
985 /* dump the affinities */
986 for(j = 0; !env->dumb && j < n_live; ++j) {
987 ir_node *irn = live_arr[j];
988 int old_nr = PTR_TO_INT(get_irn_link(irn));
990 /* if the node was already live in the last insn dump the affinity */
991 if(old_nr > start_nr) {
992 int weight = appel_aff_weight(env, bl);
993 fprintf(env->f, "%d %d %d\n", old_nr, curr_nr + j, weight);
997 /* set the current numbers into the link field. */
998 for(j = 0; j < n_live; ++j) {
999 ir_node *irn = live_arr[j];
1000 set_irn_link(irn, INT_TO_PTR(curr_nr + j));
1003 return curr_nr + n_live;
1006 static void appel_walker(ir_node *bl, void *data)
1008 appel_clique_walker_t *env = data;
1009 appel_block_info_t *bli = phase_get_or_set_irn_data(&env->ph, bl);
1010 struct obstack *obst = &env->obst;
1011 void *base = obstack_base(obst);
1012 pset *live = pset_new_ptr_default();
1013 be_lv_t *lv = env->co->cenv->birg->lv;
1017 int start_nr = env->curr_nr;
1018 int curr_nr = start_nr;
1020 be_insn_env_t insn_env;
1025 insn_env.aenv = env->co->aenv;
1026 insn_env.cls = env->co->cls;
1027 insn_env.obst = obst;
1028 insn_env.ignore_colors = env->co->cenv->ignore_colors;
1030 /* Guess how many insns will be in this block. */
1031 sched_foreach(bl, irn)
1035 insns = malloc(n_nodes * sizeof(insns[0]));
1037 /* Put all insns in an array. */
1038 irn = sched_first(bl);
1039 while(!sched_is_end(irn)) {
1041 insn = be_scan_insn(&insn_env, irn);
1042 insns[n_insns++] = insn;
1043 irn = insn->next_insn;
1046 DBG((env->co->cenv->dbg, LEVEL_2, "%+F\n", bl));
1047 be_liveness_end_of_block(lv, env->co->aenv, env->co->cls, bl, live);
1049 /* Generate the bad and ugly. */
1050 for(i = n_insns - 1; i >= 0; --i) {
1051 be_insn_t *insn = insns[i];
1053 /* The first live set has to be saved in the block border set. */
1054 if(i == n_insns - 1) {
1056 foreach_pset(live, irn) {
1057 bli->live_end[j] = irn;
1058 bli->live_end_nr[j] = curr_nr + j;
1061 bli->n_live_end = j;
1065 for(j = 0; j < insn->use_start; ++j) {
1066 ir_node *op = insn->ops[j].carrier;
1067 bitset_t *adm = insn->ops[j].regs;
1071 if(!insn->ops[j].has_constraints)
1075 foreach_pset(live, irn) {
1083 assert(nr < pset_count(live));
1085 for(k = 0; k < env->co->cls->n_regs; ++k) {
1086 int mapped_col = env->color_map[k];
1087 if(mapped_col >= 0 && !bitset_is_set(adm, k) && !bitset_is_set(env->co->cenv->ignore_colors, k))
1088 fprintf(env->f, "%d %d -1\n", curr_nr + nr, mapped_col);
1093 /* dump the clique and update the stuff. */
1094 curr_nr = appel_dump_clique(env, live, bl, curr_nr, start_nr);
1096 /* remove all defs. */
1097 for(j = 0; j < insn->use_start; ++j)
1098 pset_remove_ptr(live, insn->ops[j].carrier);
1100 if(is_Phi(insn->irn) && arch_irn_consider_in_reg_alloc(env->co->aenv, env->co->cls, insn->irn)) {
1101 bli->phi[bli->n_phi] = insn->irn;
1102 bli->phi_nr[bli->n_phi] = PTR_TO_INT(get_irn_link(insn->irn));
1108 for(j = insn->use_start; j < insn->n_ops; ++j)
1109 pset_insert_ptr(live, insn->ops[j].carrier);
1112 /* print the start clique. */
1113 curr_nr = appel_dump_clique(env, live, bl, curr_nr, start_nr);
1116 foreach_pset(live, irn) {
1117 bli->live_in[i] = irn;
1118 bli->live_in_nr[i] = PTR_TO_INT(get_irn_link(irn));
1125 obstack_free(obst, base);
1126 env->curr_nr = curr_nr;
1129 static void appel_inter_block_aff(ir_node *bl, void *data)
1131 appel_clique_walker_t *env = data;
1132 appel_block_info_t *bli = phase_get_irn_data(&env->ph, bl);
1136 for(i = 0; i < bli->n_live_in; ++i) {
1137 ir_node *irn = bli->live_in[i];
1139 for(j = 0, n = get_Block_n_cfgpreds(bl); j < n; ++j) {
1140 ir_node *pred = get_Block_cfgpred_block(bl, j);
1142 int nr = appel_get_live_end_nr(env, pred, irn);
1144 fprintf(env->f, "%d %d 1\n", bli->live_in_nr[i], nr);
1148 for(i = 0; i < bli->n_phi; ++i) {
1149 ir_node *irn = bli->phi[i];
1151 for(j = 0, n = get_Block_n_cfgpreds(bl); j < n; ++j) {
1152 ir_node *pred = get_Block_cfgpred_block(bl, j);
1153 ir_node *op = get_irn_n(irn, j);
1155 int nr = appel_get_live_end_nr(env, pred, op);
1157 fprintf(env->f, "%d %d 1\n", bli->phi_nr[i], nr);
1163 void co_dump_appel_graph_cliques(const copy_opt_t *co, FILE *f)
1167 appel_clique_walker_t env;
1168 bitset_t *adm = bitset_alloca(co->cls->n_regs);
1169 be_lv_t *lv = co->cenv->birg->lv;
1171 be_liveness_recompute(lv);
1172 obstack_init(&env.obst);
1173 phase_init(&env.ph, "appel_clique_dumper", co->irg, PHASE_DEFAULT_GROWTH, appel_clique_walker_irn_init);
1174 env.curr_nr = co->cls->n_regs;
1178 bitset_copy(adm, co->cenv->ignore_colors);
1179 bitset_flip_all(adm);
1181 /* Make color map. */
1182 env.color_map = alloca(co->cls->n_regs * sizeof(env.color_map[0]));
1183 for(i = 0, n_colors = 0; i < co->cls->n_regs; ++i) {
1184 const arch_register_t *reg = &co->cls->regs[i];
1185 env.color_map[i] = arch_register_type_is(reg, ignore) ? -1 : n_colors++;
1189 env.curr_nr = n_colors;
1190 irg_block_walk_graph(co->irg, firm_clear_link, NULL, NULL);
1191 irg_block_walk_graph(co->irg, appel_walker, NULL, &env);
1193 fprintf(f, "%d %d\n", env.curr_nr, n_colors);
1195 /* make the first k nodes interfere */
1196 for(i = 0; i < n_colors; ++i) {
1198 for(j = i + 1; j < n_colors; ++j)
1199 fprintf(f, "%d %d -1 ", i, j);
1204 env.curr_nr = n_colors;
1205 irg_block_walk_graph(co->irg, firm_clear_link, NULL, NULL);
1206 irg_block_walk_graph(co->irg, appel_walker, NULL, &env);
1207 irg_block_walk_graph(co->irg, appel_inter_block_aff, NULL, &env);
1208 obstack_free(&env.obst, NULL);
1212 ___ _____ ____ ____ ___ _____ ____ _
1213 |_ _| ___/ ___| | _ \ / _ \_ _| | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1214 | || |_ | | _ | | | | | | || | | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1215 | || _|| |_| | | |_| | |_| || | | |_| | |_| | | | | | | |_) | | | | | (_| |
1216 |___|_| \____| |____/ \___/ |_| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1220 static const char *get_dot_color_name(int col)
1222 static const char *names[] = {
1256 return col < sizeof(names)/sizeof(names[0]) ? names[col] : "white";
1259 typedef struct _co_ifg_dump_t {
1260 const copy_opt_t *co;
1264 static void ifg_dump_graph_attr(FILE *f, void *self)
1266 fprintf(f, "overlap=scale");
1269 static int ifg_is_dump_node(void *self, ir_node *irn)
1271 co_ifg_dump_t *cod = self;
1272 return !arch_irn_is(cod->co->aenv, irn, ignore);
1275 static void ifg_dump_node_attr(FILE *f, void *self, ir_node *irn)
1277 co_ifg_dump_t *env = self;
1278 const arch_register_t *reg = arch_get_irn_register(env->co->aenv, irn);
1279 arch_register_req_t req;
1282 arch_get_register_req(env->co->aenv, &req, irn, BE_OUT_POS(0));
1283 limited = arch_register_req_is(&req, limited);
1285 if(env->flags & CO_IFG_DUMP_LABELS) {
1286 ir_fprintf(f, "label=\"%+F", irn);
1288 if((env->flags & CO_IFG_DUMP_CONSTR) && limited) {
1289 bitset_t *bs = bitset_alloca(env->co->cls->n_regs);
1290 req.limited(req.limited_env, bs);
1291 ir_fprintf(f, "\\n%B", bs);
1293 ir_fprintf(f, "\" ");
1297 fprintf(f, "label=\"\" shape=point " );
1299 if(env->flags & CO_IFG_DUMP_SHAPE)
1300 fprintf(f, "shape=%s ", limited ? "diamond" : "ellipse");
1302 if(env->flags & CO_IFG_DUMP_COLORS)
1303 fprintf(f, "style=filled color=%s ", get_dot_color_name(reg->index));
1306 static void ifg_dump_at_end(FILE *file, void *self)
1308 co_ifg_dump_t *env = self;
1311 co_gs_foreach_aff_node(env->co, a) {
1312 const arch_register_t *ar = arch_get_irn_register(env->co->aenv, a->irn);
1313 unsigned aidx = get_irn_idx(a->irn);
1316 co_gs_foreach_neighb(a, n) {
1317 const arch_register_t *nr = arch_get_irn_register(env->co->aenv, n->irn);
1318 unsigned nidx = get_irn_idx(n->irn);
1321 const char *color = nr == ar ? "blue" : "red";
1322 fprintf(file, "\tn%d -- n%d [weight=0.01 ", aidx, nidx);
1323 if(env->flags & CO_IFG_DUMP_LABELS)
1324 fprintf(file, "label=\"%d\" ", n->costs);
1325 if(env->flags & CO_IFG_DUMP_COLORS)
1326 fprintf(file, "color=%s ", color);
1328 fprintf(file, "style=dotted");
1329 fprintf(file, "];\n");
1336 static be_ifg_dump_dot_cb_t ifg_dot_cb = {
1338 ifg_dump_graph_attr,
1347 void co_dump_ifg_dot(const copy_opt_t *co, FILE *f, unsigned flags)
1353 be_ifg_dump_dot(co->cenv->ifg, co->irg, f, &ifg_dot_cb, &cod);
1357 void co_solve_park_moon(copy_opt_t *opt)
1362 static int void_algo(copy_opt_t *co)
1369 / \ | | __ _ ___ _ __(_) |_| |__ _ __ ___ ___
1370 / _ \ | |/ _` |/ _ \| '__| | __| '_ \| '_ ` _ \/ __|
1371 / ___ \| | (_| | (_) | | | | |_| | | | | | | | \__ \
1372 /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_|___/
1379 int can_improve_existing;
1382 static co_algo_info_t algos[] = {
1383 { void_algo, "none", 0 },
1384 { co_solve_heuristic, "heur1", 0 },
1385 { co_solve_heuristic_new, "heur2", 0 },
1386 { co_solve_heuristic_java, "heur3", 0 },
1388 { co_solve_ilp2, "ilp", 1 },
1395 | \/ | __ _(_)_ __ | _ \ _ __(_)_ _____ _ __
1396 | |\/| |/ _` | | '_ \ | | | | '__| \ \ / / _ \ '__|
1397 | | | | (_| | | | | | | |_| | | | |\ V / __/ |
1398 |_| |_|\__,_|_|_| |_| |____/|_| |_| \_/ \___|_|
1402 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
1407 ir_snprintf(buf, sizeof(buf), "%s%F_%s%s", prefix, env->irg, env->cls->name, suffix);
1408 result = fopen(buf, "wt");
1409 if(result == NULL) {
1410 panic("Couldn't open '%s' for writing.", buf);
1416 void co_driver(be_chordal_env_t *cenv)
1419 lc_timer_t *timer = lc_timer_register("firm.be.copyopt", "runtime");
1421 co_complete_stats_t before, after;
1423 co_algo_t *algo_func;
1424 int was_optimal = 0;
1426 if(algo < 0 || algo >= CO_ALGO_LAST)
1429 co = new_copy_opt(cenv, cost_func);
1430 co_build_ou_structure(co);
1431 co_build_graph_structure(co);
1433 co_complete_stats(co, &before);
1435 be_stat_ev_ull("co_aff_nodes", before.aff_nodes);
1436 be_stat_ev_ull("co_aff_edges", before.aff_edges);
1437 be_stat_ev_ull("co_max_costs", before.max_costs);
1438 be_stat_ev_ull("co_inevit_costs", before.inevit_costs);
1439 be_stat_ev_ull("co_aff_int", before.aff_int);
1441 be_stat_ev_ull("co_init_costs", before.costs);
1442 be_stat_ev_ull("co_init_unsat", before.unsatisfied_edges);
1444 /* Dump the interference graph in Appel's format. */
1445 if(dump_flags & DUMP_APPEL) {
1446 FILE *f = my_open(cenv, "", ".apl");
1447 co_dump_appel_graph(co, f);
1451 if(dump_flags & DUMP_BEFORE) {
1452 FILE *f = my_open(cenv, "", "-before.dot");
1453 co_dump_ifg_dot(co, f, style_flags);
1457 /* if the algo can improve results, provide an initial solution with heur3 */
1458 if(improve && algos[algo].can_improve_existing) {
1459 co_complete_stats_t stats;
1461 /* produce a heuristical solution */
1462 co_solve_heuristic_java(co);
1464 /* do the stats and provide the current costs */
1465 co_complete_stats(co, &stats);
1466 be_stat_ev_ull("co_prepare_costs", stats.costs);
1470 /* start the JVM here so that it does not tamper the timing. */
1471 if(algo == CO_ALGO_HEUR3)
1472 be_java_coal_start_jvm();
1475 algo_func = algos[algo].algo;
1478 lc_timer_reset_and_start(timer);
1481 was_optimal = algo_func(co);
1484 lc_timer_stop(timer);
1485 be_stat_ev("co_time", lc_timer_elapsed_msec(timer));
1488 be_stat_ev_ull("co_optimal", was_optimal);
1490 if(dump_flags & DUMP_AFTER) {
1491 FILE *f = my_open(cenv, "", "-after.dot");
1492 co_dump_ifg_dot(co, f, style_flags);
1496 co_complete_stats(co, &after);
1499 ulong64 optimizable_costs = after.max_costs - after.inevit_costs;
1500 ulong64 evitable = after.costs - after.inevit_costs;
1502 ir_printf("%30F ", cenv->irg);
1503 printf("%10s %10" ULL_FMT "%10" ULL_FMT "%10" ULL_FMT, cenv->cls->name, after.max_costs, before.costs, after.inevit_costs);
1505 if(optimizable_costs > 0)
1506 printf("%10" ULL_FMT " %5.2f\n", after.costs, (evitable * 100.0) / optimizable_costs);
1508 printf("%10" ULL_FMT " %5s\n", after.costs, "-");
1511 be_stat_ev_ull("co_after_costs", after.costs);
1512 be_stat_ev_ull("co_after_unsat", after.unsatisfied_edges);
1514 co_free_graph_structure(co);
1515 co_free_ou_structure(co);