beifg: Factorise code to count interference components.
[libfirm] / ir / be / beirgmod.c
index 2dfbc34..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.
  */
 
 /**
@@ -51,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);
+}
+
 /*
   ___                     _     ____
  |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___ _ __ _ __ ___
@@ -78,21 +73,19 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
 */
 
-ir_node *insert_Perm_after(ir_graph *irg, 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     = be_get_irg_liveness(irg);
-       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 *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) {
@@ -110,19 +103,20 @@ ir_node *insert_Perm_after(ir_graph *irg, const arch_register_class_t *cls,
                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);
 
        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);
 
                be_ssa_construction_init(&senv, irg);
                be_ssa_construction_add_copy(&senv, perm_op);
@@ -145,16 +139,13 @@ static int blocks_removed;
  */
 static void remove_empty_block(ir_node *block)
 {
-       const ir_edge_t *edge;
-       const ir_edge_t *next;
-       int              i;
-       int              arity;
-       ir_node         *node;
-       ir_node         *pred;
-       ir_node         *succ_block;
-       ir_node         *jump = NULL;
-       ir_graph        *irg = get_irn_irg(block);
-       ir_entity       *entity;
+       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;
@@ -179,7 +170,7 @@ static void remove_empty_block(ir_node *block)
        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);
@@ -202,8 +193,8 @@ static void remove_empty_block(ir_node *block)
 
        /* 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;
@@ -265,8 +256,7 @@ int be_remove_empty_blocks(ir_graph *irg)
 
        if (blocks_removed) {
                /* invalidate analysis info */
-               clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
-                                  | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
+               clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
        }
        return blocks_removed;
 }
@@ -295,12 +285,8 @@ static void mark_dead_nodes_walker(ir_node *node, void *data)
 static void remove_dead_nodes_walker(ir_node *block, void *data)
 {
        remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
-       ir_node                 *node, *next;
-
-       for (node = sched_first(block); ! sched_is_end(node); node = next) {
-               /* get next node now, as after calling sched_remove it will be invalid */
-               next = sched_next(node);
 
+       sched_foreach_safe(block, node) {
                if (bitset_is_set(env->reachable, get_irn_idx(node)))
                        continue;
 
@@ -310,9 +296,7 @@ static void remove_dead_nodes_walker(ir_node *block, void *data)
 
                /* kill projs */
                if (get_irn_mode(node) == mode_T) {
-                       const ir_edge_t *edge;
-                       const ir_edge_t *next_edge;
-                       foreach_out_edge_safe(node, edge, next_edge) {
+                       foreach_out_edge_safe(node, edge) {
                                ir_node *proj = get_edge_src_irn(edge);
                                if (!is_Proj(proj))
                                        continue;