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"
33 /******************************************************************************
36 | | __ ___ _ __ ___ _ __ __ _| |
37 | | |_ |/ _ \ '_ \ / _ \ '__/ _` | |
38 | |__| | __/ | | | __/ | | (_| | |
39 \_____|\___|_| |_|\___|_| \__,_|_|
41 ******************************************************************************/
43 static firm_dbg_module_t *dbg = NULL;
45 void be_copy_opt_init(void) {
48 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
49 const char *s1, *s2, *s3;
53 dbg = firm_dbg_register("ir.be.copyopt");
55 co = xcalloc(1, sizeof(*co));
56 co->cenv = chordal_env;
57 co->aenv = chordal_env->birg->main_env->arch_env;
58 co->irg = chordal_env->irg;
59 co->cls = chordal_env->cls;
60 co->get_costs = get_costs;
62 s1 = get_irp_prog_name();
63 s2 = get_entity_name(get_irg_entity(co->irg));
64 s3 = chordal_env->cls->name;
65 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
66 co->name = xmalloc(len);
67 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
72 void free_copy_opt(copy_opt_t *co) {
76 int co_is_optimizable_root(const copy_opt_t *co, ir_node *irn) {
77 arch_register_req_t req;
78 const arch_register_t *reg;
80 if (arch_irn_is(co->aenv, irn, ignore))
83 reg = arch_get_irn_register(co->aenv, irn);
84 if (arch_register_type_is(reg, ignore))
87 if (is_Reg_Phi(irn) || is_Perm_Proj(co->aenv, irn) || is_2addr_code(co->aenv, irn, &req))
93 int co_is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
94 const ir_edge_t *edge;
95 const arch_register_t *reg;
97 assert(0 && "Is buggy and obsolete. Do not use");
99 if (arch_irn_is(co->aenv, irn, ignore))
102 reg = arch_get_irn_register(co->aenv, irn);
103 if (arch_register_type_is(reg, ignore))
106 foreach_out_edge(irn, edge) {
107 ir_node *n = edge->src;
109 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
110 arch_register_req_t req;
111 arch_get_register_req(co->aenv, &req, n, -1);
114 is_Perm(co->aenv, n) ||
115 (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
124 int co_get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
127 ir_node *root_block = get_nodes_block(root);
130 /* for phis the copies are placed in the corresponding pred-block */
131 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
133 /* a perm places the copy in the same block as it resides */
134 loop = get_irn_loop(root_block);
137 int d = get_loop_depth(loop);
143 int co_get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
147 /******************************************************************************
148 ____ _ _ _ _ _ _____ _
149 / __ \ | | | | | | (_) | / ____| |
150 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
151 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
152 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
153 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
156 ******************************************************************************/
159 * Determines a maximum weighted independent set with respect to
160 * the interference and conflict edges of all nodes in a qnode.
162 static int ou_max_ind_set_costs(unit_t *ou) {
163 be_chordal_env_t *chordal_env = ou->co->cenv;
164 ir_node **safe, **unsafe;
165 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
167 int max, pos, curr_weight, best_weight = 0;
169 /* assign the nodes into two groups.
170 * safe: node has no interference, hence it is in every max stable set.
171 * unsafe: node has an interference
173 safe = alloca((ou->node_count-1) * sizeof(*safe));
176 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
177 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
179 for(i=1; i<ou->node_count; ++i) {
181 for(o=1; o<ou->node_count; ++o) {
184 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
185 unsafe_costs[unsafe_count] = ou->costs[i];
186 unsafe[unsafe_count] = ou->nodes[i];
193 safe_costs += ou->costs[i];
194 safe[safe_count++] = ou->nodes[i];
199 /* now compute the best set out of the unsafe nodes*/
200 if (unsafe_count > MIS_HEUR_TRIGGER) {
201 bitset_t *best = bitset_alloca(unsafe_count);
202 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
203 for (i=0; i<unsafe_count; ++i) {
205 /* check if it is a stable set */
206 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
207 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
208 bitset_clear(best, i); /* clear the bit and try next one */
212 /* compute the weight */
213 bitset_foreach(best, pos)
214 best_weight += unsafe_costs[pos];
216 /* Exact Algorithm: Brute force */
217 curr = bitset_alloca(unsafe_count);
218 bitset_set_all(curr);
219 while ((max = bitset_popcnt(curr)) != 0) {
220 /* check if curr is a stable set */
221 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
222 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) */
223 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
226 /* if we arrive here, we have a stable set */
227 /* compute the weigth of the stable set*/
229 bitset_foreach(curr, pos)
230 curr_weight += unsafe_costs[pos];
233 if (curr_weight > best_weight) {
234 best_weight = curr_weight;
242 return safe_costs+best_weight;
245 static void co_collect_units(ir_node *irn, void *env) {
246 copy_opt_t *co = env;
248 arch_register_req_t req;
250 if (!is_curr_reg_class(co, irn))
252 if (!co_is_optimizable_root(co, irn))
255 /* Init a new unit */
256 unit = xcalloc(1, sizeof(*unit));
258 unit->node_count = 1;
259 INIT_LIST_HEAD(&unit->queue);
261 /* Phi with some/all of its arguments */
262 if (is_Reg_Phi(irn)) {
266 arity = get_irn_arity(irn);
267 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
268 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
269 unit->nodes[0] = irn;
272 for (i=0; i<arity; ++i) {
274 ir_node *arg = get_irn_n(irn, i);
276 assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
279 if (nodes_interfere(co->cenv, irn, arg)) {
280 unit->inevitable_costs += co->get_costs(irn, arg, i);
284 /* Else insert the argument of the phi to the members of this ou */
285 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
287 /* Check if arg has occurred at a prior position in the arg/list */
289 for (o=0; o<unit->node_count; ++o)
290 if (unit->nodes[o] == arg) {
295 if (!arg_pos) { /* a new argument */
296 /* insert node, set costs */
297 unit->nodes[unit->node_count] = arg;
298 unit->costs[unit->node_count] = co->get_costs(irn, arg, i);
300 } else { /* arg has occured before in same phi */
301 /* increase costs for existing arg */
302 unit->costs[arg_pos] += co->get_costs(irn, arg, i);
305 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
306 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
309 /* Proj of a perm with corresponding arg */
310 if (is_Perm_Proj(co->aenv, irn)) {
311 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
312 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
313 unit->costs = xmalloc(2 * sizeof(*unit->costs));
314 unit->node_count = 2;
315 unit->nodes[0] = irn;
316 unit->nodes[1] = get_Perm_src(irn);
317 unit->costs[1] = co->get_costs(irn, unit->nodes[1], -1);
320 /* Src == Tgt of a 2-addr-code instruction */
321 if (is_2addr_code(co->aenv, irn, &req)) {
322 ir_node *other = req.other_same;
323 if (!nodes_interfere(co->cenv, irn, other)) {
324 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
325 unit->costs = xmalloc(2 * sizeof(*unit->costs));
326 unit->node_count = 2;
327 unit->nodes[0] = irn;
328 unit->nodes[1] = other;
329 unit->costs[1] = co->get_costs(irn, other, -120480);
332 assert(0 && "This is not an optimizable node!");
334 /* Insert the new unit at a position according to its costs */
335 if (unit->node_count > 1) {
337 struct list_head *tmp;
339 /* Determine the maximum costs this unit can cause: all_nodes_cost */
340 for(i=1; i<unit->node_count; ++i) {
341 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
342 unit->all_nodes_costs += unit->costs[i];
345 /* Determine the minimal costs this unit will cause: min_nodes_costs */
346 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
348 /* Insert the new ou according to its sort_key */
350 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
352 list_add(&unit->units, tmp);
358 void co_build_ou_structure(copy_opt_t *co) {
359 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
360 INIT_LIST_HEAD(&co->units);
361 irg_walk_graph(co->irg, co_collect_units, NULL, co);
364 void co_free_ou_structure(copy_opt_t *co) {
366 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
373 /* co_solve_heuristic() is implemented in becopyheur.c */
375 int co_get_max_copy_costs(const copy_opt_t *co) {
379 list_for_each_entry(unit_t, curr, &co->units, units) {
380 res += curr->inevitable_costs;
381 for (i=1; i<curr->node_count; ++i)
382 res += curr->costs[i];
387 int co_get_inevit_copy_costs(const copy_opt_t *co) {
391 list_for_each_entry(unit_t, curr, &co->units, units)
392 res += curr->inevitable_costs;
396 int co_get_copy_costs(const copy_opt_t *co) {
400 list_for_each_entry(unit_t, curr, &co->units, units) {
401 int root_col = get_irn_col(co, curr->nodes[0]);
402 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
403 res += curr->inevitable_costs;
404 for (i=1; i<curr->node_count; ++i) {
405 int arg_col = get_irn_col(co, curr->nodes[i]);
406 if (root_col != arg_col) {
407 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
408 res += curr->costs[i];
415 int co_get_lower_bound(const copy_opt_t *co) {
418 list_for_each_entry(unit_t, curr, &co->units, units)
419 res += curr->inevitable_costs + curr->min_nodes_costs;
423 /******************************************************************************
425 / ____| | | / ____| |
426 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
427 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
428 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
429 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
432 ******************************************************************************/
434 static int compare_affinity_t(const void *k1, const void *k2, size_t size) {
435 const affinity_t *n1 = k1;
436 const affinity_t *n2 = k2;
438 return (n1->irn != n2->irn);
441 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
442 affinity_t new_node, *node;
443 neighb_t new_nbr, *nbr;
448 new_node.neighbours = NULL;
449 node = set_insert(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
452 for (nbr = node->neighbours; nbr; nbr = nbr->next)
453 if (nbr->irn == n2) {
458 /* if we did not find n2 in n1's neighbourhood insert it */
460 obstack_grow(&co->obst, &new_nbr, sizeof(new_nbr));
461 nbr = obstack_finish(&co->obst);
464 nbr->next = node->neighbours;
465 node->neighbours = nbr;
469 /* now nbr points to n1's neighbour-entry of n2 */
473 static INLINE void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
474 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
475 add_edge(co, n1, n2, costs);
476 add_edge(co, n2, n1, costs);
480 static void build_graph_walker(ir_node *irn, void *env) {
481 copy_opt_t *co = env;
483 arch_register_req_t req;
484 const arch_register_t *reg;
486 if (!is_curr_reg_class(co, irn) || arch_irn_is(co->aenv, irn, ignore))
489 reg = arch_get_irn_register(co->aenv, irn);
490 if (arch_register_type_is(reg, ignore))
495 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
496 ir_node *arg = get_irn_n(irn, pos);
497 add_edges(co, irn, arg, co->get_costs(irn, arg, pos));
501 else if (is_Perm_Proj(co->aenv, irn)) {
502 ir_node *arg = get_Perm_src(irn);
503 add_edges(co, irn, arg, co->get_costs(irn, arg, 0));
507 else if (is_2addr_code(co->aenv, irn, &req))
508 add_edges(co, irn, req.other_same, co->get_costs(irn, req.other_same, 0));
511 void co_build_graph_structure(copy_opt_t *co) {
512 obstack_init(&co->obst);
513 co->nodes = new_set(compare_affinity_t, 32);
515 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
518 void co_free_graph_structure(copy_opt_t *co) {
520 obstack_free(&co->obst, NULL);
523 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
525 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn) {
526 affinity_t new_node, *n;
529 n = set_find(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
531 return (n->count > 0);