Bugfixes
[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 <libcore/lc_timing.h>
18
19 #include "xmalloc.h"
20 #include "debug.h"
21 #include "pmap.h"
22 #include "irgraph.h"
23 #include "irgwalk.h"
24 #include "irprog.h"
25 #include "irloop_t.h"
26 #include "iredges_t.h"
27 #include "phiclass.h"
28
29 #include "bearch.h"
30 #include "beutil.h"
31 #include "becopyopt_t.h"
32 #include "becopystat.h"
33
34
35
36 static firm_dbg_module_t *dbg = NULL;
37
38 /**
39  * Determines a maximum weighted independent set with respect to
40  * the interference and conflict edges of all nodes in a qnode.
41  */
42 static int ou_max_ind_set_costs(unit_t *ou) {
43         be_chordal_env_t *chordal_env = ou->co->cenv;
44         ir_node **safe, **unsafe;
45         int i, o, safe_count, safe_costs, unsafe_count, *unsafe_costs;
46         bitset_t *curr;
47         int max, pos, curr_weight, best_weight = 0;
48
49         /* assign the nodes into two groups.
50          * safe: node has no interference, hence it is in every max stable set.
51          * unsafe: node has an interference
52          */
53         safe = alloca((ou->node_count-1) * sizeof(*safe));
54         safe_costs = 0;
55         safe_count = 0;
56         unsafe = alloca((ou->node_count-1) * sizeof(*unsafe));
57         unsafe_costs = alloca((ou->node_count-1) * sizeof(*unsafe_costs));
58         unsafe_count = 0;
59         for(i=1; i<ou->node_count; ++i) {
60                 int is_safe = 1;
61                 for(o=1; o<ou->node_count; ++o) {
62                         if (i==o)
63                                 continue;
64                         if (nodes_interfere(chordal_env, ou->nodes[i], ou->nodes[o])) {
65                                 unsafe_costs[unsafe_count] = ou->costs[i];
66                                 unsafe[unsafe_count] = ou->nodes[i];
67                                 ++unsafe_count;
68                                 is_safe = 0;
69                                 break;
70                         }
71                 }
72                 if (is_safe) {
73                         safe_costs += ou->costs[i];
74                         safe[safe_count++] = ou->nodes[i];
75                 }
76         }
77
78
79         /* now compute the best set out of the unsafe nodes*/
80         if (unsafe_count > MIS_HEUR_TRIGGER) {
81                 bitset_t *best = bitset_alloca(unsafe_count);
82                 /* Heuristik: Greedy trial and error form index 0 to unsafe_count-1 */
83                 for (i=0; i<unsafe_count; ++i) {
84                         bitset_set(best, i);
85                         /* check if it is a stable set */
86                         for (o=bitset_next_set(best, 0); o!=-1 && o<i; o=bitset_next_set(best, o+1))
87                                 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o])) {
88                                         bitset_clear(best, i); /* clear the bit and try next one */
89                                         break;
90                                 }
91                 }
92                 /* compute the weight */
93                 bitset_foreach(best, pos)
94                         best_weight += unsafe_costs[pos];
95         } else {
96                 /* Exact Algorithm: Brute force */
97                 curr = bitset_alloca(unsafe_count);
98                 bitset_set_all(curr);
99                 while ((max = bitset_popcnt(curr)) != 0) {
100                         /* check if curr is a stable set */
101                         for (i=bitset_next_set(curr, 0); i!=-1; i=bitset_next_set(curr, i+1))
102                                 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) */
103                                                 if (nodes_interfere(chordal_env, unsafe[i], unsafe[o]))
104                                                         goto no_stable_set;
105
106                         /* if we arrive here, we have a stable set */
107                         /* compute the weigth of the stable set*/
108                         curr_weight = 0;
109                         bitset_foreach(curr, pos)
110                                 curr_weight += unsafe_costs[pos];
111
112                         /* any better ? */
113                         if (curr_weight > best_weight) {
114                                 best_weight = curr_weight;
115                         }
116
117         no_stable_set:
118                         bitset_minus1(curr);
119                 }
120         }
121
122         return safe_costs+best_weight;
123 }
124
125 static void co_collect_units(ir_node *irn, void *env) {
126         copy_opt_t *co = env;
127         unit_t *unit;
128         arch_register_req_t req;
129
130         if (!is_curr_reg_class(co, irn))
131                 return;
132         if (!co_is_optimizable(co->aenv, irn, &req))
133                 return;
134
135         /* Init a new unit */
136         unit = xcalloc(1, sizeof(*unit));
137         unit->co = co;
138         unit->node_count = 1;
139         INIT_LIST_HEAD(&unit->queue);
140
141         /* Phi with some/all of its arguments */
142         if (is_Reg_Phi(irn)) {
143                 int i, arity;
144
145                 /* init */
146                 arity = get_irn_arity(irn);
147                 unit->nodes = xmalloc((arity+1) * sizeof(*unit->nodes));
148                 unit->costs = xmalloc((arity+1) * sizeof(*unit->costs));
149                 unit->nodes[0] = irn;
150
151                 /* fill */
152                 for (i=0; i<arity; ++i) {
153                         int o, arg_pos;
154                         ir_node *arg = get_irn_n(irn, i);
155
156                         assert(is_curr_reg_class(co, arg) && "Argument not in same register class.");
157                         if (arg == irn)
158                                 continue;
159                         if (nodes_interfere(co->cenv, irn, arg)) {
160                                 unit->inevitable_costs += co->get_costs(irn, arg, i);
161                                 continue;
162                         }
163
164                         /* Else insert the argument of the phi to the members of this ou */
165                         DBG((dbg, LEVEL_1, "\t   Member: %+F\n", arg));
166
167                         /* Check if arg has occurred at a prior position in the arg/list */
168                         arg_pos = 0;
169                         for (o=0; o<unit->node_count; ++o)
170                                 if (unit->nodes[o] == arg) {
171                                         arg_pos = o;
172                                         break;
173                                 }
174
175                         if (!arg_pos) { /* a new argument */
176                                 /* insert node, set costs */
177                                 unit->nodes[unit->node_count] = arg;
178                                 unit->costs[unit->node_count] = co->get_costs(irn, arg, i);
179                                 unit->node_count++;
180                         } else { /* arg has occured before in same phi */
181                                 /* increase costs for existing arg */
182                                 unit->costs[arg_pos] += co->get_costs(irn, arg, i);
183                         }
184                 }
185                 unit->nodes = xrealloc(unit->nodes, unit->node_count * sizeof(*unit->nodes));
186                 unit->costs = xrealloc(unit->costs, unit->node_count * sizeof(*unit->costs));
187         } else
188
189         /* Proj of a perm with corresponding arg */
190         if (is_Perm_Proj(co->aenv, irn)) {
191                 assert(!nodes_interfere(co->cenv, irn, get_Copy_src(irn)));
192                 unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
193                 unit->costs = xmalloc(2 * sizeof(*unit->costs));
194                 unit->node_count = 2;
195                 unit->nodes[0] = irn;
196                 unit->nodes[1] = get_Copy_src(irn);
197                 unit->costs[1] = co->get_costs(irn, unit->nodes[1], -1);
198         } else
199
200         /* Src == Tgt of a 2-addr-code instruction */
201         if (is_2addr_code(co->aenv, irn, &req)) {
202                 ir_node *other = req.other_same;
203                 if (!nodes_interfere(co->cenv, irn, other)) {
204                         unit->nodes = xmalloc(2 * sizeof(*unit->nodes));
205                         unit->costs = xmalloc(2 * sizeof(*unit->costs));
206                         unit->node_count = 2;
207                         unit->nodes[0] = irn;
208                         unit->nodes[1] = other;
209                         unit->costs[1] = co->get_costs(irn, other, -120480);
210                 }
211         } else
212                 assert(0 && "This is not an optimizable node!");
213
214         /* Insert the new unit at a position according to its costs */
215         if (unit->node_count > 1) {
216                 int i;
217                 struct list_head *tmp;
218
219                 /* Determine the maximum costs this unit can cause: all_nodes_cost */
220                 for(i=1; i<unit->node_count; ++i) {
221                         unit->sort_key = MAX(unit->sort_key, unit->costs[i]);
222                         unit->all_nodes_costs += unit->costs[i];
223                 }
224
225                 /* Determine the minimal costs this unit will cause: min_nodes_costs */
226                 unit->min_nodes_costs += unit->all_nodes_costs - ou_max_ind_set_costs(unit);
227
228                 /* Insert the new ou according to its sort_key */
229                 tmp = &co->units;
230                 while (tmp->next != &co->units && list_entry_units(tmp->next)->sort_key > unit->sort_key)
231                         tmp = tmp->next;
232                 list_add(&unit->units, tmp);
233         } else {
234                 free(unit);
235         }
236 }
237
238 void be_copy_opt_init(void) {
239         dbg = firm_dbg_register("ir.be.copyoptmain");
240 }
241
242 copy_opt_t *new_copy_opt(be_chordal_env_t *chordal_env, int (*get_costs)(ir_node*, ir_node*, int)) {
243         const char *s1, *s2, *s3;
244         int len;
245         copy_opt_t *co;
246
247         dbg = firm_dbg_register("ir.be.copyopt");
248
249         co = xcalloc(1, sizeof(*co));
250         co->cenv = chordal_env;
251         co->aenv = chordal_env->birg->main_env->arch_env;
252         co->irg = chordal_env->irg;
253         co->cls = chordal_env->cls;
254         co->get_costs = get_costs;
255
256         s1 = get_irp_prog_name();
257         s2 = get_entity_name(get_irg_entity(co->irg));
258         s3 = chordal_env->cls->name;
259         len = strlen(s1) + strlen(s2) + strlen(s3) + 5;
260         co->name = xmalloc(len);
261         snprintf(co->name, len, "%s__%s__%s", s1, s2, s3);
262
263         DBG((dbg, LEVEL_1, "\tCollecting optimization units\n"));
264         INIT_LIST_HEAD(&co->units);
265         irg_walk_graph(co->irg, co_collect_units, NULL, co);
266         return co;
267 }
268
269 void free_copy_opt(copy_opt_t *co) {
270         unit_t *curr, *tmp;
271         xfree(co->name);
272         list_for_each_entry_safe(unit_t, curr, tmp, &co->units, units) {
273                 xfree(curr->nodes);
274                 xfree(curr->costs);
275                 xfree(curr);
276         }
277 }
278
279 int is_optimizable_arg(const copy_opt_t *co, ir_node *irn) {
280         const ir_edge_t *edge;
281
282         if (arch_irn_is_ignore(co->aenv, irn))
283                 return 0;
284
285         foreach_out_edge(irn, edge) {
286                 ir_node *n = edge->src;
287
288                 if (!nodes_interfere(co->cenv, irn, n) || irn == n) {
289                         arch_register_req_t req;
290                         arch_get_register_req(co->aenv, &req, n, -1);
291
292                         if(is_Reg_Phi(n) ||
293                            is_Perm(co->aenv, n) ||
294                            (arch_register_req_is(&req, should_be_same) && req.other_same == irn)
295                           )
296                                 return 1;
297                 }
298         }
299
300         return 0;
301 }
302
303 int get_costs_loop_depth(ir_node *root, ir_node* arg, int pos) {
304         int cost = 0;
305         ir_loop *loop;
306         ir_node *root_block = get_nodes_block(root);
307
308         if (is_Phi(root)) {
309                 /* for phis the copies are placed in the corresponding pred-block */
310                 loop = get_irn_loop(get_Block_cfgpred_block(root_block, pos));
311         } else {
312                 /* a perm places the copy in the same block as it resides */
313                 loop = get_irn_loop(root_block);
314         }
315         if (loop) {
316                 int d = get_loop_depth(loop);
317                 cost = d*d;
318         }
319         return cost+1;
320 }
321
322 int get_costs_all_one(ir_node *root, ir_node* arg, int pos) {
323         return 1;
324 }
325
326 int co_get_max_copy_costs(const copy_opt_t *co) {
327         int i, res = 0;
328         unit_t *curr;
329
330         list_for_each_entry(unit_t, curr, &co->units, units) {
331                 res += curr->inevitable_costs;
332                 for (i=1; i<curr->node_count; ++i)
333                         res += curr->costs[i];
334         }
335         return res;
336 }
337
338 int co_get_inevit_copy_costs(const copy_opt_t *co) {
339         int res = 0;
340         unit_t *curr;
341
342         list_for_each_entry(unit_t, curr, &co->units, units)
343                 res += curr->inevitable_costs;
344         return res;
345 }
346
347 int co_get_copy_costs(const copy_opt_t *co) {
348         int i, res = 0;
349         unit_t *curr;
350
351         list_for_each_entry(unit_t, curr, &co->units, units) {
352                 int root_col = get_irn_col(co, curr->nodes[0]);
353                 DBG((dbg, LEVEL_1, "  %3d costs for root %+F color %d\n", curr->inevitable_costs, curr->nodes[0], root_col));
354                 res += curr->inevitable_costs;
355                 for (i=1; i<curr->node_count; ++i) {
356                         int arg_col = get_irn_col(co, curr->nodes[i]);
357                         if (root_col != arg_col) {
358                                 DBG((dbg, LEVEL_1, "  %3d for arg %+F color %d\n", curr->costs[i], curr->nodes[i], arg_col));
359                                 res += curr->costs[i];
360                         }
361                 }
362         }
363         return res;
364 }
365
366 int co_get_lower_bound(const copy_opt_t *co) {
367         int res = 0;
368         unit_t *curr;
369         list_for_each_entry(unit_t, curr, &co->units, units)
370                 res += curr->inevitable_costs + curr->min_nodes_costs;
371         return res;
372 }
373
374
375
376 #define DO_HEUR
377 #undef DO_CLASSES
378 #undef DO_ILP
379
380
381 /**
382  * Helpers for saving and restoring colors of nodes.
383  * Used to get dependable and comparable benchmark results.
384  */
385 #if (defined(DO_HEUR) && defined(DO_BETTER)) || (defined(DO_HEUR) && defined(DO_ILP)) || (defined(DO_BETTER) && defined(DO_ILP))
386
387 typedef struct color_saver {
388         arch_env_t *arch_env;
389         be_chordal_env_t *chordal_env;
390         pmap *saved_colors;
391         int flag; /* 0 save, 1 load */
392 } color_save_t;
393
394 static void save_load(ir_node *irn, void *env) {
395         color_save_t *saver = env;
396         if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, -1)) {
397                 if (saver->flag == 0) { /* save */
398                         const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn);
399                         pmap_insert(saver->saved_colors, irn, (void *) reg);
400                 } else { /*load */
401                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
402                         arch_set_irn_register(saver->arch_env, irn, reg);
403                 }
404         }
405 }
406
407 static void save_colors(color_save_t *color_saver) {
408         color_saver->flag = 0;
409         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
410 }
411
412 static void load_colors(color_save_t *color_saver) {
413         color_saver->flag = 1;
414         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
415 }
416
417 #endif /* Need save/load stuff */
418
419
420
421 void co_compare_solvers(be_chordal_env_t *chordal_env) {
422         copy_opt_t *co;
423
424 #ifdef DO_STAT
425         lc_timer_t *timer;
426         color_save_t saver;
427         int costs, costs_inevit, costs_init, costs_heur, costs_classes, costs_ilp, lower_bound;
428 #endif
429
430         co = new_copy_opt(chordal_env, get_costs_loop_depth);
431         DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
432         phi_class_compute(chordal_env->irg);
433
434
435 #ifdef DO_STAT
436 #if (defined(DO_HEUR) && defined(DO_BETTER)) || (defined(DO_HEUR) && defined(DO_ILP)) || (defined(DO_BETTER) && defined(DO_ILP))
437                 saver.arch_env = chordal_env->main_env->arch_env;
438                 saver.chordal_env = chordal_env;
439                 saver.saved_colors = pmap_create();
440                 save_colors(&saver);
441 #endif
442
443                 costs_inevit = co_get_inevit_copy_costs(co);
444                 lower_bound  = co_get_lower_bound(co);
445                 costs_init   = co_get_copy_costs(co);
446
447                 DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", costs_inevit));
448                 DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
449                 DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
450
451                 copystat_add_inevit_costs(costs_inevit);
452                 copystat_add_init_costs(costs_init);
453                 copystat_add_max_costs(co_get_max_copy_costs(co));
454 #endif
455
456
457 #ifdef DO_HEUR
458 #ifdef DO_STAT
459         timer = lc_timer_register("heur", NULL);
460         lc_timer_reset_and_start(timer);
461 #endif
462
463         co_solve_heuristic(co);
464
465 #ifdef DO_STAT
466         lc_timer_stop(timer);
467         costs_heur = co_get_copy_costs(co);
468         DBG((dbg, LEVEL_1, "Heur costs: %3d\n", costs_heur));
469         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
470         copystat_add_heur_costs(costs_heur);
471         assert(lower_bound <= costs_heur);
472 #endif
473 #endif /* DO_HEUR */
474
475
476
477 #ifdef DO_CLASSES
478 #ifdef DO_STAT
479 #ifdef DO_HEUR
480         load_colors(&saver);
481 #endif
482         timer = lc_timer_register("classes", NULL);
483         lc_timer_reset_and_start(timer);
484 #endif
485
486         co_classes_opt(co);
487
488 #ifdef DO_STAT
489         lc_timer_stop(timer);
490         costs_classes = co_get_copy_costs(co);
491         DBG((dbg, LEVEL_1, "Classes costs: %3d\n", costs_classes));
492         copystat_add_classes_time(lc_timer_elapsed_msec(timer));
493         copystat_add_classes_costs(costs_heur);
494         assert(lower_bound <= costs_classes);
495 #endif
496 #endif /* DO_CLASSES */
497
498
499
500 #ifdef DO_ILP
501 #ifdef DO_STAT
502 #if defined(DO_HEUR) || defined(DO_CLASSES)
503         load_colors(&saver);
504 #endif
505 #endif
506
507         co_solve_ilp1(co, 60.0);
508
509 #ifdef DO_STAT
510         costs_ilp = co_get_copy_costs(co);
511         DBG((dbg, LEVEL_1, "Opt  costs: %3d\n", costs_ilp));
512         copystat_add_opt_costs(costs_ilp);
513         assert(lower_bound <= costs_ilp);
514 #endif
515 #endif /* DO_ILP */
516
517
518 #ifdef DO_STAT
519 #if (defined(DO_HEUR) && defined(DO_BETTER)) || (defined(DO_HEUR) && defined(DO_ILP)) || (defined(DO_BETTER) && defined(DO_ILP))
520         pmap_destroy(saver.saved_colors);
521 #endif
522 #endif
523         free_copy_opt(co);
524 }