don't call be_spill_phis for phis of other reg classes
[libfirm] / ir / be / becopystat.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                19.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
11 #include <string.h>
12 #include <libcore/lc_timing.h>
13
14 #include "xmalloc.h"
15 #include "irgraph.h"
16 #include "irgwalk.h"
17 #include "irprog.h"
18 #include "iredges_t.h"
19 #include "phiclass.h"
20 #include "irnodeset.h"
21
22 #include "bechordal_t.h"
23 #include "beutil.h"
24 #include "becopyopt_t.h"
25 #include "becopystat.h"
26 #include "beirg_t.h"
27 #include "bemodule.h"
28
29 #define DEBUG_LVL SET_LEVEL_1
30 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
31
32 #define MAX_ARITY 20
33 #define MAX_CLS_SIZE 20
34 #define MAX_CLS_PHIS 20
35
36 /**
37  * For an explanation of these values see the code of copystat_dump_pretty
38  */
39 enum vals_t {
40         /* FROM HERE: PROBLEM CHARACTERIZATION */
41
42         I_ALL_NODES = 0,
43         I_BLOCKS,
44
45         /* phi nodes */
46         I_PHI_CNT,                      /* number of phi nodes */
47         I_PHI_ARG_CNT,          /* number of arguments of phis */
48         I_PHI_ARG_SELF,         /* number of arguments of phis being the phi itself */
49         I_PHI_ARG_CONST,        /* number of arguments of phis being consts */
50         I_PHI_ARG_PRED,         /* ... being defined in a cf-pred */
51         I_PHI_ARG_GLOB,         /* ... being defined elsewhere */
52         I_PHI_ARITY_S,
53         I_PHI_ARITY_E    = I_PHI_ARITY_S+MAX_ARITY,
54
55         /* copy nodes */
56         I_CPY_CNT,                      /* number of copynodes */
57
58         /* phi classes */
59         I_CLS_CNT,                      /* number of phi classes */
60         I_CLS_IF_FREE,          /* number of pc having no interference */
61         I_CLS_IF_MAX,           /* number of possible interferences in all classes */
62         I_CLS_IF_CNT,           /* number of actual interferences in all classes */
63         I_CLS_SIZE_S,
64         I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
65         I_CLS_PHIS_S,
66         I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
67
68         /* FROM HERE: RESULT VLAUES */
69         /* all of them are external set */
70
71         /* ilp values */
72         I_HEUR_TIME,            /* solving time in milli seconds */
73         I_ILP_TIME,                     /* solving time in milli seconds */
74         I_ILP_VARS,
75         I_ILP_CSTR,
76         I_ILP_ITER,                     /* number of simplex iterations */
77
78         /* copy instructions */
79         I_COPIES_MAX,           /* max possible costs of copies*/
80         I_COPIES_INIT,          /* number of copies in initial allocation */
81         I_COPIES_HEUR,          /* number of copies after heuristic */
82         I_COPIES_5SEC,          /* number of copies after ilp with max n sec */
83         I_COPIES_30SEC,         /* number of copies after ilp with max n sec */
84         I_COPIES_OPT,           /* number of copies after ilp */
85         I_COPIES_IF,            /* number of copies inevitable due to root-arg-interf */
86
87         ASIZE
88 };
89
90 /**
91  * Holds current values. Values are added till next copystat_reset
92  */
93 int curr_vals[ASIZE];
94
95 static ir_nodeset_t *all_phi_nodes;
96 static ir_nodeset_t *all_copy_nodes;
97 static ir_graph *last_irg;
98
99 void be_init_copystat(void) {
100         FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
101
102         all_phi_nodes  = ir_nodeset_new(64);
103         all_copy_nodes = ir_nodeset_new(64);
104         memset(curr_vals, 0, sizeof(curr_vals));
105 }
106 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat);
107
108 void be_quit_copystat(void) {
109         ir_nodeset_del(all_phi_nodes);
110         ir_nodeset_del(all_copy_nodes);
111 }
112 BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat);
113
114 void copystat_reset(void) {
115         be_quit_copystat();
116         be_init_copystat();
117 }
118
119 /**
120  * Collect general data
121  */
122 static void irg_stat_walker(ir_node *node, void *env) {
123         arch_env_t *arch_env = env;
124         curr_vals[I_ALL_NODES]++; /* count all nodes */
125
126         if (is_Block(node)) /* count all blocks */
127                 curr_vals[I_BLOCKS]++;
128
129         if (is_Reg_Phi(node)) /* collect phis */
130                 ir_nodeset_insert(all_phi_nodes, node);
131
132         if (is_Perm_Proj(arch_env, node))
133                 ir_nodeset_insert(all_copy_nodes, node);
134
135         /* TODO: Add 2-Addr-Code nodes */
136 }
137
138 static void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
139         irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
140         last_irg = irg;
141 }
142
143 /**
144  * @return 1 if the block at pos @p pos removed a critical edge
145  *                 0 else
146  */
147 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
148         const ir_edge_t *edge;
149         const ir_node *bl_at_pos, *bl_before;
150         assert(is_Block(bl));
151
152         /* Does bl have several predecessors ?*/
153         if (get_irn_arity(bl) <= 1)
154                 return 0;
155
156         /* Does the pred have exactly one predecessor */
157         bl_at_pos = get_irn_n(bl, pos);
158         if (get_irn_arity(bl_at_pos) != 1)
159                 return 0;
160
161         /* Does the pred of the pred have several successors */
162         bl_before = get_irn_n(bl_at_pos, 0);
163         edge = get_block_succ_first(bl_before);
164         return get_block_succ_next(bl_before, edge) ? 1 : 0;
165 }
166
167 /**
168  * Collect phi node data
169  */
170 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
171         int arity, i;
172         ir_node *phi_bl;
173         assert(is_Phi(phi));
174
175         /* count all phi phis */
176         curr_vals[I_PHI_CNT]++;
177
178         /* argument count */
179         arity = get_irn_arity(phi);
180         curr_vals[I_PHI_ARG_CNT] += arity;
181         if (arity > MAX_ARITY)
182                 curr_vals[I_PHI_ARITY_E]++;
183         else
184                 curr_vals[I_PHI_ARITY_S + arity]++;
185
186         phi_bl = get_nodes_block(phi);
187         /* type of argument {self, const, pred, glob} */
188         for (i = 0; i < arity; i++) {
189         ir_node *block_of_arg, *block_ith_pred;
190                 ir_node *arg = get_irn_n(phi, i);
191
192                 if (arg == phi) {
193                         curr_vals[I_PHI_ARG_SELF]++;
194                         continue;
195                 }
196
197                 if (iro_Const == get_irn_opcode(arg)) {
198                         curr_vals[I_PHI_ARG_CONST]++;
199                         continue;
200                 }
201
202                 /* get the pred block skipping blocks on critical edges */
203                 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
204                 if (was_edge_critical(phi_bl, i))
205                         block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
206
207                 block_of_arg = get_nodes_block(arg);
208                 if (block_of_arg == block_ith_pred) {
209                         curr_vals[I_PHI_ARG_PRED]++;
210                         continue;
211                 }
212
213                 curr_vals[I_PHI_ARG_GLOB]++;
214         }
215 }
216
217 /**
218  * Collect register-constrained node data
219  */
220 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
221         be_lv_t *lv = be_get_birg_liveness(chordal_env->birg);
222         curr_vals[I_CPY_CNT]++;
223         curr_vals[I_COPIES_MAX]++;
224         if (values_interfere(lv, root, get_Perm_src(root))) {
225                 curr_vals[I_COPIES_IF]++;
226                 assert(0 && "A Perm pair (in/out) should never interfere!");
227         }
228 }
229
230 /**
231  * Collect phi class data
232  */
233 static void stat_phi_class(be_chordal_env_t *chordal_env, ir_node **pc) {
234         int i, o, size, if_free, phis;
235         be_lv_t *lv = be_get_birg_liveness(chordal_env->birg);
236
237         /* phi class count */
238         curr_vals[I_CLS_CNT]++;
239
240         /* phi class size */
241         size = ARR_LEN(pc);
242         if (size > MAX_CLS_SIZE)
243                 curr_vals[I_CLS_SIZE_E]++;
244         else
245                 curr_vals[I_CLS_SIZE_S + size]++;
246
247         /* determine number of phis on this class */
248         for (phis = i = 0; i < size; ++i)
249                 if (is_Phi(pc[i]))
250                         phis++;
251
252         if (phis > MAX_CLS_PHIS)
253                 curr_vals[I_CLS_PHIS_E]++;
254         else
255                 curr_vals[I_CLS_PHIS_S + phis]++;
256
257         /* determine interference of phi class members */
258         curr_vals[I_CLS_IF_MAX] += size * (size - 1) / 2;
259         for (if_free = 1, i = 0; i < size - 1; ++i)
260                 for (o = i + 1; o < size; ++o)
261                         if (values_interfere(lv, pc[i], pc[o])) {
262                                 if_free = 0;
263                                 curr_vals[I_CLS_IF_CNT]++;
264                         }
265
266         /* Does this phi class have an inner interference? */
267         curr_vals[I_CLS_IF_FREE] += if_free;
268 }
269
270 static void copystat_collect_cls(be_chordal_env_t *cenv) {
271         ir_graph              *irg  = cenv->irg;
272         arch_env_t            *aenv = cenv->birg->main_env->arch_env;
273         ir_node               *n, **pc;
274         phi_classes_t         *pc_obj;
275         pset                  *all_phi_classes;
276         ir_nodeset_iterator_t iter;
277
278         copystat_reset();
279         copystat_collect_irg(irg, aenv);
280
281         /* compute the Phi classes of the collected Phis */
282         pc_obj          = phi_class_new_from_set(cenv->irg, all_phi_nodes, 0);
283         all_phi_classes = get_all_phi_classes(pc_obj);
284
285         foreach_ir_nodeset(all_phi_nodes, n, iter) {
286                 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
287                         stat_phi_node(cenv, n);
288         }
289
290         foreach_ir_nodeset(all_copy_nodes, n, iter) {
291                 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
292                         stat_copy_node(cenv, n);
293         }
294
295         foreach_pset(all_phi_classes, pc) {
296                 ir_node *member = pc[0];
297                 if (arch_get_irn_reg_class(aenv, member, -1) == cenv->cls)
298                         stat_phi_class(cenv, pc);
299         }
300
301         /* free the phi class object */
302         phi_class_free(pc_obj);
303 }
304
305 void copystat_add_max_costs(int costs) {
306         curr_vals[I_COPIES_MAX] += costs;
307 }
308 void copystat_add_inevit_costs(int costs) {
309         curr_vals[I_COPIES_IF] += costs;
310 }
311 void copystat_add_init_costs(int costs) {
312         curr_vals[I_COPIES_INIT] += costs;
313 }
314 void copystat_add_heur_costs(int costs) {
315         curr_vals[I_COPIES_HEUR] += costs;
316 }
317 void copystat_add_ilp_5_sec_costs(int costs) {
318         curr_vals[I_COPIES_5SEC] += costs;
319 }
320 void copystat_add_ilp_30_sec_costs(int costs) {
321         curr_vals[I_COPIES_30SEC] += costs;
322 }
323 void copystat_add_opt_costs(int costs) {
324         curr_vals[I_COPIES_OPT] += costs;
325 }
326 void copystat_add_heur_time(int time) {
327         curr_vals[I_HEUR_TIME] += time;
328 }
329
330 #ifdef WITH_ILP
331
332 void copystat_add_ilp_time(int time) {
333         curr_vals[I_ILP_TIME] += time;
334 }
335 void copystat_add_ilp_vars(int vars) {
336         curr_vals[I_ILP_VARS] += vars;
337 }
338 void copystat_add_ilp_csts(int csts) {
339         curr_vals[I_ILP_CSTR] += csts;
340 }
341 void copystat_add_ilp_iter(int iters) {
342         curr_vals[I_ILP_ITER] += iters;
343 }
344
345 #endif /* WITH_ILP */
346
347 void copystat_dump(ir_graph *irg) {
348         int i;
349         char buf[1024];
350         FILE *out;
351
352         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
353         buf[sizeof(buf) - 1] = '\0';
354         out = ffopen(buf, "stat", "wt");
355
356         fprintf(out, "%d\n", ASIZE);
357         for (i = 0; i < ASIZE; i++) {
358 #if 0
359                 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
360                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
361                 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
362                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
363                 else
364 #endif
365                         fprintf(out, "%i\n", curr_vals[i]);
366         }
367
368         fclose(out);
369 }
370
371 void copystat_dump_pretty(ir_graph *irg) {
372         int i;
373         char buf[1024];
374         FILE *out;
375
376         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
377         buf[sizeof(buf) - 1] = '\0';
378         out = ffopen(buf, "pstat", "wt");
379
380         fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
381         fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
382         fprintf(out, "CopyIrn   %4d\n", curr_vals[I_CPY_CNT]);
383
384         fprintf(out, "\nPhis      %4d\n", curr_vals[I_PHI_CNT]);
385         fprintf(out, "... argument types\n");
386         fprintf(out, " Total      %4d\n", curr_vals[I_PHI_ARG_CNT]);
387         fprintf(out, " Self       %4d\n", curr_vals[I_PHI_ARG_SELF]);
388         fprintf(out, " Constants  %4d\n", curr_vals[I_PHI_ARG_CONST]);
389         fprintf(out, " CF-Pred    %4d\n", curr_vals[I_PHI_ARG_PRED]);
390         fprintf(out, " Others     %4d\n", curr_vals[I_PHI_ARG_GLOB]);
391         fprintf(out, "... arities\n");
392         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
393                 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
394
395         fprintf(out, "\nPhi classes   %4d\n", curr_vals[I_CLS_CNT]);
396         fprintf(out, " compl. free  %4d\n", curr_vals[I_CLS_IF_FREE]);
397         fprintf(out, " inner intf.  %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
398         fprintf(out, "... sizes\n");
399         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
400                 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
401         fprintf(out, "... contained phis\n");
402         for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
403                 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
404
405         fprintf(out, "\nILP stat\n");
406         fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
407         fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
408
409         fprintf(out, "\nCopy stat\n");
410         fprintf(out, " Max  %4d\n", curr_vals[I_COPIES_MAX]);
411         fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
412         fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
413         fprintf(out, " Opt  %4d\n", curr_vals[I_COPIES_OPT]);
414         fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
415
416         fclose(out);
417 }
418
419 /**
420  * Helpers for saving and restoring colors of nodes.
421  * Used to get dependable and comparable benchmark results.
422  */
423 typedef struct color_saver {
424         arch_env_t *arch_env;
425         be_chordal_env_t *chordal_env;
426         pmap *saved_colors;
427         int flag; /* 0 save, 1 load */
428 } color_save_t;
429
430 static void save_load(ir_node *irn, void *env) {
431         color_save_t *saver = env;
432         if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, -1)) {
433                 if (saver->flag == 0) { /* save */
434                         const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn);
435                         pmap_insert(saver->saved_colors, irn, (void *) reg);
436                 } else { /*load */
437                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
438                         arch_set_irn_register(saver->arch_env, irn, reg);
439                 }
440         }
441 }
442
443 static void save_colors(color_save_t *color_saver) {
444         color_saver->flag = 0;
445         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
446 }
447
448 #ifdef WITH_ILP
449 static void load_colors(color_save_t *color_saver) {
450         color_saver->flag = 1;
451         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
452 }
453 #endif
454
455 /**
456  * Main compare routine
457  */
458 void co_compare_solvers(be_chordal_env_t *chordal_env) {
459         copy_opt_t    *co;
460         lc_timer_t    *timer;
461         color_save_t  saver;
462         int costs_inevit, costs_init, costs_solved, lower_bound;
463
464         copystat_collect_cls(chordal_env);
465
466         co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
467         co_build_ou_structure(co);
468         co_build_graph_structure(co);
469         DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
470
471         /* save colors */
472         saver.arch_env     = chordal_env->birg->main_env->arch_env;
473         saver.chordal_env  = chordal_env;
474         saver.saved_colors = pmap_create();
475         save_colors(&saver);
476
477         /* initial values */
478         costs_inevit = co_get_inevit_copy_costs(co);
479         lower_bound  = co_get_lower_bound(co);
480         costs_init   = co_get_copy_costs(co);
481
482         DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", costs_inevit));
483         DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
484         DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
485
486         copystat_add_inevit_costs(costs_inevit);
487         copystat_add_init_costs(costs_init);
488         copystat_add_max_costs(co_get_max_copy_costs(co));
489
490         /* heuristic 1 (Daniel Grund) */
491         timer = lc_timer_register("heur1", NULL);
492         lc_timer_reset_and_start(timer);
493
494         co_solve_heuristic(co);
495
496         lc_timer_stop(timer);
497
498         costs_solved = co_get_copy_costs(co);
499         DBG((dbg, LEVEL_1, "HEUR1 costs: %3d\n", costs_solved));
500         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
501         copystat_add_heur_costs(costs_solved);
502         assert(lower_bound <= costs_solved);
503
504         /* heuristic 2 (Sebastian Hack) */
505         timer = lc_timer_register("heur2", NULL);
506         lc_timer_reset_and_start(timer);
507
508         co_solve_heuristic_new(co);
509
510         lc_timer_stop(timer);
511
512         costs_solved = co_get_copy_costs(co);
513         DBG((dbg, LEVEL_1, "HEUR2 costs: %3d\n", costs_solved));
514         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
515         copystat_add_heur_costs(costs_solved);
516         assert(lower_bound <= costs_solved);
517
518         /* Park & Moon register coalescing (Kimon Hoffmann) */
519         timer = lc_timer_register("park", NULL);
520         lc_timer_reset_and_start(timer);
521
522         co_solve_park_moon(co);
523
524         lc_timer_stop(timer);
525
526         costs_solved = co_get_copy_costs(co);
527         DBG((dbg, LEVEL_1, "Park/Moon costs: %3d\n", costs_solved));
528         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
529         copystat_add_heur_costs(costs_solved);
530         assert(lower_bound <= costs_solved);
531
532
533 #ifdef WITH_ILP
534
535         /* ILP 1 is not yet implemented, so it makes no sense to compare */
536 #if 0
537         load_colors(&saver);
538
539         co_solve_ilp1(co, 60.0);
540
541         costs_solved = co_get_copy_costs(co);
542         DBG((dbg, LEVEL_1, "ILP1 costs: %3d\n", costs_solved));
543         copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
544         assert(lower_bound <= costs_solved);
545 #endif /* 0 */
546
547         /* ILP 2 */
548         load_colors(&saver);
549
550         co_solve_ilp2(co);
551
552         costs_solved = co_get_copy_costs(co);
553         DBG((dbg, LEVEL_1, "ILP2 costs: %3d\n", costs_solved));
554         copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
555         assert(lower_bound <= costs_solved);
556
557 #endif /* WITH_ILP */
558
559         /* free memory for statistic structures */
560         pmap_destroy(saver.saved_colors);
561         co_free_graph_structure(co);
562         co_free_ou_structure(co);
563         free_copy_opt(co);
564 }