From 79776976e2fee1f831190a0fc30cd6984566f5de Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Sat, 28 Nov 2009 16:09:56 +0100 Subject: [PATCH] isl_tab_add_ineq and isl_tab_mark_empty: return status instead of isl_tab * isl_tabs are not reference counted, so it's easier to deal with a status than to have to keep track of whether the isl_tab may have been destroyed. --- isl_affine_hull.c | 5 ++-- isl_map_simplify.c | 3 ++- isl_sample.c | 5 ++-- isl_tab.c | 70 ++++++++++++++++++++++++++++++------------------------ isl_tab.h | 4 ++-- isl_tab_pip.c | 55 +++++++++++++++++++++++++++++------------- 6 files changed, 88 insertions(+), 54 deletions(-) diff --git a/isl_affine_hull.c b/isl_affine_hull.c index a133598..17dd7d8 100644 --- a/isl_affine_hull.c +++ b/isl_affine_hull.c @@ -239,7 +239,7 @@ error: static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up) { struct isl_ctx *ctx; - struct isl_vec *sample; + struct isl_vec *sample = NULL; struct isl_tab_undo *snap; unsigned dim; int k; @@ -269,7 +269,8 @@ static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up) if (isl_tab_extend_cons(tab, 1) < 0) goto error; - tab = isl_tab_add_ineq(tab, eq); + if (isl_tab_add_ineq(tab, eq) < 0) + goto error; sample = isl_tab_sample(tab); diff --git a/isl_map_simplify.c b/isl_map_simplify.c index ed9e033..354de11 100644 --- a/isl_map_simplify.c +++ b/isl_map_simplify.c @@ -1581,7 +1581,8 @@ static struct isl_basic_set *uset_gist(struct isl_basic_set *bset, if (!tab) goto error; for (i = 0; i < bset->n_ineq; ++i) - tab = isl_tab_add_ineq(tab, bset->ineq[i]); + if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0) + goto error; bset = isl_basic_set_add_constraints(combined, bset, 0); tab = isl_tab_detect_implicit_equalities(tab); tab = isl_tab_detect_redundant(tab); diff --git a/isl_sample.c b/isl_sample.c index 4e2cdd6..83fbf4e 100644 --- a/isl_sample.c +++ b/isl_sample.c @@ -957,6 +957,7 @@ static int tab_shift_cone(struct isl_tab *tab, bset = tab_cone->bset; U = isl_mat_drop_cols(U, 0, tab->n_var - tab->n_unbounded); for (i = 0; i < bset->n_ineq; ++i) { + int ok; struct isl_vec *row = NULL; if (isl_tab_is_equality(tab_cone, tab_cone->n_eq + i)) continue; @@ -973,9 +974,9 @@ static int tab_shift_cone(struct isl_tab *tab, continue; tab = isl_tab_extend(tab, 1); isl_int_add(bset->ineq[i][0], bset->ineq[i][0], v); - tab = isl_tab_add_ineq(tab, bset->ineq[i]); + ok = isl_tab_add_ineq(tab, bset->ineq[i]) >= 0; isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], v); - if (!tab) + if (!ok) goto error; } diff --git a/isl_tab.c b/isl_tab.c index 348fd09..38a3aef 100644 --- a/isl_tab.c +++ b/isl_tab.c @@ -925,17 +925,15 @@ int isl_tab_mark_redundant(struct isl_tab *tab, int row) } } -struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab) +int isl_tab_mark_empty(struct isl_tab *tab) { if (!tab) - return NULL; + return -1; if (!tab->empty && tab->need_undo) - if (isl_tab_push(tab, isl_tab_undo_empty) < 0) { - isl_tab_free(tab); - return NULL; - } + if (isl_tab_push(tab, isl_tab_undo_empty) < 0) + return -1; tab->empty = 1; - return tab; + return 0; } /* Update the rows signs after a pivot of "row" and "col", with "row_sgn" @@ -1633,25 +1631,25 @@ static int drop_col(struct isl_tab *tab, int col) /* Add inequality "ineq" and check if it conflicts with the * previously added constraints or if it is obviously redundant. */ -struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) +int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) { int r; int sgn; isl_int cst; if (!tab) - return NULL; + return -1; if (tab->bset) { struct isl_basic_set *bset = tab->bset; - isl_assert(tab->mat->ctx, tab->n_eq == bset->n_eq, goto error); + isl_assert(tab->mat->ctx, tab->n_eq == bset->n_eq, return -1); isl_assert(tab->mat->ctx, - tab->n_con == bset->n_eq + bset->n_ineq, goto error); + tab->n_con == bset->n_eq + bset->n_ineq, return -1); tab->bset = isl_basic_set_add_ineq(tab->bset, ineq); if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0) - goto error; + return -1; if (!tab->bset) - goto error; + return -1; } if (tab->cone) { isl_int_init(cst); @@ -1663,28 +1661,25 @@ struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) isl_int_clear(cst); } if (r < 0) - goto error; + return -1; tab->con[r].is_nonneg = 1; if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0) - goto error; + return -1; if (isl_tab_row_is_redundant(tab, tab->con[r].index)) { if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0) - goto error; - return tab; + return -1; + return 0; } sgn = restore_row(tab, &tab->con[r]); if (sgn < -1) - goto error; + return -1; if (sgn < 0) return isl_tab_mark_empty(tab); if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index)) if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0) - goto error; - return tab; -error: - isl_tab_free(tab); - return NULL; + return -1; + return 0; } /* Pivot a non-negative variable down until it reaches the value zero @@ -1891,8 +1886,11 @@ struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) sgn = sign_of_max(tab, var); if (sgn < -1) goto error; - if (sgn < 0) - return isl_tab_mark_empty(tab); + if (sgn < 0) { + if (isl_tab_mark_empty(tab) < 0) + goto error; + return tab; + } } var->is_nonneg = 1; @@ -1921,19 +1919,26 @@ struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap) if (!tab) return NULL; tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL); - if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) - return isl_tab_mark_empty(tab); + if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) { + if (isl_tab_mark_empty(tab) < 0) + goto error; + return tab; + } for (i = 0; i < bmap->n_eq; ++i) { tab = add_eq(tab, bmap->eq[i]); if (!tab) return tab; } for (i = 0; i < bmap->n_ineq; ++i) { - tab = isl_tab_add_ineq(tab, bmap->ineq[i]); - if (!tab || tab->empty) + if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0) + goto error; + if (tab->empty) return tab; } return tab; +error: + isl_tab_free(tab); + return NULL; } struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset) @@ -2195,8 +2200,11 @@ static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab, sgn = sign_of_max(tab, &tab->con[r]); if (sgn < -1) goto error; - if (sgn < 0) - return isl_tab_mark_empty(tab); + if (sgn < 0) { + if (isl_tab_mark_empty(tab) < 0) + goto error; + return tab; + } tab->con[r].is_nonneg = 1; if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0) goto error; diff --git a/isl_tab.h b/isl_tab.h index 13919cf..9f55e0c 100644 --- a/isl_tab.h +++ b/isl_tab.h @@ -179,7 +179,7 @@ enum isl_lp_result isl_tab_min(struct isl_tab *tab, unsigned flags) WARN_UNUSED; struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new) WARN_UNUSED; -struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) WARN_UNUSED; +int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) WARN_UNUSED; struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED; struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED; @@ -216,7 +216,7 @@ struct isl_map *isl_tab_basic_map_partial_lexopt( struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i); int isl_tab_mark_redundant(struct isl_tab *tab, int row) WARN_UNUSED; -struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab); +int isl_tab_mark_empty(struct isl_tab *tab) WARN_UNUSED; struct isl_tab *isl_tab_dup(struct isl_tab *tab); struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2); int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new) WARN_UNUSED; diff --git a/isl_tab_pip.c b/isl_tab_pip.c index 4758106..8f3311c 100644 --- a/isl_tab_pip.c +++ b/isl_tab_pip.c @@ -1100,8 +1100,11 @@ static struct isl_tab *restore_lexmin(struct isl_tab *tab) return tab; while ((row = first_neg(tab)) != -1) { col = lexmin_pivot_col(tab, row); - if (col >= tab->n_col) - return isl_tab_mark_empty(tab); + if (col >= tab->n_col) { + if (isl_tab_mark_empty(tab) < 0) + goto error; + return tab; + } if (col < 0) goto error; if (isl_tab_pivot(tab, row, col) < 0) @@ -1243,8 +1246,11 @@ static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq) row = tab->con[r1].index; if (is_constant(tab, row)) { if (!isl_int_is_zero(tab->mat->row[row][1]) || - (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) - return isl_tab_mark_empty(tab); + (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) { + if (isl_tab_mark_empty(tab) < 0) + goto error; + return tab; + } if (isl_tab_rollback(tab, snap) < 0) goto error; return tab; @@ -1515,8 +1521,11 @@ static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab) return tab; while ((row = first_non_integer(tab, &flags)) != -1) { - if (ISL_FL_ISSET(flags, I_VAR)) - return isl_tab_mark_empty(tab); + if (ISL_FL_ISSET(flags, I_VAR)) { + if (isl_tab_mark_empty(tab) < 0) + goto error; + return tab; + } row = add_cut(tab, row); if (row < 0) goto error; @@ -1979,8 +1988,11 @@ static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap, if (!tab->row_sign) goto error; } - if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) - return isl_tab_mark_empty(tab); + if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) { + if (isl_tab_mark_empty(tab) < 0) + goto error; + return tab; + } for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) { tab->var[i].is_nonneg = 1; @@ -2051,6 +2063,7 @@ static int best_split(struct isl_tab *tab, struct isl_tab *context_tab) struct isl_tab_undo *snap2; struct isl_vec *ineq = NULL; int r = 0; + int ok; if (!isl_tab_var_from_row(tab, split)->is_nonneg) continue; @@ -2060,8 +2073,10 @@ static int best_split(struct isl_tab *tab, struct isl_tab *context_tab) ineq = get_row_parameter_ineq(tab, split); if (!ineq) return -1; - context_tab = isl_tab_add_ineq(context_tab, ineq->el); + ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0; isl_vec_free(ineq); + if (!ok) + return -1; snap2 = isl_tab_snap(context_tab); @@ -2078,8 +2093,10 @@ static int best_split(struct isl_tab *tab, struct isl_tab *context_tab) ineq = get_row_parameter_ineq(tab, row); if (!ineq) return -1; - context_tab = isl_tab_add_ineq(context_tab, ineq->el); + ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0; isl_vec_free(ineq); + if (!ok) + return -1; var = &context_tab->con[context_tab->n_con - 1]; if (!context_tab->empty && !isl_tab_min_at_most_neg_one(context_tab, var)) @@ -2347,7 +2364,8 @@ static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab, isl_seq_clr(ineq->el, ineq->size); for (i = 0; i < context_tab->n_var; ++i) { isl_int_set_si(ineq->el[1 + i], 1); - context_tab = isl_tab_add_ineq(context_tab, ineq->el); + if (isl_tab_add_ineq(context_tab, ineq->el) < 0) + goto error; var = &context_tab->con[context_tab->n_con - 1]; if (!context_tab->empty && !isl_tab_min_at_most_neg_one(context_tab, var)) { @@ -2663,7 +2681,8 @@ static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr) if (sample->size == 0) { isl_vec_free(sample); - cgbr->tab = isl_tab_mark_empty(cgbr->tab); + if (isl_tab_mark_empty(cgbr->tab) < 0) + goto error; return; } @@ -2729,7 +2748,8 @@ static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq) if (isl_tab_extend_cons(cgbr->tab, 1) < 0) goto error; - cgbr->tab = isl_tab_add_ineq(cgbr->tab, ineq); + if (isl_tab_add_ineq(cgbr->tab, ineq) < 0) + goto error; if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) { int i; @@ -2745,7 +2765,8 @@ static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq) isl_int_add(ineq[0], ineq[0], ineq[1 + i]); } - cgbr->shifted = isl_tab_add_ineq(cgbr->shifted, ineq); + if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0) + goto error; for (i = 0; i < dim; ++i) { if (!isl_int_is_neg(ineq[1 + i])) @@ -2757,7 +2778,8 @@ static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq) if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) { if (isl_tab_extend_cons(cgbr->cone, 1) < 0) goto error; - cgbr->cone = isl_tab_add_ineq(cgbr->cone, ineq); + if (isl_tab_add_ineq(cgbr->cone, ineq) < 0) + goto error; } return; @@ -3636,7 +3658,8 @@ static void find_solutions(struct isl_sol *sol, struct isl_tab *tab) break; if (ISL_FL_ISSET(flags, I_PAR)) { if (ISL_FL_ISSET(flags, I_VAR)) { - tab = isl_tab_mark_empty(tab); + if (isl_tab_mark_empty(tab) < 0) + goto error; break; } row = add_cut(tab, row); -- 2.7.4