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"
34 #include "becopyopt_t.h"
35 #include "becopystat.h"
38 #include "besched_t.h"
42 /* Insert additional options registration functions here. */
43 extern void be_co2_register_options(lc_opt_entry_t *grp);
45 void co_register_options(lc_opt_entry_t *grp)
47 be_co2_register_options(grp);
52 #undef QUICK_AND_DIRTY_HACK
54 /******************************************************************************
57 | | __ ___ _ __ ___ _ __ __ _| |
58 | | |_ |/ _ \ '_ \ / _ \ '__/ _` | |
59 | |__| | __/ | | | __/ | | (_| | |
60 \_____|\___|_| |_|\___|_| \__,_|_|
62 ******************************************************************************/
64 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
66 void be_copy_opt_init(void) {
69 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, cost_fct_t get_costs)
71 const char *s1, *s2, *s3;
75 FIRM_DBG_REGISTER(dbg, "ir.be.copyopt");
77 co = xcalloc(1, sizeof(*co));
78 co->cenv = chordal_env;
79 co->aenv = chordal_env->birg->main_env->arch_env;
80 co->irg = chordal_env->irg;
81 co->cls = chordal_env->cls;
82 co->get_costs = get_costs;
84 s1 = get_irp_prog_name();
85 s2 = get_entity_name(get_irg_entity(co->irg));
86 s3 = chordal_env->cls->name;
87 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
88 co->name = xmalloc(len);
89 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
94 void free_copy_opt(copy_opt_t *co) {
99 int co_is_optimizable_root(const copy_opt_t *co, ir_node *irn) {
100 arch_register_req_t req;
101 const arch_register_t *reg;
103 if (arch_irn_is(co->aenv, irn, ignore))
106 reg = arch_get_irn_register(co->aenv, irn);
107 if (arch_register_type_is(reg, ignore))
110 if (is_Reg_Phi(irn) || is_Perm_Proj(co->aenv, irn) || is_2addr_code(co->aenv, irn, &req))
116 int co_is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
117 const ir_edge_t *edge;
118 const arch_register_t *reg;
120 assert(0 && "Is buggy and obsolete. Do not use");
122 if (arch_irn_is(co->aenv, irn, ignore))
125 reg = arch_get_irn_register(co->aenv, irn);
126 if (arch_register_type_is(reg, ignore))
129 foreach_out_edge(irn, edge) {
130 ir_node *n = edge->src;
132 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
133 arch_register_req_t req;
134 arch_get_register_req(co->aenv, &req, n, -1);
137 is_Perm(co->aenv, n) ||
138 (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
147 int co_get_costs_loop_depth(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos) {
150 ir_node *root_block = get_nodes_block(root);
153 /* for phis the copies are placed in the corresponding pred-block */
154 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
156 /* a perm places the copy in the same block as it resides */
157 loop = get_irn_loop(root_block);
160 int d = get_loop_depth(loop);
166 int co_get_costs_exec_freq(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos) {
167 ir_node *root_bl = get_nodes_block(root);
168 ir_node *copy_bl = is_Phi(root) ? get_Block_cfgpred_block(root_bl, pos) : root_bl;
169 return (int) get_block_execfreq(co->cenv->exec_freq, copy_bl);
173 int co_get_costs_all_one(const copy_opt_t *co, ir_node *root, ir_node* arg, int pos) {
177 /******************************************************************************
178 ____ _ _ _ _ _ _____ _
179 / __ \ | | | | | | (_) | / ____| |
180 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
181 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
182 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
183 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
186 ******************************************************************************/
189 * Determines a maximum weighted independent set with respect to
190 * the interference and conflict edges of all nodes in a qnode.
192 static int ou_max_ind_set_costs(unit_t *ou) {
193 be_chordal_env_t *chordal_env = ou->co->cenv;
194 ir_node **safe, **unsafe;
195 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
197 int max, pos, curr_weight, best_weight = 0;
199 /* assign the nodes into two groups.
200 * safe: node has no interference, hence it is in every max stable set.
201 * unsafe: node has an interference
203 safe = alloca((ou->node_count-1) * sizeof(*safe));
206 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
207 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
209 for(i=1; i<ou->node_count; ++i) {
211 for(o=1; o<ou->node_count; ++o) {
214 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
215 unsafe_costs[unsafe_count] = ou->costs[i];
216 unsafe[unsafe_count] = ou->nodes[i];
223 safe_costs += ou->costs[i];
224 safe[safe_count++] = ou->nodes[i];
229 /* now compute the best set out of the unsafe nodes*/
230 if (unsafe_count > MIS_HEUR_TRIGGER) {
231 bitset_t *best = bitset_alloca(unsafe_count);
232 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
233 for (i=0; i<unsafe_count; ++i) {
235 /* check if it is a stable set */
236 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
237 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
238 bitset_clear(best, i); /* clear the bit and try next one */
242 /* compute the weight */
243 bitset_foreach(best, pos)
244 best_weight += unsafe_costs[pos];
246 /* Exact Algorithm: Brute force */
247 curr = bitset_alloca(unsafe_count);
248 bitset_set_all(curr);
249 while ((max = bitset_popcnt(curr)) != 0) {
250 /* check if curr is a stable set */
251 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
252 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) */
253 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
256 /* if we arrive here, we have a stable set */
257 /* compute the weigth of the stable set*/
259 bitset_foreach(curr, pos)
260 curr_weight += unsafe_costs[pos];
263 if (curr_weight > best_weight) {
264 best_weight = curr_weight;
272 return safe_costs+best_weight;
275 static void co_collect_units(ir_node *irn, void *env) {
276 copy_opt_t *co = env;
278 arch_register_req_t req;
280 if (!is_curr_reg_class(co, irn))
282 if (!co_is_optimizable_root(co, irn))
285 /* Init a new unit */
286 unit = xcalloc(1, sizeof(*unit));
288 unit->node_count = 1;
289 INIT_LIST_HEAD(&unit->queue);
291 /* Phi with some/all of its arguments */
292 if (is_Reg_Phi(irn)) {
296 arity = get_irn_arity(irn);
297 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
298 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
299 unit->nodes[0] = irn;
302 for (i=0; i<arity; ++i) {
304 ir_node *arg = get_irn_n(irn, i);
306 assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
309 if (nodes_interfere(co->cenv, irn, arg)) {
310 unit->inevitable_costs += co->get_costs(co, irn, arg, i);
314 /* Else insert the argument of the phi to the members of this ou */
315 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
317 /* Check if arg has occurred at a prior position in the arg/list */
319 for (o=0; o<unit->node_count; ++o)
320 if (unit->nodes[o] == arg) {
325 if (!arg_pos) { /* a new argument */
326 /* insert node, set costs */
327 unit->nodes[unit->node_count] = arg;
328 unit->costs[unit->node_count] = co->get_costs(co, irn, arg, i);
330 } else { /* arg has occured before in same phi */
331 /* increase costs for existing arg */
332 unit->costs[arg_pos] += co->get_costs(co, irn, arg, i);
335 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
336 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
339 /* Proj of a perm with corresponding arg */
340 if (is_Perm_Proj(co->aenv, irn)) {
341 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
342 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
343 unit->costs = xmalloc(2 * sizeof(*unit->costs));
344 unit->node_count = 2;
345 unit->nodes[0] = irn;
346 unit->nodes[1] = get_Perm_src(irn);
347 unit->costs[1] = co->get_costs(co, irn, unit->nodes[1], -1);
350 /* Src == Tgt of a 2-addr-code instruction */
351 if (is_2addr_code(co->aenv, irn, &req)) {
352 ir_node *other = req.other_same;
353 if (!nodes_interfere(co->cenv, irn, other)) {
354 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
355 unit->costs = xmalloc(2 * sizeof(*unit->costs));
356 unit->node_count = 2;
357 unit->nodes[0] = irn;
358 unit->nodes[1] = other;
359 unit->costs[1] = co->get_costs(co, irn, other, -1);
362 assert(0 && "This is not an optimizable node!");
364 /* Insert the new unit at a position according to its costs */
365 if (unit->node_count > 1) {
367 struct list_head *tmp;
369 /* Determine the maximum costs this unit can cause: all_nodes_cost */
370 for(i=1; i<unit->node_count; ++i) {
371 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
372 unit->all_nodes_costs += unit->costs[i];
375 /* Determine the minimal costs this unit will cause: min_nodes_costs */
376 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
377 /* Insert the new ou according to its sort_key */
379 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
381 list_add(&unit->units, tmp);
387 #ifdef QUICK_AND_DIRTY_HACK
389 static int compare_ous(const void *k1, const void *k2) {
390 const unit_t *u1 = *((const unit_t **) k1);
391 const unit_t *u2 = *((const unit_t **) k2);
392 int i, o, u1_has_constr, u2_has_constr;
393 arch_register_req_t req;
394 const arch_env_t *aenv = u1->co->aenv;
396 /* Units with constraints come first */
398 for (i=0; i<u1->node_count; ++i) {
399 arch_get_register_req(aenv, &req, u1->nodes[i], -1);
400 if (arch_register_req_is(&req, limited)) {
407 for (i=0; i<u2->node_count; ++i) {
408 arch_get_register_req(aenv, &req, u2->nodes[i], -1);
409 if (arch_register_req_is(&req, limited)) {
415 if (u1_has_constr != u2_has_constr)
416 return u2_has_constr - u1_has_constr;
418 /* Now check, whether the two units are connected */
420 for (i=0; i<u1->node_count; ++i)
421 for (o=0; o<u2->node_count; ++o)
422 if (u1->nodes[i] == u2->nodes[o])
426 /* After all, the sort key decides. Greater keys come first. */
427 return u2->sort_key - u1->sort_key;
432 * Sort the ou's according to constraints and their sort_key
434 static void co_sort_units(copy_opt_t *co) {
435 int i, count = 0, costs;
438 /* get the number of ous, remove them form the list and fill the array */
439 list_for_each_entry(unit_t, ou, &co->units, units)
441 ous = alloca(count * sizeof(*ous));
443 costs = co_get_max_copy_costs(co);
446 list_for_each_entry(unit_t, ou, &co->units, units)
449 INIT_LIST_HEAD(&co->units);
451 assert(count == i && list_empty(&co->units));
453 for (i=0; i<count; ++i)
454 ir_printf("%+F\n", ous[i]->nodes[0]);
456 qsort(ous, count, sizeof(*ous), compare_ous);
459 for (i=0; i<count; ++i)
460 ir_printf("%+F\n", ous[i]->nodes[0]);
462 /* reinsert into list in correct order */
463 for (i=0; i<count; ++i)
464 list_add_tail(&ous[i]->units, &co->units);
466 assert(costs == co_get_max_copy_costs(co));
470 void co_build_ou_structure(copy_opt_t *co) {
471 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
472 INIT_LIST_HEAD(&co->units);
473 irg_walk_graph(co->irg, co_collect_units, NULL, co);
474 #ifdef QUICK_AND_DIRTY_HACK
479 void co_free_ou_structure(copy_opt_t *co) {
482 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
487 co->units.next = NULL;
490 /* co_solve_heuristic() is implemented in becopyheur.c */
492 int co_get_max_copy_costs(const copy_opt_t *co) {
498 list_for_each_entry(unit_t, curr, &co->units, units) {
499 res += curr->inevitable_costs;
500 for (i=1; i<curr->node_count; ++i)
501 res += curr->costs[i];
506 int co_get_inevit_copy_costs(const copy_opt_t *co) {
512 list_for_each_entry(unit_t, curr, &co->units, units)
513 res += curr->inevitable_costs;
517 int co_get_copy_costs(const copy_opt_t *co) {
523 list_for_each_entry(unit_t, curr, &co->units, units) {
524 int root_col = get_irn_col(co, curr->nodes[0]);
525 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
526 res += curr->inevitable_costs;
527 for (i=1; i<curr->node_count; ++i) {
528 int arg_col = get_irn_col(co, curr->nodes[i]);
529 if (root_col != arg_col) {
530 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
531 res += curr->costs[i];
538 int co_get_lower_bound(const copy_opt_t *co) {
544 list_for_each_entry(unit_t, curr, &co->units, units)
545 res += curr->inevitable_costs + curr->min_nodes_costs;
549 /******************************************************************************
551 / ____| | | / ____| |
552 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
553 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
554 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
555 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
558 ******************************************************************************/
560 static int compare_affinity_node_t(const void *k1, const void *k2, size_t size) {
561 const affinity_node_t *n1 = k1;
562 const affinity_node_t *n2 = k2;
564 return (n1->irn != n2->irn);
567 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
568 affinity_node_t new_node, *node;
569 neighb_t new_nbr, *nbr;
574 new_node.neighbours = NULL;
575 node = set_insert(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
578 for (nbr = node->neighbours; nbr; nbr = nbr->next)
579 if (nbr->irn == n2) {
584 /* if we did not find n2 in n1's neighbourhood insert it */
586 obstack_grow(&co->obst, &new_nbr, sizeof(new_nbr));
587 nbr = obstack_finish(&co->obst);
590 nbr->next = node->neighbours;
591 node->neighbours = nbr;
595 /* now nbr points to n1's neighbour-entry of n2 */
599 static INLINE void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
600 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
601 add_edge(co, n1, n2, costs);
602 add_edge(co, n2, n1, costs);
606 static void build_graph_walker(ir_node *irn, void *env) {
607 copy_opt_t *co = env;
609 arch_register_req_t req;
610 const arch_register_t *reg;
612 if (!is_curr_reg_class(co, irn) || arch_irn_is(co->aenv, irn, ignore))
615 reg = arch_get_irn_register(co->aenv, irn);
616 if (arch_register_type_is(reg, ignore))
621 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
622 ir_node *arg = get_irn_n(irn, pos);
623 add_edges(co, irn, arg, co->get_costs(co, irn, arg, pos));
627 else if (is_Perm_Proj(co->aenv, irn)) {
628 ir_node *arg = get_Perm_src(irn);
629 add_edges(co, irn, arg, co->get_costs(co, irn, arg, 0));
633 else if (is_2addr_code(co->aenv, irn, &req))
634 add_edges(co, irn, req.other_same, co->get_costs(co, irn, req.other_same, 0));
637 void co_build_graph_structure(copy_opt_t *co) {
638 obstack_init(&co->obst);
639 co->nodes = new_set(compare_affinity_node_t, 32);
641 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
644 void co_free_graph_structure(copy_opt_t *co) {
648 obstack_free(&co->obst, NULL);
652 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
654 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn) {
655 affinity_node_t new_node, *n;
660 n = set_find(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
662 return (n->degree > 0);
667 void co_dump_appel_graph(const copy_opt_t *co, FILE *f)
669 be_ifg_t *ifg = co->cenv->ifg;
670 int *color_map = alloca(co->cls->n_regs * sizeof(color_map[0]));
671 bitset_t *adm = bitset_alloca(co->cls->n_regs);
678 for(i = 0; i < co->cls->n_regs; ++i) {
679 const arch_register_t *reg = &co->cls->regs[i];
680 color_map[i] = arch_register_type_is(reg, ignore) ? -1 : n_regs++;
684 * n contains the first node number.
685 * the values below n are the pre-colored register nodes
688 it = be_ifg_nodes_iter_alloca(ifg);
689 nit = be_ifg_neighbours_iter_alloca(ifg);
692 be_ifg_foreach_node(ifg, it, irn) {
693 if(!arch_irn_is(co->aenv, irn, ignore))
694 set_irn_link(irn, INT_TO_PTR(n++));
697 fprintf(f, "%d %d\n", n, n_regs);
699 be_ifg_foreach_node(ifg, it, irn) {
700 if(!arch_irn_is(co->aenv, irn, ignore)) {
701 int idx = PTR_TO_INT(get_irn_link(irn));
702 affinity_node_t *a = get_affinity_info(co, irn);
704 arch_register_req_t req;
707 arch_get_register_req(co->aenv, &req, irn, BE_OUT_POS(0));
708 if(arch_register_req_is(&req, limited)) {
709 bitset_clear_all(adm);
710 req.limited(req.limited_env, adm);
711 for(i = 0; i < co->cls->n_regs; ++i)
712 if(!bitset_is_set(adm, i) && color_map[i] >= 0)
713 fprintf(f, "%d %d -1\n", color_map[i], idx);
718 be_ifg_foreach_neighbour(ifg, nit, irn, adj) {
719 if(!arch_irn_is(co->aenv, adj, ignore)) {
720 int adj_idx = PTR_TO_INT(get_irn_link(adj));
722 fprintf(f, "%d %d -1\n", idx, adj_idx);
729 co_gs_foreach_neighb(a, n) {
730 if(!arch_irn_is(co->aenv, n->irn, ignore)) {
731 int n_idx = PTR_TO_INT(get_irn_link(n->irn));
733 fprintf(f, "%d %d %d\n", idx, n_idx, n->costs);
741 typedef struct _appel_clique_walker_t {
743 const copy_opt_t *co;
750 } appel_clique_walker_t;
752 typedef struct _appel_block_info_t {
762 } appel_block_info_t;
764 static int appel_aff_weight(const appel_clique_walker_t *env, ir_node *bl)
767 double freq = get_block_execfreq(env->co->cenv->execfreq, bl);
768 int res = (int) freq;
769 return res == 0 ? 1 : res;
771 ir_loop *loop = get_irn_loop(bl);
773 int d = get_loop_depth(loop);
780 static void *appel_clique_walker_irn_init(phase_t *phase, ir_node *irn, void *old)
782 appel_block_info_t *res = NULL;
785 appel_clique_walker_t *d = (void *) phase;
786 res = phase_alloc(phase, sizeof(res[0]));
787 res->phi_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end_nr));
788 res->live_end_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end_nr));
789 res->live_in_nr = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in_nr));
790 res->live_end = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_end));
791 res->live_in = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in));
792 res->phi = phase_alloc(phase, d->co->cls->n_regs * sizeof(res->live_in));
798 typedef struct _insn_list_t {
800 struct list_head list;
803 static int appel_get_live_end_nr(appel_clique_walker_t *env, ir_node *bl, ir_node *irn)
805 appel_block_info_t *bli = phase_get_irn_data(&env->ph, bl);
808 for(i = 0; i < bli->n_live_end; ++i)
809 if(bli->live_end[i] == irn)
810 return bli->live_end_nr[i];
815 static int appel_dump_clique(appel_clique_walker_t *env, pset *live, ir_node *bl, int curr_nr, int start_nr)
817 ir_node **live_arr = alloca(env->co->cls->n_regs * sizeof(live_arr[0]));
823 foreach_pset(live, irn)
824 live_arr[n_live++] = irn;
826 /* dump the live after clique */
828 for(j = 0; j < n_live; ++j) {
831 for(k = j + 1; k < n_live; ++k) {
832 fprintf(env->f, "%d %d -1 ", curr_nr + j, curr_nr + k);
834 fprintf(env->f, "\n");
838 /* dump the affinities */
839 for(j = 0; !env->dumb && j < n_live; ++j) {
840 ir_node *irn = live_arr[j];
841 int old_nr = PTR_TO_INT(get_irn_link(irn));
843 /* if the node was already live in the last insn dump the affinity */
844 if(old_nr > start_nr) {
845 int weight = appel_aff_weight(env, bl);
846 fprintf(env->f, "%d %d %d\n", old_nr, curr_nr + j, weight);
850 /* set the current numbers into the link field. */
851 for(j = 0; j < n_live; ++j) {
852 ir_node *irn = live_arr[j];
853 set_irn_link(irn, INT_TO_PTR(curr_nr + j));
856 return curr_nr + n_live;
859 static void appel_walker(ir_node *bl, void *data)
861 appel_clique_walker_t *env = data;
862 appel_block_info_t *bli = phase_get_or_set_irn_data(&env->ph, bl);
863 struct obstack *obst = &env->obst;
864 void *base = obstack_base(obst);
865 pset *live = pset_new_ptr_default();
869 int start_nr = env->curr_nr;
870 int curr_nr = start_nr;
872 be_insn_env_t insn_env;
877 insn_env.aenv = env->co->aenv;
878 insn_env.cls = env->co->cls;
879 insn_env.obst = obst;
880 insn_env.ignore_colors = env->co->cenv->ignore_colors;
882 /* Guess how many insns will be in this block. */
883 sched_foreach(bl, irn)
887 insns = malloc(n_nodes * sizeof(insns[0]));
889 /* Put all insns in an array. */
890 irn = sched_first(bl);
891 while(!sched_is_end(irn)) {
893 insn = be_scan_insn(&insn_env, irn);
894 insns[n_insns++] = insn;
895 irn = insn->next_insn;
898 DBG((env->co->cenv->dbg, LEVEL_2, "%+F\n", bl));
899 be_liveness_end_of_block(env->co->aenv, env->co->cls, bl, live);
901 /* Generate the bad and ugly. */
902 for(i = n_insns - 1; i >= 0; --i) {
903 be_insn_t *insn = insns[i];
905 /* The first live set has to be saved in the block border set. */
906 if(i == n_insns - 1) {
908 foreach_pset(live, irn) {
909 bli->live_end[j] = irn;
910 bli->live_end_nr[j] = curr_nr + j;
917 for(j = 0; j < insn->use_start; ++j) {
918 ir_node *op = insn->ops[j].carrier;
919 bitset_t *adm = insn->ops[j].regs;
923 if(!insn->ops[j].has_constraints)
927 foreach_pset(live, irn) {
935 assert(nr < pset_count(live));
937 for(k = 0; k < env->co->cls->n_regs; ++k) {
938 int mapped_col = env->color_map[k];
939 if(mapped_col >= 0 && !bitset_is_set(adm, k) && !bitset_is_set(env->co->cenv->ignore_colors, k))
940 fprintf(env->f, "%d %d -1\n", curr_nr + nr, mapped_col);
945 /* dump the clique and update the stuff. */
946 curr_nr = appel_dump_clique(env, live, bl, curr_nr, start_nr);
948 /* remove all defs. */
949 for(j = 0; j < insn->use_start; ++j)
950 pset_remove_ptr(live, insn->ops[j].carrier);
952 if(is_Phi(insn->irn) && arch_irn_consider_in_reg_alloc(env->co->aenv, env->co->cls, insn->irn)) {
953 bli->phi[bli->n_phi] = insn->irn;
954 bli->phi_nr[bli->n_phi] = PTR_TO_INT(get_irn_link(insn->irn));
960 for(j = insn->use_start; j < insn->n_ops; ++j)
961 pset_insert_ptr(live, insn->ops[j].carrier);
964 /* print the start clique. */
965 curr_nr = appel_dump_clique(env, live, bl, curr_nr, start_nr);
968 foreach_pset(live, irn) {
969 bli->live_in[i] = irn;
970 bli->live_in_nr[i] = PTR_TO_INT(get_irn_link(irn));
977 obstack_free(obst, base);
978 env->curr_nr = curr_nr;
981 static void appel_inter_block_aff(ir_node *bl, void *data)
983 appel_clique_walker_t *env = data;
984 appel_block_info_t *bli = phase_get_irn_data(&env->ph, bl);
988 for(i = 0; i < bli->n_live_in; ++i) {
989 ir_node *irn = bli->live_in[i];
991 for(j = 0, n = get_Block_n_cfgpreds(bl); j < n; ++j) {
992 ir_node *pred = get_Block_cfgpred_block(bl, j);
993 appel_block_info_t *pred_bli = phase_get_irn_data(&env->ph, pred);
995 int nr = appel_get_live_end_nr(env, pred, irn);
997 fprintf(env->f, "%d %d 1\n", bli->live_in_nr[i], nr);
1001 for(i = 0; i < bli->n_phi; ++i) {
1002 ir_node *irn = bli->phi[i];
1004 for(j = 0, n = get_Block_n_cfgpreds(bl); j < n; ++j) {
1005 ir_node *pred = get_Block_cfgpred_block(bl, j);
1006 appel_block_info_t *pred_bli = phase_get_irn_data(&env->ph, pred);
1007 ir_node *op = get_irn_n(irn, j);
1009 int nr = appel_get_live_end_nr(env, pred, op);
1011 fprintf(env->f, "%d %d 1\n", bli->phi_nr[i], nr);
1017 void co_dump_appel_graph_cliques(const copy_opt_t *co, FILE *f)
1021 appel_clique_walker_t env;
1022 bitset_t *adm = bitset_alloca(co->cls->n_regs);
1024 be_liveness(co->irg);
1025 obstack_init(&env.obst);
1026 phase_init(&env.ph, "appel_clique_dumper", co->irg, PHASE_DEFAULT_GROWTH, appel_clique_walker_irn_init);
1027 env.curr_nr = co->cls->n_regs;
1031 bitset_copy(adm, co->cenv->ignore_colors);
1032 bitset_flip_all(adm);
1034 /* Make color map. */
1035 env.color_map = alloca(co->cls->n_regs * sizeof(env.color_map[0]));
1036 for(i = 0, n_colors = 0; i < co->cls->n_regs; ++i) {
1037 const arch_register_t *reg = &co->cls->regs[i];
1038 env.color_map[i] = arch_register_type_is(reg, ignore) ? -1 : n_colors++;
1042 env.curr_nr = n_colors;
1043 irg_block_walk_graph(co->irg, firm_clear_link, NULL, NULL);
1044 irg_block_walk_graph(co->irg, appel_walker, NULL, &env);
1046 fprintf(f, "%d %d\n", env.curr_nr, n_colors);
1048 /* make the first k nodes interfere */
1049 for(i = 0; i < n_colors; ++i) {
1051 for(j = i + 1; j < n_colors; ++j)
1052 fprintf(f, "%d %d -1 ", i, j);
1057 env.curr_nr = n_colors;
1058 irg_block_walk_graph(co->irg, firm_clear_link, NULL, NULL);
1059 irg_block_walk_graph(co->irg, appel_walker, NULL, &env);
1060 irg_block_walk_graph(co->irg, appel_inter_block_aff, NULL, &env);
1061 obstack_free(&env.obst, NULL);
1065 void co_solve_park_moon(copy_opt_t *opt)