beifg: Factorise code to count interference components.
[libfirm] / ir / be / beirgmod.c
index 0005b7b..aa9b51b 100644 (file)
@@ -1,20 +1,6 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
- *
  * This file is part of libFirm.
- *
- * This file may be distributed and/or modified under the terms of the
- * GNU General Public License version 2 as published by the Free Software
- * Foundation and appearing in the file LICENSE.GPL included in the
- * packaging of this file.
- *
- * Licensees holding valid libFirm Professional Edition licenses may use
- * this file in accordance with the libFirm Commercial License.
- * Agreement provided with the Software.
- *
- * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
- * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE.
+ * Copyright (C) 2012 University of Karlsruhe.
  */
 
 /**
@@ -22,7 +8,6 @@
  * @brief       Backend IRG modification routines.
  * @author      Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig
  * @date        04.05.2005
- * @version     $Id$
  *
  * This file contains the following IRG modifications for be routines:
  * - insertion of Perm nodes
@@ -52,7 +37,7 @@
 #include "irgraph_t.h"
 #include "irgopt.h"
 #include "irgmod.h"
-#include "irprintf_t.h"
+#include "irprintf.h"
 #include "irgwalk.h"
 
 #include "be_t.h"
 
 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
+static int cmp_node_nr(const void *a, const void *b)
+{
+       ir_node **p1 = (ir_node**)a;
+       ir_node **p2 = (ir_node**)b;
+       long      n1 = get_irn_node_nr(*p1);
+       long      n2 = get_irn_node_nr(*p2);
+       return (n1>n2) - (n1<n2);
+}
+
 /*
   ___                     _     ____
  |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___ _ __ _ __ ___
@@ -79,22 +73,19 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
 */
 
-ir_node *insert_Perm_after(be_irg_t *birg,
-                                                  const arch_register_class_t *cls,
+ir_node *insert_Perm_before(ir_graph *irg, const arch_register_class_t *cls,
                                                   ir_node *pos)
 {
-       be_lv_t *lv     = birg->lv;
-       ir_node *bl     = is_Block(pos) ? pos : get_nodes_block(pos);
-       ir_nodeset_t          live;
-       ir_nodeset_iterator_t iter;
+       be_lv_t     *lv = be_get_irg_liveness(irg);
+       ir_nodeset_t live;
 
-       ir_node *curr, *irn, *perm, **nodes;
+       ir_node *perm, **nodes;
        size_t i, n;
 
-       DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
+       DBG((dbg, LEVEL_1, "Insert Perm before: %+F\n", pos));
 
        ir_nodeset_init(&live);
-       be_liveness_nodes_live_at(lv, cls, pos, &live);
+       be_liveness_nodes_live_before(lv, cls, pos, &live);
 
        n = ir_nodeset_size(&live);
        if (n == 0) {
@@ -112,24 +103,22 @@ ir_node *insert_Perm_after(be_irg_t *birg,
                i++;
        }
        ir_nodeset_destroy(&live);
+       /* make the input order deterministic */
+       qsort(nodes, n, sizeof(nodes[0]), cmp_node_nr);
 
+       ir_node *const bl = get_nodes_block(pos);
        perm = be_new_Perm(cls, bl, n, nodes);
-       sched_add_after(pos, perm);
+       sched_add_before(pos, perm);
        free(nodes);
 
-       curr = perm;
        for (i = 0; i < n; ++i) {
                ir_node *perm_op = get_irn_n(perm, i);
-               const arch_register_t *reg = arch_get_irn_register(perm_op);
                be_ssa_construction_env_t senv;
 
                ir_mode *mode = get_irn_mode(perm_op);
                ir_node *proj = new_r_Proj(perm, mode, i);
-               arch_set_irn_register(proj, reg);
 
-               curr = proj;
-
-               be_ssa_construction_init(&senv, birg);
+               be_ssa_construction_init(&senv, irg);
                be_ssa_construction_add_copy(&senv, perm_op);
                be_ssa_construction_add_copy(&senv, proj);
                be_ssa_construction_fix_users(&senv, perm_op);
@@ -150,12 +139,13 @@ static int blocks_removed;
  */
 static void remove_empty_block(ir_node *block)
 {
-       const ir_edge_t *edge, *next;
-       int      i, arity;
-       ir_node *node;
-       ir_node *pred;
-       ir_node *succ_block;
-       ir_node *jump = NULL;
+       int        i;
+       int        arity;
+       ir_node   *pred;
+       ir_node   *succ_block;
+       ir_node   *jump = NULL;
+       ir_graph  *irg = get_irn_irg(block);
+       ir_entity *entity;
 
        if (irn_visited_else_mark(block))
                return;
@@ -165,7 +155,7 @@ static void remove_empty_block(ir_node *block)
 
        sched_foreach(block, node) {
                if (! is_Jmp(node)
-                               && !(arch_irn_get_flags(node) & arch_irn_flags_simple_jump))
+                               && !(arch_get_irn_flags(node) & arch_irn_flags_simple_jump))
                        goto check_preds;
                if (jump != NULL) {
                        /* we should never have 2 jumps in a block */
@@ -177,14 +167,15 @@ static void remove_empty_block(ir_node *block)
        if (jump == NULL)
                goto check_preds;
 
+       entity     = get_Block_entity(block);
        pred       = get_Block_cfgpred(block, 0);
        succ_block = NULL;
-       foreach_out_edge_safe(jump, edge, next) {
+       foreach_out_edge_safe(jump, edge) {
                int pos = get_edge_src_pos(edge);
 
                assert(succ_block == NULL);
                succ_block = get_edge_src_irn(edge);
-               if (has_Block_entity(succ_block) && has_Block_entity(block)) {
+               if (get_Block_entity(succ_block) != NULL && entity != NULL) {
                        /*
                         * Currently we can add only one label for a block.
                         * Therefore we cannot combine them if  both block already have one.
@@ -195,25 +186,18 @@ static void remove_empty_block(ir_node *block)
                set_irn_n(succ_block, pos, pred);
        }
 
-       if (has_Block_entity(block)) {
+       if (entity != NULL) {
                /* move the label to the successor block */
-               ir_entity *entity = get_Block_entity(block);
                set_Block_entity(succ_block, entity);
        }
 
        /* there can be some non-scheduled Pin nodes left in the block, move them
         * to the succ block (Pin) or pred block (Sync) */
-       foreach_out_edge_safe(block, edge, next) {
-               node = get_edge_src_irn(edge);
+       foreach_out_edge_safe(block, edge) {
+               ir_node *const node = get_edge_src_irn(edge);
 
                if (node == jump)
                        continue;
-               if (is_Block(node)) {
-                       /* a Block->Block edge: This should be the MacroBlock
-                          edge, ignore it. */
-                       assert(get_Block_MacroBlock(node) == block && "Wrong Block->Block edge");
-                       continue;
-               }
                /* we simply kill Pins, because there are some strange interactions
                 * between jump threading, which produce PhiMs with Pins, we simply
                 * kill the pins here, everything is scheduled anyway */
@@ -233,7 +217,7 @@ static void remove_empty_block(ir_node *block)
                panic("Unexpected node %+F in block %+F with empty schedule", node, block);
        }
 
-       set_Block_cfgpred(block, 0, new_Bad());
+       set_Block_cfgpred(block, 0, new_r_Bad(irg, mode_X));
        kill_node(jump);
        blocks_removed = 1;
 
@@ -272,15 +256,74 @@ int be_remove_empty_blocks(ir_graph *irg)
 
        if (blocks_removed) {
                /* invalidate analysis info */
-               set_irg_doms_inconsistent(irg);
-               set_irg_extblk_inconsistent(irg);
-               set_irg_outs_inconsistent(irg);
-               set_irg_loopinfo_inconsistent(irg);
+               clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
        }
        return blocks_removed;
 }
 
-BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);
+//---------------------------------------------------------------------------
+
+typedef struct remove_dead_nodes_env_t_ {
+       bitset_t *reachable;
+       ir_graph *irg;
+       be_lv_t  *lv;
+} remove_dead_nodes_env_t;
+
+/**
+ * Post-walker: remember all visited nodes in a bitset.
+ */
+static void mark_dead_nodes_walker(ir_node *node, void *data)
+{
+       remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
+       bitset_set(env->reachable, get_irn_idx(node));
+}
+
+/**
+ * Post-block-walker:
+ * Walk through the schedule of every block and remove all dead nodes from it.
+ */
+static void remove_dead_nodes_walker(ir_node *block, void *data)
+{
+       remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
+
+       sched_foreach_safe(block, node) {
+               if (bitset_is_set(env->reachable, get_irn_idx(node)))
+                       continue;
+
+               if (env->lv != NULL)
+                       be_liveness_remove(env->lv, node);
+               sched_remove(node);
+
+               /* kill projs */
+               if (get_irn_mode(node) == mode_T) {
+                       foreach_out_edge_safe(node, edge) {
+                               ir_node *proj = get_edge_src_irn(edge);
+                               if (!is_Proj(proj))
+                                       continue;
+                               if (env->lv != NULL)
+                                       be_liveness_remove(env->lv, proj);
+                               kill_node(proj);
+                       }
+               }
+               kill_node(node);
+       }
+}
+
+void be_remove_dead_nodes_from_schedule(ir_graph *irg)
+{
+       remove_dead_nodes_env_t env;
+       env.reachable = bitset_alloca(get_irg_last_idx(irg));
+       env.lv        = be_get_irg_liveness(irg);
+       env.irg       = irg;
+
+       // mark all reachable nodes
+       irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
+
+       // walk schedule and remove non-marked nodes
+       irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
+}
+
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod)
 void be_init_irgmod(void)
 {
        FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");