Fixed small copy & paste error in the "foreach" defines and added additional defines...
[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
19 #include "xmalloc.h"
20 #include "bechordal_t.h"
21 #include "becopyopt.h"
22 #include "becopystat.h"
23
24 #define DEBUG_LVL 0 //SET_LEVEL_1
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
31 /**
32  * Computes a 'max independent set' wrt. ifg-edges only (no coloring conflicts, no register constraints)
33  * @return The size of such a mis
34  * NOTE: Code adapted from becopyheur
35  * BETTER: Here we can be sure having a chordal graph to work on, so for 'larger' opt-units we
36  *         could use a special algorithm.
37  */
38 static int get_ifg_mis_size(unit_t *ou) {
39         int all_size, curr_size, i, o;
40         int *which;
41         ir_node **curr, **all = alloca(ou->node_count * sizeof(*all));
42
43         /* all contains all nodes */
44         all_size = 0;
45         for (i=0; i<ou->node_count; ++i)
46                 all[all_size++] = ou->nodes[i];
47
48         /* which[i] says which element to take out of all[] and put into curr[i] */
49         which = alloca(all_size*sizeof(*which));
50         for (curr_size=0; curr_size<all_size; ++curr_size)
51                 which[curr_size] = curr_size;
52
53         /* stores the currently examined set */
54         curr = alloca(all_size*sizeof(*curr));
55
56         while (1) { /* this loop will terminate because at least a single node will be a max indep. set */
57                 /* build current set */
58                 for (i=0; i<curr_size; ++i)
59                         curr[i] = all[which[all_size-curr_size+i]];
60
61                 /* check current set */
62                 for (i=0; i<curr_size; ++i)
63                         for (o=i+1; o<curr_size; ++o)
64                                 if (nodes_interfere(ou->co->chordal_env, curr[i], curr[o]))
65                                         goto conflict_found;
66
67                 /* We had no conflict. This is the (one) max indep. set */
68                 return curr_size;
69
70 conflict_found:
71                 /* We had a conflict. Generate next set */
72                 if (which[all_size-curr_size+1] == all_size-curr_size+1) {
73                         curr_size--;
74                         for (i=0; i<curr_size; ++i)
75                                 which[all_size-curr_size+i] = i;
76                 } else {
77                         int redo = 1;
78                         while (redo) {
79                                 int pos = all_size;
80                                 do {
81                                         pos--;
82                                 } while (!(which[pos] = (which[pos]+1) % all_size));
83
84                                 for (i=pos+1; i<all_size; ++i)
85                                         which[i] = MIN(which[i-1]+1, all_size-1);
86
87                                 redo = 0;
88                                 for (i=all_size-curr_size; i<all_size-1; ++i)
89                                         if (which[i]>=which[i+1]) {
90                                                 redo = 1;
91                                                 break;
92                                         }
93                         }
94                 }
95         }
96         assert(0 && "How did you get here?");
97 }
98
99 /**
100  * Builds an optimization unit for a given optimizable irn (root).
101  * This opt-unit is inserted in the main structure co.
102  * If an arg of root itself is optimizable process this arg before with a
103  * recursive call. For handling this situation and loops co->root is used
104  * to remember all roots.
105  */
106 static void co_append_unit(copy_opt_t *co, ir_node *root) {
107         int i, arity;
108         unit_t *unit;
109         DBG((dbg, LEVEL_1, "\t  Root: %n %N\n", root, root));
110         /* check if we encountered this root earlier */
111         if (pset_find_ptr(co->roots, root))
112                 return;
113         pset_insert_ptr(co->roots, root);
114
115         assert(is_curr_reg_class(root) && "node is in wrong register class!");
116
117         /* init unit */
118         arity = get_irn_arity(root);
119         unit = xcalloc(1, sizeof(*unit));
120         unit->co = co;
121         unit->interf = 0;
122         unit->node_count = 1;
123         unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
124         unit->nodes[0] = root;
125         INIT_LIST_HEAD(&unit->queue);
126
127         /* check all args */
128         if (is_Phi(root)) {
129                 for (i=0; i<arity; ++i) {
130                         ir_node *arg = get_irn_n(root, i);
131                         assert(is_curr_reg_class(arg) && "Argument not in same register class.");
132                         if (arg != root) {
133                                 if (!nodes_interfere(co->chordal_env, root, arg)) {
134                                         DBG((dbg, LEVEL_1, "\t   Member: %n %N\n", arg, arg));
135                                         if (is_optimizable(co->chordal_env->arch_env, arg))
136                                                 co_append_unit(co, arg);
137                                         unit->nodes[unit->node_count++] = arg;
138                                 } else
139                                         unit->interf++;
140                         }
141                 }
142                 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
143         } else if (is_Copy(co->chordal_env->arch_env, root)) {
144                 assert(!nodes_interfere(co->chordal_env, root, get_Copy_src(root)));
145                 unit->nodes[unit->node_count++] = get_Copy_src(root);
146                 unit->nodes = xrealloc(unit->nodes, 2 * sizeof(*unit->nodes));
147         } else
148                 assert(0 && "This is not an optimizable node!");
149
150         list_add_tail(&unit->units, &co->units);
151         /* Init ifg_mis_size to node_count. So get_lower_bound returns correct results. */
152         unit->ifg_mis_size = get_ifg_mis_size(unit);
153 }
154
155 static void co_collect_in_block(ir_node *block, void *env) {
156         copy_opt_t *co = env;
157         struct list_head *head = get_block_border_head(co->chordal_env, block);
158         border_t *curr;
159
160         list_for_each_entry_reverse(border_t, curr, head, list)
161                 if (curr->is_def && curr->is_real && is_optimizable(co->chordal_env->arch_env, curr->irn))
162                         co_append_unit(co, curr->irn);
163 }
164
165 static void co_collect_units(copy_opt_t *co) {
166         DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
167         co->roots = pset_new_ptr(64);
168         dom_tree_walk_irg(co->chordal_env->irg, co_collect_in_block, NULL, co);
169         del_pset(co->roots);
170 }
171
172 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env) {
173         const char *s1, *s2, *s3;
174         int len;
175         copy_opt_t *co;
176
177         dbg = firm_dbg_register("ir.be.copyopt");
178         firm_dbg_set_mask(dbg, DEBUG_LVL);
179
180         co = xcalloc(1, sizeof(*co));
181         co->chordal_env = chordal_env;
182
183         s1 = get_irp_prog_name();
184         s2 = get_entity_name(get_irg_entity(co->chordal_env->irg));
185         s3 = chordal_env->cls->name;
186         len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
187         co->name = xmalloc(len);
188         snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
189         if (!strcmp(co->name, DEBUG_IRG))
190                 firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
191         else
192                 firm_dbg_set_mask(dbg, DEBUG_LVL);
193
194         INIT_LIST_HEAD(&co->units);
195         co_collect_units(co);
196         return co;
197 }
198
199 void free_copy_opt(copy_opt_t *co) {
200         unit_t *curr, *tmp;
201         xfree(co->name);
202         list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
203                 xfree(curr->nodes);
204                 xfree(curr);
205         }
206 }
207
208 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
209         int i, max;
210         for(i=0, max=get_irn_n_outs(irn); i<max; ++i) {
211                 ir_node *n = get_irn_out(irn, i);
212                 if ((is_Phi(n) || is_Perm(co->chordal_env->arch_env, n)) && (irn == n || !nodes_interfere(co->chordal_env, irn, n)))
213                         return 1;
214         }
215         return 0;
216 }
217
218 int co_get_copy_count(const copy_opt_t *co) {
219         int i, res = 0;
220         unit_t *curr;
221         list_for_each_entry(unit_t, curr, &co->units, units) {
222                 int root_col = get_irn_col(co, curr->nodes[0]);
223                 res += curr->interf;
224                 DBG((dbg, LEVEL_1, "%n %N has %d intf\n", curr->nodes[0], curr->nodes[0], curr->interf));
225                 for (i=1; i<curr->node_count; ++i)
226                         if (root_col != get_irn_col(co, curr->nodes[i])) {
227                                 DBG((dbg, LEVEL_1, "  %n %N\n", curr->nodes[i], curr->nodes[i]));
228                                 res++;
229                         }
230         }
231         return res;
232 }
233
234 int co_get_lower_bound(const copy_opt_t *co) {
235         int res = 0;
236         unit_t *curr;
237         list_for_each_entry(unit_t, curr, &co->units, units)
238                 res += curr->interf + curr->node_count - curr->ifg_mis_size;
239         return res;
240 }
241
242 int co_get_interferer_count(const copy_opt_t *co) {
243         int res = 0;
244         unit_t *curr;
245         list_for_each_entry(unit_t, curr, &co->units, units)
246                 res += curr->interf;
247         return res;
248 }
249
250 /**
251  * Needed for result checking
252  */
253 static void co_collect_for_checker(ir_node *block, void *env) {
254         copy_opt_t *co = env;
255         struct list_head *head = get_block_border_head(co->chordal_env, block);
256         border_t *curr;
257
258         list_for_each_entry_reverse(border_t, curr, head, list)
259                 if (curr->is_def && curr->is_real && is_curr_reg_class(curr->irn))
260                         obstack_ptr_grow(&co->ob, curr->irn);
261 }
262
263 /**
264  * This O(n^2) checker checks if
265  *      two ifg-connected nodes have the same color
266  *      register constraint are satisfied
267  */
268 void co_check_allocation(copy_opt_t *co) {
269         ir_node **nodes, *n1, *n2;
270         int i, o;
271
272         obstack_init(&co->ob);
273         dom_tree_walk_irg(co->chordal_env->irg, co_collect_for_checker, NULL, co);
274         obstack_ptr_grow(&co->ob, NULL);
275
276         nodes = (ir_node **) obstack_finish(&co->ob);
277         for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
278                 assert(arch_reg_is_allocatable(co->chordal_env->arch_env, n1, arch_pos_make_out(0),
279                         arch_get_irn_register(co->chordal_env->arch_env, n1, 0)) && "Constraint does not hold");
280                 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o])
281                         if (nodes_interfere(co->chordal_env, n1, n2)
282           && get_irn_col(co, n1) == get_irn_col(co, n2)) {
283                                 DBG((dbg, 0, "Error in graph %s: %n %d  and  %n %d have the same color %d.\n", co->name, n1, get_irn_graph_nr(n1), n2, get_irn_graph_nr(n2), get_irn_col(co, n1)));
284                                 assert(0 && "Interfering values have the same color!");
285                         }
286         }
287         obstack_free(&co->ob, NULL);
288         DBG((dbg, 2, "The checker seems to be happy!\n"));
289 }