- get_Block_cfgpred_arr() IS supported, but should not be in the official
[libfirm] / ir / stat / firmstat.c
index f665ee9..7c9a7c4 100644 (file)
  * @author  Michael Beck
  * @version $Id$
  */
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #ifdef FIRM_STATISTICS
 
 #include <stdio.h>
-
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
-#ifdef HAVE_STRING_H
-# include <string.h>
-#endif
+#include <stdlib.h>
+#include <string.h>
 
 #include "irouts.h"
 #include "irdump.h"
@@ -212,10 +205,12 @@ static void opcode_clear_entry(node_entry_t *elem) {
        cnt_clr(&elem->cnt_alive);
        cnt_clr(&elem->new_node);
        cnt_clr(&elem->into_Id);
+       cnt_clr(&elem->normalized);
 }  /* opcode_clear_entry */
 
 /**
- * Returns the associates node_entry_t for an ir_op
+ * Returns the associates node_entry_t for an ir_op (and allocates
+ * one if not yet available).
  *
  * @param op    the IR operation
  * @param hmap  a hash map containing ir_op* -> node_entry_t*
@@ -285,7 +280,7 @@ static void graph_clear_entry(graph_entry_t *elem, int all) {
 /**
  * Returns the associated graph_entry_t for an IR graph.
  *
- * @param irg   the IR graph
+ * @param irg   the IR graph, NULL for the global counter
  * @param hmap  the hash map containing ir_graph* -> graph_entry_t*
  */
 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
@@ -597,10 +592,10 @@ static ir_op *stat_get_irn_op(ir_node *node)
                }  /* if */
                break;
        case iro_Sel:
-               if (get_irn_op(get_Sel_ptr(node)) == op_Sel) {
+               if (is_Sel(get_Sel_ptr(node))) {
                        /* special case, a Sel of a Sel, count on extra counter */
                        op = status->op_SelSel ? status->op_SelSel : op;
-                       if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
+                       if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
                                /* special case, a Sel of a Sel of a Sel, count on extra counter */
                                op = status->op_SelSelSel ? status->op_SelSelSel : op;
                        }  /* if */
@@ -722,7 +717,7 @@ static void update_extbb_info(ir_node *node, graph_entry_t *graph)
        cnt_inc(&eb_entry->cnt[bcnt_nodes]);
 
        /* don't count keep-alive edges */
-       if (get_irn_op(node) == op_End)
+       if (is_End(node))
                return;
 
        arity = get_irn_arity(node);
@@ -804,7 +799,7 @@ static void stat_update_call(ir_node *call, graph_entry_t *graph)
        /* found a call, this function is not a leaf */
        graph->is_leaf = 0;
 
-       if (get_irn_op(ptr) == op_SymConst) {
+       if (is_SymConst(ptr)) {
                if (get_SymConst_kind(ptr) == symconst_addr_ent) {
                        /* ok, we seems to know the entity */
                        ent = get_SymConst_entity(ptr);
@@ -872,7 +867,7 @@ static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
        if (is_Bad(block))
                return;
 
-       if (get_irn_op(ptr) == op_SymConst) {
+       if (is_SymConst(ptr)) {
                if (get_SymConst_kind(ptr) == symconst_addr_ent) {
                        /* ok, we seems to know the entity */
                        ent = get_SymConst_entity(ptr);
@@ -970,7 +965,7 @@ static void update_node_stat(ir_node *node, void *env)
        node_entry_t *entry;
 
        ir_op *op = stat_get_irn_op(node);
-       int arity = get_irn_arity(node);
+       int i, arity = get_irn_arity(node);
 
        entry = opcode_get_entry(op, graph->opcode_hash);
 
@@ -989,12 +984,6 @@ static void update_node_stat(ir_node *node, void *env)
        /* handle statistics for special node types */
 
        switch (op->code) {
-       case iro_Const:
-               if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
-                       /* check properties of constants */
-                       stat_update_const(status, node, graph);
-               }  /* if */
-               break;
        case iro_Call:
                /* check for properties that depends on calls like recursion/leaf/indirect call */
                stat_update_call(node, graph);
@@ -1007,9 +996,33 @@ static void update_node_stat(ir_node *node, void *env)
                /* check address properties */
                stat_update_address(get_Store_ptr(node), graph);
                break;
+       case iro_Phi:
+               /* check for non-strict Phi nodes */
+               for (i = arity - 1; i >= 0; --i) {
+                       ir_node *pred = get_Phi_pred(node, i);
+                       if (is_Unknown(pred)) {
+                               /* found an Unknown predecessor, graph is not strict */
+                               graph->is_strict = 0;
+                               break;
+                       }
+               }
        default:
                ;
        }  /* switch */
+
+       /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
+       if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
+               int i;
+
+               for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+                       ir_node *pred = get_irn_n(node, i);
+
+                       if (is_Const(pred)) {
+                               /* check properties of constants */
+                               stat_update_const(status, pred, graph);
+                       }  /* if */
+               }  /* for */
+       }  /* if */
 }  /* update_node_stat */
 
 /**
@@ -1178,6 +1191,7 @@ static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
        graph->is_leaf_call  = LCS_UNKNOWN;
        graph->is_recursive  = 0;
        graph->is_chain_call = 1;
+       graph->is_strict     = 1;
 
        /* create new block counter */
        graph->block_hash = new_pset(block_cmp, 5);
@@ -1291,15 +1305,13 @@ static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
  * Register a dumper.
  */
 static void stat_register_dumper(const dumper_t *dumper) {
-       dumper_t *p = xmalloc(sizeof(*p));
+       dumper_t *p = XMALLOC(dumper_t);
 
-       if (p) {
-               memcpy(p, dumper, sizeof(*p));
+       memcpy(p, dumper, sizeof(*p));
 
-               p->next        = status->dumper;
-               p->status      = status;
-               status->dumper = p;
-       }
+       p->next        = status->dumper;
+       p->status      = status;
+       status->dumper = p;
 
        /* FIXME: memory leak */
 }  /* stat_register_dumper */
@@ -1521,6 +1533,36 @@ static void stat_turn_into_id(void *ctx, ir_node *node) {
        STAT_LEAVE;
 }  /* stat_turn_into_id */
 
+/**
+ * Hook: A node is normalized
+ *
+ * @param ctx   the hook context
+ * @param node  the IR node that was normalized
+ */
+static void stat_normalize(void *ctx, ir_node *node) {
+       (void) ctx;
+       if (! status->stat_options)
+               return;
+
+       STAT_ENTER;
+       {
+               node_entry_t *entry;
+               graph_entry_t *graph;
+               ir_op *op = stat_get_irn_op(node);
+
+               /* increase global value */
+               graph = graph_get_entry(NULL, status->irg_hash);
+               entry = opcode_get_entry(op, graph->opcode_hash);
+               cnt_inc(&entry->normalized);
+
+               /* increase local value */
+               graph = graph_get_entry(current_ir_graph, status->irg_hash);
+               entry = opcode_get_entry(op, graph->opcode_hash);
+               cnt_inc(&entry->normalized);
+       }
+       STAT_LEAVE;
+}  /* stat_normalize */
+
 /**
  * Hook: A new graph was created
  *
@@ -1544,6 +1586,7 @@ static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
                graph->is_leaf_call  = 0;
                graph->is_recursive  = 0;
                graph->is_chain_call = 0;
+               graph->is_strict     = 1;
                graph->is_analyzed   = 0;
        }
        STAT_LEAVE;
@@ -1653,14 +1696,15 @@ static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic
  * @param kind  the optimization kind
  */
 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind) {
-       ir_op *op          = stat_get_irn_op(n);
-       opt_entry_t *entry = opt_get_entry(op, hmap);
+       opt_entry_t *entry;
+       ir_op *op = stat_get_irn_op(n);
 
        /* ignore CSE for Constants */
        if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
                return;
 
        /* increase global value */
+       entry = opt_get_entry(op, hmap);
        cnt_inc(&entry->count);
 }  /* removed_due_opt */
 
@@ -2149,7 +2193,7 @@ void stat_dump_snapshot(const char *name, const char *phase)
 
                stat_finish_pattern_history(fname);
 
-               /* clear the global counter here */
+               /* clear the global counters here */
                {
                        node_entry_t *entry;
 
@@ -2157,7 +2201,7 @@ void stat_dump_snapshot(const char *name, const char *phase)
                                opcode_clear_entry(entry);
                        }  /* for */
                        /* clear all global counter */
-                       graph_clear_entry(global, 1);
+                       graph_clear_entry(global, /*all=*/1);
                }
        }
        STAT_LEAVE;
@@ -2177,8 +2221,7 @@ void firm_init_stat(unsigned enable_options)
        if (! (enable_options & FIRMSTAT_ENABLED))
                return;
 
-       status = xmalloc(sizeof(*status));
-       memset(status, 0, sizeof(*status));
+       status = XMALLOCZ(stat_info_t);
 
        /* enable statistics */
        status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
@@ -2188,6 +2231,7 @@ void firm_init_stat(unsigned enable_options)
        HOOK(hook_free_ir_op,                         stat_free_ir_op);
        HOOK(hook_new_node,                           stat_new_node);
        HOOK(hook_turn_into_id,                       stat_turn_into_id);
+       HOOK(hook_normalize,                          stat_normalize);
        HOOK(hook_new_graph,                          stat_new_graph);
        HOOK(hook_free_graph,                         stat_free_graph);
        HOOK(hook_irg_walk,                           stat_irg_walk);