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