beifg: Factorise code to count interference components.
[libfirm] / ir / ir / irgwalk_blk.c
index 7d09350..f19a096 100644 (file)
@@ -1,39 +1,23 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
- *
  * This file is part of libFirm.
- *
- * This file may be distributed and/or modified under the terms of the
- * GNU General Public License version 2 as published by the Free Software
- * Foundation and appearing in the file LICENSE.GPL included in the
- * packaging of this file.
- *
- * Licensees holding valid libFirm Professional Edition licenses may use
- * this file in accordance with the libFirm Commercial License.
- * Agreement provided with the Software.
- *
- * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
- * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE.
+ * Copyright (C) 2012 University of Karlsruhe.
  */
 
 /**
  * @file
  * @brief   Blockwise walker implementation
  * @author  Michael Beck
- * @version $Id$
  */
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #include "irnode_t.h"
-#include "irgraph_t.h" /* visited flag */
+#include "irgraph_t.h"
 #include "irgwalk.h"
 #include "pset.h"
 #include "irhooks.h"
 #include "array.h"
 #include "hashptr.h"
+#include "ircons.h"
 
 #define _get_walk_arity(env, node) \
        ((env)->follow_deps ? get_irn_ins_or_deps((node)) : get_irn_arity((node)))
@@ -43,7 +27,7 @@
 /**
  * Metadata for block walker
  */
-typedef struct _blk_collect_data_t {
+typedef struct blk_collect_data_t {
        struct obstack obst;            /**< obstack to allocate objects on */
        pset           *blk_map;        /**< Hash map: Block -> List */
        ir_node        **blk_list;      /**< the Block list */
@@ -53,7 +37,7 @@ typedef struct _blk_collect_data_t {
 /**
  * An entry for a block in the blk_map
  */
-typedef struct _block_entry_t {
+typedef struct block_entry_t {
        ir_node *block;       /**< the block */
        ir_node **phi_list;   /**< the list of Phi instruction */
        ir_node **df_list;    /**< the list of data flow instruction */
@@ -64,9 +48,10 @@ typedef struct _block_entry_t {
 /**
  * compare two block_entries
  */
-static int addr_cmp(const void *elt, const void *key) {
-       const block_entry_t *e1 = elt;
-       const block_entry_t *e2 = key;
+static int addr_cmp(const void *elt, const void *key)
+{
+       const block_entry_t *e1 = (const block_entry_t*)elt;
+       const block_entry_t *e2 = (const block_entry_t*)key;
 
        return e1->block != e2->block;
 }
@@ -80,11 +65,11 @@ static block_entry_t *block_find_entry(ir_node *block, blk_collect_data_t *ctx)
        block_entry_t *elem;
 
        key.block = block;
-       elem = pset_find(ctx->blk_map, &key, HASH_PTR(block));
+       elem = (block_entry_t*)pset_find(ctx->blk_map, &key, hash_ptr(block));
        if (elem)
                return elem;
 
-       elem = obstack_alloc(&ctx->obst, sizeof(*elem));
+       elem = OALLOC(&ctx->obst, block_entry_t);
 
        elem->block      = block;
        elem->phi_list   = NEW_ARR_F(ir_node *, 0);
@@ -92,27 +77,28 @@ static block_entry_t *block_find_entry(ir_node *block, blk_collect_data_t *ctx)
        elem->cf_list    = NEW_ARR_F(ir_node *, 0);
        elem->entry_list = NEW_ARR_F(ir_node *, 0);
 
-       return pset_insert(ctx->blk_map, elem, HASH_PTR(block));
+       return (block_entry_t*)pset_insert(ctx->blk_map, elem, hash_ptr(block));
 }
 
 /**
  * Traverse a block in pre order.
  */
-static void traverse_block_pre(ir_node *block, block_entry_t *entry, irg_walk_func *pre, void *env) {
-       int j;
+static void traverse_block_pre(ir_node *block, block_entry_t *entry, irg_walk_func *pre, void *env)
+{
+       size_t j;
 
-       for (j = ARR_LEN(entry->cf_list) - 1; j >= 0; --j) {
-               ir_node *node = entry->cf_list[j];
+       for (j = ARR_LEN(entry->cf_list); j > 0;) {
+               ir_node *node = entry->cf_list[--j];
                pre(node, env);
        }
 
-       for (j = ARR_LEN(entry->df_list) - 1; j >= 0; --j) {
-               ir_node *node = entry->df_list[j];
+       for (j = ARR_LEN(entry->df_list); j > 0;) {
+               ir_node *node = entry->df_list[--j];
                pre(node, env);
        }
 
-       for (j = ARR_LEN(entry->phi_list) - 1; j >= 0; --j) {
-               ir_node *node = entry->phi_list[j];
+       for (j = ARR_LEN(entry->phi_list); j > 0;) {
+               ir_node *node = entry->phi_list[--j];
                pre(node, env);
        }
 
@@ -122,8 +108,10 @@ static void traverse_block_pre(ir_node *block, block_entry_t *entry, irg_walk_fu
 /**
  * Traverse a block in post order.
  */
-void traverse_block_post(ir_node *block, block_entry_t *entry, irg_walk_func *post, void *env)  {
-       int j, n;
+static void traverse_block_post(ir_node *block, block_entry_t *entry,
+                                irg_walk_func *post, void *env)
+{
+       size_t j, n;
 
        post(block, env);
 
@@ -146,11 +134,12 @@ void traverse_block_post(ir_node *block, block_entry_t *entry, irg_walk_func *po
 /**
  * traverse the pre order only, from End to Start
  */
-static void traverse_pre(blk_collect_data_t *blks, irg_walk_func *pre, void *env) {
-       int i;
+static void traverse_pre(blk_collect_data_t *blks, irg_walk_func *pre, void *env)
+{
+       size_t i;
 
-       for (i = ARR_LEN(blks->blk_list) - 1; i >= 0; --i) {
-               ir_node       *block = blks->blk_list[i];
+       for (i = ARR_LEN(blks->blk_list); i > 0;) {
+               ir_node       *block = blks->blk_list[--i];
                block_entry_t *entry = block_find_entry(block, blks);
 
                traverse_block_pre(block, entry, pre, env);
@@ -165,8 +154,9 @@ static void traverse_pre(blk_collect_data_t *blks, irg_walk_func *pre, void *env
 /**
  * traverse the post order only, from Start to End
  */
-static void traverse_post(blk_collect_data_t *blks, irg_walk_func *post, void *env) {
-       int i, k;
+static void traverse_post(blk_collect_data_t *blks, irg_walk_func *post, void *env)
+{
+       size_t i, k;
 
        for (i = 0, k = ARR_LEN(blks->blk_list); i < k; ++i) {
                ir_node       *block = blks->blk_list[i];
@@ -186,10 +176,10 @@ static void traverse_post(blk_collect_data_t *blks, irg_walk_func *post, void *e
  */
 static void traverse_both(blk_collect_data_t *blks, irg_walk_func *pre, irg_walk_func *post, void *env)
 {
-       int i;
+       size_t i;
 
-       for (i = ARR_LEN(blks->blk_list) - 1; i >= 0; --i) {
-               ir_node       *block = blks->blk_list[i];
+       for (i = ARR_LEN(blks->blk_list); i > 0;) {
+               ir_node       *block = blks->blk_list[--i];
                block_entry_t *entry = block_find_entry(block, blks);
 
                traverse_block_pre(block, entry, pre, env);
@@ -202,7 +192,8 @@ static void traverse_both(blk_collect_data_t *blks, irg_walk_func *pre, irg_walk
 /**
  * Do the traversal.
  */
-static void traverse_blocks(blk_collect_data_t *blks, irg_walk_func *pre, irg_walk_func *post, void *env) {
+static void traverse_blocks(blk_collect_data_t *blks, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
        if      (!post) traverse_pre (blks, pre, env);
        else if (!pre)  traverse_post(blks, post, env);
        else            traverse_both(blks, pre, post, env);
@@ -218,8 +209,9 @@ typedef struct dom_traversal_t {
 /**
  * Dom block walker. Visit all nodes in pre oder.
  */
-static void dom_block_visit_pre(ir_node *block, void *env) {
-       dom_traversal_t *ctx   = env;
+static void dom_block_visit_pre(ir_node *block, void *env)
+{
+       dom_traversal_t *ctx   = (dom_traversal_t*)env;
        block_entry_t   *entry = block_find_entry(block, ctx->blks);
 
        traverse_block_pre(block, entry, ctx->pre, ctx->env);
@@ -228,8 +220,9 @@ static void dom_block_visit_pre(ir_node *block, void *env) {
 /**
  * Dom block walker. Visit all nodes in post oder.
  */
-static void dom_block_visit_post(ir_node *block, void *env) {
-       dom_traversal_t *ctx   = env;
+static void dom_block_visit_post(ir_node *block, void *env)
+{
+       dom_traversal_t *ctx   = (dom_traversal_t*)env;
        block_entry_t   *entry = block_find_entry(block, ctx->blks);
 
        traverse_block_post(block, entry, ctx->post, ctx->env);
@@ -238,8 +231,9 @@ static void dom_block_visit_post(ir_node *block, void *env) {
 /**
  * Dom block walker. Visit all nodes in pre oder, than in post order.
  */
-static void dom_block_visit_both(ir_node *block, void *env) {
-       dom_traversal_t *ctx   = env;
+static void dom_block_visit_both(ir_node *block, void *env)
+{
+       dom_traversal_t *ctx   = (dom_traversal_t*)env;
        block_entry_t   *entry = block_find_entry(block, ctx->blks);
 
        traverse_block_pre(block, entry, ctx->pre, ctx->env);
@@ -249,7 +243,8 @@ static void dom_block_visit_both(ir_node *block, void *env) {
 /**
  * Do the traversal in the dominator tree in top-down order.
  */
-static void traverse_dom_blocks_top_down(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env) {
+static void traverse_dom_blocks_top_down(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
        dom_traversal_t ctx;
 
        ctx.blks = blks;
@@ -258,11 +253,11 @@ static void traverse_dom_blocks_top_down(blk_collect_data_t* blks, irg_walk_func
        ctx.env  = env;
 
        if (pre != NULL && post != NULL)
-               dom_tree_walk_irg(current_ir_graph,     dom_block_visit_both, NULL, &ctx);
+               dom_tree_walk_irg(current_ir_graph, dom_block_visit_both, NULL, &ctx);
        else if (pre != NULL)
-               dom_tree_walk_irg(current_ir_graph,     dom_block_visit_pre, NULL, &ctx);
+               dom_tree_walk_irg(current_ir_graph, dom_block_visit_pre, NULL, &ctx);
        else if (post != NULL)
-               dom_tree_walk_irg(current_ir_graph,     dom_block_visit_post, NULL, &ctx);
+               dom_tree_walk_irg(current_ir_graph, dom_block_visit_post, NULL, &ctx);
 }
 
 /**
@@ -276,7 +271,7 @@ static void collect_walk(ir_node *node, blk_collect_data_t *env)
 
        mark_irn_visited(node);
 
-       if (node->op == op_Block) {
+       if (is_Block(node)) {
                /* predecessors of a block are control flow nodes */
                for (i = _get_walk_arity(env, node) - 1; i >= 0; --i) {
                        ir_node *pred = _get_walk_irn_n(env, node, i);
@@ -315,7 +310,7 @@ static void collect_walk(ir_node *node, blk_collect_data_t *env)
                                collect_walk(pred, env);
 
                                /* BEWARE: predecessors of End nodes might be blocks */
-                               if (is_no_Block(pred)) {
+                               if (!is_Block(pred)) {
                                        ir_node *blk  = get_nodes_block(pred);
 
                                        /*
@@ -353,7 +348,7 @@ static void collect_blks_lists(ir_node *node, ir_node *block,
                        ir_node *pred = _get_walk_irn_n(env, node, i);
 
                        /* BEWARE: predecessors of End nodes might be blocks */
-                       if (is_no_Block(pred)) {
+                       if (!is_Block(pred)) {
                                ir_node *blk  = get_nodes_block(pred);
 
                                if (!irn_visited(pred)) {
@@ -380,18 +375,18 @@ static void collect_blks_lists(ir_node *node, ir_node *block,
  */
 static void collect_lists(blk_collect_data_t *env)
 {
-       int             i, j;
+       size_t          i, j;
        ir_node         *block, *node;
        block_entry_t   *entry;
 
        inc_irg_visited(current_ir_graph);
 
-       for (i = ARR_LEN(env->blk_list) - 1; i >= 0; --i) {
-               block = env->blk_list[i];
+       for (i = ARR_LEN(env->blk_list); i > 0;) {
+               block = env->blk_list[--i];
                entry = block_find_entry(block, env);
 
-               for (j = ARR_LEN(entry->entry_list) - 1; j >= 0; --j) {
-                       node = entry->entry_list[j];
+               for (j = ARR_LEN(entry->entry_list); j > 0;) {
+                       node = entry->entry_list[--j];
 
                        /* a entry might already be visited due to Phi loops */
                        if (node->visited < current_ir_graph->visited)
@@ -401,23 +396,17 @@ static void collect_lists(blk_collect_data_t *env)
 }
 
 /**
- * Intraprozedural graph walker over blocks.
+ * Intra procedural graph walker over blocks.
  */
-static void
-do_irg_walk_blk(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env, unsigned follow_deps,
-                void (*traverse)(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env))
+static void do_irg_walk_blk(ir_graph *irg, irg_walk_func *pre,
+       irg_walk_func *post, void *env, unsigned follow_deps,
+       void (*traverse)(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env))
 {
        ir_node            *end_node = get_irg_end(irg);
        ir_node            *end_blk = get_irg_end_block(irg);
        blk_collect_data_t blks;
        block_entry_t      *entry;
 
-#ifdef INTERPROCEDURAL_VIEW
-       /* switch off interprocedural view */
-       int old_view       = get_interprocedural_view();
-       set_interprocedural_view(0);
-#endif
-
        obstack_init(&blks.obst);
        blks.blk_map     = new_pset(addr_cmp, 1);
        blks.blk_list    = NEW_ARR_F(ir_node *, 0);
@@ -444,13 +433,11 @@ do_irg_walk_blk(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *en
        del_pset(blks.blk_map);
        obstack_free(&blks.obst, NULL);
 
-#ifdef INTERPROCEDURAL_VIEW
-       set_interprocedural_view(old_view);
-#endif
        ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
 }
 
-void irg_walk_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
+void irg_walk_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
        ir_graph * rem = current_ir_graph;
 
        hook_irg_walk_blkwise(irg, (generic_func *)pre, (generic_func *)post);
@@ -459,7 +446,8 @@ void irg_walk_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *po
        current_ir_graph = rem;
 }
 
-void irg_walk_in_or_dep_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
+void irg_walk_in_or_dep_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
        ir_graph * rem = current_ir_graph;
 
        hook_irg_walk_blkwise(irg, (generic_func *)pre, (generic_func *)post);
@@ -468,7 +456,8 @@ void irg_walk_in_or_dep_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_wal
        current_ir_graph = rem;
 }
 
-void irg_walk_blkwise_dom_top_down(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
+void irg_walk_blkwise_dom_top_down(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
        ir_graph * rem = current_ir_graph;
 
        hook_irg_walk_blkwise(irg, (generic_func *)pre, (generic_func *)post);