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