2 * Copyright (C) 1995-2007 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
30 #ifdef FIRM_STATISTICS
44 #include "firmstat_t.h"
52 * need this to be static:
53 * Special pseudo Opcodes that we need to count some interesting cases
57 * The Phi0, a node that is created during SSA construction
59 static ir_op _op_Phi0;
61 /** The PhiM, just to count memory Phi's. */
62 static ir_op _op_PhiM;
64 /** The Mul by Const node. */
65 static ir_op _op_MulC;
67 /** The Div by Const node. */
68 static ir_op _op_DivC;
70 /** The Div by Const node. */
71 static ir_op _op_ModC;
73 /** The Div by Const node. */
74 static ir_op _op_DivModC;
76 /** The memory Proj node. */
77 static ir_op _op_ProjM;
80 static ir_op _op_SelSel;
82 /** A Sel of a Sel of a Sel */
83 static ir_op _op_SelSelSel;
85 /* ---------------------------------------------------------------------------------- */
87 /** Marks the begin of a statistic (hook) function. */
88 #define STAT_ENTER ++status->recursive
90 /** Marks the end of a statistic (hook) functions. */
91 #define STAT_LEAVE --status->recursive
93 /** Allows to enter a statistic function only when we are not already in a hook. */
94 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
99 static const int status_disable = 0;
100 static stat_info_t *status = (stat_info_t *)&status_disable;
103 * Compare two elements of the opcode hash.
105 static int opcode_cmp(const void *elt, const void *key) {
106 const node_entry_t *e1 = elt;
107 const node_entry_t *e2 = key;
109 return e1->op->code - e2->op->code;
113 * Compare two elements of the graph hash.
115 static int graph_cmp(const void *elt, const void *key) {
116 const graph_entry_t *e1 = elt;
117 const graph_entry_t *e2 = key;
119 return e1->irg != e2->irg;
123 * Compare two elements of the optimization hash.
125 static int opt_cmp(const void *elt, const void *key) {
126 const opt_entry_t *e1 = elt;
127 const opt_entry_t *e2 = key;
129 return e1->op->code != e2->op->code;
133 * Compare two elements of the block/extbb hash.
135 static int block_cmp(const void *elt, const void *key) {
136 const block_entry_t *e1 = elt;
137 const block_entry_t *e2 = key;
139 return e1->block_nr != e2->block_nr;
143 * Compare two elements of the be_block hash.
145 static int be_block_cmp(const void *elt, const void *key) {
146 const be_block_entry_t *e1 = elt;
147 const be_block_entry_t *e2 = key;
149 return e1->block_nr != e2->block_nr;
153 * Compare two elements of reg pressure hash.
155 static int reg_pressure_cmp(const void *elt, const void *key) {
156 const reg_pressure_entry_t *e1 = elt;
157 const reg_pressure_entry_t *e2 = key;
159 return e1->class_name != e2->class_name;
160 } /* reg_pressure_cmp */
163 * Compare two elements of the perm_stat hash.
165 static int perm_stat_cmp(const void *elt, const void *key) {
166 const perm_stat_entry_t *e1 = elt;
167 const perm_stat_entry_t *e2 = key;
169 return e1->perm != e2->perm;
170 } /* perm_stat_cmp */
173 * Compare two elements of the perm_class hash.
175 static int perm_class_cmp(const void *elt, const void *key) {
176 const perm_class_entry_t *e1 = elt;
177 const perm_class_entry_t *e2 = key;
179 return e1->class_name != e2->class_name;
180 } /* perm_class_cmp */
183 * Compare two elements of the ir_op hash.
185 static int opcode_cmp_2(const void *elt, const void *key) {
186 const ir_op *e1 = elt;
187 const ir_op *e2 = key;
189 return e1->code != e2->code;
193 * Compare two elements of the address_mark set.
195 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
196 const address_mark_entry_t *e1 = elt;
197 const address_mark_entry_t *e2 = key;
200 /* compare only the nodes, the rest is used as data container */
201 return e1->node != e2->node;
202 } /* address_mark_cmp */
205 * Clear all counter in a node_entry_t.
207 static void opcode_clear_entry(node_entry_t *elem) {
208 cnt_clr(&elem->cnt_alive);
209 cnt_clr(&elem->new_node);
210 cnt_clr(&elem->into_Id);
211 } /* opcode_clear_entry */
214 * Returns the associates node_entry_t for an ir_op
216 * @param op the IR operation
217 * @param hmap a hash map containing ir_op* -> node_entry_t*
219 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap) {
225 elem = pset_find(hmap, &key, op->code);
229 elem = obstack_alloc(&status->cnts, sizeof(*elem));
230 memset(elem, 0, sizeof(*elem));
233 opcode_clear_entry(elem);
237 return pset_insert(hmap, elem, op->code);
238 } /* opcode_get_entry */
241 * Returns the associates ir_op for an opcode
243 * @param code the IR opcode
244 * @param hmap the hash map containing opcode -> ir_op*
246 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap) {
250 return pset_find(hmap, &key, code);
251 } /* opcode_find_entry */
254 * Clears all counter in a graph_entry_t.
256 * @param elem the graph entry
257 * @param all if non-zero, clears all counters, else leave accumulated ones
259 static void graph_clear_entry(graph_entry_t *elem, int all) {
262 /* clear accumulated / non-acumulated counter */
263 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
264 cnt_clr(&elem->cnt[i]);
267 if (elem->block_hash) {
268 del_pset(elem->block_hash);
269 elem->block_hash = NULL;
272 if (elem->extbb_hash) {
273 del_pset(elem->extbb_hash);
274 elem->extbb_hash = NULL;
277 obstack_free(&elem->recalc_cnts, NULL);
278 obstack_init(&elem->recalc_cnts);
279 } /* graph_clear_entry */
282 * Returns the associated graph_entry_t for an IR graph.
284 * @param irg the IR graph
285 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
287 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
295 elem = pset_find(hmap, &key, HASH_PTR(irg));
298 /* create hash map backend block information */
299 if (! elem->be_block_hash)
300 elem->be_block_hash = new_pset(be_block_cmp, 5);
305 /* allocate a new one */
306 elem = obstack_alloc(&status->cnts, sizeof(*elem));
307 memset(elem, 0, sizeof(*elem));
308 obstack_init(&elem->recalc_cnts);
311 graph_clear_entry(elem, 1);
313 /* new hash table for opcodes here */
314 elem->opcode_hash = new_pset(opcode_cmp, 5);
315 elem->address_mark = new_set(address_mark_cmp, 5);
318 /* these hash tables are created on demand */
319 elem->block_hash = NULL;
320 elem->extbb_hash = NULL;
322 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
323 elem->opt_hash[i] = new_pset(opt_cmp, 4);
325 return pset_insert(hmap, elem, HASH_PTR(irg));
326 } /* graph_get_entry */
329 * Clear all counter in an opt_entry_t.
331 static void opt_clear_entry(opt_entry_t *elem) {
332 cnt_clr(&elem->count);
333 } /* opt_clear_entry */
336 * Returns the associated opt_entry_t for an IR operation.
338 * @param op the IR operation
339 * @param hmap the hash map containing ir_op* -> opt_entry_t*
341 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
348 elem = pset_find(hmap, &key, op->code);
352 elem = obstack_alloc(&status->cnts, sizeof(*elem));
353 memset(elem, 0, sizeof(*elem));
355 /* clear new counter */
356 opt_clear_entry(elem);
360 return pset_insert(hmap, elem, op->code);
361 } /* opt_get_entry */
364 * clears all counter in a block_entry_t
366 static void block_clear_entry(block_entry_t *elem) {
369 for (i = 0; i < _bcnt_last; ++i)
370 cnt_clr(&elem->cnt[i]);
371 } /* block_clear_entry */
374 * Returns the associated block_entry_t for an block.
376 * @param block_nr an IR block number
377 * @param hmap a hash map containing long -> block_entry_t
379 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
384 key.block_nr = block_nr;
386 elem = pset_find(hmap, &key, block_nr);
390 elem = obstack_alloc(obst, sizeof(*elem));
391 memset(elem, 0, sizeof(*elem));
393 /* clear new counter */
394 block_clear_entry(elem);
396 elem->block_nr = block_nr;
398 return pset_insert(hmap, elem, block_nr);
399 } /* block_get_entry */
402 * Clear all sets in be_block_entry_t.
404 static void be_block_clear_entry(be_block_entry_t *elem)
406 if (elem->reg_pressure)
407 del_pset(elem->reg_pressure);
409 if (elem->sched_ready)
410 stat_delete_distrib_tbl(elem->sched_ready);
412 if (elem->perm_class_stat)
413 del_pset(elem->perm_class_stat);
415 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
416 elem->sched_ready = stat_new_int_distrib_tbl();
417 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
418 } /* be_block_clear_entry */
421 * Returns the associated be_block_entry_t for an block.
423 * @param block_nr an IR block number
424 * @param hmap a hash map containing long -> be_block_entry_t
426 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
428 be_block_entry_t key;
429 be_block_entry_t *elem;
431 key.block_nr = block_nr;
433 elem = pset_find(hmap, &key, block_nr);
437 elem = obstack_alloc(obst, sizeof(*elem));
438 memset(elem, 0, sizeof(*elem));
440 /* clear new counter */
441 be_block_clear_entry(elem);
443 elem->block_nr = block_nr;
445 return pset_insert(hmap, elem, block_nr);
446 } /* be_block_get_entry */
449 * clears all sets in perm_class_entry_t
451 static void perm_class_clear_entry(perm_class_entry_t *elem) {
453 del_pset(elem->perm_stat);
455 elem->perm_stat = new_pset(perm_stat_cmp, 5);
456 } /* perm_class_clear_entry */
459 * Returns the associated perm_class entry for a register class.
461 * @param class_name the register class name
462 * @param hmap a hash map containing class_name -> perm_class_entry_t
464 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
465 hmap_perm_class_entry_t *hmap)
467 perm_class_entry_t key;
468 perm_class_entry_t *elem;
470 key.class_name = class_name;
472 elem = pset_find(hmap, &key, HASH_PTR(class_name));
476 elem = obstack_alloc(obst, sizeof(*elem));
477 memset(elem, 0, sizeof(*elem));
479 /* clear new counter */
480 perm_class_clear_entry(elem);
482 elem->class_name = class_name;
484 return pset_insert(hmap, elem, HASH_PTR(class_name));
485 } /* perm_class_get_entry */
488 * clears all sets in perm_stat_entry_t
490 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
492 stat_delete_distrib_tbl(elem->chains);
495 stat_delete_distrib_tbl(elem->cycles);
497 elem->chains = stat_new_int_distrib_tbl();
498 elem->cycles = stat_new_int_distrib_tbl();
499 } /* perm_stat_clear_entry */
502 * Returns the associated perm_stat entry for a perm.
504 * @param perm the perm node
505 * @param hmap a hash map containing perm -> perm_stat_entry_t
507 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
509 perm_stat_entry_t key;
510 perm_stat_entry_t *elem;
514 elem = pset_find(hmap, &key, HASH_PTR(perm));
518 elem = obstack_alloc(obst, sizeof(*elem));
519 memset(elem, 0, sizeof(*elem));
521 /* clear new counter */
522 perm_stat_clear_entry(elem);
526 return pset_insert(hmap, elem, HASH_PTR(perm));
527 } /* perm_stat_get_entry */
530 * Returns the ir_op for an IR-node,
531 * handles special cases and return pseudo op codes.
533 * @param none an IR node
535 static ir_op *stat_get_irn_op(ir_node *node)
537 ir_op *op = get_irn_op(node);
538 ir_opcode opc = op->code;
542 if (get_irn_arity(node) == 0) {
543 /* special case, a Phi0 node, count on extra counter */
544 op = status->op_Phi0 ? status->op_Phi0 : op;
545 } else if (get_irn_mode(node) == mode_M) {
546 /* special case, a Memory Phi node, count on extra counter */
547 op = status->op_PhiM ? status->op_PhiM : op;
551 if (get_irn_mode(node) == mode_M) {
552 /* special case, a Memory Proj node, count on extra counter */
553 op = status->op_ProjM ? status->op_ProjM : op;
557 if (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const) {
558 /* special case, a Multiply by a const, count on extra counter */
559 op = status->op_MulC ? status->op_MulC : op;
563 if (get_irn_op(get_Div_right(node)) == op_Const) {
564 /* special case, a division by a const, count on extra counter */
565 op = status->op_DivC ? status->op_DivC : op;
569 if (get_irn_op(get_Mod_right(node)) == op_Const) {
570 /* special case, a module by a const, count on extra counter */
571 op = status->op_ModC ? status->op_ModC : op;
575 if (get_irn_op(get_DivMod_right(node)) == op_Const) {
576 /* special case, a division/modulo by a const, count on extra counter */
577 op = status->op_DivModC ? status->op_DivModC : op;
581 if (get_irn_op(get_Sel_ptr(node)) == op_Sel) {
582 /* special case, a Sel of a Sel, count on extra counter */
583 op = status->op_SelSel ? status->op_SelSel : op;
584 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
585 /* special case, a Sel of a Sel of a Sel, count on extra counter */
586 op = status->op_SelSelSel ? status->op_SelSelSel : op;
595 } /* stat_get_irn_op */
598 * update the block counter
600 static void undate_block_info(ir_node *node, graph_entry_t *graph)
602 ir_op *op = get_irn_op(node);
604 block_entry_t *b_entry;
607 /* check for block */
608 if (op == op_Block) {
609 arity = get_irn_arity(node);
610 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
612 /* count all incoming edges */
613 for (i = 0; i < arity; ++i) {
614 ir_node *pred = get_irn_n(node, i);
615 ir_node *other_block = get_nodes_block(pred);
616 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
618 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
619 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
624 block = get_nodes_block(node);
625 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
627 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
628 /* count data Phi per block */
629 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
632 /* we have a new node in our block */
633 cnt_inc(&b_entry->cnt[bcnt_nodes]);
635 /* don't count keep-alive edges */
636 if (get_irn_op(node) == op_End)
639 arity = get_irn_arity(node);
641 for (i = 0; i < arity; ++i) {
642 ir_node *pred = get_irn_n(node, i);
643 ir_node *other_block;
645 other_block = get_nodes_block(pred);
647 if (other_block == block)
648 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
650 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
652 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
653 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
656 } /* undate_block_info */
659 * Update the extended block counter.
661 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
663 ir_op *op = get_irn_op(node);
665 extbb_entry_t *eb_entry;
668 /* check for block */
669 if (op == op_Block) {
670 extbb = get_nodes_extbb(node);
671 arity = get_irn_arity(node);
672 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
674 /* count all incoming edges */
675 for (i = 0; i < arity; ++i) {
676 ir_node *pred = get_irn_n(node, i);
677 ir_extblk *other_extbb = get_nodes_extbb(pred);
679 if (extbb != other_extbb) {
680 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
682 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
683 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
689 extbb = get_nodes_extbb(node);
690 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
692 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
693 /* count data Phi per extbb */
694 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
697 /* we have a new node in our block */
698 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
700 /* don't count keep-alive edges */
701 if (get_irn_op(node) == op_End)
704 arity = get_irn_arity(node);
706 for (i = 0; i < arity; ++i) {
707 ir_node *pred = get_irn_n(node, i);
708 ir_extblk *other_extbb = get_nodes_extbb(pred);
710 if (other_extbb == extbb)
711 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
713 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
715 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
716 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
719 } /* update_extbb_info */
722 * Calculates how many arguments of the call are const, updates
723 * param distribution.
725 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call) {
726 int i, num_const_args = 0, num_local_adr = 0;
727 int n = get_Call_n_params(call);
729 for (i = 0; i < n; ++i) {
730 ir_node *param = get_Call_param(call, i);
732 if (is_irn_constlike(param))
734 else if (is_Sel(param)) {
735 ir_node *base = param;
738 base = get_Sel_ptr(base);
739 } while (is_Sel(base));
741 if (base == get_irg_frame(current_ir_graph))
747 if (num_const_args > 0)
748 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
749 if (num_const_args == n)
750 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
751 if (num_local_adr > 0)
752 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
754 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
755 } /* analyse_params_of_Call */
758 * Update info on calls.
760 * @param call The call
761 * @param graph The graph entry containing the call
763 static void stat_update_call(ir_node *call, graph_entry_t *graph)
765 ir_node *block = get_nodes_block(call);
766 ir_node *ptr = get_Call_ptr(call);
767 ir_entity *ent = NULL;
768 ir_graph *callee = NULL;
771 * If the block is bad, the whole subgraph will collapse later
772 * so do not count this call.
773 * This happens in dead code.
778 cnt_inc(&graph->cnt[gcnt_all_calls]);
780 /* found a call, this function is not a leaf */
783 if (get_irn_op(ptr) == op_SymConst) {
784 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
785 /* ok, we seems to know the entity */
786 ent = get_SymConst_entity(ptr);
787 callee = get_entity_irg(ent);
789 /* it is recursive, if it calls at least once */
790 if (callee == graph->irg)
791 graph->is_recursive = 1;
794 /* indirect call, be could not predict */
795 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
797 /* NOT a leaf call */
798 graph->is_leaf_call = LCS_NON_LEAF_CALL;
801 /* check, if it's a chain-call: Then, the call-block
802 * must dominate the end block. */
804 ir_node *curr = get_irg_end_block(graph->irg);
805 int depth = get_Block_dom_depth(block);
807 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
808 curr = get_Block_idom(curr);
810 if (! curr || is_no_Block(curr))
815 graph->is_chain_call = 0;
818 /* check, if the callee is a leaf */
820 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
822 if (called->is_analyzed) {
823 if (! called->is_leaf)
824 graph->is_leaf_call = LCS_NON_LEAF_CALL;
828 analyse_params_of_Call(graph, call);
829 } /* stat_update_call */
832 * Update info on calls for graphs on the wait queue.
834 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
836 ir_node *block = get_nodes_block(call);
837 ir_node *ptr = get_Call_ptr(call);
838 ir_entity *ent = NULL;
839 ir_graph *callee = NULL;
842 * If the block is bad, the whole subgraph will collapse later
843 * so do not count this call.
844 * This happens in dead code.
849 if (get_irn_op(ptr) == op_SymConst) {
850 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
851 /* ok, we seems to know the entity */
852 ent = get_SymConst_entity(ptr);
853 callee = get_entity_irg(ent);
857 /* check, if the callee is a leaf */
859 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
861 assert(called->is_analyzed);
863 if (! called->is_leaf)
864 graph->is_leaf_call = LCS_NON_LEAF_CALL;
866 graph->is_leaf_call = LCS_NON_LEAF_CALL;
867 } /* stat_update_call_2 */
870 * Find the base address and entity of an Sel node.
872 * @param sel the node
874 * @return the base address.
876 static ir_node *find_base_adr(ir_node *sel) {
877 ir_node *ptr = get_Sel_ptr(sel);
879 while (is_Sel(ptr)) {
881 ptr = get_Sel_ptr(sel);
884 } /* find_base_adr */
887 * Update info on Load/Store address statistics.
889 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
890 ir_opcode opc = get_irn_opcode(node);
896 /* a global address */
897 cnt_inc(&graph->cnt[gcnt_global_adr]);
900 base = find_base_adr(node);
901 irg = current_ir_graph;
902 if (base == get_irg_tls(irg)) {
903 /* a TLS variable, like a global. */
904 cnt_inc(&graph->cnt[gcnt_global_adr]);
905 } else if (base == get_irg_frame(irg)) {
906 /* a local Variable. */
907 cnt_inc(&graph->cnt[gcnt_local_adr]);
910 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
911 /* pointer access through parameter, check for THIS */
912 ir_entity *ent = get_irg_entity(irg);
915 ir_type *ent_tp = get_entity_type(ent);
917 if (get_method_calling_convention(ent_tp) & cc_this_call) {
918 if (get_Proj_proj(base) == 0) {
920 cnt_inc(&graph->cnt[gcnt_this_adr]);
925 /* other parameter */
926 cnt_inc(&graph->cnt[gcnt_param_adr]);
929 /* unknown Pointer access */
930 cnt_inc(&graph->cnt[gcnt_other_adr]);
936 } /* stat_update_address */
939 * Walker for reachable nodes count.
941 static void update_node_stat(ir_node *node, void *env)
943 graph_entry_t *graph = env;
946 ir_op *op = stat_get_irn_op(node);
947 int arity = get_irn_arity(node);
949 entry = opcode_get_entry(op, graph->opcode_hash);
951 cnt_inc(&entry->cnt_alive);
952 cnt_add_i(&graph->cnt[gcnt_edges], arity);
954 /* count block edges */
955 undate_block_info(node, graph);
957 /* count extended block edges */
958 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
959 if (graph->irg != get_const_code_irg())
960 update_extbb_info(node, graph);
963 /* handle statistics for special node types */
967 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
968 /* check properties of constants */
969 stat_update_const(status, node, graph);
973 /* check for properties that depends on calls like recursion/leaf/indirect call */
974 stat_update_call(node, graph);
977 /* check address properties */
978 stat_update_address(get_Load_ptr(node), graph);
981 /* check address properties */
982 stat_update_address(get_Store_ptr(node), graph);
987 } /* update_node_stat */
990 * Walker for reachable nodes count for graphs on the wait_q.
992 static void update_node_stat_2(ir_node *node, void *env) {
993 graph_entry_t *graph = env;
995 /* check for properties that depends on calls like recursion/leaf/indirect call */
997 stat_update_call_2(node, graph);
998 } /* update_node_stat_2 */
1001 * Get the current address mark.
1003 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
1004 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1006 return value ? value->mark : 0;
1007 } /* get_adr_mark */
1010 * Set the current address mark.
1012 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
1013 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1016 } /* set_adr_mark */
1018 #undef DUMP_ADR_MODE
1020 #ifdef DUMP_ADR_MODE
1022 * a vcg attribute hook: Color a node with a different color if
1023 * it's identified as a part of an address expression or at least referenced
1024 * by an address expression.
1026 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1028 ir_node *n = local ? local : node;
1029 ir_graph *irg = get_irn_irg(n);
1030 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1031 unsigned mark = get_adr_mark(graph, n);
1033 if (mark & MARK_ADDRESS_CALC)
1034 fprintf(F, "color: purple");
1035 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1036 fprintf(F, "color: pink");
1037 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1038 fprintf(F, "color: lightblue");
1042 /* I know the color! */
1044 } /* stat_adr_mark_hook */
1045 #endif /* DUMP_ADR_MODE */
1048 * Return the "operational" mode of a Firm node.
1050 static ir_mode *get_irn_op_mode(ir_node *node) {
1051 switch (get_irn_opcode(node)) {
1053 return get_Load_mode(node);
1055 return get_irn_mode(get_Store_value(node));
1057 return get_irn_mode(get_DivMod_left(node));
1059 return get_irn_mode(get_Div_left(node));
1061 return get_irn_mode(get_Mod_left(node));
1063 /* Cmp is no address calculation, or is it? */
1065 return get_irn_mode(node);
1067 } /* get_irn_op_mode */
1070 * Post-walker that marks every node that is an address calculation.
1072 * Users of a node must be visited first. We ensure this by
1073 * calling it in the post of an outs walk. This should work even in cycles,
1074 * while the normal pre-walk will not.
1076 static void mark_address_calc(ir_node *node, void *env) {
1077 graph_entry_t *graph = env;
1078 ir_mode *mode = get_irn_op_mode(node);
1080 unsigned mark_preds = MARK_REF_NON_ADR;
1082 if (! mode_is_data(mode))
1085 if (mode_is_reference(mode)) {
1086 /* a reference is calculated here, we are sure */
1087 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1089 mark_preds = MARK_REF_ADR;
1091 unsigned mark = get_adr_mark(graph, node);
1093 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1095 * this node has no reference mode, but is only
1096 * referenced by address calculations
1098 mark_preds = MARK_REF_ADR;
1102 /* mark all predecessors */
1103 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1104 ir_node *pred = get_irn_n(node, i);
1106 mode = get_irn_op_mode(pred);
1107 if (! mode_is_data(mode))
1110 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1112 } /* mark_address_calc */
1115 * Post-walker that marks every node that is an address calculation.
1117 * Users of a node must be visited first. We ensure this by
1118 * calling it in the post of an outs walk. This should work even in cycles,
1119 * while the normal pre-walk will not.
1121 static void count_adr_ops(ir_node *node, void *env) {
1122 graph_entry_t *graph = env;
1123 unsigned mark = get_adr_mark(graph, node);
1125 if (mark & MARK_ADDRESS_CALC)
1126 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1127 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1128 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1129 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1130 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1131 } /* count_adr_ops */
1134 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1135 * is called, must recalculate all statistic info.
1137 * @param global The global entry
1138 * @param graph The current entry
1140 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1142 node_entry_t *entry;
1145 /* clear first the alive counter in the graph */
1146 foreach_pset(graph->opcode_hash, entry) {
1147 cnt_clr(&entry->cnt_alive);
1148 } /* foreach_pset */
1150 /* set pessimistic values */
1152 graph->is_leaf_call = LCS_UNKNOWN;
1153 graph->is_recursive = 0;
1154 graph->is_chain_call = 1;
1156 /* create new block counter */
1157 graph->block_hash = new_pset(block_cmp, 5);
1159 /* we need dominator info */
1160 if (graph->irg != get_const_code_irg()) {
1161 assure_doms(graph->irg);
1163 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1164 /* we need extended basic blocks */
1165 compute_extbb(graph->irg);
1167 /* create new extbb counter */
1168 graph->extbb_hash = new_pset(block_cmp, 5);
1172 /* count the nodes in the graph */
1173 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1176 /* Uncomment this code if chain-call means call exact one. */
1177 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1179 /* check if we have more than 1 call */
1180 if (cnt_gt(entry->cnt_alive, 1))
1181 graph->is_chain_call = 0;
1184 /* recursive functions are never chain calls, leafs don't have calls */
1185 if (graph->is_recursive || graph->is_leaf)
1186 graph->is_chain_call = 0;
1188 /* assume we walk every graph only ONCE, we could sum here the global count */
1189 foreach_pset(graph->opcode_hash, entry) {
1190 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1192 /* update the node counter */
1193 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1194 } /* foreach_pset */
1196 /* count the number of address calculation */
1197 if (graph->irg != get_const_code_irg()) {
1198 ir_graph *rem = current_ir_graph;
1200 assure_irg_outs(graph->irg);
1202 /* Must be done an the outs graph */
1203 current_ir_graph = graph->irg;
1204 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1205 current_ir_graph = rem;
1207 #ifdef DUMP_ADR_MODE
1208 /* register the vcg hook and dump the graph for test */
1209 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1210 dump_ir_block_graph(graph->irg, "-adr");
1211 set_dump_node_vcgattr_hook(NULL);
1212 #endif /* DUMP_ADR_MODE */
1214 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1217 /* count the DAG's */
1218 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1219 count_dags_in_graph(global, graph);
1221 /* calculate the patterns of this graph */
1222 stat_calc_pattern_history(graph->irg);
1224 /* leaf function did not call others */
1226 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1227 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1228 /* we still don't know if this graph calls leaf-functions, so enqueue */
1229 pdeq_putl(status->wait_q, graph);
1232 /* we have analyzed this graph */
1233 graph->is_analyzed = 1;
1235 /* accumulate all counter's */
1236 for (i = 0; i < _gcnt_last; ++i)
1237 cnt_add(&global->cnt[i], &graph->cnt[i]);
1238 } /* update_graph_stat */
1241 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1242 * must finish all statistic info calculations.
1244 * @param global The global entry
1245 * @param graph The current entry
1247 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1250 if (graph->is_deleted) {
1251 /* deleted, ignore */
1256 /* count the nodes in the graph */
1257 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1259 if (graph->is_leaf_call == LCS_UNKNOWN)
1260 graph->is_leaf_call = LCS_LEAF_CALL;
1262 } /* update_graph_stat_2 */
1265 * Register a dumper.
1267 static void stat_register_dumper(const dumper_t *dumper) {
1268 dumper_t *p = xmalloc(sizeof(*p));
1271 memcpy(p, dumper, sizeof(*p));
1273 p->next = status->dumper;
1278 /* FIXME: memory leak */
1279 } /* stat_register_dumper */
1282 * Dumps the statistics of an IR graph.
1284 static void stat_dump_graph(graph_entry_t *entry) {
1287 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1288 if (dumper->dump_graph)
1289 dumper->dump_graph(dumper, entry);
1291 } /* stat_dump_graph */
1294 * Calls all registered dumper functions.
1296 static void stat_dump_registered(graph_entry_t *entry) {
1299 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1300 if (dumper->func_map) {
1301 dump_graph_FUNC func;
1303 foreach_pset(dumper->func_map, func)
1304 func(dumper, entry);
1307 } /* stat_dump_registered */
1310 * Dumps a constant table.
1312 static void stat_dump_consts(const constant_info_t *tbl) {
1315 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1316 if (dumper->dump_const_tbl)
1317 dumper->dump_const_tbl(dumper, tbl);
1319 } /* stat_dump_consts */
1322 * Dumps the parameter distribution
1324 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1327 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1328 if (dumper->dump_const_tbl)
1329 dumper->dump_param_tbl(dumper, tbl, global);
1334 * Initialize the dumper.
1336 static void stat_dump_init(const char *name) {
1339 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1341 dumper->init(dumper, name);
1343 } /* stat_dump_init */
1346 * Finish the dumper.
1348 static void stat_dump_finish(void) {
1351 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1353 dumper->finish(dumper);
1355 } /* stat_dump_finish */
1358 * Register an additional function for all dumper.
1360 void stat_register_dumper_func(dump_graph_FUNC func) {
1363 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1364 if (! dumper->func_map)
1365 dumper->func_map = pset_new_ptr(3);
1366 pset_insert_ptr(dumper->func_map, func);
1368 } /* stat_register_dumper_func */
1370 /* ---------------------------------------------------------------------- */
1373 * Helper: get an ir_op from an opcode.
1375 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1376 return opcode_find_entry(code, status->ir_op_hash);
1377 } /* stat_get_op_from_opcode */
1380 * Hook: A new IR op is registered.
1382 * @param ctx the hook context
1383 * @param op the new IR opcode that was created.
1385 static void stat_new_ir_op(void *ctx, ir_op *op) {
1387 if (! status->stat_options)
1392 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1394 /* execute for side effect :-) */
1395 (void)opcode_get_entry(op, graph->opcode_hash);
1397 pset_insert(status->ir_op_hash, op, op->code);
1400 } /* stat_new_ir_op */
1403 * Hook: An IR op is freed.
1405 * @param ctx the hook context
1406 * @param op the IR opcode that is freed
1408 static void stat_free_ir_op(void *ctx, ir_op *op) {
1411 if (! status->stat_options)
1418 } /* stat_free_ir_op */
1421 * Hook: A new node is created.
1423 * @param ctx the hook context
1424 * @param irg the IR graph on which the node is created
1425 * @param node the new IR node that was created
1427 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1430 if (! status->stat_options)
1433 /* do NOT count during dead node elimination */
1434 if (status->in_dead_node_elim)
1439 node_entry_t *entry;
1440 graph_entry_t *graph;
1441 ir_op *op = stat_get_irn_op(node);
1443 /* increase global value */
1444 graph = graph_get_entry(NULL, status->irg_hash);
1445 entry = opcode_get_entry(op, graph->opcode_hash);
1446 cnt_inc(&entry->new_node);
1448 /* increase local value */
1449 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1450 entry = opcode_get_entry(op, graph->opcode_hash);
1451 cnt_inc(&entry->new_node);
1454 } /* stat_new_node */
1457 * Hook: A node is changed into a Id node
1459 * @param ctx the hook context
1460 * @param node the IR node that will be turned into an ID
1462 static void stat_turn_into_id(void *ctx, ir_node *node) {
1464 if (! status->stat_options)
1469 node_entry_t *entry;
1470 graph_entry_t *graph;
1471 ir_op *op = stat_get_irn_op(node);
1473 /* increase global value */
1474 graph = graph_get_entry(NULL, status->irg_hash);
1475 entry = opcode_get_entry(op, graph->opcode_hash);
1476 cnt_inc(&entry->into_Id);
1478 /* increase local value */
1479 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1480 entry = opcode_get_entry(op, graph->opcode_hash);
1481 cnt_inc(&entry->into_Id);
1484 } /* stat_turn_into_id */
1487 * Hook: A new graph was created
1489 * @param ctx the hook context
1490 * @param irg the new IR graph that was created
1491 * @param ent the entity of this graph
1493 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1495 if (! status->stat_options)
1500 /* execute for side effect :-) */
1501 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1504 graph->is_deleted = 0;
1506 graph->is_leaf_call = 0;
1507 graph->is_recursive = 0;
1508 graph->is_chain_call = 0;
1509 graph->is_analyzed = 0;
1512 } /* stat_new_graph */
1515 * Hook: A graph will be deleted
1517 * @param ctx the hook context
1518 * @param irg the IR graph that will be deleted
1520 * Note that we still hold the information for this graph
1521 * in our hash maps, only a flag is set which prevents this
1522 * information from being changed, it's "frozen" from now.
1524 static void stat_free_graph(void *ctx, ir_graph *irg) {
1526 if (! status->stat_options)
1531 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1532 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1534 graph->is_deleted = 1;
1536 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1537 /* count the nodes of the graph yet, it will be destroyed later */
1538 update_graph_stat(global, graph);
1542 } /* stat_free_graph */
1545 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1547 * @param ctx the hook context
1548 * @param irg the IR graph that will be walked
1549 * @param pre the pre walker
1550 * @param post the post walker
1552 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1557 if (! status->stat_options)
1562 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1564 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1567 } /* stat_irg_walk */
1570 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1572 * @param ctx the hook context
1573 * @param irg the IR graph that will be walked
1574 * @param pre the pre walker
1575 * @param post the post walker
1577 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1579 /* for now, do NOT differentiate between blockwise and normal */
1580 stat_irg_walk(ctx, irg, pre, post);
1581 } /* stat_irg_walk_blkwise */
1584 * Hook: A walk over the graph's blocks 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 node the IR node
1589 * @param pre the pre walker
1590 * @param post the post walker
1592 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1598 if (! status->stat_options)
1603 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1605 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1608 } /* stat_irg_block_walk */
1611 * Called for every node that is removed due to an optimization.
1613 * @param n the IR node that will be removed
1614 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1616 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap) {
1617 ir_op *op = stat_get_irn_op(n);
1618 opt_entry_t *entry = opt_get_entry(op, hmap);
1620 /* increase global value */
1621 cnt_inc(&entry->count);
1622 } /* removed_due_opt */
1625 * Hook: Some nodes were optimized into some others due to an optimization.
1627 * @param ctx the hook context
1629 static void stat_merge_nodes(
1631 ir_node **new_node_array, int new_num_entries,
1632 ir_node **old_node_array, int old_num_entries,
1636 if (! status->stat_options)
1642 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1644 if (status->reassoc_run)
1645 opt = HOOK_OPT_REASSOC;
1647 for (i = 0; i < old_num_entries; ++i) {
1648 /* nodes might be in new and old, so if we found a node
1649 in both sets, this one is NOT removed */
1650 for (j = 0; j < new_num_entries; ++j) {
1651 if (old_node_array[i] == new_node_array[j])
1654 if (j >= new_num_entries) {
1657 /* sometimes we did not detect, that it is replaced by a Const */
1658 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1659 ir_op *op = get_irn_op(new_node_array[0]);
1661 if (op == op_Const || op == op_SymConst)
1662 xopt = HOOK_OPT_CONFIRM_C;
1665 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1670 } /* stat_merge_nodes */
1673 * Hook: Reassociation is started/stopped.
1675 * @param ctx the hook context
1676 * @param flag if non-zero, reassociation is started else stopped
1678 static void stat_reassociate(void *ctx, int flag) {
1680 if (! status->stat_options)
1685 status->reassoc_run = flag;
1688 } /* stat_reassociate */
1691 * Hook: A node was lowered into other nodes
1693 * @param ctx the hook context
1694 * @param node the IR node that will be lowered
1696 static void stat_lower(void *ctx, ir_node *node) {
1698 if (! status->stat_options)
1703 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1705 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1711 * Hook: A graph was inlined.
1713 * @param ctx the hook context
1714 * @param call the IR call that will re changed into the body of
1715 * the called IR graph
1716 * @param called_irg the IR graph representing the called routine
1718 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1721 if (! status->stat_options)
1726 ir_graph *irg = get_irn_irg(call);
1727 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1728 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1730 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1731 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1737 * Hook: A graph with tail-recursions was optimized.
1739 * @param ctx the hook context
1741 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1743 if (! status->stat_options)
1748 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1750 graph->num_tail_recursion += n_calls;
1753 } /* stat_tail_rec */
1756 * Strength reduction was performed on an iteration variable.
1758 * @param ctx the hook context
1760 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1762 if (! status->stat_options)
1767 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1768 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1770 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1773 } /* stat_strength_red */
1776 * Hook: Start/Stop the dead node elimination.
1778 * @param ctx the hook context
1780 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1783 if (! status->stat_options)
1786 status->in_dead_node_elim = (start != 0);
1787 } /* stat_dead_node_elim */
1790 * Hook: if-conversion was tried.
1792 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1793 int pos, ir_node *mux, if_result_t reason)
1799 if (! status->stat_options)
1804 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1806 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1809 } /* stat_if_conversion */
1812 * Hook: real function call was optimized.
1814 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1818 if (! status->stat_options)
1823 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1825 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1828 } /* stat_func_call */
1831 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1833 * @param ctx the hook context
1835 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1837 if (! status->stat_options)
1842 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1843 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1846 } /* stat_arch_dep_replace_mul_with_shifts */
1849 * Hook: A division by const was replaced.
1851 * @param ctx the hook context
1852 * @param node the division node that will be optimized
1854 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1856 if (! status->stat_options)
1861 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1862 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1865 } /* stat_arch_dep_replace_division_by_const */
1868 * Update the register pressure of a block.
1870 * @param irg the irg containing the block
1871 * @param block the block for which the reg pressure should be set
1872 * @param pressure the pressure
1873 * @param class_name the name of the register class
1875 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1877 if (! status->stat_options)
1882 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1883 be_block_entry_t *block_ent;
1884 reg_pressure_entry_t *rp_ent;
1886 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1887 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1888 memset(rp_ent, 0, sizeof(*rp_ent));
1890 rp_ent->class_name = class_name;
1891 rp_ent->pressure = pressure;
1893 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1896 } /* stat_be_block_regpressure */
1899 * Update the distribution of ready nodes of a block
1901 * @param irg the irg containing the block
1902 * @param block the block for which the reg pressure should be set
1903 * @param num_ready the number of ready nodes
1905 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1907 if (! status->stat_options)
1912 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1913 be_block_entry_t *block_ent;
1915 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1917 /* increase the counter of corresponding number of ready nodes */
1918 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1921 } /* stat_be_block_sched_ready */
1924 * Update the permutation statistic of a block.
1926 * @param class_name the name of the register class
1927 * @param n_regs number of registers in the register class
1928 * @param perm the perm node
1929 * @param block the block containing the perm
1930 * @param size the size of the perm
1931 * @param real_size number of pairs with different registers
1933 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1934 int size, int real_size)
1936 if (! status->stat_options)
1941 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1942 be_block_entry_t *block_ent;
1943 perm_class_entry_t *pc_ent;
1944 perm_stat_entry_t *ps_ent;
1946 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1947 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1948 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1950 pc_ent->n_regs = n_regs;
1952 /* update information */
1953 ps_ent->size = size;
1954 ps_ent->real_size = real_size;
1957 } /* stat_be_block_stat_perm */
1960 * Update the permutation statistic of a single perm.
1962 * @param class_name the name of the register class
1963 * @param perm the perm node
1964 * @param block the block containing the perm
1965 * @param is_chain 1 if chain, 0 if cycle
1966 * @param size length of the cycle/chain
1967 * @param n_ops the number of ops representing this cycle/chain after lowering
1969 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1970 int is_chain, int size, int n_ops)
1972 if (! status->stat_options)
1977 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1978 be_block_entry_t *block_ent;
1979 perm_class_entry_t *pc_ent;
1980 perm_stat_entry_t *ps_ent;
1982 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1983 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1984 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1987 ps_ent->n_copies += n_ops;
1988 stat_inc_int_distrib_tbl(ps_ent->chains, size);
1990 ps_ent->n_exchg += n_ops;
1991 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
1995 } /* stat_be_block_stat_permcycle */
1997 /* Dumps a statistics snapshot. */
1998 void stat_dump_snapshot(const char *name, const char *phase)
2004 if (! status->stat_options)
2009 graph_entry_t *entry;
2010 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2013 * The constant counter is only global, so we clear it here.
2014 * Note that it does NOT contain the constants in DELETED
2015 * graphs due to this.
2017 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2018 stat_const_clear(status);
2020 /* build the name */
2021 p = strrchr(name, '/');
2026 q = strrchr(name, '\\');
2028 /* NULL might be not the smallest pointer */
2029 if (q && (!p || q > p))
2037 if (l > (int) (sizeof(fname) - 1))
2038 l = sizeof(fname) - 1;
2040 memcpy(fname, name, l);
2046 strncat(fname, "firmstat-", sizeof(fname));
2047 strncat(fname, phase, sizeof(fname));
2048 strncat(fname, "-", sizeof(fname));
2049 strncat(fname, p, sizeof(fname));
2051 stat_dump_init(fname);
2053 /* calculate the graph statistics */
2054 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2055 if (entry->irg == NULL) {
2056 /* special entry for the global count */
2059 if (! entry->is_deleted) {
2060 /* the graph is still alive, count the nodes on it */
2061 update_graph_stat(global, entry);
2065 /* some calculations are dependent, we pushed them on the wait_q */
2066 while (! pdeq_empty(status->wait_q)) {
2067 entry = pdeq_getr(status->wait_q);
2069 update_graph_stat_2(global, entry);
2072 /* dump per graph */
2073 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2074 if (entry->irg == NULL) {
2075 /* special entry for the global count */
2079 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2080 stat_dump_graph(entry);
2081 stat_dump_registered(entry);
2084 if (! entry->is_deleted) {
2085 /* clear the counter that are not accumulated */
2086 graph_clear_entry(entry, 0);
2091 stat_dump_graph(global);
2093 /* dump the const info */
2094 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2095 stat_dump_consts(&status->const_info);
2097 /* dump the parameter distribution */
2098 stat_dump_param_tbl(status->dist_param_cnt, global);
2102 stat_finish_pattern_history(fname);
2104 /* clear the global counter here */
2106 node_entry_t *entry;
2108 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2109 opcode_clear_entry(entry);
2111 /* clear all global counter */
2112 graph_clear_entry(global, 1);
2116 } /* stat_dump_snapshot */
2118 /** the hook entries for the Firm statistics module */
2119 static hook_entry_t stat_hooks[hook_last];
2121 /* initialize the statistics module. */
2122 void firm_init_stat(unsigned enable_options)
2124 #define X(a) a, sizeof(a)-1
2125 #define HOOK(h, fkt) \
2126 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2129 if (! (enable_options & FIRMSTAT_ENABLED))
2132 status = xmalloc(sizeof(*status));
2133 memset(status, 0, sizeof(*status));
2135 /* enable statistics */
2136 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2138 /* register all hooks */
2139 HOOK(hook_new_ir_op, stat_new_ir_op);
2140 HOOK(hook_free_ir_op, stat_free_ir_op);
2141 HOOK(hook_new_node, stat_new_node);
2142 HOOK(hook_turn_into_id, stat_turn_into_id);
2143 HOOK(hook_new_graph, stat_new_graph);
2144 HOOK(hook_free_graph, stat_free_graph);
2145 HOOK(hook_irg_walk, stat_irg_walk);
2146 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2147 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2148 HOOK(hook_merge_nodes, stat_merge_nodes);
2149 HOOK(hook_reassociate, stat_reassociate);
2150 HOOK(hook_lower, stat_lower);
2151 HOOK(hook_inline, stat_inline);
2152 HOOK(hook_tail_rec, stat_tail_rec);
2153 HOOK(hook_strength_red, stat_strength_red);
2154 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2155 HOOK(hook_if_conversion, stat_if_conversion);
2156 HOOK(hook_func_call, stat_func_call);
2157 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2158 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2160 obstack_init(&status->cnts);
2161 obstack_init(&status->be_data);
2163 /* create the hash-tables */
2164 status->irg_hash = new_pset(graph_cmp, 8);
2165 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2167 /* create the wait queue */
2168 status->wait_q = new_pdeq();
2170 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2171 /* build the pseudo-ops */
2173 _op_Phi0.code = --num;
2174 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2176 _op_PhiM.code = --num;
2177 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2179 _op_ProjM.code = --num;
2180 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2182 _op_MulC.code = --num;
2183 _op_MulC.name = new_id_from_chars(X("MulC"));
2185 _op_DivC.code = --num;
2186 _op_DivC.name = new_id_from_chars(X("DivC"));
2188 _op_ModC.code = --num;
2189 _op_ModC.name = new_id_from_chars(X("ModC"));
2191 _op_DivModC.code = --num;
2192 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2194 status->op_Phi0 = &_op_Phi0;
2195 status->op_PhiM = &_op_PhiM;
2196 status->op_ProjM = &_op_ProjM;
2197 status->op_MulC = &_op_MulC;
2198 status->op_DivC = &_op_DivC;
2199 status->op_ModC = &_op_ModC;
2200 status->op_DivModC = &_op_DivModC;
2202 status->op_Phi0 = NULL;
2203 status->op_PhiM = NULL;
2204 status->op_ProjM = NULL;
2205 status->op_MulC = NULL;
2206 status->op_DivC = NULL;
2207 status->op_ModC = NULL;
2208 status->op_DivModC = NULL;
2211 /* for Florian: count the Sel depth */
2212 if (enable_options & FIRMSTAT_COUNT_SELS) {
2213 _op_SelSel.code = --num;
2214 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2216 _op_SelSelSel.code = --num;
2217 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2219 status->op_SelSel = &_op_SelSel;
2220 status->op_SelSelSel = &_op_SelSelSel;
2222 status->op_SelSel = NULL;
2223 status->op_SelSelSel = NULL;
2226 /* register the dumper */
2227 stat_register_dumper(&simple_dumper);
2229 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2230 stat_register_dumper(&csv_dumper);
2232 /* initialize the pattern hash */
2233 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2235 /* initialize the Const options */
2236 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2237 stat_init_const_cnt(status);
2239 /* distribution table for parameter counts */
2240 status->dist_param_cnt = stat_new_int_distrib_tbl();
2244 } /* firm_init_stat */
2247 * Frees all dumper structures.
2249 static void stat_term_dumper(void) {
2250 dumper_t *dumper, *next_dumper;
2252 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2253 if (dumper->func_map)
2254 del_pset(dumper->func_map);
2256 next_dumper = dumper->next;
2258 dumper = next_dumper;
2260 } /* stat_term_dumper */
2263 /* Terminates the statistics module, frees all memory. */
2264 void stat_term(void) {
2265 if (status != (stat_info_t *)&status_disable) {
2266 obstack_free(&status->be_data, NULL);
2267 obstack_free(&status->cnts, NULL);
2272 status = (stat_info_t *)&status_disable;
2276 /* returns 1 if statistics were initialized, 0 otherwise */
2277 int stat_is_active(void) {
2278 return status != (stat_info_t *)&status_disable;
2279 } /* stat_is_active */
2283 /* initialize the statistics module. */
2284 void firm_init_stat(unsigned enable_options) {}
2286 /* Dumps a statistics snapshot */
2287 void stat_dump_snapshot(const char *name, const char *phase) {}
2289 /* terminates the statistics module, frees all memory */
2290 void stat_term(void);
2292 #endif /* FIRM_STATISTICS */