3 * File name: ir/ir/firmstat.c
4 * Purpose: Statistics for Firm.
8 * Copyright: (c) 2004 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
19 # include "irnode_t.h"
20 # include "irgraph_t.h"
23 # include "firmstat.h"
28 #undef obstack_chunk_alloc
29 #undef obstack_chunk_free
30 #define obstack_chunk_alloc malloc
31 #define obstack_chunk_free free
35 * 64 bit should be enough for now
37 #define STAT_CNT_NUM 2
39 typedef struct _counter_t {
40 unsigned cnt[STAT_CNT_NUM];
44 * An entry for ir_nodes
46 typedef struct _node_entry_t {
47 counter_t new_node; /**< amount of new nodes for this entry */
48 counter_t into_Id; /**< amount of nodes that turned into Id's for this entry */
49 const ir_op *op; /**< the op for this entry */
53 * An entry for ir_graphs
55 typedef struct _graph_entry_t {
56 pset *opcode_hash; /**< hash map containing the opcode counter */
57 counter_t walked; /**< walker walked over the graph */
58 counter_t walked_blocks; /**< walker walked over the graph blocks */
59 const ir_graph *irg; /**< the graph of this object */
60 pset *opt_hash[STAT_OPT_MAX]; /**< hash maps containing opcode counter for optimizations */
64 * An entry for optimized ir_nodes
66 typedef struct _opt_entry_t {
67 counter_t count; /**< optimization counter */
68 const ir_op *op; /**< the op for this entry */
74 typedef struct _statistic_info_t {
75 struct obstack cnts; /**< obstack containing the counters */
76 pset *opcode_hash; /**< hash map containing the opcode counter */
77 pset *irg_hash; /**< hash map containing the counter for irgs */
78 FILE *f; /**< outputfile */
79 ir_op *op_Phi0; /**< needed pseudo op */
85 static stat_info_t _status, *status = &_status;
90 static INLINE void cnt_inc(counter_t *cnt)
94 for (i = 0; i < STAT_CNT_NUM; ++i) {
103 static INLINE void cnt_dec(counter_t *cnt)
107 for (i = 0; i < STAT_CNT_NUM; ++i) {
108 if (--cnt->cnt[i] != -1)
114 * set a counter to zero
116 static INLINE void cnt_clr(counter_t *cnt)
118 memset(cnt->cnt, 0, sizeof(cnt->cnt));
122 * compare two elements of the opcode hash
124 static int opcode_cmp(const void *elt, const void *key)
126 const node_entry_t *e1 = elt;
127 const node_entry_t *e2 = key;
129 return e1->op->code - e2->op->code;
133 * compare two elements of the graph hash
135 static int graph_cmp(const void *elt, const void *key)
137 const graph_entry_t *e1 = elt;
138 const graph_entry_t *e2 = key;
140 return e1->irg != e2->irg;
144 * compare two elements of the optimization hash
146 static int opt_cmp(const void *elt, const void *key)
148 const opt_entry_t *e1 = elt;
149 const opt_entry_t *e2 = key;
151 return e1->op->code != e2->op->code;
155 * Returns the associates node_entry_t for an ir_op
157 static node_entry_t *opcode_get_entry(const ir_op *op, pset *set)
164 elem = pset_find(set, &key, op->code);
168 elem = obstack_alloc(&status->cnts, sizeof(*elem));
170 /* clear new counter */
171 cnt_clr(&elem->new_node);
172 cnt_clr(&elem->into_Id);
176 return pset_insert(set, elem, op->code);
180 * calculates a hash value for an irg
182 static INLINE unsigned irg_hash(const ir_graph *irg)
184 return (unsigned)irg;
188 * Returns the acssociates graph_entry_t for an irg
190 static graph_entry_t *graph_get_entry(const ir_graph *irg, pset *set)
198 elem = pset_find(set, &key, irg_hash(irg));
202 elem = obstack_alloc(&status->cnts, sizeof(*elem));
204 cnt_clr(&elem->walked);
205 cnt_clr(&elem->walked_blocks);
207 /* new hash table for opcodes here */
208 elem->opcode_hash = new_pset(opcode_cmp, 5);
211 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
212 elem->opt_hash[i] = new_pset(opt_cmp, 4);
214 return pset_insert(set, elem, irg_hash(irg));
218 * Returns the associates opt_entry_t for an ir_op
220 static opt_entry_t *opt_get_entry(const ir_op *op, pset *set)
227 elem = pset_find(set, &key, op->code);
231 elem = obstack_alloc(&status->cnts, sizeof(*elem));
233 /* clear new counter */
234 cnt_clr(&elem->count);
238 return pset_insert(set, elem, op->code);
241 /* ---------------------------------------------------------------------- */
243 /* A new IR op is registered. */
244 void stat_new_ir_op(const ir_op *op)
246 /* execute for side effect :-) */
247 opcode_get_entry(op, status->opcode_hash);
250 /* An IR op is freed. */
251 void stat_free_ir_op(const ir_op *op)
255 /* initialize the statistics module. */
258 obstack_init(&status->cnts);
260 /* create the hash-tables */
261 status->opcode_hash = new_pset(opcode_cmp, 8);
262 status->irg_hash = new_pset(graph_cmp, 8);
264 status->f = fopen("firmstat.txt", "w");
265 status->op_Phi0 = NULL;
268 /* A new node is created. */
269 void stat_new_node(const ir_node *node)
272 graph_entry_t *graph;
273 ir_op *op = get_irn_op(node);
275 if (op->code == iro_Phi && get_irn_arity(node) == 0) {
276 /* special case, a Phi0 node, count on extra counter */
277 if (! status->op_Phi0) {
278 ir_op *op_Phi = get_op_Phi();
280 status->op_Phi0 = new_ir_op(0xFFFF, "Phi0", op_Phi->pinned, op_Phi->flags, op_Phi->opar, op_Phi->op_index, op_Phi->attr_size);
282 op = status->op_Phi0;
285 entry = opcode_get_entry(op, status->opcode_hash);
286 graph = graph_get_entry(current_ir_graph, status->irg_hash);
288 /* increase global value */
289 cnt_inc(&entry->new_node);
291 /* increase local value */
292 entry = opcode_get_entry(op, graph->opcode_hash);
293 cnt_inc(&entry->new_node);
296 /* A node is changed into a Id node */
297 void stat_turn_into_id(const ir_node *node)
300 graph_entry_t *graph;
301 ir_op *op = get_irn_op(node);
303 if (op->code == iro_Phi && get_irn_arity(node) == 0) {
304 /* special case, a Phi0 node, count on extra counter */
305 if (! status->op_Phi0) {
306 ir_op *op_Phi = get_op_Phi();
308 status->op_Phi0 = new_ir_op(0xFFFF, "Phi0", op_Phi->pinned, op_Phi->flags, op_Phi->opar, op_Phi->op_index, op_Phi->attr_size);
310 op = status->op_Phi0;
313 entry = opcode_get_entry(op, status->opcode_hash);
314 graph = graph_get_entry(current_ir_graph, status->irg_hash);
316 /* increase global value */
317 cnt_inc(&entry->into_Id);
319 /* increase local value */
320 entry = opcode_get_entry(op, graph->opcode_hash);
321 cnt_inc(&entry->into_Id);
324 /* A new graph was created */
325 void stat_new_graph(const ir_graph *irg)
327 /* execute for side effect :-) */
328 graph_get_entry(irg, status->irg_hash);
332 * A walk over a graph is initiated
334 void stat_irg_walk(const ir_graph *irg, void *pre, void *post)
336 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
338 cnt_inc(&graph->walked);
342 * A walk over the graph's blocks is initiated
344 void stat_irg_block_walk(const ir_graph *irg, const ir_node *node, void *pre, void *post)
346 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
348 cnt_inc(&graph->walked_blocks);
352 * called for every node that is removed due to an optimization
354 static void removed_due_opt(ir_node *n, pset *set)
356 ir_op *op = get_irn_op(n);
357 opt_entry_t *entry = opt_get_entry(op, set);
359 /* increase global value */
360 cnt_inc(&entry->count);
364 * Some nodes were optimized into some others due to an optimization
366 void stat_merge_nodes(
367 ir_node **new_node_array, int new_num_entries,
368 ir_node **old_node_array, int old_num_entries,
372 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
374 for (i = 0; i < old_num_entries; ++i) {
375 for (j = 0; j < new_num_entries; ++j)
376 if (old_node_array[i] == new_node_array[j])
379 /* nodes might be in new and old, these are NOT removed */
380 if (j >= new_num_entries) {
381 removed_due_opt(old_node_array[i], graph->opt_hash[opt]);
387 * A node was lowered into other nodes
389 void stat_lower(ir_node *node)
391 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
393 removed_due_opt(node, graph->opt_hash[STAT_LOWERED]);
397 * dumps a opcode hash
399 static void dump_opcode_hash(pset *set)
403 fprintf(status->f, "%-16s %-8s %-8s\n", "Opcode", "created", "->Id");
404 for (entry = pset_first(set); entry; entry = pset_next(set)) {
405 fprintf(status->f, "%-16s %8d %8d\n",
406 get_id_str(entry->op->name), entry->new_node.cnt[0], entry->into_Id.cnt[0]);
410 static const char *opt_names[] = {
411 "straightening optimization",
413 "algebraic simplification",
415 "Write-After-Write optimization",
416 "Write-After-Read optimization",
417 "Tuple optimization",
419 "Constant evaluation",
424 * dumps a optimization hash
426 static void dump_opt_hash(pset *set, int index)
428 opt_entry_t *entry = pset_first(set);
431 fprintf(status->f, "\n%s:\n", opt_names[index]);
432 fprintf(status->f, "%-16s %-8s\n", "Opcode", "deref");
434 for (; entry; entry = pset_next(set)) {
435 fprintf(status->f, "%-16s %8d\n",
436 get_id_str(entry->op->name), entry->count.cnt[0]);
441 /* Finish the statistics */
442 void stat_finish(void)
445 graph_entry_t *entry;
446 ir_graph *const_irg = get_const_code_irg();
449 fprintf(status->f, "\nGlobal counts:\n");
450 dump_opcode_hash(status->opcode_hash);
452 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
453 entity *ent = entry->irg->ent;
455 if (entry->irg == const_irg) {
456 fprintf(status->f, "\nConst code Irg %p", entry->irg);
460 fprintf(status->f, "\nEntity %s, Irg %p", get_entity_name(ent), entry->irg);
462 fprintf(status->f, "\nIrg %p", entry->irg);
465 fprintf(status->f, " walked %d over blocks %d:\n", entry->walked.cnt[0], entry->walked_blocks.cnt[0]);
467 dump_opcode_hash(entry->opcode_hash);
469 for (i = 0; i < sizeof(entry->opt_hash)/sizeof(entry->opt_hash[0]); ++i) {
470 dump_opt_hash(entry->opt_hash[i], i);