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