24a1c7836f623e5f271e33a316a5349672cda1c3
[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 "phiclass_t.h"
15 #include "becopyopt.h"
16 #include "becopystat.h"
17 #include "xmalloc.h"
18
19 #ifdef DO_STAT
20
21 #define DEBUG_LVL 0 //SET_LEVEL_1
22 static firm_dbg_module_t *dbg = NULL;
23
24 static pset *all_phi_nodes;
25 static pset *all_phi_classes;
26 static pset *all_copy_nodes;
27
28 void copystat_init(void) {
29         dbg = firm_dbg_register("ir.be.copystat");
30         firm_dbg_set_mask(dbg, DEBUG_LVL);
31
32         all_phi_nodes = pset_new_ptr_default();
33         all_phi_classes = pset_new_ptr_default();
34         all_copy_nodes = pset_new_ptr_default();
35         phi_class_init();
36 }
37
38 void copystat_reset(void) {
39         int i;
40         for (i = 0; i < ASIZE; ++i)
41                 curr_vals[i] = 0;
42         del_pset(all_phi_nodes);
43         del_pset(all_phi_classes);
44         del_pset(all_copy_nodes);
45         all_phi_nodes = pset_new_ptr_default();
46         all_phi_classes = pset_new_ptr_default();
47         all_copy_nodes = pset_new_ptr_default();
48 }
49
50 /**
51  * Collect general data
52  */
53 static void irg_stat_walker(ir_node *node, void *env) {
54         arch_env_t *arch_env = env;
55         curr_vals[I_ALL_NODES]++; /* count all nodes */
56
57         if (is_Block(node)) /* count all blocks */
58                 curr_vals[I_BLOCKS]++;
59
60         if (is_Phi(node)) /* collect phis */
61                 pset_insert_ptr(all_phi_nodes, node);
62
63         if (is_Copy(arch_env, node))
64                 pset_insert_ptr(all_copy_nodes, node);
65 }
66
67 void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
68         irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
69         curr_vals[I_BLOCKS] -= 2; /* substract 2 for start and end block */
70         all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
71 }
72
73 /**
74  * Collect phi node data
75  */
76 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
77         int arity, i;
78         assert(is_Phi(phi));
79
80         /* count all phi phis */
81         curr_vals[I_PHI_CNT]++;
82
83         /* argument count */
84         arity = get_irn_arity(phi);
85         curr_vals[I_PHI_ARG_CNT] += arity;
86         if (arity > MAX_ARITY)
87                 curr_vals[I_PHI_ARITY_E]++;
88         else
89                 curr_vals[I_PHI_ARITY_S + arity]++;
90
91         /* type of argument {self, const, pred, glob} */
92         for (i = 0; i < arity; i++) {
93         ir_node *block_of_arg, *block_ith_pred;
94                 ir_node *arg = get_irn_n(phi, i);
95
96                 if (phi != arg) {
97                         curr_vals[I_COPIES_MAX]++; /* if arg!=phi this is a possible copy */
98                         if (nodes_interfere(chordal_env, phi, arg)) {
99                                 DBG((dbg, LEVEL_1, "%e -- In Block %N: %n %N %n %N\n",
100               get_irg_entity(chordal_env->session_env->irg), get_nodes_block(phi), phi, phi, arg, arg));
101                                 curr_vals[I_COPIES_IF]++;
102                         }
103                 }
104
105                 if (arg == phi) {
106                         curr_vals[I_PHI_ARG_SELF]++;
107                         continue;
108                 }
109
110                 if (iro_Const == get_irn_opcode(arg)) {
111                         curr_vals[I_PHI_ARG_CONST]++;
112                         continue;
113                 }
114
115                 block_of_arg = get_Block_cfgpred_block(get_nodes_block(phi), i);
116                 block_ith_pred = get_nodes_block(get_irn_n(get_nodes_block(phi), i));
117                 if (block_of_arg == block_ith_pred) {
118                         curr_vals[I_PHI_ARG_PRED]++;
119                         continue;
120                 }
121
122                 curr_vals[I_PHI_ARG_GLOB]++;
123         }
124 }
125
126 /**
127  * Collect register-constrained node data
128  */
129 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
130         curr_vals[I_CPY_CNT]++;
131         curr_vals[I_COPIES_MAX]++;
132         if (nodes_interfere(chordal_env, root, get_Copy_src(root))) {
133                 curr_vals[I_COPIES_IF]++;
134                 assert(0 && "A Perm pair (in/out) should never interfere!");
135         }
136 }
137
138 /**
139  * Collect phi class data
140  */
141 static void stat_phi_class(be_chordal_env_t *chordal_env, pset *pc) {
142         int i, o, size, if_free, phis;
143         ir_node **members, *p;
144
145         /* phi class count */
146         curr_vals[I_CLS_CNT]++;
147
148         /* phi class size */
149         size = pset_count(pc);
150         if (size > MAX_CLS_SIZE)
151                 curr_vals[I_CLS_SIZE_E]++;
152         else
153                 curr_vals[I_CLS_SIZE_S + size]++;
154
155         /* get an array of all members for double iterating */
156         members = xmalloc(size * sizeof(*members));
157         for (i = 0, p = pset_first(pc); p; p = pset_next(pc))
158                 members[i++] = p;
159         assert(i == size);
160
161         /* determine number of phis on this class */
162         phis = 0;
163         for (i = 0; i < size-1; ++i)
164                 if (is_Phi(members[i]))
165                         phis++;
166         if (phis > MAX_CLS_PHIS)
167                 curr_vals[I_CLS_PHIS_E]++;
168         else
169                 curr_vals[I_CLS_PHIS_S + phis]++;
170
171         /* determine interference of phi class members */
172         curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
173         if_free = 1;
174         for (i = 0; i < size-1; ++i)
175                 for (o = i+1; o < size; ++o)
176                         if (nodes_interfere(chordal_env, members[i], members[o])) {
177                                 if_free = 0;
178                                 curr_vals[I_CLS_IF_CNT]++;
179                         }
180
181         /* Does this phi class have an inner interference? */
182         curr_vals[I_CLS_IF_FREE] += if_free;
183
184         xfree(members);
185 }
186
187 #define is_curr_reg_class(irn) \
188   (arch_get_irn_reg_class(chordal_env->session_env->main_env->arch_env, irn, \
189                           arch_pos_make_out(0)) == chordal_env->cls)
190
191 void copystat_collect_cls(be_chordal_env_t *chordal_env) {
192         ir_node *n;
193         pset *pc;
194
195         for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
196                 if (is_curr_reg_class(n))
197                         stat_phi_node(chordal_env, n);
198
199         for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
200                 if (is_curr_reg_class(n))
201                         stat_copy_node(chordal_env, n);
202
203         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
204                 ir_node *member = pset_first(pc);
205                 pset_break(pc);
206                 if (is_curr_reg_class(member))
207                         stat_phi_class(chordal_env, pc);
208         }
209 }
210
211 void copystat_dump(ir_graph *irg) {
212         int i;
213         char buf[1024];
214         FILE *out;
215
216         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
217         out = ffopen(buf, "stat", "wt");
218
219         fprintf(out, "%s\n", get_irp_prog_name());
220         for (i = 0; i < ASIZE; i++) {
221                 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
222                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
223                 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
224                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
225                 else
226                         fprintf(out, "%i\n", curr_vals[i]);
227         }
228
229     fclose(out);
230 }
231
232 void copystat_dump_pretty(ir_graph *irg) {
233         int i;
234         char buf[1024];
235         FILE *out;
236
237         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
238         out = ffopen(buf, "pstat", "wt");
239
240         fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
241         fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
242         fprintf(out, "CopyIrn   %4d\n", curr_vals[I_CPY_CNT]);
243
244         fprintf(out, "\nPhis      %4d\n", curr_vals[I_PHI_CNT]);
245         fprintf(out, "... argument types\n");
246         fprintf(out, " Total      %4d\n", curr_vals[I_PHI_ARG_CNT]);
247         fprintf(out, " Self       %4d\n", curr_vals[I_PHI_ARG_SELF]);
248         fprintf(out, " Constants  %4d\n", curr_vals[I_PHI_ARG_CONST]);
249         fprintf(out, " CF-Pred    %4d\n", curr_vals[I_PHI_ARG_PRED]);
250         fprintf(out, " Others     %4d\n", curr_vals[I_PHI_ARG_GLOB]);
251         fprintf(out, "... arities\n");
252         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
253                 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
254
255         fprintf(out, "\nPhi classes   %4d\n", curr_vals[I_CLS_CNT]);
256         fprintf(out, " compl. free  %4d\n", curr_vals[I_CLS_IF_FREE]);
257         fprintf(out, " inner intf.  %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
258         fprintf(out, "... sizes\n");
259         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
260                 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
261         fprintf(out, "... contained phis\n");
262         for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
263                 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
264
265         fprintf(out, "\nILP stat\n");
266         fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
267         fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
268
269         fprintf(out, "\nCopy stat\n");
270         fprintf(out, " Max  %4d\n", curr_vals[I_COPIES_MAX]);
271         fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
272         fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
273         fprintf(out, " Opt  %4d\n", curr_vals[I_COPIES_OPT]);
274         fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
275
276         fclose(out);
277 }
278
279 #endif