Moved some ia32 independent code to bemain.
[libfirm] / ir / ana / irdom.c
index 789d8c3..f485c9c 100644 (file)
  * @date      2.2002
  * @version   $Id$
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
-#ifdef HAVE_STRING_H
 #include <string.h>
-#endif
 
 #include "irouts.h"
 
@@ -360,7 +356,7 @@ void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
        /* The root of the dominator tree should be the Start block. */
        ir_node *root = get_irg_start_block(irg);
 
-  assert(irg->dom_state == dom_consistent
+       assert(irg->dom_state == dom_consistent
                        && "The dominators of the irg must be consistent");
        assert(root && "The start block of the graph is NULL?");
        assert(get_dom_info(root)->idom == NULL
@@ -444,15 +440,13 @@ static void assign_tree_postdom_pre_order_max(ir_node *bl, void *data)
  * count the number of blocks and clears the post dominance info
  */
 static void count_and_init_blocks_pdom(ir_node *bl, void *env) {
-       if (is_Block(bl)) {
-               int *n_blocks = (int *) env;
-               (*n_blocks) ++;
-
-               memset(get_pdom_info(bl), 0, sizeof(ir_dom_info));
-               set_Block_ipostdom(bl, NULL);
-               set_Block_postdom_pre_num(bl, -1);
-               set_Block_postdom_depth(bl, -1);
-       }
+       int *n_blocks = (int *) env;
+       (*n_blocks) ++;
+
+       memset(get_pdom_info(bl), 0, sizeof(ir_dom_info));
+       set_Block_ipostdom(bl, NULL);
+       set_Block_postdom_pre_num(bl, -1);
+       set_Block_postdom_depth(bl, -1);
 }
 
 /** temporary type used while constructing the dominator / post dominator tree. */
@@ -509,7 +503,11 @@ static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
        /* Iterate */
        for (i = get_Block_n_cfg_outs_ka(bl) - 1; i >= 0; --i) {
                ir_node *pred = get_Block_cfg_out_ka(bl, i);
-               assert(is_Block(pred));
+               /* can happen for half-optimized dead code (I've seen this in student
+                  projects */
+               if (!is_Block(pred))
+                       continue;
+
                init_tmp_dom_info(pred, tdi, tdi_list, used, n_blocks);
        }
 }
@@ -581,14 +579,14 @@ static void dom_compress(tmp_dom_info *v) {
  * if V is a root, return v, else return the vertex u, not being the
  * root, with minimum u->semi on the path from v to its root.
  */
-INLINE static tmp_dom_info *dom_eval(tmp_dom_info *v) {
+inline static tmp_dom_info *dom_eval(tmp_dom_info *v) {
        if (!v->ancestor) return v;
        dom_compress (v);
        return v->label;
 }
 
 /** make V W's ancestor */
-INLINE static void dom_link(tmp_dom_info *v, tmp_dom_info *w) {
+inline static void dom_link(tmp_dom_info *v, tmp_dom_info *w) {
        w->ancestor = v;
 }
 
@@ -596,15 +594,13 @@ INLINE static void dom_link(tmp_dom_info *v, tmp_dom_info *w) {
  * Walker: count the number of blocks and clears the dominance info
  */
 static void count_and_init_blocks_dom(ir_node *bl, void *env) {
-       if (is_Block(bl)) {
-               int *n_blocks = (int *) env;
-               (*n_blocks) ++;
-
-               memset(get_dom_info(bl), 0, sizeof(ir_dom_info));
-               set_Block_idom(bl, NULL);
-               set_Block_dom_pre_num(bl, -1);
-               set_Block_dom_depth(bl, -1);
-       }
+       int *n_blocks = (int *) env;
+       (*n_blocks) ++;
+
+       memset(get_dom_info(bl), 0, sizeof(ir_dom_info));
+       set_Block_idom(bl, NULL);
+       set_Block_dom_pre_num(bl, -1);
+       set_Block_dom_depth(bl, -1);
 }
 
 /**
@@ -612,21 +608,53 @@ static void count_and_init_blocks_dom(ir_node *bl, void *env) {
  *
  * - count the number of blocks
  * - clear the dominance info
+ * - remove Block-keepalives of live blocks to reduce
+ *   the number of "phantom" block edges
  *
  * @param irg  the graph
  * @param pre  a walker function that will be called for every block in the graph
  */
 static int init_construction(ir_graph *irg, irg_walk_func *pre) {
+       ir_graph *rem = current_ir_graph;
+       ir_node *end;
+       int arity;
        int n_blocks = 0;
 
-       /*
-        * Normally one would use irg_block_walk_graph() here, however, this does NOT
-        * guarantee that all UNREACHABLE blocks are visited.
-        * This could led to wrong dominance info in those blocks, causing
-        * the verifier to crash for instance.
-        * So, we visit EVERY node to ensure the info is updated.
-        */
-       irg_walk_graph(irg, pre, NULL, &n_blocks);
+       current_ir_graph = irg;
+
+       /* this visits only the reachable blocks */
+       irg_block_walk(get_irg_end_block(irg), pre, NULL, &n_blocks);
+
+       /* now visit the unreachable (from End) Blocks and remove unnecessary keep-alives */
+       end   = get_irg_end(irg);
+       arity = get_End_n_keepalives(end);
+       if (arity) {    /* we have keep-alives */
+               ir_node **in;
+               int i, j;
+
+               NEW_ARR_A(ir_node *, in, arity);
+               for (i = j = 0; i < arity; i++) {
+                       ir_node *pred = get_End_keepalive(end, i);
+
+                       if (!is_Block(pred)) {
+                               pred = get_nodes_block(pred);
+                               if (!is_Block(pred)) {
+                                       /* a node which has a bad block input: kill it */
+                                       continue;
+                               }
+                       }
+                       dec_irg_block_visited(irg);
+                       irg_block_walk(pred, pre, NULL, &n_blocks);
+                       in[j++] = pred;
+               }
+               if (j != arity) {
+                       /* we kill some keep-alives */
+                       set_End_keepalives(end, j, in);
+                       set_irg_outs_inconsistent(irg);
+               }
+       }
+
+       current_ir_graph = rem;
        return n_blocks;
 }