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