Cond nodes are a kind of cf node, too. Therefore also ignore them when copying block...
[libfirm] / ir / opt / condeval.c
index fea5db6..d4dd6f8 100644 (file)
@@ -1,12 +1,28 @@
 /*
- * Project:     libFIRM
- * File name:   ir/opt/cfopt.c
- * Purpose:     Partial condition evaluation
- * Author:      Christoph Mallon, Matthias Braun
- * Created:     10. Sep. 2006
- * CVS-ID:      $Id$
- * Copyright:   (c) 1998-2006 Universität Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ * Copyright (C) 1995-2007 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.
+ */
+
+/**
+ * @file
+ * @brief   Partial condition evaluation
+ * @date    10. Sep. 2006
+ * @author  Christoph Mallon, Matthias Braun
+ * @version $Id$
  */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -49,42 +65,6 @@ static void add_pred(ir_node* node, ir_node* x)
        set_irn_in(node, n + 1, ins);
 }
 
-/**
- * Remove predecessor j from node, which is either a Block or a Phi
- * returns true if only one predecessor is left
- */
-static int remove_pred(ir_node* node, int j)
-{
-       int n;
-
-       assert(is_Block(node) || is_Phi(node));
-
-       n = get_irn_arity(node);
-       if (n == 2) {
-               ir_node* pred = get_irn_n(node, 1 - j);
-
-               if (is_Block(node)) {
-                       pred = get_nodes_block(pred);
-                       edges_reroute(node, pred, current_ir_graph);
-               } else {
-                       exchange(node, pred);
-               }
-               return 1;
-       } else {
-               ir_node** ins;
-               int i;
-
-               NEW_ARR_A(ir_node*, ins, n - 1);
-               for (i = 0; i < j; i++)
-                       ins[i] = get_irn_n(node, i);
-               for (i++; i < n; i++)
-                       ins[i - 1] = get_irn_n(node, i);
-
-               set_irn_in(node, n - 1, ins);
-               return 0;
-       }
-}
-
 static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
 {
        int i;
@@ -230,7 +210,7 @@ static void copy_and_fix(ir_node *block, ir_node *copy_block, int j, const conde
                ir_mode *mode = get_irn_mode(node);
 
                /* ignore control flow */
-               if (mode == mode_X)
+               if (mode == mode_X || is_Cond(node))
                        continue;
                /* we may not copy mode_b nodes, because this could produce phi with mode_bs which can't
                   be handled in all backends. Instead we duplicate the node and move it to it's users */
@@ -289,7 +269,7 @@ static void copy_and_fix(ir_node *block, ir_node *copy_block, int j, const conde
                ir_node *node = get_edge_src_irn(edge);
                ir_mode *mode = get_irn_mode(node);
 
-               if (mode == mode_X)
+               if (mode == mode_X || is_Cond(node))
                        continue;
                if (mode == mode_b)
                        continue;
@@ -382,7 +362,7 @@ static ir_node *find_phi_with_const(ir_node *jump, ir_node *value, condeval_env_
 
 
 /**
- * Block-walker: searchs for the following construct
+ * Block-walker: searches for the following construct
  *
  *  Const or Phi with constants
  *           |
@@ -489,46 +469,53 @@ static void cond_eval(ir_node* block, void* data)
                         * jumps into the true_block. We also have to shorten phis
                         * in our block because of this */
                        const ir_edge_t *edge, *next;
+                       ir_node* bad = new_Bad();
+                       size_t cnst_pos = env.cnst_pos;
 
                        /* shorten phis */
                        foreach_out_edge_safe(env.cnst_pred, edge, next) {
                                ir_node *node = get_edge_src_irn(edge);
 
                                if(is_Phi(node))
-                                       remove_pred(node, env.cnst_pos);
+                                       set_Phi_pred(node, cnst_pos, bad);
                        }
 
-                       remove_pred(env.cnst_pred, env.cnst_pos);
+                       set_Block_cfgpred(env.cnst_pred, cnst_pos, bad);
 
-                       // the graph is changed now
+                       /* the graph is changed now */
                        *changed = 1;
-                       set_irg_doms_inconsistent(irg);
-                       set_irg_extblk_inconsistent(irg);
-                       set_irg_loopinfo_inconsistent(irg);
                }
        }
 }
 
 void opt_cond_eval(ir_graph* irg)
 {
-       int changed;
+       int changed, rerun;
 
        FIRM_DBG_REGISTER(dbg, "firm.opt.condeval");
 
        DB((dbg, LEVEL_1, "===> Performing condition evaluation on %+F\n", irg));
 
-       edges_assure(irg);
        remove_critical_cf_edges(irg);
-
        normalize_proj_nodes(irg);
 
+       edges_assure(irg);
        set_using_irn_link(irg);
        set_using_visited(irg);
 
+       changed = 0;
        do {
-               changed = 0;
-               irg_block_walk_graph(irg, cond_eval, NULL, &changed);
-       } while(changed);
+               rerun = 0;
+               irg_block_walk_graph(irg, cond_eval, NULL, &rerun);
+               changed |= rerun;
+       } while (rerun);
+
+       if (changed) {
+               /* control flow changed, some blocks may become dead */
+               set_irg_doms_inconsistent(irg);
+               set_irg_extblk_inconsistent(irg);
+               set_irg_loopinfo_inconsistent(irg);
+       }
 
        clear_using_visited(irg);
        clear_using_irn_link(irg);