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