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