0a6cb0ef81868d3e8ed6eedcb13ee5796487c61b
[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 "beutil.h"
22 #include "bechordal_t.h"
23 #include "becopyopt.h"
24 #include "becopystat.h"
25
26 static firm_dbg_module_t *dbg = NULL;
27
28 #define is_curr_reg_class(irn) \
29   (arch_get_irn_reg_class(get_arch_env(co), \
30                           irn, -1) == co->chordal_env->cls)
31
32 #define MIN(a,b) ((a<b)?(a):(b))
33 #define MAX(a,b) ((a<b)?(b):(a))
34
35
36 /**
37  * Determines a maximum weighted independent set with respect to
38  * the interference and conflict edges of all nodes in a qnode.
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         /* 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) {
82                         bitset_set(best, 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 */
87                                         break;
88                                 }
89                 }
90                 /* compute the weight */
91                 bitset_foreach(best, pos)
92                         best_weight += unsafe_costs[pos];
93         } else {
94                 /* Exact Algorithm: Brute force */
95                 curr = bitset_alloca(unsafe_count);
96                 bitset_set_all(curr);
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]))
102                                                         goto no_stable_set;
103
104                         /* if we arrive here, we have a stable set */
105                         /* compute the weigth of the stable set*/
106                         curr_weight = 0;
107                         bitset_foreach(curr, pos)
108                                 curr_weight += unsafe_costs[pos];
109
110                         /* any better ? */
111                         if (curr_weight > best_weight) {
112                                 best_weight = curr_weight;
113                         }
114
115         no_stable_set:
116                         bitset_minus1(curr);
117                 }
118         }
119
120         return safe_costs+best_weight;
121 }
122
123 static void co_collect_units(ir_node *irn, void *env) {
124         copy_opt_t *co = env;
125         unit_t *unit;
126         arch_register_req_t req;
127         arch_env_t *aenv = co->chordal_env->main_env->arch_env;
128
129         if (!is_curr_reg_class(irn))
130                 return;
131         if (!is_optimizable(get_arch_env(co), irn, &req))
132                 return;
133
134         /* Init a new unit */
135         unit = xcalloc(1, sizeof(*unit));
136         unit->co = co;
137         unit->node_count = 1;
138         INIT_LIST_HEAD(&unit->queue);
139
140         /* Phi with some/all of its arguments */
141         if (is_Reg_Phi(irn)) {
142                 int i, arity;
143
144                 /* init */
145                 arity = get_irn_arity(irn);
146                 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
147                 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
148                 unit->nodes[0] = irn;
149
150                 /* fill */
151                 for (i=0; i<arity; ++i) {
152                         int o, arg_pos;
153                         ir_node *arg = get_irn_n(irn, i);
154
155                         assert(is_curr_reg_class(arg) && "Argument not in same register class.");
156                         if (arg == irn)
157                                 continue;
158                         if (nodes_interfere(co->chordal_env, irn, arg)) {
159                                 unit->inevitable_costs += co->get_costs(irn, arg, i);
160                                 continue;
161                         }
162
163                         /* Else insert the argument of the phi to the members of this ou */
164                         DBG((dbg, LEVEL_1, "\t   Member: %+F\n", arg));
165
166                         /* Check if arg has occurred at a prior position in the arg/list */
167                         arg_pos = 0;
168                         for (o=0; o<unit->node_count; ++o)
169                                 if (unit->nodes[o] == arg) {
170                                         arg_pos = o;
171                                         break;
172                                 }
173
174                         if (!arg_pos) { /* a new argument */
175                                 /* insert node, set costs */
176                                 unit->nodes[unit->node_count] = arg;
177                                 unit->costs[unit->node_count] = co->get_costs(irn, arg, i);
178                                 unit->node_count++;
179                         } else { /* arg has occured before in same phi */
180                                 /* increase costs for existing arg */
181                                 unit->costs[arg_pos] += co->get_costs(irn, arg, i);
182                         }
183                 }
184                 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
185                 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
186         } else
187
188         /* Proj of a perm with corresponding arg */
189         if (is_Perm_Proj(get_arch_env(co), irn)) {
190                 assert(!nodes_interfere(co->chordal_env, irn, get_Copy_src(irn)));
191                 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
192                 unit->costs = xmalloc(2 * sizeof(*unit->costs));
193                 unit->node_count = 2;
194                 unit->nodes[0] = irn;
195                 unit->nodes[1] = get_Copy_src(irn);
196                 unit->costs[1] = co->get_costs(irn, unit->nodes[1], -1);
197         } else
198
199         /* Src == Tgt of a 2-addr-code instruction */
200         if (is_2addr_code(get_arch_env(co), irn, &req)) {
201                 ir_node *other = req.other;
202                 if (!nodes_interfere(co->chordal_env, irn, other)) {
203                         unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
204                         unit->costs = xmalloc(2 * sizeof(*unit->costs));
205                         unit->node_count = 2;
206                         unit->nodes[0] = irn;
207                         unit->nodes[1] = other;
208                         unit->costs[1] = co->get_costs(irn, other, -120480);
209                 }
210         } else
211                 assert(0 && "This is not an optimizable node!");
212
213         /* Insert the new unit at a position according to its costs */
214         if (unit->node_count > 1) {
215                 int i;
216                 struct list_head *tmp;
217
218                 /* Determine the maximum costs this unit can cause: all_nodes_cost */
219                 for(i=1; i<unit->node_count; ++i) {
220                         unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
221                         unit->all_nodes_costs += unit->costs[i];
222                 }
223
224                 /* Determine the minimal costs this unit will cause: min_nodes_costs */
225                 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
226
227                 /* Insert the new ou according to its sort_key */
228                 tmp = &co->units;
229                 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
230                         tmp = tmp->next;
231                 list_add(&unit->units, tmp);
232         } else {
233                 free(unit);
234         }
235 }
236
237 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
238         const char *s1, *s2, *s3;
239         int len;
240         copy_opt_t *co;
241
242         dbg = firm_dbg_register("ir.be.copyopt");
243
244         co = xcalloc(1, sizeof(*co));
245         co->chordal_env = chordal_env;
246         co->get_costs = get_costs;
247
248         s1 = get_irp_prog_name();
249         s2 = get_entity_name(get_irg_entity(get_irg(co)));
250         s3 = chordal_env->cls->name;
251         len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
252         co->name = xmalloc(len);
253         snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
254
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);
258         return co;
259 }
260
261 void free_copy_opt(copy_opt_t *co) {
262         unit_t *curr, *tmp;
263         xfree(co->name);
264         list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
265                 xfree(curr->nodes);
266                 xfree(curr->costs);
267                 xfree(curr);
268         }
269 }
270
271 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
272         arch_env_t *aenv = get_arch_env(co);
273         const ir_edge_t *edge;
274
275         if (arch_irn_is_ignore(aenv, irn))
276                 return 0;
277
278         foreach_out_edge(irn, edge) {
279                 ir_node *n = edge->src;
280
281                 if (!nodes_interfere(co->chordal_env, irn, n) || irn == n) {
282                         arch_register_req_t req;
283                         arch_get_register_req(aenv, &req, n, -1);
284
285                         if(is_Reg_Phi(n) ||
286                            is_Perm(aenv, n) ||
287                            (arch_register_req_is(&req, should_be_same) && req.other == irn)
288                           )
289                                 return 1;
290                 }
291         }
292
293         return 0;
294 }
295
296 int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
297         int cost = 0;
298         ir_loop *loop;
299         ir_node *root_block = get_nodes_block(root);
300
301         if (is_Phi(root)) {
302                 /* for phis the copies are placed in the corresponding pred-block */
303                 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
304         } else {
305                 /* a perm places the copy in the same block as it resides */
306                 loop = get_irn_loop(root_block);
307         }
308         if (loop) {
309                 int d = get_loop_depth(loop);
310                 cost = d*d;
311         }
312         return cost+1;
313 }
314
315 int get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
316         return 1;
317 }
318
319 int co_get_max_copy_costs(const copy_opt_t *co) {
320         int i, res = 0;
321         unit_t *curr;
322
323         list_for_each_entry(unit_t, curr, &co->units, units) {
324                 res += curr->inevitable_costs;
325                 for (i=1; i<curr->node_count; ++i)
326                         res += curr->costs[i];
327         }
328         return res;
329 }
330
331 int co_get_inevit_copy_costs(const copy_opt_t *co) {
332         int res = 0;
333         unit_t *curr;
334
335         list_for_each_entry(unit_t, curr, &co->units, units)
336                 res += curr->inevitable_costs;
337         return res;
338 }
339
340 int co_get_copy_costs(const copy_opt_t *co) {
341         int i, res = 0;
342         unit_t *curr;
343
344         list_for_each_entry(unit_t, curr, &co->units, units) {
345                 int root_col = get_irn_col(co, curr->nodes[0]);
346                 DBG((dbg, LEVEL_1, "  %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
347                 res += curr->inevitable_costs;
348                 for (i=1; i<curr->node_count; ++i) {
349                         int arg_col = get_irn_col(co, curr->nodes[i]);
350                         if (root_col != arg_col) {
351                                 DBG((dbg, LEVEL_1, "  %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
352                                 res += curr->costs[i];
353                         }
354                 }
355         }
356         return res;
357 }
358
359 int co_get_lower_bound(const copy_opt_t *co) {
360         int res = 0;
361         unit_t *curr;
362         list_for_each_entry(unit_t, curr, &co->units, units)
363                 res += curr->inevitable_costs + curr->min_nodes_costs;
364         return res;
365 }