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;
163 } /* reg_pressure_cmp */
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;
174 } /* perm_stat_cmp */
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;
185 } /* perm_class_cmp */
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;
209 } /* address_mark_cmp */
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);
220 } /* opcode_clear_entry */
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);
248 } /* opcode_get_entry */
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);
262 } /* opcode_find_entry */
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);
286 } /* graph_clear_entry */
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));
331 } /* graph_get_entry */
334 * Clear all counter in an opt_entry_t.
336 static void opt_clear_entry(opt_entry_t *elem)
338 cnt_clr(&elem->count);
339 } /* opt_clear_entry */
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);
366 } /* opt_get_entry */
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]);
377 } /* block_clear_entry */
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);
404 } /* block_get_entry */
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);
423 } /* be_block_clear_entry */
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);
450 } /* be_block_get_entry */
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);
461 } /* perm_class_clear_entry */
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));
489 } /* perm_class_get_entry */
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();
504 } /* perm_stat_clear_entry */
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));
531 } /* perm_stat_get_entry */
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;
603 } /* stat_get_irn_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]);
668 } /* undate_block_info */
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);
705 } /* analyse_params_of_Call */
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);
781 } /* stat_update_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;
819 } /* stat_update_call_2 */
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);
837 } /* find_base_adr */
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]);
887 } /* stat_update_address */
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);
950 } /* update_node_stat */
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);
962 } /* update_node_stat_2 */
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! */
1010 } /* stat_adr_mark_hook */
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);
1032 } /* get_irn_op_mode */
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);
1078 } /* mark_address_calc */
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]);
1098 } /* count_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);
1114 } /* foreach_pset */
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);
1135 /* Uncomment this code if chain-call means call exact one. */
1136 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1138 /* check if we have more than 1 call */
1139 if (cnt_gt(entry->cnt_alive, 1))
1140 graph->is_chain_call = 0;
1143 /* recursive functions are never chain calls, leafs don't have calls */
1144 if (graph->is_recursive || graph->is_leaf)
1145 graph->is_chain_call = 0;
1147 /* assume we walk every graph only ONCE, we could sum here the global count */
1148 foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1149 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1151 /* update the node counter */
1152 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1153 } /* foreach_pset */
1155 /* count the number of address calculation */
1156 if (graph->irg != get_const_code_irg()) {
1157 ir_graph *rem = current_ir_graph;
1159 assure_irg_outs(graph->irg);
1161 /* Must be done an the outs graph */
1162 current_ir_graph = graph->irg;
1163 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1164 current_ir_graph = rem;
1166 #ifdef DUMP_ADR_MODE
1167 /* register the vcg hook and dump the graph for test */
1168 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1169 dump_ir_block_graph(graph->irg, "-adr");
1170 set_dump_node_vcgattr_hook(NULL);
1171 #endif /* DUMP_ADR_MODE */
1173 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1176 /* count the DAG's */
1177 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1178 count_dags_in_graph(global, graph);
1180 /* calculate the patterns of this graph */
1181 stat_calc_pattern_history(graph->irg);
1183 /* leaf function did not call others */
1185 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1186 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1187 /* we still don't know if this graph calls leaf-functions, so enqueue */
1188 pdeq_putl(status->wait_q, graph);
1191 /* we have analyzed this graph */
1192 graph->is_analyzed = 1;
1194 /* accumulate all counter's */
1195 for (i = 0; i < _gcnt_last; ++i)
1196 cnt_add(&global->cnt[i], &graph->cnt[i]);
1197 } /* update_graph_stat */
1200 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1201 * must finish all statistic info calculations.
1203 * @param global The global entry
1204 * @param graph The current entry
1206 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1209 if (graph->is_deleted) {
1210 /* deleted, ignore */
1215 /* count the nodes in the graph */
1216 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1218 if (graph->is_leaf_call == LCS_UNKNOWN)
1219 graph->is_leaf_call = LCS_LEAF_CALL;
1221 } /* update_graph_stat_2 */
1224 * Register a dumper.
1226 static void stat_register_dumper(const dumper_t *dumper)
1228 dumper_t *p = XMALLOC(dumper_t);
1231 p->next = status->dumper;
1235 /* FIXME: memory leak */
1236 } /* stat_register_dumper */
1239 * Dumps the statistics of an IR graph.
1241 static void stat_dump_graph(graph_entry_t *entry)
1245 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1246 if (dumper->dump_graph)
1247 dumper->dump_graph(dumper, entry);
1249 } /* stat_dump_graph */
1252 * Calls all registered dumper functions.
1254 static void stat_dump_registered(graph_entry_t *entry)
1258 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1259 if (dumper->func_map) {
1260 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1261 func(dumper, entry);
1264 } /* stat_dump_registered */
1267 * Dumps a constant table.
1269 static void stat_dump_consts(const constant_info_t *tbl)
1273 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1274 if (dumper->dump_const_tbl)
1275 dumper->dump_const_tbl(dumper, tbl);
1277 } /* stat_dump_consts */
1280 * Dumps the parameter distribution
1282 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1286 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1287 if (dumper->dump_param_tbl)
1288 dumper->dump_param_tbl(dumper, tbl, global);
1290 } /* stat_dump_param_tbl */
1293 * Dumps the optimization counter
1295 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1299 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1300 if (dumper->dump_opt_cnt)
1301 dumper->dump_opt_cnt(dumper, tbl, len);
1303 } /* stat_dump_opt_cnt */
1306 * Initialize the dumper.
1308 static void stat_dump_init(const char *name)
1312 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1314 dumper->init(dumper, name);
1316 } /* stat_dump_init */
1319 * Finish the dumper.
1321 static void stat_dump_finish(void)
1325 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1327 dumper->finish(dumper);
1329 } /* stat_dump_finish */
1332 * Register an additional function for all dumper.
1334 void stat_register_dumper_func(dump_graph_FUNC *const func)
1338 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1339 if (! dumper->func_map)
1340 dumper->func_map = pset_new_ptr(3);
1341 pset_insert_ptr(dumper->func_map, (void*)func);
1343 } /* stat_register_dumper_func */
1345 /* ---------------------------------------------------------------------- */
1348 * Helper: get an ir_op from an opcode.
1350 ir_op *stat_get_op_from_opcode(unsigned code)
1352 return opcode_find_entry((ir_opcode)code, status->ir_op_hash);
1353 } /* stat_get_op_from_opcode */
1356 * Hook: A new IR op is registered.
1358 * @param ctx the hook context
1359 * @param op the new IR opcode that was created.
1361 static void stat_new_ir_op(void *ctx, ir_op *op)
1364 if (! status->stat_options)
1369 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1371 /* execute for side effect :-) */
1372 (void)opcode_get_entry(op, graph->opcode_hash);
1374 pset_insert(status->ir_op_hash, op, op->code);
1377 } /* stat_new_ir_op */
1380 * Hook: An IR op is freed.
1382 * @param ctx the hook context
1383 * @param op the IR opcode that is freed
1385 static void stat_free_ir_op(void *ctx, ir_op *op)
1389 if (! status->stat_options)
1396 } /* stat_free_ir_op */
1399 * Hook: A new node is created.
1401 * @param ctx the hook context
1402 * @param irg the IR graph on which the node is created
1403 * @param node the new IR node that was created
1405 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1409 if (! status->stat_options)
1412 /* do NOT count during dead node elimination */
1413 if (status->in_dead_node_elim)
1418 node_entry_t *entry;
1419 graph_entry_t *graph;
1420 ir_op *op = stat_get_irn_op(node);
1422 /* increase global value */
1423 graph = graph_get_entry(NULL, status->irg_hash);
1424 entry = opcode_get_entry(op, graph->opcode_hash);
1425 cnt_inc(&entry->new_node);
1427 /* increase local value */
1428 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1429 entry = opcode_get_entry(op, graph->opcode_hash);
1430 cnt_inc(&entry->new_node);
1433 } /* stat_new_node */
1436 * Hook: A node is changed into a Id node
1438 * @param ctx the hook context
1439 * @param node the IR node that will be turned into an ID
1441 static void stat_turn_into_id(void *ctx, ir_node *node)
1444 if (! status->stat_options)
1449 node_entry_t *entry;
1450 graph_entry_t *graph;
1451 ir_op *op = stat_get_irn_op(node);
1453 /* increase global value */
1454 graph = graph_get_entry(NULL, status->irg_hash);
1455 entry = opcode_get_entry(op, graph->opcode_hash);
1456 cnt_inc(&entry->into_Id);
1458 /* increase local value */
1459 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1460 entry = opcode_get_entry(op, graph->opcode_hash);
1461 cnt_inc(&entry->into_Id);
1464 } /* stat_turn_into_id */
1467 * Hook: A node is normalized
1469 * @param ctx the hook context
1470 * @param node the IR node that was normalized
1472 static void stat_normalize(void *ctx, ir_node *node)
1475 if (! status->stat_options)
1480 node_entry_t *entry;
1481 graph_entry_t *graph;
1482 ir_op *op = stat_get_irn_op(node);
1484 /* increase global value */
1485 graph = graph_get_entry(NULL, status->irg_hash);
1486 entry = opcode_get_entry(op, graph->opcode_hash);
1487 cnt_inc(&entry->normalized);
1489 /* increase local value */
1490 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1491 entry = opcode_get_entry(op, graph->opcode_hash);
1492 cnt_inc(&entry->normalized);
1495 } /* stat_normalize */
1498 * Hook: A new graph was created
1500 * @param ctx the hook context
1501 * @param irg the new IR graph that was created
1502 * @param ent the entity of this graph
1504 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1507 if (! status->stat_options)
1512 /* execute for side effect :-) */
1513 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1516 graph->is_deleted = 0;
1518 graph->is_leaf_call = 0;
1519 graph->is_recursive = 0;
1520 graph->is_chain_call = 0;
1521 graph->is_strict = 1;
1522 graph->is_analyzed = 0;
1525 } /* stat_new_graph */
1528 * Hook: A graph will be deleted
1530 * @param ctx the hook context
1531 * @param irg the IR graph that will be deleted
1533 * Note that we still hold the information for this graph
1534 * in our hash maps, only a flag is set which prevents this
1535 * information from being changed, it's "frozen" from now.
1537 static void stat_free_graph(void *ctx, ir_graph *irg)
1540 if (! status->stat_options)
1545 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1546 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1548 graph->is_deleted = 1;
1550 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1551 /* count the nodes of the graph yet, it will be destroyed later */
1552 update_graph_stat(global, graph);
1556 } /* stat_free_graph */
1559 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1561 * @param ctx the hook context
1562 * @param irg the IR graph that will be walked
1563 * @param pre the pre walker
1564 * @param post the post walker
1566 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1571 if (! status->stat_options)
1576 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1578 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1581 } /* stat_irg_walk */
1584 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1586 * @param ctx the hook context
1587 * @param irg the IR graph that will be walked
1588 * @param pre the pre walker
1589 * @param post the post walker
1591 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1593 /* for now, do NOT differentiate between blockwise and normal */
1594 stat_irg_walk(ctx, irg, pre, post);
1595 } /* stat_irg_walk_blkwise */
1598 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1600 * @param ctx the hook context
1601 * @param irg the IR graph that will be walked
1602 * @param node the IR node
1603 * @param pre the pre walker
1604 * @param post the post walker
1606 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1612 if (! status->stat_options)
1617 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1619 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1622 } /* stat_irg_block_walk */
1625 * Called for every node that is removed due to an optimization.
1627 * @param n the IR node that will be removed
1628 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1629 * @param kind the optimization kind
1631 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1634 ir_op *op = stat_get_irn_op(n);
1636 /* ignore CSE for Constants */
1637 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1640 /* increase global value */
1641 entry = opt_get_entry(op, hmap);
1642 cnt_inc(&entry->count);
1643 } /* removed_due_opt */
1646 * Hook: Some nodes were optimized into some others due to an optimization.
1648 * @param ctx the hook context
1650 static void stat_merge_nodes(
1652 ir_node **new_node_array, int new_num_entries,
1653 ir_node **old_node_array, int old_num_entries,
1657 if (! status->stat_options)
1663 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1665 cnt_inc(&status->num_opts[opt]);
1666 if (status->reassoc_run)
1667 opt = HOOK_OPT_REASSOC;
1669 for (i = 0; i < old_num_entries; ++i) {
1670 /* nodes might be in new and old, so if we found a node
1671 in both sets, this one is NOT removed */
1672 for (j = 0; j < new_num_entries; ++j) {
1673 if (old_node_array[i] == new_node_array[j])
1676 if (j >= new_num_entries) {
1679 /* sometimes we did not detect, that it is replaced by a Const */
1680 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1681 ir_node *const irn = new_node_array[0];
1682 if (is_Const(irn) || is_SymConst(irn))
1683 xopt = HOOK_OPT_CONFIRM_C;
1686 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1691 } /* stat_merge_nodes */
1694 * Hook: Reassociation is started/stopped.
1696 * @param ctx the hook context
1697 * @param flag if non-zero, reassociation is started else stopped
1699 static void stat_reassociate(void *ctx, int flag)
1702 if (! status->stat_options)
1707 status->reassoc_run = flag;
1710 } /* stat_reassociate */
1713 * Hook: A node was lowered into other nodes
1715 * @param ctx the hook context
1716 * @param node the IR node that will be lowered
1718 static void stat_lower(void *ctx, ir_node *node)
1721 if (! status->stat_options)
1726 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1728 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1734 * Hook: A graph was inlined.
1736 * @param ctx the hook context
1737 * @param call the IR call that will re changed into the body of
1738 * the called IR graph
1739 * @param called_irg the IR graph representing the called routine
1741 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1744 if (! status->stat_options)
1749 ir_graph *irg = get_irn_irg(call);
1750 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1751 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1753 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1754 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1760 * Hook: A graph with tail-recursions was optimized.
1762 * @param ctx the hook context
1764 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1767 if (! status->stat_options)
1772 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1774 graph->num_tail_recursion += n_calls;
1777 } /* stat_tail_rec */
1780 * Strength reduction was performed on an iteration variable.
1782 * @param ctx the hook context
1784 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1787 if (! status->stat_options)
1792 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1793 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1795 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1798 } /* stat_strength_red */
1801 * Hook: Start/Stop the dead node elimination.
1803 * @param ctx the hook context
1805 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1809 if (! status->stat_options)
1812 status->in_dead_node_elim = (start != 0);
1813 } /* stat_dead_node_elim */
1816 * Hook: if-conversion was tried.
1818 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1819 int pos, ir_node *mux, if_result_t reason)
1825 if (! status->stat_options)
1830 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1832 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1835 } /* stat_if_conversion */
1838 * Hook: real function call was optimized.
1840 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1844 if (! status->stat_options)
1849 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1851 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1854 } /* stat_func_call */
1857 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1859 * @param ctx the hook context
1861 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1864 if (! status->stat_options)
1869 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1870 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1873 } /* stat_arch_dep_replace_mul_with_shifts */
1876 * Hook: A division by const was replaced.
1878 * @param ctx the hook context
1879 * @param node the division node that will be optimized
1881 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1884 if (! status->stat_options)
1889 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1890 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1893 } /* stat_arch_dep_replace_division_by_const */
1896 * Update the register pressure of a block.
1898 * @param irg the irg containing the block
1899 * @param block the block for which the reg pressure should be set
1900 * @param pressure the pressure
1901 * @param class_name the name of the register class
1903 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1905 if (! status->stat_options)
1910 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1911 be_block_entry_t *block_ent;
1912 reg_pressure_entry_t *rp_ent;
1914 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1915 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1917 rp_ent->class_name = class_name;
1918 rp_ent->pressure = pressure;
1920 pset_insert(block_ent->reg_pressure, rp_ent, hash_ptr(class_name));
1923 } /* stat_be_block_regpressure */
1926 * Update the distribution of ready nodes of a block
1928 * @param irg the irg containing the block
1929 * @param block the block for which the reg pressure should be set
1930 * @param num_ready the number of ready nodes
1932 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1934 if (! status->stat_options)
1939 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1940 be_block_entry_t *block_ent;
1942 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1944 /* increase the counter of corresponding number of ready nodes */
1945 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1948 } /* stat_be_block_sched_ready */
1951 * Update the permutation statistic of a block.
1953 * @param class_name the name of the register class
1954 * @param n_regs number of registers in the register class
1955 * @param perm the perm node
1956 * @param block the block containing the perm
1957 * @param size the size of the perm
1958 * @param real_size number of pairs with different registers
1960 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1961 int size, int real_size)
1963 if (! status->stat_options)
1968 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1969 be_block_entry_t *block_ent;
1970 perm_class_entry_t *pc_ent;
1971 perm_stat_entry_t *ps_ent;
1973 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1974 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1975 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1977 pc_ent->n_regs = n_regs;
1979 /* update information */
1980 ps_ent->size = size;
1981 ps_ent->real_size = real_size;
1984 } /* stat_be_block_stat_perm */
1987 * Update the permutation statistic of a single perm.
1989 * @param class_name the name of the register class
1990 * @param perm the perm node
1991 * @param block the block containing the perm
1992 * @param is_chain 1 if chain, 0 if cycle
1993 * @param size length of the cycle/chain
1994 * @param n_ops the number of ops representing this cycle/chain after lowering
1996 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1997 int is_chain, int size, int n_ops)
1999 if (! status->stat_options)
2004 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2005 be_block_entry_t *block_ent;
2006 perm_class_entry_t *pc_ent;
2007 perm_stat_entry_t *ps_ent;
2009 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2010 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2011 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2014 ps_ent->n_copies += n_ops;
2015 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2017 ps_ent->n_exchg += n_ops;
2018 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2022 } /* stat_be_block_stat_permcycle */
2024 /* Dumps a statistics snapshot. */
2025 void stat_dump_snapshot(const char *name, const char *phase)
2031 if (! status->stat_options)
2036 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2039 * The constant counter is only global, so we clear it here.
2040 * Note that it does NOT contain the constants in DELETED
2041 * graphs due to this.
2043 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2044 stat_const_clear(status);
2046 /* build the name */
2047 p = strrchr(name, '/');
2052 q = strrchr(name, '\\');
2054 /* NULL might be not the smallest pointer */
2055 if (q && (!p || q > p))
2063 if (l > (int) (sizeof(fname) - 1))
2064 l = sizeof(fname) - 1;
2066 memcpy(fname, name, l);
2072 strncat(fname, "firmstat-", sizeof(fname)-strlen(fname)-1);
2073 strncat(fname, phase, sizeof(fname)-strlen(fname)-1);
2074 strncat(fname, "-", sizeof(fname)-strlen(fname)-1);
2075 strncat(fname, p, sizeof(fname)-strlen(fname)-1);
2077 stat_dump_init(fname);
2079 /* calculate the graph statistics */
2080 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2081 if (entry->irg == NULL) {
2082 /* special entry for the global count */
2085 if (! entry->is_deleted) {
2086 /* the graph is still alive, count the nodes on it */
2087 update_graph_stat(global, entry);
2091 /* some calculations are dependent, we pushed them on the wait_q */
2092 while (! pdeq_empty(status->wait_q)) {
2093 graph_entry_t *const entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2095 update_graph_stat_2(global, entry);
2098 /* dump per graph */
2099 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2100 if (entry->irg == NULL) {
2101 /* special entry for the global count */
2105 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2106 stat_dump_graph(entry);
2107 stat_dump_registered(entry);
2110 if (! entry->is_deleted) {
2111 /* clear the counter that are not accumulated */
2112 graph_clear_entry(entry, 0);
2117 stat_dump_graph(global);
2119 /* dump the const info */
2120 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2121 stat_dump_consts(&status->const_info);
2123 /* dump the parameter distribution */
2124 stat_dump_param_tbl(status->dist_param_cnt, global);
2126 /* dump the optimization counter and clear them */
2127 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2128 clear_optimization_counter();
2132 stat_finish_pattern_history(fname);
2134 /* clear the global counters here */
2135 foreach_pset(global->opcode_hash, node_entry_t, entry) {
2136 opcode_clear_entry(entry);
2138 /* clear all global counter */
2139 graph_clear_entry(global, /*all=*/1);
2142 } /* stat_dump_snapshot */
2144 typedef struct pass_t {
2145 ir_prog_pass_t pass;
2151 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2153 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2155 pass_t *pass = (pass_t*)context;
2158 stat_dump_snapshot(pass->fname, pass->phase);
2160 } /* stat_dump_snapshot_wrapper */
2163 * Ensure that no verifier is run from the wrapper.
2165 static int no_verify(ir_prog *prog, void *ctx)
2173 * Ensure that no dumper is run from the wrapper.
2175 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2182 /* create an ir_pog pass */
2183 ir_prog_pass_t *stat_dump_snapshot_pass(
2184 const char *name, const char *fname, const char *phase)
2186 pass_t *pass = XMALLOCZ(pass_t);
2188 def_prog_pass_constructor(
2189 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2190 pass->fname = fname;
2191 pass->phase = phase;
2193 /* no dump/verify */
2194 pass->pass.dump_irprog = no_dump;
2195 pass->pass.verify_irprog = no_verify;
2198 } /* stat_dump_snapshot_pass */
2200 /** the hook entries for the Firm statistics module */
2201 static hook_entry_t stat_hooks[hook_last];
2203 /* initialize the statistics module. */
2204 void firm_init_stat(void)
2206 #define X(a) a, sizeof(a)-1
2207 #define HOOK(h, fkt) \
2208 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2211 if (! (stat_options & FIRMSTAT_ENABLED))
2214 status = XMALLOCZ(stat_info_t);
2216 /* enable statistics */
2217 status->stat_options = stat_options & FIRMSTAT_ENABLED ? stat_options : 0;
2219 /* register all hooks */
2220 HOOK(hook_new_ir_op, stat_new_ir_op);
2221 HOOK(hook_free_ir_op, stat_free_ir_op);
2222 HOOK(hook_new_node, stat_new_node);
2223 HOOK(hook_turn_into_id, stat_turn_into_id);
2224 HOOK(hook_normalize, stat_normalize);
2225 HOOK(hook_new_graph, stat_new_graph);
2226 HOOK(hook_free_graph, stat_free_graph);
2227 HOOK(hook_irg_walk, stat_irg_walk);
2228 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2229 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2230 HOOK(hook_merge_nodes, stat_merge_nodes);
2231 HOOK(hook_reassociate, stat_reassociate);
2232 HOOK(hook_lower, stat_lower);
2233 HOOK(hook_inline, stat_inline);
2234 HOOK(hook_tail_rec, stat_tail_rec);
2235 HOOK(hook_strength_red, stat_strength_red);
2236 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2237 HOOK(hook_if_conversion, stat_if_conversion);
2238 HOOK(hook_func_call, stat_func_call);
2239 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2240 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2242 obstack_init(&status->cnts);
2243 obstack_init(&status->be_data);
2245 /* create the hash-tables */
2246 status->irg_hash = new_pset(graph_cmp, 8);
2247 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2249 /* create the wait queue */
2250 status->wait_q = new_pdeq();
2252 if (stat_options & FIRMSTAT_COUNT_STRONG_OP) {
2253 /* build the pseudo-ops */
2255 _op_Phi0.code = --num;
2256 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2258 _op_PhiM.code = --num;
2259 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2261 _op_ProjM.code = --num;
2262 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2264 _op_MulC.code = --num;
2265 _op_MulC.name = new_id_from_chars(X("MulC"));
2267 _op_DivC.code = --num;
2268 _op_DivC.name = new_id_from_chars(X("DivC"));
2270 _op_ModC.code = --num;
2271 _op_ModC.name = new_id_from_chars(X("ModC"));
2273 status->op_Phi0 = &_op_Phi0;
2274 status->op_PhiM = &_op_PhiM;
2275 status->op_ProjM = &_op_ProjM;
2276 status->op_MulC = &_op_MulC;
2277 status->op_DivC = &_op_DivC;
2278 status->op_ModC = &_op_ModC;
2280 status->op_Phi0 = NULL;
2281 status->op_PhiM = NULL;
2282 status->op_ProjM = NULL;
2283 status->op_MulC = NULL;
2284 status->op_DivC = NULL;
2285 status->op_ModC = NULL;
2288 /* for Florian: count the Sel depth */
2289 if (stat_options & FIRMSTAT_COUNT_SELS) {
2290 _op_SelSel.code = --num;
2291 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2293 _op_SelSelSel.code = --num;
2294 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2296 status->op_SelSel = &_op_SelSel;
2297 status->op_SelSelSel = &_op_SelSelSel;
2299 status->op_SelSel = NULL;
2300 status->op_SelSelSel = NULL;
2303 /* register the dumper */
2304 stat_register_dumper(&simple_dumper);
2306 if (stat_options & FIRMSTAT_CSV_OUTPUT)
2307 stat_register_dumper(&csv_dumper);
2309 /* initialize the pattern hash */
2310 stat_init_pattern_history(stat_options & FIRMSTAT_PATTERN_ENABLED);
2312 /* initialize the Const options */
2313 if (stat_options & FIRMSTAT_COUNT_CONSTS)
2314 stat_init_const_cnt(status);
2316 /* distribution table for parameter counts */
2317 status->dist_param_cnt = stat_new_int_distrib_tbl();
2319 clear_optimization_counter();
2323 } /* firm_init_stat */
2326 * Frees all dumper structures.
2328 static void stat_term_dumper(void)
2330 dumper_t *dumper, *next_dumper;
2332 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2333 if (dumper->func_map)
2334 del_pset(dumper->func_map);
2336 next_dumper = dumper->next;
2338 dumper = next_dumper;
2340 } /* stat_term_dumper */
2343 /* Terminates the statistics module, frees all memory. */
2344 void stat_term(void)
2346 if (status != (stat_info_t *)&status_disable) {
2347 obstack_free(&status->be_data, NULL);
2348 obstack_free(&status->cnts, NULL);
2353 status = (stat_info_t *)&status_disable;
2357 /* returns 1 if statistics were initialized, 0 otherwise */
2358 int stat_is_active(void)
2360 return status != (stat_info_t *)&status_disable;
2361 } /* stat_is_active */
2363 void init_stat(void)
2365 lc_opt_entry_t *root_grp = firm_opt_get_root();
2366 lc_opt_entry_t *be_grp = lc_opt_get_grp(root_grp, "be");
2368 static const lc_opt_enum_mask_items_t stat_items[] = {
2369 { "enabled", FIRMSTAT_ENABLED },
2370 { "pattern", FIRMSTAT_PATTERN_ENABLED },
2371 { "count_strong_op", FIRMSTAT_COUNT_STRONG_OP },
2372 { "count_dag", FIRMSTAT_COUNT_DAG },
2373 { "count_deleted", FIRMSTAT_COUNT_DELETED },
2374 { "count_sels", FIRMSTAT_COUNT_SELS },
2375 { "count_consts", FIRMSTAT_COUNT_CONSTS },
2376 { "csv_output", FIRMSTAT_CSV_OUTPUT },
2379 static lc_opt_enum_mask_var_t statmask = { &stat_options, stat_items };
2380 static const lc_opt_table_entry_t stat_optionstable[] = {
2381 LC_OPT_ENT_ENUM_MASK("statistics", "enable statistics", &statmask),
2384 lc_opt_add_table(be_grp, stat_optionstable);