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.
16 #ifdef FIRM_STATISTICS
22 #include "firmstat_t.h"
27 * names of the optimizations
29 static const char *opt_names[] = {
30 "straightening optimization",
32 "algebraic simplification",
34 "Write-After-Write optimization",
35 "Write-After-Read optimization",
36 "Read-After-Write optimization",
39 "Constant evaluation",
44 * need this to be static
46 static ir_op _op_Phi0, _op_PhiM;
48 /* ---------------------------------------------------------------------------------- */
50 #define STAT_ENTER ++status->recursive
51 #define STAT_LEAVE --status->recursive
52 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
57 static stat_info_t _status, *status = &_status;
60 * compare two elements of the opcode hash
62 static int opcode_cmp(const void *elt, const void *key)
64 const node_entry_t *e1 = elt;
65 const node_entry_t *e2 = key;
67 return e1->op->code - e2->op->code;
71 * compare two elements of the graph hash
73 static int graph_cmp(const void *elt, const void *key)
75 const graph_entry_t *e1 = elt;
76 const graph_entry_t *e2 = key;
78 return e1->irg != e2->irg;
82 * compare two elements of the optimization hash
84 static int opt_cmp(const void *elt, const void *key)
86 const opt_entry_t *e1 = elt;
87 const opt_entry_t *e2 = key;
89 return e1->op->code != e2->op->code;
93 * compare two elements of the block hash
95 static int block_cmp(const void *elt, const void *key)
97 const block_entry_t *e1 = elt;
98 const block_entry_t *e2 = key;
100 return e1->block_nr != e2->block_nr;
104 * compare two elements of the ir_op hash
106 static int opcode_cmp_2(const void *elt, const void *key)
108 const ir_op *e1 = elt;
109 const ir_op *e2 = key;
111 return e1->code != e2->code;
115 * clears all counter in a node_entry_t
117 static void opcode_clear_entry(node_entry_t *elem)
119 cnt_clr(&elem->cnt_alive);
120 cnt_clr(&elem->new_node);
121 cnt_clr(&elem->into_Id);
125 * Returns the associates node_entry_t for an ir_op
127 static node_entry_t *opcode_get_entry(const ir_op *op, pset *set)
134 elem = pset_find(set, &key, op->code);
138 elem = obstack_alloc(&status->cnts, sizeof(*elem));
141 opcode_clear_entry(elem);
145 return pset_insert(set, elem, op->code);
149 * Returns the associates ir_op for an opcode
151 static ir_op *opcode_find_entry(opcode code, pset *set)
156 return pset_find(set, &key, code);
160 * calculates a hash value for an irg
161 * Addresses are typically aligned at 32bit, so we ignore the lowest bits
163 static INLINE unsigned irg_hash(const ir_graph *irg)
165 return (unsigned)irg >> 3;
169 * clears all counter in a graph_entry_t
171 static void graph_clear_entry(graph_entry_t *elem)
173 cnt_clr(&elem->cnt_walked);
174 cnt_clr(&elem->cnt_walked_blocks);
175 cnt_clr(&elem->cnt_got_inlined);
176 cnt_clr(&elem->cnt_was_inlined);
177 cnt_clr(&elem->cnt_edges);
181 * Returns the acssociates graph_entry_t for an irg
183 static graph_entry_t *graph_get_entry(ir_graph *irg, pset *set)
191 elem = pset_find(set, &key, irg_hash(irg));
195 elem = obstack_alloc(&status->cnts, sizeof(*elem));
198 graph_clear_entry(elem);
200 /* new hash table for opcodes here */
201 elem->opcode_hash = new_pset(opcode_cmp, 5);
202 elem->block_hash = new_pset(block_cmp, 5);
205 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
206 elem->opt_hash[i] = new_pset(opt_cmp, 4);
208 return pset_insert(set, elem, irg_hash(irg));
212 * clears all counter in an opt_entry_t
214 static void opt_clear_entry(opt_entry_t *elem)
216 cnt_clr(&elem->count);
220 * Returns the associates opt_entry_t for an ir_op
222 static opt_entry_t *opt_get_entry(const ir_op *op, pset *set)
229 elem = pset_find(set, &key, op->code);
233 elem = obstack_alloc(&status->cnts, sizeof(*elem));
235 /* clear new counter */
236 opt_clear_entry(elem);
240 return pset_insert(set, elem, op->code);
244 * clears all counter in a block_entry_t
246 static void block_clear_entry(block_entry_t *elem)
248 cnt_clr(&elem->cnt_nodes);
249 cnt_clr(&elem->cnt_edges);
250 cnt_clr(&elem->cnt_in_edges);
251 cnt_clr(&elem->cnt_out_edges);
255 * Returns the associates block_entry_t for an block
257 static block_entry_t *block_get_entry(long block_nr, pset *set)
262 key.block_nr = block_nr;
264 elem = pset_find(set, &key, block_nr);
268 elem = obstack_alloc(&status->cnts, sizeof(*elem));
270 /* clear new counter */
271 block_clear_entry(elem);
273 elem->block_nr = block_nr;
275 return pset_insert(set, elem, block_nr);
280 * Returns the ir_op for an IR-node,
281 * handles special cases and return pseudo op codes
283 static ir_op *stat_get_irn_op(const ir_node *node)
285 ir_op *op = get_irn_op(node);
287 if (op->code == iro_Phi && get_irn_arity(node) == 0) {
288 /* special case, a Phi0 node, count on extra counter */
289 op = status->op_Phi0;
291 else if (op->code == iro_Phi && get_irn_mode(node) == mode_M) {
292 /* special case, a Memory Phi node, count on extra counter */
293 op = status->op_PhiM;
299 * update the block counter
301 static void count_block_info(ir_node *node, graph_entry_t *graph)
303 ir_op *op = get_irn_op(node);
305 block_entry_t *b_entry;
308 /* check for block */
309 if (op == op_Block) {
310 arity = get_irn_arity(node);
311 b_entry = block_get_entry(get_irn_node_nr(node), graph->block_hash);
313 /* count all incoming edges */
314 for (i = 0; i < arity; ++i) {
315 ir_node *pred = get_irn_n(node, i);
316 ir_node *other_block = get_nodes_block(pred);
317 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
319 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
320 cnt_inc(&b_entry_other->cnt_out_edges);
324 else if (op == op_Call) {
328 block = get_nodes_block(node);
329 b_entry = block_get_entry(get_irn_node_nr(block), graph->block_hash);
331 /* we have a new nodes */
332 cnt_inc(&b_entry->cnt_nodes);
334 arity = get_irn_arity(node);
336 for (i = 0; i < arity; ++i) {
337 ir_node *pred = get_irn_n(node, i);
338 ir_node *other_block;
340 if (get_irn_op(pred) == op_Block)
343 other_block = get_nodes_block(pred);
345 if (other_block == block)
346 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
348 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
350 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
351 cnt_inc(&b_entry_other->cnt_out_edges);
357 * walker for reachable nodes count
359 static void count_nodes(ir_node *node, void *env)
361 graph_entry_t *graph = env;
364 ir_op *op = stat_get_irn_op(node);
365 int arity = get_irn_arity(node);
367 entry = opcode_get_entry(op, graph->opcode_hash);
369 cnt_inc(&entry->cnt_alive);
370 cnt_add_i(&graph->cnt_edges, arity);
372 /* count block edges */
373 count_block_info(node, graph);
377 * count all alive nodes and edges in a graph
379 static void count_nodes_in_graph(graph_entry_t *global, graph_entry_t *graph)
383 /* clear first the alive counter in the graph */
384 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
385 cnt_clr(&entry->cnt_alive);
388 /* count the nodes in the graph */
389 irg_walk_graph(graph->irg, count_nodes, NULL, graph);
391 /* assume we walk every graph only ONCE, we could sum here the global count */
392 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
393 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
395 /* update the node counter */
396 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
399 /* update the edge counter */
400 cnt_add(&global->cnt_edges, &graph->cnt_edges);
406 static void stat_register_dumper(dumper_t *dumper)
408 dumper->next = status->dumper;
409 status->dumper = dumper;
415 static void dump_graph(graph_entry_t *entry)
419 for (dumper = status->dumper; dumper; dumper = dumper->next) {
420 if (dumper->dump_graph)
421 dumper->dump_graph(dumper, entry);
426 * initialise the dumper
428 static void dump_init(const char *name)
432 for (dumper = status->dumper; dumper; dumper = dumper->next) {
434 dumper->init(dumper, name);
441 static void dump_finish(void)
445 for (dumper = status->dumper; dumper; dumper = dumper->next) {
447 dumper->finish(dumper);
451 /* ---------------------------------------------------------------------- */
454 * dumps a opcode hash into human readable form
456 static void simple_dump_opcode_hash(dumper_t *dmp, pset *set)
460 counter_t f_new_node;
464 cnt_clr(&f_new_node);
467 fprintf(dmp->f, "%-16s %-8s %-8s %-8s\n", "Opcode", "alive", "created", "->Id");
468 for (entry = pset_first(set); entry; entry = pset_next(set)) {
469 fprintf(dmp->f, "%-16s %8d %8d %8d\n",
470 get_id_str(entry->op->name), entry->cnt_alive.cnt[0], entry->new_node.cnt[0], entry->into_Id.cnt[0]);
472 cnt_add(&f_alive, &entry->cnt_alive);
473 cnt_add(&f_new_node, &entry->new_node);
474 cnt_add(&f_Id, &entry->into_Id);
476 fprintf(dmp->f, "-------------------------------------------\n");
477 fprintf(dmp->f, "%-16s %8d %8d %8d\n", "Sum",
484 * dumps a optimization hash into human readable form
486 static void simple_dump_opt_hash(dumper_t *dmp, pset *set, int index)
488 opt_entry_t *entry = pset_first(set);
491 fprintf(dmp->f, "\n%s:\n", opt_names[index]);
492 fprintf(dmp->f, "%-16s %-8s\n", "Opcode", "deref");
494 for (; entry; entry = pset_next(set)) {
495 fprintf(dmp->f, "%-16s %8d\n",
496 get_id_str(entry->op->name), entry->count.cnt[0]);
502 * dumps the endges count
504 static void simple_dump_edges(dumper_t *dmp, counter_t *cnt)
506 fprintf(dmp->f, "%-16s %8d\n", "Edges", cnt->cnt[0]);
512 static void simple_dump_graph(dumper_t *dmp, graph_entry_t *entry)
515 block_entry_t *b_entry;
518 ir_graph *const_irg = get_const_code_irg();
520 if (entry->irg == const_irg) {
521 fprintf(dmp->f, "\nConst code Irg %p", (void *)entry->irg);
525 fprintf(dmp->f, "\nEntity %s, Irg %p", get_entity_name(entry->ent), (void *)entry->irg);
527 fprintf(dmp->f, "\nIrg %p", (void *)entry->irg);
530 fprintf(dmp->f, " %swalked %d over blocks %d was inlined %d got inlined %d:\n",
531 entry->deleted ? "DELETED " : "",
532 entry->cnt_walked.cnt[0], entry->cnt_walked_blocks.cnt[0],
533 entry->cnt_was_inlined.cnt[0],
534 entry->cnt_got_inlined.cnt[0]
538 fprintf(dmp->f, "\nGlobals counts:\n");
539 fprintf(dmp->f, "--------------\n");
543 simple_dump_opcode_hash(dmp, entry->opcode_hash);
544 simple_dump_edges(dmp, &entry->cnt_edges);
546 /* effects of optimizations */
550 for (i = 0; i < sizeof(entry->opt_hash)/sizeof(entry->opt_hash[0]); ++i) {
551 simple_dump_opt_hash(dmp, entry->opt_hash[i], i);
554 /* dump block info */
555 fprintf(dmp->f, "\n%12s %12s %12s %12s %12s %12s\n", "Block Nr", "Nodes", "intern E", "incoming E", "outgoing E", "quot");
556 for (b_entry = pset_first(entry->block_hash);
558 b_entry = pset_next(entry->block_hash)) {
559 fprintf(dmp->f, "BLK %12ld %12u %12u %12u %12u %4.8f\n",
561 b_entry->cnt_nodes.cnt[0],
562 b_entry->cnt_edges.cnt[0],
563 b_entry->cnt_in_edges.cnt[0],
564 b_entry->cnt_out_edges.cnt[0],
565 (double)b_entry->cnt_edges.cnt[0] / (double)b_entry->cnt_nodes.cnt[0]
572 * initialise the simple dumper
574 static void simple_init(dumper_t *dmp, const char *name)
578 snprintf(fname, sizeof(fname), "%s.txt", name);
579 dmp->f = fopen(fname, "w");
583 * finishes the simple dumper
585 static void simple_finish(dumper_t *dmp)
592 * the simple human readable dumper
594 static dumper_t simple_dumper = {
602 /* ---------------------------------------------------------------------- */
605 * count the nodes as needed:
607 * 1 normal (data) Phi's
612 static void csv_count_nodes(graph_entry_t *graph, counter_t cnt[])
617 for (i = 0; i < 4; ++i)
620 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
621 if (entry->op == op_Phi) {
623 cnt_add(&cnt[1], &entry->cnt_alive);
625 else if (entry->op == status->op_PhiM) {
627 cnt_add(&cnt[2], &entry->cnt_alive);
629 else if (entry->op == op_Proj) {
631 cnt_add(&cnt[3], &entry->cnt_alive);
634 /* all other nodes */
635 cnt_add(&cnt[0], &entry->cnt_alive);
643 static void csv_dump_graph(dumper_t *dmp, graph_entry_t *entry)
650 ir_graph *const_irg = get_const_code_irg();
652 if (entry->irg == const_irg) {
653 name = "<Const code Irg>";
658 name = get_entity_name(entry->ent);
660 name = "<UNKNOWN IRG>";
663 csv_count_nodes(entry, cnt);
665 fprintf(dmp->f, "%-40s, %p, %d, %d, %d, %d\n",
677 * initialise the simple dumper
679 static void csv_init(dumper_t *dmp, const char *name)
683 snprintf(fname, sizeof(fname), "%s.csv", name);
684 dmp->f = fopen(fname, "a");
688 * finishes the simple dumper
690 static void csv_finish(dumper_t *dmp)
697 * the simple human readable dumper
699 static dumper_t csv_dumper = {
708 /* ---------------------------------------------------------------------- */
711 * helper: get an ir_op from an opcode
713 ir_op *stat_get_op_from_opcode(opcode code)
715 return opcode_find_entry(code, status->ir_op_hash);
718 /* initialize the statistics module. */
719 void init_stat(unsigned enable_options)
721 #define X(a) a, sizeof(a)-1
725 /* enable statistics */
726 status->enable = enable_options & FIRMSTAT_ENABLED;
728 if (! status->enable)
731 obstack_init(&status->cnts);
733 /* build the pseudo-ops */
734 _op_Phi0.code = --pseudo_id;
735 _op_Phi0.name = new_id_from_chars(X("Phi0"));
737 _op_PhiM.code = --pseudo_id;
738 _op_PhiM.name = new_id_from_chars(X("PhiM"));
740 /* create the hash-tables */
741 status->irg_hash = new_pset(graph_cmp, 8);
742 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
744 status->op_Phi0 = &_op_Phi0;
745 status->op_PhiM = &_op_PhiM;
747 stat_register_dumper(&simple_dumper);
748 stat_register_dumper(&csv_dumper);
750 /* initialize the pattern hash */
751 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
755 /* A new IR op is registered. */
756 void stat_new_ir_op(const ir_op *op)
758 if (! status->enable)
763 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
765 /* execute for side effect :-) */
766 opcode_get_entry(op, graph->opcode_hash);
768 pset_insert(status->ir_op_hash, op, op->code);
773 /* An IR op is freed. */
774 void stat_free_ir_op(const ir_op *op)
776 if (! status->enable)
785 /* A new node is created. */
786 void stat_new_node(const ir_node *node)
788 if (! status->enable)
791 /* do NOT count during dead node elimination */
792 if (status->in_dead_node_elim > 0)
798 graph_entry_t *graph;
799 ir_op *op = stat_get_irn_op(node);
801 /* increase global value */
802 graph = graph_get_entry(NULL, status->irg_hash);
803 entry = opcode_get_entry(op, graph->opcode_hash);
804 cnt_inc(&entry->new_node);
806 /* increase local value */
807 graph = graph_get_entry(current_ir_graph, status->irg_hash);
808 entry = opcode_get_entry(op, graph->opcode_hash);
809 cnt_inc(&entry->new_node);
814 /* A node is changed into a Id node */
815 void stat_turn_into_id(const ir_node *node)
817 if (! status->enable)
823 graph_entry_t *graph;
824 ir_op *op = stat_get_irn_op(node);
826 /* increase global value */
827 graph = graph_get_entry(NULL, status->irg_hash);
828 entry = opcode_get_entry(op, graph->opcode_hash);
829 cnt_inc(&entry->into_Id);
831 /* increase local value */
832 graph = graph_get_entry(current_ir_graph, status->irg_hash);
833 entry = opcode_get_entry(op, graph->opcode_hash);
834 cnt_inc(&entry->into_Id);
839 /* A new graph was created */
840 void stat_new_graph(ir_graph *irg, entity *ent)
842 if (! status->enable)
847 /* execute for side effect :-) */
848 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
857 * A graph was deleted
859 void stat_free_graph(ir_graph *irg)
861 if (! status->enable)
866 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
867 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
871 /* count the nodes of the graph yet, it will be destroyed later */
872 count_nodes_in_graph(global, graph);
874 /* count the DAG's */
875 count_dags_in_graph(global, graph);
877 /* calculate the pattern */
878 stat_calc_pattern_history(irg);
884 * A walk over a graph is initiated. Do not count walks from statistic code.
886 void stat_irg_walk(ir_graph *irg, void *pre, void *post)
888 if (! status->enable)
893 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
895 cnt_inc(&graph->cnt_walked);
901 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
903 void stat_irg_walk_blkwise(ir_graph *irg, void *pre, void *post)
905 /* for now, do NOT differentiate between blockwise and normal */
906 stat_irg_walk(irg, pre, post);
910 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
912 void stat_irg_block_walk(ir_graph *irg, const ir_node *node, void *pre, void *post)
914 if (! status->enable)
919 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
921 cnt_inc(&graph->cnt_walked_blocks);
927 * called for every node that is removed due to an optimization
929 static void removed_due_opt(ir_node *n, pset *set)
931 ir_op *op = get_irn_op(n);
932 opt_entry_t *entry = opt_get_entry(op, set);
934 /* increase global value */
935 cnt_inc(&entry->count);
939 * Some nodes were optimized into some others due to an optimization
941 void stat_merge_nodes(
942 ir_node **new_node_array, int new_num_entries,
943 ir_node **old_node_array, int old_num_entries,
946 if (! status->enable)
952 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
954 for (i = 0; i < old_num_entries; ++i) {
955 for (j = 0; j < new_num_entries; ++j)
956 if (old_node_array[i] == new_node_array[j])
959 /* nodes might be in new and old, these are NOT removed */
960 if (j >= new_num_entries) {
961 removed_due_opt(old_node_array[i], graph->opt_hash[opt]);
969 * A node was lowered into other nodes
971 void stat_lower(ir_node *node)
973 if (! status->enable)
978 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
980 removed_due_opt(node, graph->opt_hash[STAT_LOWERED]);
986 * A graph was inlined
988 void stat_inline(ir_node *call, ir_graph *called_irg)
990 if (! status->enable)
995 ir_graph *irg = get_irn_irg(call);
996 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
997 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
999 cnt_inc(&graph->cnt_got_inlined);
1000 cnt_inc(&i_graph->cnt_was_inlined);
1006 * Start the dead node elimination.
1008 void stat_dead_node_elim_start(ir_graph *irg)
1010 if (! status->enable)
1013 ++status->in_dead_node_elim;
1017 * Stops the dead node elimination.
1019 void stat_dead_node_elim_stop(ir_graph *irg)
1021 if (! status->enable)
1024 --status->in_dead_node_elim;
1027 /* Finish the statistics */
1028 void stat_finish(const char *name)
1030 if (! status->enable)
1035 graph_entry_t *entry;
1036 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1040 /* dump per graph */
1041 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1043 if (entry->irg == NULL) {
1044 /* special entry for the global count */
1048 if (! entry->deleted) {
1049 /* the graph is still alive, count the nodes on it */
1050 count_nodes_in_graph(global, entry);
1052 /* count the DAG's */
1053 count_dags_in_graph(global, entry);
1055 /* calculate the pattern */
1056 stat_calc_pattern_history(entry->irg);
1061 /* clear the counter here:
1062 * we need only the edge counter to be cleared, all others are cumulative
1064 cnt_clr(&entry->cnt_edges);
1068 // dump_graph(global);
1071 stat_finish_pattern_history();
1073 /* clear the global counter here */
1075 node_entry_t *entry;
1077 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1078 opcode_clear_entry(entry);
1080 graph_clear_entry(global);
1084 // status->enable = 0;
1091 /* need this for prototypes */
1092 #define FIRM_STATISTICS
1093 #include "firmstat.h"
1095 void init_stat(unsigned enable_options) {}
1097 void stat_finish(const char *name) {}
1099 void stat_new_ir_op(const ir_op *op) {}
1101 void stat_free_ir_op(const ir_op *op) {}
1103 void stat_new_node(const ir_node *node) {}
1105 void stat_turn_into_id(const ir_node *node) {}
1107 void stat_new_graph(ir_graph *irg, entity *ent) {}
1109 void stat_free_graph(ir_graph *irg) {}
1111 void stat_irg_walk(ir_graph *irg, void *pre, void *post) {}
1113 void stat_irg_block_walk(ir_graph *irg, const ir_node *node, void *pre, void *post) {}
1115 void stat_merge_nodes(
1116 ir_node **new_node_array, int new_num_entries,
1117 ir_node **old_node_array, int old_num_entries,
1118 stat_opt_kind opt) {}
1120 void stat_lower(ir_node *node) {}
1122 void stat_inline(ir_node *call, ir_graph *irg) {}
1124 void stat_dead_node_elim_start(ir_graph *irg) {}
1126 void stat_dead_node_elim_stop(ir_graph *irg) {}