2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Copy node statistics.
20 #include "iredges_t.h"
21 #include "irnodeset.h"
23 #include "bechordal_t.h"
26 #include "becopyopt_t.h"
27 #include "becopystat.h"
29 #include "beintlive_t.h"
31 #define DEBUG_LVL SET_LEVEL_1
32 DEBUG_ONLY(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
95 static int curr_vals[ASIZE];
97 static ir_nodeset_t *all_phi_nodes;
98 static ir_nodeset_t *all_copy_nodes;
100 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat)
101 void be_init_copystat(void)
103 FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
105 all_phi_nodes = ir_nodeset_new(64);
106 all_copy_nodes = ir_nodeset_new(64);
107 memset(curr_vals, 0, sizeof(curr_vals));
110 BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat)
111 void be_quit_copystat(void)
113 if (all_phi_nodes != NULL) {
114 ir_nodeset_del(all_phi_nodes);
115 all_phi_nodes = NULL;
117 if (all_copy_nodes != NULL) {
118 ir_nodeset_del(all_copy_nodes);
119 all_copy_nodes = NULL;
124 * @return 1 if the block at pos @p pos removed a critical edge
127 static inline int was_edge_critical(const ir_node *bl, int pos)
129 const ir_edge_t *edge;
130 const ir_node *bl_at_pos, *bl_before;
132 /* Does bl have several predecessors ?*/
133 if (get_Block_n_cfgpreds(bl) <= 1)
136 /* Does the pred have exactly one predecessor */
137 bl_at_pos = get_irn_n(bl, pos);
138 if (get_irn_arity(bl_at_pos) != 1)
141 /* Does the pred of the pred have several successors */
142 bl_before = get_irn_n(bl_at_pos, 0);
143 edge = get_block_succ_first(bl_before);
144 return get_block_succ_next(bl_before, edge) ? 1 : 0;
147 void copystat_add_max_costs(int costs)
149 curr_vals[I_COPIES_MAX] += costs;
151 void copystat_add_inevit_costs(int costs)
153 curr_vals[I_COPIES_IF] += costs;
155 void copystat_add_init_costs(int costs)
157 curr_vals[I_COPIES_INIT] += costs;
159 void copystat_add_heur_costs(int costs)
161 curr_vals[I_COPIES_HEUR] += costs;
163 void copystat_add_opt_costs(int costs)
165 curr_vals[I_COPIES_OPT] += costs;
167 void copystat_add_heur_time(int time)
169 curr_vals[I_HEUR_TIME] += time;
172 void copystat_add_ilp_5_sec_costs(int costs)
174 curr_vals[I_COPIES_5SEC] += costs;
176 void copystat_add_ilp_30_sec_costs(int costs)
178 curr_vals[I_COPIES_30SEC] += costs;
180 void copystat_add_ilp_time(int time)
182 curr_vals[I_ILP_TIME] += time;
184 void copystat_add_ilp_vars(int vars)
186 curr_vals[I_ILP_VARS] += vars;
188 void copystat_add_ilp_csts(int csts)
190 curr_vals[I_ILP_CSTR] += csts;
192 void copystat_add_ilp_iter(int iters)
194 curr_vals[I_ILP_ITER] += iters;
198 * Opens a file named base.ext with the mode mode.
200 static FILE *be_ffopen(const char *base, const char *ext, const char *mode)
205 snprintf(buf, sizeof(buf), "%s.%s", base, ext);
206 buf[sizeof(buf) - 1] = '\0';
207 if (! (out = fopen(buf, mode))) {
208 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
214 void copystat_dump(ir_graph *irg)
220 snprintf(buf, sizeof(buf), "%s__%s", get_irp_name(), get_entity_name(get_irg_entity(irg)));
221 buf[sizeof(buf) - 1] = '\0';
222 out = be_ffopen(buf, "stat", "wt");
224 fprintf(out, "%d\n", (int)ASIZE);
225 for (i = 0; i < ASIZE; i++) {
226 fprintf(out, "%i\n", curr_vals[i]);
232 void copystat_dump_pretty(ir_graph *irg)
238 snprintf(buf, sizeof(buf), "%s__%s", get_irp_name(), get_entity_name(get_irg_entity(irg)));
239 buf[sizeof(buf) - 1] = '\0';
240 out = be_ffopen(buf, "pstat", "wt");
242 fprintf(out, "Nodes %4d\n", curr_vals[I_ALL_NODES]);
243 fprintf(out, "Blocks %4d\n", curr_vals[I_BLOCKS]);
244 fprintf(out, "CopyIrn %4d\n", curr_vals[I_CPY_CNT]);
246 fprintf(out, "\nPhis %4d\n", curr_vals[I_PHI_CNT]);
247 fprintf(out, "... argument types\n");
248 fprintf(out, " Total %4d\n", curr_vals[I_PHI_ARG_CNT]);
249 fprintf(out, " Self %4d\n", curr_vals[I_PHI_ARG_SELF]);
250 fprintf(out, " Constants %4d\n", curr_vals[I_PHI_ARG_CONST]);
251 fprintf(out, " CF-Pred %4d\n", curr_vals[I_PHI_ARG_PRED]);
252 fprintf(out, " Others %4d\n", curr_vals[I_PHI_ARG_GLOB]);
253 fprintf(out, "... arities\n");
254 for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
255 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
257 fprintf(out, "\nPhi classes %4d\n", curr_vals[I_CLS_CNT]);
258 fprintf(out, " compl. free %4d\n", curr_vals[I_CLS_IF_FREE]);
259 fprintf(out, " inner intf. %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
260 fprintf(out, "... sizes\n");
261 for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
262 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
263 fprintf(out, "... contained phis\n");
264 for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
265 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
267 fprintf(out, "\nILP stat\n");
268 fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
269 fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
271 fprintf(out, "\nCopy stat\n");
272 fprintf(out, " Max %4d\n", curr_vals[I_COPIES_MAX]);
273 fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
274 fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
275 fprintf(out, " Opt %4d\n", curr_vals[I_COPIES_OPT]);
276 fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);