X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_tab.c;h=e4cc386679d8760504af441f0741f9241120f479;hb=3d9f65131f9da197bca3a30eccf3a70107f50f03;hp=e45ee880ffc6d7ab94437050db846d5e48392412;hpb=88e08a163e4032c1a673ac770223fd98f2cd05da;p=platform%2Fupstream%2Fisl.git diff --git a/isl_tab.c b/isl_tab.c index e45ee88..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 #include #include "isl_map_private.h" #include "isl_tab.h" #include +#include /* * The implementation of tableaus in this file was inspired by Section 8 @@ -85,6 +89,11 @@ 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; @@ -171,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; } @@ -774,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; @@ -926,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) @@ -1083,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) { @@ -1170,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; @@ -2161,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; @@ -2176,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; @@ -2188,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]); @@ -2203,17 +2242,21 @@ 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". @@ -2522,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; @@ -2555,6 +2605,29 @@ error: return NULL; } +/* 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) @@ -2565,7 +2638,6 @@ int isl_tab_select_facet(struct isl_tab *tab, int 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]); @@ -2657,6 +2729,106 @@ int isl_tab_detect_implicit_equalities(struct isl_tab *tab) 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) { if (!tab) @@ -2907,11 +3079,26 @@ 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; @@ -2949,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; @@ -3004,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; } @@ -3047,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); @@ -3097,7 +3289,7 @@ int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap) tab->in_undo = 0; return -1; } - free(undo); + free_undo_record(undo); } tab->in_undo = 0; tab->top = undo; @@ -3207,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; @@ -3235,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; @@ -3304,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_print_internal(tab->bmap, out, indent); } + +void isl_tab_dump(__isl_keep struct isl_tab *tab) +{ + isl_tab_print_internal(tab, stderr, 0); +}