X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_tab.c;h=e4cc386679d8760504af441f0741f9241120f479;hb=761e8797207a4953b2da4ddaf83b987b09fa366f;hp=da644532ef07d111b6a497bef0eccd320be02eae;hpb=77217749765eda514eb091e6986549152cd8e624;p=platform%2Fupstream%2Fisl.git diff --git a/isl_tab.c b/isl_tab.c index da64453..e4cc386 100644 --- a/isl_tab.c +++ b/isl_tab.c @@ -1,16 +1,20 @@ /* * Copyright 2008-2009 Katholieke Universiteit Leuven + * Copyright 2013 Ecole Normale Superieure * - * Use of this software is governed by the GNU LGPLv2.1 license + * Use of this software is governed by the MIT license * * Written by Sven Verdoolaege, K.U.Leuven, Departement * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium + * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France */ -#include "isl_mat.h" +#include +#include #include "isl_map_private.h" #include "isl_tab.h" -#include "isl_seq.h" +#include +#include /* * The implementation of tableaus in this file was inspired by Section 8 @@ -64,6 +68,7 @@ struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx, tab->n_div = 0; tab->n_dead = 0; tab->n_redundant = 0; + tab->strict_redundant = 0; tab->need_undo = 0; tab->rational = 0; tab->empty = 0; @@ -84,13 +89,20 @@ error: return NULL; } +isl_ctx *isl_tab_get_ctx(struct isl_tab *tab) +{ + return tab ? isl_mat_get_ctx(tab->mat) : NULL; +} + int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new) { - unsigned off = 2 + tab->M; + unsigned off; if (!tab) return -1; + off = 2 + tab->M; + if (tab->max_con < tab->n_con + n_new) { struct isl_tab_var *con; @@ -168,13 +180,24 @@ struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new) return NULL; } +static void free_undo_record(struct isl_tab_undo *undo) +{ + switch (undo->type) { + case isl_tab_undo_saved_basis: + free(undo->u.col_var); + break; + default:; + } + free(undo); +} + static void free_undo(struct isl_tab *tab) { struct isl_tab_undo *undo, *next; for (undo = tab->top; undo && undo != &tab->bottom; undo = next) { next = undo->next; - free(undo); + free_undo_record(undo); } tab->top = undo; } @@ -208,34 +231,34 @@ struct isl_tab *isl_tab_dup(struct isl_tab *tab) return NULL; off = 2 + tab->M; - dup = isl_calloc_type(tab->ctx, struct isl_tab); + dup = isl_calloc_type(tab->mat->ctx, struct isl_tab); if (!dup) return NULL; dup->mat = isl_mat_dup(tab->mat); if (!dup->mat) goto error; - dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var); + dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var); if (!dup->var) goto error; for (i = 0; i < tab->n_var; ++i) dup->var[i] = tab->var[i]; - dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con); + dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con); if (!dup->con) goto error; for (i = 0; i < tab->n_con; ++i) dup->con[i] = tab->con[i]; - dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off); + dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off); if (!dup->col_var) goto error; for (i = 0; i < tab->n_col; ++i) dup->col_var[i] = tab->col_var[i]; - dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row); + dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row); if (!dup->row_var) goto error; for (i = 0; i < tab->n_row; ++i) dup->row_var[i] = tab->row_var[i]; if (tab->row_sign) { - dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign, + dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign, tab->mat->n_row); if (!dup->row_sign) goto error; @@ -266,6 +289,7 @@ struct isl_tab *isl_tab_dup(struct isl_tab *tab) dup->n_redundant = tab->n_redundant; dup->rational = tab->rational; dup->empty = tab->empty; + dup->strict_redundant = 0; dup->need_undo = 0; dup->in_undo = 0; dup->M = tab->M; @@ -308,6 +332,8 @@ static struct isl_mat *tab_mat_product(struct isl_mat *mat1, prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row, off + col1 + col2); + if (!prod) + return NULL; n = 0; for (i = 0; i < r1; ++i) { @@ -516,6 +542,7 @@ struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2) prod->n_redundant = tab1->n_redundant + tab2->n_redundant; prod->rational = tab1->rational; prod->empty = tab1->empty || tab2->empty; + prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant; prod->need_undo = 0; prod->in_undo = 0; prod->M = tab1->M; @@ -723,6 +750,8 @@ int isl_tab_row_is_redundant(struct isl_tab *tab, int row) if (isl_int_is_neg(tab->mat->row[row][1])) return 0; + if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1])) + return 0; if (tab->M && isl_int_is_neg(tab->mat->row[row][2])) return 0; @@ -742,6 +771,8 @@ int isl_tab_row_is_redundant(struct isl_tab *tab, int row) static void swap_rows(struct isl_tab *tab, int row1, int row2) { int t; + enum isl_tab_row_sign s; + t = tab->row_var[row1]; tab->row_var[row1] = tab->row_var[row2]; tab->row_var[row2] = t; @@ -751,9 +782,9 @@ static void swap_rows(struct isl_tab *tab, int row1, int row2) if (!tab->row_sign) return; - t = tab->row_sign[row1]; + s = tab->row_sign[row1]; tab->row_sign[row1] = tab->row_sign[row2]; - tab->row_sign[row2] = t; + tab->row_sign[row2] = s; } static int push_union(struct isl_tab *tab, @@ -763,6 +794,8 @@ static int push_union(struct isl_tab *tab, { struct isl_tab_undo *undo; + if (!tab) + return -1; if (!tab->need_undo) return 0; @@ -915,7 +948,7 @@ int isl_tab_mark_redundant(struct isl_tab *tab, int row) struct isl_tab_var *var = isl_tab_var_from_row(tab, row); var->is_redundant = 1; isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1); - if (tab->need_undo || tab->row_var[row] >= 0) { + if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) { if (tab->row_var[row] >= 0 && !var->is_nonneg) { var->is_nonneg = 1; if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0) @@ -1072,6 +1105,11 @@ int isl_tab_pivot(struct isl_tab *tab, int row, int col) struct isl_tab_var *var; unsigned off = 2 + tab->M; + if (tab->mat->ctx->abort) { + isl_ctx_set_error(tab->mat->ctx, isl_error_abort); + return -1; + } + isl_int_swap(mat->row[row][0], mat->row[row][off + col]); sgn = isl_int_sgn(mat->row[row][0]); if (sgn < 0) { @@ -1159,6 +1197,11 @@ static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) return isl_tab_pivot(tab, r, var->index); } +/* Check whether all variables that are marked as non-negative + * also have a non-negative sample value. This function is not + * called from the current code but is useful during debugging. + */ +static void check_table(struct isl_tab *tab) __attribute__ ((unused)); static void check_table(struct isl_tab *tab) { int i; @@ -1171,11 +1214,13 @@ static void check_table(struct isl_tab *tab) if (!var->is_nonneg) continue; if (tab->M) { - assert(!isl_int_is_neg(tab->mat->row[i][2])); + isl_assert(tab->mat->ctx, + !isl_int_is_neg(tab->mat->row[i][2]), abort()); if (isl_int_is_pos(tab->mat->row[i][2])) continue; } - assert(!isl_int_is_neg(tab->mat->row[i][1])); + isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]), + abort()); } } @@ -1210,6 +1255,20 @@ static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var) return 1; } +int isl_tab_sign_of_max(struct isl_tab *tab, int con) +{ + struct isl_tab_var *var; + + if (!tab) + return -2; + + var = &tab->con[con]; + isl_assert(tab->mat->ctx, !var->is_redundant, return -2); + isl_assert(tab->mat->ctx, !var->is_zero, return -2); + + return sign_of_max(tab, var); +} + static int row_is_neg(struct isl_tab *tab, int row) { if (!tab->M) @@ -1361,7 +1420,8 @@ static int row_at_most_neg_one(struct isl_tab *tab, int row) * Return 0 otherwise. * * The sample value of "var" is assumed to be non-negative when the - * the function is called and will be made non-negative again before + * the function is called. If 1 is returned then the constraint + * is not redundant and the sample value is made non-negative again before * the function returns. */ int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var) @@ -1396,8 +1456,11 @@ int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var) return 0; do { find_pivot(tab, var, var, -1, &row, &col); - if (row == var->index) + if (row == var->index) { + if (restore_row(tab, var) < -1) + return -1; return 1; + } if (row == -1) return 0; pivot_var = var_from_col(tab, col); @@ -1486,6 +1549,43 @@ int isl_tab_kill_col(struct isl_tab *tab, int col) } } +static int row_is_manifestly_non_integral(struct isl_tab *tab, int row) +{ + unsigned off = 2 + tab->M; + + if (tab->M && !isl_int_eq(tab->mat->row[row][2], + tab->mat->row[row][0])) + return 0; + if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead, + tab->n_col - tab->n_dead) != -1) + return 0; + + return !isl_int_is_divisible_by(tab->mat->row[row][1], + tab->mat->row[row][0]); +} + +/* For integer tableaus, check if any of the coordinates are stuck + * at a non-integral value. + */ +static int tab_is_manifestly_empty(struct isl_tab *tab) +{ + int i; + + if (tab->empty) + return 1; + if (tab->rational) + return 0; + + for (i = 0; i < tab->n_var; ++i) { + if (!tab->var[i].is_row) + continue; + if (row_is_manifestly_non_integral(tab, tab->var[i].index)) + return 1; + } + + return 0; +} + /* Row variable "var" is non-negative and cannot attain any values * larger than zero. This means that the coefficients of the unrestricted * column variables are zero and that the coefficients of the non-negative @@ -1507,15 +1607,21 @@ static int close_row(struct isl_tab *tab, struct isl_tab_var *var) if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0) return -1; for (j = tab->n_dead; j < tab->n_col; ++j) { + int recheck; if (isl_int_is_zero(mat->row[var->index][off + j])) continue; isl_assert(tab->mat->ctx, isl_int_is_neg(mat->row[var->index][off + j]), return -1); - if (isl_tab_kill_col(tab, j)) + recheck = isl_tab_kill_col(tab, j); + if (recheck < 0) + return -1; + if (recheck) --j; } if (isl_tab_mark_redundant(tab, var->index) < 0) return -1; + if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0) + return -1; return 0; } @@ -1595,6 +1701,9 @@ int isl_tab_allocate_var(struct isl_tab *tab) * d_r d_r d_r d_x/g m * * with g the gcd of d_r and d_x and m the lcm of d_r and d_x. + * + * If tab->M is set, then, internally, each variable x is represented + * as x' - M. We then also need no subtract k d_r from the coefficient of M. */ int isl_tab_add_row(struct isl_tab *tab, isl_int *line) { @@ -1639,7 +1748,7 @@ int isl_tab_add_row(struct isl_tab *tab, isl_int *line) isl_int_clear(b); if (tab->row_sign) - tab->row_sign[tab->con[r].index] = 0; + tab->row_sign[tab->con[r].index] = isl_tab_row_unknown; return r; } @@ -1798,24 +1907,24 @@ static int row_is_manifestly_zero(struct isl_tab *tab, int row) /* Add an equality that is known to be valid for the given tableau. */ -struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) +int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) { struct isl_tab_var *var; int r; if (!tab) - return NULL; + return -1; r = isl_tab_add_row(tab, eq); if (r < 0) - goto error; + return -1; var = &tab->con[r]; r = var->index; if (row_is_manifestly_zero(tab, r)) { var->is_zero = 1; if (isl_tab_mark_redundant(tab, r) < 0) - goto error; - return tab; + return -1; + return 0; } if (isl_int_is_neg(tab->mat->row[r][1])) { @@ -1825,15 +1934,12 @@ struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) } var->is_nonneg = 1; if (to_col(tab, var) < 0) - goto error; + return -1; var->is_nonneg = 0; if (isl_tab_kill_col(tab, var->index) < 0) - goto error; + return -1; - return tab; -error: - isl_tab_free(tab); - return NULL; + return 0; } static int add_zero_row(struct isl_tab *tab) @@ -1855,7 +1961,7 @@ static int add_zero_row(struct isl_tab *tab) /* Add equality "eq" and check if it conflicts with the * previously added constraints or if it is obviously redundant. */ -struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) +int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) { struct isl_tab_undo *snap = NULL; struct isl_tab_var *var; @@ -1865,8 +1971,8 @@ struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) isl_int cst; if (!tab) - return NULL; - isl_assert(tab->mat->ctx, !tab->M, goto error); + return -1; + isl_assert(tab->mat->ctx, !tab->M, return -1); if (tab->need_undo) snap = isl_tab_snap(tab); @@ -1881,32 +1987,32 @@ struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) isl_int_clear(cst); } if (r < 0) - goto error; + return -1; var = &tab->con[r]; row = var->index; if (row_is_manifestly_zero(tab, row)) { if (snap) { if (isl_tab_rollback(tab, snap) < 0) - goto error; + return -1; } else drop_row(tab, row); - return tab; + return 0; } if (tab->bmap) { tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq); if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0) - goto error; + return -1; isl_seq_neg(eq, eq, 1 + tab->n_var); tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq); isl_seq_neg(eq, eq, 1 + tab->n_var); if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0) - goto error; + return -1; if (!tab->bmap) - goto error; + return -1; if (add_zero_row(tab) < 0) - goto error; + return -1; } sgn = isl_int_sgn(tab->mat->row[row][1]); @@ -1921,25 +2027,22 @@ struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) if (sgn < 0) { sgn = sign_of_max(tab, var); if (sgn < -1) - goto error; + return -1; if (sgn < 0) { if (isl_tab_mark_empty(tab) < 0) - goto error; - return tab; + return -1; + return 0; } } var->is_nonneg = 1; if (to_col(tab, var) < 0) - goto error; + return -1; var->is_nonneg = 0; if (isl_tab_kill_col(tab, var->index) < 0) - goto error; + return -1; - return tab; -error: - isl_tab_free(tab); - return NULL; + return 0; } /* Construct and return an inequality that expresses an upper bound @@ -2028,7 +2131,35 @@ error: return -1; } -/* Add an extra div, prescrived by "div" to the tableau and +/* Check whether the div described by "div" is obviously non-negative. + * If we are using a big parameter, then we will encode the div + * as div' = M + div, which is always non-negative. + * Otherwise, we check whether div is a non-negative affine combination + * of non-negative variables. + */ +static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div) +{ + int i; + + if (tab->M) + return 1; + + if (isl_int_is_neg(div->el[1])) + return 0; + + for (i = 0; i < tab->n_var; ++i) { + if (isl_int_is_neg(div->el[2 + i])) + return 0; + if (isl_int_is_zero(div->el[2 + i])) + continue; + if (!tab->var[i].is_nonneg) + return 0; + } + + return 1; +} + +/* Add an extra div, prescribed by "div" to the tableau and * the associated bmap (which is assumed to be non-NULL). * * If add_ineq is not NULL, then this function is used instead @@ -2040,7 +2171,6 @@ error: int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div, int (*add_ineq)(void *user, isl_int *), void *user) { - int i; int r; int k; int nonneg; @@ -2050,15 +2180,7 @@ int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div, isl_assert(tab->mat->ctx, tab->bmap, return -1); - for (i = 0; i < tab->n_var; ++i) { - if (isl_int_is_neg(div->el[2 + i])) - break; - if (isl_int_is_zero(div->el[2 + i])) - continue; - if (!tab->var[i].is_nonneg) - break; - } - nonneg = i == tab->n_var && !isl_int_is_neg(div->el[1]); + nonneg = div_is_nonneg(tab, div); if (isl_tab_extend_cons(tab, 3) < 0) return -1; @@ -2071,8 +2193,8 @@ int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div, if (nonneg) tab->var[r].is_nonneg = 1; - tab->bmap = isl_basic_map_extend_dim(tab->bmap, - isl_basic_map_get_dim(tab->bmap), 1, 0, 2); + tab->bmap = isl_basic_map_extend_space(tab->bmap, + isl_basic_map_get_space(tab->bmap), 1, 0, 2); k = isl_basic_map_alloc_div(tab->bmap); if (k < 0) return -1; @@ -2086,7 +2208,13 @@ int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div, return r; } -struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap) +/* If "track" is set, then we want to keep track of all constraints in tab + * in its bmap field. This field is initialized from a copy of "bmap", + * so we need to make sure that all constraints in "bmap" also appear + * in the constructed tab. + */ +__isl_give struct isl_tab *isl_tab_from_basic_map( + __isl_keep isl_basic_map *bmap, int track) { int i; struct isl_tab *tab; @@ -2098,11 +2226,12 @@ struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap) isl_basic_map_total_dim(bmap), 0); if (!tab) return NULL; + tab->preserve = track; tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL); if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) { if (isl_tab_mark_empty(tab) < 0) goto error; - return tab; + goto done; } for (i = 0; i < bmap->n_eq; ++i) { tab = add_eq(tab, bmap->eq[i]); @@ -2113,31 +2242,39 @@ struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap) if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0) goto error; if (tab->empty) - return tab; + goto done; } +done: + if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0) + goto error; return tab; error: isl_tab_free(tab); return NULL; } -struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset) +__isl_give struct isl_tab *isl_tab_from_basic_set( + __isl_keep isl_basic_set *bset, int track) { - return isl_tab_from_basic_map((struct isl_basic_map *)bset); + return isl_tab_from_basic_map(bset, track); } /* Construct a tableau corresponding to the recession cone of "bset". */ -struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset) +struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset, + int parametric) { isl_int cst; int i; struct isl_tab *tab; + unsigned offset = 0; if (!bset) return NULL; + if (parametric) + offset = isl_basic_set_dim(bset, isl_dim_param); tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq, - isl_basic_set_total_dim(bset), 0); + isl_basic_set_total_dim(bset) - offset, 0); if (!tab) return NULL; tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL); @@ -2145,17 +2282,21 @@ struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset) isl_int_init(cst); for (i = 0; i < bset->n_eq; ++i) { - isl_int_swap(bset->eq[i][0], cst); - tab = add_eq(tab, bset->eq[i]); - isl_int_swap(bset->eq[i][0], cst); + isl_int_swap(bset->eq[i][offset], cst); + if (offset > 0) { + if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0) + goto error; + } else + tab = add_eq(tab, bset->eq[i]); + isl_int_swap(bset->eq[i][offset], cst); if (!tab) goto done; } for (i = 0; i < bset->n_ineq; ++i) { int r; - isl_int_swap(bset->ineq[i][0], cst); - r = isl_tab_add_row(tab, bset->ineq[i]); - isl_int_swap(bset->ineq[i][0], cst); + isl_int_swap(bset->ineq[i][offset], cst); + r = isl_tab_add_row(tab, bset->ineq[i] + offset); + isl_int_swap(bset->ineq[i][offset], cst); if (r < 0) goto error; tab->con[r].is_nonneg = 1; @@ -2338,8 +2479,7 @@ struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset, * the resulting tableau is empty. * Otherwise, we know the value will be zero and we close the row. */ -static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab, - struct isl_tab_var *var) +static int cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var) { unsigned r; isl_int *row; @@ -2347,12 +2487,12 @@ static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab, unsigned off = 2 + tab->M; if (var->is_zero) - return tab; - isl_assert(tab->mat->ctx, !var->is_redundant, goto error); - isl_assert(tab->mat->ctx, var->is_nonneg, goto error); + return 0; + isl_assert(tab->mat->ctx, !var->is_redundant, return -1); + isl_assert(tab->mat->ctx, var->is_nonneg, return -1); if (isl_tab_extend_cons(tab, 1) < 0) - goto error; + return -1; r = tab->n_con; tab->con[r].index = tab->n_row; @@ -2378,27 +2518,24 @@ static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab, tab->n_row++; tab->n_con++; if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0) - goto error; + return -1; sgn = sign_of_max(tab, &tab->con[r]); if (sgn < -1) - goto error; + return -1; if (sgn < 0) { if (isl_tab_mark_empty(tab) < 0) - goto error; - return tab; + return -1; + return 0; } tab->con[r].is_nonneg = 1; if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0) - goto error; + return -1; /* sgn == 0 */ if (close_row(tab, &tab->con[r]) < 0) - goto error; + return -1; - return tab; -error: - isl_tab_free(tab); - return NULL; + return 0; } /* Given a tableau "tab" and an inequality constraint "con" of the tableau, @@ -2428,6 +2565,13 @@ struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con) var = &tab->con[con]; + if (var->is_row && (var->index < 0 || var->index < tab->n_redundant)) + isl_die(tab->mat->ctx, isl_error_invalid, + "cannot relax redundant constraint", goto error); + if (!var->is_row && (var->index < 0 || var->index < tab->n_dead)) + isl_die(tab->mat->ctx, isl_error_invalid, + "cannot relax dead constraint", goto error); + if (!var->is_row && !max_is_manifestly_unbounded(tab, var)) if (to_row(tab, var, 1) < 0) goto error; @@ -2461,22 +2605,42 @@ error: return NULL; } -struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con) +/* Remove the sign constraint from constraint "con". + * + * If the constraint variable was originally marked non-negative, + * then we make sure we mark it non-negative again during rollback. + */ +int isl_tab_unrestrict(struct isl_tab *tab, int con) +{ + struct isl_tab_var *var; + + if (!tab) + return -1; + + var = &tab->con[con]; + if (!var->is_nonneg) + return 0; + + var->is_nonneg = 0; + if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0) + return -1; + + return 0; +} + +int isl_tab_select_facet(struct isl_tab *tab, int con) { if (!tab) - return NULL; + return -1; return cut_to_hyperplane(tab, &tab->con[con]); } static int may_be_equality(struct isl_tab *tab, int row) { - unsigned off = 2 + tab->M; - return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1]) - : isl_int_lt(tab->mat->row[row][1], - tab->mat->row[row][0])) && - isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead, - tab->n_col - tab->n_dead) != -1; + return tab->rational ? isl_int_is_zero(tab->mat->row[row][1]) + : isl_int_lt(tab->mat->row[row][1], + tab->mat->row[row][0]); } /* Check for (near) equalities among the constraints. @@ -2495,17 +2659,17 @@ static int may_be_equality(struct isl_tab *tab, int row) * tableau is integer), then we restrict the value to being zero * by adding an opposite non-negative variable. */ -struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab) +int isl_tab_detect_implicit_equalities(struct isl_tab *tab) { int i; unsigned n_marked; if (!tab) - return NULL; + return -1; if (tab->empty) - return tab; + return 0; if (tab->n_dead == tab->n_col) - return tab; + return 0; n_marked = 0; for (i = tab->n_redundant; i < tab->n_row; ++i) { @@ -2542,12 +2706,13 @@ struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab) n_marked--; sgn = sign_of_max(tab, var); if (sgn < 0) - goto error; + return -1; if (sgn == 0) { if (close_row(tab, var) < 0) - goto error; + return -1; } else if (!tab->rational && !at_least_one(tab, var)) { - tab = cut_to_hyperplane(tab, var); + if (cut_to_hyperplane(tab, var) < 0) + return -1; return isl_tab_detect_implicit_equalities(tab); } for (i = tab->n_redundant; i < tab->n_row; ++i) { @@ -2561,10 +2726,107 @@ struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab) } } - return tab; -error: - isl_tab_free(tab); - return NULL; + return 0; +} + +/* Update the element of row_var or col_var that corresponds to + * constraint tab->con[i] to a move from position "old" to position "i". + */ +static int update_con_after_move(struct isl_tab *tab, int i, int old) +{ + int *p; + int index; + + index = tab->con[i].index; + if (index == -1) + return 0; + p = tab->con[i].is_row ? tab->row_var : tab->col_var; + if (p[index] != ~old) + isl_die(tab->mat->ctx, isl_error_internal, + "broken internal state", return -1); + p[index] = ~i; + + return 0; +} + +/* Rotate the "n" constraints starting at "first" to the right, + * putting the last constraint in the position of the first constraint. + */ +static int rotate_constraints(struct isl_tab *tab, int first, int n) +{ + int i, last; + struct isl_tab_var var; + + if (n <= 1) + return 0; + + last = first + n - 1; + var = tab->con[last]; + for (i = last; i > first; --i) { + tab->con[i] = tab->con[i - 1]; + if (update_con_after_move(tab, i, i - 1) < 0) + return -1; + } + tab->con[first] = var; + if (update_con_after_move(tab, first, last) < 0) + return -1; + + return 0; +} + +/* Make the equalities that are implicit in "bmap" but that have been + * detected in the corresponding "tab" explicit in "bmap" and update + * "tab" to reflect the new order of the constraints. + * + * In particular, if inequality i is an implicit equality then + * isl_basic_map_inequality_to_equality will move the inequality + * in front of the other equality and it will move the last inequality + * in the position of inequality i. + * In the tableau, the inequalities of "bmap" are stored after the equalities + * and so the original order + * + * E E E E E A A A I B B B B L + * + * is changed into + * + * I E E E E E A A A L B B B B + * + * where I is the implicit equality, the E are equalities, + * the A inequalities before I, the B inequalities after I and + * L the last inequality. + * We therefore need to rotate to the right two sets of constraints, + * those up to and including I and those after I. + * + * If "tab" contains any constraints that are not in "bmap" then they + * appear after those in "bmap" and they should be left untouched. + * + * Note that this function leaves "bmap" in a temporary state + * as it does not call isl_basic_map_gauss. Calling this function + * is the responsibility of the caller. + */ +__isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab, + __isl_take isl_basic_map *bmap) +{ + int i; + + if (!tab || !bmap) + return isl_basic_map_free(bmap); + if (tab->empty) + return bmap; + + for (i = bmap->n_ineq - 1; i >= 0; --i) { + if (!isl_tab_is_equality(tab, bmap->n_eq + i)) + continue; + isl_basic_map_inequality_to_equality(bmap, i); + if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0) + return isl_basic_map_free(bmap); + if (rotate_constraints(tab, tab->n_eq + i + 1, + bmap->n_ineq - i) < 0) + return isl_basic_map_free(bmap); + tab->n_eq++; + } + + return bmap; } static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var) @@ -2680,16 +2942,17 @@ int isl_tab_is_equality(struct isl_tab *tab, int con) off = 2 + tab->M; return isl_int_is_zero(tab->mat->row[row][1]) && - isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead, + (!tab->M || isl_int_is_zero(tab->mat->row[row][2])) && + isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead, tab->n_col - tab->n_dead) == -1; } -/* Return the minimial value of the affine expression "f" with denominator +/* Return the minimal value of the affine expression "f" with denominator * "denom" in *opt, *opt_denom, assuming the tableau is not empty and * the expression cannot attain arbitrarily small values. * If opt_denom is NULL, then *opt is rounded up to the nearest integer. * The return value reflects the nature of the result (empty, unbounded, - * minmimal value returned in *opt). + * minimal value returned in *opt). */ enum isl_lp_result isl_tab_min(struct isl_tab *tab, isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom, @@ -2700,6 +2963,9 @@ enum isl_lp_result isl_tab_min(struct isl_tab *tab, struct isl_tab_var *var; struct isl_tab_undo *snap; + if (!tab) + return isl_lp_error; + if (tab->empty) return isl_lp_empty; @@ -2708,8 +2974,6 @@ enum isl_lp_result isl_tab_min(struct isl_tab *tab, if (r < 0) return isl_lp_error; var = &tab->con[r]; - isl_int_mul(tab->mat->row[var->index][0], - tab->mat->row[var->index][0], denom); for (;;) { int row, col; find_pivot(tab, var, var, -1, &row, &col); @@ -2722,6 +2986,8 @@ enum isl_lp_result isl_tab_min(struct isl_tab *tab, if (isl_tab_pivot(tab, row, col) < 0) return isl_lp_error; } + isl_int_mul(tab->mat->row[var->index][0], + tab->mat->row[var->index][0], denom); if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) { int i; @@ -2813,17 +3079,33 @@ static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) return 0; } +/* Undo the operation performed by isl_tab_unrestrict. + * + * In particular, mark the variable as being non-negative and make + * sure the sample value respects this constraint. + */ +static int ununrestrict(struct isl_tab *tab, struct isl_tab_var *var) +{ + var->is_nonneg = 1; + + if (var->is_row && restore_row(tab, var) < -1) + return -1; + + return 0; +} + static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED; static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) { struct isl_tab_var *var = var_from_index(tab, undo->u.var_index); - switch(undo->type) { + switch (undo->type) { case isl_tab_undo_nonneg: var->is_nonneg = 0; break; case isl_tab_undo_redundant: var->is_redundant = 0; tab->n_redundant--; + restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant)); break; case isl_tab_undo_freeze: var->frozen = 0; @@ -2854,6 +3136,12 @@ static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) break; case isl_tab_undo_relax: return unrelax(tab, var); + case isl_tab_undo_unrestrict: + return ununrestrict(tab, var); + default: + isl_die(tab->mat->ctx, isl_error_internal, + "perform_undo_var called on invalid undo record", + return -1); } return 0; @@ -2909,11 +3197,9 @@ static int restore_basis(struct isl_tab *tab, int *col_var) } free(extra); - free(col_var); return 0; error: free(extra); - free(col_var); return -1; } @@ -2952,6 +3238,7 @@ static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) case isl_tab_undo_zero: case isl_tab_undo_allocate: case isl_tab_undo_relax: + case isl_tab_undo_unrestrict: return perform_undo_var(tab, undo); case isl_tab_undo_bmap_eq: return isl_basic_map_free_equality(tab->bmap, 1); @@ -2997,11 +3284,12 @@ int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap) if (undo == snap) break; if (perform_undo(tab, undo) < 0) { + tab->top = undo; free_undo(tab); tab->in_undo = 0; return -1; } - free(undo); + free_undo_record(undo); } tab->in_undo = 0; tab->top = undo; @@ -3016,9 +3304,9 @@ int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap) * In particular, if the row has been reduced to the constant -1, * then we know the inequality is adjacent (but opposite) to * an equality in the tableau. - * If the row has been reduced to r = -1 -r', with r' an inequality - * of the tableau, then the inequality is adjacent (but opposite) - * to the inequality r'. + * If the row has been reduced to r = c*(-1 -r'), with r' an inequality + * of the tableau and c a positive constant, then the inequality + * is adjacent (but opposite) to the inequality r'. */ static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row) { @@ -3030,15 +3318,18 @@ static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row) if (!isl_int_is_one(tab->mat->row[row][0])) return isl_ineq_separate; - if (!isl_int_is_negone(tab->mat->row[row][1])) - return isl_ineq_separate; pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead, tab->n_col - tab->n_dead); - if (pos == -1) - return isl_ineq_adj_eq; + if (pos == -1) { + if (isl_int_is_negone(tab->mat->row[row][1])) + return isl_ineq_adj_eq; + else + return isl_ineq_separate; + } - if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos])) + if (!isl_int_eq(tab->mat->row[row][1], + tab->mat->row[row][off + tab->n_dead + pos])) return isl_ineq_separate; pos = isl_seq_first_non_zero( @@ -3108,12 +3399,21 @@ error: int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap) { + bmap = isl_basic_map_cow(bmap); if (!tab || !bmap) goto error; - isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1); + if (tab->empty) { + bmap = isl_basic_map_set_to_empty(bmap); + if (!bmap) + goto error; + tab->bmap = bmap; + return 0; + } + + isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error); isl_assert(tab->mat->ctx, - tab->n_con == bmap->n_eq + bmap->n_ineq, return -1); + tab->n_con == bmap->n_eq + bmap->n_ineq, goto error); tab->bmap = bmap; @@ -3136,7 +3436,8 @@ __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab) return (isl_basic_set *)tab->bmap; } -void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent) +static void isl_tab_print_internal(__isl_keep struct isl_tab *tab, + FILE *out, int indent) { unsigned r, c; int i; @@ -3205,9 +3506,14 @@ void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent) tab->mat->n_row = tab->n_row; c = tab->mat->n_col; tab->mat->n_col = 2 + tab->M + tab->n_col; - isl_mat_dump(tab->mat, out, indent); + isl_mat_print_internal(tab->mat, out, indent); tab->mat->n_row = r; tab->mat->n_col = c; if (tab->bmap) - isl_basic_map_dump(tab->bmap, out, indent); + isl_basic_map_print_internal(tab->bmap, out, indent); +} + +void isl_tab_dump(__isl_keep struct isl_tab *tab) +{ + isl_tab_print_internal(tab, stderr, 0); }