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;
79 if (arch_irn_is_ignore(co->aenv, irn))
82 if (is_Reg_Phi(irn) || is_Perm_Proj(co->aenv, irn) || is_2addr_code(co->aenv, irn, &req))
88 int co_is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
89 const ir_edge_t *edge;
91 if (arch_irn_is_ignore(co->aenv, irn))
94 foreach_out_edge(irn, edge) {
95 ir_node *n = edge->src;
97 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
98 arch_register_req_t req;
99 arch_get_register_req(co->aenv, &req, n, -1);
102 is_Perm(co->aenv, n) ||
103 (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
112 int co_get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
115 ir_node *root_block = get_nodes_block(root);
118 /* for phis the copies are placed in the corresponding pred-block */
119 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
121 /* a perm places the copy in the same block as it resides */
122 loop = get_irn_loop(root_block);
125 int d = get_loop_depth(loop);
131 int co_get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
135 /******************************************************************************
136 ____ _ _ _ _ _ _____ _
137 / __ \ | | | | | | (_) | / ____| |
138 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
139 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
140 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
141 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
144 ******************************************************************************/
147 * Determines a maximum weighted independent set with respect to
148 * the interference and conflict edges of all nodes in a qnode.
150 static int ou_max_ind_set_costs(unit_t *ou) {
151 be_chordal_env_t *chordal_env = ou->co->cenv;
152 ir_node **safe, **unsafe;
153 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
155 int max, pos, curr_weight, best_weight = 0;
157 /* assign the nodes into two groups.
158 * safe: node has no interference, hence it is in every max stable set.
159 * unsafe: node has an interference
161 safe = alloca((ou->node_count-1) * sizeof(*safe));
164 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
165 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
167 for(i=1; i<ou->node_count; ++i) {
169 for(o=1; o<ou->node_count; ++o) {
172 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
173 unsafe_costs[unsafe_count] = ou->costs[i];
174 unsafe[unsafe_count] = ou->nodes[i];
181 safe_costs += ou->costs[i];
182 safe[safe_count++] = ou->nodes[i];
187 /* now compute the best set out of the unsafe nodes*/
188 if (unsafe_count > MIS_HEUR_TRIGGER) {
189 bitset_t *best = bitset_alloca(unsafe_count);
190 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
191 for (i=0; i<unsafe_count; ++i) {
193 /* check if it is a stable set */
194 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
195 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
196 bitset_clear(best, i); /* clear the bit and try next one */
200 /* compute the weight */
201 bitset_foreach(best, pos)
202 best_weight += unsafe_costs[pos];
204 /* Exact Algorithm: Brute force */
205 curr = bitset_alloca(unsafe_count);
206 bitset_set_all(curr);
207 while ((max = bitset_popcnt(curr)) != 0) {
208 /* check if curr is a stable set */
209 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
210 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) */
211 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
214 /* if we arrive here, we have a stable set */
215 /* compute the weigth of the stable set*/
217 bitset_foreach(curr, pos)
218 curr_weight += unsafe_costs[pos];
221 if (curr_weight > best_weight) {
222 best_weight = curr_weight;
230 return safe_costs+best_weight;
233 static void co_collect_units(ir_node *irn, void *env) {
234 copy_opt_t *co = env;
236 arch_register_req_t req;
238 if (!is_curr_reg_class(co, irn))
240 if (!co_is_optimizable_root(co, irn))
243 /* Init a new unit */
244 unit = xcalloc(1, sizeof(*unit));
246 unit->node_count = 1;
247 INIT_LIST_HEAD(&unit->queue);
249 /* Phi with some/all of its arguments */
250 if (is_Reg_Phi(irn)) {
254 arity = get_irn_arity(irn);
255 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
256 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
257 unit->nodes[0] = irn;
260 for (i=0; i<arity; ++i) {
262 ir_node *arg = get_irn_n(irn, i);
264 assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
267 if (nodes_interfere(co->cenv, irn, arg)) {
268 unit->inevitable_costs += co->get_costs(irn, arg, i);
272 /* Else insert the argument of the phi to the members of this ou */
273 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
275 /* Check if arg has occurred at a prior position in the arg/list */
277 for (o=0; o<unit->node_count; ++o)
278 if (unit->nodes[o] == arg) {
283 if (!arg_pos) { /* a new argument */
284 /* insert node, set costs */
285 unit->nodes[unit->node_count] = arg;
286 unit->costs[unit->node_count] = co->get_costs(irn, arg, i);
288 } else { /* arg has occured before in same phi */
289 /* increase costs for existing arg */
290 unit->costs[arg_pos] += co->get_costs(irn, arg, i);
293 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
294 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
297 /* Proj of a perm with corresponding arg */
298 if (is_Perm_Proj(co->aenv, irn)) {
299 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
300 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
301 unit->costs = xmalloc(2 * sizeof(*unit->costs));
302 unit->node_count = 2;
303 unit->nodes[0] = irn;
304 unit->nodes[1] = get_Perm_src(irn);
305 unit->costs[1] = co->get_costs(irn, unit->nodes[1], -1);
308 /* Src == Tgt of a 2-addr-code instruction */
309 if (is_2addr_code(co->aenv, irn, &req)) {
310 ir_node *other = req.other_same;
311 if (!nodes_interfere(co->cenv, irn, other)) {
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] = other;
317 unit->costs[1] = co->get_costs(irn, other, -120480);
320 assert(0 && "This is not an optimizable node!");
322 /* Insert the new unit at a position according to its costs */
323 if (unit->node_count > 1) {
325 struct list_head *tmp;
327 /* Determine the maximum costs this unit can cause: all_nodes_cost */
328 for(i=1; i<unit->node_count; ++i) {
329 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
330 unit->all_nodes_costs += unit->costs[i];
333 /* Determine the minimal costs this unit will cause: min_nodes_costs */
334 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
336 /* Insert the new ou according to its sort_key */
338 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
340 list_add(&unit->units, tmp);
346 void co_build_ou_structure(copy_opt_t *co) {
347 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
348 INIT_LIST_HEAD(&co->units);
349 irg_walk_graph(co->irg, co_collect_units, NULL, co);
352 void co_free_ou_structure(copy_opt_t *co) {
354 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
361 /* co_solve_heuristic() is implemented in becopyheur.c */
363 int co_get_max_copy_costs(const copy_opt_t *co) {
367 list_for_each_entry(unit_t, curr, &co->units, units) {
368 res += curr->inevitable_costs;
369 for (i=1; i<curr->node_count; ++i)
370 res += curr->costs[i];
375 int co_get_inevit_copy_costs(const copy_opt_t *co) {
379 list_for_each_entry(unit_t, curr, &co->units, units)
380 res += curr->inevitable_costs;
384 int co_get_copy_costs(const copy_opt_t *co) {
388 list_for_each_entry(unit_t, curr, &co->units, units) {
389 int root_col = get_irn_col(co, curr->nodes[0]);
390 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
391 res += curr->inevitable_costs;
392 for (i=1; i<curr->node_count; ++i) {
393 int arg_col = get_irn_col(co, curr->nodes[i]);
394 if (root_col != arg_col) {
395 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
396 res += curr->costs[i];
403 int co_get_lower_bound(const copy_opt_t *co) {
406 list_for_each_entry(unit_t, curr, &co->units, units)
407 res += curr->inevitable_costs + curr->min_nodes_costs;
411 /******************************************************************************
413 / ____| | | / ____| |
414 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
415 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
416 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
417 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
420 ******************************************************************************/
422 static int compare_node_t(const void *k1, const void *k2, size_t size) {
423 const node_t *n1 = k1;
424 const node_t *n2 = k2;
426 return (n1->irn != n2->irn);
429 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
430 node_t new_node, *node;
431 neighb_t new_nbr, *nbr;
436 new_node.neighbours = NULL;
437 node = set_insert(co->nodes, new_node.irn, sizeof(new_node), HASH_PTR(new_node.irn));
440 for (nbr = node->neighbours; nbr; nbr = nbr->next)
441 if (nbr->irn == n2) {
446 /* if we did not find n2 in n1's neighbourhood insert it */
448 obstack_grow(&co->obst, &new_nbr, sizeof(new_nbr));
449 nbr = obstack_finish(&co->obst);
452 nbr->next = node->neighbours;
453 node->neighbours = nbr;
457 /* now nbr points to n1's neighbour-entry of n2 */
461 static INLINE void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
462 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
463 add_edge(co, n1, n2, costs);
464 add_edge(co, n2, n1, costs);
468 static void build_graph_walker(ir_node *irn, void *env) {
469 copy_opt_t *co = env;
471 arch_register_req_t req;
473 if (!is_curr_reg_class(co, irn) || arch_irn_is_ignore(co->aenv, irn))
478 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
479 ir_node *arg = get_irn_n(irn, pos);
480 add_edges(co, irn, arg, co->get_costs(irn, arg, pos));
484 else if (is_Perm_Proj(co->aenv, irn)) {
485 ir_node *arg = get_Perm_src(irn);
486 add_edges(co, irn, arg, co->get_costs(irn, arg, 0));
490 else if (is_2addr_code(co->aenv, irn, &req))
491 add_edges(co, irn, req.other_same, co->get_costs(irn, req.other_same, 0));
494 void co_build_graph_structure(copy_opt_t *co) {
495 obstack_init(&co->obst);
496 co->nodes = new_set(compare_node_t, 32);
498 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
501 void co_free_graph_structure(copy_opt_t *co) {
503 obstack_free(&co->obst, NULL);
506 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
508 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn) {
512 return (int)set_find(co->nodes, new_node.irn, sizeof(new_node), HASH_PTR(new_node.irn));