debug stuff and bugfixes
[libfirm] / ir / be / bechordal.c
index 567fbca..2fcb990 100644 (file)
@@ -14,6 +14,7 @@
 #include <ctype.h>
 
 #include "obst.h"
+#include "pset.h"
 #include "list.h"
 #include "bitset.h"
 #include "iterator.h"
@@ -37,6 +38,7 @@
 #include "bechordal_t.h"
 #include "bechordal_draw.h"
 
+#define DBG_LEVEL 0
 #define NO_COLOR (-1)
 
 #undef DUMP_INTERVALS
 #endif
 
 
-#ifdef DEBUG_libfirm
 #include "fourcc.h"
 
 /* Make a fourcc for border checking. */
 #define BORDER_FOURCC                          FOURCC('B', 'O', 'R', 'D')
 
-#endif /* DEBUG_libfirm */
-
 static firm_dbg_module_t *dbg;
 
 #ifdef BUILD_GRAPH
@@ -129,94 +128,12 @@ int ifg_has_edge(const be_chordal_env_t *env, const if_node_t *n1, const if_node
 
 static void dump_ifg(const be_chordal_env_t *env)
 {
-       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;
-  set *edges = env->edges;
-  ir_graph *irg = env->irg;
-  char filename[128];
+       set *edges = env->edges;
+       ir_graph *irg = env->irg;
+       char filename[128];
 
-  ir_snprintf(filename, sizeof(filename), "ifg_%s_%F.dot", env->cls->name, irg);
+       ir_snprintf(filename, sizeof(filename), "ifg_%s_%F.dot", env->cls->name, irg);
 
        if((f = fopen(filename, "wt")) != NULL) {
                bitset_pos_t pos;
@@ -238,10 +155,8 @@ static void dump_ifg(const be_chordal_env_t *env)
                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");
+                       ir_fprintf(f, "\tn%d [label=\"%+F\"]\n", nr, irn);
                }
 
                for(edge = set_first(edges); edge; edge = set_next(edges)) {
@@ -260,6 +175,23 @@ static void dump_ifg(const be_chordal_env_t *env)
 
 #endif /* BUILD_GRAPH */
 
+static void check_border_list(struct list_head *head)
+{
+  border_t *x;
+  list_for_each_entry(border_t, x, head, list) {
+    assert(x->magic == BORDER_FOURCC);
+  }
+}
+
+static void check_heads(be_chordal_env_t *env)
+{
+  pmap_entry *ent;
+  for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
+    /* ir_printf("checking border list of block %+F\n", ent->key); */
+    check_border_list(ent->value);
+  }
+}
+
 
 /**
  * Add an interval border to the list of a block's list
@@ -286,6 +218,7 @@ static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head
 
                /* also allocate the def and tie it to the use. */
                def = obstack_alloc(&env->obst, sizeof(*def));
+    memset(def, 0, sizeof(*def));
                b->other_end = def;
                def->other_end = b;
 
@@ -296,10 +229,8 @@ static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head
                 */
                set_irn_link(irn, def);
 
-#ifdef DEBUG_libfirm
                b->magic = BORDER_FOURCC;
                def->magic = BORDER_FOURCC;
-#endif
        }
 
        /*
@@ -310,9 +241,7 @@ static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head
        else {
                b = get_irn_link(irn);
 
-#ifdef DEBUG_libfirm
                assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
-#endif
        }
 
        b->pressure = pressure;
@@ -320,13 +249,21 @@ static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head
        b->is_real = is_real;
        b->irn = irn;
        b->step = step;
+  check_heads(env);
        list_add_tail(&b->list, head);
-       DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
+  check_heads(env);
+       DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n",
                                is_def ? "def" : "use", irn, step));
 
+
        return b;
 }
 
+static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
+{
+  return arch_irn_has_reg_class(env->arch_env, irn, arch_pos_make_out(0), env->cls);
+}
+
 /**
  * Annotate the register pressure to the nodes and compute
  * the liveness intervals.
@@ -351,29 +288,29 @@ static void pressure(ir_node *block, void *env_ptr)
        unsigned step = 0;
        unsigned pressure = 0;
        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;
+       pset *live_in = put_live_in(block, pset_new_ptr_default());
+       pset *live_end = put_live_end(block, pset_new_ptr_default());
+       const arch_register_class_t *cls = env->cls;
 
-       DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
+       DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
        bitset_clear_all(live);
 
        /* Set up the border list in the block info */
-  head = obstack_alloc(&env->obst, sizeof(*head));
+       head = obstack_alloc(&env->obst, sizeof(*head));
        INIT_LIST_HEAD(head);
-  pmap_insert(env->border_heads, block, head);
+  assert(pmap_get(env->border_heads, block) == NULL);
+       pmap_insert(env->border_heads, block, head);
 
        /*
         * Make final uses of all values live out of the block.
-        * They are neccessary to build up real intervals.
+        * They are necessary to build up real intervals.
         */
        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)));
+               DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
                bitset_set(live, get_irn_graph_nr(irn));
-    if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls))
-      border_use(irn, step, 0);
+               if(has_reg_class(env, irn))
+                       border_use(irn, step, 0);
        }
-
        ++step;
 
        /*
@@ -381,23 +318,23 @@ static void pressure(ir_node *block, void *env_ptr)
         * relevant for the interval borders.
         */
        sched_foreach_reverse(block, irn) {
-               DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
+               DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
                DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
 
-    /*
-     * 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;
+           /*
+            * If the node defines some value, which can put into a
+            * register of the current class, make a border for it.
+            */
+               if(has_reg_class(env, irn)) {
+                       bitset_pos_t elm;
                        int nr = get_irn_graph_nr(irn);
 
                        bitset_clear(live, nr);
                        border_def(irn, step, 1);
 
 #ifdef BUILD_GRAPH
-      bitset_foreach(live, elm)
-        add_if(env, nr, (int) elm);
+                       bitset_foreach(live, elm)
+                               add_if(env, nr, (int) elm);
 #endif
                }
 
@@ -408,10 +345,10 @@ 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(arch_irn_has_reg_class(env->arch_env, op, 0, cls)) {
+                               if(has_reg_class(env, op)) {
                                        int nr = get_irn_graph_nr(op);
 
-                                       DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
+                                       DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
 
                                        if(!bitset_is_set(live, nr)) {
                                                border_use(op, step, 1);
@@ -420,7 +357,6 @@ static void pressure(ir_node *block, void *env_ptr)
                                }
                        }
                }
-
                ++step;
        }
 
@@ -428,7 +364,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(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
+               if(has_reg_class(env, irn)) {
 
                        /* Mark the value live in. */
                        bitset_set(live, get_irn_graph_nr(irn));
@@ -437,6 +373,11 @@ static void pressure(ir_node *block, void *env_ptr)
                        border_def(irn, step, 0);
                }
        }
+
+  check_heads(env);
+
+  del_pset(live_in);
+  del_pset(live_end);
 }
 
 static void assign(ir_node *block, void *env_ptr)
@@ -446,7 +387,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;
-  const arch_register_class_t *cls = env->cls;
+       const arch_register_class_t *cls = env->cls;
 
        /* Mark the obstack level and allocate the temporary tmp_colors */
        void *obstack_level = obstack_base(obst);
@@ -455,16 +396,19 @@ static void assign(ir_node *block, void *env_ptr)
        const ir_node *irn;
        border_t *b;
        struct list_head *head = get_block_border_head(env, block);
-       pset *live_in = get_live_in(block);
+       pset *live_in = put_live_in(block, pset_new_ptr_default());
+
+  check_heads(env);
+
 
        bitset_clear_all(live);
        bitset_clear_all(colors);
        bitset_clear_all(in_colors);
 
-       DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
+       DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
        DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
        list_for_each_entry(border_t, b, head, list) {
-               DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
+               DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
                                        b->irn, get_irn_graph_nr(b->irn)));
        }
 
@@ -474,11 +418,11 @@ 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(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
+               if(has_reg_class(env, irn)) {
       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");
+      assert(reg && "Node must have been assigned a register");
                        col = arch_register_get_index(reg);
 
                        /* Mark the color of the live in value as used. */
@@ -520,7 +464,7 @@ static void assign(ir_node *block, void *env_ptr)
                        bitset_set(live, nr);
 
                        arch_set_irn_register(env->arch_env, irn, 0, reg);
-                       DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %n\n",
+                       DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
             arch_register_get_name(reg), col, irn));
                }
 
@@ -541,12 +485,14 @@ static void assign(ir_node *block, void *env_ptr)
 
        /* Free the auxillary data on the obstack. */
        obstack_free(obst, obstack_level);
+
+  del_pset(live_in);
 }
 
 void be_ra_chordal_init(void)
 {
        dbg = firm_dbg_register(DBG_CHORDAL);
-       firm_dbg_set_mask(dbg, 0);
+       firm_dbg_set_mask(dbg, DBG_LEVEL);
 }
 
 be_chordal_env_t *be_ra_chordal(ir_graph *irg,
@@ -554,7 +500,7 @@ be_chordal_env_t *be_ra_chordal(ir_graph *irg,
     const arch_register_class_t *cls)
 {
        int node_count = get_graph_node_count(irg);
-  int colors_n = arch_register_class_n_regs(cls);
+       int colors_n = arch_register_class_n_regs(cls);
        be_chordal_env_t *env = malloc(sizeof(*env));
 
        if(get_irg_dom_state(irg) != dom_consistent)
@@ -571,10 +517,10 @@ be_chordal_env_t *be_ra_chordal(ir_graph *irg,
        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;
-  env->border_heads = pmap_create();
+       env->cls = cls;
+       env->arch_env = arch_env;
+       env->irg = irg;
+       env->border_heads = pmap_create();
 
        /* First, determine the pressure */
        dom_tree_walk_irg(irg, pressure, NULL, env);
@@ -586,25 +532,68 @@ be_chordal_env_t *be_ra_chordal(ir_graph *irg,
        dom_tree_walk_irg(irg, assign, NULL, env);
 
 #ifdef DUMP_IFG
-  dump_ifg(env);
+       dump_ifg(env);
 #endif
 
 #ifdef DUMP_INTERVALS
        {
                char buf[128];
-    plotter_t *plotter;
+       plotter_t *plotter;
 
                ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
-    plotter = new_plotter_ps(buf);
+       plotter = new_plotter_ps(buf);
 
-    draw_interval_tree(&draw_chordal_def_opts, env, plotter, arch_env, cls);
-    plotter_free(plotter);
+       draw_interval_tree(&draw_chordal_def_opts, env, plotter, arch_env, cls);
+       plotter_free(plotter);
        }
 #endif
+       return env;
+}
+
+void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
+       const arch_env_t *arch_env;
+       struct obstack ob;
+       pmap_entry *pme;
+       ir_node **nodes, *n1, *n2;
+       int i, o;
+
+       arch_env = chordal_env->arch_env;
+
+       /* Collect all irns */
+       obstack_init(&ob);
+       pmap_foreach(chordal_env->border_heads, pme) {
+               border_t *curr;
+               struct list_head *head = pme->value;
+               list_for_each_entry(border_t, curr, head, list)
+                       if (curr->is_def && curr->is_real)
+                               if (arch_get_irn_reg_class(arch_env, curr->irn, arch_pos_make_out(0)) == chordal_env->cls)
+                                       obstack_ptr_grow(&ob, curr->irn);
+       }
+       obstack_ptr_grow(&ob, NULL);
+       nodes = (ir_node **) obstack_finish(&ob);
 
-  return env;
+       /* Check them */
+       for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
+               const arch_register_t *n1_reg, *n2_reg;
+
+               n1_reg = arch_get_irn_register(arch_env, n1, 0);
+               if (!arch_reg_is_allocatable(arch_env, n1, arch_pos_make_out(0), n1_reg)) {
+                       DBG((dbg, 0, "Register assigned to %+F is not allowed\n", n1));
+                       assert(0 && "Register constraint does not hold");
+               }
+               for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
+                       n2_reg = arch_get_irn_register(arch_env, n2, 0);
+                       if (nodes_interfere(chordal_env, n1, n2) && n1_reg == n2_reg) {
+                               DBG((dbg, 0, "Values %+F and %+F interfere and have the same regiseter assigned\n", n1, n2));
+                               assert(0 && "Interfering values have the same color!");
+                       }
+               }
+       }
+       obstack_free(&ob, NULL);
 }
 
+/* BETTER #ifdef BUILD_GRAPH --> faster version of checker with edges */
+
 void be_ra_chordal_done(be_chordal_env_t *env)
 {
 #ifdef BUILD_GRAPH
@@ -622,6 +611,8 @@ void be_ra_chordal_done(be_chordal_env_t *env)
        free(env);
 }
 
+
+
 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
 {
 #ifdef BUILD_GRAPH