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 assert(0 && "Is buggy and obsolete. Do not use");
93 if (arch_irn_is_ignore(co->aenv, irn))
96 foreach_out_edge(irn, edge) {
97 ir_node *n = edge->src;
99 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
100 arch_register_req_t req;
101 arch_get_register_req(co->aenv, &req, n, -1);
104 is_Perm(co->aenv, n) ||
105 (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
114 int co_get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
117 ir_node *root_block = get_nodes_block(root);
120 /* for phis the copies are placed in the corresponding pred-block */
121 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
123 /* a perm places the copy in the same block as it resides */
124 loop = get_irn_loop(root_block);
127 int d = get_loop_depth(loop);
133 int co_get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
137 /******************************************************************************
138 ____ _ _ _ _ _ _____ _
139 / __ \ | | | | | | (_) | / ____| |
140 | | | |_ __ | |_| | | |_ __ _| |_ ___ | (___ | |_ ___ _ __ __ _ __ _ ___
141 | | | | '_ \| __| | | | '_ \| | __/ __| \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
142 | |__| | |_) | |_| |__| | | | | | |_\__ \ ____) | || (_) | | | (_| | (_| | __/
143 \____/| .__/ \__|\____/|_| |_|_|\__|___/ |_____/ \__\___/|_| \__,_|\__, |\___|
146 ******************************************************************************/
149 * Determines a maximum weighted independent set with respect to
150 * the interference and conflict edges of all nodes in a qnode.
152 static int ou_max_ind_set_costs(unit_t *ou) {
153 be_chordal_env_t *chordal_env = ou->co->cenv;
154 ir_node **safe, **unsafe;
155 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
157 int max, pos, curr_weight, best_weight = 0;
159 /* assign the nodes into two groups.
160 * safe: node has no interference, hence it is in every max stable set.
161 * unsafe: node has an interference
163 safe = alloca((ou->node_count-1) * sizeof(*safe));
166 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
167 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
169 for(i=1; i<ou->node_count; ++i) {
171 for(o=1; o<ou->node_count; ++o) {
174 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
175 unsafe_costs[unsafe_count] = ou->costs[i];
176 unsafe[unsafe_count] = ou->nodes[i];
183 safe_costs += ou->costs[i];
184 safe[safe_count++] = ou->nodes[i];
189 /* now compute the best set out of the unsafe nodes*/
190 if (unsafe_count > MIS_HEUR_TRIGGER) {
191 bitset_t *best = bitset_alloca(unsafe_count);
192 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
193 for (i=0; i<unsafe_count; ++i) {
195 /* check if it is a stable set */
196 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
197 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
198 bitset_clear(best, i); /* clear the bit and try next one */
202 /* compute the weight */
203 bitset_foreach(best, pos)
204 best_weight += unsafe_costs[pos];
206 /* Exact Algorithm: Brute force */
207 curr = bitset_alloca(unsafe_count);
208 bitset_set_all(curr);
209 while ((max = bitset_popcnt(curr)) != 0) {
210 /* check if curr is a stable set */
211 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
212 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) */
213 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
216 /* if we arrive here, we have a stable set */
217 /* compute the weigth of the stable set*/
219 bitset_foreach(curr, pos)
220 curr_weight += unsafe_costs[pos];
223 if (curr_weight > best_weight) {
224 best_weight = curr_weight;
232 return safe_costs+best_weight;
235 static void co_collect_units(ir_node *irn, void *env) {
236 copy_opt_t *co = env;
238 arch_register_req_t req;
240 if (!is_curr_reg_class(co, irn))
242 if (!co_is_optimizable_root(co, irn))
245 /* Init a new unit */
246 unit = xcalloc(1, sizeof(*unit));
248 unit->node_count = 1;
249 INIT_LIST_HEAD(&unit->queue);
251 /* Phi with some/all of its arguments */
252 if (is_Reg_Phi(irn)) {
256 arity = get_irn_arity(irn);
257 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
258 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
259 unit->nodes[0] = irn;
262 for (i=0; i<arity; ++i) {
264 ir_node *arg = get_irn_n(irn, i);
266 assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
269 if (nodes_interfere(co->cenv, irn, arg)) {
270 unit->inevitable_costs += co->get_costs(irn, arg, i);
274 /* Else insert the argument of the phi to the members of this ou */
275 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
277 /* Check if arg has occurred at a prior position in the arg/list */
279 for (o=0; o<unit->node_count; ++o)
280 if (unit->nodes[o] == arg) {
285 if (!arg_pos) { /* a new argument */
286 /* insert node, set costs */
287 unit->nodes[unit->node_count] = arg;
288 unit->costs[unit->node_count] = co->get_costs(irn, arg, i);
290 } else { /* arg has occured before in same phi */
291 /* increase costs for existing arg */
292 unit->costs[arg_pos] += co->get_costs(irn, arg, i);
295 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
296 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
299 /* Proj of a perm with corresponding arg */
300 if (is_Perm_Proj(co->aenv, irn)) {
301 assert(!nodes_interfere(co->cenv, irn, get_Perm_src(irn)));
302 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
303 unit->costs = xmalloc(2 * sizeof(*unit->costs));
304 unit->node_count = 2;
305 unit->nodes[0] = irn;
306 unit->nodes[1] = get_Perm_src(irn);
307 unit->costs[1] = co->get_costs(irn, unit->nodes[1], -1);
310 /* Src == Tgt of a 2-addr-code instruction */
311 if (is_2addr_code(co->aenv, irn, &req)) {
312 ir_node *other = req.other_same;
313 if (!nodes_interfere(co->cenv, irn, other)) {
314 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
315 unit->costs = xmalloc(2 * sizeof(*unit->costs));
316 unit->node_count = 2;
317 unit->nodes[0] = irn;
318 unit->nodes[1] = other;
319 unit->costs[1] = co->get_costs(irn, other, -120480);
322 assert(0 && "This is not an optimizable node!");
324 /* Insert the new unit at a position according to its costs */
325 if (unit->node_count > 1) {
327 struct list_head *tmp;
329 /* Determine the maximum costs this unit can cause: all_nodes_cost */
330 for(i=1; i<unit->node_count; ++i) {
331 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
332 unit->all_nodes_costs += unit->costs[i];
335 /* Determine the minimal costs this unit will cause: min_nodes_costs */
336 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
338 /* Insert the new ou according to its sort_key */
340 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
342 list_add(&unit->units, tmp);
348 void co_build_ou_structure(copy_opt_t *co) {
349 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
350 INIT_LIST_HEAD(&co->units);
351 irg_walk_graph(co->irg, co_collect_units, NULL, co);
354 void co_free_ou_structure(copy_opt_t *co) {
356 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
363 /* co_solve_heuristic() is implemented in becopyheur.c */
365 int co_get_max_copy_costs(const copy_opt_t *co) {
369 list_for_each_entry(unit_t, curr, &co->units, units) {
370 res += curr->inevitable_costs;
371 for (i=1; i<curr->node_count; ++i)
372 res += curr->costs[i];
377 int co_get_inevit_copy_costs(const copy_opt_t *co) {
381 list_for_each_entry(unit_t, curr, &co->units, units)
382 res += curr->inevitable_costs;
386 int co_get_copy_costs(const copy_opt_t *co) {
390 list_for_each_entry(unit_t, curr, &co->units, units) {
391 int root_col = get_irn_col(co, curr->nodes[0]);
392 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
393 res += curr->inevitable_costs;
394 for (i=1; i<curr->node_count; ++i) {
395 int arg_col = get_irn_col(co, curr->nodes[i]);
396 if (root_col != arg_col) {
397 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
398 res += curr->costs[i];
405 int co_get_lower_bound(const copy_opt_t *co) {
408 list_for_each_entry(unit_t, curr, &co->units, units)
409 res += curr->inevitable_costs + curr->min_nodes_costs;
413 /******************************************************************************
415 / ____| | | / ____| |
416 | | __ _ __ __ _ _ __ | |__ | (___ | |_ ___ _ __ __ _ __ _ ___
417 | | |_ | '__/ _` | '_ \| '_ \ \___ \| __/ _ \| '__/ _` |/ _` |/ _ \
418 | |__| | | | (_| | |_) | | | | ____) | || (_) | | | (_| | (_| | __/
419 \_____|_| \__,_| .__/|_| |_| |_____/ \__\___/|_| \__,_|\__, |\___|
422 ******************************************************************************/
424 static int compare_affinity_t(const void *k1, const void *k2, size_t size) {
425 const affinity_t *n1 = k1;
426 const affinity_t *n2 = k2;
428 return (n1->irn != n2->irn);
431 static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
432 affinity_t new_node, *node;
433 neighb_t new_nbr, *nbr;
438 new_node.neighbours = NULL;
439 node = set_insert(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
442 for (nbr = node->neighbours; nbr; nbr = nbr->next)
443 if (nbr->irn == n2) {
448 /* if we did not find n2 in n1's neighbourhood insert it */
450 obstack_grow(&co->obst, &new_nbr, sizeof(new_nbr));
451 nbr = obstack_finish(&co->obst);
454 nbr->next = node->neighbours;
455 node->neighbours = nbr;
459 /* now nbr points to n1's neighbour-entry of n2 */
463 static INLINE void add_edges(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
464 if (! be_ifg_connected(co->cenv->ifg, n1, n2)) {
465 add_edge(co, n1, n2, costs);
466 add_edge(co, n2, n1, costs);
470 static void build_graph_walker(ir_node *irn, void *env) {
471 copy_opt_t *co = env;
473 arch_register_req_t req;
475 if (!is_curr_reg_class(co, irn) || arch_irn_is_ignore(co->aenv, irn))
480 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
481 ir_node *arg = get_irn_n(irn, pos);
482 add_edges(co, irn, arg, co->get_costs(irn, arg, pos));
486 else if (is_Perm_Proj(co->aenv, irn)) {
487 ir_node *arg = get_Perm_src(irn);
488 add_edges(co, irn, arg, co->get_costs(irn, arg, 0));
492 else if (is_2addr_code(co->aenv, irn, &req))
493 add_edges(co, irn, req.other_same, co->get_costs(irn, req.other_same, 0));
496 void co_build_graph_structure(copy_opt_t *co) {
497 obstack_init(&co->obst);
498 co->nodes = new_set(compare_affinity_t, 32);
500 irg_walk_graph(co->irg, build_graph_walker, NULL, co);
503 void co_free_graph_structure(copy_opt_t *co) {
505 obstack_free(&co->obst, NULL);
508 /* co_solve_ilp1() co_solve_ilp2() are implemented in becopyilpX.c */
510 int co_gs_is_optimizable(copy_opt_t *co, ir_node *irn) {
511 affinity_t new_node, *n;
514 n = set_find(co->nodes, &new_node, sizeof(new_node), HASH_PTR(new_node.irn));
516 return (n->count > 0);