Added call to eliminate_phi_interferences. Enabled phi-destruction.
[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 #define DEBUG_LVL 0 //SET_LEVEL_1
26 static firm_dbg_module_t *dbg = NULL;
27
28 #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)
29
30 #define MIN(a,b) ((a<b)?(a):(b))
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                 bitset_foreach(curr, pos)
54                         irns[pos] = ou->nodes[1+pos];
55                 for(i=0; i<max; ++i)
56                         for(o=i+1; o<max; ++o) /* !!!!! difference to qnode_max_ind_set(): NOT o=i */
57                                 if (nodes_interfere(ou->co->chordal_env, irns[i], irns[o])) {
58                                         is_stable_set = 0;
59                                         break;
60                                 }
61
62                 if (is_stable_set) {
63                         /* calc current weigth */
64                         curr_weight = 0;
65                         bitset_foreach(curr, pos)
66                                 curr_weight += ou->costs[1+pos];
67
68                         /* any better ? */
69                         if (curr_weight > best_weight)
70                                 best_weight = curr_weight;
71                 }
72
73                 bitset_minus1(curr);
74         }
75         return best_weight;
76 }
77
78 /**
79  * Builds an optimization unit for a given optimizable irn (root).
80  * This opt-unit is inserted in the main structure co.
81  * If an arg of root itself is optimizable process this arg before with a
82  * recursive call. For handling this situation and loops co->root is used
83  * to remember all roots.
84  */
85 static void co_append_unit(copy_opt_t *co, ir_node *root) {
86         int i, arity;
87         unit_t *unit;
88         struct list_head *tmp;
89
90         DBG((dbg, LEVEL_1, "\t  Root: %n %N\n", root, root));
91         /* check if we encountered this root earlier */
92         if (pset_find_ptr(co->roots, root))
93                 return;
94         pset_insert_ptr(co->roots, root);
95
96         assert(is_curr_reg_class(root) && "node is in wrong register class!");
97
98         /* init unit */
99         arity = get_irn_arity(root);
100         unit = xcalloc(1, sizeof(*unit));
101         unit->co = co;
102         unit->node_count = 1;
103         unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
104         unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
105         unit->nodes[0] = root;
106         unit->complete_costs = 0;
107         unit->avg_costs = 0;
108         INIT_LIST_HEAD(&unit->queue);
109
110         /* check all args */
111         if (is_Phi(root)) {
112                 for (i=0; i<arity; ++i) {
113                         ir_node *arg = get_irn_n(root, i);
114                         assert(is_curr_reg_class(arg) && "Argument not in same register class.");
115                         if (arg != root) {
116                                 int o, arg_pos = 0;
117                                 if (nodes_interfere(co->chordal_env, root, arg))
118                                         assert(0 && "root and arg interfere");
119                                 //TODO do not insert duplicate args
120                                 DBG((dbg, LEVEL_1, "\t   Member: %n %N\n", arg, arg));
121
122                                 /* check if arg has occurred at a prior position in the arg/list */
123                                 for (o=0; o<unit->node_count; ++o)
124                                         if (unit->nodes[o] == arg) {
125                                                 arg_pos = o;
126                                                 break;
127                                         }
128
129                                 if (!arg_pos) { /* a new argument */
130                                         /* TODO Think about the next 2 lines. (inserting in arg-order) */
131                                         if (is_optimizable(co->chordal_env->arch_env, arg))
132                                                 co_append_unit(co, arg);
133                                         /* insert node, set costs */
134                                         unit->nodes[unit->node_count] = arg;
135                                         unit->costs[unit->node_count] = co->get_costs(root, arg, i);
136                                         unit->node_count++;
137                                 } else { /* arg has occured before in same phi */
138                                         /* increase costs for existing arg */
139                                         unit->costs[arg_pos] = co->get_costs(root, arg, i);
140                                 }
141                         }
142                 }
143                 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
144                 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
145         } else if (is_Copy(co->chordal_env->arch_env, root)) {
146                 assert(!nodes_interfere(co->chordal_env, root, get_Copy_src(root)));
147                 unit->nodes[1] = get_Copy_src(root);
148                 unit->costs[1] = co->get_costs(root, unit->nodes[1], -1);
149                 unit->node_count = 2;
150                 unit->nodes = xrealloc(unit->nodes, 2 * sizeof(*unit->nodes));
151                 unit->costs = xrealloc(unit->costs, 2 * sizeof(*unit->costs));
152         } else
153                 assert(0 && "This is not an optimizable node!");
154         /* TODO add ou's for 2-addr-code instructions */
155
156
157         for(i=1; i<unit->node_count; ++i)
158                 unit->complete_costs += unit->costs[i];
159
160         assert(unit->node_count > 1);
161         unit->avg_costs = (100 * unit->complete_costs) / (unit->node_count-1);
162
163         /* insert according to average costs */
164         tmp = &co->units;
165         while (tmp->next != &co->units && list_entry_units(tmp->next)->avg_costs > unit->avg_costs)
166                 tmp = tmp->next;
167         list_add(&unit->units, tmp);
168
169         /* Init ifg_mis_size to node_count. So get_lower_bound returns correct results. */
170         unit->minimal_costs = unit->complete_costs - ou_max_ind_set_costs(unit);
171 }
172
173 static void co_collect_in_block(ir_node *block, void *env) {
174         copy_opt_t *co = env;
175         struct list_head *head = get_block_border_head(co->chordal_env, block);
176         border_t *curr;
177
178         list_for_each_entry_reverse(border_t, curr, head, list)
179                 if (curr->is_def && curr->is_real && is_optimizable(co->chordal_env->arch_env, curr->irn))
180                         co_append_unit(co, curr->irn);
181 }
182
183 static void co_collect_units(copy_opt_t *co) {
184         DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
185         co->roots = pset_new_ptr(64);
186         dom_tree_walk_irg(co->chordal_env->irg, co_collect_in_block, NULL, co);
187         del_pset(co->roots);
188 }
189
190 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
191         const char *s1, *s2, *s3;
192         int len;
193         copy_opt_t *co;
194
195         dbg = firm_dbg_register("ir.be.copyopt");
196         firm_dbg_set_mask(dbg, DEBUG_LVL);
197
198         co = xcalloc(1, sizeof(*co));
199         co->chordal_env = chordal_env;
200         co->get_costs = get_costs;
201
202         s1 = get_irp_prog_name();
203         s2 = get_entity_name(get_irg_entity(co->chordal_env->irg));
204         s3 = chordal_env->cls->name;
205         len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
206         co->name = xmalloc(len);
207         snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
208         if (!strcmp(co->name, DEBUG_IRG))
209                 firm_dbg_set_mask(dbg, DEBUG_LVL_CO);
210         else
211                 firm_dbg_set_mask(dbg, DEBUG_LVL);
212
213         INIT_LIST_HEAD(&co->units);
214         co_collect_units(co);
215         return co;
216 }
217
218 void free_copy_opt(copy_opt_t *co) {
219         unit_t *curr, *tmp;
220         xfree(co->name);
221         list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
222                 xfree(curr->nodes);
223                 xfree(curr);
224         }
225 }
226
227 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
228         int i, max;
229         for(i=0, max=get_irn_n_outs(irn); i<max; ++i) {
230                 ir_node *n = get_irn_out(irn, i);
231                 if ((is_Phi(n) || is_Perm(co->chordal_env->arch_env, n)) && (irn == n || !nodes_interfere(co->chordal_env, irn, n)))
232                         return 1;
233         }
234         return 0;
235 }
236
237 int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
238         int cost = 0;
239         ir_loop *loop;
240         ir_node *root_block = get_nodes_block(root);
241
242         assert(pos==-1 || is_Phi(root));
243         if (pos == -1) {
244                 /* a perm places the copy in the same block as it resides */
245                 loop = get_irn_loop(root_block);
246         } else {
247                 /* for phis the copies are placed in the corresponding pred-block */
248                 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
249         }
250         if (loop)
251                 cost = 2*get_loop_depth(loop);
252         return cost+1;
253 }
254
255 int get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
256         return 1;
257 }
258
259 int co_get_copy_costs(const copy_opt_t *co) {
260         int i, res = 0;
261         unit_t *curr;
262         list_for_each_entry(unit_t, curr, &co->units, units) {
263                 int root_col = get_irn_col(co, curr->nodes[0]);
264                 for (i=1; i<curr->node_count; ++i)
265                         if (root_col != get_irn_col(co, curr->nodes[i])) {
266                                 DBG((dbg, LEVEL_1, "  %n %N\n", curr->nodes[i], curr->nodes[i]));
267                                 res += curr->costs[i];
268                         }
269         }
270         return res;
271 }
272
273 int co_get_lower_bound(const copy_opt_t *co) {
274         int res = 0;
275         unit_t *curr;
276         list_for_each_entry(unit_t, curr, &co->units, units)
277                 res += curr->minimal_costs;
278         return res;
279 }