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