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