convert opts to use the opt_manage framework
[libfirm] / ir / opt / combo.c
index 384617c..0ae4337 100644 (file)
@@ -84,6 +84,7 @@
 #include "irpass.h"
 #include "tv_t.h"
 #include "irtools.h"
+#include "opt_manage.h"
 
 #include "irprintf.h"
 #include "irdump.h"
@@ -780,7 +781,8 @@ static void init_block_phis(ir_node *irn, void *ctx)
        (void) ctx;
 
        if (is_Phi(irn)) {
-               add_Block_phi(get_nodes_block(irn), irn);
+               ir_node *block = get_nodes_block(irn);
+               add_Block_phi(block, irn);
        }
 }  /* init_block_phis */
 
@@ -845,7 +847,7 @@ static void add_to_cprop(node_t *y, environment_t *env)
        irn = y->node;
        if (get_irn_mode(irn) == mode_T) {
                /* mode_T nodes always produce tarval_bottom, so we must explicitly
-                  add it's Proj's to get constant evaluation to work */
+                * add its Projs to get constant evaluation to work */
                int i;
 
                for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
@@ -1813,7 +1815,7 @@ static void split_by(partition_t *X, environment_t *env)
        dump_partition("split_by", X);
 
        if (X->n_leader == 1) {
-               /* we have only one leader, no need to split, just check it's type */
+               /* we have only one leader, no need to split, just check its type */
                node_t *x = get_first_node(X);
                X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
                return;
@@ -2257,11 +2259,13 @@ static void compute_Cmp(node_t *node)
        } else if (r->part == l->part && !mode_is_float(get_irn_mode(l->node))) {
                tv = relation & ir_relation_equal ? tarval_b_true : tarval_b_false;
 
-               /* if the node was ONCE evaluated by all constants, but now
+               /* if the node was ONCE evaluated to a constant, but now
                   this breaks AND we get from the argument partitions a different
-                  result, switch to bottom.
+                  result, ensure monotony by fall to bottom.
                   This happens because initially all nodes are in the same partition ... */
-               if (node->type.tv != tv)
+               if (node->type.tv == tarval_bottom)
+                       tv = tarval_bottom;
+               else if (node->type.tv != tv && is_constant_type(node->type))
                        tv = tarval_bottom;
                node->type.tv = tv;
        } else {
@@ -2972,6 +2976,11 @@ static int can_exchange(ir_node *pred, ir_node *block)
                return 0;
        else if (is_Jmp(pred))
                return 1;
+       else if (is_Raise(pred)) {
+               /* Raise is a tuple and usually has only one reachable ProjX,
+                * but it must not be eliminated like a Jmp */
+               return 0;
+       }
        else if (get_irn_mode(pred) == mode_T) {
                /* if the predecessor block has more than one
                   reachable outputs we cannot remove the block */
@@ -3001,31 +3010,29 @@ static void apply_cf(ir_node *block, void *ctx)
                        ir_node *pred = get_Block_cfgpred(block, i);
 
                        if (! is_Bad(pred)) {
-                               node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
-
-                               if (pred_bl->flagged == 0) {
-                                       pred_bl->flagged = 3;
-
-                                       if (pred_bl->type.tv == tarval_reachable) {
-                                               /*
-                                                * We will remove an edge from block to its pred.
-                                                * This might leave the pred block as an endless loop
-                                                */
-                                               if (! is_backedge(block, i))
-                                                       keep_alive(pred_bl->node);
+                               ir_node *pred_block = get_nodes_block(skip_Proj(pred));
+                               if (!is_Bad(pred_block)) {
+                                       node_t *pred_bl = get_irn_node(pred_block);
+
+                                       if (pred_bl->flagged == 0) {
+                                               pred_bl->flagged = 3;
+
+                                               if (pred_bl->type.tv == tarval_reachable) {
+                                                       /*
+                                                        * We will remove an edge from block to its pred.
+                                                        * This might leave the pred block as an endless loop
+                                                        */
+                                                       if (! is_backedge(block, i))
+                                                               keep_alive(pred_bl->node);
+                                               }
                                        }
                                }
                        }
                }
 
-               /* the EndBlock is always reachable even if the analysis
-                  finds out the opposite :-) */
-               if (block != get_irg_end_block(current_ir_graph)) {
-                       /* mark dead blocks */
-                       set_Block_dead(block);
-                       DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
-               } else {
-                       /* the endblock is unreachable */
+               if (block == get_irg_end_block(current_ir_graph)) {
+                       /* Analysis found out that the end block is unreachable,
+                        * hence we remove all its control flow predecessors. */
                        set_irn_in(block, 0, NULL);
                }
                return;
@@ -3057,18 +3064,21 @@ static void apply_cf(ir_node *block, void *ctx)
                } else {
                        DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
                        if (! is_Bad(pred)) {
-                               node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
-
-                               if (pred_bl->flagged == 0) {
-                                       pred_bl->flagged = 3;
-
-                                       if (pred_bl->type.tv == tarval_reachable) {
-                                               /*
-                                                * We will remove an edge from block to its pred.
-                                                * This might leave the pred block as an endless loop
-                                                */
-                                               if (! is_backedge(block, i))
-                                                       keep_alive(pred_bl->node);
+                               ir_node *pred_block = get_nodes_block(skip_Proj(pred));
+                               if (!is_Bad(pred_block)) {
+                                       node_t *pred_bl = get_irn_node(pred_block);
+
+                                       if (!is_Bad(pred_bl->node) && pred_bl->flagged == 0) {
+                                               pred_bl->flagged = 3;
+
+                                               if (pred_bl->type.tv == tarval_reachable) {
+                                                       /*
+                                                        * We will remove an edge from block to its pred.
+                                                        * This might leave the pred block as an endless loop
+                                                        */
+                                                       if (! is_backedge(block, i))
+                                                               keep_alive(pred_bl->node);
+                                               }
                                        }
                                }
                        }
@@ -3242,7 +3252,9 @@ static void apply_result(ir_node *irn, void *ctx)
                node_t *block = get_irn_node(get_nodes_block(irn));
 
                if (block->type.tv == tarval_unreachable) {
-                       ir_node *bad = get_irg_bad(current_ir_graph);
+                       ir_graph *irg  = get_irn_irg(irn);
+                       ir_mode  *mode = get_irn_mode(node->node);
+                       ir_node  *bad  = new_r_Bad(irg, mode);
 
                        /* here, bad might already have a node, but this can be safely ignored
                           as long as bad has at least ONE valid node */
@@ -3397,13 +3409,22 @@ static void apply_end(ir_node *end, environment_t *env)
 
        /* fix the keep alive */
        for (i = j = 0; i < n; i++) {
-               ir_node *ka   = get_End_keepalive(end, i);
-               node_t  *node = get_irn_node(ka);
+               ir_node *ka = get_End_keepalive(end, i);
+               ir_node *block;
+               node_t  *node;
 
-               if (! is_Block(ka))
-                       node = get_irn_node(get_nodes_block(ka));
+               if (is_Bad(ka))
+                       continue;
+               if (!is_Block(ka)) {
+                       block = get_nodes_block(ka);
+                       if (is_Bad(block))
+                               continue;
+               } else {
+                       block = ka;
+               }
 
-               if (node->type.tv != tarval_unreachable && !is_Bad(ka))
+               node = get_irn_node(block);
+               if (node->type.tv != tarval_unreachable)
                        in[j++] = ka;
        }
        if (j != n) {
@@ -3471,7 +3492,7 @@ static void add_memory_keeps(ir_node **kept_memory, size_t len)
        ir_nodeset_destroy(&set);
 }  /* add_memory_keeps */
 
-void combo(ir_graph *irg)
+static ir_graph_state_t do_combo(ir_graph *irg)
 {
        environment_t env;
        ir_node       *initial_bl;
@@ -3504,9 +3525,6 @@ void combo(ir_graph *irg)
        env.commutative    = 1;
        env.opt_unknown    = 1;
 
-       assure_irg_outs(irg);
-       assure_cf_loop(irg);
-
        /* we have our own value_of function */
        set_value_of_func(get_node_tarval);
 
@@ -3571,15 +3589,6 @@ void combo(ir_graph *irg)
                DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
        }
 
-       if (env.modified) {
-               /* control flow might changed */
-               set_irg_outs_inconsistent(irg);
-               set_irg_extblk_inconsistent(irg);
-               set_irg_doms_inconsistent(irg);
-               set_irg_loopinfo_inconsistent(irg);
-               set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
-       }
-
        ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
 
        /* remove the partition hook */
@@ -3592,8 +3601,21 @@ void combo(ir_graph *irg)
        /* restore value_of() default behavior */
        set_value_of_func(NULL);
        current_ir_graph = rem;
+
+       return 0; // cannot guarantee anything
 }  /* combo */
 
+optdesc_t opt_combo = {
+       "combo",
+       IR_GRAPH_STATE_NO_BAD_BLOCKS | IR_GRAPH_STATE_CONSISTENT_OUTS | IR_GRAPH_STATE_CONSISTENT_LOOPINFO,
+       do_combo,
+};
+
+void combo(ir_graph *irg)
+{
+       perform_irg_optimization(irg, &opt_combo);
+}
+
 /* Creates an ir_graph pass for combo. */
 ir_graph_pass_t *combo_pass(const char *name)
 {