Teach new_tarval_from_str_int() to parse binary numbers: 0[bB][01]+.
[libfirm] / ir / ir / irgmod.c
index a6fb42e..16c5111 100644 (file)
@@ -21,7 +21,6 @@
  * @file
  * @brief    Support for ir graph modification.
  * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier
- * @version  $Id$
  */
 #include "config.h"
 
 #include "irtools.h"
 #include "error.h"
 
-/**
- * Turns a node into a "useless" Tuple.  The Tuple just forms a tuple
- * from several inputs.
- * This is useful if a node returning a tuple is removed, but the Projs
- * extracting values from the tuple are not available.
- */
 void turn_into_tuple(ir_node *node, int arity)
 {
        ir_graph *irg = get_irn_irg(node);
        ir_node **in  = ALLOCAN(ir_node*, arity);
+       ir_node  *bad = new_r_Bad(irg, mode_ANY);
        int       i;
 
        /* construct a new in array, with every input being bad */
        for (i = 0; i < arity; ++i) {
-               in[i] = new_r_Bad(irg);
+               in[i] = bad;
        }
        set_irn_in(node, arity, in);
        set_irn_op(node, op_Tuple);
 }
 
-/**
- * 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.
- */
 void exchange(ir_node *old, ir_node *nw)
 {
        ir_graph *irg;
@@ -82,9 +71,9 @@ void exchange(ir_node *old, ir_node *nw)
                /* copy all dependencies from old to new */
                add_irn_deps(nw, old);
 
-               edges_reroute(old, nw, irg);
-               edges_reroute_kind(old, nw, EDGE_KIND_DEP, irg);
-               edges_node_deleted(old, irg);
+               edges_reroute(old, nw);
+               edges_reroute_kind(old, nw, EDGE_KIND_DEP);
+               edges_node_deleted(old);
                /* noone is allowed to reference this node anymore */
                set_irn_op(old, op_Deleted);
        } else {
@@ -111,16 +100,15 @@ void exchange(ir_node *old, ir_node *nw)
                old->in[0] = block;
                old->in[1] = nw;
        }
-}
 
-/*--------------------------------------------------------------------*/
-/*  Functionality for collect_phis                                    */
-/*--------------------------------------------------------------------*/
+       /* update irg flags */
+       clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS
+                          | IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
+}
 
 /**
  * Walker: links all Phi nodes to their Blocks lists,
- *         all Proj nodes to there predecessors and all
- *         partBlocks to there MacroBlock header.
+ *         all Proj nodes to there predecessors.
  */
 static void collect_phiprojs_walker(ir_node *n, void *env)
 {
@@ -138,13 +126,6 @@ static void collect_phiprojs_walker(ir_node *n, void *env)
 
                set_irn_link(n, get_irn_link(pred));
                set_irn_link(pred, n);
-       } else if (is_Block(n)) {
-               ir_node *mbh = get_Block_MacroBlock(n);
-
-               if (mbh != n) {
-                       set_irn_link(n, get_irn_link(mbh));
-                       set_irn_link(mbh, n);
-               }
        }
 }
 
@@ -155,10 +136,6 @@ void collect_phiprojs(ir_graph *irg)
        irg_walk_graph(irg, firm_clear_node_and_phi_links, collect_phiprojs_walker, NULL);
 }
 
-/*--------------------------------------------------------------------*/
-/*  Functionality for part_block                                      */
-/*--------------------------------------------------------------------*/
-
 /**
  * Moves node and all predecessors of node from from_bl to to_bl.
  * Does not move predecessors of Phi nodes (or block nodes).
@@ -172,18 +149,18 @@ static void move(ir_node *node, ir_node *from_bl, ir_node *to_bl)
 
        /* move its Projs */
        if (get_irn_mode(node) == mode_T) {
-               ir_node *proj = get_irn_link(node);
+               ir_node *proj = (ir_node*)get_irn_link(node);
                while (proj) {
                        if (get_nodes_block(proj) == from_bl)
                                set_nodes_block(proj, to_bl);
-                       proj = get_irn_link(proj);
+                       proj = (ir_node*)get_irn_link(proj);
                }
        }
 
-       /* recursion ... */
        if (is_Phi(node))
                return;
 
+       /* recursion ... */
        arity = get_irn_arity(node);
        for (i = 0; i < arity; i++) {
                ir_node *pred = get_irn_n(node, i);
@@ -192,31 +169,66 @@ static void move(ir_node *node, ir_node *from_bl, ir_node *to_bl)
        }
 }
 
+static void move_projs(const ir_node *node, ir_node *to_bl)
+{
+       const ir_edge_t *edge;
+
+       if (get_irn_mode(node) != mode_T)
+               return;
+
+       foreach_out_edge(node, edge) {
+               ir_node *proj = get_edge_src_irn(edge);
+               if (!is_Proj(proj))
+                       continue;
+               set_nodes_block(proj, to_bl);
+               move_projs(proj, to_bl);
+       }
+}
+
+/**
+ * Moves node and all predecessors of node from from_bl to to_bl.
+ * Does not move predecessors of Phi nodes (or block nodes).
+ */
+static void move_edges(ir_node *node, ir_node *from_bl, ir_node *to_bl)
+{
+       int i, arity;
+
+       /* move this node */
+       set_nodes_block(node, to_bl);
+
+       /* move its Projs */
+       move_projs(node, to_bl);
+
+       /* We must not move predecessors of Phi nodes, even if they are in
+        * from_bl. (because these are values from an earlier loop iteration
+        * which are not predecessors of node here)
+        */
+       if (is_Phi(node))
+               return;
+
+       /* recursion ... */
+       arity = get_irn_arity(node);
+       for (i = 0; i < arity; i++) {
+               ir_node *pred = get_irn_n(node, i);
+               if (get_nodes_block(pred) == from_bl)
+                       move_edges(pred, from_bl, to_bl);
+       }
+}
+
 void part_block(ir_node *node)
 {
-       ir_node *new_block, *old_block, *mbh;
-       ir_node *phi, *jmp, *next, *block;
-       ir_graph *rem = current_ir_graph;
+       ir_graph *irg = get_irn_irg(node);
+       ir_node  *new_block, *old_block;
+       ir_node  *phi, *jmp;
 
        /* Turn off optimizations so that blocks are not merged again. */
        int rem_opt = get_opt_optimize();
        set_optimize(0);
 
-       current_ir_graph = get_irn_irg(node);
-
        /* Transform the control flow */
        old_block = get_nodes_block(node);
-       mbh       = get_Block_MacroBlock(old_block);
-       new_block = new_Block(get_Block_n_cfgpreds(old_block),
-                             get_Block_cfgpred_arr(old_block));
-
-       if (mbh != old_block) {
-               /* we splitting a partBlock */
-               set_Block_MacroBlock(new_block, mbh);
-       } else {
-               /* we are splitting a header: this creates a new header */
-               set_Block_MacroBlock(new_block, new_block);
-       }
+       new_block = new_r_Block(irg, get_Block_n_cfgpreds(old_block),
+                               get_Block_cfgpred_arr(old_block));
 
        /* create a jump from new_block to old_block, which is now the lower one */
        jmp = new_r_Jmp(new_block);
@@ -234,78 +246,43 @@ void part_block(ir_node *node)
                phi = get_Phi_next(phi);
        }
 
-       /* rewire partBlocks: This is necessary, because old_block is a new MacroBlock
-          header now */
-       if (mbh != old_block) {
-               ir_node *list = NULL;
-
-               /* move blocks from mbh to old_block if old_block dominates them */
-               block = get_irn_link(mbh);
-
-               /* mbh's list will be rebuild */
-               set_irn_link(mbh, NULL);
-               /* old_block is a new mbh */
-               set_Block_MacroBlock(old_block, old_block);
-
-               /* note that we must splice the list of partBlock here */
-               for (; block != NULL; block = next) {
-                       ir_node *curr = block;
-                       assert(is_Block(curr));
+       set_optimize(rem_opt);
+}
 
-                       next = get_irn_link(block);
+ir_node *part_block_edges(ir_node *node)
+{
+       ir_graph        *irg       = get_irn_irg(node);
+       ir_node         *old_block = get_nodes_block(node);
+       ir_node         *new_block = new_r_Block(irg,
+                                                get_Block_n_cfgpreds(old_block),
+                                                get_Block_cfgpred_arr(old_block));
+       const ir_edge_t *edge;
+       const ir_edge_t *next;
 
-                       if (block == old_block) {
-                               /* this effectively removed old_block from mbh's list */
-                               continue;
-                       }
+       /* old_block has no predecessors anymore for now */
+       set_irn_in(old_block, 0, NULL);
 
-                       assert(get_Block_MacroBlock(curr) == mbh);
-
-                       for (;;) {
-                               if (curr == old_block) {
-                                       /* old_block dominates the block, so old_block will be
-                                          the new macro block header */
-                                       set_Block_MacroBlock(block, old_block);
-                                       set_irn_link(block, list);
-                                       list = block;
-                                       break;
-                               }
-                               if (curr == mbh) {
-                                       /* leave it in the mbh */
-                                       set_irn_link(block, get_irn_link(mbh));
-                                       set_irn_link(mbh, block);
-                                       break;
-                               }
-
-                               assert(get_Block_n_cfgpreds(curr) == 1);
-                               curr = get_Block_cfgpred_block(curr, 0);
-                       }
-               }
-               /* beware: do NOT directly manipulate old_block's list, as old_block is
-                  in mbh's list and this would destroy the list! */
-               set_irn_link(old_block, list);
+       /* move node and its predecessors to new_block */
+       move_edges(node, old_block, new_block);
 
-               /* finally add new_block to mbh's list */
-               set_irn_link(new_block, get_irn_link(mbh));
-               set_irn_link(mbh, new_block);
-       } else {
-               /* old_block is the mbh, as well as new_block */
-               set_Block_MacroBlock(new_block, new_block);
+       /* move Phi nodes to new_block */
+       foreach_out_edge_safe(old_block, edge, next) {
+               ir_node *phi = get_edge_src_irn(edge);
+               if (!is_Phi(phi))
+                       continue;
+               set_nodes_block(phi, new_block);
        }
 
-       set_optimize(rem_opt);
-       current_ir_graph = rem;
+       return old_block;
 }
 
-/* kill a node by setting its predecessors to Bad and finally exchange the node by Bad itself. */
 void kill_node(ir_node *node)
 {
        ir_graph *irg = get_irn_irg(node);
-       ir_node *bad = get_irg_bad(irg);
-       int i;
 
-       for (i = get_irn_arity(node) - 1; i >= -1; --i) {
-               set_irn_n(node, i, bad);
+       if (edges_activated(irg)) {
+               edges_node_deleted(node);
        }
-       exchange(node, bad);
+       /* noone is allowed to reference this node anymore */
+       set_irn_op(node, op_Deleted);
 }