2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Statistics for Firm.
23 * @author Michael Beck
34 #include "firmstat_t.h"
46 #include "lc_opts_enum.h"
49 * need this to be static:
50 * Special pseudo Opcodes that we need to count some interesting cases
54 * The Phi0, a node that is created during SSA construction
56 static ir_op _op_Phi0;
58 /** The PhiM, just to count memory Phi's. */
59 static ir_op _op_PhiM;
61 /** The Mul by Const node. */
62 static ir_op _op_MulC;
64 /** The Div by Const node. */
65 static ir_op _op_DivC;
67 /** The Div by Const node. */
68 static ir_op _op_ModC;
70 /** The memory Proj node. */
71 static ir_op _op_ProjM;
74 static ir_op _op_SelSel;
76 /** A Sel of a Sel of a Sel */
77 static ir_op _op_SelSelSel;
79 static unsigned stat_options;
81 /* ---------------------------------------------------------------------------------- */
83 /** Marks the begin of a statistic (hook) function. */
84 #define STAT_ENTER ++status->recursive
86 /** Marks the end of a statistic (hook) functions. */
87 #define STAT_LEAVE --status->recursive
89 /** Allows to enter a statistic function only when we are not already in a hook. */
90 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
95 static const unsigned status_disable = 0;
96 static stat_info_t *status = (stat_info_t *)&status_disable;
99 * Compare two elements of the opcode hash.
101 static int opcode_cmp(const void *elt, const void *key)
103 const node_entry_t *e1 = (const node_entry_t*)elt;
104 const node_entry_t *e2 = (const node_entry_t*)key;
106 return e1->op->code - e2->op->code;
110 * Compare two elements of the graph hash.
112 static int graph_cmp(const void *elt, const void *key)
114 const graph_entry_t *e1 = (const graph_entry_t*)elt;
115 const graph_entry_t *e2 = (const graph_entry_t*)key;
117 return e1->irg != e2->irg;
121 * Compare two elements of the optimization hash.
123 static int opt_cmp(const void *elt, const void *key)
125 const opt_entry_t *e1 = (const opt_entry_t*)elt;
126 const opt_entry_t *e2 = (const opt_entry_t*)key;
128 return e1->op->code != e2->op->code;
132 * Compare two elements of the block hash.
134 static int block_cmp(const void *elt, const void *key)
136 const block_entry_t *e1 = (const block_entry_t*)elt;
137 const block_entry_t *e2 = (const block_entry_t*)key;
139 /* it's enough to compare the block number */
140 return e1->block_nr != e2->block_nr;
144 * Compare two elements of the be_block hash.
146 static int be_block_cmp(const void *elt, const void *key)
148 const be_block_entry_t *e1 = (const be_block_entry_t*)elt;
149 const be_block_entry_t *e2 = (const be_block_entry_t*)key;
151 return e1->block_nr != e2->block_nr;
155 * Compare two elements of reg pressure hash.
157 static int reg_pressure_cmp(const void *elt, const void *key)
159 const reg_pressure_entry_t *e1 = (const reg_pressure_entry_t*)elt;
160 const reg_pressure_entry_t *e2 = (const reg_pressure_entry_t*)key;
162 return e1->class_name != e2->class_name;
166 * Compare two elements of the perm_stat hash.
168 static int perm_stat_cmp(const void *elt, const void *key)
170 const perm_stat_entry_t *e1 = (const perm_stat_entry_t*)elt;
171 const perm_stat_entry_t *e2 = (const perm_stat_entry_t*)key;
173 return e1->perm != e2->perm;
177 * Compare two elements of the perm_class hash.
179 static int perm_class_cmp(const void *elt, const void *key)
181 const perm_class_entry_t *e1 = (const perm_class_entry_t*)elt;
182 const perm_class_entry_t *e2 = (const perm_class_entry_t*)key;
184 return e1->class_name != e2->class_name;
188 * Compare two elements of the ir_op hash.
190 static int opcode_cmp_2(const void *elt, const void *key)
192 const ir_op *e1 = (const ir_op*)elt;
193 const ir_op *e2 = (const ir_op*)key;
195 return e1->code != e2->code;
199 * Compare two elements of the address_mark set.
201 static int address_mark_cmp(const void *elt, const void *key, size_t size)
203 const address_mark_entry_t *e1 = (const address_mark_entry_t*)elt;
204 const address_mark_entry_t *e2 = (const address_mark_entry_t*)key;
207 /* compare only the nodes, the rest is used as data container */
208 return e1->node != e2->node;
212 * Clear all counter in a node_entry_t.
214 static void opcode_clear_entry(node_entry_t *elem)
216 cnt_clr(&elem->cnt_alive);
217 cnt_clr(&elem->new_node);
218 cnt_clr(&elem->into_Id);
219 cnt_clr(&elem->normalized);
223 * Returns the associates node_entry_t for an ir_op (and allocates
224 * one if not yet available).
226 * @param op the IR operation
227 * @param hmap a hash map containing ir_op* -> node_entry_t*
229 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
236 elem = (node_entry_t*)pset_find(hmap, &key, op->code);
240 elem = OALLOCZ(&status->cnts, node_entry_t);
243 opcode_clear_entry(elem);
247 return (node_entry_t*)pset_insert(hmap, elem, op->code);
251 * Returns the associates ir_op for an opcode
253 * @param code the IR opcode
254 * @param hmap the hash map containing opcode -> ir_op*
256 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap)
261 return (ir_op*)pset_find(hmap, &key, code);
265 * Clears all counter in a graph_entry_t.
267 * @param elem the graph entry
268 * @param all if non-zero, clears all counters, else leave accumulated ones
270 static void graph_clear_entry(graph_entry_t *elem, int all)
274 /* clear accumulated / non-accumulated counter */
275 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
276 cnt_clr(&elem->cnt[i]);
279 if (elem->block_hash) {
280 del_pset(elem->block_hash);
281 elem->block_hash = NULL;
284 obstack_free(&elem->recalc_cnts, NULL);
285 obstack_init(&elem->recalc_cnts);
289 * Returns the associated graph_entry_t for an IR graph.
291 * @param irg the IR graph, NULL for the global counter
292 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
294 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
302 elem = (graph_entry_t*)pset_find(hmap, &key, hash_ptr(irg));
305 /* create hash map backend block information */
306 if (! elem->be_block_hash)
307 elem->be_block_hash = new_pset(be_block_cmp, 5);
312 /* allocate a new one */
313 elem = OALLOCZ(&status->cnts, graph_entry_t);
314 obstack_init(&elem->recalc_cnts);
317 graph_clear_entry(elem, 1);
319 /* new hash table for opcodes here */
320 elem->opcode_hash = new_pset(opcode_cmp, 5);
321 elem->address_mark = new_set(address_mark_cmp, 5);
324 /* these hash tables are created on demand */
325 elem->block_hash = NULL;
327 for (i = 0; i != ARRAY_SIZE(elem->opt_hash); ++i)
328 elem->opt_hash[i] = new_pset(opt_cmp, 4);
330 return (graph_entry_t*)pset_insert(hmap, elem, hash_ptr(irg));
334 * Clear all counter in an opt_entry_t.
336 static void opt_clear_entry(opt_entry_t *elem)
338 cnt_clr(&elem->count);
342 * Returns the associated opt_entry_t for an IR operation.
344 * @param op the IR operation
345 * @param hmap the hash map containing ir_op* -> opt_entry_t*
347 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
354 elem = (opt_entry_t*)pset_find(hmap, &key, op->code);
358 elem = OALLOCZ(&status->cnts, opt_entry_t);
360 /* clear new counter */
361 opt_clear_entry(elem);
365 return (opt_entry_t*)pset_insert(hmap, elem, op->code);
369 * clears all counter in a block_entry_t
371 static void block_clear_entry(block_entry_t *elem)
375 for (i = 0; i < _bcnt_last; ++i)
376 cnt_clr(&elem->cnt[i]);
380 * Returns the associated block_entry_t for an block.
382 * @param block_nr an IR block number
383 * @param hmap a hash map containing long -> block_entry_t
385 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
390 key.block_nr = block_nr;
392 elem = (block_entry_t*)pset_find(hmap, &key, block_nr);
396 elem = OALLOCZ(obst, block_entry_t);
398 /* clear new counter */
399 block_clear_entry(elem);
401 elem->block_nr = block_nr;
403 return (block_entry_t*)pset_insert(hmap, elem, block_nr);
407 * Clear all sets in be_block_entry_t.
409 static void be_block_clear_entry(be_block_entry_t *elem)
411 if (elem->reg_pressure)
412 del_pset(elem->reg_pressure);
414 if (elem->sched_ready)
415 stat_delete_distrib_tbl(elem->sched_ready);
417 if (elem->perm_class_stat)
418 del_pset(elem->perm_class_stat);
420 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
421 elem->sched_ready = stat_new_int_distrib_tbl();
422 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
426 * Returns the associated be_block_entry_t for an block.
428 * @param block_nr an IR block number
429 * @param hmap a hash map containing long -> be_block_entry_t
431 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
433 be_block_entry_t key;
434 be_block_entry_t *elem;
436 key.block_nr = block_nr;
438 elem = (be_block_entry_t*)pset_find(hmap, &key, block_nr);
442 elem = OALLOCZ(obst, be_block_entry_t);
444 /* clear new counter */
445 be_block_clear_entry(elem);
447 elem->block_nr = block_nr;
449 return (be_block_entry_t*)pset_insert(hmap, elem, block_nr);
453 * clears all sets in perm_class_entry_t
455 static void perm_class_clear_entry(perm_class_entry_t *elem)
458 del_pset(elem->perm_stat);
460 elem->perm_stat = new_pset(perm_stat_cmp, 5);
464 * Returns the associated perm_class entry for a register class.
466 * @param class_name the register class name
467 * @param hmap a hash map containing class_name -> perm_class_entry_t
469 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
470 hmap_perm_class_entry_t *hmap)
472 perm_class_entry_t key;
473 perm_class_entry_t *elem;
475 key.class_name = class_name;
477 elem = (perm_class_entry_t*)pset_find(hmap, &key, hash_ptr(class_name));
481 elem = OALLOCZ(obst, perm_class_entry_t);
483 /* clear new counter */
484 perm_class_clear_entry(elem);
486 elem->class_name = class_name;
488 return (perm_class_entry_t*)pset_insert(hmap, elem, hash_ptr(class_name));
492 * clears all sets in perm_stat_entry_t
494 static void perm_stat_clear_entry(perm_stat_entry_t *elem)
497 stat_delete_distrib_tbl(elem->chains);
500 stat_delete_distrib_tbl(elem->cycles);
502 elem->chains = stat_new_int_distrib_tbl();
503 elem->cycles = stat_new_int_distrib_tbl();
507 * Returns the associated perm_stat entry for a perm.
509 * @param perm the perm node
510 * @param hmap a hash map containing perm -> perm_stat_entry_t
512 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
514 perm_stat_entry_t key;
515 perm_stat_entry_t *elem;
519 elem = (perm_stat_entry_t*)pset_find(hmap, &key, hash_ptr(perm));
523 elem = OALLOCZ(obst, perm_stat_entry_t);
525 /* clear new counter */
526 perm_stat_clear_entry(elem);
530 return (perm_stat_entry_t*)pset_insert(hmap, elem, hash_ptr(perm));
534 * Clear optimizations counter,
536 static void clear_optimization_counter(void)
539 for (i = 0; i < FS_OPT_MAX; ++i)
540 cnt_clr(&status->num_opts[i]);
544 * Returns the ir_op for an IR-node,
545 * handles special cases and return pseudo op codes.
547 * @param none an IR node
549 static ir_op *stat_get_irn_op(ir_node *node)
551 ir_op *op = get_irn_op(node);
552 unsigned opc = op->code;
556 if (get_irn_arity(node) == 0) {
557 /* special case, a Phi0 node, count on extra counter */
558 op = status->op_Phi0 ? status->op_Phi0 : op;
559 } else if (get_irn_mode(node) == mode_M) {
560 /* special case, a Memory Phi node, count on extra counter */
561 op = status->op_PhiM ? status->op_PhiM : op;
565 if (get_irn_mode(node) == mode_M) {
566 /* special case, a Memory Proj node, count on extra counter */
567 op = status->op_ProjM ? status->op_ProjM : op;
571 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
572 /* special case, a Multiply by a const, count on extra counter */
573 op = status->op_MulC ? status->op_MulC : op;
577 if (is_Const(get_Div_right(node))) {
578 /* special case, a division by a const, count on extra counter */
579 op = status->op_DivC ? status->op_DivC : op;
583 if (is_Const(get_Mod_right(node))) {
584 /* special case, a module by a const, count on extra counter */
585 op = status->op_ModC ? status->op_ModC : op;
589 if (is_Sel(get_Sel_ptr(node))) {
590 /* special case, a Sel of a Sel, count on extra counter */
591 op = status->op_SelSel ? status->op_SelSel : op;
592 if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
593 /* special case, a Sel of a Sel of a Sel, count on extra counter */
594 op = status->op_SelSelSel ? status->op_SelSelSel : op;
606 * update the block counter
608 static void undate_block_info(ir_node *node, graph_entry_t *graph)
611 block_entry_t *b_entry;
614 /* check for block */
615 if (is_Block(node)) {
616 arity = get_irn_arity(node);
617 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
618 /* mark start end block to allow to filter them out */
619 if (node == get_irg_start_block(graph->irg))
620 b_entry->is_start = 1;
621 else if (node == get_irg_end_block(graph->irg))
624 /* count all incoming edges */
625 for (i = 0; i < arity; ++i) {
626 ir_node *pred = get_irn_n(node, i);
627 ir_node *other_block = get_nodes_block(pred);
628 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
630 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
631 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
636 block = get_nodes_block(node);
637 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
639 if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
640 /* count data Phi per block */
641 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
644 /* we have a new node in our block */
645 cnt_inc(&b_entry->cnt[bcnt_nodes]);
647 /* don't count keep-alive edges */
651 arity = get_irn_arity(node);
653 for (i = 0; i < arity; ++i) {
654 ir_node *pred = get_irn_n(node, i);
655 ir_node *other_block;
657 other_block = get_nodes_block(pred);
659 if (other_block == block)
660 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
662 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
664 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
665 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
671 * Calculates how many arguments of the call are const, updates
672 * param distribution.
674 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call)
676 int i, num_const_args = 0, num_local_adr = 0;
677 int n = get_Call_n_params(call);
679 for (i = 0; i < n; ++i) {
680 ir_node *param = get_Call_param(call, i);
682 if (is_irn_constlike(param))
684 else if (is_Sel(param)) {
685 ir_node *base = param;
688 base = get_Sel_ptr(base);
689 } while (is_Sel(base));
691 if (base == get_irg_frame(current_ir_graph))
697 if (num_const_args > 0)
698 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
699 if (num_const_args == n)
700 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
701 if (num_local_adr > 0)
702 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
704 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
708 * Update info on calls.
710 * @param call The call
711 * @param graph The graph entry containing the call
713 static void stat_update_call(ir_node *call, graph_entry_t *graph)
715 ir_node *block = get_nodes_block(call);
716 ir_node *ptr = get_Call_ptr(call);
717 ir_entity *ent = NULL;
718 ir_graph *callee = NULL;
721 * If the block is bad, the whole subgraph will collapse later
722 * so do not count this call.
723 * This happens in dead code.
728 cnt_inc(&graph->cnt[gcnt_all_calls]);
730 /* found a call, this function is not a leaf */
733 if (is_SymConst(ptr)) {
734 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
735 /* ok, we seems to know the entity */
736 ent = get_SymConst_entity(ptr);
737 callee = get_entity_irg(ent);
739 /* it is recursive, if it calls at least once */
740 if (callee == graph->irg)
741 graph->is_recursive = 1;
743 cnt_inc(&graph->cnt[gcnt_external_calls]);
746 /* indirect call, be could not predict */
747 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
749 /* NOT a leaf call */
750 graph->is_leaf_call = LCS_NON_LEAF_CALL;
753 /* check, if it's a chain-call: Then, the call-block
754 * must dominate the end block. */
756 ir_node *curr = get_irg_end_block(graph->irg);
757 int depth = get_Block_dom_depth(block);
759 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
760 curr = get_Block_idom(curr);
762 if (! curr || !is_Block(curr))
767 graph->is_chain_call = 0;
770 /* check, if the callee is a leaf */
772 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
774 if (called->is_analyzed) {
775 if (! called->is_leaf)
776 graph->is_leaf_call = LCS_NON_LEAF_CALL;
780 analyse_params_of_Call(graph, call);
784 * Update info on calls for graphs on the wait queue.
786 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
788 ir_node *block = get_nodes_block(call);
789 ir_node *ptr = get_Call_ptr(call);
790 ir_entity *ent = NULL;
791 ir_graph *callee = NULL;
794 * If the block is bad, the whole subgraph will collapse later
795 * so do not count this call.
796 * This happens in dead code.
801 if (is_SymConst(ptr)) {
802 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
803 /* ok, we seems to know the entity */
804 ent = get_SymConst_entity(ptr);
805 callee = get_entity_irg(ent);
809 /* check, if the callee is a leaf */
811 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
813 assert(called->is_analyzed);
815 if (! called->is_leaf)
816 graph->is_leaf_call = LCS_NON_LEAF_CALL;
818 graph->is_leaf_call = LCS_NON_LEAF_CALL;
822 * Find the base address and entity of an Sel node.
824 * @param sel the node
826 * @return the base address.
828 static ir_node *find_base_adr(ir_node *sel)
830 ir_node *ptr = get_Sel_ptr(sel);
832 while (is_Sel(ptr)) {
834 ptr = get_Sel_ptr(sel);
840 * Update info on Load/Store address statistics.
842 static void stat_update_address(ir_node *node, graph_entry_t *graph)
844 unsigned opc = get_irn_opcode(node);
850 /* a global address */
851 cnt_inc(&graph->cnt[gcnt_global_adr]);
854 base = find_base_adr(node);
855 irg = current_ir_graph;
856 if (base == get_irg_frame(irg)) {
857 /* a local Variable. */
858 cnt_inc(&graph->cnt[gcnt_local_adr]);
861 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
862 /* pointer access through parameter, check for THIS */
863 ir_entity *ent = get_irg_entity(irg);
866 ir_type *ent_tp = get_entity_type(ent);
868 if (get_method_calling_convention(ent_tp) & cc_this_call) {
869 if (get_Proj_proj(base) == 0) {
871 cnt_inc(&graph->cnt[gcnt_this_adr]);
876 /* other parameter */
877 cnt_inc(&graph->cnt[gcnt_param_adr]);
880 /* unknown Pointer access */
881 cnt_inc(&graph->cnt[gcnt_other_adr]);
890 * Walker for reachable nodes count.
892 static void update_node_stat(ir_node *node, void *env)
894 graph_entry_t *graph = (graph_entry_t*)env;
897 ir_op *op = stat_get_irn_op(node);
898 int i, arity = get_irn_arity(node);
900 entry = opcode_get_entry(op, graph->opcode_hash);
902 cnt_inc(&entry->cnt_alive);
903 cnt_add_i(&graph->cnt[gcnt_edges], arity);
905 /* count block edges */
906 undate_block_info(node, graph);
908 /* handle statistics for special node types */
912 /* check for properties that depends on calls like recursion/leaf/indirect call */
913 stat_update_call(node, graph);
916 /* check address properties */
917 stat_update_address(get_Load_ptr(node), graph);
920 /* check address properties */
921 stat_update_address(get_Store_ptr(node), graph);
924 /* check for non-strict Phi nodes */
925 for (i = arity - 1; i >= 0; --i) {
926 ir_node *pred = get_Phi_pred(node, i);
927 if (is_Unknown(pred)) {
928 /* found an Unknown predecessor, graph is not strict */
929 graph->is_strict = 0;
937 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
938 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
941 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
942 ir_node *pred = get_irn_n(node, i);
944 if (is_Const(pred)) {
945 /* check properties of constants */
946 stat_update_const(status, pred, graph);
953 * Walker for reachable nodes count for graphs on the wait_q.
955 static void update_node_stat_2(ir_node *node, void *env)
957 graph_entry_t *graph = (graph_entry_t*)env;
959 /* check for properties that depends on calls like recursion/leaf/indirect call */
961 stat_update_call_2(node, graph);
965 * Get the current address mark.
967 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
969 address_mark_entry_t const val = { node, 0 };
970 address_mark_entry_t *value = set_find(address_mark_entry_t, graph->address_mark, &val, sizeof(val), hash_ptr(node));
972 return value ? value->mark : 0;
976 * Set the current address mark.
978 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
980 address_mark_entry_t const value = { node, val };
981 (void)set_insert(address_mark_entry_t, graph->address_mark, &value, sizeof(value), hash_ptr(node));
988 * a vcg attribute hook: Color a node with a different color if
989 * it's identified as a part of an address expression or at least referenced
990 * by an address expression.
992 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
994 ir_node *n = local ? local : node;
995 ir_graph *irg = get_irn_irg(n);
996 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
997 unsigned mark = get_adr_mark(graph, n);
999 if (mark & MARK_ADDRESS_CALC)
1000 fprintf(F, "color: purple");
1001 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1002 fprintf(F, "color: pink");
1003 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1004 fprintf(F, "color: lightblue");
1008 /* I know the color! */
1011 #endif /* DUMP_ADR_MODE */
1014 * Return the "operational" mode of a Firm node.
1016 static ir_mode *get_irn_op_mode(ir_node *node)
1018 switch (get_irn_opcode(node)) {
1020 return get_Load_mode(node);
1022 return get_irn_mode(get_Store_value(node));
1024 return get_irn_mode(get_Div_left(node));
1026 return get_irn_mode(get_Mod_left(node));
1028 /* Cmp is no address calculation, or is it? */
1030 return get_irn_mode(node);
1035 * Post-walker that marks every node that is an address calculation.
1037 * Users of a node must be visited first. We ensure this by
1038 * calling it in the post of an outs walk. This should work even in cycles,
1039 * while the normal pre-walk will not.
1041 static void mark_address_calc(ir_node *node, void *env)
1043 graph_entry_t *graph = (graph_entry_t*)env;
1044 ir_mode *mode = get_irn_op_mode(node);
1046 unsigned mark_preds = MARK_REF_NON_ADR;
1048 if (! mode_is_data(mode))
1051 if (mode_is_reference(mode)) {
1052 /* a reference is calculated here, we are sure */
1053 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1055 mark_preds = MARK_REF_ADR;
1057 unsigned mark = get_adr_mark(graph, node);
1059 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1061 * this node has no reference mode, but is only
1062 * referenced by address calculations
1064 mark_preds = MARK_REF_ADR;
1068 /* mark all predecessors */
1069 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1070 ir_node *pred = get_irn_n(node, i);
1072 mode = get_irn_op_mode(pred);
1073 if (! mode_is_data(mode))
1076 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1081 * Post-walker that marks every node that is an address calculation.
1083 * Users of a node must be visited first. We ensure this by
1084 * calling it in the post of an outs walk. This should work even in cycles,
1085 * while the normal pre-walk will not.
1087 static void count_adr_ops(ir_node *node, void *env)
1089 graph_entry_t *graph = (graph_entry_t*)env;
1090 unsigned mark = get_adr_mark(graph, node);
1092 if (mark & MARK_ADDRESS_CALC)
1093 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1094 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1095 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1096 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1097 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1101 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1102 * is called, must recalculate all statistic info.
1104 * @param global The global entry
1105 * @param graph The current entry
1107 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1111 /* clear first the alive counter in the graph */
1112 foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1113 cnt_clr(&entry->cnt_alive);
1116 /* set pessimistic values */
1118 graph->is_leaf_call = LCS_UNKNOWN;
1119 graph->is_recursive = 0;
1120 graph->is_chain_call = 1;
1121 graph->is_strict = 1;
1123 /* create new block counter */
1124 graph->block_hash = new_pset(block_cmp, 5);
1126 /* we need dominator info */
1127 if (graph->irg != get_const_code_irg()) {
1128 assure_doms(graph->irg);
1131 /* count the nodes in the graph */
1132 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1134 /* recursive functions are never chain calls, leafs don't have calls */
1135 if (graph->is_recursive || graph->is_leaf)
1136 graph->is_chain_call = 0;
1138 /* assume we walk every graph only ONCE, we could sum here the global count */
1139 foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1140 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1142 /* update the node counter */
1143 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1146 /* count the number of address calculation */
1147 if (graph->irg != get_const_code_irg()) {
1148 ir_graph *rem = current_ir_graph;
1150 assure_irg_outs(graph->irg);
1152 /* Must be done an the outs graph */
1153 current_ir_graph = graph->irg;
1154 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1155 current_ir_graph = rem;
1157 #ifdef DUMP_ADR_MODE
1158 /* register the vcg hook and dump the graph for test */
1159 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1160 dump_ir_block_graph(graph->irg, "-adr");
1161 set_dump_node_vcgattr_hook(NULL);
1162 #endif /* DUMP_ADR_MODE */
1164 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1167 /* count the DAG's */
1168 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1169 count_dags_in_graph(global, graph);
1171 /* calculate the patterns of this graph */
1172 stat_calc_pattern_history(graph->irg);
1174 /* leaf function did not call others */
1176 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1177 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1178 /* we still don't know if this graph calls leaf-functions, so enqueue */
1179 pdeq_putl(status->wait_q, graph);
1182 /* we have analyzed this graph */
1183 graph->is_analyzed = 1;
1185 /* accumulate all counter's */
1186 for (i = 0; i < _gcnt_last; ++i)
1187 cnt_add(&global->cnt[i], &graph->cnt[i]);
1191 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1192 * must finish all statistic info calculations.
1194 * @param global The global entry
1195 * @param graph The current entry
1197 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1200 if (graph->is_deleted) {
1201 /* deleted, ignore */
1206 /* count the nodes in the graph */
1207 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1209 if (graph->is_leaf_call == LCS_UNKNOWN)
1210 graph->is_leaf_call = LCS_LEAF_CALL;
1215 * Register a dumper.
1217 static void stat_register_dumper(const dumper_t *dumper)
1219 dumper_t *p = XMALLOC(dumper_t);
1222 p->next = status->dumper;
1226 /* FIXME: memory leak */
1230 * Dumps the statistics of an IR graph.
1232 static void stat_dump_graph(graph_entry_t *entry)
1236 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1237 if (dumper->dump_graph)
1238 dumper->dump_graph(dumper, entry);
1243 * Calls all registered dumper functions.
1245 static void stat_dump_registered(graph_entry_t *entry)
1249 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1250 if (dumper->func_map) {
1251 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1252 func(dumper, entry);
1258 * Dumps a constant table.
1260 static void stat_dump_consts(const constant_info_t *tbl)
1264 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1265 if (dumper->dump_const_tbl)
1266 dumper->dump_const_tbl(dumper, tbl);
1271 * Dumps the parameter distribution
1273 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1277 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1278 if (dumper->dump_param_tbl)
1279 dumper->dump_param_tbl(dumper, tbl, global);
1284 * Dumps the optimization counter
1286 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1290 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1291 if (dumper->dump_opt_cnt)
1292 dumper->dump_opt_cnt(dumper, tbl, len);
1297 * Initialize the dumper.
1299 static void stat_dump_init(const char *name)
1303 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1305 dumper->init(dumper, name);
1310 * Finish the dumper.
1312 static void stat_dump_finish(void)
1316 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1318 dumper->finish(dumper);
1323 * Register an additional function for all dumper.
1325 void stat_register_dumper_func(dump_graph_FUNC *const func)
1329 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1330 if (! dumper->func_map)
1331 dumper->func_map = pset_new_ptr(3);
1332 pset_insert_ptr(dumper->func_map, (void*)func);
1336 /* ---------------------------------------------------------------------- */
1339 * Helper: get an ir_op from an opcode.
1341 ir_op *stat_get_op_from_opcode(unsigned code)
1343 return opcode_find_entry((ir_opcode)code, status->ir_op_hash);
1347 * Hook: A new IR op is registered.
1349 * @param ctx the hook context
1350 * @param op the new IR opcode that was created.
1352 static void stat_new_ir_op(void *ctx, ir_op *op)
1355 if (! status->stat_options)
1360 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1362 /* execute for side effect :-) */
1363 (void)opcode_get_entry(op, graph->opcode_hash);
1365 pset_insert(status->ir_op_hash, op, op->code);
1371 * Hook: An IR op is freed.
1373 * @param ctx the hook context
1374 * @param op the IR opcode that is freed
1376 static void stat_free_ir_op(void *ctx, ir_op *op)
1380 if (! status->stat_options)
1390 * Hook: A new node is created.
1392 * @param ctx the hook context
1393 * @param irg the IR graph on which the node is created
1394 * @param node the new IR node that was created
1396 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1400 if (! status->stat_options)
1403 /* do NOT count during dead node elimination */
1404 if (status->in_dead_node_elim)
1409 node_entry_t *entry;
1410 graph_entry_t *graph;
1411 ir_op *op = stat_get_irn_op(node);
1413 /* increase global value */
1414 graph = graph_get_entry(NULL, status->irg_hash);
1415 entry = opcode_get_entry(op, graph->opcode_hash);
1416 cnt_inc(&entry->new_node);
1418 /* increase local value */
1419 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1420 entry = opcode_get_entry(op, graph->opcode_hash);
1421 cnt_inc(&entry->new_node);
1427 * Hook: A node is changed into a Id node
1429 * @param ctx the hook context
1430 * @param node the IR node that will be turned into an ID
1432 static void stat_turn_into_id(void *ctx, ir_node *node)
1435 if (! status->stat_options)
1440 node_entry_t *entry;
1441 graph_entry_t *graph;
1442 ir_op *op = stat_get_irn_op(node);
1444 /* increase global value */
1445 graph = graph_get_entry(NULL, status->irg_hash);
1446 entry = opcode_get_entry(op, graph->opcode_hash);
1447 cnt_inc(&entry->into_Id);
1449 /* increase local value */
1450 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1451 entry = opcode_get_entry(op, graph->opcode_hash);
1452 cnt_inc(&entry->into_Id);
1458 * Hook: A node is normalized
1460 * @param ctx the hook context
1461 * @param node the IR node that was normalized
1463 static void stat_normalize(void *ctx, ir_node *node)
1466 if (! status->stat_options)
1471 node_entry_t *entry;
1472 graph_entry_t *graph;
1473 ir_op *op = stat_get_irn_op(node);
1475 /* increase global value */
1476 graph = graph_get_entry(NULL, status->irg_hash);
1477 entry = opcode_get_entry(op, graph->opcode_hash);
1478 cnt_inc(&entry->normalized);
1480 /* increase local value */
1481 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1482 entry = opcode_get_entry(op, graph->opcode_hash);
1483 cnt_inc(&entry->normalized);
1489 * Hook: A new graph was created
1491 * @param ctx the hook context
1492 * @param irg the new IR graph that was created
1493 * @param ent the entity of this graph
1495 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1498 if (! status->stat_options)
1503 /* execute for side effect :-) */
1504 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1507 graph->is_deleted = 0;
1509 graph->is_leaf_call = 0;
1510 graph->is_recursive = 0;
1511 graph->is_chain_call = 0;
1512 graph->is_strict = 1;
1513 graph->is_analyzed = 0;
1519 * Hook: A graph will be deleted
1521 * @param ctx the hook context
1522 * @param irg the IR graph that will be deleted
1524 * Note that we still hold the information for this graph
1525 * in our hash maps, only a flag is set which prevents this
1526 * information from being changed, it's "frozen" from now.
1528 static void stat_free_graph(void *ctx, ir_graph *irg)
1531 if (! status->stat_options)
1536 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1537 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1539 graph->is_deleted = 1;
1541 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1542 /* count the nodes of the graph yet, it will be destroyed later */
1543 update_graph_stat(global, graph);
1550 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1552 * @param ctx the hook context
1553 * @param irg the IR graph that will be walked
1554 * @param pre the pre walker
1555 * @param post the post walker
1557 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1562 if (! status->stat_options)
1567 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1569 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1575 * Hook: A walk over a graph in block-wise order 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 pre the pre walker
1580 * @param post the post walker
1582 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1584 /* for now, do NOT differentiate between blockwise and normal */
1585 stat_irg_walk(ctx, irg, pre, post);
1589 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1591 * @param ctx the hook context
1592 * @param irg the IR graph that will be walked
1593 * @param node the IR node
1594 * @param pre the pre walker
1595 * @param post the post walker
1597 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1603 if (! status->stat_options)
1608 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1610 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1616 * Called for every node that is removed due to an optimization.
1618 * @param n the IR node that will be removed
1619 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1620 * @param kind the optimization kind
1622 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1625 ir_op *op = stat_get_irn_op(n);
1627 /* ignore CSE for Constants */
1628 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1631 /* increase global value */
1632 entry = opt_get_entry(op, hmap);
1633 cnt_inc(&entry->count);
1637 * Hook: Some nodes were optimized into some others due to an optimization.
1639 * @param ctx the hook context
1641 static void stat_merge_nodes(
1643 ir_node **new_node_array, int new_num_entries,
1644 ir_node **old_node_array, int old_num_entries,
1648 if (! status->stat_options)
1654 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1656 cnt_inc(&status->num_opts[opt]);
1657 if (status->reassoc_run)
1658 opt = HOOK_OPT_REASSOC;
1660 for (i = 0; i < old_num_entries; ++i) {
1661 /* nodes might be in new and old, so if we found a node
1662 in both sets, this one is NOT removed */
1663 for (j = 0; j < new_num_entries; ++j) {
1664 if (old_node_array[i] == new_node_array[j])
1667 if (j >= new_num_entries) {
1670 /* sometimes we did not detect, that it is replaced by a Const */
1671 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1672 ir_node *const irn = new_node_array[0];
1673 if (is_Const(irn) || is_SymConst(irn))
1674 xopt = HOOK_OPT_CONFIRM_C;
1677 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1685 * Hook: Reassociation is started/stopped.
1687 * @param ctx the hook context
1688 * @param flag if non-zero, reassociation is started else stopped
1690 static void stat_reassociate(void *ctx, int flag)
1693 if (! status->stat_options)
1698 status->reassoc_run = flag;
1704 * Hook: A node was lowered into other nodes
1706 * @param ctx the hook context
1707 * @param node the IR node that will be lowered
1709 static void stat_lower(void *ctx, ir_node *node)
1712 if (! status->stat_options)
1717 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1719 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1725 * Hook: A graph was inlined.
1727 * @param ctx the hook context
1728 * @param call the IR call that will re changed into the body of
1729 * the called IR graph
1730 * @param called_irg the IR graph representing the called routine
1732 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1735 if (! status->stat_options)
1740 ir_graph *irg = get_irn_irg(call);
1741 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1742 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1744 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1745 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1751 * Hook: A graph with tail-recursions was optimized.
1753 * @param ctx the hook context
1755 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1758 if (! status->stat_options)
1763 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1765 graph->num_tail_recursion += n_calls;
1771 * Strength reduction was performed on an iteration variable.
1773 * @param ctx the hook context
1775 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1778 if (! status->stat_options)
1783 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1784 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1786 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1792 * Hook: Start/Stop the dead node elimination.
1794 * @param ctx the hook context
1796 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1800 if (! status->stat_options)
1803 status->in_dead_node_elim = (start != 0);
1807 * Hook: if-conversion was tried.
1809 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1810 int pos, ir_node *mux, if_result_t reason)
1816 if (! status->stat_options)
1821 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1823 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1829 * Hook: real function call was optimized.
1831 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1835 if (! status->stat_options)
1840 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1842 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1848 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1850 * @param ctx the hook context
1852 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1855 if (! status->stat_options)
1860 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1861 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1867 * Hook: A division by const was replaced.
1869 * @param ctx the hook context
1870 * @param node the division node that will be optimized
1872 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1875 if (! status->stat_options)
1880 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1881 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1887 * Update the register pressure of a block.
1889 * @param irg the irg containing the block
1890 * @param block the block for which the reg pressure should be set
1891 * @param pressure the pressure
1892 * @param class_name the name of the register class
1894 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1896 if (! status->stat_options)
1901 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1902 be_block_entry_t *block_ent;
1903 reg_pressure_entry_t *rp_ent;
1905 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1906 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1908 rp_ent->class_name = class_name;
1909 rp_ent->pressure = pressure;
1911 pset_insert(block_ent->reg_pressure, rp_ent, hash_ptr(class_name));
1917 * Update the distribution of ready nodes of a block
1919 * @param irg the irg containing the block
1920 * @param block the block for which the reg pressure should be set
1921 * @param num_ready the number of ready nodes
1923 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1925 if (! status->stat_options)
1930 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1931 be_block_entry_t *block_ent;
1933 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1935 /* increase the counter of corresponding number of ready nodes */
1936 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1942 * Update the permutation statistic of a block.
1944 * @param class_name the name of the register class
1945 * @param n_regs number of registers in the register class
1946 * @param perm the perm node
1947 * @param block the block containing the perm
1948 * @param size the size of the perm
1949 * @param real_size number of pairs with different registers
1951 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1952 int size, int real_size)
1954 if (! status->stat_options)
1959 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1960 be_block_entry_t *block_ent;
1961 perm_class_entry_t *pc_ent;
1962 perm_stat_entry_t *ps_ent;
1964 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1965 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1966 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1968 pc_ent->n_regs = n_regs;
1970 /* update information */
1971 ps_ent->size = size;
1972 ps_ent->real_size = real_size;
1978 * Update the permutation statistic of a single perm.
1980 * @param class_name the name of the register class
1981 * @param perm the perm node
1982 * @param block the block containing the perm
1983 * @param is_chain 1 if chain, 0 if cycle
1984 * @param size length of the cycle/chain
1985 * @param n_ops the number of ops representing this cycle/chain after lowering
1987 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1988 int is_chain, int size, int n_ops)
1990 if (! status->stat_options)
1995 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1996 be_block_entry_t *block_ent;
1997 perm_class_entry_t *pc_ent;
1998 perm_stat_entry_t *ps_ent;
2000 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2001 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2002 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2005 ps_ent->n_copies += n_ops;
2006 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2008 ps_ent->n_exchg += n_ops;
2009 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2015 /* Dumps a statistics snapshot. */
2016 void stat_dump_snapshot(const char *name, const char *phase)
2022 if (! status->stat_options)
2027 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2030 * The constant counter is only global, so we clear it here.
2031 * Note that it does NOT contain the constants in DELETED
2032 * graphs due to this.
2034 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2035 stat_const_clear(status);
2037 /* build the name */
2038 p = strrchr(name, '/');
2043 q = strrchr(name, '\\');
2045 /* NULL might be not the smallest pointer */
2046 if (q && (!p || q > p))
2054 if (l > (int) (sizeof(fname) - 1))
2055 l = sizeof(fname) - 1;
2057 memcpy(fname, name, l);
2063 strncat(fname, "firmstat-", sizeof(fname)-strlen(fname)-1);
2064 strncat(fname, phase, sizeof(fname)-strlen(fname)-1);
2065 strncat(fname, "-", sizeof(fname)-strlen(fname)-1);
2066 strncat(fname, p, sizeof(fname)-strlen(fname)-1);
2068 stat_dump_init(fname);
2070 /* calculate the graph statistics */
2071 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2072 if (entry->irg == NULL) {
2073 /* special entry for the global count */
2076 if (! entry->is_deleted) {
2077 /* the graph is still alive, count the nodes on it */
2078 update_graph_stat(global, entry);
2082 /* some calculations are dependent, we pushed them on the wait_q */
2083 while (! pdeq_empty(status->wait_q)) {
2084 graph_entry_t *const entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2086 update_graph_stat_2(global, entry);
2089 /* dump per graph */
2090 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2091 if (entry->irg == NULL) {
2092 /* special entry for the global count */
2096 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2097 stat_dump_graph(entry);
2098 stat_dump_registered(entry);
2101 if (! entry->is_deleted) {
2102 /* clear the counter that are not accumulated */
2103 graph_clear_entry(entry, 0);
2108 stat_dump_graph(global);
2110 /* dump the const info */
2111 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2112 stat_dump_consts(&status->const_info);
2114 /* dump the parameter distribution */
2115 stat_dump_param_tbl(status->dist_param_cnt, global);
2117 /* dump the optimization counter and clear them */
2118 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2119 clear_optimization_counter();
2123 stat_finish_pattern_history(fname);
2125 /* clear the global counters here */
2126 foreach_pset(global->opcode_hash, node_entry_t, entry) {
2127 opcode_clear_entry(entry);
2129 /* clear all global counter */
2130 graph_clear_entry(global, /*all=*/1);
2135 typedef struct pass_t {
2136 ir_prog_pass_t pass;
2142 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2144 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2146 pass_t *pass = (pass_t*)context;
2149 stat_dump_snapshot(pass->fname, pass->phase);
2154 * Ensure that no verifier is run from the wrapper.
2156 static int no_verify(ir_prog *prog, void *ctx)
2164 * Ensure that no dumper is run from the wrapper.
2166 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2173 /* create an ir_pog pass */
2174 ir_prog_pass_t *stat_dump_snapshot_pass(
2175 const char *name, const char *fname, const char *phase)
2177 pass_t *pass = XMALLOCZ(pass_t);
2179 def_prog_pass_constructor(
2180 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2181 pass->fname = fname;
2182 pass->phase = phase;
2184 /* no dump/verify */
2185 pass->pass.dump_irprog = no_dump;
2186 pass->pass.verify_irprog = no_verify;
2191 /** the hook entries for the Firm statistics module */
2192 static hook_entry_t stat_hooks[hook_last];
2194 /* initialize the statistics module. */
2195 void firm_init_stat(void)
2197 #define X(a) a, sizeof(a)-1
2198 #define HOOK(h, fkt) \
2199 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2202 if (! (stat_options & FIRMSTAT_ENABLED))
2205 status = XMALLOCZ(stat_info_t);
2207 /* enable statistics */
2208 status->stat_options = stat_options & FIRMSTAT_ENABLED ? stat_options : 0;
2210 /* register all hooks */
2211 HOOK(hook_new_ir_op, stat_new_ir_op);
2212 HOOK(hook_free_ir_op, stat_free_ir_op);
2213 HOOK(hook_new_node, stat_new_node);
2214 HOOK(hook_turn_into_id, stat_turn_into_id);
2215 HOOK(hook_normalize, stat_normalize);
2216 HOOK(hook_new_graph, stat_new_graph);
2217 HOOK(hook_free_graph, stat_free_graph);
2218 HOOK(hook_irg_walk, stat_irg_walk);
2219 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2220 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2221 HOOK(hook_merge_nodes, stat_merge_nodes);
2222 HOOK(hook_reassociate, stat_reassociate);
2223 HOOK(hook_lower, stat_lower);
2224 HOOK(hook_inline, stat_inline);
2225 HOOK(hook_tail_rec, stat_tail_rec);
2226 HOOK(hook_strength_red, stat_strength_red);
2227 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2228 HOOK(hook_if_conversion, stat_if_conversion);
2229 HOOK(hook_func_call, stat_func_call);
2230 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2231 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2233 obstack_init(&status->cnts);
2234 obstack_init(&status->be_data);
2236 /* create the hash-tables */
2237 status->irg_hash = new_pset(graph_cmp, 8);
2238 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2240 /* create the wait queue */
2241 status->wait_q = new_pdeq();
2243 if (stat_options & FIRMSTAT_COUNT_STRONG_OP) {
2244 /* build the pseudo-ops */
2246 _op_Phi0.code = --num;
2247 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2249 _op_PhiM.code = --num;
2250 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2252 _op_ProjM.code = --num;
2253 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2255 _op_MulC.code = --num;
2256 _op_MulC.name = new_id_from_chars(X("MulC"));
2258 _op_DivC.code = --num;
2259 _op_DivC.name = new_id_from_chars(X("DivC"));
2261 _op_ModC.code = --num;
2262 _op_ModC.name = new_id_from_chars(X("ModC"));
2264 status->op_Phi0 = &_op_Phi0;
2265 status->op_PhiM = &_op_PhiM;
2266 status->op_ProjM = &_op_ProjM;
2267 status->op_MulC = &_op_MulC;
2268 status->op_DivC = &_op_DivC;
2269 status->op_ModC = &_op_ModC;
2271 status->op_Phi0 = NULL;
2272 status->op_PhiM = NULL;
2273 status->op_ProjM = NULL;
2274 status->op_MulC = NULL;
2275 status->op_DivC = NULL;
2276 status->op_ModC = NULL;
2279 /* for Florian: count the Sel depth */
2280 if (stat_options & FIRMSTAT_COUNT_SELS) {
2281 _op_SelSel.code = --num;
2282 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2284 _op_SelSelSel.code = --num;
2285 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2287 status->op_SelSel = &_op_SelSel;
2288 status->op_SelSelSel = &_op_SelSelSel;
2290 status->op_SelSel = NULL;
2291 status->op_SelSelSel = NULL;
2294 /* register the dumper */
2295 stat_register_dumper(&simple_dumper);
2297 if (stat_options & FIRMSTAT_CSV_OUTPUT)
2298 stat_register_dumper(&csv_dumper);
2300 /* initialize the pattern hash */
2301 stat_init_pattern_history(stat_options & FIRMSTAT_PATTERN_ENABLED);
2303 /* initialize the Const options */
2304 if (stat_options & FIRMSTAT_COUNT_CONSTS)
2305 stat_init_const_cnt(status);
2307 /* distribution table for parameter counts */
2308 status->dist_param_cnt = stat_new_int_distrib_tbl();
2310 clear_optimization_counter();
2317 * Frees all dumper structures.
2319 static void stat_term_dumper(void)
2321 dumper_t *dumper, *next_dumper;
2323 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2324 if (dumper->func_map)
2325 del_pset(dumper->func_map);
2327 next_dumper = dumper->next;
2329 dumper = next_dumper;
2334 /* Terminates the statistics module, frees all memory. */
2335 void stat_term(void)
2337 if (status != (stat_info_t *)&status_disable) {
2338 obstack_free(&status->be_data, NULL);
2339 obstack_free(&status->cnts, NULL);
2344 status = (stat_info_t *)&status_disable;
2348 /* returns 1 if statistics were initialized, 0 otherwise */
2349 int stat_is_active(void)
2351 return status != (stat_info_t *)&status_disable;
2354 void init_stat(void)
2356 lc_opt_entry_t *root_grp = firm_opt_get_root();
2357 lc_opt_entry_t *be_grp = lc_opt_get_grp(root_grp, "be");
2359 static const lc_opt_enum_mask_items_t stat_items[] = {
2360 { "enabled", FIRMSTAT_ENABLED },
2361 { "pattern", FIRMSTAT_PATTERN_ENABLED },
2362 { "count_strong_op", FIRMSTAT_COUNT_STRONG_OP },
2363 { "count_dag", FIRMSTAT_COUNT_DAG },
2364 { "count_deleted", FIRMSTAT_COUNT_DELETED },
2365 { "count_sels", FIRMSTAT_COUNT_SELS },
2366 { "count_consts", FIRMSTAT_COUNT_CONSTS },
2367 { "csv_output", FIRMSTAT_CSV_OUTPUT },
2370 static lc_opt_enum_mask_var_t statmask = { &stat_options, stat_items };
2371 static const lc_opt_table_entry_t stat_optionstable[] = {
2372 LC_OPT_ENT_ENUM_MASK("statistics", "enable statistics", &statmask),
2375 lc_opt_add_table(be_grp, stat_optionstable);