*** empty log message ***
[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_5SEC,          /* number of copies after ilp with max n sec */
76         I_COPIES_30SEC,         /* number of copies after ilp with max n sec */
77         I_COPIES_OPT,           /* number of copies after ilp */
78         I_COPIES_IF,            /* number of copies inevitable due to root-arg-interf */
79
80         ASIZE
81 };
82
83 /**
84  * Holds current values. Values are added till next copystat_reset
85  */
86 int curr_vals[ASIZE];
87
88 static pset *all_phi_nodes;
89 static pset *all_phi_classes;
90 static pset *all_copy_nodes;
91 static ir_graph *last_irg;
92
93 void copystat_init(void) {
94         dbg = firm_dbg_register("ir.be.copystat");
95         firm_dbg_set_mask(dbg, DEBUG_LVL);
96
97         all_phi_nodes = pset_new_ptr_default();
98         all_phi_classes = pset_new_ptr_default();
99         all_copy_nodes = pset_new_ptr_default();
100 }
101
102 void copystat_reset(void) {
103         int i;
104         for (i = 0; i < ASIZE; ++i)
105                 curr_vals[i] = 0;
106         del_pset(all_phi_nodes);
107         del_pset(all_phi_classes);
108         del_pset(all_copy_nodes);
109         all_phi_nodes = pset_new_ptr_default();
110         all_phi_classes = pset_new_ptr_default();
111         all_copy_nodes = pset_new_ptr_default();
112 }
113
114 /**
115  * Collect general data
116  */
117 static void irg_stat_walker(ir_node *node, void *env) {
118         arch_env_t *arch_env = env;
119         curr_vals[I_ALL_NODES]++; /* count all nodes */
120
121         if (is_Block(node)) /* count all blocks */
122                 curr_vals[I_BLOCKS]++;
123
124         if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) /* collect phis */
125                 pset_insert_ptr(all_phi_nodes, node);
126
127         if (is_Copy(arch_env, node))
128                 pset_insert_ptr(all_copy_nodes, node);
129 }
130
131 static void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
132         irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
133         all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
134         last_irg = irg;
135 }
136
137 /**
138  * @return 1 if the block at pos @p pos removed a critical edge
139  *                 0 else
140  */
141 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
142         const ir_edge_t *edge;
143         const ir_node *bl_at_pos, *bl_before;
144         assert(is_Block(bl));
145
146         /* Does bl have several predecessors ?*/
147         if (get_irn_arity(bl) <= 1)
148                 return 0;
149
150         /* Does the pred have exactly one predecessor */
151         bl_at_pos = get_irn_n(bl, pos);
152         if (get_irn_arity(bl_at_pos) != 1)
153                 return 0;
154
155         /* Does the pred of the pred have several sucsecessors */
156         bl_before = get_irn_n(bl_at_pos, 0);
157         edge = get_block_succ_first(bl_before);
158         return get_block_succ_next(bl_before, edge) ? 1 : 0;
159 }
160
161 /**
162  * Collect phi node data
163  */
164 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
165         int arity, i;
166         ir_node *phi_bl;
167         assert(is_Phi(phi));
168
169         /* count all phi phis */
170         curr_vals[I_PHI_CNT]++;
171
172         /* argument count */
173         arity = get_irn_arity(phi);
174         curr_vals[I_PHI_ARG_CNT] += arity;
175         if (arity > MAX_ARITY)
176                 curr_vals[I_PHI_ARITY_E]++;
177         else
178                 curr_vals[I_PHI_ARITY_S + arity]++;
179
180         phi_bl = get_nodes_block(phi);
181         /* type of argument {self, const, pred, glob} */
182         for (i = 0; i < arity; i++) {
183         ir_node *block_of_arg, *block_ith_pred;
184                 ir_node *arg = get_irn_n(phi, i);
185
186                 if (arg == phi) {
187                         curr_vals[I_PHI_ARG_SELF]++;
188                         continue;
189                 }
190
191                 if (iro_Const == get_irn_opcode(arg)) {
192                         curr_vals[I_PHI_ARG_CONST]++;
193                         continue;
194                 }
195
196                 /* get the pred block skipping blocks on critical edges */
197                 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
198                 if (was_edge_critical(phi_bl, i))
199                         block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
200
201                 block_of_arg = get_nodes_block(arg);
202                 if (block_of_arg == block_ith_pred) {
203                         curr_vals[I_PHI_ARG_PRED]++;
204                         continue;
205                 }
206
207                 curr_vals[I_PHI_ARG_GLOB]++;
208         }
209 }
210
211 /**
212  * Collect register-constrained node data
213  */
214 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
215         curr_vals[I_CPY_CNT]++;
216         curr_vals[I_COPIES_MAX]++;
217         if (nodes_interfere(chordal_env, root, get_Copy_src(root))) {
218                 curr_vals[I_COPIES_IF]++;
219                 assert(0 && "A Perm pair (in/out) should never interfere!");
220         }
221 }
222
223 /**
224  * Collect phi class data
225  */
226 static void stat_phi_class(be_chordal_env_t *chordal_env, pset *pc) {
227         int i, o, size, if_free, phis;
228         ir_node **members, *p;
229
230         /* phi class count */
231         curr_vals[I_CLS_CNT]++;
232
233         /* phi class size */
234         size = pset_count(pc);
235         if (size > MAX_CLS_SIZE)
236                 curr_vals[I_CLS_SIZE_E]++;
237         else
238                 curr_vals[I_CLS_SIZE_S + size]++;
239
240         /* get an array of all members for double iterating */
241         members = xmalloc(size * sizeof(*members));
242         DBG((dbg, LEVEL_2, "Phi-class:\n"));
243         for (i = 0, p = pset_first(pc); p; p = pset_next(pc)) {
244                 DBG((dbg, LEVEL_2, "  %+F\n", p));
245                 members[i++] = p;
246         }
247         assert(i == size);
248
249         /* determine number of phis on this class */
250         phis = 0;
251         for (i = 0; i < size; ++i)
252                 if (is_Phi(members[i]))
253                         phis++;
254         if (phis > MAX_CLS_PHIS)
255                 curr_vals[I_CLS_PHIS_E]++;
256         else
257                 curr_vals[I_CLS_PHIS_S + phis]++;
258
259         /* determine interference of phi class members */
260         curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
261         if_free = 1;
262         for (i = 0; i < size-1; ++i)
263                 for (o = i+1; o < size; ++o)
264                         if (nodes_interfere(chordal_env, members[i], members[o])) {
265                                 if_free = 0;
266                                 curr_vals[I_CLS_IF_CNT]++;
267                         }
268
269         /* Does this phi class have an inner interference? */
270         curr_vals[I_CLS_IF_FREE] += if_free;
271
272         xfree(members);
273 }
274
275 #define is_curr_reg_class(irn) \
276   (arch_get_irn_reg_class(chordal_env->session_env->main_env->arch_env, irn, \
277                           arch_pos_make_out(0)) == chordal_env->cls)
278
279 void copystat_collect_cls(be_chordal_env_t *chordal_env) {
280         ir_node *n;
281         pset *pc;
282         ir_graph *irg = chordal_env->session_env->irg;
283
284         if (last_irg != irg) {
285                 copystat_reset();
286                 copystat_collect_irg(irg, chordal_env->session_env->main_env->arch_env);
287         }
288
289         for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
290                 if (is_curr_reg_class(n))
291                         stat_phi_node(chordal_env, n);
292
293         for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
294                 if (is_curr_reg_class(n))
295                         stat_copy_node(chordal_env, n);
296
297         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
298                 ir_node *member = pset_first(pc);
299                 pset_break(pc);
300                 if (is_curr_reg_class(member))
301                         stat_phi_class(chordal_env, pc);
302         }
303 }
304
305 void copystat_add_max_costs(int costs) {
306         curr_vals[I_COPIES_MAX] += costs;
307 }
308 void copystat_add_inevit_costs(int costs) {
309         curr_vals[I_COPIES_IF] += costs;
310 }
311 void copystat_add_init_costs(int costs) {
312         curr_vals[I_COPIES_INIT] += costs;
313 }
314 void copystat_add_heur_costs(int costs) {
315         curr_vals[I_COPIES_HEUR] += costs;
316 }
317 void copystat_add_ilp_5_sec_costs(int costs) {
318         curr_vals[I_COPIES_5SEC] += costs;
319 }
320 void copystat_add_ilp_30_sec_costs(int costs) {
321         curr_vals[I_COPIES_30SEC] += costs;
322 }
323 void copystat_add_opt_costs(int costs) {
324         curr_vals[I_COPIES_OPT] += costs;
325 }
326 void copystat_add_heur_time(int time) {
327         curr_vals[I_HEUR_TIME] += time;
328 }
329 void copystat_add_ilp_time(int time) {
330         curr_vals[I_ILP_TIME] += time;
331 }
332 void copystat_add_ilp_vars(int vars) {
333         curr_vals[I_ILP_VARS] += vars;
334 }
335 void copystat_add_ilp_csts(int csts) {
336         curr_vals[I_ILP_CSTR] += csts;
337 }
338 void copystat_add_ilp_iter(int iters) {
339         curr_vals[I_ILP_ITER] += iters;
340 }
341
342 void copystat_dump(ir_graph *irg) {
343         int i;
344         char buf[1024];
345         FILE *out;
346
347         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
348         out = ffopen(buf, "stat", "wt");
349
350         fprintf(out, "%d\n", ASIZE);
351         for (i = 0; i < ASIZE; i++) {
352 #if 0
353                 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
354                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
355                 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
356                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
357                 else
358 #endif
359                         fprintf(out, "%i\n", curr_vals[i]);
360         }
361
362     fclose(out);
363 }
364
365 void copystat_dump_pretty(ir_graph *irg) {
366         int i;
367         char buf[1024];
368         FILE *out;
369
370         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
371         out = ffopen(buf, "pstat", "wt");
372
373         fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
374         fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
375         fprintf(out, "CopyIrn   %4d\n", curr_vals[I_CPY_CNT]);
376
377         fprintf(out, "\nPhis      %4d\n", curr_vals[I_PHI_CNT]);
378         fprintf(out, "... argument types\n");
379         fprintf(out, " Total      %4d\n", curr_vals[I_PHI_ARG_CNT]);
380         fprintf(out, " Self       %4d\n", curr_vals[I_PHI_ARG_SELF]);
381         fprintf(out, " Constants  %4d\n", curr_vals[I_PHI_ARG_CONST]);
382         fprintf(out, " CF-Pred    %4d\n", curr_vals[I_PHI_ARG_PRED]);
383         fprintf(out, " Others     %4d\n", curr_vals[I_PHI_ARG_GLOB]);
384         fprintf(out, "... arities\n");
385         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
386                 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
387
388         fprintf(out, "\nPhi classes   %4d\n", curr_vals[I_CLS_CNT]);
389         fprintf(out, " compl. free  %4d\n", curr_vals[I_CLS_IF_FREE]);
390         fprintf(out, " inner intf.  %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
391         fprintf(out, "... sizes\n");
392         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
393                 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
394         fprintf(out, "... contained phis\n");
395         for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
396                 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
397
398         fprintf(out, "\nILP stat\n");
399         fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
400         fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
401
402         fprintf(out, "\nCopy stat\n");
403         fprintf(out, " Max  %4d\n", curr_vals[I_COPIES_MAX]);
404         fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
405         fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
406         fprintf(out, " Opt  %4d\n", curr_vals[I_COPIES_OPT]);
407         fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
408
409         fclose(out);
410 }
411
412 #endif