condeval is called jump threading now
[libfirm] / ir / opt / cfopt.c
index 64c60c1..5cddcae 100644 (file)
@@ -102,7 +102,7 @@ static int remove_senseless_conds(ir_node *bl) {
                                ir_node *cond_j = skip_Proj(pred_j);
 
                                if (cond_j == cond_i) {
-                                       ir_node *jmp = new_r_Jmp(current_ir_graph, get_nodes_block(cond_i));
+                                       ir_node *jmp = new_r_Jmp(get_nodes_block(cond_i));
                                        set_irn_n(bl, i, jmp);
                                        set_irn_n(bl, j, new_Bad());
 
@@ -224,8 +224,11 @@ static void remove_unreachable_blocks_and_conds(ir_node *block, void *env) {
 
        *changed |= remove_senseless_conds(block);
 
-       /* clear the block mark of all blocks */
-       set_Block_removable(block);
+       /* clear the block mark of all non labeled blocks */
+       if (has_Block_entity(block))
+               set_Block_non_removable(block);
+       else
+               set_Block_removable(block);
 }
 
 /**
@@ -241,7 +244,7 @@ static void collect_nodes(ir_node *n, void *ctx) {
 
        if (code == iro_Block) {
                /* mark the block as non-removable if it is labeled */
-               if (has_Block_label(n))
+               if (has_Block_entity(n))
                        set_Block_non_removable(n);
        } else {
                ir_node *b = get_nodes_block(n);
@@ -559,7 +562,7 @@ static void optimize_blocks(ir_node *b, void *ctx) {
                        /* case 1: Do nothing */
                } else if (is_Block_removable(pred) && !Block_block_visited(pred)) {
                        /* case 2: It's an empty block and not yet visited. */
-                       assert(get_Block_n_cfgpreds(b) > 1);
+                       assert(get_Block_n_cfgpreds(b) > 1 || has_Block_entity(b));
                        /* Else it should be optimized by equivalent_node. */
                        for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
                                ir_node *pred_X = get_Block_cfgpred(pred, j);
@@ -622,9 +625,9 @@ static int handle_switch_cond(ir_node *cond) {
 
        if (proj2 == NULL) {
                /* this Cond has only one Proj: must be the defProj */
-               assert(get_Cond_defaultProj(cond) == get_Proj_proj(proj1));
+               assert(get_Cond_default_proj(cond) == get_Proj_proj(proj1));
                /* convert it into a Jmp */
-               jmp = new_r_Jmp(current_ir_graph, blk);
+               jmp = new_r_Jmp(blk);
                exchange(proj1, jmp);
                return 1;
        } else if (get_irn_link(proj2) == NULL) {
@@ -635,12 +638,12 @@ static int handle_switch_cond(ir_node *cond) {
                if (tv != tarval_bad) {
                        /* we have a constant switch */
                        long num     = get_tarval_long(tv);
-                       long def_num = get_Cond_defaultProj(cond);
+                       long def_num = get_Cond_default_proj(cond);
 
                        if (def_num == get_Proj_proj(proj1)) {
                                /* first one is the defProj */
                                if (num == get_Proj_proj(proj2)) {
-                                       jmp = new_r_Jmp(current_ir_graph, blk);
+                                       jmp = new_r_Jmp(blk);
                                        exchange(proj2, jmp);
                                        exchange(proj1, new_Bad());
                                        return 1;
@@ -648,7 +651,7 @@ static int handle_switch_cond(ir_node *cond) {
                        } else if (def_num == get_Proj_proj(proj2)) {
                                /* second one is the defProj */
                                if (num == get_Proj_proj(proj1)) {
-                                       jmp = new_r_Jmp(current_ir_graph, blk);
+                                       jmp = new_r_Jmp(blk);
                                        exchange(proj1, jmp);
                                        exchange(proj2, new_Bad());
                                        return 1;
@@ -656,12 +659,12 @@ static int handle_switch_cond(ir_node *cond) {
                        } else {
                                /* neither: strange, Cond was not optimized so far */
                                if (num == get_Proj_proj(proj1)) {
-                                       jmp = new_r_Jmp(current_ir_graph, blk);
+                                       jmp = new_r_Jmp(blk);
                                        exchange(proj1, jmp);
                                        exchange(proj2, new_Bad());
                                        return 1;
                                } else if (num == get_Proj_proj(proj2)) {
-                                       jmp = new_r_Jmp(current_ir_graph, blk);
+                                       jmp = new_r_Jmp(blk);
                                        exchange(proj2, jmp);
                                        exchange(proj1, new_Bad());
                                        return 1;
@@ -689,7 +692,7 @@ static int handle_switch_cond(ir_node *cond) {
  * phase.
  */
 void optimize_cf(ir_graph *irg) {
-       int i, j, n;
+       int i, j, n, changed;
        ir_node **in = NULL;
        ir_node *cond, *end = get_irg_end(irg);
        ir_graph *rem = current_ir_graph;
@@ -719,6 +722,7 @@ restart:
        irg_block_walk_graph(irg, NULL, remove_unreachable_blocks_and_conds, &env.changed);
 
        /* fix the keep-alives */
+       changed = 0;
        for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) {
                ir_node *ka = get_End_keepalive(end, i);
 
@@ -726,15 +730,19 @@ restart:
                        /* do NOT keep dead blocks */
                        if (is_Block_dead(ka) || get_Block_dom_depth(ka) < 0) {
                                set_End_keepalive(end, i, new_Bad());
-                               env.changed = 1;
+                               changed = 1;
+                       }
+               } else {
+                       ir_node *block = get_nodes_block(ka);
+
+                       if (is_Bad(block) || is_Block_dead(block) || get_Block_dom_depth(block) < 0) {
+                               /* do NOT keep nodes in dead blocks */
+                               set_End_keepalive(end, i, new_Bad());
+                               changed = 1;
                        }
-               } else if (is_Block_dead(get_nodes_block(ka)) ||
-                          get_Block_dom_depth(get_nodes_block(ka)) < 0) {
-                       /* do NOT keep nodes in dead blocks */
-                       set_End_keepalive(end, i, new_Bad());
-                       env.changed = 1;
                }
        }
+       env.changed |= changed;
 
        ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
 
@@ -749,6 +757,7 @@ restart:
                set_irg_doms_inconsistent(irg);
                set_irg_extblk_inconsistent(irg);
                set_irg_loopinfo_inconsistent(irg);
+               set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
                env.changed = 0;
        }
 
@@ -768,44 +777,37 @@ restart:
        /* Optimize the standard code. */
        env.changed = 0;
        assure_doms(irg);
-       irg_block_walk(get_irg_end_block(irg), optimize_blocks, remove_simple_blocks, &env);
-
-       /* Walk all keep alives, optimize them if block, add to new in-array
-          for end if useful. */
-       n  = get_End_n_keepalives(end);
-       if (n > 0)
-               NEW_ARR_A(ir_node *, in, n);
+       irg_block_walk_graph(irg, optimize_blocks, remove_simple_blocks, &env);
 
        /* in rare cases a node may be kept alive more than once, use the visited flag to detect this */
        ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
        inc_irg_visited(irg);
 
-       /* fix the keep alive */
-       for (i = j = 0; i < n; i++) {
+       /* fix the keep-alives again */
+       changed = 0;
+       for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) {
                ir_node *ka = get_End_keepalive(end, i);
 
-               if (!irn_visited(ka)) {
-                       if (is_Block(ka)) {
-                               if (!Block_block_visited(ka)) {
-                                       /* irg_block_walk() will increase the block visited flag, but we must visit only
-                                          these blocks that are not visited yet, so decrease it first. */
-                                       set_irg_block_visited(irg, get_irg_block_visited(irg) - 1);
-                                       irg_block_walk(ka, optimize_blocks, remove_simple_blocks, &env.changed);
-                                       mark_irn_visited(ka);
-                                       in[j++] = ka;
-                               }
-                       } else {
-                               mark_irn_visited(ka);
-                               /* don't keep alive dead blocks */
-                               if (!is_Bad(ka) && !is_Block_dead(get_nodes_block(ka)))
-                                       in[j++] = ka;
+               if (is_Block(ka)) {
+                       /* do NOT keep dead blocks */
+                       if (is_Block_dead(ka) || get_Block_dom_depth(ka) < 0) {
+                               set_End_keepalive(end, i, new_Bad());
+                               changed = 1;
+                       }
+               } else {
+                       ir_node *block = get_nodes_block(ka);
+
+                       if (is_Bad(block) || is_Block_dead(block) || get_Block_dom_depth(block) < 0) {
+                               /* do NOT keep nodes in dead blocks */
+                               set_End_keepalive(end, i, new_Bad());
+                               changed = 1;
                        }
                }
        }
-       if (j != n) {
-               set_End_keepalives(end, j, in);
-               env.changed = 1;
-       }
+       env.changed |= changed;
+
+       remove_End_Bads_and_doublets(end);
+
 
        ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_IRN_VISITED);
 
@@ -816,6 +818,7 @@ restart:
                 */
                n = get_End_n_keepalives(end);
                if (n > 0) {
+                       NEW_ARR_A(ir_node *, in, n);
                        if (env.changed) {
                                /* Handle graph state if was changed. */
                                set_irg_outs_inconsistent(irg);
@@ -854,6 +857,7 @@ restart:
                set_irg_doms_inconsistent(irg);
                set_irg_extblk_inconsistent(irg);
                set_irg_loopinfo_inconsistent(irg);
+               set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
        }