added new licence header
[libfirm] / ir / stat / firmstat_t.h
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /*
21  * Project:     libFIRM
22  * File name:   ir/stat/firmstat_t.h
23  * Purpose:     Statistics for Firm. Internal data structures.
24  * Author:      Michael Beck
25  * Created:
26  * CVS-ID:      $Id$
27  * Copyright:   (c) 2004 Universität Karlsruhe
28  */
29 #ifndef _FIRMSTAT_T_H_
30 #define _FIRMSTAT_T_H_
31
32 /**
33  * @file firmstat_t.h
34  */
35 #include "firmstat.h"
36
37 #include "irop_t.h"
38 #include "irnode_t.h"
39 #include "irgraph_t.h"
40 #include "pset.h"
41 #include "pdeq.h"
42 #include "irprog.h"
43 #include "irgwalk.h"
44 #include "counter.h"
45 #include "irhooks.h"
46
47 /* some useful macro. */
48 #define ARR_SIZE(a)   (sizeof(a)/sizeof((a)[0]))
49
50 /*
51  * just be make some things clear :-), the
52  * poor man "generics"
53  */
54 #define HASH_MAP(type) hmap_##type
55
56 typedef pset hmap_node_entry_t;
57 typedef pset hmap_graph_entry_t;
58 typedef pset hmap_opt_entry_t;
59 typedef pset hmap_block_entry_t;
60 typedef pset hmap_be_block_entry_t;
61 typedef pset hmap_reg_pressure_entry_t;
62 typedef pset hmap_perm_stat_entry_t;
63 typedef pset hmap_perm_class_entry_t;
64 typedef pset hmap_ir_op;
65 typedef pset hmap_distrib_entry_t;
66
67 /**
68  * An entry in a distribution table
69  */
70 typedef struct _distrib_entry_t {
71         counter_t        cnt;       /**< the current count */
72         const void *object;   /**< the object which is counted */
73 } distrib_entry_t;
74
75 /** The type of the hash function for objects in distribution tables. */
76 typedef unsigned (*distrib_hash_fun)(const void *object);
77
78 /**
79  * The distribution table.
80  */
81 typedef struct _distrib_tbl_t {
82         struct obstack            cnts;       /**< obstack containing the distrib_entry_t entries */
83         HASH_MAP(distrib_entry_t) *hash_map;  /**< the hash map containing the distribution */
84         distrib_hash_fun          hash_func;  /**< the hash function for object in this distribution */
85         unsigned                  int_dist;   /**< non-zero, if it's a integer distribution */
86 } distrib_tbl_t;
87
88 /**
89  * possible address marker values
90  */
91 enum adr_marker_t {
92         MARK_ADDRESS_CALC     = 1,    /**< the node is an address expression */
93         MARK_REF_ADR          = 2,    /**< the node is referenced by an address expression */
94         MARK_REF_NON_ADR      = 4,    /**< the node is referenced by a non-address expression */
95 };
96
97 /**
98  * An entry in the address_mark set
99  */
100 typedef struct _address_mark_entry_t {
101   ir_node  *node;               /**< the node which this entry belongs to, needed for compare */
102   unsigned mark;                /**< the mark, a bitmask of enum adr_marker_t */
103 } address_mark_entry_t;
104
105 /**
106  * An entry for ir_nodes, used in ir_graph statistics.
107  */
108 typedef struct _node_entry_t {
109         counter_t   cnt_alive;    /**< amount of nodes in this entry */
110         counter_t   new_node;     /**< amount of new nodes for this entry */
111         counter_t   into_Id;      /**< amount of nodes that turned into Id's for this entry */
112         const ir_op *op;          /**< the op for this entry */
113 } node_entry_t;
114
115 enum leaf_call_state_t {
116         LCS_UNKNOWN       = 0,      /**< state is unknown yet */
117         LCS_LEAF_CALL     = 1,      /**< only leaf functions will be called */
118         LCS_NON_LEAF_CALL = 2,      /**< at least one non-leaf function will be called or indetermined */
119 };
120
121 /**
122  * Graph counter indexes. The first one are accumulated once, the other are always deleted before an
123  * snapshot is taken.
124  */
125 enum graph_counter_names {
126         gcnt_acc_walked,               /**< walker walked over the graph, accumulated */
127         gcnt_acc_walked_blocks,        /**< walker walked over the graph blocks, accumulated */
128         gcnt_acc_was_inlined,          /**< number of times other graph were inlined, accumulated */
129         gcnt_acc_got_inlined,          /**< number of times this graph was inlined, accumulated */
130         gcnt_acc_strength_red,         /**< number of times strength reduction was successful on this graph, accumulated */
131         gcnt_acc_real_func_call,       /**< number real function call optimization, accumulated */
132
133         /* --- non-accumulated values from here */
134         _gcnt_non_acc,                 /**< first non-accumulated counter */
135
136         gcnt_edges = _gcnt_non_acc,    /**< number of DF edges in this graph */
137         gcnt_all_calls,                /**< number of all calls */
138         gcnt_call_with_cnst_arg,       /**< number of calls with const args */
139         gcnt_call_with_all_cnst_arg,   /**< number of calls with all const args */
140         gcnt_call_with_local_adr,      /**< number of calls with address of local var args */
141         gcnt_indirect_calls,           /**< number of indirect calls */
142         gcnt_pure_adr_ops,             /**< number of pure address operation */
143         gcnt_all_adr_ops,              /**< number of all address operation */
144         gcnt_global_adr,               /**< number of global load/store addresses. */
145         gcnt_local_adr,                /**< number of local load/store addresses. */
146         gcnt_param_adr,                /**< number of parameter load/store addresses. */
147         gcnt_this_adr,                 /**< number of this load/store addresses. */
148         gcnt_other_adr,                /**< number of other load/store addresses. */
149         gcnt_if_conv,                  /**< number of if conversions */
150
151         /* --- must be the last enum constant --- */
152         _gcnt_last = gcnt_if_conv + IF_RESULT_LAST                 /**< number of counters */
153 };
154
155 /**
156  * An entry for ir_graphs. These numbers are calculated for every IR graph.
157  */
158 typedef struct _graph_entry_t {
159         struct obstack             recalc_cnts;                  /**< obstack containing the counters that are recalculated */
160         HASH_MAP(node_entry_t)     *opcode_hash;                 /**< hash map containing the opcode counter */
161         HASH_MAP(block_entry_t)    *block_hash;                  /**< hash map containing the block counter */
162         HASH_MAP(block_entry_t)    *extbb_hash;                  /**< hash map containing the extended block counter */
163         HASH_MAP(be_block_entry_t) *be_block_hash;               /**< hash map containing backend block information */
164         counter_t                  cnt[_gcnt_last];               /**< counter */
165         unsigned                   num_tail_recursion;           /**< number of tail recursion optimizations */
166         HASH_MAP(opt_entry_t)      *opt_hash[FS_OPT_MAX];        /**< hash maps containing opcode counter for optimizations */
167         ir_graph                   *irg;                         /**< the graph of this object */
168         ir_entity                  *ent;                         /**< the entity of this graph if one exists */
169         set                        *address_mark;                /**< a set containing the address marks of the nodes */
170         unsigned                   is_deleted:1;                 /**< set if this irg was deleted */
171         unsigned                   is_leaf:1;                    /**< set, if this irg is a leaf function */
172         unsigned                   is_leaf_call:2;               /**< set, if this irg calls only leaf functions */
173         unsigned                   is_recursive:1;               /**< set, if this irg has recursive calls */
174         unsigned                   is_chain_call:1;              /**< set, if this irg is a chain call */
175         unsigned                   is_analyzed:1;                /**< helper: set, if this irg was already analysed */
176 } graph_entry_t;
177
178 /**
179  * An entry for optimized ir_nodes
180  */
181 typedef struct _opt_entry_t {
182         counter_t   count;    /**< optimization counter */
183         const ir_op *op;      /**< the op for this entry */
184 } opt_entry_t;
185
186 /**
187  * An entry for register pressure.
188  */
189 typedef struct _reg_pressure_entry_t {
190         const char *class_name; /**< name of the register class */
191         int         pressure;   /**< the register pressure for this class */
192 } reg_pressure_entry_t;
193
194 /**
195  * An entry for permutation statistics.
196  */
197 typedef struct _perm_stat_entry_t {
198         ir_node       *perm;       /**< the perm node */
199         int            size;       /**< complete size */
200         int            real_size;  /**< number of pairs with different registers */
201         int            n_copies;   /**< number of copies created for lowering */
202         int            n_exchg;    /**< number of exchanges created for lowering */
203         distrib_tbl_t *cycles;     /**< distribution of cycle lengths */
204         distrib_tbl_t *chains;     /**< distribution of chain lengths */
205 } perm_stat_entry_t;
206
207 /**
208  * An entry for permutation statistics per class.
209  */
210 typedef struct _perm_class_entry_t {
211         const char                  *class_name; /**< name of the register class */
212         int                          n_regs;     /**< number of register in this class */
213         HASH_MAP(perm_stat_entry_t) *perm_stat;  /**< statistics about all perm nodes of this class */
214 } perm_class_entry_t;
215
216 /**
217  * An entry for a block or extended block in a ir-graph
218  */
219 typedef struct _be_block_entry_t {
220         long                           block_nr;         /**< block nr */
221         distrib_tbl_t                  *sched_ready;     /**< distribution of ready nodes per block */
222         /**< the highest register pressures for this block for each register class */
223         HASH_MAP(reg_pressure_entry_t) *reg_pressure;
224         HASH_MAP(perm_class_entry_t)   *perm_class_stat; /**< statistics about perm nodes for each register class */
225 } be_block_entry_t;
226
227 /**
228  * Block counter indexes. The first one are accumulated once, the other are always deleted before an
229  * snapshot is taken.
230  */
231 enum block_counter_names {
232         bcnt_nodes,     /**< the counter of nodes in this block */
233         bcnt_edges,     /**< the counter of edges in this block */
234         bcnt_in_edges,  /**< the counter of edges incoming from other blocks to this block */
235         bcnt_out_edges, /**< the counter of edges outgoing from this block to other blocks */
236         bcnt_phi_data,  /**< the counter of data Phi nodes in this block */
237
238         /* --- must be the last enum constant --- */
239         _bcnt_last      /**< number of counters */
240 };
241
242 /**
243  * An entry for a block or extended block in a ir-graph
244  */
245 typedef struct _block_entry_t {
246   counter_t       cnt[_bcnt_last];  /**< counter */
247   long            block_nr;         /**< block nr */
248 } block_entry_t;
249
250 /** An entry for an extended block in a ir-graph */
251 typedef block_entry_t extbb_entry_t;
252
253 /**
254  * Some potential interesting float values
255  */
256 typedef enum _float_classify_t {
257         STAT_FC_0,                /**< the float value 0.0 */
258         STAT_FC_1,                /**< the float value 1.0 */
259         STAT_FC_2,                /**< the float value 2.0 */
260         STAT_FC_0_5,              /**< the float value 0.5 */
261         STAT_FC_EXACT,            /**< an exact value */
262         STAT_FC_OTHER,            /**< all other values */
263         STAT_FC_MAX               /**< last value */
264 } float_classify_t;
265
266 /**
267  * constant info
268  */
269 typedef struct _constant_info_t {
270         counter_t  int_bits_count[32];  /**< distribution of bit sizes of integer constants */
271         counter_t  floats[STAT_FC_MAX]; /**< floating point constants */
272         counter_t  others;              /**< all other constants */
273 } constant_info_t;
274
275 /** forward */
276 typedef struct _dumper_t dumper_t;
277
278 /**
279  * handler for dumping an IRG
280  *
281  * @param dmp   the dumper
282  * @param entry the IR-graph hash map entry
283  */
284 typedef void (*dump_graph_FUNC)(dumper_t *dmp, graph_entry_t *entry);
285
286 /**
287  * handler for dumper a constant info table
288  *
289  * @param dmp   the dumper
290  */
291 typedef void (*dump_const_table_FUNC)(dumper_t *dmp, const constant_info_t *tbl);
292
293 /**
294  * dumps the parameter distribution table
295  */
296 typedef void (*dump_param_tbl_FUNC)(dumper_t *dmp, const distrib_tbl_t *tbl, graph_entry_t *global);
297
298 /**
299  * handler for dumper init
300  *
301  * @param dmp   the dumper
302  * @param name  name of the file to dump to
303  */
304 typedef void (*dump_init_FUNC)(dumper_t *dmp, const char *name);
305
306 /**
307  * handler for dumper finish
308  *
309  * @param dmp   the dumper
310  */
311 typedef void (*dump_finish_FUNC)(dumper_t *dmp);
312
313 /**
314  * statistics info
315  */
316 typedef struct _statistic_info_t {
317         unsigned                stat_options;        /**< statistic options: field must be first */
318         struct obstack          cnts;                /**< obstack containing the counters that are incremented */
319         struct obstack          be_data;             /**< obstack containing backend statistics data */
320         HASH_MAP(graph_entry_t) *irg_hash;           /**< hash map containing the counter for irgs */
321         HASH_MAP(ir_op)         *ir_op_hash;         /**< hash map containing all ir_ops (accessible by op_codes) */
322         pdeq                    *wait_q;             /**< wait queue for leaf call decision */
323         unsigned                recursive:1;         /**< flag for detecting recursive hook calls */
324         unsigned                in_dead_node_elim:1; /**< flag for dead node elimination runs */
325         ir_op                   *op_Phi0;            /**< pseudo op for Phi0 */
326         ir_op                   *op_PhiM;            /**< pseudo op for memory Phi */
327         ir_op                   *op_ProjM;           /**< pseudo op for memory Proj */
328         ir_op                   *op_MulC;            /**< pseudo op for multiplication by const */
329         ir_op                   *op_DivC;            /**< pseudo op for division by const */
330         ir_op                   *op_ModC;            /**< pseudo op for modulo by const */
331         ir_op                   *op_DivModC;         /**< pseudo op for DivMod by const */
332         ir_op                   *op_SelSel;          /**< pseudo op for Sel(Sel) */
333         ir_op                   *op_SelSelSel;       /**< pseudo op for Sel(Sel(Sel)) */
334         dumper_t                *dumper;             /**< list of dumper */
335         int                     reassoc_run;         /**< if set, reassociation is running */
336         constant_info_t         const_info;          /**< statistic info for constants */
337         distrib_tbl_t           *dist_param_cnt;     /**< distributation table for call parameters */
338 } stat_info_t;
339
340 /**
341  * a dumper description
342  */
343 struct _dumper_t {
344         dump_graph_FUNC         dump_graph;     /**< handler for dumping an irg */
345         dump_const_table_FUNC   dump_const_tbl; /**< handler for dumping a const table */
346         dump_param_tbl_FUNC     dump_param_tbl; /**< handler for dumper the Call parameter table */
347         dump_init_FUNC          init;           /**< handler for init */
348         dump_finish_FUNC        finish;         /**< handler for finish */
349         FILE                    *f;             /**< the file to dump to */
350         stat_info_t             *status;        /**< access to the global status */
351         dumper_t                *next;          /**< link to the next dumper */
352         pset                    *func_map;      /**< pset containing all registered functions */
353         unsigned                tag;            /**< the id tag of the dumper */
354 };
355
356 /**
357  * helper: get an ir_op from an opcode
358  */
359 ir_op *stat_get_op_from_opcode(ir_opcode code);
360
361 /* API for distribution tables */
362
363 /**
364  * creates a new distribution table.
365  *
366  * @param cmp_func   Compare function for objects in the distribution
367  * @param hash_func  Hash function for objects in the distribution
368  */
369 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func);
370
371 /**
372  * creates a new distribution table for an integer distribution.
373  */
374 distrib_tbl_t *stat_new_int_distrib_tbl(void);
375
376 /**
377  * destroys a distribution table.
378  */
379 void stat_delete_distrib_tbl(distrib_tbl_t *tbl);
380
381 /**
382  * adds a new object count into the distribution table.
383  */
384 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt);
385
386 /**
387  * adds a new key count into the integer distribution table.
388  */
389 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt);
390
391 /**
392  * increases object count by one
393  */
394 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object);
395
396 /**
397  * increases key count by one
398  */
399 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key);
400
401 /**
402  * inserts a new object with count 0 into the distribution table
403  * if object is already present, nothing happens
404  */
405 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object);
406
407 /**
408  * inserts a new key with count 0 into the integer distribution table
409  * if key is already present, nothing happens
410  */
411 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key);
412
413 /**
414  * returns the sum over all counters in a distribution table
415  */
416 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl);
417
418 /**
419  * calculates the mean value of a distribution.
420  */
421 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl);
422
423 /**
424  * calculates the average value of a distribution
425  */
426 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl);
427
428 /** evaluates each entry of a distribution table. */
429 typedef void (*eval_distrib_entry_fun)(const distrib_entry_t *entry, void *env);
430
431 /**
432  * iterates over all entries in a distribution table
433  */
434 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env);
435
436 /**
437  * update info on Consts.
438  *
439  * @param node   The Const node
440  * @param graph  The graph entry containing the call
441  */
442 void stat_update_const(stat_info_t *status, ir_node *node, graph_entry_t *graph);
443
444 /**
445  * clears the const statistics for a new snapshot.
446  */
447 void stat_const_clear(stat_info_t *status);
448
449 /**
450  * initialize the Const statistic.
451  */
452 void stat_init_const_cnt(stat_info_t *status);
453
454 /**
455  * return a human readable name for an float classification
456  */
457 const char *stat_fc_name(float_classify_t classification);
458
459 /**
460  * Update the register pressure of a block
461  *
462  * @param irg        the irg containing the block
463  * @param block      the block for which the reg pressure should be set
464  * @param pressure   the pressure
465  * @param class_name the name of the register class
466  */
467 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name);
468
469 /**
470  * Update the distribution of ready nodes of a block
471  *
472  * @param irg        the irg containing the block
473  * @param block      the block for which the reg pressure should be set
474  * @param num_ready  the number of ready nodes
475  */
476 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready);
477
478 /**
479  * Update the permutation statistic of a block
480  *
481  * @param class_name the name of the register class
482  * @param perm       the perm node
483  * @param block      the block containing the perm
484  * @param size       the size of the perm
485  * @param real_size  number of pairs with different registers
486  */
487 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
488                              int size, int real_size);
489
490 /**
491  * Update the permutation statistic of a single perm
492  *
493  * @param class_name the name of the register class
494  * @param perm       the perm node
495  * @param block      the block containing the perm
496  * @param is_chain   1 if chain, 0 if cycle
497  * @param size       length of the cycle/chain
498  * @param n_ops      the number of ops representing this cycle/chain after lowering
499  */
500 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
501                                   int is_chain, int size, int n_ops);
502
503 /**
504  * Register an additional function for all dumper.  This function
505  * is called in dump_snapshot once for each graph_entry and dumper.
506  *
507  * @param func  the dump function to register
508  */
509 void stat_register_dumper_func(dump_graph_FUNC func);
510
511 #endif /* _FIRMSTAT_T_H_ */