Before benchmarking
[libfirm] / ir / be / becopystat.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                19.04.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <string.h>
12 #include "irgraph.h"
13 #include "irprog.h"
14 #include "iredges.h"
15 #include "phiclass_t.h"
16 #include "becopyopt.h"
17 #include "becopystat.h"
18 #include "xmalloc.h"
19
20 #ifdef DO_STAT
21
22 #define DEBUG_LVL SET_LEVEL_1
23 static firm_dbg_module_t *dbg = NULL;
24
25 #define MAX_ARITY 10
26 #define MAX_CLS_SIZE 10
27 #define MAX_CLS_PHIS 10
28
29 /**
30  * For an explanation of these values see the code of copystat_dump_pretty
31  */
32 enum vals_t {
33         /* FROM HERE: PROBLEM CHARACTERIZATION */
34
35         I_ALL_NODES = 0,
36         I_BLOCKS,
37
38         /* phi nodes */
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 */
45         I_PHI_ARITY_S,
46         I_PHI_ARITY_E    = I_PHI_ARITY_S+MAX_ARITY,
47
48         /* copy nodes */
49         I_CPY_CNT,                      /* number of copynodes */
50
51         /* phi classes */
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 */
56         I_CLS_SIZE_S,
57         I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
58         I_CLS_PHIS_S,
59         I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
60
61         /* FROM HERE: RESULT VLAUES */
62         /* all of them are external set */
63
64         /* ilp values */
65         I_HEUR_TIME,            /* solving time in milli seconds */
66         I_ILP_TIME,                     /* solving time in milli seconds */
67     I_ILP_VARS,
68     I_ILP_CSTR,
69         I_ILP_ITER,                     /* number of simplex iterations */
70
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 */
78
79         ASIZE
80 };
81
82 /**
83  * Holds current values. Values are added till next copystat_reset
84  */
85 int curr_vals[ASIZE];
86
87 static pset *all_phi_nodes;
88 static pset *all_phi_classes;
89 static pset *all_copy_nodes;
90 static ir_graph *last_irg;
91
92 void copystat_init(void) {
93         dbg = firm_dbg_register("ir.be.copystat");
94         firm_dbg_set_mask(dbg, DEBUG_LVL);
95
96         all_phi_nodes = pset_new_ptr_default();
97         all_phi_classes = pset_new_ptr_default();
98         all_copy_nodes = pset_new_ptr_default();
99 }
100
101 void copystat_reset(void) {
102         int i;
103         for (i = 0; i < ASIZE; ++i)
104                 curr_vals[i] = 0;
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();
111 }
112
113 /**
114  * Collect general data
115  */
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 */
119
120         if (is_Block(node)) /* count all blocks */
121                 curr_vals[I_BLOCKS]++;
122
123         if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) /* collect phis */
124                 pset_insert_ptr(all_phi_nodes, node);
125
126         if (is_Copy(arch_env, node))
127                 pset_insert_ptr(all_copy_nodes, node);
128 }
129
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);
133         last_irg = irg;
134 }
135
136 /**
137  * @return 1 if the block at pos @p pos removed a critical edge
138  *                 0 else
139  */
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));
144
145         /* Does bl have several predecessors ?*/
146         if (get_irn_arity(bl) <= 1)
147                 return 0;
148
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)
152                 return 0;
153
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;
158 }
159
160 /**
161  * Collect phi node data
162  */
163 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
164         int arity, i;
165         ir_node *phi_bl;
166         assert(is_Phi(phi));
167
168         /* count all phi phis */
169         curr_vals[I_PHI_CNT]++;
170
171         /* argument count */
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]++;
176         else
177                 curr_vals[I_PHI_ARITY_S + arity]++;
178
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);
184
185                 if (arg == phi) {
186                         curr_vals[I_PHI_ARG_SELF]++;
187                         continue;
188                 }
189
190                 if (iro_Const == get_irn_opcode(arg)) {
191                         curr_vals[I_PHI_ARG_CONST]++;
192                         continue;
193                 }
194
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);
199
200                 block_of_arg = get_nodes_block(arg);
201                 if (block_of_arg == block_ith_pred) {
202                         curr_vals[I_PHI_ARG_PRED]++;
203                         continue;
204                 }
205
206                 curr_vals[I_PHI_ARG_GLOB]++;
207         }
208 }
209
210 /**
211  * Collect register-constrained node data
212  */
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!");
219         }
220 }
221
222 /**
223  * Collect phi class data
224  */
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;
228
229         /* phi class count */
230         curr_vals[I_CLS_CNT]++;
231
232         /* phi class size */
233         size = pset_count(pc);
234         if (size > MAX_CLS_SIZE)
235                 curr_vals[I_CLS_SIZE_E]++;
236         else
237                 curr_vals[I_CLS_SIZE_S + size]++;
238
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));
244                 members[i++] = p;
245         }
246         assert(i == size);
247
248         /* determine number of phis on this class */
249         phis = 0;
250         for (i = 0; i < size; ++i)
251                 if (is_Phi(members[i]))
252                         phis++;
253         if (phis > MAX_CLS_PHIS)
254                 curr_vals[I_CLS_PHIS_E]++;
255         else
256                 curr_vals[I_CLS_PHIS_S + phis]++;
257
258         /* determine interference of phi class members */
259         curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
260         if_free = 1;
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])) {
264                                 if_free = 0;
265                                 curr_vals[I_CLS_IF_CNT]++;
266                         }
267
268         /* Does this phi class have an inner interference? */
269         curr_vals[I_CLS_IF_FREE] += if_free;
270
271         xfree(members);
272 }
273
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)
277
278 void copystat_collect_cls(be_chordal_env_t *chordal_env) {
279         ir_node *n;
280         pset *pc;
281         ir_graph *irg = chordal_env->session_env->irg;
282
283         if (last_irg != irg) {
284                 copystat_reset();
285                 copystat_collect_irg(irg, chordal_env->session_env->main_env->arch_env);
286         }
287
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);
291
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);
295
296         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
297                 ir_node *member = pset_first(pc);
298                 pset_break(pc);
299                 if (is_curr_reg_class(member))
300                         stat_phi_class(chordal_env, pc);
301         }
302 }
303
304 void copystat_add_max_costs(int costs) {
305         curr_vals[I_COPIES_MAX] += costs;
306 }
307 void copystat_add_inevit_costs(int costs) {
308         curr_vals[I_COPIES_IF] += costs;
309 }
310 void copystat_add_init_costs(int costs) {
311         curr_vals[I_COPIES_INIT] += costs;
312 }
313 void copystat_add_heur_costs(int costs) {
314         curr_vals[I_COPIES_HEUR] += costs;
315 }
316 void copystat_add_ilp_n_sec_costs(int costs) {
317         curr_vals[I_COPIES_NSEC] += costs;
318 }
319 void copystat_add_opt_costs(int costs) {
320         curr_vals[I_COPIES_OPT] += costs;
321 }
322 void copystat_add_heur_time(int time) {
323         curr_vals[I_HEUR_TIME] += time;
324 }
325 void copystat_add_ilp_time(int time) {
326         curr_vals[I_ILP_TIME] += time;
327 }
328 void copystat_add_ilp_vars(int vars) {
329         curr_vals[I_ILP_VARS] += vars;
330 }
331 void copystat_add_ilp_csts(int csts) {
332         curr_vals[I_ILP_CSTR] += csts;
333 }
334 void copystat_add_ilp_iter(int iters) {
335         curr_vals[I_ILP_ITER] += iters;
336 }
337
338 void copystat_dump(ir_graph *irg) {
339         int i;
340         char buf[1024];
341         FILE *out;
342
343         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
344         out = ffopen(buf, "stat", "wt");
345
346         fprintf(out, "%d\n", ASIZE);
347         for (i = 0; i < ASIZE; i++) {
348 #if 0
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]);
353                 else
354 #endif
355                         fprintf(out, "%i\n", curr_vals[i]);
356         }
357
358     fclose(out);
359 }
360
361 void copystat_dump_pretty(ir_graph *irg) {
362         int i;
363         char buf[1024];
364         FILE *out;
365
366         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
367         out = ffopen(buf, "pstat", "wt");
368
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]);
372
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]);
383
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]);
393
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]);
397
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]);
404
405         fclose(out);
406 }
407
408 #endif