4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
12 #include <libcore/lc_timing.h>
18 #include "iredges_t.h"
19 #include "phiclass_t.h"
20 #include "bechordal_t.h"
22 #include "becopyopt_t.h"
23 #include "becopystat.h"
26 #define DEBUG_LVL SET_LEVEL_1
27 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
30 #define MAX_CLS_SIZE 20
31 #define MAX_CLS_PHIS 20
34 * For an explanation of these values see the code of copystat_dump_pretty
37 /* FROM HERE: PROBLEM CHARACTERIZATION */
43 I_PHI_CNT, /* number of phi nodes */
44 I_PHI_ARG_CNT, /* number of arguments of phis */
45 I_PHI_ARG_SELF, /* number of arguments of phis being the phi itself */
46 I_PHI_ARG_CONST, /* number of arguments of phis being consts */
47 I_PHI_ARG_PRED, /* ... being defined in a cf-pred */
48 I_PHI_ARG_GLOB, /* ... being defined elsewhere */
50 I_PHI_ARITY_E = I_PHI_ARITY_S+MAX_ARITY,
53 I_CPY_CNT, /* number of copynodes */
56 I_CLS_CNT, /* number of phi classes */
57 I_CLS_IF_FREE, /* number of pc having no interference */
58 I_CLS_IF_MAX, /* number of possible interferences in all classes */
59 I_CLS_IF_CNT, /* number of actual interferences in all classes */
61 I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
63 I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
65 /* FROM HERE: RESULT VLAUES */
66 /* all of them are external set */
69 I_HEUR_TIME, /* solving time in milli seconds */
70 I_ILP_TIME, /* solving time in milli seconds */
73 I_ILP_ITER, /* number of simplex iterations */
75 /* copy instructions */
76 I_COPIES_MAX, /* max possible costs of copies*/
77 I_COPIES_INIT, /* number of copies in initial allocation */
78 I_COPIES_HEUR, /* number of copies after heuristic */
79 I_COPIES_5SEC, /* number of copies after ilp with max n sec */
80 I_COPIES_30SEC, /* number of copies after ilp with max n sec */
81 I_COPIES_OPT, /* number of copies after ilp */
82 I_COPIES_IF, /* number of copies inevitable due to root-arg-interf */
88 * Holds current values. Values are added till next copystat_reset
92 static pset *all_phi_nodes;
93 static pset *all_phi_classes;
94 static pset *all_copy_nodes;
95 static ir_graph *last_irg;
97 void copystat_init(void) {
98 FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
100 all_phi_nodes = pset_new_ptr_default();
101 all_phi_classes = pset_new_ptr_default();
102 all_copy_nodes = pset_new_ptr_default();
105 void copystat_reset(void) {
107 for (i = 0; i < ASIZE; ++i)
109 del_pset(all_phi_nodes);
110 del_pset(all_phi_classes);
111 del_pset(all_copy_nodes);
112 all_phi_nodes = pset_new_ptr_default();
113 all_phi_classes = pset_new_ptr_default();
114 all_copy_nodes = pset_new_ptr_default();
118 * Collect general data
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 */
124 if (is_Block(node)) /* count all blocks */
125 curr_vals[I_BLOCKS]++;
127 if (is_Reg_Phi(node)) /* collect phis */
128 pset_insert_ptr(all_phi_nodes, node);
130 if (is_Perm_Proj(arch_env, node))
131 pset_insert_ptr(all_copy_nodes, node);
133 /* TODO: Add 2-Addr-Code nodes */
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 all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
143 * @return 1 if the block at pos @p pos removed a critical edge
146 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
147 const ir_edge_t *edge;
148 const ir_node *bl_at_pos, *bl_before;
149 assert(is_Block(bl));
151 /* Does bl have several predecessors ?*/
152 if (get_irn_arity(bl) <= 1)
155 /* Does the pred have exactly one predecessor */
156 bl_at_pos = get_irn_n(bl, pos);
157 if (get_irn_arity(bl_at_pos) != 1)
160 /* Does the pred of the pred have several successors */
161 bl_before = get_irn_n(bl_at_pos, 0);
162 edge = get_block_succ_first(bl_before);
163 return get_block_succ_next(bl_before, edge) ? 1 : 0;
167 * Collect phi node data
169 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
174 /* count all phi phis */
175 curr_vals[I_PHI_CNT]++;
178 arity = get_irn_arity(phi);
179 curr_vals[I_PHI_ARG_CNT] += arity;
180 if (arity > MAX_ARITY)
181 curr_vals[I_PHI_ARITY_E]++;
183 curr_vals[I_PHI_ARITY_S + arity]++;
185 phi_bl = get_nodes_block(phi);
186 /* type of argument {self, const, pred, glob} */
187 for (i = 0; i < arity; i++) {
188 ir_node *block_of_arg, *block_ith_pred;
189 ir_node *arg = get_irn_n(phi, i);
192 curr_vals[I_PHI_ARG_SELF]++;
196 if (iro_Const == get_irn_opcode(arg)) {
197 curr_vals[I_PHI_ARG_CONST]++;
201 /* get the pred block skipping blocks on critical edges */
202 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
203 if (was_edge_critical(phi_bl, i))
204 block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
206 block_of_arg = get_nodes_block(arg);
207 if (block_of_arg == block_ith_pred) {
208 curr_vals[I_PHI_ARG_PRED]++;
212 curr_vals[I_PHI_ARG_GLOB]++;
217 * Collect register-constrained node data
219 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
220 curr_vals[I_CPY_CNT]++;
221 curr_vals[I_COPIES_MAX]++;
222 if (nodes_interfere(chordal_env, root, get_Perm_src(root))) {
223 curr_vals[I_COPIES_IF]++;
224 assert(0 && "A Perm pair (in/out) should never interfere!");
229 * Collect phi class data
231 static void stat_phi_class(be_chordal_env_t *chordal_env, pset *pc) {
232 int i, o, size, if_free, phis;
233 ir_node **members, *p;
235 /* phi class count */
236 curr_vals[I_CLS_CNT]++;
239 size = pset_count(pc);
240 if (size > MAX_CLS_SIZE)
241 curr_vals[I_CLS_SIZE_E]++;
243 curr_vals[I_CLS_SIZE_S + size]++;
245 /* get an array of all members for double iterating */
246 members = xmalloc(size * sizeof(*members));
247 DBG((dbg, LEVEL_2, "Phi-class:\n"));
248 for (i = 0, p = pset_first(pc); p; p = pset_next(pc)) {
249 DBG((dbg, LEVEL_2, " %+F\n", p));
254 /* determine number of phis on this class */
256 for (i = 0; i < size; ++i)
257 if (is_Phi(members[i]))
259 if (phis > MAX_CLS_PHIS)
260 curr_vals[I_CLS_PHIS_E]++;
262 curr_vals[I_CLS_PHIS_S + phis]++;
264 /* determine interference of phi class members */
265 curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
267 for (i = 0; i < size-1; ++i)
268 for (o = i+1; o < size; ++o)
269 if (nodes_interfere(chordal_env, members[i], members[o])) {
271 curr_vals[I_CLS_IF_CNT]++;
274 /* Does this phi class have an inner interference? */
275 curr_vals[I_CLS_IF_FREE] += if_free;
280 void copystat_collect_cls(be_chordal_env_t *cenv) {
283 ir_graph *irg = cenv->irg;
284 arch_env_t *aenv = cenv->birg->main_env->arch_env;
287 copystat_collect_irg(irg, aenv);
289 for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
290 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
291 stat_phi_node(cenv, n);
293 for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
294 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
295 stat_copy_node(cenv, n);
297 for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
298 ir_node *member = pset_first(pc);
300 if (arch_get_irn_reg_class(aenv, member, -1) == cenv->cls)
301 stat_phi_class(cenv, pc);
305 void copystat_add_max_costs(int costs) {
306 curr_vals[I_COPIES_MAX] += costs;
308 void copystat_add_inevit_costs(int costs) {
309 curr_vals[I_COPIES_IF] += costs;
311 void copystat_add_init_costs(int costs) {
312 curr_vals[I_COPIES_INIT] += costs;
314 void copystat_add_heur_costs(int costs) {
315 curr_vals[I_COPIES_HEUR] += costs;
317 void copystat_add_ilp_5_sec_costs(int costs) {
318 curr_vals[I_COPIES_5SEC] += costs;
320 void copystat_add_ilp_30_sec_costs(int costs) {
321 curr_vals[I_COPIES_30SEC] += costs;
323 void copystat_add_opt_costs(int costs) {
324 curr_vals[I_COPIES_OPT] += costs;
326 void copystat_add_heur_time(int time) {
327 curr_vals[I_HEUR_TIME] += time;
332 void copystat_add_ilp_time(int time) {
333 curr_vals[I_ILP_TIME] += time;
335 void copystat_add_ilp_vars(int vars) {
336 curr_vals[I_ILP_VARS] += vars;
338 void copystat_add_ilp_csts(int csts) {
339 curr_vals[I_ILP_CSTR] += csts;
341 void copystat_add_ilp_iter(int iters) {
342 curr_vals[I_ILP_ITER] += iters;
345 #endif /* WITH_ILP */
347 void copystat_dump(ir_graph *irg) {
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");
356 fprintf(out, "%d\n", ASIZE);
357 for (i = 0; i < ASIZE; i++) {
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]);
365 fprintf(out, "%i\n", curr_vals[i]);
371 void copystat_dump_pretty(ir_graph *irg) {
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");
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]);
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]);
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]);
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]);
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]);
420 * Helpers for saving and restoring colors of nodes.
421 * Used to get dependable and comparable benchmark results.
423 typedef struct color_saver {
424 arch_env_t *arch_env;
425 be_chordal_env_t *chordal_env;
427 int flag; /* 0 save, 1 load */
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);
437 arch_register_t *reg = pmap_get(saver->saved_colors, irn);
438 arch_set_irn_register(saver->arch_env, irn, reg);
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);
448 static void load_colors(color_save_t *color_saver) {
449 color_saver->flag = 1;
450 irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
454 * Main compare routine
456 void co_compare_solvers(be_chordal_env_t *chordal_env) {
460 int costs_inevit, costs_init, costs_solved, lower_bound;
462 phi_class_compute(chordal_env->irg);
463 copystat_collect_cls(chordal_env);
465 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
466 co_build_ou_structure(co);
467 co_build_graph_structure(co);
468 DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
471 saver.arch_env = chordal_env->birg->main_env->arch_env;
472 saver.chordal_env = chordal_env;
473 saver.saved_colors = pmap_create();
475 be_ra_chordal_check(co->cenv);
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);
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));
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));
490 /* heuristic 1 (Daniel Grund) */
491 timer = lc_timer_register("heur1", NULL);
492 lc_timer_reset_and_start(timer);
494 co_solve_heuristic(co);
496 lc_timer_stop(timer);
498 be_ra_chordal_check(co->cenv);
499 costs_solved = co_get_copy_costs(co);
500 DBG((dbg, LEVEL_1, "HEUR1 costs: %3d\n", costs_solved));
501 copystat_add_heur_time(lc_timer_elapsed_msec(timer));
502 copystat_add_heur_costs(costs_solved);
503 assert(lower_bound <= costs_solved);
505 /* heuristic 2 (Sebastian Hack) */
506 timer = lc_timer_register("heur2", NULL);
507 lc_timer_reset_and_start(timer);
509 co_solve_heuristic_new(co);
511 lc_timer_stop(timer);
513 be_ra_chordal_check(co->cenv);
514 costs_solved = co_get_copy_costs(co);
515 DBG((dbg, LEVEL_1, "HEUR2 costs: %3d\n", costs_solved));
516 copystat_add_heur_time(lc_timer_elapsed_msec(timer));
517 copystat_add_heur_costs(costs_solved);
518 assert(lower_bound <= costs_solved);
520 /* Park & Moon register coalescing (Kimon Hoffmann) */
521 timer = lc_timer_register("park", NULL);
522 lc_timer_reset_and_start(timer);
524 co_solve_park_moon(co);
526 lc_timer_stop(timer);
528 be_ra_chordal_check(co->cenv);
529 costs_solved = co_get_copy_costs(co);
530 DBG((dbg, LEVEL_1, "Park/Moon costs: %3d\n", costs_solved));
531 copystat_add_heur_time(lc_timer_elapsed_msec(timer));
532 copystat_add_heur_costs(costs_solved);
533 assert(lower_bound <= costs_solved);
538 /* ILP 1 is not yet implemented, so it makes no sense to compare */
542 co_solve_ilp1(co, 60.0);
544 costs_solved = co_get_copy_costs(co);
545 DBG((dbg, LEVEL_1, "ILP1 costs: %3d\n", costs_solved));
546 copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
547 assert(lower_bound <= costs_solved);
555 be_ra_chordal_check(co->cenv);
556 costs_solved = co_get_copy_costs(co);
557 DBG((dbg, LEVEL_1, "ILP2 costs: %3d\n", costs_solved));
558 copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
559 assert(lower_bound <= costs_solved);
561 #endif /* WITH_ILP */
563 /* free memory for statistic structures */
564 pmap_destroy(saver.saved_colors);
565 co_free_graph_structure(co);
566 co_free_ou_structure(co);