4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
24 #include "iredges_t.h"
30 #include "becopyopt_t.h"
31 #include "becopystat.h"
36 /* Insert additional options registration functions here. */
37 extern void be_co2_register_options(lc_opt_entry_t *grp);
39 void co_register_options(lc_opt_entry_t *grp)
41 be_co2_register_options(grp);
46 #undef QUICK_AND_DIRTY_HACK
48 /******************************************************************************
51 | | __ ___ _ __ ___ _ __ __ _| |
52 | | |_ |/ _ \ '_ \ / _ \ '__/ _` | |
53 | |__| | __/ | | | __/ | | (_| | |
54 \_____|\___|_| |_|\___|_| \__,_|_|
56 ******************************************************************************/
58 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
60 void be_copy_opt_init(void) {
63 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
64 const char *s1, *s2, *s3;
68 FIRM_DBG_REGISTER(dbg, "ir.be.copyopt");
70 co = xcalloc(1, sizeof(*co));
71 co->cenv = chordal_env;
72 co->aenv = chordal_env->birg->main_env->arch_env;
73 co->irg = chordal_env->irg;
74 co->cls = chordal_env->cls;
75 co->get_costs = get_costs;
77 s1 = get_irp_prog_name();
78 s2 = get_entity_name(get_irg_entity(co->irg));
79 s3 = chordal_env->cls->name;
80 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
81 co->name = xmalloc(len);
82 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
87 void free_copy_opt(copy_opt_t *co) {
92 int co_is_optimizable_root(const copy_opt_t *co, ir_node *irn) {
93 arch_register_req_t req;
94 const arch_register_t *reg;
96 if (arch_irn_is(co->aenv, irn, ignore))
99 reg = arch_get_irn_register(co->aenv, irn);
100 if (arch_register_type_is(reg, ignore))
103 if (is_Reg_Phi(irn) || is_Perm_Proj(co->aenv, irn) || is_2addr_code(co->aenv, irn, &req))
109 int co_is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
110 const ir_edge_t *edge;
111 const arch_register_t *reg;
113 assert(0 && "Is buggy and obsolete. Do not use");
115 if (arch_irn_is(co->aenv, irn, ignore))
118 reg = arch_get_irn_register(co->aenv, irn);
119 if (arch_register_type_is(reg, ignore))
122 foreach_out_edge(irn, edge) {
123 ir_node *n = edge->src;
125 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
126 arch_register_req_t req;
127 arch_get_register_req(co->aenv, &req, n, -1);
130 is_Perm(co->aenv, n) ||
131 (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
140 int co_get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
143 ir_node *root_block = get_nodes_block(root);
146 /* for phis the copies are placed in the corresponding pred-block */
147 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
149 /* a perm places the copy in the same block as it resides */
150 loop = get_irn_loop(root_block);
153 int d = get_loop_depth(loop);
159 int co_get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
163 /******************************************************************************
164 ____ _ _ _ _ _ _____ _
165 / __ \ | | | | | | (_) | / ____| |
166 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
167 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
168 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
169 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
172 ******************************************************************************/
175 * Determines a maximum weighted independent set with respect to
176 * the interference and conflict edges of all nodes in a qnode.
178 static int ou_max_ind_set_costs(unit_t *ou) {
179 be_chordal_env_t *chordal_env = ou->co->cenv;
180 ir_node **safe, **unsafe;
181 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
183 int max, pos, curr_weight, best_weight = 0;
185 /* assign the nodes into two groups.
186 * safe: node has no interference, hence it is in every max stable set.
187 * unsafe: node has an interference
189 safe = alloca((ou->node_count-1) * sizeof(*safe));
192 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
193 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
195 for(i=1; i<ou->node_count; ++i) {
197 for(o=1; o<ou->node_count; ++o) {
200 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
201 unsafe_costs[unsafe_count] = ou->costs[i];
202 unsafe[unsafe_count] = ou->nodes[i];
209 safe_costs += ou->costs[i];
210 safe[safe_count++] = ou->nodes[i];
215 /* now compute the best set out of the unsafe nodes*/
216 if (unsafe_count > MIS_HEUR_TRIGGER) {
217 bitset_t *best = bitset_alloca(unsafe_count);
218 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
219 for (i=0; i<unsafe_count; ++i) {
221 /* check if it is a stable set */
222 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
223 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
224 bitset_clear(best, i); /* clear the bit and try next one */
228 /* compute the weight */
229 bitset_foreach(best, pos)
230 best_weight += unsafe_costs[pos];
232 /* Exact Algorithm: Brute force */
233 curr = bitset_alloca(unsafe_count);
234 bitset_set_all(curr);
235 while ((max = bitset_popcnt(curr)) != 0) {
236 /* check if curr is a stable set */
237 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
238 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) */
239 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
242 /* if we arrive here, we have a stable set */
243 /* compute the weigth of the stable set*/
245 bitset_foreach(curr, pos)
246 curr_weight += unsafe_costs[pos];
249 if (curr_weight > best_weight) {
250 best_weight = curr_weight;
258 return safe_costs+best_weight;
261 static void co_collect_units(ir_node *irn, void *env) {
262 copy_opt_t *co = env;
264 arch_register_req_t req;
266 if (!is_curr_reg_class(co, irn))
268 if (!co_is_optimizable_root(co, irn))
271 /* Init a new unit */
272 unit = xcalloc(1, sizeof(*unit));
274 unit->node_count = 1;
275 INIT_LIST_HEAD(&unit->queue);
277 /* Phi with some/all of its arguments */
278 if (is_Reg_Phi(irn)) {
282 arity = get_irn_arity(irn);
283 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
284 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
285 unit->nodes[0] = irn;
288 for (i=0; i<arity; ++i) {
290 ir_node *arg = get_irn_n(irn, i);
292 assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
295 if (nodes_interfere(co->cenv, irn, arg)) {
296 unit->inevitable_costs += co->get_costs(irn, arg, i);
300 /* Else insert the argument of the phi to the members of this ou */
301 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
303 /* Check if arg has occurred at a prior position in the arg/list */
305 for (o=0; o<unit->node_count; ++o)
306 if (unit->nodes[o] == arg) {
311 if (!arg_pos) { /* a new argument */
312 /* insert node, set costs */
313 unit->nodes[unit->node_count] = arg;
314 unit->costs[unit->node_count] = co->get_costs(irn, arg, i);
316 } else { /* arg has occured before in same phi */
317 /* increase costs for existing arg */
318 unit->costs[arg_pos] += co->get_costs(irn, arg, i);
321 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
322 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
325 /* Proj of a perm with corresponding arg */
326 if (is_Perm_Proj(co->aenv, irn)) {
327 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
328 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
329 unit->costs = xmalloc(2 * sizeof(*unit->costs));
330 unit->node_count = 2;
331 unit->nodes[0] = irn;
332 unit->nodes[1] = get_Perm_src(irn);
333 unit->costs[1] = co->get_costs(irn, unit->nodes[1], -1);
336 /* Src == Tgt of a 2-addr-code instruction */
337 if (is_2addr_code(co->aenv, irn, &req)) {
338 ir_node *other = req.other_same;
339 if (!nodes_interfere(co->cenv, irn, other)) {
340 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
341 unit->costs = xmalloc(2 * sizeof(*unit->costs));
342 unit->node_count = 2;
343 unit->nodes[0] = irn;
344 unit->nodes[1] = other;
345 unit->costs[1] = co->get_costs(irn, other, -1);
348 assert(0 && "This is not an optimizable node!");
350 /* Insert the new unit at a position according to its costs */
351 if (unit->node_count > 1) {
353 struct list_head *tmp;
355 /* Determine the maximum costs this unit can cause: all_nodes_cost */
356 for(i=1; i<unit->node_count; ++i) {
357 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
358 unit->all_nodes_costs += unit->costs[i];
361 /* Determine the minimal costs this unit will cause: min_nodes_costs */
362 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
363 /* Insert the new ou according to its sort_key */
365 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
367 list_add(&unit->units, tmp);
373 #ifdef QUICK_AND_DIRTY_HACK
375 static int compare_ous(const void *k1, const void *k2) {
376 const unit_t *u1 = *((const unit_t **) k1);
377 const unit_t *u2 = *((const unit_t **) k2);
378 int i, o, u1_has_constr, u2_has_constr;
379 arch_register_req_t req;
380 const arch_env_t *aenv = u1->co->aenv;
382 /* Units with constraints come first */
384 for (i=0; i<u1->node_count; ++i) {
385 arch_get_register_req(aenv, &req, u1->nodes[i], -1);
386 if (arch_register_req_is(&req, limited)) {
393 for (i=0; i<u2->node_count; ++i) {
394 arch_get_register_req(aenv, &req, u2->nodes[i], -1);
395 if (arch_register_req_is(&req, limited)) {
401 if (u1_has_constr != u2_has_constr)
402 return u2_has_constr - u1_has_constr;
404 /* Now check, whether the two units are connected */
406 for (i=0; i<u1->node_count; ++i)
407 for (o=0; o<u2->node_count; ++o)
408 if (u1->nodes[i] == u2->nodes[o])
412 /* After all, the sort key decides. Greater keys come first. */
413 return u2->sort_key - u1->sort_key;
418 * Sort the ou's according to constraints and their sort_key
420 static void co_sort_units(copy_opt_t *co) {
421 int i, count = 0, costs;
424 /* get the number of ous, remove them form the list and fill the array */
425 list_for_each_entry(unit_t, ou, &co->units, units)
427 ous = alloca(count * sizeof(*ous));
429 costs = co_get_max_copy_costs(co);
432 list_for_each_entry(unit_t, ou, &co->units, units)
435 INIT_LIST_HEAD(&co->units);
437 assert(count == i && list_empty(&co->units));
439 for (i=0; i<count; ++i)
440 ir_printf("%+F\n", ous[i]->nodes[0]);
442 qsort(ous, count, sizeof(*ous), compare_ous);
445 for (i=0; i<count; ++i)
446 ir_printf("%+F\n", ous[i]->nodes[0]);
448 /* reinsert into list in correct order */
449 for (i=0; i<count; ++i)
450 list_add_tail(&ous[i]->units, &co->units);
452 assert(costs == co_get_max_copy_costs(co));
456 void co_build_ou_structure(copy_opt_t *co) {
457 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
458 INIT_LIST_HEAD(&co->units);
459 irg_walk_graph(co->irg, co_collect_units, NULL, co);
460 #ifdef QUICK_AND_DIRTY_HACK
465 void co_free_ou_structure(copy_opt_t *co) {
468 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
473 co->units.next = NULL;
476 /* co_solve_heuristic() is implemented in becopyheur.c */
478 int co_get_max_copy_costs(const copy_opt_t *co) {
484 list_for_each_entry(unit_t, curr, &co->units, units) {
485 res += curr->inevitable_costs;
486 for (i=1; i<curr->node_count; ++i)
487 res += curr->costs[i];
492 int co_get_inevit_copy_costs(const copy_opt_t *co) {
498 list_for_each_entry(unit_t, curr, &co->units, units)
499 res += curr->inevitable_costs;
503 int co_get_copy_costs(const copy_opt_t *co) {
509 list_for_each_entry(unit_t, curr, &co->units, units) {
510 int root_col = get_irn_col(co, curr->nodes[0]);
511 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
512 res += curr->inevitable_costs;
513 for (i=1; i<curr->node_count; ++i) {
514 int arg_col = get_irn_col(co, curr->nodes[i]);
515 if (root_col != arg_col) {
516 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
517 res += curr->costs[i];
524 int co_get_lower_bound(const copy_opt_t *co) {
530 list_for_each_entry(unit_t, curr, &co->units, units)
531 res += curr->inevitable_costs + curr->min_nodes_costs;
535 /******************************************************************************
537 / ____| | | / ____| |
538 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
539 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
540 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
541 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
544 ******************************************************************************/
546 static int compare_affinity_node_t(const void *k1, const void *k2, size_t size) {
547 const affinity_node_t *n1 = k1;
548 const affinity_node_t *n2 = k2;
550 return (n1->irn != n2->irn);
553 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
554 affinity_node_t new_node, *node;
555 neighb_t new_nbr, *nbr;
560 new_node.neighbours = NULL;
561 node = set_insert(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
564 for (nbr = node->neighbours; nbr; nbr = nbr->next)
565 if (nbr->irn == n2) {
570 /* if we did not find n2 in n1's neighbourhood insert it */
572 obstack_grow(&co->obst, &new_nbr, sizeof(new_nbr));
573 nbr = obstack_finish(&co->obst);
576 nbr->next = node->neighbours;
577 node->neighbours = nbr;
581 /* now nbr points to n1's neighbour-entry of n2 */
585 static INLINE void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
586 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
587 add_edge(co, n1, n2, costs);
588 add_edge(co, n2, n1, costs);
592 static void build_graph_walker(ir_node *irn, void *env) {
593 copy_opt_t *co = env;
595 arch_register_req_t req;
596 const arch_register_t *reg;
598 if (!is_curr_reg_class(co, irn) || arch_irn_is(co->aenv, irn, ignore))
601 reg = arch_get_irn_register(co->aenv, irn);
602 if (arch_register_type_is(reg, ignore))
607 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
608 ir_node *arg = get_irn_n(irn, pos);
609 add_edges(co, irn, arg, co->get_costs(irn, arg, pos));
613 else if (is_Perm_Proj(co->aenv, irn)) {
614 ir_node *arg = get_Perm_src(irn);
615 add_edges(co, irn, arg, co->get_costs(irn, arg, 0));
619 else if (is_2addr_code(co->aenv, irn, &req))
620 add_edges(co, irn, req.other_same, co->get_costs(irn, req.other_same, 0));
623 void co_build_graph_structure(copy_opt_t *co) {
624 obstack_init(&co->obst);
625 co->nodes = new_set(compare_affinity_node_t, 32);
627 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
630 void co_free_graph_structure(copy_opt_t *co) {
634 obstack_free(&co->obst, NULL);
638 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
640 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn) {
641 affinity_node_t new_node, *n;
646 n = set_find(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
648 return (n->degree > 0);
653 void co_dump_appel_graph(const copy_opt_t *co, FILE *f)
655 be_ifg_t *ifg = co->cenv->ifg;
661 it = be_ifg_nodes_iter_alloca(ifg);
662 nit = be_ifg_neighbours_iter_alloca(ifg);
665 be_ifg_foreach_node(ifg, it, irn) {
666 set_irn_link(irn, INT_TO_PTR(n++));
669 fprintf(f, "%d %d\n", n, co->cls->n_regs);
671 be_ifg_foreach_node(ifg, it, irn) {
672 int idx = PTR_TO_INT(get_irn_link(irn));
673 affinity_node_t *a = get_affinity_info(co, irn);
677 be_ifg_foreach_neighbour(ifg, nit, irn, adj) {
678 int adj_idx = PTR_TO_INT(get_irn_link(adj));
680 fprintf(f, "%d %d -1\n", idx, adj_idx);
686 co_gs_foreach_neighb(a, n) {
687 int n_idx = PTR_TO_INT(get_irn_link(n->irn));
689 fprintf(f, "%d %d %d\n", idx, n_idx, n->costs);
695 void co_solve_park_moon(copy_opt_t *opt)