added average calculation of distribution table
[libfirm] / ir / stat / firmstat_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/stat/firmstat_t.h
4  * Purpose:     Statistics for Firm. Internal data structures.
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 2004 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11 #ifndef _FIRMSTAT_T_H_
12 #define _FIRMSTAT_T_H_
13
14 /**
15  * @file firmstat_t.h
16  */
17 #include "firmstat.h"
18
19 #include "irop_t.h"
20 #include "irnode_t.h"
21 #include "irgraph_t.h"
22 #include "pset.h"
23 #include "pdeq.h"
24 #include "irprog.h"
25 #include "irgwalk.h"
26 #include "counter.h"
27 #include "irhooks.h"
28 #include "ident.h"
29
30 /* some useful macro. */
31 #define ARR_SIZE(a)   (sizeof(a)/sizeof((a)[0]))
32
33 /*
34  * just be make some things clear :-), the
35  * poor man "generics"
36  */
37 #define HASH_MAP(type) hmap_##type
38
39 typedef pset hmap_node_entry_t;
40 typedef pset hmap_graph_entry_t;
41 typedef pset hmap_opt_entry_t;
42 typedef pset hmap_block_entry_t;
43 typedef pset hmap_be_block_entry_t;
44 typedef pset hmap_reg_pressure_entry_t;
45 typedef pset hmap_ir_op;
46 typedef pset hmap_distrib_entry_t;
47
48 /**
49  * An entry in a distribution table
50  */
51 typedef struct _distrib_entry_t {
52   counter_t      cnt;       /**< the current count */
53   const void *object;   /**< the object which is counted */
54 } distrib_entry_t;
55
56 /** The type of the hash function for objects in distribution tables. */
57 typedef unsigned (*distrib_hash_fun)(const void *object);
58
59 /**
60  * The distribution table.
61  */
62 typedef struct _distrib_tbl_t {
63   struct obstack                cnts;       /**< obstack containing the distrib_entry_t entries */
64   HASH_MAP(distrib_entry_t)     *hash_map;  /**< the hash map containing the distribution */
65   distrib_hash_fun          hash_func;  /**< the hash function for object in this distribution */
66   unsigned                  int_dist;   /**< non-zero, if it's a integer distribution */
67 } distrib_tbl_t;
68
69 /**
70  * possible address marker values
71  */
72 enum adr_marker_t {
73   MARK_ADDRESS_CALC     = 1,    /**< the node is an address expression */
74   MARK_REF_ADR          = 2,    /**< the node is referenced by an address expression */
75   MARK_REF_NON_ADR      = 4,    /**< the node is referenced by a non-address expression */
76 };
77
78 /**
79  * An entry in the address_mark set
80  */
81 typedef struct _address_mark_entry_t {
82   ir_node  *node;               /**< the node which this entry belongs to, needed for compare */
83   unsigned mark;                /**< the mark, a bitmask of enum adr_marker_t */
84 } address_mark_entry_t;
85
86 /**
87  * An entry for ir_nodes, used in ir_graph statistics.
88  */
89 typedef struct _node_entry_t {
90   counter_t   cnt_alive;    /**< amount of nodes in this entry */
91   counter_t   new_node;     /**< amount of new nodes for this entry */
92   counter_t   into_Id;      /**< amount of nodes that turned into Id's for this entry */
93   const ir_op *op;          /**< the op for this entry */
94 } node_entry_t;
95
96 enum leaf_call_state_t {
97   LCS_UNKNOWN       = 0,      /**< state is unknown yet */
98   LCS_LEAF_CALL     = 1,      /**< only leaf functions will be called */
99   LCS_NON_LEAF_CALL = 2,      /**< at least one non-leaf function will be called or indetermined */
100 };
101
102 /**
103  * An entry for ir_graphs. These numbers are calculated for every IR graph.
104  */
105 typedef struct _graph_entry_t {
106   struct obstack             recalc_cnts;                  /**< obstack containing the counters that are recalculated */
107   HASH_MAP(node_entry_t)     *opcode_hash;                 /**< hash map containing the opcode counter */
108   HASH_MAP(block_entry_t)    *block_hash;                  /**< hash map containing the block counter */
109   HASH_MAP(block_entry_t)    *extbb_hash;                  /**< hash map containing the extended block counter */
110   HASH_MAP(be_block_entry_t) *be_block_hash;               /**< hash map containing backend block information */
111   counter_t                  cnt_walked;                   /**< walker walked over the graph */
112   counter_t                  cnt_walked_blocks;            /**< walker walked over the graph blocks */
113   counter_t                  cnt_was_inlined;              /**< number of times other graph were inlined */
114   counter_t                  cnt_got_inlined;              /**< number of times this graph was inlined */
115   counter_t                  cnt_strength_red;             /**< number of times strength reduction was successful on this graph */
116   counter_t                  cnt_edges;                    /**< number of DF edges in this graph */
117   counter_t                  cnt_all_calls;                /**< number of all calls */
118   counter_t                  cnt_call_with_cnst_arg;       /**< number of calls with const args */
119   counter_t                  cnt_indirect_calls;           /**< number of indirect calls */
120   counter_t                  cnt_if_conv[IF_RESULT_LAST];  /**< number of if conversions */
121   counter_t                  cnt_real_func_call;           /**< number real function call optimization */
122   unsigned                   num_tail_recursion;           /**< number of tail recursion optimizations */
123   HASH_MAP(opt_entry_t)      *opt_hash[FS_OPT_MAX];        /**< hash maps containing opcode counter for optimizations */
124   ir_graph                   *irg;                         /**< the graph of this object */
125   entity                     *ent;                         /**< the entity of this graph if one exists */
126   set                        *address_mark;                /**< a set containing the address marks of the nodes */
127   unsigned                   is_deleted:1;                 /**< set if this irg was deleted */
128   unsigned                   is_leaf:1;                    /**< set, if this irg is a leaf function */
129   unsigned                   is_leaf_call:2;               /**< set, if this irg calls only leaf functions */
130   unsigned                   is_recursive:1;               /**< set, if this irg has recursive calls */
131   unsigned                   is_chain_call:1;              /**< set, if this irg is a chain call */
132   unsigned                   is_analyzed:1;                /**< helper: set, if this irg was already analysed */
133 } graph_entry_t;
134
135 /**
136  * An entry for optimized ir_nodes
137  */
138 typedef struct _opt_entry_t {
139   counter_t   count;    /**< optimization counter */
140   const ir_op *op;      /**< the op for this entry */
141 } opt_entry_t;
142
143 /**
144  * An entry for register pressure.
145  */
146 typedef struct _reg_pressure_entry_t {
147   ident *id_name;    /**< name of the register class */
148   int    pressure;   /**< the register pressure for this class */
149 } reg_pressure_entry_t;
150
151 /**
152  * An entry for a block or extended block in a ir-graph
153  */
154 typedef struct _be_block_entry_t {
155   long                           block_nr;      /**< block nr */
156   distrib_tbl_t                  *sched_ready;  /**< distribution of ready nodes per block */
157   /**< the highest register pressures for this block for each register class */
158   HASH_MAP(reg_pressure_entry_t) *reg_pressure;
159 } be_block_entry_t;
160
161 /**
162  * An entry for a block or extended block in a ir-graph
163  */
164 typedef struct _block_entry_t {
165   counter_t       cnt_nodes;     /**< the counter of nodes in this block */
166   counter_t       cnt_edges;     /**< the counter of edges in this block */
167   counter_t       cnt_in_edges;  /**< the counter of edges incoming from other blocks to this block */
168   counter_t       cnt_out_edges; /**< the counter of edges outgoing from this block to other blocks */
169   counter_t       cnt_phi_data;  /**< the counter of data Phi nodes in this block */
170   long            block_nr;      /**< block nr */
171 } block_entry_t;
172
173 /** An entry for an extended block in a ir-graph */
174 typedef block_entry_t extbb_entry_t;
175
176 /**
177  * Some potential interesting float values
178  */
179 typedef enum _float_classify_t {
180   STAT_FC_0,                /**< the float value 0.0 */
181   STAT_FC_1,                /**< the float value 1.0 */
182   STAT_FC_2,                /**< the float value 2.0 */
183   STAT_FC_0_5,              /**< the float value 0.5 */
184   STAT_FC_EXACT,            /**< an exact value */
185   STAT_FC_OTHER,            /**< all other values */
186   STAT_FC_MAX               /**< last value */
187 } float_classify_t;
188
189 /**
190  * constant info
191  */
192 typedef struct _constant_info_t {
193   counter_t  int_bits_count[32];  /**< distribution of bit sizes of integer constants */
194   counter_t  floats[STAT_FC_MAX]; /**< floating point constants */
195   counter_t  others;              /**< all other constants */
196 } constant_info_t;
197
198 /** forward */
199 typedef struct _dumper_t dumper_t;
200
201 /**
202  * handler for dumping an IRG
203  *
204  * @param dmp   the dumper
205  * @param entry the IR-graph hash map entry
206  */
207 typedef void (*dump_graph_FUNC)(dumper_t *dmp, graph_entry_t *entry);
208
209 /**
210  * handler for dumper init
211  *
212  * @param dmp   the dumper
213  * @param name  name of the file to dump to
214  */
215 typedef void (*dump_init_FUNC)(dumper_t *dmp, const char *name);
216
217 /**
218  * handler for dumper a constant info table
219  *
220  * @param dmp   the dumper
221  */
222 typedef void (*dump_const_table_FUNC)(dumper_t *dmp, const constant_info_t *tbl);
223
224 /**
225  * handler for dumper finish
226  *
227  * @param dmp   the dumper
228  */
229 typedef void (*dump_finish_FUNC)(dumper_t *dmp);
230
231 /**
232  * statistics info
233  */
234 typedef struct _statistic_info_t {
235   unsigned                stat_options;   /**< statistic options: field must be first */
236   struct obstack          cnts;           /**< obstack containing the counters that are incremented */
237   struct obstack          be_data;        /**< obstack containing backend statistics data */
238   HASH_MAP(graph_entry_t) *irg_hash;      /**< hash map containing the counter for irgs */
239   HASH_MAP(ir_op)         *ir_op_hash;    /**< hash map containing all ir_ops (accessible by op_codes) */
240   pdeq                    *wait_q;        /**< wait queue for leaf call decision */
241   int                     recursive;      /**< flag for detecting recursive hook calls */
242   int                     in_dead_node_elim;    /**< set, if dead node elimination runs */
243   ir_op                   *op_Phi0;       /**< pseudo op for Phi0 */
244   ir_op                   *op_PhiM;       /**< pseudo op for memory Phi */
245   ir_op                   *op_ProjM;      /**< pseudo op for memory Proj */
246   ir_op                   *op_MulC;       /**< pseudo op for multiplication by const */
247   ir_op                   *op_DivC;       /**< pseudo op for division by const */
248   ir_op                   *op_ModC;       /**< pseudo op for modulo by const */
249   ir_op                   *op_DivModC;    /**< pseudo op for DivMod by const */
250   ir_op                   *op_SelSel;     /**< pseudo op for Sel(Sel) */
251   ir_op                   *op_SelSelSel;  /**< pseudo op for Sel(Sel(Sel)) */
252   dumper_t                *dumper;        /**< list of dumper */
253   int                     reassoc_run;    /**< if set, reassociation is running */
254   constant_info_t         const_info;     /**< statistic info for constants */
255 } stat_info_t;
256
257 /**
258  * a dumper description
259  */
260 struct _dumper_t {
261   dump_graph_FUNC         dump_graph;     /**< handler for dumping an irg */
262   dump_const_table_FUNC   dump_const_tbl; /**< handler for dumping a const table */
263   dump_init_FUNC          init;           /**< handler for init */
264   dump_finish_FUNC        finish;         /**< handler for finish */
265   FILE                    *f;             /**< the file to dump to */
266   stat_info_t             *status;        /**< access to the global status */
267   dumper_t                *next;          /**< link to the next dumper */
268 };
269
270 /**
271  * helper: get an ir_op from an opcode
272  */
273 ir_op *stat_get_op_from_opcode(opcode code);
274
275 /* API for distribution tables */
276
277 /**
278  * creates a new distribution table.
279  *
280  * @param cmp_func   Compare function for objects in the distribution
281  * @param hash_func  Hash function for objects in the distribution
282  */
283 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func);
284
285 /**
286  * creates a new distribution table for an integer distribution.
287  */
288 distrib_tbl_t *stat_new_int_distrib_tbl(void);
289
290 /**
291  * destroys a distribution table.
292  */
293 void stat_delete_distrib_tbl(distrib_tbl_t *tbl);
294
295 /**
296  * adds a new object count into the distribution table.
297  */
298 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt);
299
300 /**
301  * adds a new key count into the integer distribution table.
302  */
303 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt);
304
305 /**
306  * calculates the mean value of a distribution.
307  */
308 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl);
309
310 /**
311  * calculates the average value of a distribution
312  */
313 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl);
314
315 /** evaluates each entry of a distribution table. */
316 typedef void (*eval_distrib_entry_fun)(const distrib_entry_t *entry, void *env);
317
318 /**
319  * iterates over all entries in a distribution table
320  */
321 void stat_iterate_distrib_tbl(distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env);
322
323 /**
324  * update info on Consts.
325  *
326  * @param node   The Const node
327  * @param graph  The graph entry containing the call
328  */
329 void stat_update_const(stat_info_t *status, ir_node *node, graph_entry_t *graph);
330
331 /**
332  * clears the const statistics for a new snapshot.
333  */
334 void stat_const_clear(stat_info_t *status);
335
336 /**
337  * initialize the Const statistic.
338  */
339 void stat_init_const_cnt(stat_info_t *status);
340
341 /**
342  * return a human readable name for an float classification
343  */
344 const char *stat_fc_name(float_classify_t classification);
345
346 #endif /* _FIRMSTAT_T_H_ */