From: Sebastian Buchwald Date: Mon, 6 Oct 2008 10:41:00 +0000 (+0000) Subject: - use RII reduction (no back propagation yet) X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=dab331acb65abde58d635b58de24f676f3eae69b;p=libfirm - use RII reduction (no back propagation yet) - fixed some bugs in RII reduction - added html dump for RII reduction - use pbqp_add function to add to vector/matrix entries [r22523] --- diff --git a/heuristical.c b/heuristical.c index 440373d7f..03221b417 100644 --- a/heuristical.c +++ b/heuristical.c @@ -17,13 +17,6 @@ static pbqp_node **node_buckets[4]; static pbqp_node **reduced_bucket = NULL; static int buckets_filled = 0; -static num add(num x, num y) -{ - if (x == INF_COSTS || y == INF_COSTS) return INF_COSTS; - - return x + y; -} - static void init_buckets(void) { int i; @@ -103,7 +96,7 @@ static void normalize_towards_source(pbqp *pbqp, pbqp_edge *edge) if (min != 0) { pbqp_matrix_sub_row_value(mat, src_index, tgt_vec, min); - src_vec->entries[src_index].data = add( + src_vec->entries[src_index].data = pbqp_add( src_vec->entries[src_index].data, min); // TODO add to edge_list if inf @@ -148,7 +141,7 @@ static void normalize_towards_target(pbqp *pbqp, pbqp_edge *edge) if (min != 0) { pbqp_matrix_sub_col_value(mat, tgt_index, src_vec, min); - tgt_vec->entries[tgt_index].data = add( + tgt_vec->entries[tgt_index].data = pbqp_add( tgt_vec->entries[tgt_index].data, min); // TODO add to edge_list if inf @@ -309,7 +302,7 @@ void solve_pbqp_heuristical(pbqp *pbqp) } else if (ARR_LEN(node_buckets[1]) > 0) { applyRI(pbqp); } else if (ARR_LEN(node_buckets[2]) > 0) { - panic("Please implement RII simplification"); + applyRII(pbqp); } else if (ARR_LEN(node_buckets[3]) > 0) { panic("Please implement RN simplification"); } else { @@ -329,7 +322,7 @@ void solve_pbqp_heuristical(pbqp *pbqp) assert(node); node->solution = vector_get_min_index(node->costs); - pbqp->solution = add(pbqp->solution, + pbqp->solution = pbqp_add(pbqp->solution, node->costs->entries[node->solution].data); if (pbqp->dump_file) { fprintf(pbqp->dump_file, "node n%d is set to %d
\n", node->index, node->solution); @@ -415,7 +408,7 @@ void applyRI(pbqp *pbqp) void applyRII(pbqp *pbqp) { - pbqp_node **bucket = node_buckets[1]; + pbqp_node **bucket = node_buckets[2]; unsigned bucket_len = ARR_LEN(bucket); pbqp_node *node = bucket[bucket_len - 1]; pbqp_edge *src_edge = node->edges[0]; @@ -437,6 +430,8 @@ void applyRII(pbqp *pbqp) unsigned row_len; unsigned node_len; + assert(pbqp); + if (src_is_src) { src_node = src_edge->tgt; } else { @@ -466,6 +461,19 @@ void applyRII(pbqp *pbqp) tgt_is_src = tgt_edge->src == node; } + if (pbqp->dump_file) { + char txt[100]; + sprintf(txt, "RII-Reduktion of Node n%d", node->index); + dump_section(pbqp->dump_file, 2, txt); + pbqp_dump_graph(pbqp); + fputs("
\nBefore reduction:
\n", pbqp->dump_file); + dump_node(pbqp, src_node); + dump_edge(pbqp, src_edge); + dump_node(pbqp, node); + dump_edge(pbqp, tgt_edge); + dump_node(pbqp, tgt_node); + } + src_mat = src_edge->costs; tgt_mat = tgt_edge->costs; @@ -473,9 +481,9 @@ void applyRII(pbqp *pbqp) tgt_vec = tgt_node->costs; node_vec = node->costs; - row_len = ARR_LEN(src_vec); - col_len = ARR_LEN(tgt_vec); - node_len = ARR_LEN(node_vec); + row_len = src_vec->len; + col_len = tgt_vec->len; + node_len = node_vec->len; mat = pbqp_matrix_alloc(pbqp, row_len, col_len); @@ -495,12 +503,23 @@ void applyRII(pbqp *pbqp) vector_add_matrix_row(vec, tgt_mat, col_index); } - mat->entries[row_index * col_len + col_index] = vector_get_min_index(vec); + mat->entries[row_index * col_len + col_index] = vector_get_min(vec); } } pbqp_edge *edge = get_edge(pbqp, src_node->index, tgt_node->index); + /* Disconnect node. */ + disconnect_edge(src_node, src_edge); + disconnect_edge(tgt_node, tgt_edge); + + /* Remove node from bucket... */ + ARR_SHRINKLEN(bucket, (int)bucket_len - 1); + + /* ...and add it to back propagation list. */ + node->bucket_index = ARR_LEN(reduced_bucket); + ARR_APP1(pbqp_node *, reduced_bucket, node); + if (edge == NULL) { edge = alloc_edge(pbqp, src_node->index, tgt_node->index, mat); } else { @@ -508,11 +527,15 @@ void applyRII(pbqp *pbqp) /* Free local matrix. */ obstack_free(&pbqp->obstack, mat); + + reorder_node(src_node); + reorder_node(tgt_node); } - /* Disconnect node. */ - disconnect_edge(src_node, src_edge); - disconnect_edge(tgt_node, tgt_edge); + if (pbqp->dump_file) { + fputs("
\nAfter reduction:
\n", pbqp->dump_file); + dump_edge(pbqp, edge); + } /* Edge has changed so we simplify it. */ simplify_edge(pbqp, edge); diff --git a/heuristical.h b/heuristical.h index 6df7ae028..a37a077b7 100644 --- a/heuristical.h +++ b/heuristical.h @@ -6,6 +6,7 @@ void solve_pbqp_heuristical(pbqp *pbqp); void applyRI(pbqp *pbqp); +void applyRII(pbqp *pbqp); void back_propagate_RI(pbqp *pbqp, pbqp_node *node); diff --git a/matrix.c b/matrix.c index b25c71ac0..43dda121d 100644 --- a/matrix.c +++ b/matrix.c @@ -65,7 +65,7 @@ void pbqp_matrix_add(pbqp_matrix *sum, pbqp_matrix *summand) len = sum->rows * sum->cols; for (i = 0; i < len; ++i) { - sum->entries[i] += summand->entries[i]; + sum->entries[i] = pbqp_add(sum->entries[i], summand->entries[i]); } } @@ -217,10 +217,8 @@ void pbqp_matrix_add_to_all_cols(pbqp_matrix *mat, vector *vec) for (row_index = 0; row_index < row_len; ++row_index) { num value = vec->entries[row_index].data; for (col_index = 0; col_index < col_len; ++col_index) { - if (mat->entries[row_index * col_len + col_index] == INF_COSTS) - continue; - - mat->entries[row_index * col_len + col_index] += value; + mat->entries[row_index * col_len + col_index] = pbqp_add( + mat->entries[row_index * col_len + col_index], value); } } } @@ -242,10 +240,8 @@ void pbqp_matrix_add_to_all_rows(pbqp_matrix *mat, vector *vec) for (row_index = 0; row_index < row_len; ++row_index) { for (col_index = 0; col_index < col_len; ++col_index) { num value = vec->entries[col_index].data; - if (mat->entries[row_index * col_len + col_index] == INF_COSTS) - continue; - mat->entries[row_index * col_len + col_index] += value; + mat->entries[row_index * col_len + col_index] = pbqp_add(mat->entries[row_index * col_len + col_index], value); } } } diff --git a/vector.c b/vector.c index f7a5b49f8..4f8ce17ee 100644 --- a/vector.c +++ b/vector.c @@ -5,6 +5,13 @@ #include "pbqp_t.h" #include "vector.h" +num pbqp_add(num x, num y) +{ + if (x == INF_COSTS || y == INF_COSTS) return INF_COSTS; + + return x + y; +} + vector *vector_alloc(pbqp *pbqp, unsigned length) { assert(length > 0); @@ -38,13 +45,8 @@ void vector_add(vector *sum, vector *summand) len = sum->len; for (i = 0; i < len; ++i) { - if (sum->entries[i].data == INF_COSTS) continue; - - if (summand->entries[i].data == INF_COSTS) { - sum->entries[i].data = INF_COSTS; - } else { - sum->entries[i].data += summand->entries[i].data; - } + sum->entries[i].data = pbqp_add(sum->entries[i].data, + summand->entries[i].data); } } @@ -72,13 +74,7 @@ void vector_add_value(vector *vec, num value) len = vec->len; for (index = 0; index < len; ++index) { - if (vec->entries[index].data == INF_COSTS) continue; - - if (value == INF_COSTS) { - vec->entries[index].data = INF_COSTS; - } else { - vec->entries[index].data += value; - } + vec->entries[index].data = pbqp_add(vec->entries[index].data, value); } } @@ -95,13 +91,7 @@ void vector_add_matrix_col(vector *vec, pbqp_matrix *mat, unsigned col_index) len = vec->len; for (index = 0; index < len; ++index) { - if (vec->entries[index].data == INF_COSTS) continue; - - if (mat->entries[index * mat->cols + col_index] == INF_COSTS) { - vec->entries[index].data = INF_COSTS; - } else { - vec->entries[index].data += mat->entries[index * mat->cols + col_index]; - } + vec->entries[index].data = pbqp_add(vec->entries[index].data, mat->entries[index * mat->cols + col_index]); } } @@ -118,13 +108,8 @@ void vector_add_matrix_row(vector *vec, pbqp_matrix *mat, unsigned row_index) len = vec->len; for (index = 0; index < len; ++index) { - if (vec->entries[index].data == INF_COSTS) continue; - - if (mat->entries[row_index * mat->cols + index] == INF_COSTS) { - vec->entries[index].data = INF_COSTS; - } else { - vec->entries[index].data += mat->entries[row_index * mat->cols + index]; - } + vec->entries[index].data = pbqp_add(vec->entries[index].data, + mat->entries[row_index * mat->cols + index]); } } diff --git a/vector.h b/vector.h index 80433a06c..4c20db66b 100644 --- a/vector.h +++ b/vector.h @@ -3,6 +3,8 @@ #include "vector_t.h" +num pbqp_add(num x, num y); + vector *vector_alloc(pbqp *pbqp, unsigned length); /* Copy the given vector. */ @@ -22,6 +24,7 @@ void vector_add_value(vector *vec, num value); void vector_add_matrix_col(vector *vec, pbqp_matrix *mat, unsigned col_index); void vector_add_matrix_row(vector *vec, pbqp_matrix *mat, unsigned row_index); +num vector_get_min(vector *vec); unsigned vector_get_min_index(vector *vec); #endif /* KAPS_VECTOR_H */