2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Statistics for Firm.
20 #include "firmstat_t.h"
32 #include "lc_opts_enum.h"
35 * need this to be static:
36 * Special pseudo Opcodes that we need to count some interesting cases
40 * The Phi0, a node that is created during SSA construction
42 static ir_op _op_Phi0;
44 /** The PhiM, just to count memory Phi's. */
45 static ir_op _op_PhiM;
47 /** The Mul by Const node. */
48 static ir_op _op_MulC;
50 /** The Div by Const node. */
51 static ir_op _op_DivC;
53 /** The Div by Const node. */
54 static ir_op _op_ModC;
56 /** The memory Proj node. */
57 static ir_op _op_ProjM;
60 static ir_op _op_SelSel;
62 /** A Sel of a Sel of a Sel */
63 static ir_op _op_SelSelSel;
65 static unsigned stat_options;
67 /* ---------------------------------------------------------------------------------- */
69 /** Marks the begin of a statistic (hook) function. */
70 #define STAT_ENTER ++status->recursive
72 /** Marks the end of a statistic (hook) functions. */
73 #define STAT_LEAVE --status->recursive
75 /** Allows to enter a statistic function only when we are not already in a hook. */
76 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
81 static const unsigned status_disable = 0;
82 static stat_info_t *status = (stat_info_t *)&status_disable;
85 * Compare two elements of the opcode hash.
87 static int opcode_cmp(const void *elt, const void *key)
89 const node_entry_t *e1 = (const node_entry_t*)elt;
90 const node_entry_t *e2 = (const node_entry_t*)key;
92 return e1->op->code - e2->op->code;
96 * Compare two elements of the graph hash.
98 static int graph_cmp(const void *elt, const void *key)
100 const graph_entry_t *e1 = (const graph_entry_t*)elt;
101 const graph_entry_t *e2 = (const graph_entry_t*)key;
103 return e1->irg != e2->irg;
107 * Compare two elements of the optimization hash.
109 static int opt_cmp(const void *elt, const void *key)
111 const opt_entry_t *e1 = (const opt_entry_t*)elt;
112 const opt_entry_t *e2 = (const opt_entry_t*)key;
114 return e1->op->code != e2->op->code;
118 * Compare two elements of the block hash.
120 static int block_cmp(const void *elt, const void *key)
122 const block_entry_t *e1 = (const block_entry_t*)elt;
123 const block_entry_t *e2 = (const block_entry_t*)key;
125 /* it's enough to compare the block number */
126 return e1->block_nr != e2->block_nr;
130 * Compare two elements of the be_block hash.
132 static int be_block_cmp(const void *elt, const void *key)
134 const be_block_entry_t *e1 = (const be_block_entry_t*)elt;
135 const be_block_entry_t *e2 = (const be_block_entry_t*)key;
137 return e1->block_nr != e2->block_nr;
141 * Compare two elements of reg pressure hash.
143 static int reg_pressure_cmp(const void *elt, const void *key)
145 const reg_pressure_entry_t *e1 = (const reg_pressure_entry_t*)elt;
146 const reg_pressure_entry_t *e2 = (const reg_pressure_entry_t*)key;
148 return e1->class_name != e2->class_name;
152 * Compare two elements of the perm_stat hash.
154 static int perm_stat_cmp(const void *elt, const void *key)
156 const perm_stat_entry_t *e1 = (const perm_stat_entry_t*)elt;
157 const perm_stat_entry_t *e2 = (const perm_stat_entry_t*)key;
159 return e1->perm != e2->perm;
163 * Compare two elements of the perm_class hash.
165 static int perm_class_cmp(const void *elt, const void *key)
167 const perm_class_entry_t *e1 = (const perm_class_entry_t*)elt;
168 const perm_class_entry_t *e2 = (const perm_class_entry_t*)key;
170 return e1->class_name != e2->class_name;
174 * Compare two elements of the ir_op hash.
176 static int opcode_cmp_2(const void *elt, const void *key)
178 const ir_op *e1 = (const ir_op*)elt;
179 const ir_op *e2 = (const ir_op*)key;
181 return e1->code != e2->code;
185 * Compare two elements of the address_mark set.
187 static int address_mark_cmp(const void *elt, const void *key, size_t size)
189 const address_mark_entry_t *e1 = (const address_mark_entry_t*)elt;
190 const address_mark_entry_t *e2 = (const address_mark_entry_t*)key;
193 /* compare only the nodes, the rest is used as data container */
194 return e1->node != e2->node;
198 * Clear all counter in a node_entry_t.
200 static void opcode_clear_entry(node_entry_t *elem)
202 cnt_clr(&elem->cnt_alive);
203 cnt_clr(&elem->new_node);
204 cnt_clr(&elem->into_Id);
205 cnt_clr(&elem->normalized);
209 * Returns the associates node_entry_t for an ir_op (and allocates
210 * one if not yet available).
212 * @param op the IR operation
213 * @param hmap a hash map containing ir_op* -> node_entry_t*
215 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
222 elem = (node_entry_t*)pset_find(hmap, &key, op->code);
226 elem = OALLOCZ(&status->cnts, node_entry_t);
229 opcode_clear_entry(elem);
233 return (node_entry_t*)pset_insert(hmap, elem, op->code);
237 * Returns the associates ir_op for an opcode
239 * @param code the IR opcode
240 * @param hmap the hash map containing opcode -> ir_op*
242 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap)
247 return (ir_op*)pset_find(hmap, &key, code);
251 * Clears all counter in a graph_entry_t.
253 * @param elem the graph entry
254 * @param all if non-zero, clears all counters, else leave accumulated ones
256 static void graph_clear_entry(graph_entry_t *elem, int all)
260 /* clear accumulated / non-accumulated counter */
261 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
262 cnt_clr(&elem->cnt[i]);
265 if (elem->block_hash) {
266 del_pset(elem->block_hash);
267 elem->block_hash = NULL;
270 obstack_free(&elem->recalc_cnts, NULL);
271 obstack_init(&elem->recalc_cnts);
275 * Returns the associated graph_entry_t for an IR graph.
277 * @param irg the IR graph, NULL for the global counter
278 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
280 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
288 elem = (graph_entry_t*)pset_find(hmap, &key, hash_ptr(irg));
291 /* create hash map backend block information */
292 if (! elem->be_block_hash)
293 elem->be_block_hash = new_pset(be_block_cmp, 5);
298 /* allocate a new one */
299 elem = OALLOCZ(&status->cnts, graph_entry_t);
300 obstack_init(&elem->recalc_cnts);
303 graph_clear_entry(elem, 1);
305 /* new hash table for opcodes here */
306 elem->opcode_hash = new_pset(opcode_cmp, 5);
307 elem->address_mark = new_set(address_mark_cmp, 5);
310 /* these hash tables are created on demand */
311 elem->block_hash = NULL;
313 for (i = 0; i != ARRAY_SIZE(elem->opt_hash); ++i)
314 elem->opt_hash[i] = new_pset(opt_cmp, 4);
316 return (graph_entry_t*)pset_insert(hmap, elem, hash_ptr(irg));
320 * Clear all counter in an opt_entry_t.
322 static void opt_clear_entry(opt_entry_t *elem)
324 cnt_clr(&elem->count);
328 * Returns the associated opt_entry_t for an IR operation.
330 * @param op the IR operation
331 * @param hmap the hash map containing ir_op* -> opt_entry_t*
333 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
340 elem = (opt_entry_t*)pset_find(hmap, &key, op->code);
344 elem = OALLOCZ(&status->cnts, opt_entry_t);
346 /* clear new counter */
347 opt_clear_entry(elem);
351 return (opt_entry_t*)pset_insert(hmap, elem, op->code);
355 * clears all counter in a block_entry_t
357 static void block_clear_entry(block_entry_t *elem)
361 for (i = 0; i < _bcnt_last; ++i)
362 cnt_clr(&elem->cnt[i]);
366 * Returns the associated block_entry_t for an block.
368 * @param block_nr an IR block number
369 * @param hmap a hash map containing long -> block_entry_t
371 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
376 key.block_nr = block_nr;
378 elem = (block_entry_t*)pset_find(hmap, &key, block_nr);
382 elem = OALLOCZ(obst, block_entry_t);
384 /* clear new counter */
385 block_clear_entry(elem);
387 elem->block_nr = block_nr;
389 return (block_entry_t*)pset_insert(hmap, elem, block_nr);
393 * Clear all sets in be_block_entry_t.
395 static void be_block_clear_entry(be_block_entry_t *elem)
397 if (elem->reg_pressure)
398 del_pset(elem->reg_pressure);
400 if (elem->sched_ready)
401 stat_delete_distrib_tbl(elem->sched_ready);
403 if (elem->perm_class_stat)
404 del_pset(elem->perm_class_stat);
406 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
407 elem->sched_ready = stat_new_int_distrib_tbl();
408 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
412 * Returns the associated be_block_entry_t for an block.
414 * @param block_nr an IR block number
415 * @param hmap a hash map containing long -> be_block_entry_t
417 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
419 be_block_entry_t key;
420 be_block_entry_t *elem;
422 key.block_nr = block_nr;
424 elem = (be_block_entry_t*)pset_find(hmap, &key, block_nr);
428 elem = OALLOCZ(obst, be_block_entry_t);
430 /* clear new counter */
431 be_block_clear_entry(elem);
433 elem->block_nr = block_nr;
435 return (be_block_entry_t*)pset_insert(hmap, elem, block_nr);
439 * clears all sets in perm_class_entry_t
441 static void perm_class_clear_entry(perm_class_entry_t *elem)
444 del_pset(elem->perm_stat);
446 elem->perm_stat = new_pset(perm_stat_cmp, 5);
450 * Returns the associated perm_class entry for a register class.
452 * @param class_name the register class name
453 * @param hmap a hash map containing class_name -> perm_class_entry_t
455 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
456 hmap_perm_class_entry_t *hmap)
458 perm_class_entry_t key;
459 perm_class_entry_t *elem;
461 key.class_name = class_name;
463 elem = (perm_class_entry_t*)pset_find(hmap, &key, hash_ptr(class_name));
467 elem = OALLOCZ(obst, perm_class_entry_t);
469 /* clear new counter */
470 perm_class_clear_entry(elem);
472 elem->class_name = class_name;
474 return (perm_class_entry_t*)pset_insert(hmap, elem, hash_ptr(class_name));
478 * clears all sets in perm_stat_entry_t
480 static void perm_stat_clear_entry(perm_stat_entry_t *elem)
483 stat_delete_distrib_tbl(elem->chains);
486 stat_delete_distrib_tbl(elem->cycles);
488 elem->chains = stat_new_int_distrib_tbl();
489 elem->cycles = stat_new_int_distrib_tbl();
493 * Returns the associated perm_stat entry for a perm.
495 * @param perm the perm node
496 * @param hmap a hash map containing perm -> perm_stat_entry_t
498 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
500 perm_stat_entry_t key;
501 perm_stat_entry_t *elem;
505 elem = (perm_stat_entry_t*)pset_find(hmap, &key, hash_ptr(perm));
509 elem = OALLOCZ(obst, perm_stat_entry_t);
511 /* clear new counter */
512 perm_stat_clear_entry(elem);
516 return (perm_stat_entry_t*)pset_insert(hmap, elem, hash_ptr(perm));
520 * Clear optimizations counter,
522 static void clear_optimization_counter(void)
525 for (i = 0; i < FS_OPT_MAX; ++i)
526 cnt_clr(&status->num_opts[i]);
530 * Returns the ir_op for an IR-node,
531 * handles special cases and return pseudo op codes.
533 * @param none an IR node
535 static ir_op *stat_get_irn_op(ir_node *node)
537 ir_op *op = get_irn_op(node);
538 unsigned opc = op->code;
542 if (get_irn_arity(node) == 0) {
543 /* special case, a Phi0 node, count on extra counter */
544 op = status->op_Phi0 ? status->op_Phi0 : op;
545 } else if (get_irn_mode(node) == mode_M) {
546 /* special case, a Memory Phi node, count on extra counter */
547 op = status->op_PhiM ? status->op_PhiM : op;
551 if (get_irn_mode(node) == mode_M) {
552 /* special case, a Memory Proj node, count on extra counter */
553 op = status->op_ProjM ? status->op_ProjM : op;
557 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
558 /* special case, a Multiply by a const, count on extra counter */
559 op = status->op_MulC ? status->op_MulC : op;
563 if (is_Const(get_Div_right(node))) {
564 /* special case, a division by a const, count on extra counter */
565 op = status->op_DivC ? status->op_DivC : op;
569 if (is_Const(get_Mod_right(node))) {
570 /* special case, a module by a const, count on extra counter */
571 op = status->op_ModC ? status->op_ModC : op;
575 if (is_Sel(get_Sel_ptr(node))) {
576 /* special case, a Sel of a Sel, count on extra counter */
577 op = status->op_SelSel ? status->op_SelSel : op;
578 if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
579 /* special case, a Sel of a Sel of a Sel, count on extra counter */
580 op = status->op_SelSelSel ? status->op_SelSelSel : op;
592 * update the block counter
594 static void undate_block_info(ir_node *node, graph_entry_t *graph)
597 block_entry_t *b_entry;
600 /* check for block */
601 if (is_Block(node)) {
602 arity = get_irn_arity(node);
603 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
604 /* mark start end block to allow to filter them out */
605 if (node == get_irg_start_block(graph->irg))
606 b_entry->is_start = 1;
607 else if (node == get_irg_end_block(graph->irg))
610 /* count all incoming edges */
611 for (i = 0; i < arity; ++i) {
612 ir_node *pred = get_irn_n(node, i);
613 ir_node *other_block = get_nodes_block(pred);
614 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
616 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
617 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
622 block = get_nodes_block(node);
623 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
625 if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
626 /* count data Phi per block */
627 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
630 /* we have a new node in our block */
631 cnt_inc(&b_entry->cnt[bcnt_nodes]);
633 /* don't count keep-alive edges */
637 arity = get_irn_arity(node);
639 for (i = 0; i < arity; ++i) {
640 ir_node *pred = get_irn_n(node, i);
641 ir_node *other_block;
643 other_block = get_nodes_block(pred);
645 if (other_block == block)
646 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
648 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
650 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
651 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
657 * Calculates how many arguments of the call are const, updates
658 * param distribution.
660 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call)
662 int i, num_const_args = 0, num_local_adr = 0;
663 int n = get_Call_n_params(call);
665 for (i = 0; i < n; ++i) {
666 ir_node *param = get_Call_param(call, i);
668 if (is_irn_constlike(param))
670 else if (is_Sel(param)) {
671 ir_node *base = param;
674 base = get_Sel_ptr(base);
675 } while (is_Sel(base));
677 if (base == get_irg_frame(current_ir_graph))
683 if (num_const_args > 0)
684 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
685 if (num_const_args == n)
686 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
687 if (num_local_adr > 0)
688 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
690 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
694 * Update info on calls.
696 * @param call The call
697 * @param graph The graph entry containing the call
699 static void stat_update_call(ir_node *call, graph_entry_t *graph)
701 ir_node *block = get_nodes_block(call);
702 ir_node *ptr = get_Call_ptr(call);
703 ir_entity *ent = NULL;
704 ir_graph *callee = NULL;
707 * If the block is bad, the whole subgraph will collapse later
708 * so do not count this call.
709 * This happens in dead code.
714 cnt_inc(&graph->cnt[gcnt_all_calls]);
716 /* found a call, this function is not a leaf */
719 if (is_SymConst(ptr)) {
720 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
721 /* ok, we seems to know the entity */
722 ent = get_SymConst_entity(ptr);
723 callee = get_entity_irg(ent);
725 /* it is recursive, if it calls at least once */
726 if (callee == graph->irg)
727 graph->is_recursive = 1;
729 cnt_inc(&graph->cnt[gcnt_external_calls]);
732 /* indirect call, be could not predict */
733 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
735 /* NOT a leaf call */
736 graph->is_leaf_call = LCS_NON_LEAF_CALL;
739 /* check, if it's a chain-call: Then, the call-block
740 * must dominate the end block. */
742 ir_node *curr = get_irg_end_block(graph->irg);
743 int depth = get_Block_dom_depth(block);
745 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
746 curr = get_Block_idom(curr);
748 if (! curr || !is_Block(curr))
753 graph->is_chain_call = 0;
756 /* check, if the callee is a leaf */
758 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
760 if (called->is_analyzed) {
761 if (! called->is_leaf)
762 graph->is_leaf_call = LCS_NON_LEAF_CALL;
766 analyse_params_of_Call(graph, call);
770 * Update info on calls for graphs on the wait queue.
772 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
774 ir_node *block = get_nodes_block(call);
775 ir_node *ptr = get_Call_ptr(call);
776 ir_entity *ent = NULL;
777 ir_graph *callee = NULL;
780 * If the block is bad, the whole subgraph will collapse later
781 * so do not count this call.
782 * This happens in dead code.
787 if (is_SymConst(ptr)) {
788 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
789 /* ok, we seems to know the entity */
790 ent = get_SymConst_entity(ptr);
791 callee = get_entity_irg(ent);
795 /* check, if the callee is a leaf */
797 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
799 assert(called->is_analyzed);
801 if (! called->is_leaf)
802 graph->is_leaf_call = LCS_NON_LEAF_CALL;
804 graph->is_leaf_call = LCS_NON_LEAF_CALL;
808 * Find the base address and entity of an Sel node.
810 * @param sel the node
812 * @return the base address.
814 static ir_node *find_base_adr(ir_node *sel)
816 ir_node *ptr = get_Sel_ptr(sel);
818 while (is_Sel(ptr)) {
820 ptr = get_Sel_ptr(sel);
826 * Update info on Load/Store address statistics.
828 static void stat_update_address(ir_node *node, graph_entry_t *graph)
830 unsigned opc = get_irn_opcode(node);
836 /* a global address */
837 cnt_inc(&graph->cnt[gcnt_global_adr]);
840 base = find_base_adr(node);
841 irg = current_ir_graph;
842 if (base == get_irg_frame(irg)) {
843 /* a local Variable. */
844 cnt_inc(&graph->cnt[gcnt_local_adr]);
847 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
848 /* pointer access through parameter, check for THIS */
849 ir_entity *ent = get_irg_entity(irg);
852 ir_type *ent_tp = get_entity_type(ent);
854 if (get_method_calling_convention(ent_tp) & cc_this_call) {
855 if (get_Proj_proj(base) == 0) {
857 cnt_inc(&graph->cnt[gcnt_this_adr]);
862 /* other parameter */
863 cnt_inc(&graph->cnt[gcnt_param_adr]);
866 /* unknown Pointer access */
867 cnt_inc(&graph->cnt[gcnt_other_adr]);
876 * Walker for reachable nodes count.
878 static void update_node_stat(ir_node *node, void *env)
880 graph_entry_t *graph = (graph_entry_t*)env;
883 ir_op *op = stat_get_irn_op(node);
884 int i, arity = get_irn_arity(node);
886 entry = opcode_get_entry(op, graph->opcode_hash);
888 cnt_inc(&entry->cnt_alive);
889 cnt_add_i(&graph->cnt[gcnt_edges], arity);
891 /* count block edges */
892 undate_block_info(node, graph);
894 /* handle statistics for special node types */
898 /* check for properties that depends on calls like recursion/leaf/indirect call */
899 stat_update_call(node, graph);
902 /* check address properties */
903 stat_update_address(get_Load_ptr(node), graph);
906 /* check address properties */
907 stat_update_address(get_Store_ptr(node), graph);
910 /* check for non-strict Phi nodes */
911 for (i = arity - 1; i >= 0; --i) {
912 ir_node *pred = get_Phi_pred(node, i);
913 if (is_Unknown(pred)) {
914 /* found an Unknown predecessor, graph is not strict */
915 graph->is_strict = 0;
923 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
924 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
927 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
928 ir_node *pred = get_irn_n(node, i);
930 if (is_Const(pred)) {
931 /* check properties of constants */
932 stat_update_const(status, pred, graph);
939 * Walker for reachable nodes count for graphs on the wait_q.
941 static void update_node_stat_2(ir_node *node, void *env)
943 graph_entry_t *graph = (graph_entry_t*)env;
945 /* check for properties that depends on calls like recursion/leaf/indirect call */
947 stat_update_call_2(node, graph);
951 * Get the current address mark.
953 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
955 address_mark_entry_t const val = { node, 0 };
956 address_mark_entry_t *value = set_find(address_mark_entry_t, graph->address_mark, &val, sizeof(val), hash_ptr(node));
958 return value ? value->mark : 0;
962 * Set the current address mark.
964 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
966 address_mark_entry_t const value = { node, val };
967 (void)set_insert(address_mark_entry_t, graph->address_mark, &value, sizeof(value), hash_ptr(node));
974 * a vcg attribute hook: Color a node with a different color if
975 * it's identified as a part of an address expression or at least referenced
976 * by an address expression.
978 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
980 ir_node *n = local ? local : node;
981 ir_graph *irg = get_irn_irg(n);
982 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
983 unsigned mark = get_adr_mark(graph, n);
985 if (mark & MARK_ADDRESS_CALC)
986 fprintf(F, "color: purple");
987 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
988 fprintf(F, "color: pink");
989 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
990 fprintf(F, "color: lightblue");
994 /* I know the color! */
997 #endif /* DUMP_ADR_MODE */
1000 * Return the "operational" mode of a Firm node.
1002 static ir_mode *get_irn_op_mode(ir_node *node)
1004 switch (get_irn_opcode(node)) {
1006 return get_Load_mode(node);
1008 return get_irn_mode(get_Store_value(node));
1010 return get_irn_mode(get_Div_left(node));
1012 return get_irn_mode(get_Mod_left(node));
1014 /* Cmp is no address calculation, or is it? */
1016 return get_irn_mode(node);
1021 * Post-walker that marks every node that is an address calculation.
1023 * Users of a node must be visited first. We ensure this by
1024 * calling it in the post of an outs walk. This should work even in cycles,
1025 * while the normal pre-walk will not.
1027 static void mark_address_calc(ir_node *node, void *env)
1029 graph_entry_t *graph = (graph_entry_t*)env;
1030 ir_mode *mode = get_irn_op_mode(node);
1032 unsigned mark_preds = MARK_REF_NON_ADR;
1034 if (! mode_is_data(mode))
1037 if (mode_is_reference(mode)) {
1038 /* a reference is calculated here, we are sure */
1039 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1041 mark_preds = MARK_REF_ADR;
1043 unsigned mark = get_adr_mark(graph, node);
1045 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1047 * this node has no reference mode, but is only
1048 * referenced by address calculations
1050 mark_preds = MARK_REF_ADR;
1054 /* mark all predecessors */
1055 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1056 ir_node *pred = get_irn_n(node, i);
1058 mode = get_irn_op_mode(pred);
1059 if (! mode_is_data(mode))
1062 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1067 * Post-walker that marks every node that is an address calculation.
1069 * Users of a node must be visited first. We ensure this by
1070 * calling it in the post of an outs walk. This should work even in cycles,
1071 * while the normal pre-walk will not.
1073 static void count_adr_ops(ir_node *node, void *env)
1075 graph_entry_t *graph = (graph_entry_t*)env;
1076 unsigned mark = get_adr_mark(graph, node);
1078 if (mark & MARK_ADDRESS_CALC)
1079 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1080 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1081 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1082 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1083 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1087 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1088 * is called, must recalculate all statistic info.
1090 * @param global The global entry
1091 * @param graph The current entry
1093 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1097 /* clear first the alive counter in the graph */
1098 foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1099 cnt_clr(&entry->cnt_alive);
1102 /* set pessimistic values */
1104 graph->is_leaf_call = LCS_UNKNOWN;
1105 graph->is_recursive = 0;
1106 graph->is_chain_call = 1;
1107 graph->is_strict = 1;
1109 /* create new block counter */
1110 graph->block_hash = new_pset(block_cmp, 5);
1112 /* we need dominator info */
1113 if (graph->irg != get_const_code_irg()) {
1114 assure_doms(graph->irg);
1117 /* count the nodes in the graph */
1118 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1120 /* recursive functions are never chain calls, leafs don't have calls */
1121 if (graph->is_recursive || graph->is_leaf)
1122 graph->is_chain_call = 0;
1124 /* assume we walk every graph only ONCE, we could sum here the global count */
1125 foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1126 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1128 /* update the node counter */
1129 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1132 /* count the number of address calculation */
1133 if (graph->irg != get_const_code_irg()) {
1134 ir_graph *rem = current_ir_graph;
1136 assure_irg_outs(graph->irg);
1138 /* Must be done an the outs graph */
1139 current_ir_graph = graph->irg;
1140 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1141 current_ir_graph = rem;
1143 #ifdef DUMP_ADR_MODE
1144 /* register the vcg hook and dump the graph for test */
1145 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1146 dump_ir_block_graph(graph->irg, "-adr");
1147 set_dump_node_vcgattr_hook(NULL);
1148 #endif /* DUMP_ADR_MODE */
1150 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1153 /* count the DAG's */
1154 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1155 count_dags_in_graph(global, graph);
1157 /* calculate the patterns of this graph */
1158 stat_calc_pattern_history(graph->irg);
1160 /* leaf function did not call others */
1162 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1163 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1164 /* we still don't know if this graph calls leaf-functions, so enqueue */
1165 pdeq_putl(status->wait_q, graph);
1168 /* we have analyzed this graph */
1169 graph->is_analyzed = 1;
1171 /* accumulate all counter's */
1172 for (i = 0; i < _gcnt_last; ++i)
1173 cnt_add(&global->cnt[i], &graph->cnt[i]);
1177 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1178 * must finish all statistic info calculations.
1180 * @param global The global entry
1181 * @param graph The current entry
1183 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1186 if (graph->is_deleted) {
1187 /* deleted, ignore */
1192 /* count the nodes in the graph */
1193 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1195 if (graph->is_leaf_call == LCS_UNKNOWN)
1196 graph->is_leaf_call = LCS_LEAF_CALL;
1201 * Register a dumper.
1203 static void stat_register_dumper(const dumper_t *dumper)
1205 dumper_t *p = XMALLOC(dumper_t);
1208 p->next = status->dumper;
1212 /* FIXME: memory leak */
1216 * Dumps the statistics of an IR graph.
1218 static void stat_dump_graph(graph_entry_t *entry)
1222 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1223 if (dumper->dump_graph)
1224 dumper->dump_graph(dumper, entry);
1229 * Calls all registered dumper functions.
1231 static void stat_dump_registered(graph_entry_t *entry)
1235 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1236 if (dumper->func_map) {
1237 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1238 func(dumper, entry);
1244 * Dumps a constant table.
1246 static void stat_dump_consts(const constant_info_t *tbl)
1250 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1251 if (dumper->dump_const_tbl)
1252 dumper->dump_const_tbl(dumper, tbl);
1257 * Dumps the parameter distribution
1259 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1263 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1264 if (dumper->dump_param_tbl)
1265 dumper->dump_param_tbl(dumper, tbl, global);
1270 * Dumps the optimization counter
1272 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1276 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1277 if (dumper->dump_opt_cnt)
1278 dumper->dump_opt_cnt(dumper, tbl, len);
1283 * Initialize the dumper.
1285 static void stat_dump_init(const char *name)
1289 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1291 dumper->init(dumper, name);
1296 * Finish the dumper.
1298 static void stat_dump_finish(void)
1302 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1304 dumper->finish(dumper);
1309 * Register an additional function for all dumper.
1311 void stat_register_dumper_func(dump_graph_FUNC *const func)
1315 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1316 if (! dumper->func_map)
1317 dumper->func_map = pset_new_ptr(3);
1318 pset_insert_ptr(dumper->func_map, (void*)func);
1322 /* ---------------------------------------------------------------------- */
1325 * Helper: get an ir_op from an opcode.
1327 ir_op *stat_get_op_from_opcode(unsigned code)
1329 return opcode_find_entry((ir_opcode)code, status->ir_op_hash);
1333 * Hook: A new IR op is registered.
1335 * @param ctx the hook context
1336 * @param op the new IR opcode that was created.
1338 static void stat_new_ir_op(void *ctx, ir_op *op)
1341 if (! status->stat_options)
1346 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1348 /* execute for side effect :-) */
1349 (void)opcode_get_entry(op, graph->opcode_hash);
1351 pset_insert(status->ir_op_hash, op, op->code);
1357 * Hook: An IR op is freed.
1359 * @param ctx the hook context
1360 * @param op the IR opcode that is freed
1362 static void stat_free_ir_op(void *ctx, ir_op *op)
1366 if (! status->stat_options)
1376 * Hook: A new node is created.
1378 * @param ctx the hook context
1379 * @param irg the IR graph on which the node is created
1380 * @param node the new IR node that was created
1382 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1386 if (! status->stat_options)
1389 /* do NOT count during dead node elimination */
1390 if (status->in_dead_node_elim)
1395 node_entry_t *entry;
1396 graph_entry_t *graph;
1397 ir_op *op = stat_get_irn_op(node);
1399 /* increase global value */
1400 graph = graph_get_entry(NULL, status->irg_hash);
1401 entry = opcode_get_entry(op, graph->opcode_hash);
1402 cnt_inc(&entry->new_node);
1404 /* increase local value */
1405 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1406 entry = opcode_get_entry(op, graph->opcode_hash);
1407 cnt_inc(&entry->new_node);
1413 * Hook: A node is changed into a Id node
1415 * @param ctx the hook context
1416 * @param node the IR node that will be turned into an ID
1418 static void stat_turn_into_id(void *ctx, ir_node *node)
1421 if (! status->stat_options)
1426 node_entry_t *entry;
1427 graph_entry_t *graph;
1428 ir_op *op = stat_get_irn_op(node);
1430 /* increase global value */
1431 graph = graph_get_entry(NULL, status->irg_hash);
1432 entry = opcode_get_entry(op, graph->opcode_hash);
1433 cnt_inc(&entry->into_Id);
1435 /* increase local value */
1436 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1437 entry = opcode_get_entry(op, graph->opcode_hash);
1438 cnt_inc(&entry->into_Id);
1444 * Hook: A node is normalized
1446 * @param ctx the hook context
1447 * @param node the IR node that was normalized
1449 static void stat_normalize(void *ctx, ir_node *node)
1452 if (! status->stat_options)
1457 node_entry_t *entry;
1458 graph_entry_t *graph;
1459 ir_op *op = stat_get_irn_op(node);
1461 /* increase global value */
1462 graph = graph_get_entry(NULL, status->irg_hash);
1463 entry = opcode_get_entry(op, graph->opcode_hash);
1464 cnt_inc(&entry->normalized);
1466 /* increase local value */
1467 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1468 entry = opcode_get_entry(op, graph->opcode_hash);
1469 cnt_inc(&entry->normalized);
1475 * Hook: A new graph was created
1477 * @param ctx the hook context
1478 * @param irg the new IR graph that was created
1479 * @param ent the entity of this graph
1481 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1484 if (! status->stat_options)
1489 /* execute for side effect :-) */
1490 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1493 graph->is_deleted = 0;
1495 graph->is_leaf_call = 0;
1496 graph->is_recursive = 0;
1497 graph->is_chain_call = 0;
1498 graph->is_strict = 1;
1499 graph->is_analyzed = 0;
1505 * Hook: A graph will be deleted
1507 * @param ctx the hook context
1508 * @param irg the IR graph that will be deleted
1510 * Note that we still hold the information for this graph
1511 * in our hash maps, only a flag is set which prevents this
1512 * information from being changed, it's "frozen" from now.
1514 static void stat_free_graph(void *ctx, ir_graph *irg)
1517 if (! status->stat_options)
1522 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1523 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1525 graph->is_deleted = 1;
1527 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1528 /* count the nodes of the graph yet, it will be destroyed later */
1529 update_graph_stat(global, graph);
1536 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1538 * @param ctx the hook context
1539 * @param irg the IR graph that will be walked
1540 * @param pre the pre walker
1541 * @param post the post walker
1543 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1548 if (! status->stat_options)
1553 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1555 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1561 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1563 * @param ctx the hook context
1564 * @param irg the IR graph that will be walked
1565 * @param pre the pre walker
1566 * @param post the post walker
1568 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1570 /* for now, do NOT differentiate between blockwise and normal */
1571 stat_irg_walk(ctx, irg, pre, post);
1575 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1577 * @param ctx the hook context
1578 * @param irg the IR graph that will be walked
1579 * @param node the IR node
1580 * @param pre the pre walker
1581 * @param post the post walker
1583 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1589 if (! status->stat_options)
1594 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1596 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1602 * Called for every node that is removed due to an optimization.
1604 * @param n the IR node that will be removed
1605 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1606 * @param kind the optimization kind
1608 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1611 ir_op *op = stat_get_irn_op(n);
1613 /* ignore CSE for Constants */
1614 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1617 /* increase global value */
1618 entry = opt_get_entry(op, hmap);
1619 cnt_inc(&entry->count);
1623 * Hook: Some nodes were optimized into some others due to an optimization.
1625 * @param ctx the hook context
1627 static void stat_merge_nodes(
1629 ir_node **new_node_array, int new_num_entries,
1630 ir_node **old_node_array, int old_num_entries,
1634 if (! status->stat_options)
1640 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1642 cnt_inc(&status->num_opts[opt]);
1643 if (status->reassoc_run)
1644 opt = HOOK_OPT_REASSOC;
1646 for (i = 0; i < old_num_entries; ++i) {
1647 /* nodes might be in new and old, so if we found a node
1648 in both sets, this one is NOT removed */
1649 for (j = 0; j < new_num_entries; ++j) {
1650 if (old_node_array[i] == new_node_array[j])
1653 if (j >= new_num_entries) {
1656 /* sometimes we did not detect, that it is replaced by a Const */
1657 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1658 ir_node *const irn = new_node_array[0];
1659 if (is_Const(irn) || is_SymConst(irn))
1660 xopt = HOOK_OPT_CONFIRM_C;
1663 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1671 * Hook: Reassociation is started/stopped.
1673 * @param ctx the hook context
1674 * @param flag if non-zero, reassociation is started else stopped
1676 static void stat_reassociate(void *ctx, int flag)
1679 if (! status->stat_options)
1684 status->reassoc_run = flag;
1690 * Hook: A node was lowered into other nodes
1692 * @param ctx the hook context
1693 * @param node the IR node that will be lowered
1695 static void stat_lower(void *ctx, ir_node *node)
1698 if (! status->stat_options)
1703 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1705 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1711 * Hook: A graph was inlined.
1713 * @param ctx the hook context
1714 * @param call the IR call that will re changed into the body of
1715 * the called IR graph
1716 * @param called_irg the IR graph representing the called routine
1718 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1721 if (! status->stat_options)
1726 ir_graph *irg = get_irn_irg(call);
1727 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1728 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1730 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1731 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1737 * Hook: A graph with tail-recursions was optimized.
1739 * @param ctx the hook context
1741 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1744 if (! status->stat_options)
1749 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1751 graph->num_tail_recursion += n_calls;
1757 * Strength reduction was performed on an iteration variable.
1759 * @param ctx the hook context
1761 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1764 if (! status->stat_options)
1769 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1770 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1772 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1778 * Hook: Start/Stop the dead node elimination.
1780 * @param ctx the hook context
1782 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1786 if (! status->stat_options)
1789 status->in_dead_node_elim = (start != 0);
1793 * Hook: if-conversion was tried.
1795 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1796 int pos, ir_node *mux, if_result_t reason)
1802 if (! status->stat_options)
1807 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1809 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1815 * Hook: real function call was optimized.
1817 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1821 if (! status->stat_options)
1826 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1828 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1834 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1836 * @param ctx the hook context
1838 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1841 if (! status->stat_options)
1846 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1847 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1853 * Hook: A division by const was replaced.
1855 * @param ctx the hook context
1856 * @param node the division node that will be optimized
1858 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1861 if (! status->stat_options)
1866 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1867 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1873 * Update the register pressure of a block.
1875 * @param irg the irg containing the block
1876 * @param block the block for which the reg pressure should be set
1877 * @param pressure the pressure
1878 * @param class_name the name of the register class
1880 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1882 if (! status->stat_options)
1887 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1888 be_block_entry_t *block_ent;
1889 reg_pressure_entry_t *rp_ent;
1891 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1892 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1894 rp_ent->class_name = class_name;
1895 rp_ent->pressure = pressure;
1897 pset_insert(block_ent->reg_pressure, rp_ent, hash_ptr(class_name));
1903 * Update the distribution of ready nodes of a block
1905 * @param irg the irg containing the block
1906 * @param block the block for which the reg pressure should be set
1907 * @param num_ready the number of ready nodes
1909 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1911 if (! status->stat_options)
1916 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1917 be_block_entry_t *block_ent;
1919 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1921 /* increase the counter of corresponding number of ready nodes */
1922 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1928 * Update the permutation statistic of a block.
1930 * @param class_name the name of the register class
1931 * @param n_regs number of registers in the register class
1932 * @param perm the perm node
1933 * @param block the block containing the perm
1934 * @param size the size of the perm
1935 * @param real_size number of pairs with different registers
1937 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1938 int size, int real_size)
1940 if (! status->stat_options)
1945 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1946 be_block_entry_t *block_ent;
1947 perm_class_entry_t *pc_ent;
1948 perm_stat_entry_t *ps_ent;
1950 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1951 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1952 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1954 pc_ent->n_regs = n_regs;
1956 /* update information */
1957 ps_ent->size = size;
1958 ps_ent->real_size = real_size;
1964 * Update the permutation statistic of a single perm.
1966 * @param class_name the name of the register class
1967 * @param perm the perm node
1968 * @param block the block containing the perm
1969 * @param is_chain 1 if chain, 0 if cycle
1970 * @param size length of the cycle/chain
1971 * @param n_ops the number of ops representing this cycle/chain after lowering
1973 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1974 int is_chain, int size, int n_ops)
1976 if (! status->stat_options)
1981 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1982 be_block_entry_t *block_ent;
1983 perm_class_entry_t *pc_ent;
1984 perm_stat_entry_t *ps_ent;
1986 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1987 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1988 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1991 ps_ent->n_copies += n_ops;
1992 stat_inc_int_distrib_tbl(ps_ent->chains, size);
1994 ps_ent->n_exchg += n_ops;
1995 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2001 /* Dumps a statistics snapshot. */
2002 void stat_dump_snapshot(const char *name, const char *phase)
2008 if (! status->stat_options)
2013 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2016 * The constant counter is only global, so we clear it here.
2017 * Note that it does NOT contain the constants in DELETED
2018 * graphs due to this.
2020 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2021 stat_const_clear(status);
2023 /* build the name */
2024 p = strrchr(name, '/');
2029 q = strrchr(name, '\\');
2031 /* NULL might be not the smallest pointer */
2032 if (q && (!p || q > p))
2040 if (l > (int) (sizeof(fname) - 1))
2041 l = sizeof(fname) - 1;
2043 memcpy(fname, name, l);
2049 strncat(fname, "firmstat-", sizeof(fname)-strlen(fname)-1);
2050 strncat(fname, phase, sizeof(fname)-strlen(fname)-1);
2051 strncat(fname, "-", sizeof(fname)-strlen(fname)-1);
2052 strncat(fname, p, sizeof(fname)-strlen(fname)-1);
2054 stat_dump_init(fname);
2056 /* calculate the graph statistics */
2057 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2058 if (entry->irg == NULL) {
2059 /* special entry for the global count */
2062 if (! entry->is_deleted) {
2063 /* the graph is still alive, count the nodes on it */
2064 update_graph_stat(global, entry);
2068 /* some calculations are dependent, we pushed them on the wait_q */
2069 while (! pdeq_empty(status->wait_q)) {
2070 graph_entry_t *const entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2072 update_graph_stat_2(global, entry);
2075 /* dump per graph */
2076 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2077 if (entry->irg == NULL) {
2078 /* special entry for the global count */
2082 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2083 stat_dump_graph(entry);
2084 stat_dump_registered(entry);
2087 if (! entry->is_deleted) {
2088 /* clear the counter that are not accumulated */
2089 graph_clear_entry(entry, 0);
2094 stat_dump_graph(global);
2096 /* dump the const info */
2097 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2098 stat_dump_consts(&status->const_info);
2100 /* dump the parameter distribution */
2101 stat_dump_param_tbl(status->dist_param_cnt, global);
2103 /* dump the optimization counter and clear them */
2104 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2105 clear_optimization_counter();
2109 stat_finish_pattern_history(fname);
2111 /* clear the global counters here */
2112 foreach_pset(global->opcode_hash, node_entry_t, entry) {
2113 opcode_clear_entry(entry);
2115 /* clear all global counter */
2116 graph_clear_entry(global, /*all=*/1);
2121 typedef struct pass_t {
2122 ir_prog_pass_t pass;
2128 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2130 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2132 pass_t *pass = (pass_t*)context;
2135 stat_dump_snapshot(pass->fname, pass->phase);
2140 * Ensure that no verifier is run from the wrapper.
2142 static int no_verify(ir_prog *prog, void *ctx)
2150 * Ensure that no dumper is run from the wrapper.
2152 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2159 /* create an ir_pog pass */
2160 ir_prog_pass_t *stat_dump_snapshot_pass(
2161 const char *name, const char *fname, const char *phase)
2163 pass_t *pass = XMALLOCZ(pass_t);
2165 def_prog_pass_constructor(
2166 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2167 pass->fname = fname;
2168 pass->phase = phase;
2170 /* no dump/verify */
2171 pass->pass.dump_irprog = no_dump;
2172 pass->pass.verify_irprog = no_verify;
2177 /** the hook entries for the Firm statistics module */
2178 static hook_entry_t stat_hooks[hook_last];
2180 /* initialize the statistics module. */
2181 void firm_init_stat(void)
2183 #define X(a) a, sizeof(a)-1
2184 #define HOOK(h, fkt) \
2185 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2188 if (! (stat_options & FIRMSTAT_ENABLED))
2191 status = XMALLOCZ(stat_info_t);
2193 /* enable statistics */
2194 status->stat_options = stat_options & FIRMSTAT_ENABLED ? stat_options : 0;
2196 /* register all hooks */
2197 HOOK(hook_new_ir_op, stat_new_ir_op);
2198 HOOK(hook_free_ir_op, stat_free_ir_op);
2199 HOOK(hook_new_node, stat_new_node);
2200 HOOK(hook_turn_into_id, stat_turn_into_id);
2201 HOOK(hook_normalize, stat_normalize);
2202 HOOK(hook_new_graph, stat_new_graph);
2203 HOOK(hook_free_graph, stat_free_graph);
2204 HOOK(hook_irg_walk, stat_irg_walk);
2205 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2206 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2207 HOOK(hook_merge_nodes, stat_merge_nodes);
2208 HOOK(hook_reassociate, stat_reassociate);
2209 HOOK(hook_lower, stat_lower);
2210 HOOK(hook_inline, stat_inline);
2211 HOOK(hook_tail_rec, stat_tail_rec);
2212 HOOK(hook_strength_red, stat_strength_red);
2213 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2214 HOOK(hook_if_conversion, stat_if_conversion);
2215 HOOK(hook_func_call, stat_func_call);
2216 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2217 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2219 obstack_init(&status->cnts);
2220 obstack_init(&status->be_data);
2222 /* create the hash-tables */
2223 status->irg_hash = new_pset(graph_cmp, 8);
2224 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2226 /* create the wait queue */
2227 status->wait_q = new_pdeq();
2229 if (stat_options & FIRMSTAT_COUNT_STRONG_OP) {
2230 /* build the pseudo-ops */
2232 _op_Phi0.code = --num;
2233 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2235 _op_PhiM.code = --num;
2236 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2238 _op_ProjM.code = --num;
2239 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2241 _op_MulC.code = --num;
2242 _op_MulC.name = new_id_from_chars(X("MulC"));
2244 _op_DivC.code = --num;
2245 _op_DivC.name = new_id_from_chars(X("DivC"));
2247 _op_ModC.code = --num;
2248 _op_ModC.name = new_id_from_chars(X("ModC"));
2250 status->op_Phi0 = &_op_Phi0;
2251 status->op_PhiM = &_op_PhiM;
2252 status->op_ProjM = &_op_ProjM;
2253 status->op_MulC = &_op_MulC;
2254 status->op_DivC = &_op_DivC;
2255 status->op_ModC = &_op_ModC;
2257 status->op_Phi0 = NULL;
2258 status->op_PhiM = NULL;
2259 status->op_ProjM = NULL;
2260 status->op_MulC = NULL;
2261 status->op_DivC = NULL;
2262 status->op_ModC = NULL;
2265 /* for Florian: count the Sel depth */
2266 if (stat_options & FIRMSTAT_COUNT_SELS) {
2267 _op_SelSel.code = --num;
2268 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2270 _op_SelSelSel.code = --num;
2271 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2273 status->op_SelSel = &_op_SelSel;
2274 status->op_SelSelSel = &_op_SelSelSel;
2276 status->op_SelSel = NULL;
2277 status->op_SelSelSel = NULL;
2280 /* register the dumper */
2281 stat_register_dumper(&simple_dumper);
2283 if (stat_options & FIRMSTAT_CSV_OUTPUT)
2284 stat_register_dumper(&csv_dumper);
2286 /* initialize the pattern hash */
2287 stat_init_pattern_history(stat_options & FIRMSTAT_PATTERN_ENABLED);
2289 /* initialize the Const options */
2290 if (stat_options & FIRMSTAT_COUNT_CONSTS)
2291 stat_init_const_cnt(status);
2293 /* distribution table for parameter counts */
2294 status->dist_param_cnt = stat_new_int_distrib_tbl();
2296 clear_optimization_counter();
2303 * Frees all dumper structures.
2305 static void stat_term_dumper(void)
2307 dumper_t *dumper, *next_dumper;
2309 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2310 if (dumper->func_map)
2311 del_pset(dumper->func_map);
2313 next_dumper = dumper->next;
2315 dumper = next_dumper;
2320 /* Terminates the statistics module, frees all memory. */
2321 void stat_term(void)
2323 if (status != (stat_info_t *)&status_disable) {
2324 obstack_free(&status->be_data, NULL);
2325 obstack_free(&status->cnts, NULL);
2330 status = (stat_info_t *)&status_disable;
2334 /* returns 1 if statistics were initialized, 0 otherwise */
2335 int stat_is_active(void)
2337 return status != (stat_info_t *)&status_disable;
2340 void init_stat(void)
2342 lc_opt_entry_t *root_grp = firm_opt_get_root();
2343 lc_opt_entry_t *be_grp = lc_opt_get_grp(root_grp, "be");
2345 static const lc_opt_enum_mask_items_t stat_items[] = {
2346 { "enabled", FIRMSTAT_ENABLED },
2347 { "pattern", FIRMSTAT_PATTERN_ENABLED },
2348 { "count_strong_op", FIRMSTAT_COUNT_STRONG_OP },
2349 { "count_dag", FIRMSTAT_COUNT_DAG },
2350 { "count_deleted", FIRMSTAT_COUNT_DELETED },
2351 { "count_sels", FIRMSTAT_COUNT_SELS },
2352 { "count_consts", FIRMSTAT_COUNT_CONSTS },
2353 { "csv_output", FIRMSTAT_CSV_OUTPUT },
2356 static lc_opt_enum_mask_var_t statmask = { &stat_options, stat_items };
2357 static const lc_opt_table_entry_t stat_optionstable[] = {
2358 LC_OPT_ENT_ENUM_MASK("statistics", "enable statistics", &statmask),
2361 lc_opt_add_table(be_grp, stat_optionstable);