Handle infinity entries correctly when normalize a cost matrix.
[libfirm] / heuristical.c
index 9ff2bdb..75eee76 100644 (file)
@@ -17,6 +17,19 @@ static pbqp_node **node_buckets[4];
 static pbqp_node **reduced_bucket = NULL;
 static int         buckets_filled = 0;
 
+static void insert_into_edge_bucket(pbqp_edge *edge)
+{
+       unsigned bucket_len = ARR_LEN(edge_bucket);
+
+       if (edge->bucket_index < bucket_len && edge_bucket[edge->bucket_index]
+                       == edge)
+               /* Edge is already inserted. */
+               return;
+
+       edge->bucket_index = bucket_len;
+       ARR_APP1(pbqp_edge *, edge_bucket, edge);
+}
+
 static void init_buckets(void)
 {
        int i;
@@ -35,10 +48,14 @@ static void free_buckets(void)
 
        for (i = 0; i < 4; ++i) {
                DEL_ARR_F(node_buckets[i]);
+               node_buckets[i] = NULL;
        }
 
        DEL_ARR_F(edge_bucket);
+       edge_bucket = NULL;
+
        DEL_ARR_F(reduced_bucket);
+       reduced_bucket = NULL;
 
        buckets_filled = 0;
 }
@@ -109,11 +126,17 @@ static void normalize_towards_source(pbqp *pbqp, pbqp_edge *edge)
                num min = pbqp_matrix_get_row_min(mat, src_index, tgt_vec);
 
                if (min != 0) {
-                       pbqp_matrix_sub_row_value(mat, src_index, tgt_vec, min);
+                       if (src_vec->entries[src_index].data == INF_COSTS) {
+                               pbqp_matrix_set_row_value(mat, src_index, 0);
+                       } else {
+                               pbqp_matrix_sub_row_value(mat, src_index, tgt_vec, min);
+                       }
                        src_vec->entries[src_index].data = pbqp_add(
                                        src_vec->entries[src_index].data, min);
 
-                       // TODO add to edge_list if inf
+                       if (min == INF_COSTS) {
+                               insert_into_edge_bucket(edge);
+                       }
                }
        }
 }
@@ -154,11 +177,17 @@ static void normalize_towards_target(pbqp *pbqp, pbqp_edge *edge)
                num min = pbqp_matrix_get_col_min(mat, tgt_index, src_vec);
 
                if (min != 0) {
-                       pbqp_matrix_sub_col_value(mat, tgt_index, src_vec, min);
+                       if (tgt_vec->entries[tgt_index].data == INF_COSTS) {
+                               pbqp_matrix_set_col_value(mat, tgt_index, 0);
+                       } else {
+                               pbqp_matrix_sub_col_value(mat, tgt_index, src_vec, min);
+                       }
                        tgt_vec->entries[tgt_index].data = pbqp_add(
                                        tgt_vec->entries[tgt_index].data, min);
 
-                       // TODO add to edge_list if inf
+                       if (min == INF_COSTS) {
+                               insert_into_edge_bucket(edge);
+                       }
                }
        }
 }
@@ -187,11 +216,13 @@ static void reorder_node(pbqp_node *node)
        old_bucket_len   = ARR_LEN(old_bucket);
        old_bucket_index = node->bucket_index;
 
-       if (old_bucket_len <= old_bucket_index ||
-           old_bucket[old_bucket_index] != node) {
+       if (old_bucket_len <= old_bucket_index || old_bucket[old_bucket_index]
+                       != node) {
+               unsigned bucket_len = ARR_LEN(node_buckets[arity]);
+
                /* Old arity is new arity, so we have nothing to do. */
-               assert(old_bucket_index < ARR_LEN(node_buckets[arity]) &&
-                      node_buckets[arity][old_bucket_index] == node);
+               assert(old_bucket_index < bucket_len);
+               assert(node_buckets[arity][old_bucket_index] == node);
                return;
        }
 
@@ -226,6 +257,10 @@ static void simplify_edge(pbqp *pbqp, pbqp_edge *edge)
        assert(src_node);
        assert(tgt_node);
 
+       /* If edge are already deleted, we have nothing to do. */
+       if (!is_connected(src_node, edge) || !is_connected(tgt_node, edge))
+               return;
+
        if (pbqp->dump_file) {
                char txt[100];
                sprintf(txt, "Simplification of Edge n%d-n%d", src_node->index, tgt_node->index);
@@ -312,13 +347,13 @@ void solve_pbqp_heuristical(pbqp *pbqp)
 
        for (;;) {
                if (ARR_LEN(edge_bucket) > 0) {
-                       panic("Please implement edge simplification");
+                       apply_edge(pbqp);
                } else if (ARR_LEN(node_buckets[1]) > 0) {
-                       applyRI(pbqp);
+                       apply_RI(pbqp);
                } else if (ARR_LEN(node_buckets[2]) > 0) {
-                       applyRII(pbqp);
+                       apply_RII(pbqp);
                } else if (ARR_LEN(node_buckets[3]) > 0) {
-                       panic("Please implement RN simplification");
+                       apply_RN(pbqp);
                } else {
                        break;
                }
@@ -372,7 +407,17 @@ void solve_pbqp_heuristical(pbqp *pbqp)
        free_buckets();
 }
 
-void applyRI(pbqp *pbqp)
+void apply_edge(pbqp *pbqp)
+{
+       unsigned   bucket_len = ARR_LEN(edge_bucket);
+       pbqp_edge *edge       = edge_bucket[bucket_len - 1];
+
+       ARR_SHRINKLEN(edge_bucket, (int)bucket_len - 1);
+
+       simplify_edge(pbqp, edge);
+}
+
+void apply_RI(pbqp *pbqp)
 {
        pbqp_node  **bucket     = node_buckets[1];
        unsigned     bucket_len = ARR_LEN(bucket);
@@ -390,7 +435,7 @@ void applyRI(pbqp *pbqp)
 
        if (pbqp->dump_file) {
                char     txt[100];
-               sprintf(txt, "RI-Reduktion of Node n%d", node->index);
+               sprintf(txt, "RI-Reduction of Node n%d", node->index);
                dump_section(pbqp->dump_file, 2, txt);
                pbqp_dump_graph(pbqp);
                fputs("<br>\nBefore reduction:<br>\n", pbqp->dump_file);
@@ -422,7 +467,7 @@ void applyRI(pbqp *pbqp)
        ARR_APP1(pbqp_node *, reduced_bucket, node);
 }
 
-void applyRII(pbqp *pbqp)
+void apply_RII(pbqp *pbqp)
 {
        pbqp_node  **bucket     = node_buckets[2];
        unsigned     bucket_len = ARR_LEN(bucket);
@@ -479,7 +524,7 @@ void applyRII(pbqp *pbqp)
 
        if (pbqp->dump_file) {
                char     txt[100];
-               sprintf(txt, "RII-Reduktion of Node n%d", node->index);
+               sprintf(txt, "RII-Reduction of Node n%d", node->index);
                dump_section(pbqp->dump_file, 2, txt);
                pbqp_dump_graph(pbqp);
                fputs("<br>\nBefore reduction:<br>\n", pbqp->dump_file);
@@ -559,6 +604,81 @@ void applyRII(pbqp *pbqp)
        simplify_edge(pbqp, edge);
 }
 
+void apply_RN(pbqp *pbqp)
+{
+       pbqp_node  **bucket     = node_buckets[3];
+       unsigned     bucket_len = ARR_LEN(bucket);
+       pbqp_node   *node       = bucket[bucket_len - 1];
+       pbqp_edge   *edge;
+       vector      *node_vec   = node->costs;
+       vector      *vec;
+       pbqp_matrix *mat;
+       unsigned     edge_index;
+       unsigned     edge_len   = ARR_LEN(node->edges);
+       unsigned     node_index;
+       unsigned     node_len   = node_vec->len;
+       unsigned     min_index  = 0;
+       num          min        = INF_COSTS;
+       int          is_src;
+
+       assert(pbqp);
+
+       if (pbqp->dump_file) {
+               char     txt[100];
+               sprintf(txt, "RN-Reduction of Node n%d", node->index);
+               dump_section(pbqp->dump_file, 2, txt);
+               pbqp_dump_graph(pbqp);
+       }
+
+       for (node_index = 0; node_index < node_len; ++node_index) {
+               num value = 0;
+
+               for (edge_index = 0; edge_index < edge_len; ++edge_index) {
+                       edge   = node->edges[edge_index];
+                       mat    = edge->costs;
+                       is_src = edge->src == node;
+
+                       if (is_src) {
+                               vec = vector_copy(pbqp, edge->tgt->costs);
+                               vector_add_matrix_row(vec, mat, node_index);
+                       } else {
+                               vec = vector_copy(pbqp, edge->src->costs);
+                               vector_add_matrix_col(vec, mat, node_index);
+                       }
+
+                       value = pbqp_add(value, vector_get_min(vec));
+
+                       obstack_free(&pbqp->obstack, vec);
+               }
+
+               if (value < min) {
+                       min = value;
+                       min_index = node_index;
+               }
+       }
+
+       if (pbqp->dump_file) {
+               fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
+                                       node->index, min_index);
+               fprintf(pbqp->dump_file, "Minimal cost of RN reduction: %d<br>\n",
+                                                       min);
+       }
+
+       node->solution = min_index;
+
+       /* Now that we found the local minimum set all other costs to infinity. */
+       for (node_index = 0; node_index < node_len; ++node_index) {
+               if (node_index != min_index) {
+                       node_vec->entries[node_index].data = INF_COSTS;
+               }
+       }
+
+       /* Add all incident edges to edge bucket, since they are now independent. */
+       for (edge_index = 0; edge_index < edge_len; ++edge_index) {
+               insert_into_edge_bucket(node->edges[edge_index]);
+       }
+}
+
 void back_propagate_RI(pbqp *pbqp, pbqp_node *node)
 {
        pbqp_edge   *edge;