4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 #include "bechordal_t.h"
23 #include "becopyopt.h"
24 #include "becopystat.h"
26 static firm_dbg_module_t *dbg = NULL;
28 #define is_curr_reg_class(irn) \
29 (arch_get_irn_reg_class(get_arch_env(co), \
30 irn, -1) == co->chordal_env->cls)
32 #define MIN(a,b) ((a<b)?(a):(b))
33 #define MAX(a,b) ((a<b)?(b):(a))
37 * Determines a maximum weighted independent set with respect to
38 * the interference and conflict edges of all nodes in a qnode.
40 static int ou_max_ind_set_costs(unit_t *ou) {
41 be_chordal_env_t *chordal_env = ou->co->chordal_env;
42 ir_node **safe, **unsafe;
43 int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
45 int max, pos, curr_weight, best_weight = 0;
47 /* assign the nodes into two groups.
48 * safe: node has no interference, hence it is in every max stable set.
49 * unsafe: node has an interference
51 safe = alloca((ou->node_count-1) * sizeof(*safe));
54 unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
55 unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
57 for(i=1; i<ou->node_count; ++i) {
59 for(o=1; o<ou->node_count; ++o) {
62 if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
63 unsafe_costs[unsafe_count] = ou->costs[i];
64 unsafe[unsafe_count] = ou->nodes[i];
71 safe_costs += ou->costs[i];
72 safe[safe_count++] = ou->nodes[i];
77 /* now compute the best set out of the unsafe nodes*/
78 if (unsafe_count > MIS_HEUR_TRIGGER) {
79 bitset_t *best = bitset_alloca(unsafe_count);
80 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
81 for (i=0; i<unsafe_count; ++i) {
83 /* check if it is a stable set */
84 for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
85 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
86 bitset_clear(best, i); /* clear the bit and try next one */
90 /* compute the weight */
91 bitset_foreach(best, pos)
92 best_weight += unsafe_costs[pos];
94 /* Exact Algorithm: Brute force */
95 curr = bitset_alloca(unsafe_count);
97 while ((max = bitset_popcnt(curr)) != 0) {
98 /* check if curr is a stable set */
99 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
100 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) */
101 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
104 /* if we arrive here, we have a stable set */
105 /* compute the weigth of the stable set*/
107 bitset_foreach(curr, pos)
108 curr_weight += unsafe_costs[pos];
111 if (curr_weight > best_weight) {
112 best_weight = curr_weight;
120 return safe_costs+best_weight;
123 static void co_collect_units(ir_node *irn, void *env) {
124 copy_opt_t *co = env;
126 arch_register_req_t req;
128 if (!is_curr_reg_class(irn))
130 if (!is_optimizable(get_arch_env(co), irn, &req))
133 /* Init a new unit */
134 unit = xcalloc(1, sizeof(*unit));
136 unit->node_count = 1;
137 INIT_LIST_HEAD(&unit->queue);
139 /* Phi with some/all of its arguments */
140 if (is_Reg_Phi(irn)) {
144 arity = get_irn_arity(irn);
145 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
146 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
147 unit->nodes[0] = irn;
150 for (i=0; i<arity; ++i) {
152 ir_node *arg = get_irn_n(irn, i);
154 assert(is_curr_reg_class(arg) && "Argument not in same register class.");
157 if (nodes_interfere(co->chordal_env, irn, arg)) {
158 unit->inevitable_costs += co->get_costs(irn, arg, i);
162 /* Else insert the argument of the phi to the members of this ou */
163 DBG((dbg, LEVEL_1, "\t Member: %+F\n", arg));
165 /* Check if arg has occurred at a prior position in the arg/list */
167 for (o=0; o<unit->node_count; ++o)
168 if (unit->nodes[o] == arg) {
173 if (!arg_pos) { /* a new argument */
174 /* insert node, set costs */
175 unit->nodes[unit->node_count] = arg;
176 unit->costs[unit->node_count] = co->get_costs(irn, arg, i);
178 } else { /* arg has occured before in same phi */
179 /* increase costs for existing arg */
180 unit->costs[arg_pos] += co->get_costs(irn, arg, i);
183 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
184 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
187 /* Proj of a perm with corresponding arg */
188 if (is_Copy(get_arch_env(co), irn)) {
189 assert(!nodes_interfere(co->chordal_env, irn, get_Copy_src(irn)));
190 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
191 unit->costs = xmalloc(2 * sizeof(*unit->costs));
192 unit->node_count = 2;
193 unit->nodes[0] = irn;
194 unit->nodes[1] = get_Copy_src(irn);
195 unit->costs[1] = co->get_costs(irn, unit->nodes[1], -1);
198 /* Src == Tgt of a 2-addr-code instruction */
199 if (is_2addr_code(get_arch_env(co), irn, &req)) {
200 int pos = req.data.pos;
201 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
202 unit->costs = xmalloc(2 * sizeof(*unit->costs));
203 unit->node_count = 2;
204 unit->nodes[0] = irn;
205 unit->nodes[1] = get_irn_n(irn, pos);
206 unit->costs[1] = co->get_costs(irn, unit->nodes[1], pos);
208 assert(0 && "This is not an optimizable node!");
210 /* Insert the new unit at a position according to its costs */
213 struct list_head *tmp;
215 /* Determine the maximum costs this unit can cause: all_nodes_cost */
216 for(i=1; i<unit->node_count; ++i) {
217 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
218 unit->all_nodes_costs += unit->costs[i];
221 /* Determine the minimal costs this unit will cause: min_nodes_costs */
222 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
224 /* Insert the new ou according to its sort_key */
226 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
228 list_add(&unit->units, tmp);
232 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
233 const char *s1, *s2, *s3;
237 dbg = firm_dbg_register("ir.be.copyopt");
238 firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
240 co = xcalloc(1, sizeof(*co));
241 co->chordal_env = chordal_env;
242 co->get_costs = get_costs;
244 s1 = get_irp_prog_name();
245 s2 = get_entity_name(get_irg_entity(get_irg(co)));
246 s3 = chordal_env->cls->name;
247 len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
248 co->name = xmalloc(len);
249 snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
250 if (!strcmp(co->name, DEBUG_IRG))
251 firm_dbg_set_mask(dbg, DEBUG_IRG_LVL_CO);
253 firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
255 DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
256 INIT_LIST_HEAD(&co->units);
257 irg_walk_graph(get_irg(co), co_collect_units, NULL, co);
261 void free_copy_opt(copy_opt_t *co) {
264 list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
270 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
272 for(i=0, max=get_irn_n_outs(irn); i<max; ++i) {
273 ir_node *n = get_irn_out(irn, i);
274 if (((is_Phi(n) && is_firm_be_mode(get_irn_mode(n))) ||
275 is_Perm(get_arch_env(co), n)) && (irn == n || !nodes_interfere(co->chordal_env, irn, n)))
281 int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
284 ir_node *root_block = get_nodes_block(root);
287 /* for phis the copies are placed in the corresponding pred-block */
288 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
290 /* a perm places the copy in the same block as it resides */
291 loop = get_irn_loop(root_block);
294 int d = get_loop_depth(loop);
300 int get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
304 int co_get_max_copy_costs(const copy_opt_t *co) {
308 list_for_each_entry(unit_t, curr, &co->units, units) {
309 res += curr->inevitable_costs;
310 for (i=1; i<curr->node_count; ++i)
311 res += curr->costs[i];
316 int co_get_inevit_copy_costs(const copy_opt_t *co) {
320 list_for_each_entry(unit_t, curr, &co->units, units)
321 res += curr->inevitable_costs;
325 int co_get_copy_costs(const copy_opt_t *co) {
329 list_for_each_entry(unit_t, curr, &co->units, units) {
330 int root_col = get_irn_col(co, curr->nodes[0]);
331 DBG((dbg, LEVEL_1, " %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
332 res += curr->inevitable_costs;
333 for (i=1; i<curr->node_count; ++i) {
334 int arg_col = get_irn_col(co, curr->nodes[i]);
335 if (root_col != arg_col) {
336 DBG((dbg, LEVEL_1, " %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
337 res += curr->costs[i];
344 int co_get_lower_bound(const copy_opt_t *co) {
347 list_for_each_entry(unit_t, curr, &co->units, units)
348 res += curr->inevitable_costs + curr->min_nodes_costs;