+ * We tentatively add each of these equalities to the main tableau
+ * and if this happens to result in a row with a final coefficient
+ * that is one or negative one, we use it to kill a column
+ * in the main tableau. Otherwise, we discard the tentatively
+ * added row.
+ */
+static void propagate_equalities(struct isl_context_gbr *cgbr,
+ struct isl_tab *tab, unsigned first)
+{
+ int i;
+ struct isl_vec *eq = NULL;
+
+ eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
+ if (!eq)
+ goto error;
+
+ if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
+ goto error;
+
+ isl_seq_clr(eq->el + 1 + tab->n_param,
+ tab->n_var - tab->n_param - tab->n_div);
+ for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
+ int j;
+ int r;
+ struct isl_tab_undo *snap;
+ snap = isl_tab_snap(tab);
+
+ isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
+ isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
+ cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
+ tab->n_div);
+
+ r = isl_tab_add_row(tab, eq->el);
+ if (r < 0)
+ goto error;
+ r = tab->con[r].index;
+ j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
+ if (j < 0 || j < tab->n_dead ||
+ !isl_int_is_one(tab->mat->row[r][0]) ||
+ (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
+ !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
+ if (isl_tab_rollback(tab, snap) < 0)
+ goto error;
+ continue;
+ }
+ if (isl_tab_pivot(tab, r, j) < 0)
+ goto error;
+ if (isl_tab_kill_col(tab, j) < 0)
+ goto error;
+
+ if (restore_lexmin(tab) < 0)
+ goto error;
+ }
+
+ isl_vec_free(eq);
+
+ return;
+error:
+ isl_vec_free(eq);
+ isl_tab_free(cgbr->tab);
+ cgbr->tab = NULL;
+}
+
+static int context_gbr_detect_equalities(struct isl_context *context,
+ struct isl_tab *tab)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ struct isl_ctx *ctx;
+ unsigned n_ineq;
+
+ ctx = cgbr->tab->mat->ctx;
+
+ if (!cgbr->cone) {
+ struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
+ cgbr->cone = isl_tab_from_recession_cone(bset, 0);
+ if (!cgbr->cone)
+ goto error;
+ if (isl_tab_track_bset(cgbr->cone,
+ isl_basic_set_copy(bset)) < 0)
+ goto error;
+ }
+ if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
+ goto error;
+
+ n_ineq = cgbr->tab->bmap->n_ineq;
+ cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
+ if (!cgbr->tab)
+ return -1;
+ if (cgbr->tab->bmap->n_ineq > n_ineq)
+ propagate_equalities(cgbr, tab, n_ineq);
+
+ return 0;
+error:
+ isl_tab_free(cgbr->tab);
+ cgbr->tab = NULL;
+ return -1;
+}
+
+static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
+ struct isl_vec *div)
+{
+ return get_div(tab, context, div);
+}
+
+static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ if (cgbr->cone) {
+ int k;
+
+ if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
+ return -1;
+ if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
+ return -1;
+ if (isl_tab_allocate_var(cgbr->cone) <0)
+ return -1;
+
+ cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
+ isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
+ k = isl_basic_map_alloc_div(cgbr->cone->bmap);
+ if (k < 0)
+ return -1;
+ isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
+ if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
+ return -1;
+ }
+ return context_tab_add_div(cgbr->tab, div,
+ context_gbr_add_ineq_wrap, context);
+}
+
+static int context_gbr_best_split(struct isl_context *context,
+ struct isl_tab *tab)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ struct isl_tab_undo *snap;
+ int r;
+
+ snap = isl_tab_snap(cgbr->tab);
+ r = best_split(tab, cgbr->tab);
+
+ if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
+ return -1;
+
+ return r;
+}
+
+static int context_gbr_is_empty(struct isl_context *context)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ if (!cgbr->tab)
+ return -1;
+ return cgbr->tab->empty;
+}
+
+struct isl_gbr_tab_undo {
+ struct isl_tab_undo *tab_snap;
+ struct isl_tab_undo *shifted_snap;
+ struct isl_tab_undo *cone_snap;
+};
+
+static void *context_gbr_save(struct isl_context *context)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ struct isl_gbr_tab_undo *snap;
+
+ if (!cgbr->tab)
+ return NULL;
+
+ snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
+ if (!snap)
+ return NULL;
+
+ snap->tab_snap = isl_tab_snap(cgbr->tab);
+ if (isl_tab_save_samples(cgbr->tab) < 0)
+ goto error;
+
+ if (cgbr->shifted)
+ snap->shifted_snap = isl_tab_snap(cgbr->shifted);
+ else
+ snap->shifted_snap = NULL;
+
+ if (cgbr->cone)
+ snap->cone_snap = isl_tab_snap(cgbr->cone);
+ else
+ snap->cone_snap = NULL;
+
+ return snap;
+error:
+ free(snap);
+ return NULL;
+}
+
+static void context_gbr_restore(struct isl_context *context, void *save)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
+ if (!snap)
+ goto error;
+ if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
+ isl_tab_free(cgbr->tab);
+ cgbr->tab = NULL;
+ }
+
+ if (snap->shifted_snap) {
+ if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
+ goto error;
+ } else if (cgbr->shifted) {
+ isl_tab_free(cgbr->shifted);
+ cgbr->shifted = NULL;
+ }
+
+ if (snap->cone_snap) {
+ if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
+ goto error;
+ } else if (cgbr->cone) {
+ isl_tab_free(cgbr->cone);
+ cgbr->cone = NULL;
+ }
+
+ free(snap);
+
+ return;
+error:
+ free(snap);
+ isl_tab_free(cgbr->tab);
+ cgbr->tab = NULL;
+}
+
+static void context_gbr_discard(void *save)
+{
+ struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
+ free(snap);
+}
+
+static int context_gbr_is_ok(struct isl_context *context)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ return !!cgbr->tab;
+}
+
+static void context_gbr_invalidate(struct isl_context *context)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ isl_tab_free(cgbr->tab);
+ cgbr->tab = NULL;
+}
+
+static void context_gbr_free(struct isl_context *context)
+{
+ struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
+ isl_tab_free(cgbr->tab);
+ isl_tab_free(cgbr->shifted);
+ isl_tab_free(cgbr->cone);
+ free(cgbr);
+}
+
+struct isl_context_op isl_context_gbr_op = {
+ context_gbr_detect_nonnegative_parameters,
+ context_gbr_peek_basic_set,
+ context_gbr_peek_tab,
+ context_gbr_add_eq,
+ context_gbr_add_ineq,
+ context_gbr_ineq_sign,
+ context_gbr_test_ineq,
+ context_gbr_get_div,
+ context_gbr_add_div,
+ context_gbr_detect_equalities,
+ context_gbr_best_split,
+ context_gbr_is_empty,
+ context_gbr_is_ok,
+ context_gbr_save,
+ context_gbr_restore,
+ context_gbr_discard,
+ context_gbr_invalidate,
+ context_gbr_free,
+};
+
+static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
+{
+ struct isl_context_gbr *cgbr;
+
+ if (!dom)
+ return NULL;
+
+ cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
+ if (!cgbr)
+ return NULL;
+
+ cgbr->context.op = &isl_context_gbr_op;
+
+ cgbr->shifted = NULL;
+ cgbr->cone = NULL;
+ cgbr->tab = isl_tab_from_basic_set(dom, 1);
+ cgbr->tab = isl_tab_init_samples(cgbr->tab);
+ if (!cgbr->tab)
+ goto error;
+ check_gbr_integer_feasible(cgbr);
+
+ return &cgbr->context;
+error:
+ cgbr->context.op->free(&cgbr->context);
+ return NULL;
+}
+
+static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
+{
+ if (!dom)
+ return NULL;
+
+ if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
+ return isl_context_lex_alloc(dom);
+ else
+ return isl_context_gbr_alloc(dom);
+}
+
+/* Construct an isl_sol_map structure for accumulating the solution.
+ * If track_empty is set, then we also keep track of the parts
+ * of the context where there is no solution.
+ * If max is set, then we are solving a maximization, rather than
+ * a minimization problem, which means that the variables in the
+ * tableau have value "M - x" rather than "M + x".
+ */
+static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
+ struct isl_basic_set *dom, int track_empty, int max)
+{
+ struct isl_sol_map *sol_map = NULL;
+
+ if (!bmap)
+ goto error;
+
+ sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
+ if (!sol_map)
+ goto error;
+
+ sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
+ sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
+ sol_map->sol.dec_level.sol = &sol_map->sol;
+ sol_map->sol.max = max;
+ sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
+ sol_map->sol.add = &sol_map_add_wrap;
+ sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
+ sol_map->sol.free = &sol_map_free_wrap;
+ sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
+ ISL_MAP_DISJOINT);
+ if (!sol_map->map)
+ goto error;
+
+ sol_map->sol.context = isl_context_alloc(dom);
+ if (!sol_map->sol.context)
+ goto error;
+
+ if (track_empty) {
+ sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
+ 1, ISL_SET_DISJOINT);
+ if (!sol_map->empty)
+ goto error;
+ }
+
+ isl_basic_set_free(dom);
+ return &sol_map->sol;
+error:
+ isl_basic_set_free(dom);
+ sol_map_free(sol_map);
+ return NULL;
+}
+
+/* Check whether all coefficients of (non-parameter) variables
+ * are non-positive, meaning that no pivots can be performed on the row.
+ */
+static int is_critical(struct isl_tab *tab, int row)
+{
+ int j;
+ unsigned off = 2 + tab->M;
+
+ for (j = tab->n_dead; j < tab->n_col; ++j) {
+ if (tab->col_var[j] >= 0 &&
+ (tab->col_var[j] < tab->n_param ||
+ tab->col_var[j] >= tab->n_var - tab->n_div))
+ continue;
+
+ if (isl_int_is_pos(tab->mat->row[row][off + j]))
+ return 0;
+ }
+
+ return 1;
+}
+
+/* Check whether the inequality represented by vec is strict over the integers,
+ * i.e., there are no integer values satisfying the constraint with
+ * equality. This happens if the gcd of the coefficients is not a divisor
+ * of the constant term. If so, scale the constraint down by the gcd
+ * of the coefficients.
+ */
+static int is_strict(struct isl_vec *vec)
+{
+ isl_int gcd;
+ int strict = 0;
+
+ isl_int_init(gcd);
+ isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
+ if (!isl_int_is_one(gcd)) {
+ strict = !isl_int_is_divisible_by(vec->el[0], gcd);
+ isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
+ isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
+ }
+ isl_int_clear(gcd);
+
+ return strict;
+}
+
+/* Determine the sign of the given row of the main tableau.
+ * The result is one of
+ * isl_tab_row_pos: always non-negative; no pivot needed
+ * isl_tab_row_neg: always non-positive; pivot
+ * isl_tab_row_any: can be both positive and negative; split
+ *
+ * We first handle some simple cases
+ * - the row sign may be known already
+ * - the row may be obviously non-negative
+ * - the parametric constant may be equal to that of another row
+ * for which we know the sign. This sign will be either "pos" or
+ * "any". If it had been "neg" then we would have pivoted before.
+ *
+ * If none of these cases hold, we check the value of the row for each
+ * of the currently active samples. Based on the signs of these values
+ * we make an initial determination of the sign of the row.
+ *
+ * all zero -> unk(nown)
+ * all non-negative -> pos
+ * all non-positive -> neg
+ * both negative and positive -> all
+ *
+ * If we end up with "all", we are done.
+ * Otherwise, we perform a check for positive and/or negative
+ * values as follows.
+ *
+ * samples neg unk pos
+ * <0 ? Y N Y N
+ * pos any pos
+ * >0 ? Y N Y N
+ * any neg any neg
+ *
+ * There is no special sign for "zero", because we can usually treat zero
+ * as either non-negative or non-positive, whatever works out best.
+ * However, if the row is "critical", meaning that pivoting is impossible
+ * then we don't want to limp zero with the non-positive case, because
+ * then we we would lose the solution for those values of the parameters
+ * where the value of the row is zero. Instead, we treat 0 as non-negative
+ * ensuring a split if the row can attain both zero and negative values.
+ * The same happens when the original constraint was one that could not
+ * be satisfied with equality by any integer values of the parameters.
+ * In this case, we normalize the constraint, but then a value of zero
+ * for the normalized constraint is actually a positive value for the
+ * original constraint, so again we need to treat zero as non-negative.
+ * In both these cases, we have the following decision tree instead:
+ *
+ * all non-negative -> pos
+ * all negative -> neg
+ * both negative and non-negative -> all
+ *
+ * samples neg pos
+ * <0 ? Y N
+ * any pos
+ * >=0 ? Y N
+ * any neg
+ */
+static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
+ struct isl_sol *sol, int row)
+{
+ struct isl_vec *ineq = NULL;
+ enum isl_tab_row_sign res = isl_tab_row_unknown;
+ int critical;
+ int strict;
+ int row2;
+
+ if (tab->row_sign[row] != isl_tab_row_unknown)
+ return tab->row_sign[row];
+ if (is_obviously_nonneg(tab, row))
+ return isl_tab_row_pos;
+ for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
+ if (tab->row_sign[row2] == isl_tab_row_unknown)
+ continue;
+ if (identical_parameter_line(tab, row, row2))
+ return tab->row_sign[row2];
+ }
+
+ critical = is_critical(tab, row);
+
+ ineq = get_row_parameter_ineq(tab, row);
+ if (!ineq)
+ goto error;
+
+ strict = is_strict(ineq);
+
+ res = sol->context->op->ineq_sign(sol->context, ineq->el,
+ critical || strict);
+
+ if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
+ /* test for negative values */
+ int feasible;
+ isl_seq_neg(ineq->el, ineq->el, ineq->size);
+ isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
+
+ feasible = sol->context->op->test_ineq(sol->context, ineq->el);
+ if (feasible < 0)
+ goto error;
+ if (!feasible)
+ res = isl_tab_row_pos;
+ else
+ res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
+ : isl_tab_row_any;
+ if (res == isl_tab_row_neg) {
+ isl_seq_neg(ineq->el, ineq->el, ineq->size);
+ isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
+ }
+ }
+
+ if (res == isl_tab_row_neg) {
+ /* test for positive values */
+ int feasible;
+ if (!critical && !strict)
+ isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
+
+ feasible = sol->context->op->test_ineq(sol->context, ineq->el);
+ if (feasible < 0)
+ goto error;
+ if (feasible)
+ res = isl_tab_row_any;
+ }
+
+ isl_vec_free(ineq);
+ return res;
+error:
+ isl_vec_free(ineq);
+ return isl_tab_row_unknown;
+}
+
+static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
+
+/* Find solutions for values of the parameters that satisfy the given
+ * inequality.
+ *
+ * We currently take a snapshot of the context tableau that is reset
+ * when we return from this function, while we make a copy of the main
+ * tableau, leaving the original main tableau untouched.
+ * These are fairly arbitrary choices. Making a copy also of the context
+ * tableau would obviate the need to undo any changes made to it later,
+ * while taking a snapshot of the main tableau could reduce memory usage.
+ * If we were to switch to taking a snapshot of the main tableau,
+ * we would have to keep in mind that we need to save the row signs
+ * and that we need to do this before saving the current basis
+ * such that the basis has been restore before we restore the row signs.
+ */
+static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
+{
+ void *saved;
+
+ if (!sol->context)
+ goto error;
+ saved = sol->context->op->save(sol->context);
+
+ tab = isl_tab_dup(tab);
+ if (!tab)
+ goto error;
+
+ sol->context->op->add_ineq(sol->context, ineq, 0, 1);
+
+ find_solutions(sol, tab);
+
+ if (!sol->error)
+ sol->context->op->restore(sol->context, saved);
+ else
+ sol->context->op->discard(saved);
+ return;
+error:
+ sol->error = 1;
+}
+
+/* Record the absence of solutions for those values of the parameters
+ * that do not satisfy the given inequality with equality.
+ */
+static void no_sol_in_strict(struct isl_sol *sol,
+ struct isl_tab *tab, struct isl_vec *ineq)
+{
+ int empty;
+ void *saved;
+
+ if (!sol->context || sol->error)
+ goto error;
+ saved = sol->context->op->save(sol->context);
+
+ isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
+
+ sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
+ if (!sol->context)
+ goto error;
+
+ empty = tab->empty;
+ tab->empty = 1;
+ sol_add(sol, tab);
+ tab->empty = empty;
+
+ isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
+
+ sol->context->op->restore(sol->context, saved);
+ return;
+error:
+ sol->error = 1;
+}
+
+/* Compute the lexicographic minimum of the set represented by the main
+ * tableau "tab" within the context "sol->context_tab".
+ * On entry the sample value of the main tableau is lexicographically
+ * less than or equal to this lexicographic minimum.
+ * Pivots are performed until a feasible point is found, which is then
+ * necessarily equal to the minimum, or until the tableau is found to
+ * be infeasible. Some pivots may need to be performed for only some
+ * feasible values of the context tableau. If so, the context tableau
+ * is split into a part where the pivot is needed and a part where it is not.
+ *
+ * Whenever we enter the main loop, the main tableau is such that no
+ * "obvious" pivots need to be performed on it, where "obvious" means
+ * that the given row can be seen to be negative without looking at
+ * the context tableau. In particular, for non-parametric problems,
+ * no pivots need to be performed on the main tableau.
+ * The caller of find_solutions is responsible for making this property
+ * hold prior to the first iteration of the loop, while restore_lexmin
+ * is called before every other iteration.
+ *
+ * Inside the main loop, we first examine the signs of the rows of
+ * the main tableau within the context of the context tableau.
+ * If we find a row that is always non-positive for all values of
+ * the parameters satisfying the context tableau and negative for at
+ * least one value of the parameters, we perform the appropriate pivot
+ * and start over. An exception is the case where no pivot can be
+ * performed on the row. In this case, we require that the sign of
+ * the row is negative for all values of the parameters (rather than just
+ * non-positive). This special case is handled inside row_sign, which
+ * will say that the row can have any sign if it determines that it can
+ * attain both negative and zero values.
+ *
+ * If we can't find a row that always requires a pivot, but we can find
+ * one or more rows that require a pivot for some values of the parameters
+ * (i.e., the row can attain both positive and negative signs), then we split
+ * the context tableau into two parts, one where we force the sign to be
+ * non-negative and one where we force is to be negative.
+ * The non-negative part is handled by a recursive call (through find_in_pos).
+ * Upon returning from this call, we continue with the negative part and
+ * perform the required pivot.
+ *
+ * If no such rows can be found, all rows are non-negative and we have
+ * found a (rational) feasible point. If we only wanted a rational point
+ * then we are done.
+ * Otherwise, we check if all values of the sample point of the tableau
+ * are integral for the variables. If so, we have found the minimal
+ * integral point and we are done.
+ * If the sample point is not integral, then we need to make a distinction
+ * based on whether the constant term is non-integral or the coefficients
+ * of the parameters. Furthermore, in order to decide how to handle
+ * the non-integrality, we also need to know whether the coefficients
+ * of the other columns in the tableau are integral. This leads
+ * to the following table. The first two rows do not correspond
+ * to a non-integral sample point and are only mentioned for completeness.
+ *
+ * constant parameters other
+ *
+ * int int int |
+ * int int rat | -> no problem
+ *
+ * rat int int -> fail
+ *
+ * rat int rat -> cut
+ *
+ * int rat rat |
+ * rat rat rat | -> parametric cut
+ *
+ * int rat int |
+ * rat rat int | -> split context
+ *
+ * If the parametric constant is completely integral, then there is nothing
+ * to be done. If the constant term is non-integral, but all the other
+ * coefficient are integral, then there is nothing that can be done
+ * and the tableau has no integral solution.
+ * If, on the other hand, one or more of the other columns have rational
+ * coefficients, but the parameter coefficients are all integral, then
+ * we can perform a regular (non-parametric) cut.
+ * Finally, if there is any parameter coefficient that is non-integral,
+ * then we need to involve the context tableau. There are two cases here.
+ * If at least one other column has a rational coefficient, then we
+ * can perform a parametric cut in the main tableau by adding a new
+ * integer division in the context tableau.
+ * If all other columns have integral coefficients, then we need to
+ * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
+ * is always integral. We do this by introducing an integer division
+ * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
+ * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
+ * Since q is expressed in the tableau as
+ * c + \sum a_i y_i - m q >= 0
+ * -c - \sum a_i y_i + m q + m - 1 >= 0
+ * it is sufficient to add the inequality
+ * -c - \sum a_i y_i + m q >= 0
+ * In the part of the context where this inequality does not hold, the
+ * main tableau is marked as being empty.
+ */
+static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
+{
+ struct isl_context *context;
+ int r;
+
+ if (!tab || sol->error)
+ goto error;
+
+ context = sol->context;
+
+ if (tab->empty)
+ goto done;
+ if (context->op->is_empty(context))
+ goto done;
+
+ for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
+ int flags;
+ int row;
+ enum isl_tab_row_sign sgn;
+ int split = -1;
+ int n_split = 0;
+
+ for (row = tab->n_redundant; row < tab->n_row; ++row) {
+ if (!isl_tab_var_from_row(tab, row)->is_nonneg)
+ continue;
+ sgn = row_sign(tab, sol, row);
+ if (!sgn)
+ goto error;
+ tab->row_sign[row] = sgn;
+ if (sgn == isl_tab_row_any)
+ n_split++;
+ if (sgn == isl_tab_row_any && split == -1)
+ split = row;
+ if (sgn == isl_tab_row_neg)
+ break;
+ }
+ if (row < tab->n_row)
+ continue;
+ if (split != -1) {
+ struct isl_vec *ineq;
+ if (n_split != 1)
+ split = context->op->best_split(context, tab);
+ if (split < 0)
+ goto error;
+ ineq = get_row_parameter_ineq(tab, split);
+ if (!ineq)
+ goto error;
+ is_strict(ineq);
+ for (row = tab->n_redundant; row < tab->n_row; ++row) {
+ if (!isl_tab_var_from_row(tab, row)->is_nonneg)
+ continue;
+ if (tab->row_sign[row] == isl_tab_row_any)
+ tab->row_sign[row] = isl_tab_row_unknown;
+ }
+ tab->row_sign[split] = isl_tab_row_pos;
+ sol_inc_level(sol);
+ find_in_pos(sol, tab, ineq->el);
+ tab->row_sign[split] = isl_tab_row_neg;
+ row = split;
+ isl_seq_neg(ineq->el, ineq->el, ineq->size);
+ isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
+ if (!sol->error)
+ context->op->add_ineq(context, ineq->el, 0, 1);
+ isl_vec_free(ineq);
+ if (sol->error)
+ goto error;
+ continue;
+ }
+ if (tab->rational)
+ break;
+ row = first_non_integer_row(tab, &flags);
+ if (row < 0)
+ break;
+ if (ISL_FL_ISSET(flags, I_PAR)) {
+ if (ISL_FL_ISSET(flags, I_VAR)) {
+ if (isl_tab_mark_empty(tab) < 0)
+ goto error;
+ break;
+ }
+ row = add_cut(tab, row);
+ } else if (ISL_FL_ISSET(flags, I_VAR)) {
+ struct isl_vec *div;
+ struct isl_vec *ineq;
+ int d;
+ div = get_row_split_div(tab, row);
+ if (!div)
+ goto error;
+ d = context->op->get_div(context, tab, div);
+ isl_vec_free(div);
+ if (d < 0)
+ goto error;
+ ineq = ineq_for_div(context->op->peek_basic_set(context), d);
+ if (!ineq)
+ goto error;
+ sol_inc_level(sol);
+ no_sol_in_strict(sol, tab, ineq);
+ isl_seq_neg(ineq->el, ineq->el, ineq->size);
+ context->op->add_ineq(context, ineq->el, 1, 1);
+ isl_vec_free(ineq);
+ if (sol->error || !context->op->is_ok(context))
+ goto error;
+ tab = set_row_cst_to_div(tab, row, d);
+ if (context->op->is_empty(context))
+ break;
+ } else
+ row = add_parametric_cut(tab, row, context);
+ if (row < 0)
+ goto error;
+ }
+ if (r < 0)
+ goto error;
+done:
+ sol_add(sol, tab);
+ isl_tab_free(tab);
+ return;
+error:
+ isl_tab_free(tab);
+ sol->error = 1;
+}
+
+/* Does "sol" contain a pair of partial solutions that could potentially
+ * be merged?
+ *
+ * We currently only check that "sol" is not in an error state
+ * and that there are at least two partial solutions of which the final two
+ * are defined at the same level.
+ */
+static int sol_has_mergeable_solutions(struct isl_sol *sol)
+{
+ if (sol->error)
+ return 0;
+ if (!sol->partial)
+ return 0;
+ if (!sol->partial->next)
+ return 0;
+ return sol->partial->level == sol->partial->next->level;
+}
+
+/* Compute the lexicographic minimum of the set represented by the main
+ * tableau "tab" within the context "sol->context_tab".
+ *
+ * As a preprocessing step, we first transfer all the purely parametric
+ * equalities from the main tableau to the context tableau, i.e.,
+ * parameters that have been pivoted to a row.
+ * These equalities are ignored by the main algorithm, because the
+ * corresponding rows may not be marked as being non-negative.
+ * In parts of the context where the added equality does not hold,
+ * the main tableau is marked as being empty.
+ *
+ * Before we embark on the actual computation, we save a copy
+ * of the context. When we return, we check if there are any
+ * partial solutions that can potentially be merged. If so,
+ * we perform a rollback to the initial state of the context.
+ * The merging of partial solutions happens inside calls to
+ * sol_dec_level that are pushed onto the undo stack of the context.
+ * If there are no partial solutions that can potentially be merged
+ * then the rollback is skipped as it would just be wasted effort.
+ */
+static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
+{
+ int row;
+ void *saved;
+
+ if (!tab)
+ goto error;
+
+ sol->level = 0;
+
+ for (row = tab->n_redundant; row < tab->n_row; ++row) {
+ int p;
+ struct isl_vec *eq;
+
+ if (tab->row_var[row] < 0)
+ continue;
+ if (tab->row_var[row] >= tab->n_param &&
+ tab->row_var[row] < tab->n_var - tab->n_div)
+ continue;
+ if (tab->row_var[row] < tab->n_param)
+ p = tab->row_var[row];
+ else
+ p = tab->row_var[row]
+ + tab->n_param - (tab->n_var - tab->n_div);
+
+ eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
+ if (!eq)
+ goto error;
+ get_row_parameter_line(tab, row, eq->el);
+ isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
+ eq = isl_vec_normalize(eq);
+
+ sol_inc_level(sol);
+ no_sol_in_strict(sol, tab, eq);
+
+ isl_seq_neg(eq->el, eq->el, eq->size);
+ sol_inc_level(sol);
+ no_sol_in_strict(sol, tab, eq);
+ isl_seq_neg(eq->el, eq->el, eq->size);
+
+ sol->context->op->add_eq(sol->context, eq->el, 1, 1);
+
+ isl_vec_free(eq);
+
+ if (isl_tab_mark_redundant(tab, row) < 0)
+ goto error;
+
+ if (sol->context->op->is_empty(sol->context))
+ break;
+
+ row = tab->n_redundant - 1;
+ }
+
+ saved = sol->context->op->save(sol->context);
+
+ find_solutions(sol, tab);
+
+ if (sol_has_mergeable_solutions(sol))
+ sol->context->op->restore(sol->context, saved);
+ else
+ sol->context->op->discard(saved);
+
+ sol->level = 0;
+ sol_pop(sol);
+
+ return;
+error:
+ isl_tab_free(tab);
+ sol->error = 1;
+}
+
+/* Check if integer division "div" of "dom" also occurs in "bmap".
+ * If so, return its position within the divs.
+ * If not, return -1.
+ */
+static int find_context_div(struct isl_basic_map *bmap,
+ struct isl_basic_set *dom, unsigned div)
+{
+ int i;
+ unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
+ unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
+
+ if (isl_int_is_zero(dom->div[div][0]))
+ return -1;
+ if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
+ return -1;
+
+ for (i = 0; i < bmap->n_div; ++i) {
+ if (isl_int_is_zero(bmap->div[i][0]))
+ continue;
+ if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
+ (b_dim - d_dim) + bmap->n_div) != -1)
+ continue;
+ if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
+ return i;
+ }
+ return -1;
+}
+
+/* The correspondence between the variables in the main tableau,
+ * the context tableau, and the input map and domain is as follows.
+ * The first n_param and the last n_div variables of the main tableau
+ * form the variables of the context tableau.
+ * In the basic map, these n_param variables correspond to the
+ * parameters and the input dimensions. In the domain, they correspond
+ * to the parameters and the set dimensions.
+ * The n_div variables correspond to the integer divisions in the domain.
+ * To ensure that everything lines up, we may need to copy some of the
+ * integer divisions of the domain to the map. These have to be placed
+ * in the same order as those in the context and they have to be placed
+ * after any other integer divisions that the map may have.
+ * This function performs the required reordering.
+ */
+static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
+ struct isl_basic_set *dom)
+{
+ int i;
+ int common = 0;
+ int other;
+
+ for (i = 0; i < dom->n_div; ++i)
+ if (find_context_div(bmap, dom, i) != -1)
+ common++;
+ other = bmap->n_div - common;
+ if (dom->n_div - common > 0) {
+ bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
+ dom->n_div - common, 0, 0);
+ if (!bmap)
+ return NULL;
+ }
+ for (i = 0; i < dom->n_div; ++i) {
+ int pos = find_context_div(bmap, dom, i);
+ if (pos < 0) {
+ pos = isl_basic_map_alloc_div(bmap);
+ if (pos < 0)
+ goto error;
+ isl_int_set_si(bmap->div[pos][0], 0);
+ }
+ if (pos != other + i)
+ isl_basic_map_swap_div(bmap, pos, other + i);
+ }
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+/* Base case of isl_tab_basic_map_partial_lexopt, after removing
+ * some obvious symmetries.
+ *
+ * We make sure the divs in the domain are properly ordered,
+ * because they will be added one by one in the given order
+ * during the construction of the solution map.
+ */
+static struct isl_sol *basic_map_partial_lexopt_base(
+ __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
+ __isl_give isl_set **empty, int max,
+ struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
+ __isl_take isl_basic_set *dom, int track_empty, int max))
+{
+ struct isl_tab *tab;
+ struct isl_sol *sol = NULL;
+ struct isl_context *context;
+
+ if (dom->n_div) {
+ dom = isl_basic_set_order_divs(dom);
+ bmap = align_context_divs(bmap, dom);
+ }
+ sol = init(bmap, dom, !!empty, max);
+ if (!sol)
+ goto error;
+
+ context = sol->context;
+ if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
+ /* nothing */;
+ else if (isl_basic_map_plain_is_empty(bmap)) {
+ if (sol->add_empty)
+ sol->add_empty(sol,
+ isl_basic_set_copy(context->op->peek_basic_set(context)));
+ } else {
+ tab = tab_for_lexmin(bmap,
+ context->op->peek_basic_set(context), 1, max);
+ tab = context->op->detect_nonnegative_parameters(context, tab);
+ find_solutions_main(sol, tab);
+ }
+ if (sol->error)
+ goto error;
+
+ isl_basic_map_free(bmap);
+ return sol;
+error:
+ sol_free(sol);
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+/* Base case of isl_tab_basic_map_partial_lexopt, after removing
+ * some obvious symmetries.
+ *
+ * We call basic_map_partial_lexopt_base and extract the results.
+ */
+static __isl_give isl_map *basic_map_partial_lexopt_base_map(
+ __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
+ __isl_give isl_set **empty, int max)
+{
+ isl_map *result = NULL;
+ struct isl_sol *sol;
+ struct isl_sol_map *sol_map;
+
+ sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
+ &sol_map_init);
+ if (!sol)
+ return NULL;
+ sol_map = (struct isl_sol_map *) sol;
+
+ result = isl_map_copy(sol_map->map);
+ if (empty)
+ *empty = isl_set_copy(sol_map->empty);
+ sol_free(&sol_map->sol);
+ return result;
+}
+
+/* Structure used during detection of parallel constraints.
+ * n_in: number of "input" variables: isl_dim_param + isl_dim_in
+ * n_out: number of "output" variables: isl_dim_out + isl_dim_div
+ * val: the coefficients of the output variables
+ */
+struct isl_constraint_equal_info {
+ isl_basic_map *bmap;
+ unsigned n_in;
+ unsigned n_out;
+ isl_int *val;
+};
+
+/* Check whether the coefficients of the output variables
+ * of the constraint in "entry" are equal to info->val.
+ */
+static int constraint_equal(const void *entry, const void *val)
+{
+ isl_int **row = (isl_int **)entry;
+ const struct isl_constraint_equal_info *info = val;
+
+ return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
+}
+
+/* Check whether "bmap" has a pair of constraints that have
+ * the same coefficients for the output variables.
+ * Note that the coefficients of the existentially quantified
+ * variables need to be zero since the existentially quantified
+ * of the result are usually not the same as those of the input.
+ * the isl_dim_out and isl_dim_div dimensions.
+ * If so, return 1 and return the row indices of the two constraints
+ * in *first and *second.
+ */
+static int parallel_constraints(__isl_keep isl_basic_map *bmap,
+ int *first, int *second)
+{
+ int i;
+ isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
+ struct isl_hash_table *table = NULL;
+ struct isl_hash_table_entry *entry;
+ struct isl_constraint_equal_info info;
+ unsigned n_out;
+ unsigned n_div;
+
+ ctx = isl_basic_map_get_ctx(bmap);
+ table = isl_hash_table_alloc(ctx, bmap->n_ineq);
+ if (!table)
+ goto error;
+
+ info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
+ isl_basic_map_dim(bmap, isl_dim_in);
+ info.bmap = bmap;
+ n_out = isl_basic_map_dim(bmap, isl_dim_out);
+ n_div = isl_basic_map_dim(bmap, isl_dim_div);
+ info.n_out = n_out + n_div;
+ for (i = 0; i < bmap->n_ineq; ++i) {
+ uint32_t hash;
+
+ info.val = bmap->ineq[i] + 1 + info.n_in;
+ if (isl_seq_first_non_zero(info.val, n_out) < 0)
+ continue;
+ if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
+ continue;
+ hash = isl_seq_get_hash(info.val, info.n_out);
+ entry = isl_hash_table_find(ctx, table, hash,
+ constraint_equal, &info, 1);
+ if (!entry)
+ goto error;
+ if (entry->data)
+ break;
+ entry->data = &bmap->ineq[i];
+ }
+
+ if (i < bmap->n_ineq) {
+ *first = ((isl_int **)entry->data) - bmap->ineq;
+ *second = i;
+ }
+
+ isl_hash_table_free(ctx, table);
+
+ return i < bmap->n_ineq;
+error:
+ isl_hash_table_free(ctx, table);
+ return -1;
+}
+
+/* Given a set of upper bounds in "var", add constraints to "bset"
+ * that make the i-th bound smallest.
+ *
+ * In particular, if there are n bounds b_i, then add the constraints
+ *
+ * b_i <= b_j for j > i
+ * b_i < b_j for j < i
+ */
+static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
+ __isl_keep isl_mat *var, int i)
+{
+ isl_ctx *ctx;
+ int j, k;
+
+ ctx = isl_mat_get_ctx(var);
+
+ for (j = 0; j < var->n_row; ++j) {
+ if (j == i)
+ continue;
+ k = isl_basic_set_alloc_inequality(bset);
+ if (k < 0)
+ goto error;
+ isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
+ ctx->negone, var->row[i], var->n_col);
+ isl_int_set_si(bset->ineq[k][var->n_col], 0);
+ if (j < i)
+ isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
+ }
+
+ bset = isl_basic_set_finalize(bset);
+
+ return bset;
+error:
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+/* Given a set of upper bounds on the last "input" variable m,
+ * construct a set that assigns the minimal upper bound to m, i.e.,
+ * construct a set that divides the space into cells where one
+ * of the upper bounds is smaller than all the others and assign
+ * this upper bound to m.
+ *
+ * In particular, if there are n bounds b_i, then the result
+ * consists of n basic sets, each one of the form
+ *
+ * m = b_i
+ * b_i <= b_j for j > i
+ * b_i < b_j for j < i
+ */
+static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
+ __isl_take isl_mat *var)
+{
+ int i, k;
+ isl_basic_set *bset = NULL;
+ isl_ctx *ctx;
+ isl_set *set = NULL;
+
+ if (!dim || !var)
+ goto error;
+
+ ctx = isl_space_get_ctx(dim);
+ set = isl_set_alloc_space(isl_space_copy(dim),
+ var->n_row, ISL_SET_DISJOINT);
+
+ for (i = 0; i < var->n_row; ++i) {
+ bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
+ 1, var->n_row - 1);
+ k = isl_basic_set_alloc_equality(bset);
+ if (k < 0)
+ goto error;
+ isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
+ isl_int_set_si(bset->eq[k][var->n_col], -1);
+ bset = select_minimum(bset, var, i);
+ set = isl_set_add_basic_set(set, bset);
+ }
+
+ isl_space_free(dim);
+ isl_mat_free(var);
+ return set;
+error:
+ isl_basic_set_free(bset);
+ isl_set_free(set);
+ isl_space_free(dim);
+ isl_mat_free(var);
+ return NULL;
+}
+
+/* Given that the last input variable of "bmap" represents the minimum
+ * of the bounds in "cst", check whether we need to split the domain
+ * based on which bound attains the minimum.
+ *
+ * A split is needed when the minimum appears in an integer division
+ * or in an equality. Otherwise, it is only needed if it appears in
+ * an upper bound that is different from the upper bounds on which it
+ * is defined.
+ */
+static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
+ __isl_keep isl_mat *cst)
+{
+ int i, j;
+ unsigned total;
+ unsigned pos;
+
+ pos = cst->n_col - 1;
+ total = isl_basic_map_dim(bmap, isl_dim_all);
+
+ for (i = 0; i < bmap->n_div; ++i)
+ if (!isl_int_is_zero(bmap->div[i][2 + pos]))
+ return 1;
+
+ for (i = 0; i < bmap->n_eq; ++i)
+ if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
+ return 1;
+
+ for (i = 0; i < bmap->n_ineq; ++i) {
+ if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
+ continue;
+ if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
+ return 1;
+ if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
+ total - pos - 1) >= 0)
+ return 1;
+
+ for (j = 0; j < cst->n_row; ++j)
+ if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
+ break;
+ if (j >= cst->n_row)
+ return 1;
+ }
+
+ return 0;
+}
+
+/* Given that the last set variable of "bset" represents the minimum
+ * of the bounds in "cst", check whether we need to split the domain
+ * based on which bound attains the minimum.
+ *
+ * We simply call need_split_basic_map here. This is safe because
+ * the position of the minimum is computed from "cst" and not
+ * from "bmap".
+ */
+static int need_split_basic_set(__isl_keep isl_basic_set *bset,
+ __isl_keep isl_mat *cst)
+{
+ return need_split_basic_map((isl_basic_map *)bset, cst);
+}
+
+/* Given that the last set variable of "set" represents the minimum
+ * of the bounds in "cst", check whether we need to split the domain
+ * based on which bound attains the minimum.
+ */
+static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
+{
+ int i;
+
+ for (i = 0; i < set->n; ++i)
+ if (need_split_basic_set(set->p[i], cst))
+ return 1;
+
+ return 0;
+}
+
+/* Given a set of which the last set variable is the minimum
+ * of the bounds in "cst", split each basic set in the set
+ * in pieces where one of the bounds is (strictly) smaller than the others.
+ * This subdivision is given in "min_expr".
+ * The variable is subsequently projected out.
+ *
+ * We only do the split when it is needed.
+ * For example if the last input variable m = min(a,b) and the only
+ * constraints in the given basic set are lower bounds on m,
+ * i.e., l <= m = min(a,b), then we can simply project out m
+ * to obtain l <= a and l <= b, without having to split on whether
+ * m is equal to a or b.
+ */
+static __isl_give isl_set *split(__isl_take isl_set *empty,
+ __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
+{
+ int n_in;
+ int i;
+ isl_space *dim;
+ isl_set *res;
+
+ if (!empty || !min_expr || !cst)
+ goto error;
+
+ n_in = isl_set_dim(empty, isl_dim_set);
+ dim = isl_set_get_space(empty);
+ dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
+ res = isl_set_empty(dim);
+
+ for (i = 0; i < empty->n; ++i) {
+ isl_set *set;
+
+ set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
+ if (need_split_basic_set(empty->p[i], cst))
+ set = isl_set_intersect(set, isl_set_copy(min_expr));
+ set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
+
+ res = isl_set_union_disjoint(res, set);
+ }
+
+ isl_set_free(empty);
+ isl_set_free(min_expr);
+ isl_mat_free(cst);
+ return res;
+error:
+ isl_set_free(empty);
+ isl_set_free(min_expr);
+ isl_mat_free(cst);
+ return NULL;
+}
+
+/* Given a map of which the last input variable is the minimum
+ * of the bounds in "cst", split each basic set in the set
+ * in pieces where one of the bounds is (strictly) smaller than the others.
+ * This subdivision is given in "min_expr".
+ * The variable is subsequently projected out.
+ *
+ * The implementation is essentially the same as that of "split".
+ */
+static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
+ __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
+{
+ int n_in;
+ int i;
+ isl_space *dim;
+ isl_map *res;
+
+ if (!opt || !min_expr || !cst)
+ goto error;
+
+ n_in = isl_map_dim(opt, isl_dim_in);
+ dim = isl_map_get_space(opt);
+ dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
+ res = isl_map_empty(dim);
+
+ for (i = 0; i < opt->n; ++i) {
+ isl_map *map;
+
+ map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
+ if (need_split_basic_map(opt->p[i], cst))
+ map = isl_map_intersect_domain(map,
+ isl_set_copy(min_expr));
+ map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
+
+ res = isl_map_union_disjoint(res, map);
+ }
+
+ isl_map_free(opt);
+ isl_set_free(min_expr);
+ isl_mat_free(cst);
+ return res;
+error:
+ isl_map_free(opt);
+ isl_set_free(min_expr);
+ isl_mat_free(cst);
+ return NULL;
+}
+
+static __isl_give isl_map *basic_map_partial_lexopt(
+ __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
+ __isl_give isl_set **empty, int max);
+
+union isl_lex_res {
+ void *p;
+ isl_map *map;
+ isl_pw_multi_aff *pma;
+};
+
+/* This function is called from basic_map_partial_lexopt_symm.
+ * The last variable of "bmap" and "dom" corresponds to the minimum
+ * of the bounds in "cst". "map_space" is the space of the original
+ * input relation (of basic_map_partial_lexopt_symm) and "set_space"
+ * is the space of the original domain.
+ *
+ * We recursively call basic_map_partial_lexopt and then plug in
+ * the definition of the minimum in the result.
+ */
+static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
+ __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
+ __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
+ __isl_take isl_space *map_space, __isl_take isl_space *set_space)
+{
+ isl_map *opt;
+ isl_set *min_expr;
+ union isl_lex_res res;
+
+ min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
+
+ opt = basic_map_partial_lexopt(bmap, dom, empty, max);
+
+ if (empty) {
+ *empty = split(*empty,
+ isl_set_copy(min_expr), isl_mat_copy(cst));
+ *empty = isl_set_reset_space(*empty, set_space);
+ }
+
+ opt = split_domain(opt, min_expr, cst);
+ opt = isl_map_reset_space(opt, map_space);
+
+ res.map = opt;
+ return res;
+}
+
+/* Given a basic map with at least two parallel constraints (as found
+ * by the function parallel_constraints), first look for more constraints
+ * parallel to the two constraint and replace the found list of parallel
+ * constraints by a single constraint with as "input" part the minimum
+ * of the input parts of the list of constraints. Then, recursively call
+ * basic_map_partial_lexopt (possibly finding more parallel constraints)
+ * and plug in the definition of the minimum in the result.
+ *
+ * More specifically, given a set of constraints
+ *
+ * a x + b_i(p) >= 0
+ *
+ * Replace this set by a single constraint
+ *
+ * a x + u >= 0
+ *
+ * with u a new parameter with constraints
+ *
+ * u <= b_i(p)
+ *
+ * Any solution to the new system is also a solution for the original system
+ * since
+ *
+ * a x >= -u >= -b_i(p)
+ *
+ * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
+ * therefore be plugged into the solution.
+ */
+static union isl_lex_res basic_map_partial_lexopt_symm(
+ __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
+ __isl_give isl_set **empty, int max, int first, int second,
+ __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
+ __isl_take isl_basic_set *dom,
+ __isl_give isl_set **empty,
+ int max, __isl_take isl_mat *cst,
+ __isl_take isl_space *map_space,
+ __isl_take isl_space *set_space))
+{
+ int i, n, k;
+ int *list = NULL;
+ unsigned n_in, n_out, n_div;
+ isl_ctx *ctx;
+ isl_vec *var = NULL;
+ isl_mat *cst = NULL;
+ isl_space *map_space, *set_space;
+ union isl_lex_res res;
+
+ map_space = isl_basic_map_get_space(bmap);
+ set_space = empty ? isl_basic_set_get_space(dom) : NULL;
+
+ n_in = isl_basic_map_dim(bmap, isl_dim_param) +
+ isl_basic_map_dim(bmap, isl_dim_in);
+ n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
+
+ ctx = isl_basic_map_get_ctx(bmap);
+ list = isl_alloc_array(ctx, int, bmap->n_ineq);
+ var = isl_vec_alloc(ctx, n_out);
+ if (!list || !var)
+ goto error;
+
+ list[0] = first;
+ list[1] = second;
+ isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
+ for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
+ if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
+ list[n++] = i;
+ }
+
+ cst = isl_mat_alloc(ctx, n, 1 + n_in);
+ if (!cst)
+ goto error;
+
+ for (i = 0; i < n; ++i)
+ isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
+
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap)
+ goto error;
+ for (i = n - 1; i >= 0; --i)
+ if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
+ goto error;
+
+ bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
+ bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
+ k = isl_basic_map_alloc_inequality(bmap);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bmap->ineq[k], 1 + n_in);
+ isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
+ isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
+ bmap = isl_basic_map_finalize(bmap);
+
+ n_div = isl_basic_set_dim(dom, isl_dim_div);
+ dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
+ dom = isl_basic_set_extend_constraints(dom, 0, n);
+ for (i = 0; i < n; ++i) {
+ k = isl_basic_set_alloc_inequality(dom);
+ if (k < 0)
+ goto error;
+ isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
+ isl_int_set_si(dom->ineq[k][1 + n_in], -1);
+ isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
+ }
+
+ isl_vec_free(var);
+ free(list);
+
+ return core(bmap, dom, empty, max, cst, map_space, set_space);
+error:
+ isl_space_free(map_space);
+ isl_space_free(set_space);
+ isl_mat_free(cst);
+ isl_vec_free(var);
+ free(list);
+ isl_basic_set_free(dom);
+ isl_basic_map_free(bmap);
+ res.p = NULL;
+ return res;
+}
+
+static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
+ __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
+ __isl_give isl_set **empty, int max, int first, int second)
+{
+ return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
+ first, second, &basic_map_partial_lexopt_symm_map_core).map;
+}
+
+/* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
+ * equalities and removing redundant constraints.
+ *
+ * We first check if there are any parallel constraints (left).
+ * If not, we are in the base case.
+ * If there are parallel constraints, we replace them by a single
+ * constraint in basic_map_partial_lexopt_symm and then call
+ * this function recursively to look for more parallel constraints.
+ */
+static __isl_give isl_map *basic_map_partial_lexopt(
+ __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
+ __isl_give isl_set **empty, int max)
+{
+ int par = 0;
+ int first, second;
+
+ if (!bmap)
+ goto error;
+
+ if (bmap->ctx->opt->pip_symmetry)
+ par = parallel_constraints(bmap, &first, &second);
+ if (par < 0)
+ goto error;
+ if (!par)
+ return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
+
+ return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
+ first, second);
+error:
+ isl_basic_set_free(dom);
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+/* Compute the lexicographic minimum (or maximum if "max" is set)
+ * of "bmap" over the domain "dom" and return the result as a map.
+ * If "empty" is not NULL, then *empty is assigned a set that
+ * contains those parts of the domain where there is no solution.
+ * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
+ * then we compute the rational optimum. Otherwise, we compute
+ * the integral optimum.
+ *
+ * We perform some preprocessing. As the PILP solver does not
+ * handle implicit equalities very well, we first make sure all
+ * the equalities are explicitly available.
+ *
+ * We also add context constraints to the basic map and remove
+ * redundant constraints. This is only needed because of the
+ * way we handle simple symmetries. In particular, we currently look
+ * for symmetries on the constraints, before we set up the main tableau.
+ * It is then no good to look for symmetries on possibly redundant constraints.
+ */
+struct isl_map *isl_tab_basic_map_partial_lexopt(
+ struct isl_basic_map *bmap, struct isl_basic_set *dom,
+ struct isl_set **empty, int max)
+{
+ if (empty)
+ *empty = NULL;
+ if (!bmap || !dom)
+ goto error;
+
+ isl_assert(bmap->ctx,
+ isl_basic_map_compatible_domain(bmap, dom), goto error);
+
+ if (isl_basic_set_dim(dom, isl_dim_all) == 0)
+ return basic_map_partial_lexopt(bmap, dom, empty, max);
+
+ bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
+ bmap = isl_basic_map_detect_equalities(bmap);
+ bmap = isl_basic_map_remove_redundancies(bmap);
+
+ return basic_map_partial_lexopt(bmap, dom, empty, max);
+error:
+ isl_basic_set_free(dom);
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+struct isl_sol_for {
+ struct isl_sol sol;
+ int (*fn)(__isl_take isl_basic_set *dom,
+ __isl_take isl_aff_list *list, void *user);
+ void *user;
+};
+
+static void sol_for_free(struct isl_sol_for *sol_for)
+{
+ if (sol_for->sol.context)
+ sol_for->sol.context->op->free(sol_for->sol.context);
+ free(sol_for);
+}
+
+static void sol_for_free_wrap(struct isl_sol *sol)
+{
+ sol_for_free((struct isl_sol_for *)sol);
+}
+
+/* Add the solution identified by the tableau and the context tableau.
+ *
+ * See documentation of sol_add for more details.
+ *
+ * Instead of constructing a basic map, this function calls a user
+ * defined function with the current context as a basic set and
+ * a list of affine expressions representing the relation between
+ * the input and output. The space over which the affine expressions
+ * are defined is the same as that of the domain. The number of
+ * affine expressions in the list is equal to the number of output variables.
+ */
+static void sol_for_add(struct isl_sol_for *sol,
+ struct isl_basic_set *dom, struct isl_mat *M)
+{
+ int i;
+ isl_ctx *ctx;
+ isl_local_space *ls;
+ isl_aff *aff;
+ isl_aff_list *list;
+
+ if (sol->sol.error || !dom || !M)
+ goto error;
+
+ ctx = isl_basic_set_get_ctx(dom);
+ ls = isl_basic_set_get_local_space(dom);
+ list = isl_aff_list_alloc(ctx, M->n_row - 1);
+ for (i = 1; i < M->n_row; ++i) {
+ aff = isl_aff_alloc(isl_local_space_copy(ls));
+ if (aff) {
+ isl_int_set(aff->v->el[0], M->row[0][0]);
+ isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
+ }
+ aff = isl_aff_normalize(aff);
+ list = isl_aff_list_add(list, aff);
+ }
+ isl_local_space_free(ls);
+
+ dom = isl_basic_set_finalize(dom);
+
+ if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
+ goto error;
+
+ isl_basic_set_free(dom);
+ isl_mat_free(M);
+ return;
+error:
+ isl_basic_set_free(dom);
+ isl_mat_free(M);
+ sol->sol.error = 1;
+}
+
+static void sol_for_add_wrap(struct isl_sol *sol,
+ struct isl_basic_set *dom, struct isl_mat *M)
+{
+ sol_for_add((struct isl_sol_for *)sol, dom, M);
+}
+
+static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
+ int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
+ void *user),
+ void *user)
+{
+ struct isl_sol_for *sol_for = NULL;
+ isl_space *dom_dim;
+ struct isl_basic_set *dom = NULL;
+
+ sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
+ if (!sol_for)
+ goto error;
+
+ dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
+ dom = isl_basic_set_universe(dom_dim);
+
+ sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
+ sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
+ sol_for->sol.dec_level.sol = &sol_for->sol;
+ sol_for->fn = fn;
+ sol_for->user = user;
+ sol_for->sol.max = max;
+ sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
+ sol_for->sol.add = &sol_for_add_wrap;
+ sol_for->sol.add_empty = NULL;
+ sol_for->sol.free = &sol_for_free_wrap;
+
+ sol_for->sol.context = isl_context_alloc(dom);
+ if (!sol_for->sol.context)
+ goto error;
+
+ isl_basic_set_free(dom);
+ return sol_for;
+error:
+ isl_basic_set_free(dom);
+ sol_for_free(sol_for);
+ return NULL;
+}
+
+static void sol_for_find_solutions(struct isl_sol_for *sol_for,
+ struct isl_tab *tab)
+{
+ find_solutions_main(&sol_for->sol, tab);
+}
+
+int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
+ int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
+ void *user),
+ void *user)
+{
+ struct isl_sol_for *sol_for = NULL;
+
+ bmap = isl_basic_map_copy(bmap);
+ bmap = isl_basic_map_detect_equalities(bmap);
+ if (!bmap)
+ return -1;
+
+ sol_for = sol_for_init(bmap, max, fn, user);
+ if (!sol_for)
+ goto error;
+
+ if (isl_basic_map_plain_is_empty(bmap))
+ /* nothing */;
+ else {
+ struct isl_tab *tab;
+ struct isl_context *context = sol_for->sol.context;
+ tab = tab_for_lexmin(bmap,
+ context->op->peek_basic_set(context), 1, max);
+ tab = context->op->detect_nonnegative_parameters(context, tab);
+ sol_for_find_solutions(sol_for, tab);
+ if (sol_for->sol.error)
+ goto error;
+ }
+
+ sol_free(&sol_for->sol);
+ isl_basic_map_free(bmap);
+ return 0;
+error:
+ sol_free(&sol_for->sol);
+ isl_basic_map_free(bmap);
+ return -1;
+}
+
+int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
+ int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
+ void *user),
+ void *user)
+{
+ return isl_basic_map_foreach_lexopt(bset, max, fn, user);
+}
+
+/* Check if the given sequence of len variables starting at pos
+ * represents a trivial (i.e., zero) solution.
+ * The variables are assumed to be non-negative and to come in pairs,
+ * with each pair representing a variable of unrestricted sign.
+ * The solution is trivial if each such pair in the sequence consists
+ * of two identical values, meaning that the variable being represented
+ * has value zero.
+ */
+static int region_is_trivial(struct isl_tab *tab, int pos, int len)
+{
+ int i;
+
+ if (len == 0)
+ return 0;
+
+ for (i = 0; i < len; i += 2) {
+ int neg_row;
+ int pos_row;
+
+ neg_row = tab->var[pos + i].is_row ?
+ tab->var[pos + i].index : -1;
+ pos_row = tab->var[pos + i + 1].is_row ?
+ tab->var[pos + i + 1].index : -1;
+
+ if ((neg_row < 0 ||
+ isl_int_is_zero(tab->mat->row[neg_row][1])) &&
+ (pos_row < 0 ||
+ isl_int_is_zero(tab->mat->row[pos_row][1])))
+ continue;
+
+ if (neg_row < 0 || pos_row < 0)
+ return 0;
+ if (isl_int_ne(tab->mat->row[neg_row][1],
+ tab->mat->row[pos_row][1]))
+ return 0;
+ }
+
+ return 1;
+}
+
+/* Return the index of the first trivial region or -1 if all regions
+ * are non-trivial.