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"
34 #undef QUICK_AND_DIRTY_HACK
36 /******************************************************************************
39 | | __ ___ _ __ ___ _ __ __ _| |
40 | | |_ |/ _ \ '_ \ / _ \ '__/ _` | |
41 | |__| | __/ | | | __/ | | (_| | |
42 \_____|\___|_| |_|\___|_| \__,_|_|
44 ******************************************************************************/
46 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
48 void be_copy_opt_init(void) {
51 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
52 const char *s1, *s2, *s3;
56 FIRM_DBG_REGISTER(dbg, "ir.be.copyopt");
58 co = xcalloc(1, sizeof(*co));
59 co->cenv = chordal_env;
60 co->aenv = chordal_env->birg->main_env->arch_env;
61 co->irg = chordal_env->irg;
62 co->cls = chordal_env->cls;
63 co->get_costs = get_costs;
65 s1 = get_irp_prog_name();
66 s2 = get_entity_name(get_irg_entity(co->irg));
67 s3 = chordal_env->cls->name;
68 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
69 co->name = xmalloc(len);
70 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
75 void free_copy_opt(copy_opt_t *co) {
80 int co_is_optimizable_root(const copy_opt_t *co, ir_node *irn) {
81 arch_register_req_t req;
82 const arch_register_t *reg;
84 if (arch_irn_is(co->aenv, irn, ignore))
87 reg = arch_get_irn_register(co->aenv, irn);
88 if (arch_register_type_is(reg, ignore))
91 if (is_Reg_Phi(irn) || is_Perm_Proj(co->aenv, irn) || is_2addr_code(co->aenv, irn, &req))
97 int co_is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
98 const ir_edge_t *edge;
99 const arch_register_t *reg;
101 assert(0 && "Is buggy and obsolete. Do not use");
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 foreach_out_edge(irn, edge) {
111 ir_node *n = edge->src;
113 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
114 arch_register_req_t req;
115 arch_get_register_req(co->aenv, &req, n, -1);
118 is_Perm(co->aenv, n) ||
119 (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
128 int co_get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
131 ir_node *root_block = get_nodes_block(root);
134 /* for phis the copies are placed in the corresponding pred-block */
135 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
137 /* a perm places the copy in the same block as it resides */
138 loop = get_irn_loop(root_block);
141 int d = get_loop_depth(loop);
147 int co_get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
151 /******************************************************************************
152 ____ _ _ _ _ _ _____ _
153 / __ \ | | | | | | (_) | / ____| |
154 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
155 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
156 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
157 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
160 ******************************************************************************/
163 * Determines a maximum weighted independent set with respect to
164 * the interference and conflict edges of all nodes in a qnode.
166 static int ou_max_ind_set_costs(unit_t *ou) {
167 be_chordal_env_t *chordal_env = ou->co->cenv;
168 ir_node **safe, **unsafe;
169 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
171 int max, pos, curr_weight, best_weight = 0;
173 /* assign the nodes into two groups.
174 * safe: node has no interference, hence it is in every max stable set.
175 * unsafe: node has an interference
177 safe = alloca((ou->node_count-1) * sizeof(*safe));
180 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
181 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
183 for(i=1; i<ou->node_count; ++i) {
185 for(o=1; o<ou->node_count; ++o) {
188 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
189 unsafe_costs[unsafe_count] = ou->costs[i];
190 unsafe[unsafe_count] = ou->nodes[i];
197 safe_costs += ou->costs[i];
198 safe[safe_count++] = ou->nodes[i];
203 /* now compute the best set out of the unsafe nodes*/
204 if (unsafe_count > MIS_HEUR_TRIGGER) {
205 bitset_t *best = bitset_alloca(unsafe_count);
206 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
207 for (i=0; i<unsafe_count; ++i) {
209 /* check if it is a stable set */
210 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
211 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
212 bitset_clear(best, i); /* clear the bit and try next one */
216 /* compute the weight */
217 bitset_foreach(best, pos)
218 best_weight += unsafe_costs[pos];
220 /* Exact Algorithm: Brute force */
221 curr = bitset_alloca(unsafe_count);
222 bitset_set_all(curr);
223 while ((max = bitset_popcnt(curr)) != 0) {
224 /* check if curr is a stable set */
225 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
226 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) */
227 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
230 /* if we arrive here, we have a stable set */
231 /* compute the weigth of the stable set*/
233 bitset_foreach(curr, pos)
234 curr_weight += unsafe_costs[pos];
237 if (curr_weight > best_weight) {
238 best_weight = curr_weight;
246 return safe_costs+best_weight;
249 static void co_collect_units(ir_node *irn, void *env) {
250 copy_opt_t *co = env;
252 arch_register_req_t req;
254 if (!is_curr_reg_class(co, irn))
256 if (!co_is_optimizable_root(co, irn))
259 /* Init a new unit */
260 unit = xcalloc(1, sizeof(*unit));
262 unit->node_count = 1;
263 INIT_LIST_HEAD(&unit->queue);
265 /* Phi with some/all of its arguments */
266 if (is_Reg_Phi(irn)) {
270 arity = get_irn_arity(irn);
271 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
272 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
273 unit->nodes[0] = irn;
276 for (i=0; i<arity; ++i) {
278 ir_node *arg = get_irn_n(irn, i);
280 assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
283 if (nodes_interfere(co->cenv, irn, arg)) {
284 unit->inevitable_costs += co->get_costs(irn, arg, i);
288 /* Else insert the argument of the phi to the members of this ou */
289 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
291 /* Check if arg has occurred at a prior position in the arg/list */
293 for (o=0; o<unit->node_count; ++o)
294 if (unit->nodes[o] == arg) {
299 if (!arg_pos) { /* a new argument */
300 /* insert node, set costs */
301 unit->nodes[unit->node_count] = arg;
302 unit->costs[unit->node_count] = co->get_costs(irn, arg, i);
304 } else { /* arg has occured before in same phi */
305 /* increase costs for existing arg */
306 unit->costs[arg_pos] += co->get_costs(irn, arg, i);
309 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
310 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
313 /* Proj of a perm with corresponding arg */
314 if (is_Perm_Proj(co->aenv, irn)) {
315 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
316 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
317 unit->costs = xmalloc(2 * sizeof(*unit->costs));
318 unit->node_count = 2;
319 unit->nodes[0] = irn;
320 unit->nodes[1] = get_Perm_src(irn);
321 unit->costs[1] = co->get_costs(irn, unit->nodes[1], -1);
324 /* Src == Tgt of a 2-addr-code instruction */
325 if (is_2addr_code(co->aenv, irn, &req)) {
326 ir_node *other = req.other_same;
327 if (!nodes_interfere(co->cenv, irn, other)) {
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] = other;
333 unit->costs[1] = co->get_costs(irn, other, -1);
336 assert(0 && "This is not an optimizable node!");
338 /* Insert the new unit at a position according to its costs */
339 if (unit->node_count > 1) {
341 struct list_head *tmp;
343 /* Determine the maximum costs this unit can cause: all_nodes_cost */
344 for(i=1; i<unit->node_count; ++i) {
345 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
346 unit->all_nodes_costs += unit->costs[i];
349 /* Determine the minimal costs this unit will cause: min_nodes_costs */
350 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
351 /* Insert the new ou according to its sort_key */
353 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
355 list_add(&unit->units, tmp);
361 #ifdef QUICK_AND_DIRTY_HACK
363 static int compare_ous(const void *k1, const void *k2) {
364 const unit_t *u1 = *((const unit_t **) k1);
365 const unit_t *u2 = *((const unit_t **) k2);
366 int i, o, u1_has_constr, u2_has_constr;
367 arch_register_req_t req;
368 const arch_env_t *aenv = u1->co->aenv;
370 /* Units with constraints come first */
372 for (i=0; i<u1->node_count; ++i) {
373 arch_get_register_req(aenv, &req, u1->nodes[i], -1);
374 if (arch_register_req_is(&req, limited)) {
381 for (i=0; i<u2->node_count; ++i) {
382 arch_get_register_req(aenv, &req, u2->nodes[i], -1);
383 if (arch_register_req_is(&req, limited)) {
389 if (u1_has_constr != u2_has_constr)
390 return u2_has_constr - u1_has_constr;
392 /* Now check, whether the two units are connected */
394 for (i=0; i<u1->node_count; ++i)
395 for (o=0; o<u2->node_count; ++o)
396 if (u1->nodes[i] == u2->nodes[o])
400 /* After all, the sort key decides. Greater keys come first. */
401 return u2->sort_key - u1->sort_key;
406 * Sort the ou's according to constraints and their sort_key
408 static void co_sort_units(copy_opt_t *co) {
409 int i, count = 0, costs;
412 /* get the number of ous, remove them form the list and fill the array */
413 list_for_each_entry(unit_t, ou, &co->units, units)
415 ous = alloca(count * sizeof(*ous));
417 costs = co_get_max_copy_costs(co);
420 list_for_each_entry(unit_t, ou, &co->units, units)
423 INIT_LIST_HEAD(&co->units);
425 assert(count == i && list_empty(&co->units));
427 for (i=0; i<count; ++i)
428 ir_printf("%+F\n", ous[i]->nodes[0]);
430 qsort(ous, count, sizeof(*ous), compare_ous);
433 for (i=0; i<count; ++i)
434 ir_printf("%+F\n", ous[i]->nodes[0]);
436 /* reinsert into list in correct order */
437 for (i=0; i<count; ++i)
438 list_add_tail(&ous[i]->units, &co->units);
440 assert(costs == co_get_max_copy_costs(co));
444 void co_build_ou_structure(copy_opt_t *co) {
445 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
446 INIT_LIST_HEAD(&co->units);
447 irg_walk_graph(co->irg, co_collect_units, NULL, co);
448 #ifdef QUICK_AND_DIRTY_HACK
453 void co_free_ou_structure(copy_opt_t *co) {
456 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
461 co->units.next = NULL;
464 /* co_solve_heuristic() is implemented in becopyheur.c */
466 int co_get_max_copy_costs(const copy_opt_t *co) {
472 list_for_each_entry(unit_t, curr, &co->units, units) {
473 res += curr->inevitable_costs;
474 for (i=1; i<curr->node_count; ++i)
475 res += curr->costs[i];
480 int co_get_inevit_copy_costs(const copy_opt_t *co) {
486 list_for_each_entry(unit_t, curr, &co->units, units)
487 res += curr->inevitable_costs;
491 int co_get_copy_costs(const copy_opt_t *co) {
497 list_for_each_entry(unit_t, curr, &co->units, units) {
498 int root_col = get_irn_col(co, curr->nodes[0]);
499 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
500 res += curr->inevitable_costs;
501 for (i=1; i<curr->node_count; ++i) {
502 int arg_col = get_irn_col(co, curr->nodes[i]);
503 if (root_col != arg_col) {
504 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
505 res += curr->costs[i];
512 int co_get_lower_bound(const copy_opt_t *co) {
518 list_for_each_entry(unit_t, curr, &co->units, units)
519 res += curr->inevitable_costs + curr->min_nodes_costs;
523 /******************************************************************************
525 / ____| | | / ____| |
526 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
527 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
528 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
529 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
532 ******************************************************************************/
534 static int compare_affinity_node_t(const void *k1, const void *k2, size_t size) {
535 const affinity_node_t *n1 = k1;
536 const affinity_node_t *n2 = k2;
538 return (n1->irn != n2->irn);
541 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
542 affinity_node_t new_node, *node;
543 neighb_t new_nbr, *nbr;
548 new_node.neighbours = NULL;
549 node = set_insert(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
552 for (nbr = node->neighbours; nbr; nbr = nbr->next)
553 if (nbr->irn == n2) {
558 /* if we did not find n2 in n1's neighbourhood insert it */
560 obstack_grow(&co->obst, &new_nbr, sizeof(new_nbr));
561 nbr = obstack_finish(&co->obst);
564 nbr->next = node->neighbours;
565 node->neighbours = nbr;
569 /* now nbr points to n1's neighbour-entry of n2 */
573 static INLINE void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
574 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
575 add_edge(co, n1, n2, costs);
576 add_edge(co, n2, n1, costs);
580 static void build_graph_walker(ir_node *irn, void *env) {
581 copy_opt_t *co = env;
583 arch_register_req_t req;
584 const arch_register_t *reg;
586 if (!is_curr_reg_class(co, irn) || arch_irn_is(co->aenv, irn, ignore))
589 reg = arch_get_irn_register(co->aenv, irn);
590 if (arch_register_type_is(reg, ignore))
595 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
596 ir_node *arg = get_irn_n(irn, pos);
597 add_edges(co, irn, arg, co->get_costs(irn, arg, pos));
601 else if (is_Perm_Proj(co->aenv, irn)) {
602 ir_node *arg = get_Perm_src(irn);
603 add_edges(co, irn, arg, co->get_costs(irn, arg, 0));
607 else if (is_2addr_code(co->aenv, irn, &req))
608 add_edges(co, irn, req.other_same, co->get_costs(irn, req.other_same, 0));
611 void co_build_graph_structure(copy_opt_t *co) {
612 obstack_init(&co->obst);
613 co->nodes = new_set(compare_affinity_node_t, 32);
615 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
618 void co_free_graph_structure(copy_opt_t *co) {
622 obstack_free(&co->obst, NULL);
626 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
628 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn) {
629 affinity_node_t new_node, *n;
634 n = set_find(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
636 return (n->degree > 0);