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 Copy minimization driver.
23 * @author Daniel Grund
27 * Main file for the optimization reducing the copies needed for:
29 * - Register-constrained nodes
30 * - Two-address code instructions
38 #include "raw_bitset.h"
44 #include "iredges_t.h"
46 #include "irphase_t.h"
47 #include "irprintf_t.h"
54 #include "beintlive_t.h"
55 #include "becopyopt_t.h"
56 #include "becopystat.h"
60 #include "bestatevent.h"
65 #include "lc_opts_enum.h"
70 #define DUMP_ALL 2 * DUMP_APPEL - 1
72 #define COST_FUNC_FREQ 1
73 #define COST_FUNC_LOOP 2
74 #define COST_FUNC_ALL_ONE 3
76 static unsigned dump_flags = 0;
77 static unsigned style_flags = 0;
78 static unsigned do_stats = 0;
79 static cost_fct_t cost_func = co_get_costs_exec_freq;
80 static int improve = 1;
82 static const lc_opt_enum_mask_items_t dump_items[] = {
83 { "before", DUMP_BEFORE },
84 { "after", DUMP_AFTER },
85 { "appel", DUMP_APPEL },
90 static const lc_opt_enum_mask_items_t style_items[] = {
91 { "color", CO_IFG_DUMP_COLORS },
92 { "labels", CO_IFG_DUMP_LABELS },
93 { "constr", CO_IFG_DUMP_CONSTR },
94 { "shape", CO_IFG_DUMP_SHAPE },
95 { "full", 2 * CO_IFG_DUMP_SHAPE - 1 },
99 typedef int (*opt_funcptr)(void);
101 static const lc_opt_enum_func_ptr_items_t cost_func_items[] = {
102 { "freq", (opt_funcptr) co_get_costs_exec_freq },
103 { "loop", (opt_funcptr) co_get_costs_loop_depth },
104 { "one", (opt_funcptr) co_get_costs_all_one },
108 static lc_opt_enum_mask_var_t dump_var = {
109 &dump_flags, dump_items
112 static lc_opt_enum_mask_var_t style_var = {
113 &style_flags, style_items
116 static lc_opt_enum_func_ptr_var_t cost_func_var = {
117 (opt_funcptr*) &cost_func, cost_func_items
120 static const lc_opt_table_entry_t options[] = {
121 LC_OPT_ENT_ENUM_FUNC_PTR ("cost", "select a cost function", &cost_func_var),
122 LC_OPT_ENT_ENUM_MASK ("dump", "dump ifg before or after copy optimization", &dump_var),
123 LC_OPT_ENT_ENUM_MASK ("style", "dump style for ifg dumping", &style_var),
124 LC_OPT_ENT_BOOL ("stats", "dump statistics after each optimization", &do_stats),
125 LC_OPT_ENT_BOOL ("improve", "run heur1 before if algo can exploit start solutions", &improve),
129 static be_module_list_entry_t *copyopts = NULL;
130 static const co_algo_info *selected_copyopt = NULL;
132 void be_register_copyopt(const char *name, co_algo_info *copyopt)
134 if (selected_copyopt == NULL)
135 selected_copyopt = copyopt;
136 be_add_module_to_list(©opts, name, copyopt);
139 void be_init_copyopt(void)
141 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
142 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
143 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
144 lc_opt_entry_t *co_grp = lc_opt_get_grp(chordal_grp, "co");
146 lc_opt_add_table(co_grp, options);
147 be_add_module_list_opt(co_grp, "algo", "select copy optimization algo",
148 ©opts, (void**) &selected_copyopt);
151 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyopt);
153 static int void_algo(copy_opt_t *co)
159 void be_init_copynone(void)
161 static co_algo_info copyheur = {
165 be_register_copyopt("none", ©heur);
168 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copynone);
170 #undef QUICK_AND_DIRTY_HACK
172 static int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
175 return be_ifg_connected(env->ifg, a, b);
177 return be_values_interfere(env->birg->lv, a, b);
181 /******************************************************************************
184 | | __ ___ _ __ ___ _ __ __ _| |
185 | | |_ |/ _ \ '_ \ / _ \ '__/ _` | |
186 | |__| | __/ | | | __/ | | (_| | |
187 \_____|\___|_| |_|\___|_| \__,_|_|
189 ******************************************************************************/
191 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
194 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, cost_fct_t get_costs)
196 const char *s1, *s2, *s3;
200 FIRM_DBG_REGISTER(dbg, "ir.be.copyopt");
202 co = XMALLOCZ(copy_opt_t);
203 co->cenv = chordal_env;
204 co->irg = chordal_env->irg;
205 co->cls = chordal_env->cls;
206 co->get_costs = get_costs;
209 s2 = get_entity_name(get_irg_entity(co->irg));
210 s3 = chordal_env->cls->name;
211 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
212 co->name = XMALLOCN(char, len);
213 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
218 void free_copy_opt(copy_opt_t *co)
225 * Checks if a node is optimizable, viz. has something to do with coalescing
226 * @param irn The irn to check
228 static int co_is_optimizable_root(ir_node *irn)
230 const arch_register_req_t *req;
231 const arch_register_t *reg;
233 if (arch_irn_is_ignore(irn))
236 reg = arch_get_irn_register(irn);
237 if (arch_register_type_is(reg, ignore))
240 if (is_Reg_Phi(irn) || is_Perm_Proj(irn))
243 req = arch_get_register_req_out(irn);
244 if (is_2addr_code(req))
250 int co_get_costs_loop_depth(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos)
254 ir_node *root_block = get_nodes_block(root);
259 /* for phis the copies are placed in the corresponding pred-block */
260 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
262 /* a perm places the copy in the same block as it resides */
263 loop = get_irn_loop(root_block);
266 int d = get_loop_depth(loop);
272 int co_get_costs_exec_freq(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos)
275 ir_node *root_bl = get_nodes_block(root);
276 ir_node *copy_bl = is_Phi(root) ? get_Block_cfgpred_block(root_bl, pos) : root_bl;
278 res = get_block_execfreq_ulong(co->cenv->birg->exec_freq, copy_bl);
280 /* don't allow values smaller than one. */
281 return res < 1 ? 1 : res;
285 int co_get_costs_all_one(const copy_opt_t *co, ir_node *root, ir_node *arg, int pos)
294 /******************************************************************************
295 ____ _ _ _ _ _ _____ _
296 / __ \ | | | | | | (_) | / ____| |
297 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
298 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
299 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
300 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
303 ******************************************************************************/
306 * Determines a maximum weighted independent set with respect to
307 * the interference and conflict edges of all nodes in a qnode.
309 static int ou_max_ind_set_costs(unit_t *ou)
311 be_chordal_env_t *chordal_env = ou->co->cenv;
312 ir_node **safe, **unsafe;
313 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
316 int max, curr_weight, best_weight = 0;
318 /* assign the nodes into two groups.
319 * safe: node has no interference, hence it is in every max stable set.
320 * unsafe: node has an interference
322 safe = ALLOCAN(ir_node*, ou->node_count - 1);
325 unsafe = ALLOCAN(ir_node*, ou->node_count - 1);
326 unsafe_costs = ALLOCAN(int, ou->node_count - 1);
328 for (i=1; i<ou->node_count; ++i) {
330 for (o=1; o<ou->node_count; ++o) {
333 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
334 unsafe_costs[unsafe_count] = ou->costs[i];
335 unsafe[unsafe_count] = ou->nodes[i];
342 safe_costs += ou->costs[i];
343 safe[safe_count++] = ou->nodes[i];
348 /* now compute the best set out of the unsafe nodes*/
349 if (unsafe_count > MIS_HEUR_TRIGGER) {
350 bitset_t *best = bitset_alloca(unsafe_count);
351 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
352 for (i=0; i<unsafe_count; ++i) {
354 /* check if it is a stable set */
355 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
356 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
357 bitset_clear(best, i); /* clear the bit and try next one */
361 /* compute the weight */
362 bitset_foreach(best, pos)
363 best_weight += unsafe_costs[pos];
365 /* Exact Algorithm: Brute force */
366 curr = bitset_alloca(unsafe_count);
367 bitset_set_all(curr);
368 while ((max = bitset_popcnt(curr)) != 0) {
369 /* check if curr is a stable set */
370 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
371 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) */
372 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
375 /* if we arrive here, we have a stable set */
376 /* compute the weigth of the stable set*/
378 bitset_foreach(curr, pos)
379 curr_weight += unsafe_costs[pos];
382 if (curr_weight > best_weight) {
383 best_weight = curr_weight;
391 return safe_costs+best_weight;
394 static void co_collect_units(ir_node *irn, void *env)
396 const arch_register_req_t *req = arch_get_register_req_out(irn);
397 copy_opt_t *co = env;
400 if (req->cls != co->cls)
402 if (!co_is_optimizable_root(irn))
405 /* Init a new unit */
406 unit = XMALLOCZ(unit_t);
408 unit->node_count = 1;
409 INIT_LIST_HEAD(&unit->queue);
411 /* Phi with some/all of its arguments */
412 if (is_Reg_Phi(irn)) {
416 arity = get_irn_arity(irn);
417 unit->nodes = XMALLOCN(ir_node*, arity + 1);
418 unit->costs = XMALLOCN(int, arity + 1);
419 unit->nodes[0] = irn;
422 for (i=0; i<arity; ++i) {
424 ir_node *arg = get_irn_n(irn, i);
426 assert(arch_get_irn_reg_class_out(arg) == co->cls && "Argument not in same register class.");
429 if (nodes_interfere(co->cenv, irn, arg)) {
430 unit->inevitable_costs += co->get_costs(co, irn, arg, i);
434 /* Else insert the argument of the phi to the members of this ou */
435 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
437 if (arch_irn_is_ignore(arg))
440 /* Check if arg has occurred at a prior position in the arg/list */
442 for (o=1; o<unit->node_count; ++o) {
443 if (unit->nodes[o] == arg) {
449 if (!arg_pos) { /* a new argument */
450 /* insert node, set costs */
451 unit->nodes[unit->node_count] = arg;
452 unit->costs[unit->node_count] = co->get_costs(co, irn, arg, i);
454 } else { /* arg has occurred before in same phi */
455 /* increase costs for existing arg */
456 unit->costs[arg_pos] += co->get_costs(co, irn, arg, i);
459 unit->nodes = XREALLOC(unit->nodes, ir_node*, unit->node_count);
460 unit->costs = XREALLOC(unit->costs, int, unit->node_count);
461 } else if (is_Perm_Proj(irn)) {
462 /* Proj of a perm with corresponding arg */
463 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
464 unit->nodes = XMALLOCN(ir_node*, 2);
465 unit->costs = XMALLOCN(int, 2);
466 unit->node_count = 2;
467 unit->nodes[0] = irn;
468 unit->nodes[1] = get_Perm_src(irn);
469 unit->costs[1] = co->get_costs(co, irn, unit->nodes[1], -1);
471 /* Src == Tgt of a 2-addr-code instruction */
472 if (is_2addr_code(req)) {
473 const unsigned other = req->other_same;
477 for (i = 0; (1U << i) <= other; ++i) {
478 if (other & (1U << i)) {
479 ir_node *o = get_irn_n(skip_Proj(irn), i);
480 if (arch_irn_is_ignore(o))
482 if (nodes_interfere(co->cenv, irn, o))
491 unit->nodes = XMALLOCN(ir_node*, count);
492 unit->costs = XMALLOCN(int, count);
493 unit->node_count = count;
494 unit->nodes[k++] = irn;
496 for (i = 0; 1U << i <= other; ++i) {
497 if (other & (1U << i)) {
498 ir_node *o = get_irn_n(skip_Proj(irn), i);
499 if (!arch_irn_is_ignore(o) &&
500 !nodes_interfere(co->cenv, irn, o)) {
502 unit->costs[k] = co->get_costs(co, irn, o, -1);
509 assert(0 && "This is not an optimizable node!");
513 /* Insert the new unit at a position according to its costs */
514 if (unit->node_count > 1) {
516 struct list_head *tmp;
518 /* Determine the maximum costs this unit can cause: all_nodes_cost */
519 for (i=1; i<unit->node_count; ++i) {
520 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
521 unit->all_nodes_costs += unit->costs[i];
524 /* Determine the minimal costs this unit will cause: min_nodes_costs */
525 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
526 /* Insert the new ou according to its sort_key */
528 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
530 list_add(&unit->units, tmp);
536 #ifdef QUICK_AND_DIRTY_HACK
538 static int compare_ous(const void *k1, const void *k2)
540 const unit_t *u1 = *((const unit_t **) k1);
541 const unit_t *u2 = *((const unit_t **) k2);
542 int i, o, u1_has_constr, u2_has_constr;
543 arch_register_req_t req;
545 /* Units with constraints come first */
547 for (i=0; i<u1->node_count; ++i) {
548 arch_get_register_req_out(&req, u1->nodes[i]);
549 if (arch_register_req_is(&req, limited)) {
556 for (i=0; i<u2->node_count; ++i) {
557 arch_get_register_req_out(&req, u2->nodes[i]);
558 if (arch_register_req_is(&req, limited)) {
564 if (u1_has_constr != u2_has_constr)
565 return u2_has_constr - u1_has_constr;
567 /* Now check, whether the two units are connected */
569 for (i=0; i<u1->node_count; ++i)
570 for (o=0; o<u2->node_count; ++o)
571 if (u1->nodes[i] == u2->nodes[o])
575 /* After all, the sort key decides. Greater keys come first. */
576 return u2->sort_key - u1->sort_key;
581 * Sort the ou's according to constraints and their sort_key
583 static void co_sort_units(copy_opt_t *co)
585 int i, count = 0, costs;
588 /* get the number of ous, remove them form the list and fill the array */
589 list_for_each_entry(unit_t, ou, &co->units, units)
591 ous = ALLOCAN(unit_t, count);
593 costs = co_get_max_copy_costs(co);
596 list_for_each_entry(unit_t, ou, &co->units, units)
599 INIT_LIST_HEAD(&co->units);
601 assert(count == i && list_empty(&co->units));
603 for (i=0; i<count; ++i)
604 ir_printf("%+F\n", ous[i]->nodes[0]);
606 qsort(ous, count, sizeof(*ous), compare_ous);
609 for (i=0; i<count; ++i)
610 ir_printf("%+F\n", ous[i]->nodes[0]);
612 /* reinsert into list in correct order */
613 for (i=0; i<count; ++i)
614 list_add_tail(&ous[i]->units, &co->units);
616 assert(costs == co_get_max_copy_costs(co));
620 void co_build_ou_structure(copy_opt_t *co)
622 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
623 INIT_LIST_HEAD(&co->units);
624 irg_walk_graph(co->irg, co_collect_units, NULL, co);
625 #ifdef QUICK_AND_DIRTY_HACK
630 void co_free_ou_structure(copy_opt_t *co)
634 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
639 co->units.next = NULL;
642 /* co_solve_heuristic() is implemented in becopyheur.c */
644 int co_get_max_copy_costs(const copy_opt_t *co)
651 list_for_each_entry(unit_t, curr, &co->units, units) {
652 res += curr->inevitable_costs;
653 for (i=1; i<curr->node_count; ++i)
654 res += curr->costs[i];
659 int co_get_inevit_copy_costs(const copy_opt_t *co)
666 list_for_each_entry(unit_t, curr, &co->units, units)
667 res += curr->inevitable_costs;
671 int co_get_copy_costs(const copy_opt_t *co)
678 list_for_each_entry(unit_t, curr, &co->units, units) {
679 int root_col = get_irn_col(curr->nodes[0]);
680 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
681 res += curr->inevitable_costs;
682 for (i=1; i<curr->node_count; ++i) {
683 int arg_col = get_irn_col(curr->nodes[i]);
684 if (root_col != arg_col) {
685 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
686 res += curr->costs[i];
693 int co_get_lower_bound(const copy_opt_t *co)
700 list_for_each_entry(unit_t, curr, &co->units, units)
701 res += curr->inevitable_costs + curr->min_nodes_costs;
705 void co_complete_stats(const copy_opt_t *co, co_complete_stats_t *stat)
707 bitset_t *seen = bitset_irg_malloc(co->irg);
710 memset(stat, 0, sizeof(stat[0]));
712 /* count affinity edges. */
713 co_gs_foreach_aff_node(co, an) {
715 stat->aff_nodes += 1;
716 bitset_add_irn(seen, an->irn);
717 co_gs_foreach_neighb(an, neigh) {
718 if (!bitset_contains_irn(seen, neigh->irn)) {
719 stat->aff_edges += 1;
720 stat->max_costs += neigh->costs;
722 if (get_irn_col(an->irn) != get_irn_col(neigh->irn)) {
723 stat->costs += neigh->costs;
724 stat->unsatisfied_edges += 1;
727 if (nodes_interfere(co->cenv, an->irn, neigh->irn)) {
729 stat->inevit_costs += neigh->costs;
739 /******************************************************************************
741 / ____| | | / ____| |
742 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
743 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
744 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
745 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
748 ******************************************************************************/
750 static int compare_affinity_node_t(const void *k1, const void *k2, size_t size)
752 const affinity_node_t *n1 = k1;
753 const affinity_node_t *n2 = k2;
756 return (n1->irn != n2->irn);
759 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs)
761 affinity_node_t new_node, *node;
767 new_node.neighbours = NULL;
768 node = set_insert(co->nodes, &new_node, sizeof(new_node), hash_irn(new_node.irn));
770 for (nbr = node->neighbours; nbr; nbr = nbr->next)
771 if (nbr->irn == n2) {
776 /* if we did not find n2 in n1's neighbourhood insert it */
778 nbr = OALLOC(&co->obst, neighb_t);
781 nbr->next = node->neighbours;
783 node->neighbours = nbr;
787 /* now nbr points to n1's neighbour-entry of n2 */
791 static inline void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs)
793 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
794 add_edge(co, n1, n2, costs);
795 add_edge(co, n2, n1, costs);
799 static void build_graph_walker(ir_node *irn, void *env)
801 const arch_register_req_t *req = arch_get_register_req_out(irn);
802 copy_opt_t *co = env;
804 const arch_register_t *reg;
806 if (req->cls != co->cls || arch_irn_is_ignore(irn))
809 reg = arch_get_irn_register(irn);
810 if (arch_register_type_is(reg, ignore))
813 if (is_Reg_Phi(irn)) { /* Phis */
814 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
815 ir_node *arg = get_irn_n(irn, pos);
816 add_edges(co, irn, arg, co->get_costs(co, irn, arg, pos));
818 } else if (is_Perm_Proj(irn)) { /* Perms */
819 ir_node *arg = get_Perm_src(irn);
820 add_edges(co, irn, arg, co->get_costs(co, irn, arg, 0));
821 } else { /* 2-address code */
822 if (is_2addr_code(req)) {
823 const unsigned other = req->other_same;
826 for (i = 0; 1U << i <= other; ++i) {
827 if (other & (1U << i)) {
828 ir_node *other = get_irn_n(skip_Proj(irn), i);
829 if (!arch_irn_is_ignore(other))
830 add_edges(co, irn, other, co->get_costs(co, irn, other, 0));
837 void co_build_graph_structure(copy_opt_t *co)
839 obstack_init(&co->obst);
840 co->nodes = new_set(compare_affinity_node_t, 32);
842 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
845 void co_free_graph_structure(copy_opt_t *co)
850 obstack_free(&co->obst, NULL);
854 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
856 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn)
858 affinity_node_t new_node, *n;
863 n = set_find(co->nodes, &new_node, sizeof(new_node), hash_irn(new_node.irn));
865 return (n->degree > 0);
870 static int co_dump_appel_disjoint_constraints(const copy_opt_t *co, ir_node *a, ir_node *b)
872 ir_node *nodes[] = { a, b };
873 bitset_t *constr[] = { NULL, NULL };
876 constr[0] = bitset_alloca(co->cls->n_regs);
877 constr[1] = bitset_alloca(co->cls->n_regs);
879 for (j = 0; j < 2; ++j) {
880 const arch_register_req_t *req = arch_get_register_req_out(nodes[j]);
881 if (arch_register_req_is(req, limited))
882 rbitset_copy_to_bitset(req->limited, constr[j]);
884 bitset_set_all(constr[j]);
888 return !bitset_intersect(constr[0], constr[1]);
891 void co_dump_appel_graph(const copy_opt_t *co, FILE *f)
893 be_ifg_t *ifg = co->cenv->ifg;
894 int *color_map = ALLOCAN(int, co->cls->n_regs);
895 int *node_map = XMALLOCN(int, get_irg_last_idx(co->irg) + 1);
903 for (i = 0; i < co->cls->n_regs; ++i) {
904 const arch_register_t *reg = &co->cls->regs[i];
905 color_map[i] = arch_register_type_is(reg, ignore) ? -1 : n_regs++;
909 * n contains the first node number.
910 * the values below n are the pre-colored register nodes
913 it = be_ifg_nodes_iter_alloca(ifg);
914 nit = be_ifg_neighbours_iter_alloca(ifg);
917 be_ifg_foreach_node(ifg, it, irn) {
918 if (arch_irn_is_ignore(irn))
920 node_map[get_irn_idx(irn)] = n++;
923 fprintf(f, "%d %d\n", n, n_regs);
925 be_ifg_foreach_node(ifg, it, irn) {
926 if (!arch_irn_is_ignore(irn)) {
927 int idx = node_map[get_irn_idx(irn)];
928 affinity_node_t *a = get_affinity_info(co, irn);
929 const arch_register_req_t *req = arch_get_register_req_out(irn);
932 if (arch_register_req_is(req, limited)) {
933 for (i = 0; i < co->cls->n_regs; ++i) {
934 if (!rbitset_is_set(req->limited, i) && color_map[i] >= 0)
935 fprintf(f, "%d %d -1\n", color_map[i], idx);
939 be_ifg_foreach_neighbour(ifg, nit, irn, adj) {
940 if (!arch_irn_is_ignore(adj) &&
941 !co_dump_appel_disjoint_constraints(co, irn, adj)) {
942 int adj_idx = node_map[get_irn_idx(adj)];
944 fprintf(f, "%d %d -1\n", idx, adj_idx);
951 co_gs_foreach_neighb(a, n) {
952 if (!arch_irn_is_ignore(n->irn)) {
953 int n_idx = node_map[get_irn_idx(n->irn)];
955 fprintf(f, "%d %d %d\n", idx, n_idx, (int) n->costs);
966 ___ _____ ____ ____ ___ _____ ____ _
967 |_ _| ___/ ___| | _ \ / _ \_ _| | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
968 | || |_ | | _ | | | | | | || | | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
969 | || _|| |_| | | |_| | |_| || | | |_| | |_| | | | | | | |_) | | | | | (_| |
970 |___|_| \____| |____/ \___/ |_| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
974 static const char *get_dot_color_name(size_t col)
976 static const char *names[] = {
1010 return col < sizeof(names)/sizeof(names[0]) ? names[col] : "white";
1013 typedef struct _co_ifg_dump_t {
1014 const copy_opt_t *co;
1018 static void ifg_dump_graph_attr(FILE *f, void *self)
1021 fprintf(f, "overlap=scale");
1024 static int ifg_is_dump_node(void *self, ir_node *irn)
1027 return !arch_irn_is_ignore(irn);
1030 static void ifg_dump_node_attr(FILE *f, void *self, ir_node *irn)
1032 co_ifg_dump_t *env = self;
1033 const arch_register_t *reg = arch_get_irn_register(irn);
1034 const arch_register_req_t *req = arch_get_register_req_out(irn);
1035 int limited = arch_register_req_is(req, limited);
1037 if (env->flags & CO_IFG_DUMP_LABELS) {
1038 ir_fprintf(f, "label=\"%+F", irn);
1040 if ((env->flags & CO_IFG_DUMP_CONSTR) && limited) {
1041 bitset_t *bs = bitset_alloca(env->co->cls->n_regs);
1042 rbitset_copy_to_bitset(req->limited, bs);
1043 ir_fprintf(f, "\\n%B", bs);
1045 ir_fprintf(f, "\" ");
1047 fprintf(f, "label=\"\" shape=point " );
1050 if (env->flags & CO_IFG_DUMP_SHAPE)
1051 fprintf(f, "shape=%s ", limited ? "diamond" : "ellipse");
1053 if (env->flags & CO_IFG_DUMP_COLORS)
1054 fprintf(f, "style=filled color=%s ", get_dot_color_name(reg->index));
1057 static void ifg_dump_at_end(FILE *file, void *self)
1059 co_ifg_dump_t *env = self;
1062 co_gs_foreach_aff_node(env->co, a) {
1063 const arch_register_t *ar = arch_get_irn_register(a->irn);
1064 unsigned aidx = get_irn_idx(a->irn);
1067 co_gs_foreach_neighb(a, n) {
1068 const arch_register_t *nr = arch_get_irn_register(n->irn);
1069 unsigned nidx = get_irn_idx(n->irn);
1072 const char *color = nr == ar ? "blue" : "red";
1073 fprintf(file, "\tn%d -- n%d [weight=0.01 ", aidx, nidx);
1074 if (env->flags & CO_IFG_DUMP_LABELS)
1075 fprintf(file, "label=\"%d\" ", n->costs);
1076 if (env->flags & CO_IFG_DUMP_COLORS)
1077 fprintf(file, "color=%s ", color);
1079 fprintf(file, "style=dotted");
1080 fprintf(file, "];\n");
1087 static be_ifg_dump_dot_cb_t ifg_dot_cb = {
1089 ifg_dump_graph_attr,
1098 void co_dump_ifg_dot(const copy_opt_t *co, FILE *f, unsigned flags)
1104 be_ifg_dump_dot(co->cenv->ifg, co->irg, f, &ifg_dot_cb, &cod);
1108 void co_solve_park_moon(copy_opt_t *opt)
1115 | \/ | __ _(_)_ __ | _ \ _ __(_)_ _____ _ __
1116 | |\/| |/ _` | | '_ \ | | | | '__| \ \ / / _ \ '__|
1117 | | | | (_| | | | | | | |_| | | | |\ V / __/ |
1118 |_| |_|\__,_|_|_| |_| |____/|_| |_| \_/ \___|_|
1122 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
1129 n = strlen(env->birg->main_env->cup_name);
1130 tu_name = XMALLOCN(char, n + 1);
1131 strcpy(tu_name, env->birg->main_env->cup_name);
1132 for (i = 0; i < n; ++i)
1133 if (tu_name[i] == '.')
1137 ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
1139 result = fopen(buf, "wt");
1140 if (result == NULL) {
1141 panic("Couldn't open '%s' for writing.", buf);
1147 void co_driver(be_chordal_env_t *cenv)
1149 ir_timer_t *timer = ir_timer_new();
1150 co_complete_stats_t before, after;
1152 int was_optimal = 0;
1154 assert(selected_copyopt);
1156 /* skip copymin if algo is 'none' */
1157 if (selected_copyopt->copyopt == void_algo)
1160 be_liveness_assure_chk(be_get_birg_liveness(cenv->birg));
1162 co = new_copy_opt(cenv, cost_func);
1163 co_build_ou_structure(co);
1164 co_build_graph_structure(co);
1166 co_complete_stats(co, &before);
1168 be_stat_ev_ull("co_aff_nodes", before.aff_nodes);
1169 be_stat_ev_ull("co_aff_edges", before.aff_edges);
1170 be_stat_ev_ull("co_max_costs", before.max_costs);
1171 be_stat_ev_ull("co_inevit_costs", before.inevit_costs);
1172 be_stat_ev_ull("co_aff_int", before.aff_int);
1174 be_stat_ev_ull("co_init_costs", before.costs);
1175 be_stat_ev_ull("co_init_unsat", before.unsatisfied_edges);
1177 if (dump_flags & DUMP_BEFORE) {
1178 FILE *f = my_open(cenv, "", "-before.dot");
1179 co_dump_ifg_dot(co, f, style_flags);
1183 /* if the algo can improve results, provide an initial solution with heur1 */
1184 if (improve && selected_copyopt->can_improve_existing) {
1185 co_complete_stats_t stats;
1187 /* produce a heuristic solution */
1188 co_solve_heuristic(co);
1190 /* do the stats and provide the current costs */
1191 co_complete_stats(co, &stats);
1192 be_stat_ev_ull("co_prepare_costs", stats.costs);
1195 /* perform actual copy minimization */
1196 ir_timer_reset_and_start(timer);
1197 was_optimal = selected_copyopt->copyopt(co);
1198 ir_timer_stop(timer);
1200 be_stat_ev("co_time", ir_timer_elapsed_msec(timer));
1201 be_stat_ev_ull("co_optimal", was_optimal);
1202 ir_timer_free(timer);
1204 if (dump_flags & DUMP_AFTER) {
1205 FILE *f = my_open(cenv, "", "-after.dot");
1206 co_dump_ifg_dot(co, f, style_flags);
1210 co_complete_stats(co, &after);
1213 ulong64 optimizable_costs = after.max_costs - after.inevit_costs;
1214 ulong64 evitable = after.costs - after.inevit_costs;
1216 ir_printf("%30F ", cenv->irg);
1217 printf("%10s %10" ULL_FMT "%10" ULL_FMT "%10" ULL_FMT, cenv->cls->name, after.max_costs, before.costs, after.inevit_costs);
1219 if (optimizable_costs > 0)
1220 printf("%10" ULL_FMT " %5.2f\n", after.costs, (evitable * 100.0) / optimizable_costs);
1222 printf("%10" ULL_FMT " %5s\n", after.costs, "-");
1225 /* Dump the interference graph in Appel's format. */
1226 if (dump_flags & DUMP_APPEL) {
1227 FILE *f = my_open(cenv, "", ".apl");
1228 fprintf(f, "# %lld %lld\n", after.costs, after.unsatisfied_edges);
1229 co_dump_appel_graph(co, f);
1233 be_stat_ev_ull("co_after_costs", after.costs);
1234 be_stat_ev_ull("co_after_unsat", after.unsatisfied_edges);
1236 co_free_graph_structure(co);
1237 co_free_ou_structure(co);