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