4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 #include "phiclass_t.h"
16 #include "becopyopt.h"
17 #include "becopystat.h"
22 #define DEBUG_LVL SET_LEVEL_1
23 static firm_dbg_module_t *dbg = NULL;
26 #define MAX_CLS_SIZE 10
27 #define MAX_CLS_PHIS 10
30 * For an explanation of these values see the code of copystat_dump_pretty
33 /* FROM HERE: PROBLEM CHARACTERIZATION */
39 I_PHI_CNT, /* number of phi nodes */
40 I_PHI_ARG_CNT, /* number of arguments of phis */
41 I_PHI_ARG_SELF, /* number of arguments of phis being the phi itself */
42 I_PHI_ARG_CONST, /* number of arguments of phis being consts */
43 I_PHI_ARG_PRED, /* ... being defined in a cf-pred */
44 I_PHI_ARG_GLOB, /* ... being defined elsewhere */
46 I_PHI_ARITY_E = I_PHI_ARITY_S+MAX_ARITY,
49 I_CPY_CNT, /* number of copynodes */
52 I_CLS_CNT, /* number of phi classes */
53 I_CLS_IF_FREE, /* number of pc having no interference */
54 I_CLS_IF_MAX, /* number of possible interferences in all classes */
55 I_CLS_IF_CNT, /* number of actual interferences in all classes */
57 I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
59 I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
61 /* FROM HERE: RESULT VLAUES */
62 /* all of them are external set */
65 I_HEUR_TIME, /* solving time in milli seconds */
66 I_ILP_TIME, /* solving time in milli seconds */
69 I_ILP_ITER, /* number of simplex iterations */
71 /* copy instructions */
72 I_COPIES_MAX, /* max possible costs of copies*/
73 I_COPIES_INIT, /* number of copies in initial allocation */
74 I_COPIES_HEUR, /* number of copies after heuristic */
75 I_COPIES_NSEC, /* number of copies after ilp with max n sec */
76 I_COPIES_OPT, /* number of copies after ilp */
77 I_COPIES_IF, /* number of copies inevitable due to root-arg-interf */
83 * Holds current values. Values are added till next copystat_reset
87 static pset *all_phi_nodes;
88 static pset *all_phi_classes;
89 static pset *all_copy_nodes;
90 static ir_graph *last_irg;
92 void copystat_init(void) {
93 dbg = firm_dbg_register("ir.be.copystat");
94 firm_dbg_set_mask(dbg, DEBUG_LVL);
96 all_phi_nodes = pset_new_ptr_default();
97 all_phi_classes = pset_new_ptr_default();
98 all_copy_nodes = pset_new_ptr_default();
101 void copystat_reset(void) {
103 for (i = 0; i < ASIZE; ++i)
105 del_pset(all_phi_nodes);
106 del_pset(all_phi_classes);
107 del_pset(all_copy_nodes);
108 all_phi_nodes = pset_new_ptr_default();
109 all_phi_classes = pset_new_ptr_default();
110 all_copy_nodes = pset_new_ptr_default();
114 * Collect general data
116 static void irg_stat_walker(ir_node *node, void *env) {
117 arch_env_t *arch_env = env;
118 curr_vals[I_ALL_NODES]++; /* count all nodes */
120 if (is_Block(node)) /* count all blocks */
121 curr_vals[I_BLOCKS]++;
123 if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) /* collect phis */
124 pset_insert_ptr(all_phi_nodes, node);
126 if (is_Copy(arch_env, node))
127 pset_insert_ptr(all_copy_nodes, node);
130 static void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
131 irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
132 all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
137 * @return 1 if the block at pos @p pos removed a critical edge
140 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
141 const ir_edge_t *edge;
142 const ir_node *bl_at_pos, *bl_before;
143 assert(is_Block(bl));
145 /* Does bl have several predecessors ?*/
146 if (get_irn_arity(bl) <= 1)
149 /* Does the pred have exactly one predecessor */
150 bl_at_pos = get_irn_n(bl, pos);
151 if (get_irn_arity(bl_at_pos) != 1)
154 /* Does the pred of the pred have several sucsecessors */
155 bl_before = get_irn_n(bl_at_pos, 0);
156 edge = get_block_succ_first(bl_before);
157 return get_block_succ_next(bl_before, edge) ? 1 : 0;
161 * Collect phi node data
163 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
168 /* count all phi phis */
169 curr_vals[I_PHI_CNT]++;
172 arity = get_irn_arity(phi);
173 curr_vals[I_PHI_ARG_CNT] += arity;
174 if (arity > MAX_ARITY)
175 curr_vals[I_PHI_ARITY_E]++;
177 curr_vals[I_PHI_ARITY_S + arity]++;
179 phi_bl = get_nodes_block(phi);
180 /* type of argument {self, const, pred, glob} */
181 for (i = 0; i < arity; i++) {
182 ir_node *block_of_arg, *block_ith_pred;
183 ir_node *arg = get_irn_n(phi, i);
186 curr_vals[I_PHI_ARG_SELF]++;
190 if (iro_Const == get_irn_opcode(arg)) {
191 curr_vals[I_PHI_ARG_CONST]++;
195 /* get the pred block skipping blocks on critical edges */
196 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
197 if (was_edge_critical(phi_bl, i))
198 block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
200 block_of_arg = get_nodes_block(arg);
201 if (block_of_arg == block_ith_pred) {
202 curr_vals[I_PHI_ARG_PRED]++;
206 curr_vals[I_PHI_ARG_GLOB]++;
211 * Collect register-constrained node data
213 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
214 curr_vals[I_CPY_CNT]++;
215 curr_vals[I_COPIES_MAX]++;
216 if (nodes_interfere(chordal_env, root, get_Copy_src(root))) {
217 curr_vals[I_COPIES_IF]++;
218 assert(0 && "A Perm pair (in/out) should never interfere!");
223 * Collect phi class data
225 static void stat_phi_class(be_chordal_env_t *chordal_env, pset *pc) {
226 int i, o, size, if_free, phis;
227 ir_node **members, *p;
229 /* phi class count */
230 curr_vals[I_CLS_CNT]++;
233 size = pset_count(pc);
234 if (size > MAX_CLS_SIZE)
235 curr_vals[I_CLS_SIZE_E]++;
237 curr_vals[I_CLS_SIZE_S + size]++;
239 /* get an array of all members for double iterating */
240 members = xmalloc(size * sizeof(*members));
241 DBG((dbg, LEVEL_2, "Phi-class:\n"));
242 for (i = 0, p = pset_first(pc); p; p = pset_next(pc)) {
243 DBG((dbg, LEVEL_2, " %+F\n", p));
248 /* determine number of phis on this class */
250 for (i = 0; i < size; ++i)
251 if (is_Phi(members[i]))
253 if (phis > MAX_CLS_PHIS)
254 curr_vals[I_CLS_PHIS_E]++;
256 curr_vals[I_CLS_PHIS_S + phis]++;
258 /* determine interference of phi class members */
259 curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
261 for (i = 0; i < size-1; ++i)
262 for (o = i+1; o < size; ++o)
263 if (nodes_interfere(chordal_env, members[i], members[o])) {
265 curr_vals[I_CLS_IF_CNT]++;
268 /* Does this phi class have an inner interference? */
269 curr_vals[I_CLS_IF_FREE] += if_free;
274 #define is_curr_reg_class(irn) \
275 (arch_get_irn_reg_class(chordal_env->session_env->main_env->arch_env, irn, \
276 arch_pos_make_out(0)) == chordal_env->cls)
278 void copystat_collect_cls(be_chordal_env_t *chordal_env) {
281 ir_graph *irg = chordal_env->session_env->irg;
283 if (last_irg != irg) {
285 copystat_collect_irg(irg, chordal_env->session_env->main_env->arch_env);
288 for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
289 if (is_curr_reg_class(n))
290 stat_phi_node(chordal_env, n);
292 for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
293 if (is_curr_reg_class(n))
294 stat_copy_node(chordal_env, n);
296 for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
297 ir_node *member = pset_first(pc);
299 if (is_curr_reg_class(member))
300 stat_phi_class(chordal_env, pc);
304 void copystat_add_max_costs(int costs) {
305 curr_vals[I_COPIES_MAX] += costs;
307 void copystat_add_inevit_costs(int costs) {
308 curr_vals[I_COPIES_IF] += costs;
310 void copystat_add_init_costs(int costs) {
311 curr_vals[I_COPIES_INIT] += costs;
313 void copystat_add_heur_costs(int costs) {
314 curr_vals[I_COPIES_HEUR] += costs;
316 void copystat_add_ilp_n_sec_costs(int costs) {
317 curr_vals[I_COPIES_NSEC] += costs;
319 void copystat_add_opt_costs(int costs) {
320 curr_vals[I_COPIES_OPT] += costs;
322 void copystat_add_heur_time(int time) {
323 curr_vals[I_HEUR_TIME] += time;
325 void copystat_add_ilp_time(int time) {
326 curr_vals[I_ILP_TIME] += time;
328 void copystat_add_ilp_vars(int vars) {
329 curr_vals[I_ILP_VARS] += vars;
331 void copystat_add_ilp_csts(int csts) {
332 curr_vals[I_ILP_CSTR] += csts;
334 void copystat_add_ilp_iter(int iters) {
335 curr_vals[I_ILP_ITER] += iters;
338 void copystat_dump(ir_graph *irg) {
343 snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
344 out = ffopen(buf, "stat", "wt");
346 fprintf(out, "%d\n", ASIZE);
347 for (i = 0; i < ASIZE; i++) {
349 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
350 fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
351 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
352 fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
355 fprintf(out, "%i\n", curr_vals[i]);
361 void copystat_dump_pretty(ir_graph *irg) {
366 snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
367 out = ffopen(buf, "pstat", "wt");
369 fprintf(out, "Nodes %4d\n", curr_vals[I_ALL_NODES]);
370 fprintf(out, "Blocks %4d\n", curr_vals[I_BLOCKS]);
371 fprintf(out, "CopyIrn %4d\n", curr_vals[I_CPY_CNT]);
373 fprintf(out, "\nPhis %4d\n", curr_vals[I_PHI_CNT]);
374 fprintf(out, "... argument types\n");
375 fprintf(out, " Total %4d\n", curr_vals[I_PHI_ARG_CNT]);
376 fprintf(out, " Self %4d\n", curr_vals[I_PHI_ARG_SELF]);
377 fprintf(out, " Constants %4d\n", curr_vals[I_PHI_ARG_CONST]);
378 fprintf(out, " CF-Pred %4d\n", curr_vals[I_PHI_ARG_PRED]);
379 fprintf(out, " Others %4d\n", curr_vals[I_PHI_ARG_GLOB]);
380 fprintf(out, "... arities\n");
381 for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
382 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
384 fprintf(out, "\nPhi classes %4d\n", curr_vals[I_CLS_CNT]);
385 fprintf(out, " compl. free %4d\n", curr_vals[I_CLS_IF_FREE]);
386 fprintf(out, " inner intf. %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
387 fprintf(out, "... sizes\n");
388 for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
389 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
390 fprintf(out, "... contained phis\n");
391 for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
392 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
394 fprintf(out, "\nILP stat\n");
395 fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
396 fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
398 fprintf(out, "\nCopy stat\n");
399 fprintf(out, " Max %4d\n", curr_vals[I_COPIES_MAX]);
400 fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
401 fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
402 fprintf(out, " Opt %4d\n", curr_vals[I_COPIES_OPT]);
403 fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);