Added pop function for edge_bucket.
[libfirm] / heuristical.c
index 8b1e142..e3eb5e6 100644 (file)
@@ -2,6 +2,7 @@
 #include "assert.h"
 #include "error.h"
 
+#include "bucket.h"
 #include "heuristical.h"
 #include "html_dumper.h"
 #include "kaps.h"
@@ -17,6 +18,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 = edge_bucket_get_length(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;
@@ -68,7 +82,7 @@ static void fill_node_buckets(pbqp *pbqp)
                        arity = 3;
                }
 
-               node->bucket_index = ARR_LEN(node_buckets[arity]);
+               node->bucket_index = node_bucket_get_length(node_buckets[arity]);
 
                ARR_APP1(pbqp_node *, node_buckets[arity], node);
        }
@@ -113,11 +127,25 @@ 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) {
+                               unsigned edge_index;
+                               unsigned edge_len = ARR_LEN(src_node->edges);
+
+                               for (edge_index = 0; edge_index < edge_len; ++edge_index) {
+                                       pbqp_edge *edge_candidate = src_node->edges[edge_index];
+                                       if (edge_candidate != edge) {
+                                               insert_into_edge_bucket(edge_candidate);
+                                       }
+                               }
+                       }
                }
        }
 }
@@ -158,11 +186,25 @@ 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) {
+                               unsigned edge_index;
+                               unsigned edge_len = ARR_LEN(tgt_node->edges);
+
+                               for (edge_index = 0; edge_index < edge_len; ++edge_index) {
+                                       pbqp_edge *edge_candidate = tgt_node->edges[edge_index];
+                                       if (edge_candidate != edge) {
+                                               insert_into_edge_bucket(edge_candidate);
+                                       }
+                               }
+                       }
                }
        }
 }
@@ -188,14 +230,16 @@ static void reorder_node(pbqp_node *node)
        /* Assume node lost one incident edge. */
        old_arity        = arity + 1;
        old_bucket       = node_buckets[old_arity];
-       old_bucket_len   = ARR_LEN(old_bucket);
+       old_bucket_len   = node_bucket_get_length(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 = node_bucket_get_length(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;
        }
 
@@ -208,7 +252,7 @@ static void reorder_node(pbqp_node *node)
        ARR_SHRINKLEN(node_buckets[old_arity], old_bucket_len - 1);
 
        /* ..and add to new one. */
-       node->bucket_index = ARR_LEN(node_buckets[arity]);
+       node->bucket_index = node_bucket_get_length(node_buckets[arity]);
        ARR_APP1(pbqp_node*, node_buckets[arity], node);
 }
 
@@ -230,6 +274,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);
@@ -315,14 +363,14 @@ void solve_pbqp_heuristical(pbqp *pbqp)
        fill_node_buckets(pbqp);
 
        for (;;) {
-               if (ARR_LEN(edge_bucket) > 0) {
-                       panic("Please implement edge simplification");
-               } else if (ARR_LEN(node_buckets[1]) > 0) {
+               if (edge_bucket_get_length(edge_bucket) > 0) {
+                       apply_edge(pbqp);
+               } else if (node_bucket_get_length(node_buckets[1]) > 0) {
                        apply_RI(pbqp);
-               } else if (ARR_LEN(node_buckets[2]) > 0) {
+               } else if (node_bucket_get_length(node_buckets[2]) > 0) {
                        apply_RII(pbqp);
-               } else if (ARR_LEN(node_buckets[3]) > 0) {
-                       panic("Please implement RN simplification");
+               } else if (node_bucket_get_length(node_buckets[3]) > 0) {
+                       apply_RN(pbqp);
                } else {
                        break;
                }
@@ -334,7 +382,7 @@ void solve_pbqp_heuristical(pbqp *pbqp)
        }
 
        /* Solve trivial nodes and calculate solution. */
-       node_len = ARR_LEN(node_buckets[0]);
+       node_len = node_bucket_get_length(node_buckets[0]);
        for (node_index = 0; node_index < node_len; ++node_index) {
                pbqp_node *node = node_buckets[0][node_index];
                assert(node);
@@ -350,12 +398,12 @@ void solve_pbqp_heuristical(pbqp *pbqp)
 
        if (pbqp->dump_file) {
                dump_section(pbqp->dump_file, 2, "Minimum");
-               fprintf(pbqp->dump_file, "Minimum is equal to %d.", pbqp->solution);
+               fprintf(pbqp->dump_file, "Minimum is equal to %lld.", pbqp->solution);
                dump_section(pbqp->dump_file, 2, "Back Propagation");
        }
 
        /* Solve reduced nodes. */
-       node_len = ARR_LEN(reduced_bucket);
+       node_len = node_bucket_get_length(reduced_bucket);
        for (node_index = node_len; node_index > 0; --node_index) {
                pbqp_node *node = reduced_bucket[node_index - 1];
                assert(node);
@@ -376,10 +424,17 @@ void solve_pbqp_heuristical(pbqp *pbqp)
        free_buckets();
 }
 
+void apply_edge(pbqp *pbqp)
+{
+       pbqp_edge *edge = edge_bucket_pop(&edge_bucket);
+
+       simplify_edge(pbqp, edge);
+}
+
 void apply_RI(pbqp *pbqp)
 {
        pbqp_node  **bucket     = node_buckets[1];
-       unsigned     bucket_len = ARR_LEN(bucket);
+       unsigned     bucket_len = node_bucket_get_length(bucket);
        pbqp_node   *node       = bucket[bucket_len - 1];
        pbqp_edge   *edge       = node->edges[0];
        pbqp_matrix *mat        = edge->costs;
@@ -394,7 +449,7 @@ void apply_RI(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,14 +477,14 @@ void apply_RI(pbqp *pbqp)
        reorder_node(other_node);
 
        /* ...and add it to back propagation list. */
-       node->bucket_index = ARR_LEN(reduced_bucket);
+       node->bucket_index = node_bucket_get_length(reduced_bucket);
        ARR_APP1(pbqp_node *, reduced_bucket, node);
 }
 
 void apply_RII(pbqp *pbqp)
 {
        pbqp_node  **bucket     = node_buckets[2];
-       unsigned     bucket_len = ARR_LEN(bucket);
+       unsigned     bucket_len = node_bucket_get_length(bucket);
        pbqp_node   *node       = bucket[bucket_len - 1];
        pbqp_edge   *src_edge   = node->edges[0];
        pbqp_edge   *tgt_edge   = node->edges[1];
@@ -483,7 +538,7 @@ void apply_RII(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);
@@ -539,7 +594,7 @@ void apply_RII(pbqp *pbqp)
        ARR_SHRINKLEN(bucket, (int)bucket_len - 1);
 
        /* ...and add it to back propagation list. */
-       node->bucket_index = ARR_LEN(reduced_bucket);
+       node->bucket_index = node_bucket_get_length(reduced_bucket);
        ARR_APP1(pbqp_node *, reduced_bucket, node);
 
        if (edge == NULL) {
@@ -565,27 +620,49 @@ void apply_RII(pbqp *pbqp)
 
 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_node  **bucket       = node_buckets[3];
+       unsigned     bucket_len   = node_bucket_get_length(bucket);
+       unsigned     bucket_index;
+       pbqp_node   *node         = NULL;
        pbqp_edge   *edge;
-       vector      *node_vec   = node->costs;
+       vector      *node_vec;
        vector      *vec;
        pbqp_matrix *mat;
        unsigned     edge_index;
-       unsigned     edge_len   = ARR_LEN(node->edges);
+       unsigned     max_degree   = 0;
        unsigned     node_index;
-       unsigned     node_len   = ARR_LEN(node_vec);
-       unsigned     min_index  = 0;
-       num          min        = INF_COSTS;
+       unsigned     node_len;
+       unsigned     min_index    = 0;
+       num          min          = INF_COSTS;
        int          is_src;
 
        assert(pbqp);
 
+       /* Search for node with maximum degree. */
+       for (bucket_index = 0; bucket_index < bucket_len; ++bucket_index) {
+               pbqp_node *candidate = bucket[bucket_index];
+               unsigned   degree    = ARR_LEN(candidate->edges);
+
+               if (degree > max_degree) {
+                       node = candidate;
+                       max_degree = degree;
+               }
+       }
+       assert(node);
+       node_vec = node->costs;
+       node_len = node_vec->len;
+
+       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;
+               num value = node_vec->entries[node_index].data;
 
-               for (edge_index = 0; edge_index < edge_len; ++edge_index) {
+               for (edge_index = 0; edge_index < max_degree; ++edge_index) {
                        edge   = node->edges[edge_index];
                        mat    = edge->costs;
                        is_src = edge->src == node;
@@ -609,6 +686,13 @@ void apply_RN(pbqp *pbqp)
                }
        }
 
+       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: %lld<br>\n",
+                                                       min);
+       }
+
        node->solution = min_index;
 
        /* Now that we found the local minimum set all other costs to infinity. */
@@ -619,8 +703,8 @@ void apply_RN(pbqp *pbqp)
        }
 
        /* Add all incident edges to edge bucket, since they are now independent. */
-       for (edge_index = 0; edge_index < edge_len; ++edge_index) {
-               ARR_APP1(pbqp_edge *, edge_bucket, node->edges[node_index]);
+       for (edge_index = 0; edge_index < max_degree; ++edge_index) {
+               insert_into_edge_bucket(node->edges[edge_index]);
        }
 }
 
@@ -739,7 +823,7 @@ int node_is_reduced(pbqp_node *node)
        assert(node);
        if (ARR_LEN(node->edges) == 0) return 1;
 
-       unsigned bucket_length = ARR_LEN(reduced_bucket);
+       unsigned bucket_length = node_bucket_get_length(reduced_bucket);
        unsigned bucket_index  = node->bucket_index;
 
        return bucket_index < bucket_length && reduced_bucket[bucket_index] == node;