get_irn_n_edges: out_count still broken, switched back to recalculation
[libfirm] / ir / ir / irgmod.c
index 39f255c..47abfa3 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Project:     libFIRM
- * File name:   ir/ir/irgmod.h
+ * File name:   ir/ir/irgmod.c
  * Purpose:     Support for ir graph modification.
  * Author:      Martin Trapp, Christian Schaefer
  * Modified by: Goetz Lindenmaier
 # include "config.h"
 #endif
 
-# include "irvrfy.h"
-# include "irflag_t.h"
-# include "irgwalk.h"
-# include "irnode_t.h"
-# include "irgraph_t.h"
-# include "irgmod.h"
-# include "array.h"
-# include "ircons.h"
-# include "firmstat.h"
+#include "irvrfy.h"
+#include "irflag_t.h"
+#include "irgwalk.h"
+#include "irnode_t.h"
+#include "irgraph_t.h"
+#include "irgmod.h"
+#include "array.h"
+#include "ircons.h"
+#include "irhooks.h"
+#include "iredges_t.h"
+#include "irtools.h"
 
 /* Turns a node into a "useless" Tuple.  The Tuple just forms a tuple
    from several inputs.
@@ -38,7 +40,10 @@ turn_into_tuple (ir_node *node, int arity)
   } else {
     /* Allocate new array, don't free old in_array, it's on the obstack. */
     ir_node *block = get_nodes_block(node);
-    node->in = NEW_ARR_D (ir_node *, current_ir_graph->obst, arity+1);
+    edges_invalidate(node, current_ir_graph);
+    node->in = NEW_ARR_D(ir_node *, current_ir_graph->obst, arity+1);
+    /* clear the new in array, else edge_notify tries to delete garbage */
+    memset(node->in, 0, (arity+1) * sizeof(node->in[0]));
     set_nodes_block(node, block);
   }
 }
@@ -46,66 +51,78 @@ turn_into_tuple (ir_node *node, int arity)
 /* Insert irnode `new' in place of irnode `old'
    Since `new' may be bigger than `old' replace `old'
    by an op_Id which is smaller than everything */
-INLINE void
+void
 exchange (ir_node *old, ir_node *nw)
 {
-  ir_node *block;
-  ir_graph *irg = get_irn_irg (old);
+  hook_replace(old, nw);
+
+  /*
+   * If new outs are on, we can skip the id node creation and reroute
+   * the edges from the old node to the new directly.
+   */
+  if (edges_activated(current_ir_graph)) {
+    edges_reroute(old, nw, current_ir_graph);
+    edges_node_deleted(old, current_ir_graph);
+    old->op = op_Bad;
+  }
+  else {
+    /* Else, do it the old-fashioned way. */
 
-  assert(old != nw);
-  assert (irg);
-  assert(get_irn_op(old)->opar != oparity_dynamic);
+    ir_graph *irg = get_irn_irg (old);
+    ir_node *block;
 
-  stat_turn_into_id(old);
+    assert(old != nw);
+    assert (irg);
+    assert(get_irn_op(old)->opar != oparity_dynamic);
 
-  block = old->in[0];
-  if (!block) {
-    if (is_Block(nw)) block = nw;
-    else (block = nw->in[0]);
-    if (!block) { DDMN(old); DDMN(nw); assert(0 && "cannot find legal block for id"); }
-  }
+    hook_turn_into_id(old);
+
+    block = old->in[0];
+    if (!block) {
+      block = is_Block(nw) ? nw : get_nodes_block(nw);
 
-  old->op = op_Id;
-  old->in = NEW_ARR_D (ir_node *, irg->obst, 2);
-  old->in[0] = block;
-  old->in[1] = nw;
+      if (!block) {
+        DDMN(old);
+        DDMN(nw);
+        assert(0 && "cannot find legal block for id");
+      }
+    }
+
+    old->op = op_Id;
+    old->in = NEW_ARR_D (ir_node *, irg->obst, 2);
+    old->in[0] = block;
+    old->in[1] = nw;
+  }
 }
 
 /*--------------------------------------------------------------------*/
 /*  Functionality for collect_phis                                    */
 /*--------------------------------------------------------------------*/
 
-static void
-clear_link (ir_node *n, void *env) {
-  set_irn_link(n, NULL);
-}
-
-static void
-collect (ir_node *n, void *env) {
+/**
+ * Walker: links all Phi nodes to their Blocks and
+ *         all Proj nodes to there predecessors
+ */
+static void collect(ir_node *n, void *env) {
   ir_node *pred;
-  if (get_irn_op(n) == op_Phi) {
+
+  if (is_Phi(n)) {
     set_irn_link(n, get_irn_link(get_nodes_block(n)));
     set_irn_link(get_nodes_block(n), n);
   }
-  if (get_irn_op(n) == op_Proj) {
+  else if (is_Proj(n)) {
     pred = n;
-    while (get_irn_op(pred) == op_Proj)
+    do {
       pred = get_Proj_pred(pred);
+    } while (is_Proj(pred));
+
     set_irn_link(n, get_irn_link(pred));
     set_irn_link(pred, n);
   }
 }
 
 void collect_phiprojs(ir_graph *irg) {
-  ir_graph *rem;
-
-  /* Remember external state of current_ir_graph. */
-  rem = current_ir_graph;
-  current_ir_graph = irg;
-
-  irg_walk(get_irg_end(current_ir_graph), clear_link, collect, NULL);
-
-  current_ir_graph = rem;
+  irg_walk_graph(irg, firm_clear_link, collect, NULL);
 }
 
 
@@ -129,7 +146,7 @@ static void move (ir_node *node, ir_node *from_bl, ir_node *to_bl) {
     proj = get_irn_link(node);
     while (proj) {
       if (get_nodes_block(proj) == from_bl)
-       set_nodes_block(proj, to_bl);
+        set_nodes_block(proj, to_bl);
       proj = get_irn_link(proj);
     }
   }
@@ -160,9 +177,8 @@ void part_block(ir_node *node) {
             get_Block_cfgpred_arr(old_block));
   set_irg_current_block(current_ir_graph, new_block);
   {
-    ir_node *in[1];
-    in[0] = new_Jmp();
-    set_irn_in(old_block, 1, in);
+    ir_node *jmp = new_Jmp();
+    set_irn_in(old_block, 1, &jmp);
     irn_vrfy_irg(old_block, current_ir_graph);
   }