Fixed and simplified rot matcher
[libfirm] / ir / ana / irouts.c
index 5ed5023..92a5755 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.
  *
@@ -93,11 +93,11 @@ int get_Block_n_cfg_outs(ir_node *bl) {
 #ifdef DEBUG_libfirm
        assert(bl->out_valid);
 #endif /* defined DEBUG_libfirm */
-  for (i = 1; i <= PTR_TO_INT(bl->out[0]); ++i) {
-    ir_node *succ = bl->out[i];
+       for (i = 1; i <= PTR_TO_INT(bl->out[0]); ++i) {
+               ir_node *succ = bl->out[i];
                if (get_irn_mode(succ) == mode_X && get_irn_op(succ) != op_End)
                        n_cfg_outs += PTR_TO_INT(succ->out[0]);
-  }
+       }
        return n_cfg_outs;
 }
 
@@ -108,17 +108,20 @@ int get_Block_n_cfg_outs_ka(ir_node *bl) {
 #ifdef DEBUG_libfirm
        assert(bl->out_valid);
 #endif /* defined DEBUG_libfirm */
-  for (i = 1; i <= PTR_TO_INT(bl->out[0]); ++i) {
-    ir_node *succ = bl->out[i];
+       for (i = 1; i <= PTR_TO_INT(bl->out[0]); ++i) {
+               ir_node *succ = bl->out[i];
                if (get_irn_mode(succ) == mode_X) {
-                       /* ignore End if we are in the Endblock */
-                       if (get_irn_op(succ) == op_End &&
-                           get_irn_n(succ, -1) == bl)
-                               continue;
-                       else
+
+                       if (get_irn_op(succ) == op_End) {
+                               /* ignore End if we are in the Endblock */
+                               if (get_irn_n(succ, -1) == bl)
+                                       continue;
+                               else /* count Keep-alive as one */
+                                       n_cfg_outs += 1;
+                       } else
                                n_cfg_outs += PTR_TO_INT(succ->out[0]);
                }
-  }
+       }
        return n_cfg_outs;
 }
 
@@ -129,16 +132,16 @@ ir_node *get_Block_cfg_out(ir_node *bl, int pos) {
 #ifdef DEBUG_libfirm
        assert (bl->out_valid);
 #endif /* defined DEBUG_libfirm */
-  for (i = 1; i <= PTR_TO_INT(bl->out[0]); ++i) {
-    ir_node *succ = bl->out[i];
-               if (get_irn_mode(succ) == mode_X  && get_irn_op(succ) != op_End) {
-      int n_outs = PTR_TO_INT(succ->out[0]);
+       for (i = 1; i <= PTR_TO_INT(bl->out[0]); ++i) {
+               ir_node *succ = bl->out[i];
+               if (get_irn_mode(succ) == mode_X && get_irn_op(succ) != op_End) {
+                       int n_outs = PTR_TO_INT(succ->out[0]);
                        if (pos < n_outs)
                                return succ->out[pos + 1];
                        else
                                pos -= n_outs;
                }
-  }
+       }
        return NULL;
 }
 
@@ -149,22 +152,28 @@ ir_node *get_Block_cfg_out_ka(ir_node *bl, int pos) {
 #ifdef DEBUG_libfirm
        assert (bl->out_valid);
 #endif /* defined DEBUG_libfirm */
-  for (i = 1; i <= PTR_TO_INT(bl->out[0]); ++i) {
+       for (i = 1; i <= PTR_TO_INT(bl->out[0]); ++i) {
                ir_node *succ = bl->out[i];
-    if (get_irn_mode(succ) == mode_X) {
-                       /* ignore End if we are in the Endblock */
-                       if (get_irn_op(succ) == op_End &&       get_irn_n(succ, -1) == bl)
-                               continue;
-      n_outs = PTR_TO_INT(succ->out[0]);
-                       if (pos < n_outs) {
-                               /* handle keep-alive here: return the Endblock instead of the End node */
-                               if (get_irn_op(succ) == op_End)
+               if (get_irn_mode(succ) == mode_X) {
+                       if (get_irn_op(succ) == op_End) {
+                               if (get_irn_n(succ, -1) == bl) {
+                                       /* ignore End if we are in the Endblock */
+                                       continue;
+                               }
+                               if (pos == 0) {
+                                       /* handle keep-alive here: return the Endblock instead of the End node */
                                        return get_irn_n(succ, -1);
-                               return succ->out[pos + 1];
-                       } else
-                               pos -= n_outs;
+                               } else
+                                       --pos;
+                       } else {
+                               n_outs = PTR_TO_INT(succ->out[0]);
+                               if (pos < n_outs)
+                                       return succ->out[pos + 1];
+                               else
+                                       pos -= n_outs;
+                       }
                }
-  }
+       }
        return NULL;
 }
 
@@ -235,18 +244,18 @@ void irg_out_block_walk(ir_node *node,
 
        inc_irg_block_visited(current_ir_graph);
 
-  if (get_irn_mode(node) == mode_X) {
-    int i, n;
-
-         for (i = 0, n = get_irn_n_outs(node); i < n; ++i) {
-                 ir_node *succ = get_irn_out(node, i);
-                 if (get_irn_visited(succ) < get_irg_visited(current_ir_graph))
-                         irg_out_walk_2(succ, pre, post, env);
-         }
-  }
-  else {
-         irg_out_block_walk2(node, pre, post, env);
-  }
+       if (get_irn_mode(node) == mode_X) {
+               int i, n;
+
+               for (i = 0, n = get_irn_n_outs(node); i < n; ++i) {
+                       ir_node *succ = get_irn_out(node, i);
+                       if (get_irn_visited(succ) < get_irg_visited(current_ir_graph))
+                               irg_out_walk_2(succ, pre, post, env);
+               }
+       }
+       else {
+               irg_out_block_walk2(node, pre, post, env);
+       }
 }
 
 /*--------------------------------------------------------------------*/
@@ -306,15 +315,22 @@ static int count_outs(ir_graph *irg) {
        inc_irg_visited(irg);
        res = _count_outs(get_irg_end(irg));
 
-       /* now handle special nodes */
+       /* Now handle special nodes. We need the out count of those
+          even if they are not visible. */
        n = get_irg_frame(irg);
-       if (irn_not_visited(n)) {
+       if (! is_Bad(n) && irn_not_visited(n)) {
                n->out = (ir_node **)1;
                ++res;
        }
 
        n = get_irg_args(irg);
-       if (irn_not_visited(n)) {
+       if (! is_Bad(n) && irn_not_visited(n)) {
+               n->out = (ir_node **)1;
+               ++res;
+       }
+
+       n = get_irg_value_param_base(irg);
+       if (! is_Bad(n) && irn_not_visited(n)) {
                n->out = (ir_node **)1;
                ++res;
        }
@@ -372,7 +388,7 @@ static ir_node **_set_out_edges(ir_node *n, ir_node **free) {
  * @return The next free address
  */
 static ir_node **set_out_edges(ir_graph *irg, ir_node **free) {
-       ir_node *n, *special[2];
+       ir_node *n, *special[3];
        int i, n_outs;
 
        inc_irg_visited(irg);
@@ -381,9 +397,12 @@ static ir_node **set_out_edges(ir_graph *irg, ir_node **free) {
        /* handle special nodes */
        special[0] = get_irg_frame(irg);
        special[1] = get_irg_args(irg);
+       special[2] = get_irg_value_param_base(irg);
 
-       for (i = 1; i >= 0; --i) {
+       for (i = 2; i >= 0; --i) {
                n = special[i];
+               if (is_Bad(n))
+                       continue;
 
                if (get_irn_visited(n) < get_irg_visited(current_ir_graph)) {
                        n_outs = PTR_TO_INT(n->out);
@@ -491,6 +510,7 @@ void free_irp_outs(void) {
  *------------------------------------------------------------*/
 
 
+#ifdef INTERPROCEDURAL_VIEW
 /**
  * Inits the number of outedges for each node
  * before counting.
@@ -522,7 +542,6 @@ static void node_arity_count(ir_node * node, void * env) {
        }
 }
 
-
 /*
  * Inits all nodes for setting the outedges
  * Returns the overall count of edges
@@ -617,6 +636,7 @@ void free_ip_outs(void) {
        }
        irp->outs_state = outs_none;
 }
+#endif
 
 
 void free_irg_outs(ir_graph *irg) {