Added sparse matrix impl. Used by copyopt_ilp
[libfirm] / ir / be / bechordal.c
index bda3cfd..5ad8d03 100644 (file)
 #include "belive_t.h"
 #include "bechordal_t.h"
 
-
-
 #undef DUMP_INTERVALS
 #undef DUMP_PRESSURE
-#define DUMP_IFG
+#undef DUMP_IFG
 
-#define BUILD_GRAPH
-
-#ifdef USE_OLD_PHI_INTERFERENCE
-#undef BUILD_GRAPH
-#define BUILD_GRAPH
-#endif
 
 #ifdef DEBUG_libfirm
 #include "fourcc.h"
@@ -84,6 +76,7 @@ static const be_chordal_dump_params_t dump_params = {
        4
 };
 
+#ifdef DUMP_INTERVALS
 static void draw_interval_graphs(ir_node *block,
                struct list_head *border_head,
                const be_chordal_dump_params_t *params)
@@ -159,12 +152,118 @@ 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)
+{
+       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];
+
+       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;
+
+       dominates_for_each(block, curr)
+               i->is_child = 0;
+}
+
+static void dump_tree_assign_x(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];
+
+       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++;
+       }
+
+       i->x = (max_x - mix_x) / n_childs;
+}
+
+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;
+       }
+}
+
+static void draw_block(ir_block *bl, void *env)
+{
+}
+
+static void dump_interval_tree(ir_graph *irg, const tree_layout_params_t *params)
+{
+       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;
+               }
+       }
+
+       dom_tree_walk_irg(irg, NULL, dump_tree_assign_xy, info);
+
+       free(info);
+}
+#endif
 
 #ifdef BUILD_GRAPH
 
-typedef struct _if_edge_t {
-       int src, tgt;
-} if_edge_t;
 
 #define IF_EDGE_HASH(e) ((e)->src)
 
@@ -204,21 +303,126 @@ static INLINE int are_connected(const env_t *env, int src, int tgt)
        return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
 }
 
-static void dump_ifg(set *edges, const char *filename)
+static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
 {
+       static const char *colors[] = {
+               "coral",
+               "azure",
+               "bisque",
+               "aliceblue",
+               "blanchedalmond",
+               "deeppink",
+               "cornsilk",
+               "blueviolet",
+               "floralwhite",
+               "hotpink",
+               "gainsboro",
+               "indianred",
+               "cornflowerblue",
+               "ghostwhite",
+               "lightpink",
+               "palegoldenrod",
+               "darkslateblue",
+               "honeydew",
+               "ivory",
+               "lavender",
+               "mediumvioletred",
+               "indigo",
+               "lavenderblush",
+               "lemonchiffon",
+               "linen",
+               "pink",
+               "mintcream",
+               "red",
+               "mediumblue",
+               "mistyrose",
+               "mediumslateblue",
+               "moccasin",
+               "tomato",
+               "forestgreen",
+               "midnightblue",
+               "navajowhite",
+               "navy",
+               "oldlace",
+               "greenyellow",
+               "navyblue",
+               "papayawhip",
+               "lawngreen",
+               "powderblue",
+               "peachpuff",
+               "seashell",
+               "snow",
+               "thistle",
+               "wheat",
+               "darkkhaki",
+               "mintcream",
+               "khaki",
+               "Magentas",
+               "whitesmoke",
+               "peru",
+               "palegreen",
+               "blueviolet",
+               "rosybrown",
+               "saddlebrown",
+               "springgreen",
+               "darkviolet",
+               "darkslategray",
+               "dimgray",
+               "sienna",
+               "gray",
+               "tan",
+               "gray",
+               "mediumvioletred",
+               "lightgray",
+               "Oranges",
+               "cyan",
+               "lightslategray",
+               "darkorange",
+               "slategray",
+               "orangered",
+               "mediumturquoise",
+               "violet",
+               "paleturquoise"
+       };
+
+       static const int n_colors = sizeof(colors) / sizeof(colors[0]);
+
        FILE *f;
 
        if((f = fopen(filename, "wt")) != NULL) {
+               long pos;
+               int n_edges = 0;
                if_edge_t *edge;
+               bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
+
+               ir_fprintf(f, "graph \"%F\" {\n", irg);
+               fprintf(f, "\tnode [shape=box,style=filled]\n");
 
-               fprintf(f, "graph G {\n");
+               for(edge = set_first(edges); edge; edge = set_next(edges)) {
+                       bitset_set(bs, edge->src);
+                       bitset_set(bs, edge->tgt);
+                       n_edges++;
+               }
+
+               fprintf(f, "\tx [label=\"nodes: %lu, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
+
+               bitset_foreach(bs, pos) {
+                       int nr = (int) pos;
+                       ir_node *irn = get_irn_for_graph_nr(irg, nr);
+                       int color = get_irn_color(irn);
+
+                       ir_fprintf(f, "\tn%d [label=\"%n\",color=\"%s\"]\n", nr, irn,
+                                       color >= 0 && color < n_colors ? colors[color] : "black");
+               }
 
                for(edge = set_first(edges); edge; edge = set_next(edges)) {
-                       fprintf(f, "i\tn%d -- n%d\n", edge->src, edge->tgt);
+                       fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
                }
 
                fprintf(f, "}\n");
                fclose(f);
+
+               bitset_free(bs);
        }
 
 }
@@ -279,7 +483,7 @@ static INLINE border_t *border_add(env_t *env, struct list_head *head,
 #endif
        }
 
-
+       b->pressure = pressure;
        b->is_def = is_def;
        b->is_real = is_real;
        b->irn = irn;
@@ -332,7 +536,7 @@ 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_Phi(irn) && is_allocatable_irn(irn))
+               if(is_allocatable_irn(irn))
                        border_use(irn, step, 0);
        }
 
@@ -421,7 +625,7 @@ static void assign(ir_node *block, void *env_ptr)
 
        /* Mark the obstack level and allocate the temporary tmp_colors */
        void *obstack_level = obstack_base(obst);
-       bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);
+       /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
 
        const ir_node *irn;
        border_t *b;
@@ -524,7 +728,7 @@ static void assign(ir_node *block, void *env_ptr)
        }
 
 #ifdef DUMP_INTERVALS
-       draw_interval_graphs(block, &head, &dump_params);
+       draw_interval_graphs(block, head, &dump_params);
 #endif
 
 #ifdef DUMP_PRESSURE
@@ -558,255 +762,10 @@ static void assign(ir_node *block, void *env_ptr)
        obstack_free(obst, obstack_level);
 }
 
-
-#if 0
-static void block_alloc(ir_node *block, void *env_ptr)
-{
-       env_t *env = env_ptr;
-       struct obstack *obst = &env->obst;
-       void *obstack_level = obstack_base(obst);
-       bitset_t *live = env->live;
-       bitset_t *colors = env->colors;
-       bitset_t *in_colors = env->in_colors;
-       bitset_t *used_colors = bitset_malloc(env->colors_n);
-       bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);
-       ir_graph *irg = get_irn_irg(block);
-       int also_assign = env->assign;
-
-       int i, n;
-       int block_nr = get_block_graph_nr(block);
-       const ir_node *irn;
-       border_t *b;
-       struct list_head *head = &get_ra_block_info(block)->border_head;
-       pset *live_in = get_live_in(block);
-       pset *live_end = get_live_end(block);
-
-       /*
-        * Check, if this block has already been processed, if true, return
-        * immediately.
-        */
-       if(bitset_is_set(env->processed, block_nr))
-               return;
-
-       /*
-        * Ensure, that the immediate dominator of this block is allocated
-        * before this block, since the values live in at this block are
-        * defined in the dominators of this block. Coloring the dominators
-        * thus is vital before coloring this block.
-        */
-       if(idom)
-               block_alloc(idom, env);
-
-       /* Clear the live and allocate the color bitset. */
-       bitset_clear_all(live);
-       bitset_clear_all(colors);
-       bitset_clear_all(in_colors);
-
-       INIT_LIST_HEAD(&head);
-
-       /*
-        * Make final uses of all values live out of the block.
-        * They are neccessary to build up real intervals.
-        */
-       for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
-               DBG((dbg, LEVEL_3, "Making live: %n/%d\n", irn, get_irn_graph_nr(irn)));
-               bitset_set(live, get_irn_graph_nr(irn));
-               if(!is_Phi(irn) && is_allocatable_irn(irn))
-                       border_add(env, &head, irn, step, 0);
-       }
-
-       ++step;
-
-       /*
-        * Determine the last uses of a value inside the block, since they are
-        * relevant for the interval borders.
-        */
-       sched_foreach_reverse(block, irn) {
-               DBG((dbg, LEVEL_1, "insn: %n\n", irn));
-               DBG((dbg, LEVEL_2, "live: %b\n", live));
-
-               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)) {
-                       int nr = get_irn_graph_nr(irn);
-
-                       bitset_clear(live, nr);
-                       border_add(env, &head, irn, step, 1);
-
-#ifdef BUILD_GRAPH
-                       {
-                               unsigned long elm;
-                               bitset_foreach(live, elm) {
-                                       int live_nr = (int) elm;
-                                       ir_node *live_irn = get_irn_for_graph_nr(irg, live_nr);
-                                       if(is_phi_operand(live_irn)) {
-                                               add_if(env, nr, live_nr);
-                                       }
-                               }
-                       }
-#endif
-               }
-
-               /*
-                * If the node is no phi node we can examine the uses.
-                */
-               if(!is_Phi(irn)) {
-                       for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
-                               ir_node *op = get_irn_n(irn, i);
-
-                               if(is_allocatable_irn(op)) {
-                                       int nr = get_irn_graph_nr(op);
-
-                                       DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
-
-                                       if(!bitset_is_set(live, nr)) {
-                                               border_add(env, &head, op, step, 0);
-                                               bitset_set(live, nr);
-                                       }
-                               }
-                       }
-               }
-
-               ++step;
-       }
-
-       bitset_clear_all(live);
-
-       /*
-        * Add initial defs for all values live in.
-        * Since their colors have already been assigned (The dominators were
-        * 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);
-
-                       /* 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));
-
-                       /* Add the def */
-                       border_add(env, &head, irn, step, 1);
-               }
-       }
-
-       DBG((dbg, LEVEL_4, "usedef chain for block %n\n", block));
-       list_for_each_entry(border_t, b, &head, list) {
-               DBG((dbg, LEVEL_4, "\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn)));
-       }
-
-       /*
-        * Mind that the sequence of defs from back to front defines a perfect
-        * elimination order. So, coloring the definitions from first to last
-        * will work.
-        */
-       list_for_each_entry_reverse(border_t, b, &head, list) {
-               const ir_node *irn = b->irn;
-               int nr = get_irn_graph_nr(irn);
-
-               /*
-                * Assign a color, if it is a local def. Global defs already have a
-                * color.
-                */
-               if(b->is_def && !is_live_in(block, irn)) {
-                       ra_node_info_t *ri = get_ra_node_info(irn);
-                       int col = NO_COLOR;
-
-                       DBG((dbg, LEVEL_4, "colors in use: %b\n", colors));
-
-                       /*
-                        * Try to assign live out values colors which are not used by live
-                        * in values.
-                        */
-                       if(is_live_out(block, irn)) {
-                               bitset_copy(tmp_colors, colors);
-                               bitset_or(tmp_colors, in_colors);
-                               col = bitset_next_clear(tmp_colors, 0);
-                               DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
-                       }
-
-                       /* If a color is not yet assigned, do it now. */
-                       if(!is_color(col))
-                               col = bitset_next_clear(colors, 0);
-
-                       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");
-
-                       bitset_set(colors, col);
-                       bitset_set(used_colors, col);
-                       bitset_set(live, nr);
-
-                       ri->color = col;
-                       ri->pressure = bitset_popcnt(colors);
-
-                       DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
-               }
-
-               /* Clear the color upon a use. */
-               else if(!b->is_def) {
-                       int col = get_irn_color(irn);
-
-                       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;
-
-       /* Mark this block has processed. */
-       bitset_set(env->processed, block_nr);
-
-       /* Reset the obstack to its initial level */
-       obstack_free(obst, obstack_level);
-}
-
-#endif
-
 void be_ra_chordal_init(void)
 {
        dbg = firm_dbg_register(DBG_BERA);
-       firm_dbg_set_mask(dbg, -1);
+       firm_dbg_set_mask(dbg, 0);
 }
 
 void be_ra_chordal(ir_graph *irg)
@@ -842,7 +801,7 @@ 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(env->graph, buf);
+               dump_ifg(irg, env->graph, buf);
        }
 #endif
 
@@ -874,3 +833,9 @@ int phi_ops_interfere(const ir_node *a, const ir_node *b)
        return values_interfere(a, b);
 #endif /* BUILD_GRAPH */
 }
+
+#ifdef BUILD_GRAPH
+set *be_ra_get_ifg(ir_graph *irg) {
+       return ((env_t *)get_irg_ra_link(irg))->graph;
+}
+#endif