Loop unrolling implemented. Unrolling passes every test of the testsuite.
[libfirm] / ir / opt / ifconv.c
index 1fda437..84a93f7 100644 (file)
@@ -27,6 +27,8 @@
 #include "config.h"
 
 #include <assert.h>
+#include <stdbool.h>
+
 #include "iroptimize.h"
 #include "obst.h"
 #include "irnode_t.h"
@@ -37,6 +39,7 @@
 #include "irgwalk.h"
 #include "irtools.h"
 #include "array_t.h"
+#include "irpass_t.h"
 
 // debug
 #include "irdump.h"
@@ -53,15 +56,15 @@ typedef struct walker_env {
 DEBUG_ONLY(static firm_dbg_module_t *dbg);
 
 /**
- * Default callback for Mux creation: allows every Mux to be created.
+ * Default callback for Mux creation: don't allow any Mux nodes
  */
-static int default_allow_ifconv(ir_node *sel, ir_node* phi_list, int i, int j)
+static int default_allow_ifconv(ir_node *sel, ir_node *mux_false,
+                                ir_node *mux_true)
 {
        (void) sel;
-       (void) phi_list;
-       (void) i;
-       (void) j;
-       return 1;
+       (void) mux_false;
+       (void) mux_true;
+       return 0;
 }
 
 /**
@@ -77,7 +80,8 @@ static const ir_settings_if_conv_t default_info = {
  *
  * @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;
 }
 
@@ -325,7 +329,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);
@@ -336,8 +343,26 @@ 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->params->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",
@@ -349,8 +374,6 @@ restart:
                                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 {
@@ -369,15 +392,15 @@ 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, current_ir_graph, mux_block, sel, f, t, get_irn_mode(phi));
+                                               mux = new_rd_Mux(cond_dbg, mux_block, sel, f, t, get_irn_mode(phi));
                                                DB((dbg, LEVEL_2, "Generating %+F for %+F\n", mux, phi));
                                        }
 
@@ -416,7 +439,7 @@ restart:
 #endif
                                        return;
                                } else {
-                                       rewire(block, i, j, new_r_Jmp(current_ir_graph, mux_block));
+                                       rewire(block, i, j, new_r_Jmp(mux_block));
                                        goto restart;
                                }
                        }
@@ -439,7 +462,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)) {
@@ -498,3 +522,28 @@ 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;
+}
+
+ir_graph_pass_t *opt_if_conv_pass(const char *name,
+                                  const ir_settings_if_conv_t *params)
+{
+       struct pass_t *pass = XMALLOCZ(struct pass_t);
+       pass->params = params;
+
+       return def_graph_pass_constructor(
+               &pass->pass, name ? name : "ifconv", pass_wrapper);
+}