Let the length of tarval be of type size_t.
[libfirm] / ir / opt / ifconv.c
index 0634cc0..27a3236 100644 (file)
  * @author  Christoph Mallon
  * @version $Id$
  */
-
 #include "config.h"
 
 #include <assert.h>
+#include <stdbool.h>
+
 #include "iroptimize.h"
 #include "obst.h"
 #include "irnode_t.h"
@@ -38,8 +39,8 @@
 #include "irtools.h"
 #include "array_t.h"
 #include "irpass_t.h"
+#include "be.h"
 
-// debug
 #include "irdump.h"
 #include "debug.h"
 
  * Environment for if-conversion.
  */
 typedef struct walker_env {
-       const ir_settings_if_conv_t *params; /**< Conversion parameter. */
-       int                         changed; /**< Set if the graph was changed. */
+       arch_allow_ifconv_func allow_ifconv;
+       bool                   changed; /**< Set if the graph was changed. */
 } walker_env;
 
 DEBUG_ONLY(static firm_dbg_module_t *dbg);
 
-/**
- * Default callback for Mux creation: allows every Mux to be created.
- */
-static int default_allow_ifconv(ir_node *sel, ir_node* phi_list, int i, int j)
-{
-       (void) sel;
-       (void) phi_list;
-       (void) i;
-       (void) j;
-       return 1;
-}
-
-/**
- * Default options.
- */
-static const ir_settings_if_conv_t default_info = {
-       0,    /* doesn't matter for Mux */
-       default_allow_ifconv
-};
-
 /**
  * Returns non-zero if a Block can be emptied.
  *
  * @param block  the block
  */
-static int can_empty_block(ir_node *block) {
+static bool can_empty_block(ir_node *block)
+{
        return get_Block_mark(block) == 0;
 }
 
@@ -292,7 +274,7 @@ static void prepare_path(ir_node* block, int i, const ir_node* dependency)
  */
 static void if_conv_walker(ir_node *block, void *ctx)
 {
-       walker_env *env = ctx;
+       walker_env *env = (walker_env*)ctx;
        int arity;
        int i;
 
@@ -326,7 +308,10 @@ restart:
                                ir_node* sel;
                                ir_node* mux_block;
                                ir_node* phi;
+                               ir_node* p;
                                ir_node* pred1;
+                               bool     supported;
+                               bool     negated;
                                dbg_info* cond_dbg;
 
                                pred1 = get_Block_cfgpred_block(block, j);
@@ -337,21 +322,37 @@ restart:
 
                                if (projx1 == NULL) continue;
 
+                               sel = get_Cond_selector(cond);
                                phi = get_Block_phis(block);
-                               if (!env->params->allow_ifconv(get_Cond_selector(cond), phi, i, j))
+                               supported = true;
+                               negated   = get_Proj_proj(projx0) == pn_Cond_false;
+                               for (p = phi; p != NULL; p = get_Phi_next(p)) {
+                                       ir_node *mux_false;
+                                       ir_node *mux_true;
+                                       if (negated) {
+                                               mux_true  = get_Phi_pred(p, j);
+                                               mux_false = get_Phi_pred(p, i);
+                                       } else {
+                                               mux_true  = get_Phi_pred(p, i);
+                                               mux_false = get_Phi_pred(p, j);
+                                       }
+                                       if (!env->allow_ifconv(sel, mux_false, mux_true)) {
+                                               supported = false;
+                                               break;
+                                       }
+                               }
+                               if (!supported)
                                        continue;
 
                                DB((dbg, LEVEL_1, "Found Cond %+F with proj %+F and %+F\n",
                                        cond, projx0, projx1
                                ));
 
-                               env->changed = 1;
+                               env->changed = true;
                                prepare_path(block, i, dependency);
                                prepare_path(block, j, dependency);
                                arity = get_irn_arity(block);
 
-                               sel = get_Cond_selector(cond);
-
                                mux_block = get_nodes_block(cond);
                                cond_dbg = get_irn_dbg_info(cond);
                                do {
@@ -370,12 +371,12 @@ restart:
                                                 * one block, but not at the same memory node
                                                 */
                                                assert(get_irn_mode(phi) != mode_M);
-                                               if (get_Proj_proj(projx0) == pn_Cond_true) {
-                                                       t = val_i;
-                                                       f = val_j;
-                                               } else {
+                                               if (negated) {
                                                        t = val_j;
                                                        f = val_i;
+                                               } else {
+                                                       t = val_i;
+                                                       f = val_j;
                                                }
 
                                                mux = new_rd_Mux(cond_dbg, mux_block, sel, f, t, get_irn_mode(phi));
@@ -440,7 +441,8 @@ static void init_block_link(ir_node *block, void *env)
  * Daisy-chain all Phis in a block.
  * If a non-movable node is encountered set the has_pinned flag in its block.
  */
-static void collect_phis(ir_node *node, void *env) {
+static void collect_phis(ir_node *node, void *env)
+{
        (void) env;
 
        if (is_Phi(node)) {
@@ -448,7 +450,7 @@ static void collect_phis(ir_node *node, void *env) {
 
                add_Block_phi(block, node);
        } else {
-               if (is_no_Block(node) && get_irn_pinned(node) == op_pin_state_pinned) {
+               if (!is_Block(node) && get_irn_pinned(node) == op_pin_state_pinned) {
                        /*
                         * Ignore control flow nodes (except Raise), these will be removed.
                         */
@@ -462,13 +464,14 @@ static void collect_phis(ir_node *node, void *env) {
        }
 }
 
-void opt_if_conv(ir_graph *irg, const ir_settings_if_conv_t *params)
+void opt_if_conv(ir_graph *irg)
 {
-       walker_env env;
+       walker_env            env;
+       const backend_params *be_params = be_get_backend_param();
 
        /* get the parameters */
-       env.params  = (params != NULL ? params : &default_info);
-       env.changed = 0;
+       env.allow_ifconv = be_params->allow_ifconv;
+       env.changed      = false;
 
        FIRM_DBG_REGISTER(dbg, "firm.opt.ifconv");
 
@@ -500,33 +503,7 @@ void opt_if_conv(ir_graph *irg, const ir_settings_if_conv_t *params)
        free_cdep(irg);
 }
 
-struct pass_t {
-       ir_graph_pass_t             pass;
-       const ir_settings_if_conv_t *params;
-};
-
-/**
- * Wrapper for running opt_if_conv() as an ir_graph pass.
- */
-static int pass_wrapper(ir_graph *irg, void *context) {
-       struct pass_t *pass = context;
-       opt_if_conv(irg, pass->params);
-       return 0;
-}  /* pass_wrapper */
-
-ir_graph_pass_t *opt_if_conv_pass(
-       const char *name, const ir_settings_if_conv_t *params)
+ir_graph_pass_t *opt_if_conv_pass(const char *name)
 {
-       struct pass_t *pass = xmalloc(sizeof(*pass));
-
-       pass->pass.kind       = k_ir_prog_pass;
-       pass->pass.run_on_irg = pass_wrapper;
-       pass->pass.context    = pass;
-       pass->pass.name       = name ? name : "if_conv";
-
-       pass->params = params;
-
-       INIT_LIST_HEAD(&pass->pass.list);
-
-       return &pass->pass;
+       return def_graph_pass(name ? name : "ifconv", opt_if_conv);
 }