2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Statistics for Firm. Internal data structures.
23 * @author Michael Beck
25 #ifndef FIRM_STAT_FIRMSTAT_T_H
26 #define FIRM_STAT_FIRMSTAT_T_H
32 #include "irgraph_t.h"
41 * just be make some things clear :-), the
44 #define HASH_MAP(type) hmap_##type
46 typedef pset hmap_node_entry_t;
47 typedef pset hmap_graph_entry_t;
48 typedef pset hmap_opt_entry_t;
49 typedef pset hmap_block_entry_t;
50 typedef pset hmap_be_block_entry_t;
51 typedef pset hmap_reg_pressure_entry_t;
52 typedef pset hmap_perm_stat_entry_t;
53 typedef pset hmap_perm_class_entry_t;
54 typedef pset hmap_ir_op;
55 typedef pset hmap_distrib_entry_t;
58 * An entry in a distribution table
60 typedef struct distrib_entry_t {
61 counter_t cnt; /**< the current count */
62 const void *object; /**< the object which is counted */
65 /** The type of the hash function for objects in distribution tables. */
66 typedef unsigned (*distrib_hash_fun)(const void *object);
69 * The distribution table.
71 typedef struct distrib_tbl_t {
72 struct obstack cnts; /**< obstack containing the distrib_entry_t entries */
73 HASH_MAP(distrib_entry_t) *hash_map; /**< the hash map containing the distribution */
74 distrib_hash_fun hash_func; /**< the hash function for object in this distribution */
75 unsigned int_dist; /**< non-zero, if it's a integer distribution */
79 * possible address marker values
82 MARK_ADDRESS_CALC = 1, /**< the node is an address expression */
83 MARK_REF_ADR = 2, /**< the node is referenced by an address expression */
84 MARK_REF_NON_ADR = 4, /**< the node is referenced by a non-address expression */
88 * An entry in the address_mark set
90 typedef struct address_mark_entry_t {
91 ir_node *node; /**< the node which this entry belongs to, needed for compare */
92 unsigned mark; /**< the mark, a bitmask of enum adr_marker_t */
93 } address_mark_entry_t;
96 * An entry for ir_nodes, used in ir_graph statistics.
98 typedef struct node_entry_t {
99 counter_t cnt_alive; /**< amount of nodes in this entry */
100 counter_t new_node; /**< amount of new nodes for this entry */
101 counter_t into_Id; /**< amount of nodes that turned into Id's for this entry */
102 counter_t normalized; /**< amount of nodes that normalized for this entry */
103 const ir_op *op; /**< the op for this entry */
106 enum leaf_call_state_t {
107 LCS_UNKNOWN = 0, /**< state is unknown yet */
108 LCS_LEAF_CALL = 1, /**< only leaf functions will be called */
109 LCS_NON_LEAF_CALL = 2, /**< at least one non-leaf function will be called or indetermined */
113 * Graph counter indexes. The first one are accumulated once, the other are always deleted before an
116 enum graph_counter_names {
117 gcnt_acc_walked, /**< walker walked over the graph, accumulated */
118 gcnt_acc_walked_blocks, /**< walker walked over the graph blocks, accumulated */
119 gcnt_acc_was_inlined, /**< number of times other graph were inlined, accumulated */
120 gcnt_acc_got_inlined, /**< number of times this graph was inlined, accumulated */
121 gcnt_acc_strength_red, /**< number of times strength reduction was successful on this graph, accumulated */
122 gcnt_acc_real_func_call, /**< number real function call optimization, accumulated */
124 /* --- non-accumulated values from here */
125 _gcnt_non_acc, /**< first non-accumulated counter */
127 gcnt_edges = _gcnt_non_acc, /**< number of DF edges in this graph */
128 gcnt_all_calls, /**< number of all calls */
129 gcnt_call_with_cnst_arg, /**< number of calls with const args */
130 gcnt_call_with_all_cnst_arg, /**< number of calls with all const args */
131 gcnt_call_with_local_adr, /**< number of calls with address of local var args */
132 gcnt_indirect_calls, /**< number of indirect calls */
133 gcnt_external_calls, /**< number of external calls */
134 gcnt_pure_adr_ops, /**< number of pure address operation */
135 gcnt_all_adr_ops, /**< number of all address operation */
136 gcnt_global_adr, /**< number of global load/store addresses. */
137 gcnt_local_adr, /**< number of local load/store addresses. */
138 gcnt_param_adr, /**< number of parameter load/store addresses. */
139 gcnt_this_adr, /**< number of this load/store addresses. */
140 gcnt_other_adr, /**< number of other load/store addresses. */
141 gcnt_if_conv, /**< number of if conversions */
143 /* --- must be the last enum constant --- */
144 _gcnt_last = gcnt_if_conv + IF_RESULT_LAST /**< number of counters */
148 * An entry for ir_graphs. These numbers are calculated for every IR graph.
150 typedef struct graph_entry_t {
151 struct obstack recalc_cnts; /**< obstack containing the counters that are recalculated */
152 HASH_MAP(node_entry_t) *opcode_hash; /**< hash map containing the opcode counter */
153 HASH_MAP(block_entry_t) *block_hash; /**< hash map containing the block counter */
154 HASH_MAP(be_block_entry_t) *be_block_hash; /**< hash map containing backend block information */
155 counter_t cnt[_gcnt_last]; /**< counter */
156 unsigned num_tail_recursion; /**< number of tail recursion optimizations */
157 HASH_MAP(opt_entry_t) *opt_hash[FS_OPT_MAX]; /**< hash maps containing opcode counter for optimizations */
158 ir_graph *irg; /**< the graph of this object */
159 ir_entity *ent; /**< the entity of this graph if one exists */
160 set *address_mark; /**< a set containing the address marks of the nodes */
161 unsigned is_deleted:1; /**< set if this irg was deleted */
162 unsigned is_leaf:1; /**< set, if this irg is a leaf function */
163 unsigned is_leaf_call:2; /**< set, if this irg calls only leaf functions */
164 unsigned is_recursive:1; /**< set, if this irg has recursive calls */
165 unsigned is_chain_call:1; /**< set, if this irg is a chain call */
166 unsigned is_strict:1; /**< set, if this irg represents a strict program */
167 unsigned is_analyzed:1; /**< helper: set, if this irg was already analysed */
171 * An entry for optimized ir_nodes
173 typedef struct opt_entry_t {
174 counter_t count; /**< optimization counter */
175 const ir_op *op; /**< the op for this entry */
179 * An entry for register pressure.
181 typedef struct reg_pressure_entry_t {
182 const char *class_name; /**< name of the register class */
183 int pressure; /**< the register pressure for this class */
184 } reg_pressure_entry_t;
187 * An entry for permutation statistics.
189 typedef struct perm_stat_entry_t {
190 ir_node *perm; /**< the perm node */
191 int size; /**< complete size */
192 int real_size; /**< number of pairs with different registers */
193 int n_copies; /**< number of copies created for lowering */
194 int n_exchg; /**< number of exchanges created for lowering */
195 distrib_tbl_t *cycles; /**< distribution of cycle lengths */
196 distrib_tbl_t *chains; /**< distribution of chain lengths */
200 * An entry for permutation statistics per class.
202 typedef struct perm_class_entry_t {
203 const char *class_name; /**< name of the register class */
204 int n_regs; /**< number of register in this class */
205 HASH_MAP(perm_stat_entry_t) *perm_stat; /**< statistics about all perm nodes of this class */
206 } perm_class_entry_t;
209 * An entry for a block or extended block in a ir-graph
211 typedef struct be_block_entry_t {
212 long block_nr; /**< block nr */
213 distrib_tbl_t *sched_ready; /**< distribution of ready nodes per block */
214 /**< the highest register pressures for this block for each register class */
215 HASH_MAP(reg_pressure_entry_t) *reg_pressure;
216 HASH_MAP(perm_class_entry_t) *perm_class_stat; /**< statistics about perm nodes for each register class */
220 * Block counter indexes. The first one are accumulated once, the other are always deleted before an
223 enum block_counter_names {
224 bcnt_nodes, /**< the counter of nodes in this block */
225 bcnt_edges, /**< the counter of edges in this block */
226 bcnt_in_edges, /**< the counter of edges incoming from other blocks to this block */
227 bcnt_out_edges, /**< the counter of edges outgoing from this block to other blocks */
228 bcnt_phi_data, /**< the counter of data Phi nodes in this block */
230 /* --- must be the last enum constant --- */
231 _bcnt_last /**< number of counters */
235 * An entry for a block or extended block in a ir-graph
237 typedef struct block_entry_t {
238 counter_t cnt[_bcnt_last]; /**< counter */
239 long block_nr; /**< block nr */
240 unsigned is_start:1; /**< set, if it's the Start block. */
241 unsigned is_end:1; /**< set, if it's the End block. */
245 * Some potential interesting float values
247 typedef enum float_classify_t {
248 STAT_FC_0, /**< the float value 0.0 */
249 STAT_FC_1, /**< the float value 1.0 */
250 STAT_FC_2, /**< the float value 2.0 */
251 STAT_FC_0_5, /**< the float value 0.5 */
252 STAT_FC_POWER_OF_TWO, /**< another 2^x value */
253 STAT_FC_OTHER, /**< all other values */
254 STAT_FC_MAX /**< last value */
260 typedef struct constant_info_t {
261 counter_t int_bits_count[32]; /**< distribution of bit sizes of integer constants */
262 counter_t floats[STAT_FC_MAX]; /**< floating point constants */
263 counter_t others; /**< all other constants */
267 typedef struct dumper_t dumper_t;
270 * handler for dumping an IRG
272 * @param dmp the dumper
273 * @param entry the IR-graph hash map entry
275 typedef void dump_graph_FUNC(dumper_t *dmp, graph_entry_t *entry);
278 * handler for dumper a constant info table
280 * @param dmp the dumper
282 typedef void dump_const_table_FUNC(dumper_t *dmp, const constant_info_t *tbl);
285 * dumps the parameter distribution table
287 typedef void dump_param_tbl_FUNC(dumper_t *dmp, const distrib_tbl_t *tbl, graph_entry_t *global);
290 * dumps the optimizations counter
292 typedef void dump_opt_cnt_FUNC(dumper_t *dumper, const counter_t *tbl, unsigned len);
295 * handler for dumper init
297 * @param dmp the dumper
298 * @param name name of the file to dump to
300 typedef void dump_init_FUNC(dumper_t *dmp, const char *name);
303 * handler for dumper finish
305 * @param dmp the dumper
307 typedef void dump_finish_FUNC(dumper_t *dmp);
312 typedef struct statistic_info_t {
313 unsigned stat_options; /**< statistic options: field must be first */
314 struct obstack cnts; /**< obstack containing the counters that are incremented */
315 struct obstack be_data; /**< obstack containing backend statistics data */
316 HASH_MAP(graph_entry_t) *irg_hash; /**< hash map containing the counter for irgs */
317 HASH_MAP(ir_op) *ir_op_hash; /**< hash map containing all ir_ops (accessible by op_codes) */
318 pdeq *wait_q; /**< wait queue for leaf call decision */
319 unsigned recursive:1; /**< flag for detecting recursive hook calls */
320 unsigned in_dead_node_elim:1; /**< flag for dead node elimination runs */
321 ir_op *op_Phi0; /**< pseudo op for Phi0 */
322 ir_op *op_PhiM; /**< pseudo op for memory Phi */
323 ir_op *op_ProjM; /**< pseudo op for memory Proj */
324 ir_op *op_MulC; /**< pseudo op for multiplication by const */
325 ir_op *op_DivC; /**< pseudo op for division by const */
326 ir_op *op_ModC; /**< pseudo op for modulo by const */
327 ir_op *op_SelSel; /**< pseudo op for Sel(Sel) */
328 ir_op *op_SelSelSel; /**< pseudo op for Sel(Sel(Sel)) */
329 dumper_t *dumper; /**< list of dumper */
330 int reassoc_run; /**< if set, reassociation is running */
331 constant_info_t const_info; /**< statistic info for constants */
332 distrib_tbl_t *dist_param_cnt; /**< distribution table for call parameters */
334 counter_t num_opts[FS_OPT_MAX];/**< count optimizations */
338 * a dumper description
341 dump_graph_FUNC *dump_graph; /**< handler for dumping an irg */
342 dump_const_table_FUNC *dump_const_tbl; /**< handler for dumping a const table */
343 dump_param_tbl_FUNC *dump_param_tbl; /**< handler for dumping the Call parameter table */
344 dump_opt_cnt_FUNC *dump_opt_cnt; /**< handler for dumping the optimization table. */
345 dump_init_FUNC *init; /**< handler for init */
346 dump_finish_FUNC *finish; /**< handler for finish */
347 FILE *f; /**< the file to dump to */
348 stat_info_t *status; /**< access to the global status */
349 dumper_t *next; /**< link to the next dumper */
350 pset *func_map; /**< pset containing all registered functions */
351 unsigned tag; /**< the id tag of the dumper */
355 * helper: get an ir_op from an opcode
357 ir_op *stat_get_op_from_opcode(unsigned code);
359 /* API for distribution tables */
362 * creates a new distribution table.
364 * @param cmp_func Compare function for objects in the distribution
365 * @param hash_func Hash function for objects in the distribution
367 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func);
370 * creates a new distribution table for an integer distribution.
372 distrib_tbl_t *stat_new_int_distrib_tbl(void);
375 * destroys a distribution table.
377 void stat_delete_distrib_tbl(distrib_tbl_t *tbl);
380 * adds a new object count into the distribution table.
382 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt);
385 * adds a new key count into the integer distribution table.
387 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt);
390 * increases object count by one
392 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object);
395 * increases key count by one
397 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key);
400 * inserts a new object with count 0 into the distribution table
401 * if object is already present, nothing happens
403 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object);
406 * inserts a new key with count 0 into the integer distribution table
407 * if key is already present, nothing happens
409 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key);
412 * returns the sum over all counters in a distribution table
414 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl);
417 * calculates the mean value of a distribution.
419 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl);
422 * calculates the average value of a distribution
424 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl);
426 /** evaluates each entry of a distribution table. */
427 typedef void (*eval_distrib_entry_fun)(const distrib_entry_t *entry, void *env);
430 * iterates over all entries in a distribution table
432 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env);
435 * update info on Consts.
437 * @param node The Const node
438 * @param graph The graph entry containing the call
440 void stat_update_const(stat_info_t *status, ir_node *node, graph_entry_t *graph);
443 * clears the const statistics for a new snapshot.
445 void stat_const_clear(stat_info_t *status);
448 * initialize the Const statistic.
450 void stat_init_const_cnt(stat_info_t *status);
453 * return a human readable name for an float classification
455 const char *stat_fc_name(float_classify_t classification);
458 * Update the register pressure of a block
460 * @param irg the irg containing the block
461 * @param block the block for which the reg pressure should be set
462 * @param pressure the pressure
463 * @param class_name the name of the register class
465 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name);
468 * Update the distribution of ready nodes of a block
470 * @param irg the irg containing the block
471 * @param block the block for which the reg pressure should be set
472 * @param num_ready the number of ready nodes
474 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready);
477 * Update the permutation statistic of a block
479 * @param class_name the name of the register class
480 * @param perm the perm node
481 * @param block the block containing the perm
482 * @param size the size of the perm
483 * @param real_size number of pairs with different registers
485 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
486 int size, int real_size);
489 * Update the permutation statistic of a single perm
491 * @param class_name the name of the register class
492 * @param perm the perm node
493 * @param block the block containing the perm
494 * @param is_chain 1 if chain, 0 if cycle
495 * @param size length of the cycle/chain
496 * @param n_ops the number of ops representing this cycle/chain after lowering
498 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
499 int is_chain, int size, int n_ops);
502 * Register an additional function for all dumper. This function
503 * is called in dump_snapshot once for each graph_entry and dumper.
505 * @param func the dump function to register
507 void stat_register_dumper_func(dump_graph_FUNC *func);
509 #endif /* FIRM_STAT_FIRMSTAT_T_H */