2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Copy node statistics.
23 * @author Daniel Grund
38 #include "iredges_t.h"
40 #include "irnodeset.h"
42 #include "bechordal_t.h"
44 #include "becopyopt_t.h"
45 #include "becopystat.h"
48 #include "beintlive_t.h"
50 #define DEBUG_LVL SET_LEVEL_1
51 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54 #define MAX_CLS_SIZE 20
55 #define MAX_CLS_PHIS 20
58 * For an explanation of these values see the code of copystat_dump_pretty
61 /* FROM HERE: PROBLEM CHARACTERIZATION */
67 I_PHI_CNT, /* number of phi nodes */
68 I_PHI_ARG_CNT, /* number of arguments of phis */
69 I_PHI_ARG_SELF, /* number of arguments of phis being the phi itself */
70 I_PHI_ARG_CONST, /* number of arguments of phis being consts */
71 I_PHI_ARG_PRED, /* ... being defined in a cf-pred */
72 I_PHI_ARG_GLOB, /* ... being defined elsewhere */
74 I_PHI_ARITY_E = I_PHI_ARITY_S+MAX_ARITY,
77 I_CPY_CNT, /* number of copynodes */
80 I_CLS_CNT, /* number of phi classes */
81 I_CLS_IF_FREE, /* number of pc having no interference */
82 I_CLS_IF_MAX, /* number of possible interferences in all classes */
83 I_CLS_IF_CNT, /* number of actual interferences in all classes */
85 I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
87 I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
89 /* FROM HERE: RESULT VLAUES */
90 /* all of them are external set */
93 I_HEUR_TIME, /* solving time in milli seconds */
94 I_ILP_TIME, /* solving time in milli seconds */
97 I_ILP_ITER, /* number of simplex iterations */
99 /* copy instructions */
100 I_COPIES_MAX, /* max possible costs of copies*/
101 I_COPIES_INIT, /* number of copies in initial allocation */
102 I_COPIES_HEUR, /* number of copies after heuristic */
103 I_COPIES_5SEC, /* number of copies after ilp with max n sec */
104 I_COPIES_30SEC, /* number of copies after ilp with max n sec */
105 I_COPIES_OPT, /* number of copies after ilp */
106 I_COPIES_IF, /* number of copies inevitable due to root-arg-interf */
112 * Holds current values. Values are added till next copystat_reset
114 int curr_vals[ASIZE];
116 static ir_nodeset_t *all_phi_nodes;
117 static ir_nodeset_t *all_copy_nodes;
118 static ir_graph *last_irg;
120 void be_init_copystat(void) {
121 FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
123 all_phi_nodes = ir_nodeset_new(64);
124 all_copy_nodes = ir_nodeset_new(64);
125 memset(curr_vals, 0, sizeof(curr_vals));
127 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat);
129 void be_quit_copystat(void) {
130 ir_nodeset_del(all_phi_nodes);
131 ir_nodeset_del(all_copy_nodes);
133 BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat);
135 void copystat_reset(void) {
141 * Collect general data
143 static void irg_stat_walker(ir_node *node, void *env) {
144 arch_env_t *arch_env = env;
145 curr_vals[I_ALL_NODES]++; /* count all nodes */
147 if (is_Block(node)) /* count all blocks */
148 curr_vals[I_BLOCKS]++;
150 if (is_Reg_Phi(node)) /* collect phis */
151 ir_nodeset_insert(all_phi_nodes, node);
153 if (is_Perm_Proj(arch_env, node))
154 ir_nodeset_insert(all_copy_nodes, node);
156 /* TODO: Add 2-Addr-Code nodes */
159 static void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
160 irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
165 * @return 1 if the block at pos @p pos removed a critical edge
168 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
169 const ir_edge_t *edge;
170 const ir_node *bl_at_pos, *bl_before;
171 assert(is_Block(bl));
173 /* Does bl have several predecessors ?*/
174 if (get_irn_arity(bl) <= 1)
177 /* Does the pred have exactly one predecessor */
178 bl_at_pos = get_irn_n(bl, pos);
179 if (get_irn_arity(bl_at_pos) != 1)
182 /* Does the pred of the pred have several successors */
183 bl_before = get_irn_n(bl_at_pos, 0);
184 edge = get_block_succ_first(bl_before);
185 return get_block_succ_next(bl_before, edge) ? 1 : 0;
189 * Collect phi node data
191 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi)
198 /* count all phi phis */
199 curr_vals[I_PHI_CNT]++;
202 arity = get_irn_arity(phi);
203 curr_vals[I_PHI_ARG_CNT] += arity;
204 if (arity > MAX_ARITY)
205 curr_vals[I_PHI_ARITY_E]++;
207 curr_vals[I_PHI_ARITY_S + arity]++;
209 phi_bl = get_nodes_block(phi);
210 /* type of argument {self, const, pred, glob} */
211 for (i = 0; i < arity; i++) {
212 ir_node *block_of_arg, *block_ith_pred;
213 ir_node *arg = get_irn_n(phi, i);
216 curr_vals[I_PHI_ARG_SELF]++;
220 if (iro_Const == get_irn_opcode(arg)) {
221 curr_vals[I_PHI_ARG_CONST]++;
225 /* get the pred block skipping blocks on critical edges */
226 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
227 if (was_edge_critical(phi_bl, i))
228 block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
230 block_of_arg = get_nodes_block(arg);
231 if (block_of_arg == block_ith_pred) {
232 curr_vals[I_PHI_ARG_PRED]++;
236 curr_vals[I_PHI_ARG_GLOB]++;
241 * Collect register-constrained node data
243 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
244 curr_vals[I_CPY_CNT]++;
245 curr_vals[I_COPIES_MAX]++;
246 if (values_interfere(chordal_env->birg, root, get_Perm_src(root))) {
247 curr_vals[I_COPIES_IF]++;
248 assert(0 && "A Perm pair (in/out) should never interfere!");
253 * Collect phi class data
255 static void stat_phi_class(be_chordal_env_t *chordal_env, ir_node **pc) {
256 int i, o, size, if_free, phis;
258 /* phi class count */
259 curr_vals[I_CLS_CNT]++;
263 if (size > MAX_CLS_SIZE)
264 curr_vals[I_CLS_SIZE_E]++;
266 curr_vals[I_CLS_SIZE_S + size]++;
268 /* determine number of phis on this class */
269 for (phis = i = 0; i < size; ++i)
273 if (phis > MAX_CLS_PHIS)
274 curr_vals[I_CLS_PHIS_E]++;
276 curr_vals[I_CLS_PHIS_S + phis]++;
278 /* determine interference of phi class members */
279 curr_vals[I_CLS_IF_MAX] += size * (size - 1) / 2;
280 for (if_free = 1, i = 0; i < size - 1; ++i)
281 for (o = i + 1; o < size; ++o)
282 if (values_interfere(chordal_env->birg, pc[i], pc[o])) {
284 curr_vals[I_CLS_IF_CNT]++;
287 /* Does this phi class have an inner interference? */
288 curr_vals[I_CLS_IF_FREE] += if_free;
291 static void copystat_collect_cls(be_chordal_env_t *cenv) {
292 ir_graph *irg = cenv->irg;
293 arch_env_t *aenv = cenv->birg->main_env->arch_env;
295 phi_classes_t *pc_obj;
296 pset *all_phi_classes;
297 ir_nodeset_iterator_t iter;
300 copystat_collect_irg(irg, aenv);
302 /* compute the Phi classes of the collected Phis */
303 pc_obj = phi_class_new_from_set(cenv->irg, all_phi_nodes, 0);
304 all_phi_classes = get_all_phi_classes(pc_obj);
306 foreach_ir_nodeset(all_phi_nodes, n, iter) {
307 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
308 stat_phi_node(cenv, n);
311 foreach_ir_nodeset(all_copy_nodes, n, iter) {
312 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
313 stat_copy_node(cenv, n);
316 foreach_pset(all_phi_classes, pc) {
317 ir_node *member = pc[0];
318 if (arch_get_irn_reg_class(aenv, member, -1) == cenv->cls)
319 stat_phi_class(cenv, pc);
322 /* free the phi class object */
323 phi_class_free(pc_obj);
326 void copystat_add_max_costs(int costs) {
327 curr_vals[I_COPIES_MAX] += costs;
329 void copystat_add_inevit_costs(int costs) {
330 curr_vals[I_COPIES_IF] += costs;
332 void copystat_add_init_costs(int costs) {
333 curr_vals[I_COPIES_INIT] += costs;
335 void copystat_add_heur_costs(int costs) {
336 curr_vals[I_COPIES_HEUR] += costs;
338 void copystat_add_ilp_5_sec_costs(int costs) {
339 curr_vals[I_COPIES_5SEC] += costs;
341 void copystat_add_ilp_30_sec_costs(int costs) {
342 curr_vals[I_COPIES_30SEC] += costs;
344 void copystat_add_opt_costs(int costs) {
345 curr_vals[I_COPIES_OPT] += costs;
347 void copystat_add_heur_time(int time) {
348 curr_vals[I_HEUR_TIME] += time;
353 void copystat_add_ilp_time(int time) {
354 curr_vals[I_ILP_TIME] += time;
356 void copystat_add_ilp_vars(int vars) {
357 curr_vals[I_ILP_VARS] += vars;
359 void copystat_add_ilp_csts(int csts) {
360 curr_vals[I_ILP_CSTR] += csts;
362 void copystat_add_ilp_iter(int iters) {
363 curr_vals[I_ILP_ITER] += iters;
366 #endif /* WITH_ILP */
368 void copystat_dump(ir_graph *irg) {
373 snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
374 buf[sizeof(buf) - 1] = '\0';
375 out = be_ffopen(buf, "stat", "wt");
377 fprintf(out, "%d\n", ASIZE);
378 for (i = 0; i < ASIZE; i++) {
380 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
381 fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
382 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
383 fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
386 fprintf(out, "%i\n", curr_vals[i]);
392 void copystat_dump_pretty(ir_graph *irg) {
397 snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
398 buf[sizeof(buf) - 1] = '\0';
399 out = be_ffopen(buf, "pstat", "wt");
401 fprintf(out, "Nodes %4d\n", curr_vals[I_ALL_NODES]);
402 fprintf(out, "Blocks %4d\n", curr_vals[I_BLOCKS]);
403 fprintf(out, "CopyIrn %4d\n", curr_vals[I_CPY_CNT]);
405 fprintf(out, "\nPhis %4d\n", curr_vals[I_PHI_CNT]);
406 fprintf(out, "... argument types\n");
407 fprintf(out, " Total %4d\n", curr_vals[I_PHI_ARG_CNT]);
408 fprintf(out, " Self %4d\n", curr_vals[I_PHI_ARG_SELF]);
409 fprintf(out, " Constants %4d\n", curr_vals[I_PHI_ARG_CONST]);
410 fprintf(out, " CF-Pred %4d\n", curr_vals[I_PHI_ARG_PRED]);
411 fprintf(out, " Others %4d\n", curr_vals[I_PHI_ARG_GLOB]);
412 fprintf(out, "... arities\n");
413 for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
414 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
416 fprintf(out, "\nPhi classes %4d\n", curr_vals[I_CLS_CNT]);
417 fprintf(out, " compl. free %4d\n", curr_vals[I_CLS_IF_FREE]);
418 fprintf(out, " inner intf. %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
419 fprintf(out, "... sizes\n");
420 for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
421 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
422 fprintf(out, "... contained phis\n");
423 for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
424 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
426 fprintf(out, "\nILP stat\n");
427 fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
428 fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
430 fprintf(out, "\nCopy stat\n");
431 fprintf(out, " Max %4d\n", curr_vals[I_COPIES_MAX]);
432 fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
433 fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
434 fprintf(out, " Opt %4d\n", curr_vals[I_COPIES_OPT]);
435 fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
441 * Helpers for saving and restoring colors of nodes.
442 * Used to get dependable and comparable benchmark results.
444 typedef struct color_saver {
445 arch_env_t *arch_env;
446 be_chordal_env_t *chordal_env;
448 int flag; /* 0 save, 1 load */
451 static void save_load(ir_node *irn, void *env) {
452 color_save_t *saver = env;
453 if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, -1)) {
454 if (saver->flag == 0) { /* save */
455 const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn);
456 pmap_insert(saver->saved_colors, irn, (void *) reg);
458 arch_register_t *reg = pmap_get(saver->saved_colors, irn);
459 arch_set_irn_register(saver->arch_env, irn, reg);
464 static void save_colors(color_save_t *color_saver) {
465 color_saver->flag = 0;
466 irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
470 static void load_colors(color_save_t *color_saver) {
471 color_saver->flag = 1;
472 irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
477 * Main compare routine
479 void co_compare_solvers(be_chordal_env_t *chordal_env) {
483 int costs_inevit, costs_init, costs_solved, lower_bound;
485 copystat_collect_cls(chordal_env);
487 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
488 co_build_ou_structure(co);
489 co_build_graph_structure(co);
490 DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
493 saver.arch_env = chordal_env->birg->main_env->arch_env;
494 saver.chordal_env = chordal_env;
495 saver.saved_colors = pmap_create();
499 costs_inevit = co_get_inevit_copy_costs(co);
500 lower_bound = co_get_lower_bound(co);
501 costs_init = co_get_copy_costs(co);
503 DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", costs_inevit));
504 DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
505 DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
507 copystat_add_inevit_costs(costs_inevit);
508 copystat_add_init_costs(costs_init);
509 copystat_add_max_costs(co_get_max_copy_costs(co));
511 /* heuristic 1 (Daniel Grund) */
512 timer = ir_timer_register("heur1", NULL);
513 ir_timer_reset_and_start(timer);
515 co_solve_heuristic(co);
517 ir_timer_stop(timer);
519 costs_solved = co_get_copy_costs(co);
520 DBG((dbg, LEVEL_1, "HEUR1 costs: %3d\n", costs_solved));
521 copystat_add_heur_time(ir_timer_elapsed_msec(timer));
522 copystat_add_heur_costs(costs_solved);
523 assert(lower_bound <= costs_solved);
525 /* heuristic 2 (Sebastian Hack) */
526 timer = ir_timer_register("heur2", NULL);
527 ir_timer_reset_and_start(timer);
529 co_solve_heuristic_new(co);
531 ir_timer_stop(timer);
533 costs_solved = co_get_copy_costs(co);
534 DBG((dbg, LEVEL_1, "HEUR2 costs: %3d\n", costs_solved));
535 copystat_add_heur_time(ir_timer_elapsed_msec(timer));
536 copystat_add_heur_costs(costs_solved);
537 assert(lower_bound <= costs_solved);
539 /* Park & Moon register coalescing (Kimon Hoffmann) */
540 timer = ir_timer_register("park", NULL);
541 ir_timer_reset_and_start(timer);
543 co_solve_park_moon(co);
545 ir_timer_stop(timer);
547 costs_solved = co_get_copy_costs(co);
548 DBG((dbg, LEVEL_1, "Park/Moon costs: %3d\n", costs_solved));
549 copystat_add_heur_time(ir_timer_elapsed_msec(timer));
550 copystat_add_heur_costs(costs_solved);
551 assert(lower_bound <= costs_solved);
556 /* ILP 1 is not yet implemented, so it makes no sense to compare */
560 co_solve_ilp1(co, 60.0);
562 costs_solved = co_get_copy_costs(co);
563 DBG((dbg, LEVEL_1, "ILP1 costs: %3d\n", costs_solved));
564 copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
565 assert(lower_bound <= costs_solved);
573 costs_solved = co_get_copy_costs(co);
574 DBG((dbg, LEVEL_1, "ILP2 costs: %3d\n", costs_solved));
575 copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
576 assert(lower_bound <= costs_solved);
578 #endif /* WITH_ILP */
580 /* free memory for statistic structures */
581 pmap_destroy(saver.saved_colors);
582 co_free_graph_structure(co);
583 co_free_ou_structure(co);