Bugfixes
[libfirm] / ir / be / becopyopt.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                12.04.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 #ifdef HAVE_ALLOCA_H
11 #include <alloca.h>
12 #endif
13 #ifdef HAVE_MALLOC_H
14 #include <malloc.h>
15 #endif
16
17 #include "irprog.h"
18 #include "irloop_t.h"
19
20 #include "xmalloc.h"
21 #include "bechordal_t.h"
22 #include "becopyopt.h"
23 #include "becopystat.h"
24
25 static firm_dbg_module_t *dbg = NULL;
26
27 #define is_curr_reg_class(irn) \
28   (arch_get_irn_reg_class(get_arch_env(co), \
29                           irn, arch_pos_make_out(0)) == co->chordal_env->cls)
30
31 #define MIN(a,b) ((a<b)?(a):(b))
32 #define MAX(a,b) ((a<b)?(b):(a))
33
34
35 /**
36  * Determines a maximum weighted independent set with respect to
37  * the interference and conflict edges of all nodes in a qnode.
38  * TODO: This runs in n! in worst case. Use a heuristic iff n>???
39  */
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;
44         bitset_t *curr;
45         int max, pos, curr_weight, best_weight = 0;
46
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
50          */
51         safe = alloca((ou->node_count-1) * sizeof(*safe));
52         safe_costs = 0;
53         safe_count = 0;
54         unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
55         unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
56         unsafe_count = 0;
57         for(i=1; i<ou->node_count; ++i) {
58                 int is_safe = 1;
59                 for(o=1; o<ou->node_count; ++o) {
60                         if (i==o)
61                                 continue;
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];
65                                 ++unsafe_count;
66                                 is_safe = 0;
67                                 break;
68                         }
69                 }
70                 if (is_safe) {
71                         safe_costs += ou->costs[i];
72                         safe[safe_count++] = ou->nodes[i];
73                 }
74         }
75
76
77
78         /* now brute force the best set out of the unsafe nodes*/
79         curr = bitset_alloca(unsafe_count);
80
81         bitset_set_all(curr);
82         while ((max = bitset_popcnt(curr)) != 0) {
83                 /* check if curr is a stable set */
84                 for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
85                         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) */
86                                         if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
87                                                 goto no_stable_set;
88
89                 /* if we arrive here, we have a stable set */
90                 /* compute the weigth of the stable set*/
91                 curr_weight = 0;
92                 bitset_foreach(curr, pos)
93                         curr_weight += unsafe_costs[pos];
94
95                 /* any better ? */
96                 if (curr_weight > best_weight) {
97                         best_weight = curr_weight;
98                 }
99
100 no_stable_set:
101                 bitset_minus1(curr);
102         }
103
104         return safe_costs+best_weight;
105 }
106
107 /**
108  * Builds an optimization unit for a given optimizable irn (root).
109  * This opt-unit is inserted in the main structure co.
110  * If an arg of root itself is optimizable process this arg before with a
111  * recursive call. For handling this situation and loops co->root is used
112  * to remember all roots.
113  */
114 static void co_append_unit(copy_opt_t *co, ir_node *root) {
115         int i, arity;
116         unit_t *unit;
117         struct list_head *tmp;
118
119         DBG((dbg, LEVEL_1, "\t  Root: %n %N\n", root, root));
120         /* check if we encountered this root earlier */
121         if (pset_find_ptr(co->roots, root))
122                 return;
123         pset_insert_ptr(co->roots, root);
124
125         assert(is_curr_reg_class(root) && "node is in wrong register class!");
126
127         /* init unit */
128         arity = get_irn_arity(root);
129         unit = xcalloc(1, sizeof(*unit));
130         unit->co = co;
131         unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
132         unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
133         unit->node_count = 1;
134         unit->nodes[0] = root;
135         INIT_LIST_HEAD(&unit->queue);
136
137         /* check all args */
138         if (is_Phi(root) && mode_is_datab(get_irn_mode(root))) {
139                 for (i=0; i<arity; ++i) {
140                         int o, arg_pos = 0;
141                         ir_node *arg = get_irn_n(root, i);
142
143                         assert(is_curr_reg_class(arg) && "Argument not in same register class.");
144                         if (arg == root)
145                                 continue;
146                         if (nodes_interfere(co->chordal_env, root, arg)) {
147                                 unit->inevitable_costs += co->get_costs(root, arg, i);
148                                 continue;
149                         }
150
151                         /* Else insert the argument of the phi to the members of this ou */
152                         DBG((dbg, LEVEL_1, "\t   Member: %n %N\n", arg, arg));
153
154                         /* Check if arg has occurred at a prior position in the arg/list */
155                         for (o=0; o<unit->node_count; ++o)
156                                 if (unit->nodes[o] == arg) {
157                                         arg_pos = o;
158                                         break;
159                                 }
160
161                         if (!arg_pos) { /* a new argument */
162                                 /* insert node, set costs */
163                                 unit->nodes[unit->node_count] = arg;
164                                 unit->costs[unit->node_count] = co->get_costs(root, arg, i);
165                                 unit->node_count++;
166                         } else { /* arg has occured before in same phi */
167                                 /* increase costs for existing arg */
168                                 unit->costs[arg_pos] += co->get_costs(root, arg, i);
169                         }
170                 }
171                 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
172                 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
173         } else if (is_Copy(get_arch_env(co), root)) {
174                 assert(!nodes_interfere(co->chordal_env, root, get_Copy_src(root)));
175                 unit->nodes[1] = get_Copy_src(root);
176                 unit->costs[1] = co->get_costs(root, unit->nodes[1], -1);
177                 unit->node_count = 2;
178                 unit->nodes = xrealloc(unit->nodes, 2 * sizeof(*unit->nodes));
179                 unit->costs = xrealloc(unit->costs, 2 * sizeof(*unit->costs));
180         } else
181                 assert(0 && "This is not an optimizable node!");
182         /* TODO add ou's for 2-addr-code instructions */
183
184
185         /* Determine the maximum costs this unit can cause: all_nodes_cost */
186         for(i=1; i<unit->node_count; ++i) {
187                 unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
188                 unit->all_nodes_costs += unit->costs[i];
189         }
190
191         /* Determine the minimal costs this unit will cause: min_nodes_costs */
192         unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
193
194         /* Insert the new ou according to its sort_key */
195         tmp = &co->units;
196         while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
197                 tmp = tmp->next;
198         list_add(&unit->units, tmp);
199 }
200
201 static void co_collect_in_block(ir_node *block, void *env) {
202         copy_opt_t *co = env;
203         struct list_head *head = get_block_border_head(co->chordal_env, block);
204         border_t *curr;
205
206         list_for_each_entry_reverse(border_t, curr, head, list)
207                 if (curr->is_def && curr->is_real && is_optimizable(get_arch_env(co), curr->irn))
208                         co_append_unit(co, curr->irn);
209 }
210
211 static void co_collect_units(copy_opt_t *co) {
212         DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
213         co->roots = pset_new_ptr(64);
214         dom_tree_walk_irg(get_irg(co), co_collect_in_block, NULL, co);
215         del_pset(co->roots);
216 }
217
218 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
219         const char *s1, *s2, *s3;
220         int len;
221         copy_opt_t *co;
222
223         dbg = firm_dbg_register("ir.be.copyopt");
224         firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
225
226         co = xcalloc(1, sizeof(*co));
227         co->chordal_env = chordal_env;
228         co->get_costs = get_costs;
229
230         s1 = get_irp_prog_name();
231         s2 = get_entity_name(get_irg_entity(get_irg(co)));
232         s3 = chordal_env->cls->name;
233         len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
234         co->name = xmalloc(len);
235         snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
236         if (!strcmp(co->name, DEBUG_IRG))
237                 firm_dbg_set_mask(dbg, DEBUG_IRG_LVL_CO);
238         else
239                 firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
240
241         INIT_LIST_HEAD(&co->units);
242         co_collect_units(co);
243         return co;
244 }
245
246 void free_copy_opt(copy_opt_t *co) {
247         unit_t *curr, *tmp;
248         xfree(co->name);
249         list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
250                 xfree(curr->nodes);
251                 xfree(curr);
252         }
253 }
254
255 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
256         int i, max;
257         for(i=0, max=get_irn_n_outs(irn); i<max; ++i) {
258                 ir_node *n = get_irn_out(irn, i);
259                 if (((is_Phi(n) && mode_is_datab(get_irn_mode(n))) ||
260                          is_Perm(get_arch_env(co), n)) && (irn == n || !nodes_interfere(co->chordal_env, irn, n)))
261                         return 1;
262         }
263         return 0;
264 }
265
266 int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
267         int cost = 0;
268         ir_loop *loop;
269         ir_node *root_block = get_nodes_block(root);
270
271         assert(pos==-1 || is_Phi(root));
272         if (pos == -1) {
273                 /* a perm places the copy in the same block as it resides */
274                 loop = get_irn_loop(root_block);
275         } else {
276                 /* for phis the copies are placed in the corresponding pred-block */
277                 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
278         }
279         if (loop) {
280                 int d = get_loop_depth(loop);
281                 cost = d*d;
282         }
283         return cost+1;
284 }
285
286 int get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
287         return 1;
288 }
289
290 int co_get_max_copy_costs(const copy_opt_t *co) {
291         int i, res = 0;
292         unit_t *curr;
293
294         list_for_each_entry(unit_t, curr, &co->units, units) {
295                 res += curr->inevitable_costs;
296                 for (i=1; i<curr->node_count; ++i)
297                         res += curr->costs[i];
298         }
299         return res;
300 }
301
302 int co_get_inevit_copy_costs(const copy_opt_t *co) {
303         int res = 0;
304         unit_t *curr;
305
306         list_for_each_entry(unit_t, curr, &co->units, units)
307                 res += curr->inevitable_costs;
308         return res;
309 }
310
311 int co_get_copy_costs(const copy_opt_t *co) {
312         int i, res = 0;
313         unit_t *curr;
314
315         list_for_each_entry(unit_t, curr, &co->units, units) {
316                 int root_col = get_irn_col(co, curr->nodes[0]);
317                 DBG((dbg, LEVEL_1, "  %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
318                 res += curr->inevitable_costs;
319                 for (i=1; i<curr->node_count; ++i) {
320                         int arg_col = get_irn_col(co, curr->nodes[i]);
321                         if (root_col != arg_col) {
322                                 DBG((dbg, LEVEL_1, "  %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
323                                 res += curr->costs[i];
324                         }
325                 }
326         }
327         return res;
328 }
329
330 int co_get_lower_bound(const copy_opt_t *co) {
331         int res = 0;
332         unit_t *curr;
333         list_for_each_entry(unit_t, curr, &co->units, units)
334                 res += curr->inevitable_costs + curr->min_nodes_costs;
335         return res;
336 }