allow_ifconv callback may not be NULL anymore
[libfirm] / ir / opt / ifconv.c
index 291abde..ee24f48 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
  * @author  Christoph Mallon
  * @version $Id$
  */
-
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <assert.h>
+#include <stdbool.h>
 
 #include "iroptimize.h"
 #include "obst.h"
 #include "irnode_t.h"
 #include "cdep.h"
 #include "ircons.h"
-#include "irdom.h"
 #include "irgmod.h"
 #include "irgopt.h"
 #include "irgwalk.h"
 #include "irtools.h"
-#include "array.h"
-#include "xmalloc.h"
+#include "array_t.h"
+#include "irpass_t.h"
+#include "be.h"
 
-// debug
 #include "irdump.h"
 #include "debug.h"
 
-DEBUG_ONLY(firm_dbg_module_t *dbg);
-
-/** allow every Psi 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 opt_if_conv_info_t default_info = {
-       0,    /* doesn't matter for Psi */
-       default_allow_ifconv
-};
-
 /**
- * Additional block info.
+ * Environment for if-conversion.
  */
-typedef struct block_info {
-       ir_node *phi;   /**< head of the Phi list */
-       int has_pinned; /**< set if the block contains instructions that cannot be moved */
-} block_info;
-
-
-static INLINE block_info* get_block_blockinfo(const ir_node* block)
-{
-       return get_irn_link(block);
-}
+typedef struct walker_env {
+       arch_allow_ifconv_func allow_ifconv;
+       bool                   changed; /**< Set if the graph was changed. */
+} walker_env;
 
+DEBUG_ONLY(static firm_dbg_module_t *dbg);
 
 /**
  * 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_blockinfo(block)->has_pinned;
+       return get_Block_mark(block) == 0;
 }
 
-
+/**
+ * Find the ProjX node leading from block dependency to block start.
+ *
+ * @param start       a block that is control depended on dependency
+ * @param dependency  the block that decides whether start is executed
+ *
+ * @return a ProjX node that represent the decision control flow or
+ *         NULL is start is not dependent at all or a block on the way
+ *         cannot be emptied
+ */
 static ir_node* walk_to_projx(ir_node* start, const ir_node* dependency)
 {
        int arity;
@@ -103,18 +86,21 @@ static ir_node* walk_to_projx(ir_node* start, const ir_node* dependency)
        arity = get_irn_arity(start);
        for (i = 0; i < arity; ++i) {
                ir_node* pred = get_irn_n(start, i);
-               ir_node* pred_block = get_nodes_block(pred);
+               ir_node* pred_block = get_nodes_block(skip_Proj(pred));
 
                if (pred_block == dependency) {
                        if (is_Proj(pred)) {
                                assert(get_irn_mode(pred) == mode_X);
+                               /* we found it */
                                return pred;
                        }
+                       /* Not a Proj? Should not happen. */
                        return NULL;
                }
 
                if (is_Proj(pred)) {
                        assert(get_irn_mode(pred) == mode_X);
+                       /* another Proj but not from the control block */
                        return NULL;
                }
 
@@ -127,22 +113,37 @@ static ir_node* walk_to_projx(ir_node* start, const ir_node* dependency)
 
 
 /**
- * Copies the DAG starting at node to the ith predecessor block of src_block
- * -if the node isn't in the src_block, this is a nop and the node is returned
- * -if the node is a phi in the src_block, the ith predecessor of the phi is
- *   returned
- * otherwise returns the copy of the passed node
+ * Recursively copies the DAG starting at node to the i-th predecessor
+ * block of src_block
+ * - if node isn't in the src_block, recursion ends and node is returned
+ * - if node is a Phi in the src_block, the i-th predecessor of this Phi is
+ *   returned and recursion ends
+ * otherwise returns a copy of the passed node created in the i-th predecessor of
+ * src_block.
+ *
+ * @param node       a root of a DAG
+ * @param src_block  the block of the DAG
+ * @param i          the position of the predecessor the DAG
+ *                   is moved to
+ *
+ * @return  the root of the copied DAG
  */
 static ir_node* copy_to(ir_node* node, ir_node* src_block, int i)
 {
        ir_node* dst_block;
        ir_node* copy;
-       int arity;
        int j;
 
-       if (get_nodes_block(node) != src_block) return node;
-       if (get_irn_op(node) == op_Phi) return get_irn_n(node, i);
+       if (get_nodes_block(node) != src_block) {
+               /* already outside src_block, do not copy */
+               return node;
+       }
+       if (is_Phi(node)) {
+               /* move through the Phi to the i-th predecessor */
+               return get_irn_n(node, i);
+       }
 
+       /* else really need a copy */
        copy = exact_copy(node);
        dst_block = get_nodes_block(get_irn_n(src_block, i));
        set_nodes_block(copy, dst_block);
@@ -150,8 +151,8 @@ static ir_node* copy_to(ir_node* node, ir_node* src_block, int i)
        DB((dbg, LEVEL_1, "Copying node %+F to block %+F, copy is %+F\n",
                node, dst_block, copy));
 
-       arity = get_irn_arity(node);
-       for (j = 0; j < arity; ++j) {
+       /* move recursively all predecessors */
+       for (j = get_irn_arity(node) - 1; j >= 0; --j) {
                set_irn_n(copy, j, copy_to(get_irn_n(node, j), src_block, i));
                DB((dbg, LEVEL_2, "-- pred %d is %+F\n", j, get_irn_n(copy, j)));
        }
@@ -160,7 +161,13 @@ static ir_node* copy_to(ir_node* node, ir_node* src_block, int i)
 
 
 /**
- * Remove predecessors i and j from node and add predecessor new_pred
+ * Remove predecessors i and j (i < j) from a node and
+ * add an additional predecessor new_pred.
+ *
+ * @param node      the node whose inputs are changed
+ * @param i         the first index to remove
+ * @param j         the second index to remove
+ * @param new_pred  a node that is added as a new input to node
  */
 static void rewire(ir_node* node, int i, int j, ir_node* new_pred)
 {
@@ -182,14 +189,14 @@ static void rewire(ir_node* node, int i, int j, ir_node* new_pred)
 
 
 /**
- * Remove the jth predecessors from the ith predecessor of block and add it to block
+ * Remove the j-th predecessors from the i-th predecessor of block and add it to block
  */
 static void split_block(ir_node* block, int i, int j)
 {
        ir_node* pred_block = get_nodes_block(get_irn_n(block, i));
        int arity = get_irn_arity(block);
        int new_pred_arity;
-       ir_node* phi;
+       ir_node *phi, *next;
        ir_node **ins;
        ir_node **pred_ins;
        int k;
@@ -198,7 +205,7 @@ static void split_block(ir_node* block, int i, int j)
 
        NEW_ARR_A(ir_node*, ins, arity + 1);
 
-       for (phi = get_block_blockinfo(block)->phi; phi != NULL; phi = get_irn_link(phi)) {
+       for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
                ir_node* copy = copy_to(get_irn_n(phi, i), pred_block, j);
 
                for (k = 0; k < i; ++k) ins[k] = get_irn_n(phi, k);
@@ -219,10 +226,11 @@ static void split_block(ir_node* block, int i, int j)
        new_pred_arity = get_irn_arity(pred_block) - 1;
        NEW_ARR_A(ir_node*, pred_ins, new_pred_arity);
 
-       for (phi = get_block_blockinfo(pred_block)->phi; phi != NULL; phi = get_irn_link(phi)) {
+       for (phi = get_Block_phis(pred_block); phi != NULL; phi = next) {
                for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(phi, k);
                for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(phi, k + 1);
                assert(k == new_pred_arity);
+               next = get_Phi_next(phi);
                if (new_pred_arity > 1) {
                        set_irn_in(phi, new_pred_arity, pred_ins);
                } else {
@@ -261,23 +269,25 @@ static void prepare_path(ir_node* block, int i, const ir_node* dependency)
        }
 }
 
-
-static void if_conv_walker(ir_node* block, void* env)
+/**
+ * Block walker: Search for diamonds and do the if conversion.
+ */
+static void if_conv_walker(ir_node *block, void *ctx)
 {
-       opt_if_conv_info_t* opt_info = env;
+       walker_env *env = ctx;
        int arity;
        int i;
 
        /* Bail out, if there are no Phis at all */
-       if (get_block_blockinfo(block)->phi == NULL) return;
+       if (get_Block_phis(block) == NULL) return;
 
 restart:
        arity = get_irn_arity(block);
        for (i = 0; i < arity; ++i) {
                ir_node* pred0;
-               cdep* cdep;
+               ir_cdep* cdep;
 
-               pred0 = get_nodes_block(get_irn_n(block, i));
+               pred0 = get_Block_cfgpred_block(block, i);
                for (cdep = find_cdep(pred0); cdep != NULL; cdep = cdep->next) {
                        const ir_node* dependency = cdep->node;
                        ir_node* projx0 = walk_to_projx(pred0, dependency);
@@ -287,19 +297,24 @@ restart:
                        if (projx0 == NULL) continue;
 
                        cond = get_Proj_pred(projx0);
-                       if (get_irn_op(cond) != op_Cond) continue;
+                       if (! is_Cond(cond))
+                               continue;
 
                        /* We only handle boolean decisions, no switches */
                        if (get_irn_mode(get_Cond_selector(cond)) != mode_b) continue;
 
                        for (j = i + 1; j < arity; ++j) {
                                ir_node* projx1;
-                               ir_node* conds[1];
-                               ir_node* psi_block;
+                               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_nodes_block(get_irn_n(block, j));
+                               pred1 = get_Block_cfgpred_block(block, j);
 
                                if (!is_cdep_on(pred1, dependency)) continue;
 
@@ -307,82 +322,103 @@ restart:
 
                                if (projx1 == NULL) continue;
 
-                               phi = get_block_blockinfo(block)->phi;
-                               if (!opt_info->allow_ifconv(get_Cond_selector(cond), phi, i, j)) continue;
+                               sel = get_Cond_selector(cond);
+                               phi = get_Block_phis(block);
+                               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 = true;
                                prepare_path(block, i, dependency);
                                prepare_path(block, j, dependency);
                                arity = get_irn_arity(block);
 
-                               conds[0] = get_Cond_selector(cond);
-
-                               psi_block = get_nodes_block(cond);
+                               mux_block = get_nodes_block(cond);
+                               cond_dbg = get_irn_dbg_info(cond);
                                do {
                                        ir_node* val_i = get_irn_n(phi, i);
                                        ir_node* val_j = get_irn_n(phi, j);
-                                       ir_node* psi;
+                                       ir_node* mux;
                                        ir_node* next_phi;
 
                                        if (val_i == val_j) {
-                                               psi = val_i;
-                                               DB((dbg, LEVEL_2,  "Generating no psi, because both values are equal\n"));
+                                               mux = val_i;
+                                               DB((dbg, LEVEL_2,  "Generating no Mux, because both values are equal\n"));
                                        } else {
-                                               ir_node* vals[2];
+                                               ir_node *t, *f;
 
                                                /* Something is very fishy if two predecessors of a PhiM point into
                                                 * one block, but not at the same memory node
                                                 */
                                                assert(get_irn_mode(phi) != mode_M);
-                                               if (get_Proj_proj(projx0) == pn_Cond_true) {
-                                                       vals[0] = val_i;
-                                                       vals[1] = val_j;
+                                               if (negated) {
+                                                       t = val_j;
+                                                       f = val_i;
                                                } else {
-                                                       vals[0] = val_j;
-                                                       vals[1] = val_i;
+                                                       t = val_i;
+                                                       f = val_j;
                                                }
 
-                                               psi = new_r_Psi(
-                                                       current_ir_graph, psi_block, 1, conds, vals, get_irn_mode(phi)
-                                               );
-                                               DB((dbg, LEVEL_2, "Generating %+F for %+F\n", psi, 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));
                                        }
 
-                                       next_phi = get_irn_link(phi);
+                                       next_phi = get_Phi_next(phi);
 
                                        if (arity == 2) {
-                                               exchange(phi, psi);
+                                               exchange(phi, mux);
                                        } else {
-                                               rewire(phi, i, j, psi);
+                                               rewire(phi, i, j, mux);
                                        }
-
                                        phi = next_phi;
                                } while (phi != NULL);
 
-                               exchange(get_nodes_block(get_irn_n(block, i)), psi_block);
-                               exchange(get_nodes_block(get_irn_n(block, j)), psi_block);
+                               exchange(get_nodes_block(get_irn_n(block, i)), mux_block);
+                               exchange(get_nodes_block(get_irn_n(block, j)), mux_block);
 
                                if (arity == 2) {
+                                       unsigned mark;
 #if 1
-                                       DB((dbg, LEVEL_1,  "Welding block %+F and %+F\n", block, psi_block));
-                                       /* copy the block-info from the Psi-block to the block before merging */
-                                       get_block_blockinfo(psi_block)->has_pinned |= get_block_blockinfo(block)->has_pinned;
-                                       set_irn_link(block, get_irn_link(psi_block));
-
-                                       set_irn_in(block, get_irn_arity(psi_block), get_irn_in(psi_block) + 1);
-                                       exchange_cdep(psi_block, block);
-                                       exchange(psi_block, block);
+                                       DB((dbg, LEVEL_1,  "Welding block %+F and %+F\n", block, mux_block));
+                                       /* copy the block-info from the Mux-block to the block before merging */
+
+                                       mark =  get_Block_mark(mux_block) | get_Block_mark(block);
+                                       set_Block_mark(block, mark);
+                                       set_Block_phis(block, get_Block_phis(mux_block));
+
+                                       set_irn_in(block, get_irn_arity(mux_block), get_irn_in(mux_block) + 1);
+                                       exchange_cdep(mux_block, block);
+                                       exchange(mux_block, block);
 #else
-                                       DB((dbg, LEVEL_1,  "Welding block %+F to %+F\n", block, psi_block));
-                                       get_block_blockinfo(psi_block)->has_pinned |=   get_block_blockinfo(block)->has_pinned;
-                                       exchange(block, psi_block);
+                                       DB((dbg, LEVEL_1,  "Welding block %+F to %+F\n", block, mux_block));
+                                       mark =  get_Block_mark(mux_block) | get_Block_mark(block);
+                                       /* mark both block just to be sure, should be enough to mark mux_block */
+                                       set_Block_mark(mux_block, mark);
+                                       exchange(block, mux_block);
 #endif
                                        return;
                                } else {
-                                       rewire(block, i, j, new_r_Jmp(current_ir_graph, psi_block));
+                                       rewire(block, i, j, new_r_Jmp(mux_block));
                                        goto restart;
                                }
                        }
@@ -391,22 +427,19 @@ restart:
 }
 
 /**
- * Block walker: add additional data
+ * Block walker: clear block marks and Phi lists.
  */
 static void init_block_link(ir_node *block, void *env)
 {
-       struct obstack *obst = env;
-       block_info *bi = obstack_alloc(obst, sizeof(*bi));
-
-       bi->phi = NULL;
-       bi->has_pinned = 0;
-       set_irn_link(block, bi);
+       (void)env;
+       set_Block_mark(block, 0);
+       set_Block_phis(block, NULL);
 }
 
 
 /**
- * Daisy-chain all phis in a block
- * If a non-movable node is encountered set the has_pinned flag
+ * 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)
 {
@@ -414,246 +447,31 @@ static void collect_phis(ir_node *node, void *env)
 
        if (is_Phi(node)) {
                ir_node *block = get_nodes_block(node);
-               block_info *bi = get_block_blockinfo(block);
 
-               set_irn_link(node, bi->phi);
-               bi->phi = node;
+               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, these will be removed.
-                        * This ignores Raise. That is surely bad. FIXME.
+                        * Ignore control flow nodes (except Raise), these will be removed.
                         */
-                       if (!is_cfop(node)) {
+                       if (!is_cfop(node) && !is_Raise(node)) {
                                ir_node *block = get_nodes_block(node);
-                               block_info *bi = get_block_blockinfo(block);
 
                                DB((dbg, LEVEL_2, "Node %+F in block %+F is unmovable\n", node, block));
-                               bi->has_pinned = 1;
+                               set_Block_mark(block, 1);
                        }
                }
        }
 }
 
-
-/*
- * Transform multiple cascaded Psis into one Psi
- */
-static ir_node* fold_psi(ir_node* psi)
+void opt_if_conv(ir_graph *irg)
 {
-       int arity = get_Psi_n_conds(psi);
-       int new_arity = 0;
-       int i;
-       ir_node* n;
-       ir_node** conds;
-       ir_node** vals;
-       int j;
-       ir_node* new_psi;
-
-       for (i = 0; i < arity; ++i) {
-               n = get_Psi_val(psi, i);
-               if (get_irn_op(n) == op_Psi) {
-                       new_arity += get_Psi_n_conds(n) + 1;
-               } else {
-                       ++new_arity;
-               }
-       }
-       n = get_Psi_default(psi);
-       if (get_irn_op(n) == op_Psi) {
-               new_arity += get_Psi_n_conds(n);
-       }
-
-       if (arity == new_arity) return psi; // no attached Psis found
-       DB((dbg, LEVEL_1, "Folding %+F from %d to %d conds\n", psi, arity, new_arity));
-
-       NEW_ARR_A(ir_node *, conds, new_arity);
-       NEW_ARR_A(ir_node *, vals, new_arity + 1);
-       j = 0;
-       for (i = 0; i < arity; ++i) {
-               ir_node* c = get_Psi_cond(psi, i);
-
-               n = get_Psi_val(psi, i);
-               if (get_irn_op(n) == op_Psi) {
-                       int a = get_Psi_n_conds(n);
-                       int k;
-
-                       for (k = 0; k < a; ++k) {
-                               conds[j] = new_r_And(
-                                       current_ir_graph, get_nodes_block(psi),
-                                       c, get_Psi_cond(n, k), mode_b
-                               );
-                               vals[j] = get_Psi_val(n, k);
-                               ++j;
-                       }
-                       conds[j] = c;
-                       vals[j] = get_Psi_default(n);
-               } else {
-                       conds[j] = c;
-                       vals[j] = n;
-               }
-               ++j;
-       }
-       n = get_Psi_default(psi);
-       if (get_irn_op(n) == op_Psi) {
-               int a = get_Psi_n_conds(n);
-               int k;
-
-               for (k = 0; k < a; ++k) {
-                       conds[j] = get_Psi_cond(n, k);
-                       vals[j] = get_Psi_val(n, k);
-                       ++j;
-               }
-               vals[j] = get_Psi_default(n);
-       } else {
-               vals[j] = n;
-       }
-       assert(j == new_arity);
-       new_psi = new_r_Psi(
-               current_ir_graph, get_nodes_block(psi),
-               new_arity, conds, vals, get_irn_mode(psi)
-       );
-       DB((dbg, LEVEL_1, "Folded %+F into new %+F\n", psi, new_psi));
-       exchange(psi, new_psi);
-       return new_psi;
-}
-
-
-/*
- * Merge consecutive psi inputs if the data inputs are the same
- */
-static ir_node* meld_psi(ir_node* psi)
-{
-       int arity = get_Psi_n_conds(psi);
-       int new_arity;
-       ir_node** conds;
-       ir_node** vals;
-       ir_node* cond;
-       ir_node* val;
-       int i;
-       int j;
-       ir_node* new_psi;
-
-       new_arity = 1;
-       val = get_Psi_val(psi, 0);
-       DB((dbg, LEVEL_1, "Pred  0 of %+F is %+F\n", psi, val));
-       for (i = 1; i < arity; ++i) {
-               ir_node* v = get_Psi_val(psi, i);
-               DB((dbg, LEVEL_1, "Pred %2d of %+F is %+F\n", i, psi, v));
-               if (val != v) {
-                       val = v;
-                       ++new_arity;
-               }
-       }
-       DB((dbg, LEVEL_1, "Default of %+F is %+F\n", psi, get_Psi_default(psi)));
-       if (val == get_Psi_default(psi)) --new_arity;
-
-       DB((dbg, LEVEL_1, "Melding Psi %+F from %d conds to %d\n", psi, arity, new_arity));
-
-       if (new_arity == arity) return psi;
-
-       /* If all data inputs of the Psi are equal, exchange the Psi with that value */
-       if (new_arity == 0) {
-               exchange(psi, val);
-               return val;
-       }
-
-       NEW_ARR_A(ir_node *, conds, new_arity);
-       NEW_ARR_A(ir_node *, vals, new_arity + 1);
-       cond = get_Psi_cond(psi, 0);
-       val = get_Psi_val(psi, 0);
-       j = 0;
-       for (i = 1; i < arity; ++i) {
-               ir_node* v = get_Psi_val(psi, i);
-
-               if (v == val) {
-                       cond = new_r_Or(
-                               current_ir_graph, get_nodes_block(psi),
-                               cond, get_Psi_cond(psi, i), mode_b
-                       );
-               } else {
-                       conds[j] = cond;
-                       vals[j] = val;
-                       ++j;
-                       val = v;
-               }
-       }
-       if (val != get_Psi_default(psi)) {
-               conds[j] = cond;
-               vals[j] = val;
-               ++j;
-       }
-       vals[j] = get_Psi_default(psi);
-       assert(j == new_arity);
-       new_psi = new_r_Psi(
-               current_ir_graph, get_nodes_block(psi),
-               new_arity, conds, vals, get_irn_mode(psi)
-       );
-       DB((dbg, LEVEL_1, "Molded %+F into %+F\n", psi, new_psi));
-       exchange(psi, new_psi);
-       return new_psi;
-}
-
-
-/**
- * Split a Psi with multiple conditions into multiple Psis with one condtition
- * each
- */
-static ir_node* split_psi(ir_node* psi)
-{
-       int arity = get_Psi_n_conds(psi);
-       ir_mode* mode;
-       ir_node* block;
-       ir_node* rval;
-       int i;
-
-       if (arity == 1) return psi;
-
-       mode  = get_irn_mode(psi);
-       block = get_nodes_block(psi);
-       rval  = get_Psi_default(psi);
-       for (i = arity - 1; i >= 0; --i) {
-               ir_node* conds[1];
-               ir_node* vals[2];
-
-               conds[0] = get_Psi_cond(psi, i);
-               vals[0]  = get_Psi_val(psi, i);
-               vals[1]  = rval;
-               rval     = new_r_Psi(
-                       current_ir_graph, block, 1, conds, vals, mode
-               );
-       }
-       exchange(psi, rval);
-       return rval;
-}
-
-
-static void optimise_psis(ir_node* node, void* env)
-{
-       (void) env;
-
-       if (get_irn_op(node) != op_Psi) return;
-#if 1
-       node = fold_psi(node);
-#endif
-#if 1
-       node = meld_psi(node);
-#endif
-#if 1
-       node = split_psi(node);
-#endif
-}
-
-
-void opt_if_conv(ir_graph *irg, const opt_if_conv_info_t *params)
-{
-       struct obstack obst;
-       opt_if_conv_info_t p;
-
-       if (! get_opt_if_conversion())
-               return;
+       walker_env            env;
+       const backend_params *be_params = be_get_backend_param();
 
        /* get the parameters */
-       p = (params != NULL ? *params : default_info);
+       env.allow_ifconv = be_params->allow_ifconv;
+       env.changed      = false;
 
        FIRM_DBG_REGISTER(dbg, "firm.opt.ifconv");
 
@@ -663,19 +481,29 @@ void opt_if_conv(ir_graph *irg, const opt_if_conv_info_t *params)
        remove_critical_cf_edges(irg);
 
        compute_cdep(irg);
-       assure_doms(irg);
 
-       obstack_init(&obst);
-       irg_block_walk_graph(irg, init_block_link, NULL, &obst);
+       ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
+
+       irg_block_walk_graph(irg, init_block_link, NULL, NULL);
        irg_walk_graph(irg, collect_phis, NULL, NULL);
-       irg_block_walk_graph(irg, NULL, if_conv_walker, &p);
+       irg_block_walk_graph(irg, NULL, if_conv_walker, &env);
 
-       local_optimize_graph(irg);
+       ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
 
-       irg_walk_graph(irg, NULL, optimise_psis, NULL);
+       if (env.changed) {
+               local_optimize_graph(irg);
 
-       obstack_free(&obst, NULL);
+               /* graph has changed, invalidate analysis info */
+               set_irg_outs_inconsistent(irg);
+               set_irg_extblk_inconsistent(irg);
+               set_irg_loopinfo_inconsistent(irg);
+               set_irg_doms_inconsistent(irg);
+       }
 
-       free_dom(irg);
        free_cdep(irg);
 }
+
+ir_graph_pass_t *opt_if_conv_pass(const char *name)
+{
+       return def_graph_pass(name ? name : "ifconv", opt_if_conv);
+}