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