beifg: Factorise code to count interference components.
[libfirm] / ir / lower / lower_mux.c
index a1dd8ff..c93fb14 100644 (file)
@@ -1,27 +1,12 @@
 /*
- * 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   Replaces Mux nodes with control-flow
  * @author  Olaf Liebe
- * @version $Id$
  */
 #include "config.h"
 
@@ -34,7 +19,7 @@
 #include "irgwalk.h"
 #include "irgmod.h"
 #include "ircons.h"
-#include "irvrfy.h"
+#include "irpass_t.h"
 
 typedef struct walk_env {
        lower_mux_callback *cb_func;
@@ -43,7 +28,7 @@ typedef struct walk_env {
 
 static void find_mux_nodes(ir_node *mux, void *ctx)
 {
-       walk_env_t *env = ctx;
+       walk_env_t *env = (walk_env_t*)ctx;
 
        /* Skip non-mux nodes. */
        if (!is_Mux(mux))
@@ -60,30 +45,33 @@ static void find_mux_nodes(ir_node *mux, void *ctx)
 
 static void lower_mux_node(ir_node* mux)
 {
-       ir_node *upper_block;
-       ir_node *lower_block;
-       ir_node *cond;
-       ir_node *trueProj;
-       ir_node *falseProj;
-       ir_node *falseBlock;
-       ir_node *mux_jmps[2];
-       ir_node *mux_values[2];
-       ir_node *phi;
+       ir_node  *upper_block;
+       ir_node  *lower_block;
+       ir_node  *cond;
+       ir_node  *trueProj;
+       ir_node  *falseProj;
+       ir_node  *falseBlock;
+       ir_node  *mux_jmps[2];
+       ir_node  *mux_values[2];
+       ir_node  *phi;
+       ir_graph *irg;
+
+       irg = get_irn_irg(mux);
 
        /* Split the block in two halfs, with the mux in the upper block. */
        lower_block = get_nodes_block(mux);
        assert(lower_block != 0);
        part_block(mux);
-       upper_block = get_cur_block();
+       upper_block = get_nodes_block(mux);
 
        /* Create a cond node with two projs and a phi as mux replacement. The
         * true proj jumps directly to the lower block, the false proj uses a
         * block in-between, so that the phi can be used to select the result
         * value from the old mux node in the lower block. */
-       cond        = new_Cond(get_Mux_sel(mux));
-       trueProj    = new_Proj(cond, mode_X, pn_Cond_true);
-       falseProj   = new_Proj(cond, mode_X, pn_Cond_false);
-       falseBlock  = new_Block(1, &falseProj);
+       cond        = new_r_Cond(upper_block, get_Mux_sel(mux));
+       trueProj    = new_r_Proj(cond, mode_X, pn_Cond_true);
+       falseProj   = new_r_Proj(cond, mode_X, pn_Cond_false);
+       falseBlock  = new_r_Block(irg, 1, &falseProj);
        mux_jmps[0] = trueProj;
        mux_jmps[1] = new_r_Jmp(falseBlock);
 
@@ -94,11 +82,9 @@ static void lower_mux_node(ir_node* mux)
 
        /* Combine the two control flows with a phi to select the correct value
         * and use it to replace the mux. */
-       set_cur_block(lower_block);
-
        mux_values[0] = get_Mux_true(mux);
        mux_values[1] = get_Mux_false(mux);
-       phi = new_Phi(2, mux_values, get_irn_mode(mux));
+       phi = new_r_Phi(lower_block, 2, mux_values, get_irn_mode(mux));
        exchange(mux, phi);
 
        /* Add links and update phi node lists, for the next part_block() call.
@@ -113,7 +99,7 @@ static void lower_mux_node(ir_node* mux)
 
 void lower_mux(ir_graph *irg, lower_mux_callback *cb_func)
 {
-       int        i, n_muxes;
+       size_t     i, n_muxes;
        walk_env_t env;
 
        /* Scan the graph for mux nodes to lower. */
@@ -136,10 +122,32 @@ void lower_mux(ir_graph *irg, lower_mux_callback *cb_func)
                /* Cleanup, verify the graph. */
                ir_free_resources(irg, resources);
 
-               set_irg_outs_inconsistent(irg);
-               set_irg_doms_inconsistent(irg);
-               set_irg_extblk_inconsistent(irg);
-               set_irg_loopinfo_inconsistent(irg);
+               clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
        }
        DEL_ARR_F(env.muxes);
 }
+
+typedef struct pass_t {
+       ir_graph_pass_t    pass;
+       lower_mux_callback *cb_func;
+} pass_t;
+
+/**
+ * Wrapper to run ir_lower_mux() as an ir_graph pass
+ */
+static int pass_wrapper(ir_graph *irg, void *context)
+{
+       pass_t *pass = (pass_t*)context;
+
+       lower_mux(irg, pass->cb_func);
+       return 0;
+}
+
+ir_graph_pass_t *lower_mux_pass(const char *name, lower_mux_callback *cb_func)
+{
+       pass_t *pass = XMALLOCZ(pass_t);
+
+       pass->cb_func = cb_func;
+       return def_graph_pass_constructor(
+               &pass->pass, name ? name : "lower_mux", pass_wrapper);
+}