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"
31 #define DEBUG_LVL SET_LEVEL_1
32 static firm_dbg_module_t *dbg = NULL;
35 #define MAX_CLS_SIZE 20
36 #define MAX_CLS_PHIS 20
39 * For an explanation of these values see the code of copystat_dump_pretty
42 /* FROM HERE: PROBLEM CHARACTERIZATION */
48 I_PHI_CNT, /* number of phi nodes */
49 I_PHI_ARG_CNT, /* number of arguments of phis */
50 I_PHI_ARG_SELF, /* number of arguments of phis being the phi itself */
51 I_PHI_ARG_CONST, /* number of arguments of phis being consts */
52 I_PHI_ARG_PRED, /* ... being defined in a cf-pred */
53 I_PHI_ARG_GLOB, /* ... being defined elsewhere */
55 I_PHI_ARITY_E = I_PHI_ARITY_S+MAX_ARITY,
58 I_CPY_CNT, /* number of copynodes */
61 I_CLS_CNT, /* number of phi classes */
62 I_CLS_IF_FREE, /* number of pc having no interference */
63 I_CLS_IF_MAX, /* number of possible interferences in all classes */
64 I_CLS_IF_CNT, /* number of actual interferences in all classes */
66 I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
68 I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
70 /* FROM HERE: RESULT VLAUES */
71 /* all of them are external set */
74 I_HEUR_TIME, /* solving time in milli seconds */
75 I_ILP_TIME, /* solving time in milli seconds */
78 I_ILP_ITER, /* number of simplex iterations */
80 /* copy instructions */
81 I_COPIES_MAX, /* max possible costs of copies*/
82 I_COPIES_INIT, /* number of copies in initial allocation */
83 I_COPIES_HEUR, /* number of copies after heuristic */
84 I_COPIES_5SEC, /* number of copies after ilp with max n sec */
85 I_COPIES_30SEC, /* number of copies after ilp with max n sec */
86 I_COPIES_OPT, /* number of copies after ilp */
87 I_COPIES_IF, /* number of copies inevitable due to root-arg-interf */
93 * Holds current values. Values are added till next copystat_reset
97 static pset *all_phi_nodes;
98 static pset *all_phi_classes;
99 static pset *all_copy_nodes;
100 static ir_graph *last_irg;
102 void copystat_init(void) {
103 dbg = firm_dbg_register("ir.be.copystat");
104 firm_dbg_set_mask(dbg, DEBUG_LVL);
106 all_phi_nodes = pset_new_ptr_default();
107 all_phi_classes = pset_new_ptr_default();
108 all_copy_nodes = pset_new_ptr_default();
111 void copystat_reset(void) {
113 for (i = 0; i < ASIZE; ++i)
115 del_pset(all_phi_nodes);
116 del_pset(all_phi_classes);
117 del_pset(all_copy_nodes);
118 all_phi_nodes = pset_new_ptr_default();
119 all_phi_classes = pset_new_ptr_default();
120 all_copy_nodes = pset_new_ptr_default();
124 * Collect general data
126 static void irg_stat_walker(ir_node *node, void *env) {
127 arch_env_t *arch_env = env;
128 curr_vals[I_ALL_NODES]++; /* count all nodes */
130 if (is_Block(node)) /* count all blocks */
131 curr_vals[I_BLOCKS]++;
133 if (is_Reg_Phi(node)) /* collect phis */
134 pset_insert_ptr(all_phi_nodes, node);
136 if (is_Perm_Proj(arch_env, node))
137 pset_insert_ptr(all_copy_nodes, node);
139 /* TODO: Add 2-Addr-Code nodes */
142 static void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
143 irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
144 all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
149 * @return 1 if the block at pos @p pos removed a critical edge
152 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
153 const ir_edge_t *edge;
154 const ir_node *bl_at_pos, *bl_before;
155 assert(is_Block(bl));
157 /* Does bl have several predecessors ?*/
158 if (get_irn_arity(bl) <= 1)
161 /* Does the pred have exactly one predecessor */
162 bl_at_pos = get_irn_n(bl, pos);
163 if (get_irn_arity(bl_at_pos) != 1)
166 /* Does the pred of the pred have several sucsecessors */
167 bl_before = get_irn_n(bl_at_pos, 0);
168 edge = get_block_succ_first(bl_before);
169 return get_block_succ_next(bl_before, edge) ? 1 : 0;
173 * Collect phi node data
175 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
180 /* count all phi phis */
181 curr_vals[I_PHI_CNT]++;
184 arity = get_irn_arity(phi);
185 curr_vals[I_PHI_ARG_CNT] += arity;
186 if (arity > MAX_ARITY)
187 curr_vals[I_PHI_ARITY_E]++;
189 curr_vals[I_PHI_ARITY_S + arity]++;
191 phi_bl = get_nodes_block(phi);
192 /* type of argument {self, const, pred, glob} */
193 for (i = 0; i < arity; i++) {
194 ir_node *block_of_arg, *block_ith_pred;
195 ir_node *arg = get_irn_n(phi, i);
198 curr_vals[I_PHI_ARG_SELF]++;
202 if (iro_Const == get_irn_opcode(arg)) {
203 curr_vals[I_PHI_ARG_CONST]++;
207 /* get the pred block skipping blocks on critical edges */
208 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
209 if (was_edge_critical(phi_bl, i))
210 block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
212 block_of_arg = get_nodes_block(arg);
213 if (block_of_arg == block_ith_pred) {
214 curr_vals[I_PHI_ARG_PRED]++;
218 curr_vals[I_PHI_ARG_GLOB]++;
223 * Collect register-constrained node data
225 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
226 curr_vals[I_CPY_CNT]++;
227 curr_vals[I_COPIES_MAX]++;
228 if (nodes_interfere(chordal_env, root, get_Perm_src(root))) {
229 curr_vals[I_COPIES_IF]++;
230 assert(0 && "A Perm pair (in/out) should never interfere!");
235 * Collect phi class data
237 static void stat_phi_class(be_chordal_env_t *chordal_env, pset *pc) {
238 int i, o, size, if_free, phis;
239 ir_node **members, *p;
241 /* phi class count */
242 curr_vals[I_CLS_CNT]++;
245 size = pset_count(pc);
246 if (size > MAX_CLS_SIZE)
247 curr_vals[I_CLS_SIZE_E]++;
249 curr_vals[I_CLS_SIZE_S + size]++;
251 /* get an array of all members for double iterating */
252 members = xmalloc(size * sizeof(*members));
253 DBG((dbg, LEVEL_2, "Phi-class:\n"));
254 for (i = 0, p = pset_first(pc); p; p = pset_next(pc)) {
255 DBG((dbg, LEVEL_2, " %+F\n", p));
260 /* determine number of phis on this class */
262 for (i = 0; i < size; ++i)
263 if (is_Phi(members[i]))
265 if (phis > MAX_CLS_PHIS)
266 curr_vals[I_CLS_PHIS_E]++;
268 curr_vals[I_CLS_PHIS_S + phis]++;
270 /* determine interference of phi class members */
271 curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
273 for (i = 0; i < size-1; ++i)
274 for (o = i+1; o < size; ++o)
275 if (nodes_interfere(chordal_env, members[i], members[o])) {
277 curr_vals[I_CLS_IF_CNT]++;
280 /* Does this phi class have an inner interference? */
281 curr_vals[I_CLS_IF_FREE] += if_free;
286 void copystat_collect_cls(be_chordal_env_t *cenv) {
289 ir_graph *irg = cenv->irg;
290 arch_env_t *aenv = cenv->birg->main_env->arch_env;
293 copystat_collect_irg(irg, aenv);
295 for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
296 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
297 stat_phi_node(cenv, n);
299 for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
300 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
301 stat_copy_node(cenv, n);
303 for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
304 ir_node *member = pset_first(pc);
306 if (arch_get_irn_reg_class(aenv, member, -1) == cenv->cls)
307 stat_phi_class(cenv, pc);
311 void copystat_add_max_costs(int costs) {
312 curr_vals[I_COPIES_MAX] += costs;
314 void copystat_add_inevit_costs(int costs) {
315 curr_vals[I_COPIES_IF] += costs;
317 void copystat_add_init_costs(int costs) {
318 curr_vals[I_COPIES_INIT] += costs;
320 void copystat_add_heur_costs(int costs) {
321 curr_vals[I_COPIES_HEUR] += costs;
323 void copystat_add_ilp_5_sec_costs(int costs) {
324 curr_vals[I_COPIES_5SEC] += costs;
326 void copystat_add_ilp_30_sec_costs(int costs) {
327 curr_vals[I_COPIES_30SEC] += costs;
329 void copystat_add_opt_costs(int costs) {
330 curr_vals[I_COPIES_OPT] += costs;
332 void copystat_add_heur_time(int time) {
333 curr_vals[I_HEUR_TIME] += time;
335 void copystat_add_ilp_time(int time) {
336 curr_vals[I_ILP_TIME] += time;
338 void copystat_add_ilp_vars(int vars) {
339 curr_vals[I_ILP_VARS] += vars;
341 void copystat_add_ilp_csts(int csts) {
342 curr_vals[I_ILP_CSTR] += csts;
344 void copystat_add_ilp_iter(int iters) {
345 curr_vals[I_ILP_ITER] += iters;
348 void copystat_dump(ir_graph *irg) {
353 snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
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 out = ffopen(buf, "pstat", "wt");
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]);
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]);
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]);
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]);
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]);
419 * Helpers for saving and restoring colors of nodes.
420 * Used to get dependable and comparable benchmark results.
422 typedef struct color_saver {
423 arch_env_t *arch_env;
424 be_chordal_env_t *chordal_env;
426 int flag; /* 0 save, 1 load */
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);
436 arch_register_t *reg = pmap_get(saver->saved_colors, irn);
437 arch_set_irn_register(saver->arch_env, irn, reg);
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);
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);
453 * Main compare routine
455 void co_compare_solvers(be_chordal_env_t *chordal_env) {
459 int costs_inevit, costs_init, costs_heur, costs_ilp1, costs_ilp2, lower_bound;
461 phi_class_compute(chordal_env->irg);
462 copystat_collect_cls(chordal_env);
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));
470 saver.arch_env = chordal_env->birg->main_env->arch_env;
471 saver.chordal_env = chordal_env;
472 saver.saved_colors = pmap_create();
474 be_ra_chordal_check(co->cenv);
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);
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));
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));
491 timer = lc_timer_register("heur", 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_heur = co_get_copy_costs(co);
500 DBG((dbg, LEVEL_1, "HEUR costs: %3d\n", costs_heur));
501 copystat_add_heur_time(lc_timer_elapsed_msec(timer));
502 copystat_add_heur_costs(costs_heur);
503 assert(lower_bound <= costs_heur);
510 co_solve_ilp1(co, 60.0);
512 costs_ilp1 = co_get_copy_costs(co);
513 DBG((dbg, LEVEL_1, "ILP1 costs: %3d\n", costs_ilp1));
514 copystat_add_opt_costs(costs_ilp1); /*TODO ADAPT */
515 assert(lower_bound <= costs_ilp1);
522 co_solve_ilp2(co, 60.0);
524 be_ra_chordal_check(co->cenv);
525 costs_ilp2 = co_get_copy_costs(co);
526 DBG((dbg, LEVEL_1, "ILP2 costs: %3d\n", costs_ilp2));
527 copystat_add_opt_costs(costs_ilp2); /*TODO ADAPT */
528 assert(lower_bound <= costs_ilp2);
531 pmap_destroy(saver.saved_colors);
532 co_free_graph_structure(co);
533 co_free_ou_structure(co);
538 #endif /* COPYOPT_STAT */