4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
25 #include "iredges_t.h"
28 #include "irphase_t.h"
29 #include "irprintf_t.h"
36 #include "becopyopt_t.h"
37 #include "becopystat.h"
40 #include "besched_t.h"
44 /* Insert additional options registration functions here. */
45 extern void be_co2_register_options(lc_opt_entry_t *grp);
46 extern void be_co3_register_options(lc_opt_entry_t *grp);
48 void co_register_options(lc_opt_entry_t *grp)
50 be_co2_register_options(grp);
51 be_co3_register_options(grp);
56 #undef QUICK_AND_DIRTY_HACK
58 /******************************************************************************
61 | | __ ___ _ __ ___ _ __ __ _| |
62 | | |_ |/ _ \ '_ \ / _ \ '__/ _` | |
63 | |__| | __/ | | | __/ | | (_| | |
64 \_____|\___|_| |_|\___|_| \__,_|_|
66 ******************************************************************************/
68 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
70 void be_copy_opt_init(void) {
73 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, cost_fct_t get_costs)
75 const char *s1, *s2, *s3;
79 FIRM_DBG_REGISTER(dbg, "ir.be.copyopt");
81 co = xcalloc(1, sizeof(*co));
82 co->cenv = chordal_env;
83 co->aenv = chordal_env->birg->main_env->arch_env;
84 co->irg = chordal_env->irg;
85 co->cls = chordal_env->cls;
86 co->get_costs = get_costs;
88 s1 = get_irp_prog_name();
89 s2 = get_entity_name(get_irg_entity(co->irg));
90 s3 = chordal_env->cls->name;
91 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
92 co->name = xmalloc(len);
93 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
98 void free_copy_opt(copy_opt_t *co) {
103 int co_is_optimizable_root(const copy_opt_t *co, ir_node *irn) {
104 arch_register_req_t req;
105 const arch_register_t *reg;
107 if (arch_irn_is(co->aenv, irn, ignore))
110 reg = arch_get_irn_register(co->aenv, irn);
111 if (arch_register_type_is(reg, ignore))
114 if (is_Reg_Phi(irn) || is_Perm_Proj(co->aenv, irn) || is_2addr_code(co->aenv, irn, &req))
120 int co_is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
121 const ir_edge_t *edge;
122 const arch_register_t *reg;
124 assert(0 && "Is buggy and obsolete. Do not use");
126 if (arch_irn_is(co->aenv, irn, ignore))
129 reg = arch_get_irn_register(co->aenv, irn);
130 if (arch_register_type_is(reg, ignore))
133 foreach_out_edge(irn, edge) {
134 ir_node *n = edge->src;
136 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
137 arch_register_req_t req;
138 arch_get_register_req(co->aenv, &req, n, -1);
141 is_Perm(co->aenv, n) ||
142 (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
151 int co_get_costs_loop_depth(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos) {
154 ir_node *root_block = get_nodes_block(root);
157 /* for phis the copies are placed in the corresponding pred-block */
158 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
160 /* a perm places the copy in the same block as it resides */
161 loop = get_irn_loop(root_block);
164 int d = get_loop_depth(loop);
170 int co_get_costs_exec_freq(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos) {
171 ir_node *root_bl = get_nodes_block(root);
172 ir_node *copy_bl = is_Phi(root) ? get_Block_cfgpred_block(root_bl, pos) : root_bl;
173 unsigned long freq = get_block_execfreq_ulong(co->cenv->exec_freq, copy_bl);
174 return freq > 0 ? (int) freq : 1;
178 int co_get_costs_all_one(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos) {
182 /******************************************************************************
183 ____ _ _ _ _ _ _____ _
184 / __ \ | | | | | | (_) | / ____| |
185 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
186 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
187 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
188 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
191 ******************************************************************************/
194 * Determines a maximum weighted independent set with respect to
195 * the interference and conflict edges of all nodes in a qnode.
197 static int ou_max_ind_set_costs(unit_t *ou) {
198 be_chordal_env_t *chordal_env = ou->co->cenv;
199 ir_node **safe, **unsafe;
200 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
202 int max, pos, curr_weight, best_weight = 0;
204 /* assign the nodes into two groups.
205 * safe: node has no interference, hence it is in every max stable set.
206 * unsafe: node has an interference
208 safe = alloca((ou->node_count-1) * sizeof(*safe));
211 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
212 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
214 for(i=1; i<ou->node_count; ++i) {
216 for(o=1; o<ou->node_count; ++o) {
219 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
220 unsafe_costs[unsafe_count] = ou->costs[i];
221 unsafe[unsafe_count] = ou->nodes[i];
228 safe_costs += ou->costs[i];
229 safe[safe_count++] = ou->nodes[i];
234 /* now compute the best set out of the unsafe nodes*/
235 if (unsafe_count > MIS_HEUR_TRIGGER) {
236 bitset_t *best = bitset_alloca(unsafe_count);
237 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
238 for (i=0; i<unsafe_count; ++i) {
240 /* check if it is a stable set */
241 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
242 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
243 bitset_clear(best, i); /* clear the bit and try next one */
247 /* compute the weight */
248 bitset_foreach(best, pos)
249 best_weight += unsafe_costs[pos];
251 /* Exact Algorithm: Brute force */
252 curr = bitset_alloca(unsafe_count);
253 bitset_set_all(curr);
254 while ((max = bitset_popcnt(curr)) != 0) {
255 /* check if curr is a stable set */
256 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
257 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) */
258 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
261 /* if we arrive here, we have a stable set */
262 /* compute the weigth of the stable set*/
264 bitset_foreach(curr, pos)
265 curr_weight += unsafe_costs[pos];
268 if (curr_weight > best_weight) {
269 best_weight = curr_weight;
277 return safe_costs+best_weight;
280 static void co_collect_units(ir_node *irn, void *env) {
281 copy_opt_t *co = env;
283 arch_register_req_t req;
285 if (!is_curr_reg_class(co, irn))
287 if (!co_is_optimizable_root(co, irn))
290 /* Init a new unit */
291 unit = xcalloc(1, sizeof(*unit));
293 unit->node_count = 1;
294 INIT_LIST_HEAD(&unit->queue);
296 /* Phi with some/all of its arguments */
297 if (is_Reg_Phi(irn)) {
301 arity = get_irn_arity(irn);
302 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
303 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
304 unit->nodes[0] = irn;
307 for (i=0; i<arity; ++i) {
309 ir_node *arg = get_irn_n(irn, i);
311 assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
314 if (nodes_interfere(co->cenv, irn, arg)) {
315 unit->inevitable_costs += co->get_costs(co, irn, arg, i);
319 /* Else insert the argument of the phi to the members of this ou */
320 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
322 /* Check if arg has occurred at a prior position in the arg/list */
324 for (o=0; o<unit->node_count; ++o)
325 if (unit->nodes[o] == arg) {
330 if (!arg_pos) { /* a new argument */
331 /* insert node, set costs */
332 unit->nodes[unit->node_count] = arg;
333 unit->costs[unit->node_count] = co->get_costs(co, irn, arg, i);
335 } else { /* arg has occured before in same phi */
336 /* increase costs for existing arg */
337 unit->costs[arg_pos] += co->get_costs(co, irn, arg, i);
340 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
341 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
344 /* Proj of a perm with corresponding arg */
345 if (is_Perm_Proj(co->aenv, irn)) {
346 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
347 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
348 unit->costs = xmalloc(2 * sizeof(*unit->costs));
349 unit->node_count = 2;
350 unit->nodes[0] = irn;
351 unit->nodes[1] = get_Perm_src(irn);
352 unit->costs[1] = co->get_costs(co, irn, unit->nodes[1], -1);
355 /* Src == Tgt of a 2-addr-code instruction */
356 if (is_2addr_code(co->aenv, irn, &req)) {
357 ir_node *other = req.other_same;
358 if (!nodes_interfere(co->cenv, irn, other)) {
359 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
360 unit->costs = xmalloc(2 * sizeof(*unit->costs));
361 unit->node_count = 2;
362 unit->nodes[0] = irn;
363 unit->nodes[1] = other;
364 unit->costs[1] = co->get_costs(co, irn, other, -1);
367 assert(0 && "This is not an optimizable node!");
369 /* Insert the new unit at a position according to its costs */
370 if (unit->node_count > 1) {
372 struct list_head *tmp;
374 /* Determine the maximum costs this unit can cause: all_nodes_cost */
375 for(i=1; i<unit->node_count; ++i) {
376 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
377 unit->all_nodes_costs += unit->costs[i];
380 /* Determine the minimal costs this unit will cause: min_nodes_costs */
381 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
382 /* Insert the new ou according to its sort_key */
384 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
386 list_add(&unit->units, tmp);
392 #ifdef QUICK_AND_DIRTY_HACK
394 static int compare_ous(const void *k1, const void *k2) {
395 const unit_t *u1 = *((const unit_t **) k1);
396 const unit_t *u2 = *((const unit_t **) k2);
397 int i, o, u1_has_constr, u2_has_constr;
398 arch_register_req_t req;
399 const arch_env_t *aenv = u1->co->aenv;
401 /* Units with constraints come first */
403 for (i=0; i<u1->node_count; ++i) {
404 arch_get_register_req(aenv, &req, u1->nodes[i], -1);
405 if (arch_register_req_is(&req, limited)) {
412 for (i=0; i<u2->node_count; ++i) {
413 arch_get_register_req(aenv, &req, u2->nodes[i], -1);
414 if (arch_register_req_is(&req, limited)) {
420 if (u1_has_constr != u2_has_constr)
421 return u2_has_constr - u1_has_constr;
423 /* Now check, whether the two units are connected */
425 for (i=0; i<u1->node_count; ++i)
426 for (o=0; o<u2->node_count; ++o)
427 if (u1->nodes[i] == u2->nodes[o])
431 /* After all, the sort key decides. Greater keys come first. */
432 return u2->sort_key - u1->sort_key;
437 * Sort the ou's according to constraints and their sort_key
439 static void co_sort_units(copy_opt_t *co) {
440 int i, count = 0, costs;
443 /* get the number of ous, remove them form the list and fill the array */
444 list_for_each_entry(unit_t, ou, &co->units, units)
446 ous = alloca(count * sizeof(*ous));
448 costs = co_get_max_copy_costs(co);
451 list_for_each_entry(unit_t, ou, &co->units, units)
454 INIT_LIST_HEAD(&co->units);
456 assert(count == i && list_empty(&co->units));
458 for (i=0; i<count; ++i)
459 ir_printf("%+F\n", ous[i]->nodes[0]);
461 qsort(ous, count, sizeof(*ous), compare_ous);
464 for (i=0; i<count; ++i)
465 ir_printf("%+F\n", ous[i]->nodes[0]);
467 /* reinsert into list in correct order */
468 for (i=0; i<count; ++i)
469 list_add_tail(&ous[i]->units, &co->units);
471 assert(costs == co_get_max_copy_costs(co));
475 void co_build_ou_structure(copy_opt_t *co) {
476 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
477 INIT_LIST_HEAD(&co->units);
478 irg_walk_graph(co->irg, co_collect_units, NULL, co);
479 #ifdef QUICK_AND_DIRTY_HACK
484 void co_free_ou_structure(copy_opt_t *co) {
487 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
492 co->units.next = NULL;
495 /* co_solve_heuristic() is implemented in becopyheur.c */
497 int co_get_max_copy_costs(const copy_opt_t *co) {
503 list_for_each_entry(unit_t, curr, &co->units, units) {
504 res += curr->inevitable_costs;
505 for (i=1; i<curr->node_count; ++i)
506 res += curr->costs[i];
511 int co_get_inevit_copy_costs(const copy_opt_t *co) {
517 list_for_each_entry(unit_t, curr, &co->units, units)
518 res += curr->inevitable_costs;
522 int co_get_copy_costs(const copy_opt_t *co) {
528 list_for_each_entry(unit_t, curr, &co->units, units) {
529 int root_col = get_irn_col(co, curr->nodes[0]);
530 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
531 res += curr->inevitable_costs;
532 for (i=1; i<curr->node_count; ++i) {
533 int arg_col = get_irn_col(co, curr->nodes[i]);
534 if (root_col != arg_col) {
535 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
536 res += curr->costs[i];
543 int co_get_lower_bound(const copy_opt_t *co) {
549 list_for_each_entry(unit_t, curr, &co->units, units)
550 res += curr->inevitable_costs + curr->min_nodes_costs;
554 /******************************************************************************
556 / ____| | | / ____| |
557 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
558 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
559 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
560 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
563 ******************************************************************************/
565 static int compare_affinity_node_t(const void *k1, const void *k2, size_t size) {
566 const affinity_node_t *n1 = k1;
567 const affinity_node_t *n2 = k2;
569 return (n1->irn != n2->irn);
572 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
573 affinity_node_t new_node, *node;
574 neighb_t new_nbr, *nbr;
579 new_node.neighbours = NULL;
580 node = set_insert(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
583 for (nbr = node->neighbours; nbr; nbr = nbr->next)
584 if (nbr->irn == n2) {
589 /* if we did not find n2 in n1's neighbourhood insert it */
591 obstack_grow(&co->obst, &new_nbr, sizeof(new_nbr));
592 nbr = obstack_finish(&co->obst);
595 nbr->next = node->neighbours;
596 node->neighbours = nbr;
600 /* now nbr points to n1's neighbour-entry of n2 */
604 static INLINE void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
605 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
606 add_edge(co, n1, n2, costs);
607 add_edge(co, n2, n1, costs);
611 static void build_graph_walker(ir_node *irn, void *env) {
612 copy_opt_t *co = env;
614 arch_register_req_t req;
615 const arch_register_t *reg;
617 if (!is_curr_reg_class(co, irn) || arch_irn_is(co->aenv, irn, ignore))
620 reg = arch_get_irn_register(co->aenv, irn);
621 if (arch_register_type_is(reg, ignore))
626 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
627 ir_node *arg = get_irn_n(irn, pos);
628 add_edges(co, irn, arg, co->get_costs(co, irn, arg, pos));
632 else if (is_Perm_Proj(co->aenv, irn)) {
633 ir_node *arg = get_Perm_src(irn);
634 add_edges(co, irn, arg, co->get_costs(co, irn, arg, 0));
638 else if (is_2addr_code(co->aenv, irn, &req))
639 add_edges(co, irn, req.other_same, co->get_costs(co, irn, req.other_same, 0));
642 void co_build_graph_structure(copy_opt_t *co) {
643 obstack_init(&co->obst);
644 co->nodes = new_set(compare_affinity_node_t, 32);
646 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
649 void co_free_graph_structure(copy_opt_t *co) {
653 obstack_free(&co->obst, NULL);
657 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
659 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn) {
660 affinity_node_t new_node, *n;
665 n = set_find(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
667 return (n->degree > 0);
672 void co_dump_appel_graph(const copy_opt_t *co, FILE *f)
674 be_ifg_t *ifg = co->cenv->ifg;
675 int *color_map = alloca(co->cls->n_regs * sizeof(color_map[0]));
676 bitset_t *adm = bitset_alloca(co->cls->n_regs);
683 for(i = 0; i < co->cls->n_regs; ++i) {
684 const arch_register_t *reg = &co->cls->regs[i];
685 color_map[i] = arch_register_type_is(reg, ignore) ? -1 : n_regs++;
689 * n contains the first node number.
690 * the values below n are the pre-colored register nodes
693 it = be_ifg_nodes_iter_alloca(ifg);
694 nit = be_ifg_neighbours_iter_alloca(ifg);
697 be_ifg_foreach_node(ifg, it, irn) {
698 if(!arch_irn_is(co->aenv, irn, ignore))
699 set_irn_link(irn, INT_TO_PTR(n++));
702 fprintf(f, "%d %d\n", n, n_regs);
704 be_ifg_foreach_node(ifg, it, irn) {
705 if(!arch_irn_is(co->aenv, irn, ignore)) {
706 int idx = PTR_TO_INT(get_irn_link(irn));
707 affinity_node_t *a = get_affinity_info(co, irn);
709 arch_register_req_t req;
712 arch_get_register_req(co->aenv, &req, irn, BE_OUT_POS(0));
713 if(arch_register_req_is(&req, limited)) {
714 bitset_clear_all(adm);
715 req.limited(req.limited_env, adm);
716 for(i = 0; i < co->cls->n_regs; ++i)
717 if(!bitset_is_set(adm, i) && color_map[i] >= 0)
718 fprintf(f, "%d %d -1\n", color_map[i], idx);
723 be_ifg_foreach_neighbour(ifg, nit, irn, adj) {
724 if(!arch_irn_is(co->aenv, adj, ignore)) {
725 int adj_idx = PTR_TO_INT(get_irn_link(adj));
727 fprintf(f, "%d %d -1\n", idx, adj_idx);
734 co_gs_foreach_neighb(a, n) {
735 if(!arch_irn_is(co->aenv, n->irn, ignore)) {
736 int n_idx = PTR_TO_INT(get_irn_link(n->irn));
738 fprintf(f, "%d %d %d\n", idx, n_idx, n->costs);
746 typedef struct _appel_clique_walker_t {
748 const copy_opt_t *co;
755 } appel_clique_walker_t;
757 typedef struct _appel_block_info_t {
767 } appel_block_info_t;
769 static int appel_aff_weight(const appel_clique_walker_t *env, ir_node *bl)
772 double freq = get_block_execfreq(env->co->cenv->execfreq, bl);
773 int res = (int) freq;
774 return res == 0 ? 1 : res;
776 ir_loop *loop = get_irn_loop(bl);
778 int d = get_loop_depth(loop);
785 static void *appel_clique_walker_irn_init(phase_t *phase, ir_node *irn, void *old)
787 appel_block_info_t *res = NULL;
790 appel_clique_walker_t *d = (void *) phase;
791 res = phase_alloc(phase, sizeof(res[0]));
792 res->phi_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end_nr));
793 res->live_end_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end_nr));
794 res->live_in_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in_nr));
795 res->live_end = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end));
796 res->live_in = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in));
797 res->phi = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in));
803 typedef struct _insn_list_t {
805 struct list_head list;
808 static int appel_get_live_end_nr(appel_clique_walker_t *env, ir_node *bl, ir_node *irn)
810 appel_block_info_t *bli = phase_get_irn_data(&env->ph, bl);
813 for(i = 0; i < bli->n_live_end; ++i)
814 if(bli->live_end[i] == irn)
815 return bli->live_end_nr[i];
820 static int appel_dump_clique(appel_clique_walker_t *env, pset *live, ir_node *bl, int curr_nr, int start_nr)
822 ir_node **live_arr = alloca(env->co->cls->n_regs * sizeof(live_arr[0]));
828 foreach_pset(live, irn)
829 live_arr[n_live++] = irn;
831 /* dump the live after clique */
833 for(j = 0; j < n_live; ++j) {
836 for(k = j + 1; k < n_live; ++k) {
837 fprintf(env->f, "%d %d -1 ", curr_nr + j, curr_nr + k);
839 fprintf(env->f, "\n");
843 /* dump the affinities */
844 for(j = 0; !env->dumb && j < n_live; ++j) {
845 ir_node *irn = live_arr[j];
846 int old_nr = PTR_TO_INT(get_irn_link(irn));
848 /* if the node was already live in the last insn dump the affinity */
849 if(old_nr > start_nr) {
850 int weight = appel_aff_weight(env, bl);
851 fprintf(env->f, "%d %d %d\n", old_nr, curr_nr + j, weight);
855 /* set the current numbers into the link field. */
856 for(j = 0; j < n_live; ++j) {
857 ir_node *irn = live_arr[j];
858 set_irn_link(irn, INT_TO_PTR(curr_nr + j));
861 return curr_nr + n_live;
864 static void appel_walker(ir_node *bl, void *data)
866 appel_clique_walker_t *env = data;
867 appel_block_info_t *bli = phase_get_or_set_irn_data(&env->ph, bl);
868 struct obstack *obst = &env->obst;
869 void *base = obstack_base(obst);
870 pset *live = pset_new_ptr_default();
874 int start_nr = env->curr_nr;
875 int curr_nr = start_nr;
877 be_insn_env_t insn_env;
882 insn_env.aenv = env->co->aenv;
883 insn_env.cls = env->co->cls;
884 insn_env.obst = obst;
885 insn_env.ignore_colors = env->co->cenv->ignore_colors;
887 /* Guess how many insns will be in this block. */
888 sched_foreach(bl, irn)
892 insns = malloc(n_nodes * sizeof(insns[0]));
894 /* Put all insns in an array. */
895 irn = sched_first(bl);
896 while(!sched_is_end(irn)) {
898 insn = be_scan_insn(&insn_env, irn);
899 insns[n_insns++] = insn;
900 irn = insn->next_insn;
903 DBG((env->co->cenv->dbg, LEVEL_2, "%+F\n", bl));
904 be_liveness_end_of_block(env->co->cenv->lv, env->co->aenv, env->co->cls, bl, live);
906 /* Generate the bad and ugly. */
907 for(i = n_insns - 1; i >= 0; --i) {
908 be_insn_t *insn = insns[i];
910 /* The first live set has to be saved in the block border set. */
911 if(i == n_insns - 1) {
913 foreach_pset(live, irn) {
914 bli->live_end[j] = irn;
915 bli->live_end_nr[j] = curr_nr + j;
922 for(j = 0; j < insn->use_start; ++j) {
923 ir_node *op = insn->ops[j].carrier;
924 bitset_t *adm = insn->ops[j].regs;
928 if(!insn->ops[j].has_constraints)
932 foreach_pset(live, irn) {
940 assert(nr < pset_count(live));
942 for(k = 0; k < env->co->cls->n_regs; ++k) {
943 int mapped_col = env->color_map[k];
944 if(mapped_col >= 0 && !bitset_is_set(adm, k) && !bitset_is_set(env->co->cenv->ignore_colors, k))
945 fprintf(env->f, "%d %d -1\n", curr_nr + nr, mapped_col);
950 /* dump the clique and update the stuff. */
951 curr_nr = appel_dump_clique(env, live, bl, curr_nr, start_nr);
953 /* remove all defs. */
954 for(j = 0; j < insn->use_start; ++j)
955 pset_remove_ptr(live, insn->ops[j].carrier);
957 if(is_Phi(insn->irn) && arch_irn_consider_in_reg_alloc(env->co->aenv, env->co->cls, insn->irn)) {
958 bli->phi[bli->n_phi] = insn->irn;
959 bli->phi_nr[bli->n_phi] = PTR_TO_INT(get_irn_link(insn->irn));
965 for(j = insn->use_start; j < insn->n_ops; ++j)
966 pset_insert_ptr(live, insn->ops[j].carrier);
969 /* print the start clique. */
970 curr_nr = appel_dump_clique(env, live, bl, curr_nr, start_nr);
973 foreach_pset(live, irn) {
974 bli->live_in[i] = irn;
975 bli->live_in_nr[i] = PTR_TO_INT(get_irn_link(irn));
982 obstack_free(obst, base);
983 env->curr_nr = curr_nr;
986 static void appel_inter_block_aff(ir_node *bl, void *data)
988 appel_clique_walker_t *env = data;
989 appel_block_info_t *bli = phase_get_irn_data(&env->ph, bl);
993 for(i = 0; i < bli->n_live_in; ++i) {
994 ir_node *irn = bli->live_in[i];
996 for(j = 0, n = get_Block_n_cfgpreds(bl); j < n; ++j) {
997 ir_node *pred = get_Block_cfgpred_block(bl, j);
998 appel_block_info_t *pred_bli = phase_get_irn_data(&env->ph, pred);
1000 int nr = appel_get_live_end_nr(env, pred, irn);
1002 fprintf(env->f, "%d %d 1\n", bli->live_in_nr[i], nr);
1006 for(i = 0; i < bli->n_phi; ++i) {
1007 ir_node *irn = bli->phi[i];
1009 for(j = 0, n = get_Block_n_cfgpreds(bl); j < n; ++j) {
1010 ir_node *pred = get_Block_cfgpred_block(bl, j);
1011 appel_block_info_t *pred_bli = phase_get_irn_data(&env->ph, pred);
1012 ir_node *op = get_irn_n(irn, j);
1014 int nr = appel_get_live_end_nr(env, pred, op);
1016 fprintf(env->f, "%d %d 1\n", bli->phi_nr[i], nr);
1022 void co_dump_appel_graph_cliques(const copy_opt_t *co, FILE *f)
1026 appel_clique_walker_t env;
1027 bitset_t *adm = bitset_alloca(co->cls->n_regs);
1029 be_liveness_recompute(co->cenv->lv);
1030 obstack_init(&env.obst);
1031 phase_init(&env.ph, "appel_clique_dumper", co->irg, PHASE_DEFAULT_GROWTH, appel_clique_walker_irn_init);
1032 env.curr_nr = co->cls->n_regs;
1036 bitset_copy(adm, co->cenv->ignore_colors);
1037 bitset_flip_all(adm);
1039 /* Make color map. */
1040 env.color_map = alloca(co->cls->n_regs * sizeof(env.color_map[0]));
1041 for(i = 0, n_colors = 0; i < co->cls->n_regs; ++i) {
1042 const arch_register_t *reg = &co->cls->regs[i];
1043 env.color_map[i] = arch_register_type_is(reg, ignore) ? -1 : n_colors++;
1047 env.curr_nr = n_colors;
1048 irg_block_walk_graph(co->irg, firm_clear_link, NULL, NULL);
1049 irg_block_walk_graph(co->irg, appel_walker, NULL, &env);
1051 fprintf(f, "%d %d\n", env.curr_nr, n_colors);
1053 /* make the first k nodes interfere */
1054 for(i = 0; i < n_colors; ++i) {
1056 for(j = i + 1; j < n_colors; ++j)
1057 fprintf(f, "%d %d -1 ", i, j);
1062 env.curr_nr = n_colors;
1063 irg_block_walk_graph(co->irg, firm_clear_link, NULL, NULL);
1064 irg_block_walk_graph(co->irg, appel_walker, NULL, &env);
1065 irg_block_walk_graph(co->irg, appel_inter_block_aff, NULL, &env);
1066 obstack_free(&env.obst, NULL);
1070 ___ _____ ____ ____ ___ _____ ____ _
1071 |_ _| ___/ ___| | _ \ / _ \_ _| | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1072 | || |_ | | _ | | | | | | || | | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1073 | || _|| |_| | | |_| | |_| || | | |_| | |_| | | | | | | |_) | | | | | (_| |
1074 |___|_| \____| |____/ \___/ |_| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1078 static const char *get_dot_color_name(int col)
1080 static const char *names[] = {
1114 return col < sizeof(names)/sizeof(names[0]) ? names[col] : "white";
1117 typedef struct _co_ifg_dump_t {
1118 const copy_opt_t *co;
1122 static const char *get_dot_shape_name(co_ifg_dump_t *cod, ir_node *irn)
1124 arch_register_req_t req;
1126 arch_get_register_req(cod->co->aenv, &req, irn, BE_OUT_POS(0));
1127 if(arch_register_req_is(&req, limited))
1133 static void ifg_dump_graph_attr(FILE *f, void *self)
1135 fprintf(f, "overlay=false");
1138 static int ifg_is_dump_node(void *self, ir_node *irn)
1140 co_ifg_dump_t *cod = self;
1141 return !arch_irn_is(cod->co->aenv, irn, ignore);
1144 static void ifg_dump_node_attr(FILE *f, void *self, ir_node *irn)
1146 co_ifg_dump_t *env = self;
1147 const arch_register_t *reg = arch_get_irn_register(env->co->aenv, irn);
1149 ir_fprintf(f, "label=\"%+F\" style=filled color=%s shape=%s", irn, get_dot_color_name(reg->index), get_dot_shape_name(env, irn));
1152 static void ifg_dump_at_end(FILE *file, void *self)
1154 co_ifg_dump_t *env = self;
1157 co_gs_foreach_aff_node(env->co, a) {
1158 const arch_register_t *ar = arch_get_irn_register(env->co->aenv, a->irn);
1159 unsigned aidx = get_irn_idx(a->irn);
1162 co_gs_foreach_neighb(a, n) {
1163 const arch_register_t *nr = arch_get_irn_register(env->co->aenv, n->irn);
1164 int nidx = get_irn_idx(n->irn);
1167 const char *color = nr == ar ? "blue" : "red";
1168 fprintf(file, "\tn%d -- n%d [label=\"%d\" style=dashed color=%s];\n", aidx, nidx, n->costs, color);
1175 static be_ifg_dump_dot_cb_t ifg_dot_cb = {
1177 ifg_dump_graph_attr,
1186 void co_dump_ifg_dot(const copy_opt_t *co, FILE *f, unsigned flags)
1192 be_ifg_dump_dot(co->cenv->ifg, co->irg, f, &ifg_dot_cb, &cod);
1196 void co_solve_park_moon(copy_opt_t *opt)