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"
28 #undef obstack_chunk_alloc
29 #undef obstack_chunk_free
30 #define obstack_chunk_alloc malloc
31 #define obstack_chunk_free free
34 #ifdef FIRM_STATISTICS
39 * 32 bit should be enough for now
41 #define STAT_CNT_NUM 1
43 typedef struct _counter_t {
44 unsigned cnt[STAT_CNT_NUM];
48 * An entry for ir_nodes
50 typedef struct _node_entry_t {
51 counter_t count; /**< amount of nodes in this entry */
52 counter_t new_node; /**< amount of new nodes for this entry */
53 counter_t into_Id; /**< amount of nodes that turned into Id's for this entry */
54 const ir_op *op; /**< the op for this entry */
58 * An entry for ir_graphs
60 typedef struct _graph_entry_t {
61 pset *opcode_hash; /**< hash map containing the opcode counter */
62 counter_t walked; /**< walker walked over the graph */
63 counter_t walked_blocks; /**< walker walked over the graph blocks */
64 counter_t was_inlined; /**< number of times other graph were inlined */
65 counter_t got_inlined; /**< number of times this graph was inlined */
66 pset *opt_hash[STAT_OPT_MAX]; /**< hash maps containing opcode counter for optimizations */
67 ir_graph *irg; /**< the graph of this object */
68 entity *ent; /**< the entity of this graph if one exists */
69 int deleted; /**< set if this irg was deleted */
73 * An entry for optimized ir_nodes
75 typedef struct _opt_entry_t {
76 counter_t count; /**< optimization counter */
77 const ir_op *op; /**< the op for this entry */
83 typedef struct _statistic_info_t {
84 struct obstack cnts; /**< obstack containing the counters */
85 pset *opcode_hash; /**< hash map containing the opcode counter */
86 pset *irg_hash; /**< hash map containing the counter for irgs */
87 FILE *f; /**< outputfile */
88 ir_op *op_Phi0; /**< needed pseudo op */
89 int recursive; /**< flag for detecting recursive hook calls */
90 int in_dead_node_elim; /**< set, if dead node elimination runs */
93 #define STAT_ENTER ++status->recursive
94 #define STAT_LEAVE --status->recursive
95 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
100 static stat_info_t _status, *status = &_status;
105 static INLINE void cnt_inc(counter_t *cnt)
109 for (i = 0; i < STAT_CNT_NUM; ++i) {
118 static INLINE void cnt_dec(counter_t *cnt)
122 for (i = 0; i < STAT_CNT_NUM; ++i) {
123 if (--cnt->cnt[i] != -1)
129 * set a counter to zero
131 static INLINE void cnt_clr(counter_t *cnt)
133 memset(cnt->cnt, 0, sizeof(cnt->cnt));
137 * add a counter to another
139 static inline void cnt_add(counter_t *dst, const counter_t *src)
143 for (i = 0; i < STAT_CNT_NUM; ++i) {
144 unsigned a = dst->cnt[i] + src->cnt[i];
145 unsigned no_carry = a >= dst->cnt[i];
157 * compare two elements of the opcode hash
159 static int opcode_cmp(const void *elt, const void *key)
161 const node_entry_t *e1 = elt;
162 const node_entry_t *e2 = key;
164 return e1->op->code - e2->op->code;
168 * compare two elements of the graph hash
170 static int graph_cmp(const void *elt, const void *key)
172 const graph_entry_t *e1 = elt;
173 const graph_entry_t *e2 = key;
175 return e1->irg != e2->irg;
179 * compare two elements of the optimization hash
181 static int opt_cmp(const void *elt, const void *key)
183 const opt_entry_t *e1 = elt;
184 const opt_entry_t *e2 = key;
186 return e1->op->code != e2->op->code;
190 * Returns the associates node_entry_t for an ir_op
192 static node_entry_t *opcode_get_entry(const ir_op *op, pset *set)
199 elem = pset_find(set, &key, op->code);
203 elem = obstack_alloc(&status->cnts, sizeof(*elem));
206 cnt_clr(&elem->count);
207 cnt_clr(&elem->new_node);
208 cnt_clr(&elem->into_Id);
212 return pset_insert(set, elem, op->code);
216 * calculates a hash value for an irg
217 * Addresses are typically aligned at 32bit, so we ignore the lowest bits
219 static INLINE unsigned irg_hash(const ir_graph *irg)
221 return (unsigned)irg >> 3;
225 * Returns the acssociates graph_entry_t for an irg
227 static graph_entry_t *graph_get_entry(ir_graph *irg, pset *set)
235 elem = pset_find(set, &key, irg_hash(irg));
239 elem = obstack_alloc(&status->cnts, sizeof(*elem));
241 cnt_clr(&elem->walked);
242 cnt_clr(&elem->walked_blocks);
243 cnt_clr(&elem->got_inlined);
244 cnt_clr(&elem->was_inlined);
246 /* new hash table for opcodes here */
247 elem->opcode_hash = new_pset(opcode_cmp, 5);
250 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
251 elem->opt_hash[i] = new_pset(opt_cmp, 4);
253 return pset_insert(set, elem, irg_hash(irg));
257 * Returns the associates opt_entry_t for an ir_op
259 static opt_entry_t *opt_get_entry(const ir_op *op, pset *set)
266 elem = pset_find(set, &key, op->code);
270 elem = obstack_alloc(&status->cnts, sizeof(*elem));
272 /* clear new counter */
273 cnt_clr(&elem->count);
277 return pset_insert(set, elem, op->code);
281 * walker for reachable nodes count
283 static void count_nodes(ir_node *node, void *env)
287 ir_op *op = get_irn_op(node);
289 if (op->code == iro_Phi && get_irn_arity(node) == 0) {
290 /* special case, a Phi0 node, count on extra counter */
291 op = status->op_Phi0;
294 entry = opcode_get_entry(op, set);
296 cnt_inc(&entry->count);
300 * count all reachable nodes in a graph
302 static void count_nodes_in_graph(ir_graph *irg, pset *set)
304 irg_walk_graph(irg, count_nodes, NULL, set);
307 /* ---------------------------------------------------------------------- */
309 /* initialize the statistics module. */
312 obstack_init(&status->cnts);
314 /* create the hash-tables */
315 status->opcode_hash = new_pset(opcode_cmp, 8);
316 status->irg_hash = new_pset(graph_cmp, 8);
318 status->f = fopen("firmstat.txt", "w");
319 status->op_Phi0 = NULL;
322 /* A new IR op is registered. */
323 void stat_new_ir_op(const ir_op *op)
327 /* execute for side effect :-) */
328 opcode_get_entry(op, status->opcode_hash);
333 /* An IR op is freed. */
334 void stat_free_ir_op(const ir_op *op)
342 /* A new node is created. */
343 void stat_new_node(const ir_node *node)
345 /* do NOT count during dead node elimination */
346 if (status->in_dead_node_elim > 0)
352 graph_entry_t *graph;
353 ir_op *op = get_irn_op(node);
355 if (op->code == iro_Phi && get_irn_arity(node) == 0) {
356 /* special case, a Phi0 node, count on extra counter */
357 if (! status->op_Phi0) {
358 ir_op *op_Phi = get_op_Phi();
360 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);
362 op = status->op_Phi0;
365 entry = opcode_get_entry(op, status->opcode_hash);
366 graph = graph_get_entry(current_ir_graph, status->irg_hash);
368 /* increase global value */
369 cnt_inc(&entry->new_node);
371 /* increase local value */
372 entry = opcode_get_entry(op, graph->opcode_hash);
373 cnt_inc(&entry->new_node);
378 /* A node is changed into a Id node */
379 void stat_turn_into_id(const ir_node *node)
384 graph_entry_t *graph;
385 ir_op *op = get_irn_op(node);
387 if (op->code == iro_Phi && get_irn_arity(node) == 0) {
388 /* special case, a Phi0 node, count on extra counter */
389 if (! status->op_Phi0) {
390 ir_op *op_Phi = get_op_Phi();
392 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);
394 op = status->op_Phi0;
397 entry = opcode_get_entry(op, status->opcode_hash);
398 graph = graph_get_entry(current_ir_graph, status->irg_hash);
400 /* increase global value */
401 cnt_inc(&entry->into_Id);
403 /* increase local value */
404 entry = opcode_get_entry(op, graph->opcode_hash);
405 cnt_inc(&entry->into_Id);
410 /* A new graph was created */
411 void stat_new_graph(ir_graph *irg, entity *ent)
415 /* execute for side effect :-) */
416 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
425 * A graph was deleted
427 void stat_free_graph(ir_graph *irg)
431 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
435 /* count the nodes of the graph yet, it will be destroyed later */
436 count_nodes_in_graph(irg, graph->opcode_hash);
442 * A walk over a graph is initiated. Do not count walks from statistic code.
444 void stat_irg_walk(ir_graph *irg, void *pre, void *post)
448 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
450 cnt_inc(&graph->walked);
456 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
458 void stat_irg_block_walk(ir_graph *irg, const ir_node *node, void *pre, void *post)
462 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
464 cnt_inc(&graph->walked_blocks);
470 * called for every node that is removed due to an optimization
472 static void removed_due_opt(ir_node *n, pset *set)
474 ir_op *op = get_irn_op(n);
475 opt_entry_t *entry = opt_get_entry(op, set);
477 /* increase global value */
478 cnt_inc(&entry->count);
482 * Some nodes were optimized into some others due to an optimization
484 void stat_merge_nodes(
485 ir_node **new_node_array, int new_num_entries,
486 ir_node **old_node_array, int old_num_entries,
492 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
494 for (i = 0; i < old_num_entries; ++i) {
495 for (j = 0; j < new_num_entries; ++j)
496 if (old_node_array[i] == new_node_array[j])
499 /* nodes might be in new and old, these are NOT removed */
500 if (j >= new_num_entries) {
501 removed_due_opt(old_node_array[i], graph->opt_hash[opt]);
509 * A node was lowered into other nodes
511 void stat_lower(ir_node *node)
515 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
517 removed_due_opt(node, graph->opt_hash[STAT_LOWERED]);
523 * A graph was inlined
525 void stat_inline(ir_node *call, ir_graph *called_irg)
529 ir_graph *irg = get_irn_irg(call);
530 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
531 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
533 cnt_inc(&graph->got_inlined);
534 cnt_inc(&i_graph->was_inlined);
540 * Start the dead node elimination.
542 void stat_dead_node_elim_start(ir_graph *irg)
544 ++status->in_dead_node_elim;
548 * Stops the dead node elimination.
550 void stat_dead_node_elim_stop(ir_graph *irg)
552 --status->in_dead_node_elim;
556 * dumps a opcode hash
558 static void dump_opcode_hash(pset *set)
562 counter_t f_new_node;
566 cnt_clr(&f_new_node);
569 fprintf(status->f, "%-16s %-8s %-8s %-8s\n", "Opcode", "alive", "created", "->Id");
570 for (entry = pset_first(set); entry; entry = pset_next(set)) {
571 fprintf(status->f, "%-16s %8d %8d %8d\n",
572 get_id_str(entry->op->name), entry->count.cnt[0], entry->new_node.cnt[0], entry->into_Id.cnt[0]);
574 cnt_add(&f_count, &entry->count);
575 cnt_add(&f_new_node, &entry->new_node);
576 cnt_add(&f_Id, &entry->into_Id);
578 fprintf(status->f, "-------------------------------------------\n");
579 fprintf(status->f, "%-16s %8d %8d %8d\n", "Sum",
585 static const char *opt_names[] = {
586 "straightening optimization",
588 "algebraic simplification",
590 "Write-After-Write optimization",
591 "Write-After-Read optimization",
592 "Tuple optimization",
594 "Constant evaluation",
599 * dumps a optimization hash
601 static void dump_opt_hash(pset *set, int index)
603 opt_entry_t *entry = pset_first(set);
606 fprintf(status->f, "\n%s:\n", opt_names[index]);
607 fprintf(status->f, "%-16s %-8s\n", "Opcode", "deref");
609 for (; entry; entry = pset_next(set)) {
610 fprintf(status->f, "%-16s %8d\n",
611 get_id_str(entry->op->name), entry->count.cnt[0]);
616 /* Finish the statistics */
617 void stat_finish(void)
622 graph_entry_t *entry;
623 ir_graph *const_irg = get_const_code_irg();
626 fprintf(status->f, "\nGlobal counts:\n");
627 dump_opcode_hash(status->opcode_hash);
629 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
630 entity *ent = entry->ent;
632 if (! entry->deleted) {
633 /* the graph is still alive, count the nodes on it */
634 count_nodes_in_graph(entry->irg, entry->opcode_hash);
637 if (entry->irg == const_irg) {
638 fprintf(status->f, "\nConst code Irg %p", entry->irg);
642 fprintf(status->f, "\nEntity %s, Irg %p", get_entity_name(ent), entry->irg);
644 fprintf(status->f, "\nIrg %p", entry->irg);
647 fprintf(status->f, " %swalked %d over blocks %d was inlined %d got inlined %d:\n",
648 entry->deleted ? "DELETED " : "",
649 entry->walked.cnt[0], entry->walked_blocks.cnt[0],
650 entry->was_inlined.cnt[0],
651 entry->got_inlined.cnt[0]
654 dump_opcode_hash(entry->opcode_hash);
656 for (i = 0; i < sizeof(entry->opt_hash)/sizeof(entry->opt_hash[0]); ++i) {
657 dump_opt_hash(entry->opt_hash[i], i);
668 /* need this for prototypes */
669 #define FIRM_STATISTICS
670 #include "firmstat.h"
672 void stat_init(void) {}
674 void stat_finish(void) {}
676 void stat_new_ir_op(const ir_op *op) {}
678 void stat_free_ir_op(const ir_op *op) {}
680 void stat_new_node(const ir_node *node) {}
682 void stat_turn_into_id(const ir_node *node) {}
684 void stat_new_graph(ir_graph *irg, entity *ent) {}
686 void stat_free_graph(ir_graph *irg) {}
688 void stat_irg_walk(ir_graph *irg, void *pre, void *post) {}
690 void stat_irg_block_walk(ir_graph *irg, const ir_node *node, void *pre, void *post) {}
692 void stat_merge_nodes(
693 ir_node **new_node_array, int new_num_entries,
694 ir_node **old_node_array, int old_num_entries,
695 stat_opt_kind opt) {}
697 void stat_lower(ir_node *node) {}
699 void stat_inline(ir_node *call, ir_graph *irg) {}
701 void stat_dead_node_elim_start(ir_graph *irg) {}
703 void stat_dead_node_elim_stop(ir_graph *irg) {}