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