Added new arch interface
[libfirm] / ir / be / bechordal.c
index 5ad8d03..cadaf20 100644 (file)
@@ -2,7 +2,11 @@
  * Chordal register allocation.
  * @author Sebastian Hack
  * @date 8.12.2004
+ *
+ * Copyright (C) Universitaet Karlsruhe
+ * Released under the GPL
  */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 #include "besched_t.h"
 #include "belive_t.h"
 #include "bechordal_t.h"
+#include "bearch.h"
 
+#define BUILD_GRAPH
 #undef DUMP_INTERVALS
 #undef DUMP_PRESSURE
 #undef DUMP_IFG
 
+#if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
+#define BUILD_GRAPH
+#endif
+
 
 #ifdef DEBUG_libfirm
 #include "fourcc.h"
@@ -53,14 +63,20 @@ static firm_dbg_module_t *dbg;
  */
 typedef struct _env_t {
        struct obstack obst;    /**< An obstack for temporary storage. */
+  ir_graph *irg;        /**< The graph the reg alloc is running on. */
 #ifdef BUILD_GRAPH
-       set *graph;                                             /**< The interference graph. */
+       set *nodes;                                             /**< The interference graph nodes. */
+       set *edges;                                             /**< The interference graph edges. */
 #endif
 
        bitset_t *live;                         /**< A liveness bitset. */
        bitset_t *colors;                       /**< The color mask. */
        bitset_t *in_colors;    /**< Colors used by live in values. */
        int colors_n;                                   /**< The number of colors. */
+  const arch_env_t *arch_env;         /**< The arch interface environment. */
+  const arch_register_class_t *cls;   /**< The current register class. */
+  void *data;           /**< Some pointer, to which different
+                          phases can attach data to. */
 } env_t;
 
 
@@ -71,26 +87,33 @@ typedef struct _be_chordal_dump_params_t {
 } be_chordal_dump_params_t;
 
 static const be_chordal_dump_params_t dump_params = {
-       30,
+       10,
        10,
        4
 };
 
 #ifdef DUMP_INTERVALS
-static void draw_interval_graphs(ir_node *block,
-               struct list_head *border_head,
-               const be_chordal_dump_params_t *params)
+
+static INLINE void intv_filename(char *s, size_t n,
+    const env_t *env, ir_node *block)
+{
+       ir_snprintf(s, n, "intv_%F_%s_bl%N.eps",
+      env->irg, env->cls->name, block);
+}
+
+static void draw_interval_graph(const env_t *env,
+    ir_node *block, const be_chordal_dump_params_t *params)
 {
        int i;
        int x_dist = params->x_dist;
        int y_dist = params->y_dist;
-       ir_graph *irg = get_irn_irg(block);
+       ir_graph *irg = env->irg;
+  struct list_head *border_head = get_block_border_head(block);
 
        FILE *f;
        char buf[1024];
 
-       ir_snprintf(buf, sizeof(buf), "intv_%s_bl%N.eps",
-                       get_entity_name(get_irg_entity(irg)), block);
+  intv_filename(buf, sizeof(buf), env, block);
 
        if((f = fopen(buf, "wt")) != NULL) {
                border_t *b;
@@ -152,120 +175,69 @@ static void draw_interval_graphs(ir_node *block,
                fclose(f);
        }
 }
-#endif
-
-#if 0
-typedef struct _tree_layout_info_t {
-       ir_node *block;
-       int is_child;
-       int child_slot;
-       unsigned x;
-       unsigned y;
-       int steps;
-} tree_layout_info_t;
-
-typedef struct _tree_layout_params_t {
-       int interval_dist;
-       int x_block_dist;
-       int y_block_dist;
-       int step_len;
-} tree_layout_params_t;
-
-static const tree_layout_params_t tree_layout_params = {
-       10, 20, 20, 5
-};
 
-static void dump_tree_collect(ir_node *block, void *env)
+static void dump_block(ir_node *bl, void *data)
 {
-       ir_block *curr;
-       struct list_head *border_head;
-
-       int pre_num = get_Block_dom_tree_pre_num(block);
-       tree_layout_info_t *info = env;
-       tree_layout_info_t *i = &info[pre_num];
+  const env_t *env = data;
+  FILE *f = env->data;
+  char buf[128];
 
-       i->block = block;
-       i->child_slot = -1;
-       i->steps = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
-       i->is_child = 1;
+  draw_interval_graph(env, bl, &dump_params);
 
-       dominates_for_each(block, curr)
-               i->is_child = 0;
+  intv_filename(buf, sizeof(buf), env, bl);
+  ir_fprintf(f, "\tb%N [shape=\"epsf\" shapefile=\"%s\"];\n", bl, buf);
 }
 
-static void dump_tree_assign_x(ir_node *block, void *env)
+static void dump_edges(ir_node *bl, void *data)
 {
-       unsigned min_x = -1, max_x = 0;
-       int n_childs = 0;
-       ir_block *curr;
-
-       int pre_num = get_Block_dom_tree_pre_num(block);
-       tree_layout_info_t *info = env;
-       tree_layout_info_t *i = &info[pre_num];
-
-       if(i->is_child)
-               return;
-
-       dominates_for_each(block, curr) {
-               tree_layout_info_t *ci = &info[get_Block_dom_tree_pre_num(curr)];
-               max_x = MAX(max_x, ci->x);
-               min_x = MIN(min_x, ci->x);
-               n_childs++;
-       }
+  const env_t *env = data;
+  FILE *f = env->data;
+  int i, n;
+  ir_node *dom;
 
-       i->x = (max_x - mix_x) / n_childs;
-}
+#if 0
+  for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
+    ir_node *pred = get_irn_n(bl, i);
+    ir_fprintf(f, "\tb%N -> b%N;\n", get_nodes_block(pred), bl);
+  }
+#endif
 
-static void dump_tree_assign_y(ir_node *block, void *env)
-{
-       unsigned min_x = -1, max_x = 0;
-       int n_childs = 0;
-       ir_block *curr;
-
-       int pre_num = get_Block_dom_tree_pre_num(block);
-       tree_layout_info_t *info = env;
-       tree_layout_info_t *i = &info[pre_num];
-       ir_block *idom = get_Block_idom(block);
-
-       i->y = 0;
-       if(idom) {
-               tree_layout_info_t *idom_info = &info[get_Block_dom_tree_pre_num(idom)];
-               i->y = idom_info->y + idom_info->steps * params->step_len + params->y_dist;
-       }
-}
+  for(dom = get_Block_dominated_first(bl); dom;
+      dom = get_Block_dominated_next(dom)) {
 
-static void draw_block(ir_block *bl, void *env)
-{
+    ir_fprintf(f, "\tb%N -> b%N;\n", dom, bl);
+  }
 }
 
-static void dump_interval_tree(ir_graph *irg, const tree_layout_params_t *params)
+static void dump_intv_cfg(env_t *env)
 {
-       int i, slot;
-       int n_blocks = get_Block_dom_max_subtree_pre_num(get_irg_start_block(irg));
-       tree_layout_info_t *info = malloc(sizeof(info[0]) * n_blocks);
-
-       /* Fill the info array. */
-       dom_tree_walk_irg(irg, NULL, dump_tree_collect, info);
-
-       /* Assign the child slots. */
-       for(i = 0, slot = 0; i < n_blocks; ++i) {
-               tree_layout_info_t *i = &info[i];
-               if(i->is_child) {
-                       i->child_slot = slot++;
-                       i->x = i->child_slot * params->max_color * params->interval_dist + params->block_dist;
-               }
-       }
+  FILE *f;
+  char buf[128];
+
+  ir_snprintf(buf, sizeof(buf), "intv_cfg_%s_%F.dot",
+      env->cls->name, env->irg);
+
+  if((f = fopen(buf, "wt")) != NULL) {
+    void *old_data = env->data;
 
-       dom_tree_walk_irg(irg, NULL, dump_tree_assign_xy, info);
+    env->data = f;
+    ir_fprintf(f, "digraph G {\n");
+    ir_fprintf(f, "\tgraph [rankdir=\"LR\", ordering=\"out\"];\n");
+    dom_tree_walk_irg(env->irg, dump_block, dump_edges, env);
+    // irg_block_walk_graph(env->irg, dump_block, dump_edges, env);
+    ir_fprintf(f, "}\n");
+    fclose(f);
 
-       free(info);
+    env->data = old_data;
+  }
 }
+
 #endif
 
 #ifdef BUILD_GRAPH
 
-
 #define IF_EDGE_HASH(e) ((e)->src)
+#define IF_NODE_HASH(n) ((n)->nnr)
 
 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
 {
@@ -275,6 +247,14 @@ static int if_edge_cmp(const void *p1, const void *p2, size_t size)
        return !(e1->src == e2->src && e1->tgt == e2->tgt);
 }
 
+static int if_node_cmp(const void *p1, const void *p2, size_t size)
+{
+       const if_node_t *n1 = p1;
+       const if_node_t *n2 = p2;
+
+       return n1->nnr != n2->nnr;
+}
+
 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
 {
        /* Bring the smaller entry to src. */
@@ -292,15 +272,33 @@ static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
 static INLINE void add_if(const env_t *env, int src, int tgt)
 {
        if_edge_t edge;
+       if_node_t node, *src_node, *tgt_node;
+       /* insert edge */
        edge_init(&edge, src, tgt);
-       set_insert(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
+       set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
+
+       /* insert nodes */
+       node.nnr = src;
+       node.neighb = pset_new_ptr(8);
+       src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
+       node.nnr = tgt;
+       node.neighb = pset_new_ptr(8);
+       tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
+
+       /* insert neighbors into nodes */
+       pset_insert_ptr(src_node->neighb, tgt_node);
+       pset_insert_ptr(tgt_node->neighb, src_node);
 }
 
 static INLINE int are_connected(const env_t *env, int src, int tgt)
 {
        if_edge_t edge;
        edge_init(&edge, src, tgt);
-       return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
+       return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
+}
+
+int ifg_has_edge(const ir_graph *irg, if_node_t *n1, if_node_t* n2) {
+       return are_connected(get_irg_ra_link(irg), n1->nnr, n2->nnr);
 }
 
 static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
@@ -390,7 +388,7 @@ static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
        FILE *f;
 
        if((f = fopen(filename, "wt")) != NULL) {
-               long pos;
+               bitset_pos_t pos;
                int n_edges = 0;
                if_edge_t *edge;
                bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
@@ -404,7 +402,7 @@ static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
                        n_edges++;
                }
 
-               fprintf(f, "\tx [label=\"nodes: %lu, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
+               fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
 
                bitset_foreach(bs, pos) {
                        int nr = (int) pos;
@@ -429,6 +427,7 @@ static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
 
 #endif /* BUILD_GRAPH */
 
+
 /**
  * Add an interval border to the list of a block's list
  * of interval border.
@@ -521,6 +520,7 @@ static void pressure(ir_node *block, void *env_ptr)
        struct list_head *head;
        pset *live_in = get_live_in(block);
        pset *live_end = get_live_end(block);
+  const arch_register_class_t *cls = env->cls;
 
        DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
        bitset_clear_all(live);
@@ -536,8 +536,8 @@ static void pressure(ir_node *block, void *env_ptr)
        for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
                DBG((dbg, LEVEL_3, "\tMaking live: %n/%d\n", irn, get_irn_graph_nr(irn)));
                bitset_set(live, get_irn_graph_nr(irn));
-               if(is_allocatable_irn(irn))
-                       border_use(irn, step, 0);
+    if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls))
+      border_use(irn, step, 0);
        }
 
        ++step;
@@ -553,24 +553,20 @@ static void pressure(ir_node *block, void *env_ptr)
                /* Erase the color of each node encountered. */
                set_irn_color(irn, NO_COLOR);
 
-               /*
-                * If the node defines a datab value, i.e. something, registers must
-                * be allocated for, add a new def border to the border list.
-                */
-               if(is_allocatable_irn(irn)) {
+    /*
+     * If the node defines some value, which can put into a
+     * register of the current class, make a border for it.
+     */
+               if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
+      bitset_pos_t elm;
                        int nr = get_irn_graph_nr(irn);
 
                        bitset_clear(live, nr);
                        border_def(irn, step, 1);
 
 #ifdef BUILD_GRAPH
-                       {
-                               unsigned long elm;
-                               bitset_foreach(live, elm) {
-                                       int live_nr = (int) elm;
-                                       add_if(env, nr, live_nr);
-                               }
-                       }
+      bitset_foreach(live, elm)
+        add_if(env, nr, (int) elm);
 #endif
                }
 
@@ -581,7 +577,7 @@ static void pressure(ir_node *block, void *env_ptr)
                        for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
                                ir_node *op = get_irn_n(irn, i);
 
-                               if(is_allocatable_irn(op)) {
+                               if(arch_irn_has_reg_class(env->arch_env, op, 0, cls)) {
                                        int nr = get_irn_graph_nr(op);
 
                                        DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
@@ -601,7 +597,7 @@ static void pressure(ir_node *block, void *env_ptr)
         * Add initial defs for all values live in.
         */
        for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
-               if(is_allocatable_irn(irn)) {
+               if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
 
                        /* Mark the value live in. */
                        bitset_set(live, get_irn_graph_nr(irn));
@@ -619,9 +615,7 @@ static void assign(ir_node *block, void *env_ptr)
        bitset_t *live = env->live;
        bitset_t *colors = env->colors;
        bitset_t *in_colors = env->in_colors;
-
-       /* The used colors will remain on the obstack. */
-       bitset_t *used_colors = bitset_obstack_alloc(obst, env->colors_n);
+  const arch_register_class_t *cls = env->cls;
 
        /* Mark the obstack level and allocate the temporary tmp_colors */
        void *obstack_level = obstack_base(obst);
@@ -649,14 +643,16 @@ static void assign(ir_node *block, void *env_ptr)
         * allocated before), we have to mark their colors as used also.
         */
        for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
-               if(is_allocatable_irn(irn)) {
-                       int col = get_irn_color(irn);
+               if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
+      const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
+      int col;
+
+      assert(reg && "Nodfe must have been assigned a register");
+                       col = arch_register_get_index(reg);
 
                        /* Mark the color of the live in value as used. */
-                       assert(is_color(col) && "Node must have been assigned a color.");
                        bitset_set(colors, col);
                        bitset_set(in_colors, col);
-                       bitset_set(used_colors, col);
 
                        /* Mark the value live in. */
                        bitset_set(live, get_irn_graph_nr(irn));
@@ -669,7 +665,7 @@ static void assign(ir_node *block, void *env_ptr)
         * will work.
         */
        list_for_each_entry_reverse(border_t, b, head, list) {
-               const ir_node *irn = b->irn;
+               ir_node *irn = b->irn;
                int nr = get_irn_graph_nr(irn);
 
                /*
@@ -677,87 +673,41 @@ static void assign(ir_node *block, void *env_ptr)
                 * color.
                 */
                if(b->is_def && !is_live_in(block, irn)) {
-                       ra_node_info_t *ri = get_ra_node_info(irn);
+      const arch_register_t *reg;
                        int col = NO_COLOR;
 
                        DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
 
-                       /*
-                        * Try to assign live out values colors which are not used by live
-                        * in values.
-                        */
-#if 0
-                       if(is_live_out(block, irn)) {
-                               int next_clear;
-
-                               bitset_copy(tmp_colors, colors);
-                               bitset_or(tmp_colors, in_colors);
-                               next_clear = bitset_next_clear(tmp_colors, 0);
-                               col = next_clear != -1 ? next_clear : NO_COLOR;
-
-                               DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
-                       }
-#endif
-
-                       /* If a color is not yet assigned, do it now. */
-                       if(!is_color(col))
-                               col = bitset_next_clear(colors, 0);
+      col = bitset_next_clear(colors, 0);
+      reg = arch_register_for_index(env->cls, col);
 
-                       assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
-                       assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
+      assert(arch_get_irn_register(env->arch_env, irn, 0) == NULL
+          && "This node must not have been assigned a register yet");
+                       assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
 
                        bitset_set(colors, col);
-                       bitset_set(used_colors, col);
                        bitset_set(live, nr);
 
-                       ri->color = col;
-
-                       DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
+                       arch_set_irn_register(env->arch_env, irn, 0, reg);
+                       DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %n\n",
+            arch_register_get_name(reg), col, irn));
                }
 
                /* Clear the color upon a use. */
                else if(!b->is_def) {
-                       int col = get_irn_color(irn);
+      const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
+                       int col;
 
+      assert(reg && "Register must have been assigned");
+
+      col = arch_register_get_index(reg);
                        assert(bitset_is_set(live, nr) && "Cannot have a non live use");
-                       assert(is_color(col) && "A color must have been assigned");
 
                        bitset_clear(colors, col);
                        bitset_clear(live, nr);
                }
        }
 
-#ifdef DUMP_INTERVALS
-       draw_interval_graphs(block, head, &dump_params);
-#endif
-
-#ifdef DUMP_PRESSURE
-       {
-               char buf[128];
-               FILE *f;
-
-               ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
-                               get_entity_name(get_irg_entity(irg)), block);
-
-               if((f = fopen(buf, "wt")) != NULL) {
-                       sched_foreach_reverse(block, irn) {
-                               if(is_allocatable_irn(irn))
-                                       ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
-                                                       get_ra_node_info(irn)->pressure);
-
-                       }
-                       fclose(f);
-               }
-       }
-#endif
-
-
-       /*
-        * Allocate the used colors array in the blocks ra info structure and
-        * fill it.
-        */
-       get_ra_block_info(block)->used_colors = used_colors;
-
        /* Free the auxillary data on the obstack. */
        obstack_free(obst, obstack_level);
 }
@@ -768,9 +718,12 @@ void be_ra_chordal_init(void)
        firm_dbg_set_mask(dbg, 0);
 }
 
-void be_ra_chordal(ir_graph *irg)
+void be_ra_chordal(ir_graph *irg,
+    const arch_env_t *arch_env,
+    const arch_register_class_t *cls)
 {
        int node_count = get_graph_node_count(irg);
+  int colors_n = arch_register_class_n_regs(cls);
        env_t *env = malloc(sizeof(*env));
 
        if(get_irg_dom_state(irg) != dom_consistent)
@@ -779,13 +732,17 @@ void be_ra_chordal(ir_graph *irg)
        obstack_init(&env->obst);
 
 #ifdef BUILD_GRAPH
-       env->graph = new_set(if_edge_cmp, node_count);
+       env->edges = new_set(if_edge_cmp, node_count);
+       env->nodes = new_set(if_node_cmp, node_count);
 #endif
 
        env->live = bitset_obstack_alloc(&env->obst, node_count);
-       env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
-       env->in_colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
-       env->colors_n = TEST_COLORS;
+       env->colors = bitset_obstack_alloc(&env->obst, colors_n);
+       env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
+       env->colors_n = colors_n;
+  env->cls = cls;
+  env->arch_env = arch_env;
+  env->irg = irg;
 
        /* First, determine the pressure */
        dom_tree_walk_irg(irg, pressure, NULL, env);
@@ -800,11 +757,15 @@ void be_ra_chordal(ir_graph *irg)
        {
                char buf[128];
 
-               ir_snprintf(buf, sizeof(buf), "ifg_%s.dot", get_entity_name(get_irg_entity(irg)));
-               dump_ifg(irg, env->graph, buf);
+               ir_snprintf(buf, sizeof(buf), "ifg_%F.dot", irg);
+               dump_ifg(irg, env->edges, buf);
        }
 #endif
 
+#ifdef DUMP_INTERVALS
+  dump_intv_cfg(env);
+#endif
+
        set_irg_ra_link(irg, env);
 }
 
@@ -813,7 +774,13 @@ void be_ra_chordal_done(ir_graph *irg)
        env_t *env = get_irg_ra_link(irg);
 
 #ifdef BUILD_GRAPH
-       free(env->graph);
+       {
+               if_node_t *ifn;
+               for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
+                       free(ifn->neighb);
+               free(env->nodes);
+               free(env->edges);
+       }
 #endif
 
        obstack_free(&env->obst, NULL);
@@ -835,7 +802,19 @@ int phi_ops_interfere(const ir_node *a, const ir_node *b)
 }
 
 #ifdef BUILD_GRAPH
+/** TODO remove this
+ * Deprecated. Use be_ra_get_ifg_edges instead.
+ */
 set *be_ra_get_ifg(ir_graph *irg) {
-       return ((env_t *)get_irg_ra_link(irg))->graph;
+       return ((env_t *)get_irg_ra_link(irg))->edges;
 }
+
+set *be_ra_get_ifg_edges(ir_graph *irg) {
+       return ((env_t *)get_irg_ra_link(irg))->edges;
+}
+
+set *be_ra_get_ifg_nodes(ir_graph *irg) {
+       return ((env_t *)get_irg_ra_link(irg))->nodes;
+}
+
 #endif