d21a06f88a0ac0b566920a5ee844446f0e59e8ef
[libfirm] / ir / be / becopystat.c
1 /**
2  * @author Daniel Grund
3  * @date 19.04.2005
4  */
5 #include <string.h>
6 #include "phiclass_t.h"
7 #include "irprog.h"
8 #include "becopyopt.h"
9 #include "becopystat.h"
10
11 #ifdef DO_STAT
12
13 static pset *all_phi_nodes;
14 static pset *all_phi_classes;
15 static pset *all_copy_nodes;
16
17 void stat_init(void) {
18         all_phi_nodes = pset_new_ptr_default();
19         all_phi_classes = pset_new_ptr_default();
20         all_copy_nodes = pset_new_ptr_default();
21         phi_class_init();
22 }
23
24 void stat_reset(void) {
25         int i;
26         for (i = 0; i < ASIZE; ++i)
27                 curr_vals[i] = 0;
28 }
29
30 /**
31  * Collect general data
32  */
33 static void stat_walker(ir_node *node, void *env) {
34         curr_vals[I_ALL_NODES]++; /* count all nodes */
35
36         if (is_Block(node)) /* count all blocks */
37                 curr_vals[I_BLOCKS]++;
38
39         if (is_Phi(node)) /* collect phis */
40                 pset_insert_ptr(all_phi_nodes, node);
41
42         if (is_Copy(node))
43                 pset_insert_ptr(all_copy_nodes, node);
44 }
45
46 /**
47  * Collect phi node data
48  */
49 static void stat_phi_node(ir_node *phi) {
50         int arity, i;
51
52         /* count all phi phis */
53         curr_vals[I_PHI_CNT]++;
54
55         /* argument count */
56         arity = get_irn_arity(phi);
57         curr_vals[I_PHI_ARG_CNT] += arity;
58         if (arity > MAX_ARITY)
59                 curr_vals[I_PHI_ARITY_E]++;
60         else
61                 curr_vals[I_PHI_ARITY_S + arity]++;
62
63         /* type of argument {self, const, pred, glob} */
64         for (i = 0; i < arity; i++) {
65         ir_node *block_of_arg, *block_ith_pred;
66                 ir_node *arg = get_irn_n(phi, i);
67
68                 if (arg == phi) {
69                         curr_vals[I_PHI_ARG_SELF]++;
70                         continue;
71                 }
72
73                 curr_vals[I_COPIES_MAX]++; /* if arg!=phi this is a possible copy */
74
75                 if (values_interfere(phi, arg))
76                         curr_vals[I_COPIES_IF]++;
77
78                 if (iro_Const == get_irn_opcode(arg)) {
79                         curr_vals[I_PHI_ARG_CONST]++;
80                         continue;
81                 }
82
83                 block_of_arg = get_nodes_block(arg);
84                 block_ith_pred = get_nodes_block(get_irn_n(get_nodes_block(phi), i));
85                 if (block_of_arg == block_ith_pred) {
86                         curr_vals[I_PHI_ARG_PRED]++;
87                         continue;
88                 }
89
90                 curr_vals[I_PHI_ARG_GLOB]++;
91         }
92 }
93
94 /**
95  * Collect register-constrained node data
96  */
97 //TODO stat_copy_node
98 static void stat_copy_node(ir_node *root) {
99         curr_vals[I_CPY_CNT]++;
100 //      if (values_interfere(root, arg))
101 //              curr_vals[I_COPIES_IF]++;
102 }
103
104 /**
105  * Collect phi class data
106  */
107 static void stat_phi_class(pset *pc) {
108         int i, o, size, if_free;
109         ir_node **members, *p;
110
111         /* phi class count */
112         curr_vals[I_CLS_CNT]++;
113
114         /* phi class size */
115         size = pset_count(pc);
116         if (size > MAX_CLS_SIZE)
117                 curr_vals[I_CLS_SIZE_E]++;
118         else
119                 curr_vals[I_CLS_SIZE_S + size]++;
120
121         /* get an array of all members for double iterating */
122         members = malloc(size * sizeof(*members));
123         for (i = 0, p = pset_first(pc); p; p = pset_next(pc))
124                 members[i++] = p;
125         assert(i == size);
126
127         /* determine interference of phi class members */
128         curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
129         if_free = 1;
130         for (i = 0; i < size-1; ++i)
131                 for (o = i+1; o < size; ++o)
132                         if (values_interfere(members[i], members[o])) {
133                                 if_free = 0;
134                                 curr_vals[I_CLS_IF_CNT]++;
135                         }
136
137         /* Does this phi class have an inner interference? */
138         curr_vals[I_CLS_IF_FREE] += if_free;
139
140         free(members);
141 }
142
143 void stat_collect_irg(ir_graph *irg) {
144         ir_node *n;
145         pset *pc;
146
147         irg_walk_graph(irg, stat_walker, NULL, NULL);
148         curr_vals[I_BLOCKS] -= 2; /* substract 2 for start and end block */
149
150         all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
151
152         for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
153                 stat_phi_node(n);
154
155         for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
156                 stat_copy_node(n);
157
158         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes))
159                 stat_phi_class(pc);
160
161 }
162
163 void stat_dump(ir_graph *irg) {
164         int i;
165         char buf[1024];
166
167         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
168         FILE *out = ffopen(buf, "stat", "wt");
169
170         fprintf(out, "%s\n", get_irp_prog_name());
171         for (i = 0; i < ASIZE; i++) {
172                 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
173                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
174                 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
175                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
176                 else
177                         fprintf(out, "%i\n", curr_vals[i]);
178         }
179
180     fclose(out);
181 }
182
183 //TODO stat_dump_pretty
184 void stat_dump_pretty(ir_graph *irg) {
185         int i;
186         FILE *out = ffopen(get_entity_name(get_irg_entity(irg)), "pretty", "wt");
187
188         fprintf(out, "\nPhi argument types\n");
189         fprintf(out, "Total     %4d\n", curr_vals[I_PHI_ARG_CNT]);
190         fprintf(out, "Constants %4d\n", curr_vals[I_PHI_ARG_CONST]);
191         fprintf(out, "CF-Pred   %4d\n", curr_vals[I_PHI_ARG_PRED]);
192         fprintf(out, "Others    %4d\n", curr_vals[I_PHI_ARG_GLOB]);
193
194         fprintf(out, "\nPhi class interference\n");
195         fprintf(out, "Blocks         %4d\n", curr_vals[I_BLOCKS]);
196         fprintf(out, "Phis           %4d\n", curr_vals[I_PHI_CNT]);
197
198         fprintf(out, "\nPhi arity\n");
199         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
200                 fprintf(out, "%2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
201
202         fprintf(out, "\nPhi class sizes\n");
203         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
204                 fprintf(out, "%2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
205
206         fprintf(out, "\n\nTotal nodes:    %4d\n", curr_vals[I_ALL_NODES]);
207
208         fclose(out);
209 }
210
211 #endif