1 #include "isl_map_private.h"
4 #include "isl_sample.h"
7 * The implementation of parametric integer linear programming in this file
8 * was inspired by the paper "Parametric Integer Programming" and the
9 * report "Solving systems of affine (in)equalities" by Paul Feautrier
12 * The strategy used for obtaining a feasible solution is different
13 * from the one used in isl_tab.c. In particular, in isl_tab.c,
14 * upon finding a constraint that is not yet satisfied, we pivot
15 * in a row that increases the constant term of row holding the
16 * constraint, making sure the sample solution remains feasible
17 * for all the constraints it already satisfied.
18 * Here, we always pivot in the row holding the constraint,
19 * choosing a column that induces the lexicographically smallest
20 * increment to the sample solution.
22 * By starting out from a sample value that is lexicographically
23 * smaller than any integer point in the problem space, the first
24 * feasible integer sample point we find will also be the lexicographically
25 * smallest. If all variables can be assumed to be non-negative,
26 * then the initial sample value may be chosen equal to zero.
27 * However, we will not make this assumption. Instead, we apply
28 * the "big parameter" trick. Any variable x is then not directly
29 * used in the tableau, but instead it its represented by another
30 * variable x' = M + x, where M is an arbitrarily large (positive)
31 * value. x' is therefore always non-negative, whatever the value of x.
32 * Taking as initial smaple value x' = 0 corresponds to x = -M,
33 * which is always smaller than any possible value of x.
35 * The big parameter trick is used in the main tableau and
36 * also in the context tableau if isl_context_lex is used.
37 * In this case, each tableaus has its own big parameter.
38 * Before doing any real work, we check if all the parameters
39 * happen to be non-negative. If so, we drop the column corresponding
40 * to M from the initial context tableau.
41 * If isl_context_gbr is used, then the big parameter trick is only
42 * used in the main tableau.
46 struct isl_context_op {
47 /* detect nonnegative parameters in context and mark them in tab */
48 struct isl_tab *(*detect_nonnegative_parameters)(
49 struct isl_context *context, struct isl_tab *tab);
50 /* return temporary reference to basic set representation of context */
51 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
52 /* return temporary reference to tableau representation of context */
53 struct isl_tab *(*peek_tab)(struct isl_context *context);
54 /* add equality; check is 1 if eq may not be valid;
55 * update is 1 if we may want to call ineq_sign on context later.
57 void (*add_eq)(struct isl_context *context, isl_int *eq,
58 int check, int update);
59 /* add inequality; check is 1 if ineq may not be valid;
60 * update is 1 if we may want to call ineq_sign on context later.
62 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
63 int check, int update);
64 /* check sign of ineq based on previous information.
65 * strict is 1 if saturation should be treated as a positive sign.
67 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
68 isl_int *ineq, int strict);
69 /* check if inequality maintains feasibility */
70 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
71 /* return index of a div that corresponds to "div" */
72 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
74 /* add div "div" to context and return index and non-negativity */
75 int (*add_div)(struct isl_context *context, struct isl_vec *div,
77 /* return row index of "best" split */
78 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
79 /* check if context has already been determined to be empty */
80 int (*is_empty)(struct isl_context *context);
81 /* check if context is still usable */
82 int (*is_ok)(struct isl_context *context);
83 /* save a copy/snapshot of context */
84 void *(*save)(struct isl_context *context);
85 /* restore saved context */
86 void (*restore)(struct isl_context *context, void *);
87 /* invalidate context */
88 void (*invalidate)(struct isl_context *context);
90 void (*free)(struct isl_context *context);
94 struct isl_context_op *op;
97 struct isl_context_lex {
98 struct isl_context context;
102 /* isl_sol is an interface for constructing a solution to
103 * a parametric integer linear programming problem.
104 * Every time the algorithm reaches a state where a solution
105 * can be read off from the tableau (including cases where the tableau
106 * is empty), the function "add" is called on the isl_sol passed
107 * to find_solutions_main.
109 * The context tableau is owned by isl_sol and is updated incrementally.
111 * There are currently two implementations of this interface,
112 * isl_sol_map, which simply collects the solutions in an isl_map
113 * and (optionally) the parts of the context where there is no solution
115 * isl_sol_for, which calls a user-defined function for each part of
119 struct isl_context *context;
120 struct isl_sol *(*add)(struct isl_sol *sol, struct isl_tab *tab);
121 void (*free)(struct isl_sol *sol);
124 static void sol_free(struct isl_sol *sol)
134 struct isl_set *empty;
138 static void sol_map_free(struct isl_sol_map *sol_map)
140 if (sol_map->sol.context)
141 sol_map->sol.context->op->free(sol_map->sol.context);
142 isl_map_free(sol_map->map);
143 isl_set_free(sol_map->empty);
147 static void sol_map_free_wrap(struct isl_sol *sol)
149 sol_map_free((struct isl_sol_map *)sol);
152 static struct isl_sol_map *add_empty(struct isl_sol_map *sol)
154 struct isl_basic_set *bset;
158 sol->empty = isl_set_grow(sol->empty, 1);
159 bset = sol->sol.context->op->peek_basic_set(sol->sol.context);
160 bset = isl_basic_set_copy(bset);
161 bset = isl_basic_set_simplify(bset);
162 bset = isl_basic_set_finalize(bset);
163 sol->empty = isl_set_add(sol->empty, bset);
172 /* Add the solution identified by the tableau and the context tableau.
174 * The layout of the variables is as follows.
175 * tab->n_var is equal to the total number of variables in the input
176 * map (including divs that were copied from the context)
177 * + the number of extra divs constructed
178 * Of these, the first tab->n_param and the last tab->n_div variables
179 * correspond to the variables in the context, i.e.,
180 * tab->n_param + tab->n_div = context_tab->n_var
181 * tab->n_param is equal to the number of parameters and input
182 * dimensions in the input map
183 * tab->n_div is equal to the number of divs in the context
185 * If there is no solution, then the basic set corresponding to the
186 * context tableau is added to the set "empty".
188 * Otherwise, a basic map is constructed with the same parameters
189 * and divs as the context, the dimensions of the context as input
190 * dimensions and a number of output dimensions that is equal to
191 * the number of output dimensions in the input map.
192 * The divs in the input map (if any) that do not correspond to any
193 * div in the context do not appear in the solution.
194 * The algorithm will make sure that they have an integer value,
195 * but these values themselves are of no interest.
197 * The constraints and divs of the context are simply copied
198 * fron context_tab->bset.
199 * To extract the value of the output variables, it should be noted
200 * that we always use a big parameter M and so the variable stored
201 * in the tableau is not an output variable x itself, but
202 * x' = M + x (in case of minimization)
204 * x' = M - x (in case of maximization)
205 * If x' appears in a column, then its optimal value is zero,
206 * which means that the optimal value of x is an unbounded number
207 * (-M for minimization and M for maximization).
208 * We currently assume that the output dimensions in the original map
209 * are bounded, so this cannot occur.
210 * Similarly, when x' appears in a row, then the coefficient of M in that
211 * row is necessarily 1.
212 * If the row represents
213 * d x' = c + d M + e(y)
214 * then, in case of minimization, an equality
215 * c + e(y) - d x' = 0
216 * is added, and in case of maximization,
217 * c + e(y) + d x' = 0
219 static struct isl_sol_map *sol_map_add(struct isl_sol_map *sol,
223 struct isl_basic_map *bmap = NULL;
224 isl_basic_set *context_bset;
237 return add_empty(sol);
239 context_bset = sol->sol.context->op->peek_basic_set(sol->sol.context);
241 n_out = isl_map_dim(sol->map, isl_dim_out);
242 n_eq = context_bset->n_eq + n_out;
243 n_ineq = context_bset->n_ineq;
244 nparam = tab->n_param;
245 total = isl_map_dim(sol->map, isl_dim_all);
246 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
247 tab->n_div, n_eq, 2 * tab->n_div + n_ineq);
252 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
253 for (i = 0; i < context_bset->n_div; ++i) {
254 int k = isl_basic_map_alloc_div(bmap);
257 isl_seq_cpy(bmap->div[k],
258 context_bset->div[i], 1 + 1 + nparam);
259 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
260 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
261 context_bset->div[i] + 1 + 1 + nparam, i);
263 for (i = 0; i < context_bset->n_eq; ++i) {
264 int k = isl_basic_map_alloc_equality(bmap);
267 isl_seq_cpy(bmap->eq[k], context_bset->eq[i], 1 + nparam);
268 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
269 isl_seq_cpy(bmap->eq[k] + 1 + total,
270 context_bset->eq[i] + 1 + nparam, n_div);
272 for (i = 0; i < context_bset->n_ineq; ++i) {
273 int k = isl_basic_map_alloc_inequality(bmap);
276 isl_seq_cpy(bmap->ineq[k],
277 context_bset->ineq[i], 1 + nparam);
278 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
279 isl_seq_cpy(bmap->ineq[k] + 1 + total,
280 context_bset->ineq[i] + 1 + nparam, n_div);
282 for (i = tab->n_param; i < total; ++i) {
283 int k = isl_basic_map_alloc_equality(bmap);
286 isl_seq_clr(bmap->eq[k] + 1, isl_basic_map_total_dim(bmap));
287 if (!tab->var[i].is_row) {
289 isl_assert(bmap->ctx, !tab->M, goto error);
290 isl_int_set_si(bmap->eq[k][0], 0);
292 isl_int_set_si(bmap->eq[k][1 + i], 1);
294 isl_int_set_si(bmap->eq[k][1 + i], -1);
297 row = tab->var[i].index;
300 isl_assert(bmap->ctx,
301 isl_int_eq(tab->mat->row[row][2],
302 tab->mat->row[row][0]),
304 isl_int_set(bmap->eq[k][0], tab->mat->row[row][1]);
305 for (j = 0; j < tab->n_param; ++j) {
307 if (tab->var[j].is_row)
309 col = tab->var[j].index;
310 isl_int_set(bmap->eq[k][1 + j],
311 tab->mat->row[row][off + col]);
313 for (j = 0; j < tab->n_div; ++j) {
315 if (tab->var[tab->n_var - tab->n_div+j].is_row)
317 col = tab->var[tab->n_var - tab->n_div+j].index;
318 isl_int_set(bmap->eq[k][1 + total + j],
319 tab->mat->row[row][off + col]);
322 isl_int_set(bmap->eq[k][1 + i],
323 tab->mat->row[row][0]);
325 isl_int_neg(bmap->eq[k][1 + i],
326 tab->mat->row[row][0]);
329 bmap = isl_basic_map_simplify(bmap);
330 bmap = isl_basic_map_finalize(bmap);
331 sol->map = isl_map_grow(sol->map, 1);
332 sol->map = isl_map_add(sol->map, bmap);
337 isl_basic_map_free(bmap);
342 static struct isl_sol *sol_map_add_wrap(struct isl_sol *sol,
345 return (struct isl_sol *)sol_map_add((struct isl_sol_map *)sol, tab);
349 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
350 * i.e., the constant term and the coefficients of all variables that
351 * appear in the context tableau.
352 * Note that the coefficient of the big parameter M is NOT copied.
353 * The context tableau may not have a big parameter and even when it
354 * does, it is a different big parameter.
356 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
359 unsigned off = 2 + tab->M;
361 isl_int_set(line[0], tab->mat->row[row][1]);
362 for (i = 0; i < tab->n_param; ++i) {
363 if (tab->var[i].is_row)
364 isl_int_set_si(line[1 + i], 0);
366 int col = tab->var[i].index;
367 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
370 for (i = 0; i < tab->n_div; ++i) {
371 if (tab->var[tab->n_var - tab->n_div + i].is_row)
372 isl_int_set_si(line[1 + tab->n_param + i], 0);
374 int col = tab->var[tab->n_var - tab->n_div + i].index;
375 isl_int_set(line[1 + tab->n_param + i],
376 tab->mat->row[row][off + col]);
381 /* Check if rows "row1" and "row2" have identical "parametric constants",
382 * as explained above.
383 * In this case, we also insist that the coefficients of the big parameter
384 * be the same as the values of the constants will only be the same
385 * if these coefficients are also the same.
387 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
390 unsigned off = 2 + tab->M;
392 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
395 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
396 tab->mat->row[row2][2]))
399 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
400 int pos = i < tab->n_param ? i :
401 tab->n_var - tab->n_div + i - tab->n_param;
404 if (tab->var[pos].is_row)
406 col = tab->var[pos].index;
407 if (isl_int_ne(tab->mat->row[row1][off + col],
408 tab->mat->row[row2][off + col]))
414 /* Return an inequality that expresses that the "parametric constant"
415 * should be non-negative.
416 * This function is only called when the coefficient of the big parameter
419 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
421 struct isl_vec *ineq;
423 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
427 get_row_parameter_line(tab, row, ineq->el);
429 ineq = isl_vec_normalize(ineq);
434 /* Return a integer division for use in a parametric cut based on the given row.
435 * In particular, let the parametric constant of the row be
439 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
440 * The div returned is equal to
442 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
444 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
448 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
452 isl_int_set(div->el[0], tab->mat->row[row][0]);
453 get_row_parameter_line(tab, row, div->el + 1);
454 div = isl_vec_normalize(div);
455 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
456 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
461 /* Return a integer division for use in transferring an integrality constraint
463 * In particular, let the parametric constant of the row be
467 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
468 * The the returned div is equal to
470 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
472 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
476 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
480 isl_int_set(div->el[0], tab->mat->row[row][0]);
481 get_row_parameter_line(tab, row, div->el + 1);
482 div = isl_vec_normalize(div);
483 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
488 /* Construct and return an inequality that expresses an upper bound
490 * In particular, if the div is given by
494 * then the inequality expresses
498 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
502 struct isl_vec *ineq;
507 total = isl_basic_set_total_dim(bset);
508 div_pos = 1 + total - bset->n_div + div;
510 ineq = isl_vec_alloc(bset->ctx, 1 + total);
514 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
515 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
519 /* Given a row in the tableau and a div that was created
520 * using get_row_split_div and that been constrained to equality, i.e.,
522 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
524 * replace the expression "\sum_i {a_i} y_i" in the row by d,
525 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
526 * The coefficients of the non-parameters in the tableau have been
527 * verified to be integral. We can therefore simply replace coefficient b
528 * by floor(b). For the coefficients of the parameters we have
529 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
532 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
535 unsigned off = 2 + tab->M;
537 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
538 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
540 isl_int_set_si(tab->mat->row[row][0], 1);
542 isl_assert(tab->mat->ctx,
543 !tab->var[tab->n_var - tab->n_div + div].is_row, goto error);
545 col = tab->var[tab->n_var - tab->n_div + div].index;
546 isl_int_set_si(tab->mat->row[row][off + col], 1);
554 /* Check if the (parametric) constant of the given row is obviously
555 * negative, meaning that we don't need to consult the context tableau.
556 * If there is a big parameter and its coefficient is non-zero,
557 * then this coefficient determines the outcome.
558 * Otherwise, we check whether the constant is negative and
559 * all non-zero coefficients of parameters are negative and
560 * belong to non-negative parameters.
562 static int is_obviously_neg(struct isl_tab *tab, int row)
566 unsigned off = 2 + tab->M;
569 if (isl_int_is_pos(tab->mat->row[row][2]))
571 if (isl_int_is_neg(tab->mat->row[row][2]))
575 if (isl_int_is_nonneg(tab->mat->row[row][1]))
577 for (i = 0; i < tab->n_param; ++i) {
578 /* Eliminated parameter */
579 if (tab->var[i].is_row)
581 col = tab->var[i].index;
582 if (isl_int_is_zero(tab->mat->row[row][off + col]))
584 if (!tab->var[i].is_nonneg)
586 if (isl_int_is_pos(tab->mat->row[row][off + col]))
589 for (i = 0; i < tab->n_div; ++i) {
590 if (tab->var[tab->n_var - tab->n_div + i].is_row)
592 col = tab->var[tab->n_var - tab->n_div + i].index;
593 if (isl_int_is_zero(tab->mat->row[row][off + col]))
595 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
597 if (isl_int_is_pos(tab->mat->row[row][off + col]))
603 /* Check if the (parametric) constant of the given row is obviously
604 * non-negative, meaning that we don't need to consult the context tableau.
605 * If there is a big parameter and its coefficient is non-zero,
606 * then this coefficient determines the outcome.
607 * Otherwise, we check whether the constant is non-negative and
608 * all non-zero coefficients of parameters are positive and
609 * belong to non-negative parameters.
611 static int is_obviously_nonneg(struct isl_tab *tab, int row)
615 unsigned off = 2 + tab->M;
618 if (isl_int_is_pos(tab->mat->row[row][2]))
620 if (isl_int_is_neg(tab->mat->row[row][2]))
624 if (isl_int_is_neg(tab->mat->row[row][1]))
626 for (i = 0; i < tab->n_param; ++i) {
627 /* Eliminated parameter */
628 if (tab->var[i].is_row)
630 col = tab->var[i].index;
631 if (isl_int_is_zero(tab->mat->row[row][off + col]))
633 if (!tab->var[i].is_nonneg)
635 if (isl_int_is_neg(tab->mat->row[row][off + col]))
638 for (i = 0; i < tab->n_div; ++i) {
639 if (tab->var[tab->n_var - tab->n_div + i].is_row)
641 col = tab->var[tab->n_var - tab->n_div + i].index;
642 if (isl_int_is_zero(tab->mat->row[row][off + col]))
644 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
646 if (isl_int_is_neg(tab->mat->row[row][off + col]))
652 /* Given a row r and two columns, return the column that would
653 * lead to the lexicographically smallest increment in the sample
654 * solution when leaving the basis in favor of the row.
655 * Pivoting with column c will increment the sample value by a non-negative
656 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
657 * corresponding to the non-parametric variables.
658 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
659 * with all other entries in this virtual row equal to zero.
660 * If variable v appears in a row, then a_{v,c} is the element in column c
663 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
664 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
665 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
666 * increment. Otherwise, it's c2.
668 static int lexmin_col_pair(struct isl_tab *tab,
669 int row, int col1, int col2, isl_int tmp)
674 tr = tab->mat->row[row] + 2 + tab->M;
676 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
680 if (!tab->var[i].is_row) {
681 if (tab->var[i].index == col1)
683 if (tab->var[i].index == col2)
688 if (tab->var[i].index == row)
691 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
692 s1 = isl_int_sgn(r[col1]);
693 s2 = isl_int_sgn(r[col2]);
694 if (s1 == 0 && s2 == 0)
701 isl_int_mul(tmp, r[col2], tr[col1]);
702 isl_int_submul(tmp, r[col1], tr[col2]);
703 if (isl_int_is_pos(tmp))
705 if (isl_int_is_neg(tmp))
711 /* Given a row in the tableau, find and return the column that would
712 * result in the lexicographically smallest, but positive, increment
713 * in the sample point.
714 * If there is no such column, then return tab->n_col.
715 * If anything goes wrong, return -1.
717 static int lexmin_pivot_col(struct isl_tab *tab, int row)
720 int col = tab->n_col;
724 tr = tab->mat->row[row] + 2 + tab->M;
728 for (j = tab->n_dead; j < tab->n_col; ++j) {
729 if (tab->col_var[j] >= 0 &&
730 (tab->col_var[j] < tab->n_param ||
731 tab->col_var[j] >= tab->n_var - tab->n_div))
734 if (!isl_int_is_pos(tr[j]))
737 if (col == tab->n_col)
740 col = lexmin_col_pair(tab, row, col, j, tmp);
741 isl_assert(tab->mat->ctx, col >= 0, goto error);
751 /* Return the first known violated constraint, i.e., a non-negative
752 * contraint that currently has an either obviously negative value
753 * or a previously determined to be negative value.
755 * If any constraint has a negative coefficient for the big parameter,
756 * if any, then we return one of these first.
758 static int first_neg(struct isl_tab *tab)
763 for (row = tab->n_redundant; row < tab->n_row; ++row) {
764 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
766 if (isl_int_is_neg(tab->mat->row[row][2]))
769 for (row = tab->n_redundant; row < tab->n_row; ++row) {
770 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
773 if (tab->row_sign[row] == 0 &&
774 is_obviously_neg(tab, row))
775 tab->row_sign[row] = isl_tab_row_neg;
776 if (tab->row_sign[row] != isl_tab_row_neg)
778 } else if (!is_obviously_neg(tab, row))
785 /* Resolve all known or obviously violated constraints through pivoting.
786 * In particular, as long as we can find any violated constraint, we
787 * look for a pivoting column that would result in the lexicographicallly
788 * smallest increment in the sample point. If there is no such column
789 * then the tableau is infeasible.
791 static struct isl_tab *restore_lexmin(struct isl_tab *tab)
799 while ((row = first_neg(tab)) != -1) {
800 col = lexmin_pivot_col(tab, row);
801 if (col >= tab->n_col)
802 return isl_tab_mark_empty(tab);
805 isl_tab_pivot(tab, row, col);
813 /* Given a row that represents an equality, look for an appropriate
815 * In particular, if there are any non-zero coefficients among
816 * the non-parameter variables, then we take the last of these
817 * variables. Eliminating this variable in terms of the other
818 * variables and/or parameters does not influence the property
819 * that all column in the initial tableau are lexicographically
820 * positive. The row corresponding to the eliminated variable
821 * will only have non-zero entries below the diagonal of the
822 * initial tableau. That is, we transform
828 * If there is no such non-parameter variable, then we are dealing with
829 * pure parameter equality and we pick any parameter with coefficient 1 or -1
830 * for elimination. This will ensure that the eliminated parameter
831 * always has an integer value whenever all the other parameters are integral.
832 * If there is no such parameter then we return -1.
834 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
836 unsigned off = 2 + tab->M;
839 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
841 if (tab->var[i].is_row)
843 col = tab->var[i].index;
844 if (col <= tab->n_dead)
846 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
849 for (i = tab->n_dead; i < tab->n_col; ++i) {
850 if (isl_int_is_one(tab->mat->row[row][off + i]))
852 if (isl_int_is_negone(tab->mat->row[row][off + i]))
858 /* Add an equality that is known to be valid to the tableau.
859 * We first check if we can eliminate a variable or a parameter.
860 * If not, we add the equality as two inequalities.
861 * In this case, the equality was a pure parameter equality and there
862 * is no need to resolve any constraint violations.
864 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
871 r = isl_tab_add_row(tab, eq);
875 r = tab->con[r].index;
876 i = last_var_col_or_int_par_col(tab, r);
878 tab->con[r].is_nonneg = 1;
879 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
880 isl_seq_neg(eq, eq, 1 + tab->n_var);
881 r = isl_tab_add_row(tab, eq);
884 tab->con[r].is_nonneg = 1;
885 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
887 isl_tab_pivot(tab, r, i);
888 isl_tab_kill_col(tab, i);
891 tab = restore_lexmin(tab);
900 /* Check if the given row is a pure constant.
902 static int is_constant(struct isl_tab *tab, int row)
904 unsigned off = 2 + tab->M;
906 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
907 tab->n_col - tab->n_dead) == -1;
910 /* Add an equality that may or may not be valid to the tableau.
911 * If the resulting row is a pure constant, then it must be zero.
912 * Otherwise, the resulting tableau is empty.
914 * If the row is not a pure constant, then we add two inequalities,
915 * each time checking that they can be satisfied.
916 * In the end we try to use one of the two constraints to eliminate
919 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
923 struct isl_tab_undo *snap;
927 snap = isl_tab_snap(tab);
928 r1 = isl_tab_add_row(tab, eq);
931 tab->con[r1].is_nonneg = 1;
932 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]);
934 row = tab->con[r1].index;
935 if (is_constant(tab, row)) {
936 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
937 (tab->M && !isl_int_is_zero(tab->mat->row[row][2])))
938 return isl_tab_mark_empty(tab);
939 if (isl_tab_rollback(tab, snap) < 0)
944 tab = restore_lexmin(tab);
945 if (!tab || tab->empty)
948 isl_seq_neg(eq, eq, 1 + tab->n_var);
950 r2 = isl_tab_add_row(tab, eq);
953 tab->con[r2].is_nonneg = 1;
954 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]);
956 tab = restore_lexmin(tab);
957 if (!tab || tab->empty)
960 if (!tab->con[r1].is_row)
961 isl_tab_kill_col(tab, tab->con[r1].index);
962 else if (!tab->con[r2].is_row)
963 isl_tab_kill_col(tab, tab->con[r2].index);
964 else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
965 unsigned off = 2 + tab->M;
967 int row = tab->con[r1].index;
968 i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
969 tab->n_col - tab->n_dead);
971 isl_tab_pivot(tab, row, tab->n_dead + i);
972 isl_tab_kill_col(tab, tab->n_dead + i);
977 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
978 isl_tab_push(tab, isl_tab_undo_bset_ineq);
979 isl_seq_neg(eq, eq, 1 + tab->n_var);
980 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
981 isl_seq_neg(eq, eq, 1 + tab->n_var);
982 isl_tab_push(tab, isl_tab_undo_bset_ineq);
993 /* Add an inequality to the tableau, resolving violations using
996 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1003 tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
1004 isl_tab_push(tab, isl_tab_undo_bset_ineq);
1008 r = isl_tab_add_row(tab, ineq);
1011 tab->con[r].is_nonneg = 1;
1012 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1013 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1014 isl_tab_mark_redundant(tab, tab->con[r].index);
1018 tab = restore_lexmin(tab);
1019 if (tab && !tab->empty && tab->con[r].is_row &&
1020 isl_tab_row_is_redundant(tab, tab->con[r].index))
1021 isl_tab_mark_redundant(tab, tab->con[r].index);
1028 /* Check if the coefficients of the parameters are all integral.
1030 static int integer_parameter(struct isl_tab *tab, int row)
1034 unsigned off = 2 + tab->M;
1036 for (i = 0; i < tab->n_param; ++i) {
1037 /* Eliminated parameter */
1038 if (tab->var[i].is_row)
1040 col = tab->var[i].index;
1041 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1042 tab->mat->row[row][0]))
1045 for (i = 0; i < tab->n_div; ++i) {
1046 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1048 col = tab->var[tab->n_var - tab->n_div + i].index;
1049 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1050 tab->mat->row[row][0]))
1056 /* Check if the coefficients of the non-parameter variables are all integral.
1058 static int integer_variable(struct isl_tab *tab, int row)
1061 unsigned off = 2 + tab->M;
1063 for (i = 0; i < tab->n_col; ++i) {
1064 if (tab->col_var[i] >= 0 &&
1065 (tab->col_var[i] < tab->n_param ||
1066 tab->col_var[i] >= tab->n_var - tab->n_div))
1068 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1069 tab->mat->row[row][0]))
1075 /* Check if the constant term is integral.
1077 static int integer_constant(struct isl_tab *tab, int row)
1079 return isl_int_is_divisible_by(tab->mat->row[row][1],
1080 tab->mat->row[row][0]);
1083 #define I_CST 1 << 0
1084 #define I_PAR 1 << 1
1085 #define I_VAR 1 << 2
1087 /* Check for first (non-parameter) variable that is non-integer and
1088 * therefore requires a cut.
1089 * For parametric tableaus, there are three parts in a row,
1090 * the constant, the coefficients of the parameters and the rest.
1091 * For each part, we check whether the coefficients in that part
1092 * are all integral and if so, set the corresponding flag in *f.
1093 * If the constant and the parameter part are integral, then the
1094 * current sample value is integral and no cut is required
1095 * (irrespective of whether the variable part is integral).
1097 static int first_non_integer(struct isl_tab *tab, int *f)
1101 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1104 if (!tab->var[i].is_row)
1106 row = tab->var[i].index;
1107 if (integer_constant(tab, row))
1108 ISL_FL_SET(flags, I_CST);
1109 if (integer_parameter(tab, row))
1110 ISL_FL_SET(flags, I_PAR);
1111 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1113 if (integer_variable(tab, row))
1114 ISL_FL_SET(flags, I_VAR);
1121 /* Add a (non-parametric) cut to cut away the non-integral sample
1122 * value of the given row.
1124 * If the row is given by
1126 * m r = f + \sum_i a_i y_i
1130 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1132 * The big parameter, if any, is ignored, since it is assumed to be big
1133 * enough to be divisible by any integer.
1134 * If the tableau is actually a parametric tableau, then this function
1135 * is only called when all coefficients of the parameters are integral.
1136 * The cut therefore has zero coefficients for the parameters.
1138 * The current value is known to be negative, so row_sign, if it
1139 * exists, is set accordingly.
1141 * Return the row of the cut or -1.
1143 static int add_cut(struct isl_tab *tab, int row)
1148 unsigned off = 2 + tab->M;
1150 if (isl_tab_extend_cons(tab, 1) < 0)
1152 r = isl_tab_allocate_con(tab);
1156 r_row = tab->mat->row[tab->con[r].index];
1157 isl_int_set(r_row[0], tab->mat->row[row][0]);
1158 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1159 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1160 isl_int_neg(r_row[1], r_row[1]);
1162 isl_int_set_si(r_row[2], 0);
1163 for (i = 0; i < tab->n_col; ++i)
1164 isl_int_fdiv_r(r_row[off + i],
1165 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1167 tab->con[r].is_nonneg = 1;
1168 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1170 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1172 return tab->con[r].index;
1175 /* Given a non-parametric tableau, add cuts until an integer
1176 * sample point is obtained or until the tableau is determined
1177 * to be integer infeasible.
1178 * As long as there is any non-integer value in the sample point,
1179 * we add an appropriate cut, if possible and resolve the violated
1180 * cut constraint using restore_lexmin.
1181 * If one of the corresponding rows is equal to an integral
1182 * combination of variables/constraints plus a non-integral constant,
1183 * then there is no way to obtain an integer point an we return
1184 * a tableau that is marked empty.
1186 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
1196 while ((row = first_non_integer(tab, &flags)) != -1) {
1197 if (ISL_FL_ISSET(flags, I_VAR))
1198 return isl_tab_mark_empty(tab);
1199 row = add_cut(tab, row);
1202 tab = restore_lexmin(tab);
1203 if (!tab || tab->empty)
1212 /* Check whether all the currently active samples also satisfy the inequality
1213 * "ineq" (treated as an equality if eq is set).
1214 * Remove those samples that do not.
1216 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1224 isl_assert(tab->mat->ctx, tab->bset, goto error);
1225 isl_assert(tab->mat->ctx, tab->samples, goto error);
1226 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1229 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1231 isl_seq_inner_product(ineq, tab->samples->row[i],
1232 1 + tab->n_var, &v);
1233 sgn = isl_int_sgn(v);
1234 if (eq ? (sgn == 0) : (sgn >= 0))
1236 tab = isl_tab_drop_sample(tab, i);
1248 /* Check whether the sample value of the tableau is finite,
1249 * i.e., either the tableau does not use a big parameter, or
1250 * all values of the variables are equal to the big parameter plus
1251 * some constant. This constant is the actual sample value.
1253 static int sample_is_finite(struct isl_tab *tab)
1260 for (i = 0; i < tab->n_var; ++i) {
1262 if (!tab->var[i].is_row)
1264 row = tab->var[i].index;
1265 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1271 /* Check if the context tableau of sol has any integer points.
1272 * Leave tab in empty state if no integer point can be found.
1273 * If an integer point can be found and if moreover it is finite,
1274 * then it is added to the list of sample values.
1276 * This function is only called when none of the currently active sample
1277 * values satisfies the most recently added constraint.
1279 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1281 struct isl_tab_undo *snap;
1287 snap = isl_tab_snap(tab);
1288 isl_tab_push_basis(tab);
1290 tab = cut_to_integer_lexmin(tab);
1294 if (!tab->empty && sample_is_finite(tab)) {
1295 struct isl_vec *sample;
1297 sample = isl_tab_get_sample_value(tab);
1299 tab = isl_tab_add_sample(tab, sample);
1302 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1311 /* Check if any of the currently active sample values satisfies
1312 * the inequality "ineq" (an equality if eq is set).
1314 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1322 isl_assert(tab->mat->ctx, tab->bset, return -1);
1323 isl_assert(tab->mat->ctx, tab->samples, return -1);
1324 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1327 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1329 isl_seq_inner_product(ineq, tab->samples->row[i],
1330 1 + tab->n_var, &v);
1331 sgn = isl_int_sgn(v);
1332 if (eq ? (sgn == 0) : (sgn >= 0))
1337 return i < tab->n_sample;
1340 /* For a div d = floor(f/m), add the constraints
1343 * -(f-(m-1)) + m d >= 0
1345 * Note that the second constraint is the negation of
1349 static void add_div_constraints(struct isl_context *context, unsigned div)
1353 struct isl_vec *ineq;
1354 struct isl_basic_set *bset;
1356 bset = context->op->peek_basic_set(context);
1360 total = isl_basic_set_total_dim(bset);
1361 div_pos = 1 + total - bset->n_div + div;
1363 ineq = ineq_for_div(bset, div);
1367 context->op->add_ineq(context, ineq->el, 0, 0);
1369 isl_seq_neg(ineq->el, bset->div[div] + 1, 1 + total);
1370 isl_int_set(ineq->el[div_pos], bset->div[div][0]);
1371 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
1372 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
1374 context->op->add_ineq(context, ineq->el, 0, 0);
1380 context->op->invalidate(context);
1383 /* Add a div specifed by "div" to the tableau "tab" and return
1384 * the index of the new div. *nonneg is set to 1 if the div
1385 * is obviously non-negative.
1387 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1393 struct isl_mat *samples;
1395 for (i = 0; i < tab->n_var; ++i) {
1396 if (isl_int_is_zero(div->el[2 + i]))
1398 if (!tab->var[i].is_nonneg)
1401 *nonneg = i == tab->n_var;
1403 if (isl_tab_extend_cons(tab, 3) < 0)
1405 if (isl_tab_extend_vars(tab, 1) < 0)
1407 r = isl_tab_allocate_var(tab);
1411 tab->var[r].is_nonneg = 1;
1412 tab->var[r].frozen = 1;
1414 samples = isl_mat_extend(tab->samples,
1415 tab->n_sample, 1 + tab->n_var);
1416 tab->samples = samples;
1419 for (i = tab->n_outside; i < samples->n_row; ++i) {
1420 isl_seq_inner_product(div->el + 1, samples->row[i],
1421 div->size - 1, &samples->row[i][samples->n_col - 1]);
1422 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1423 samples->row[i][samples->n_col - 1], div->el[0]);
1426 tab->bset = isl_basic_set_extend_dim(tab->bset,
1427 isl_basic_set_get_dim(tab->bset), 1, 0, 2);
1428 k = isl_basic_set_alloc_div(tab->bset);
1431 isl_seq_cpy(tab->bset->div[k], div->el, div->size);
1432 isl_tab_push(tab, isl_tab_undo_bset_div);
1437 /* Add a div specified by "div" to both the main tableau and
1438 * the context tableau. In case of the main tableau, we only
1439 * need to add an extra div. In the context tableau, we also
1440 * need to express the meaning of the div.
1441 * Return the index of the div or -1 if anything went wrong.
1443 static int add_div(struct isl_tab *tab, struct isl_context *context,
1444 struct isl_vec *div)
1450 k = context->op->add_div(context, div, &nonneg);
1454 add_div_constraints(context, k);
1455 if (!context->op->is_ok(context))
1458 if (isl_tab_extend_vars(tab, 1) < 0)
1460 r = isl_tab_allocate_var(tab);
1464 tab->var[r].is_nonneg = 1;
1465 tab->var[r].frozen = 1;
1468 return tab->n_div - 1;
1470 context->op->invalidate(context);
1474 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1477 unsigned total = isl_basic_set_total_dim(tab->bset);
1479 for (i = 0; i < tab->bset->n_div; ++i) {
1480 if (isl_int_ne(tab->bset->div[i][0], denom))
1482 if (!isl_seq_eq(tab->bset->div[i] + 1, div, total))
1489 /* Return the index of a div that corresponds to "div".
1490 * We first check if we already have such a div and if not, we create one.
1492 static int get_div(struct isl_tab *tab, struct isl_context *context,
1493 struct isl_vec *div)
1496 struct isl_tab *context_tab = context->op->peek_tab(context);
1501 d = find_div(context_tab, div->el + 1, div->el[0]);
1505 return add_div(tab, context, div);
1508 /* Add a parametric cut to cut away the non-integral sample value
1510 * Let a_i be the coefficients of the constant term and the parameters
1511 * and let b_i be the coefficients of the variables or constraints
1512 * in basis of the tableau.
1513 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1515 * The cut is expressed as
1517 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1519 * If q did not already exist in the context tableau, then it is added first.
1520 * If q is in a column of the main tableau then the "+ q" can be accomplished
1521 * by setting the corresponding entry to the denominator of the constraint.
1522 * If q happens to be in a row of the main tableau, then the corresponding
1523 * row needs to be added instead (taking care of the denominators).
1524 * Note that this is very unlikely, but perhaps not entirely impossible.
1526 * The current value of the cut is known to be negative (or at least
1527 * non-positive), so row_sign is set accordingly.
1529 * Return the row of the cut or -1.
1531 static int add_parametric_cut(struct isl_tab *tab, int row,
1532 struct isl_context *context)
1534 struct isl_vec *div;
1540 unsigned off = 2 + tab->M;
1545 div = get_row_parameter_div(tab, row);
1549 d = context->op->get_div(context, tab, div);
1553 if (isl_tab_extend_cons(tab, 1) < 0)
1555 r = isl_tab_allocate_con(tab);
1559 r_row = tab->mat->row[tab->con[r].index];
1560 isl_int_set(r_row[0], tab->mat->row[row][0]);
1561 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1562 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1563 isl_int_neg(r_row[1], r_row[1]);
1565 isl_int_set_si(r_row[2], 0);
1566 for (i = 0; i < tab->n_param; ++i) {
1567 if (tab->var[i].is_row)
1569 col = tab->var[i].index;
1570 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1571 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1572 tab->mat->row[row][0]);
1573 isl_int_neg(r_row[off + col], r_row[off + col]);
1575 for (i = 0; i < tab->n_div; ++i) {
1576 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1578 col = tab->var[tab->n_var - tab->n_div + i].index;
1579 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1580 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1581 tab->mat->row[row][0]);
1582 isl_int_neg(r_row[off + col], r_row[off + col]);
1584 for (i = 0; i < tab->n_col; ++i) {
1585 if (tab->col_var[i] >= 0 &&
1586 (tab->col_var[i] < tab->n_param ||
1587 tab->col_var[i] >= tab->n_var - tab->n_div))
1589 isl_int_fdiv_r(r_row[off + i],
1590 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1592 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1594 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1596 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1597 isl_int_divexact(r_row[0], r_row[0], gcd);
1598 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1599 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1600 r_row[0], tab->mat->row[d_row] + 1,
1601 off - 1 + tab->n_col);
1602 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1605 col = tab->var[tab->n_var - tab->n_div + d].index;
1606 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1609 tab->con[r].is_nonneg = 1;
1610 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1612 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1616 return tab->con[r].index;
1619 /* Construct a tableau for bmap that can be used for computing
1620 * the lexicographic minimum (or maximum) of bmap.
1621 * If not NULL, then dom is the domain where the minimum
1622 * should be computed. In this case, we set up a parametric
1623 * tableau with row signs (initialized to "unknown").
1624 * If M is set, then the tableau will use a big parameter.
1625 * If max is set, then a maximum should be computed instead of a minimum.
1626 * This means that for each variable x, the tableau will contain the variable
1627 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1628 * of the variables in all constraints are negated prior to adding them
1631 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
1632 struct isl_basic_set *dom, unsigned M, int max)
1635 struct isl_tab *tab;
1637 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
1638 isl_basic_map_total_dim(bmap), M);
1642 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1644 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
1645 tab->n_div = dom->n_div;
1646 tab->row_sign = isl_calloc_array(bmap->ctx,
1647 enum isl_tab_row_sign, tab->mat->n_row);
1651 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1652 return isl_tab_mark_empty(tab);
1654 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1655 tab->var[i].is_nonneg = 1;
1656 tab->var[i].frozen = 1;
1658 for (i = 0; i < bmap->n_eq; ++i) {
1660 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1661 bmap->eq[i] + 1 + tab->n_param,
1662 tab->n_var - tab->n_param - tab->n_div);
1663 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
1665 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1666 bmap->eq[i] + 1 + tab->n_param,
1667 tab->n_var - tab->n_param - tab->n_div);
1668 if (!tab || tab->empty)
1671 for (i = 0; i < bmap->n_ineq; ++i) {
1673 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1674 bmap->ineq[i] + 1 + tab->n_param,
1675 tab->n_var - tab->n_param - tab->n_div);
1676 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
1678 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1679 bmap->ineq[i] + 1 + tab->n_param,
1680 tab->n_var - tab->n_param - tab->n_div);
1681 if (!tab || tab->empty)
1690 /* Given a main tableau where more than one row requires a split,
1691 * determine and return the "best" row to split on.
1693 * Given two rows in the main tableau, if the inequality corresponding
1694 * to the first row is redundant with respect to that of the second row
1695 * in the current tableau, then it is better to split on the second row,
1696 * since in the positive part, both row will be positive.
1697 * (In the negative part a pivot will have to be performed and just about
1698 * anything can happen to the sign of the other row.)
1700 * As a simple heuristic, we therefore select the row that makes the most
1701 * of the other rows redundant.
1703 * Perhaps it would also be useful to look at the number of constraints
1704 * that conflict with any given constraint.
1706 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
1708 struct isl_tab_undo *snap;
1714 if (isl_tab_extend_cons(context_tab, 2) < 0)
1717 snap = isl_tab_snap(context_tab);
1719 for (split = tab->n_redundant; split < tab->n_row; ++split) {
1720 struct isl_tab_undo *snap2;
1721 struct isl_vec *ineq = NULL;
1724 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
1726 if (tab->row_sign[split] != isl_tab_row_any)
1729 ineq = get_row_parameter_ineq(tab, split);
1732 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
1735 snap2 = isl_tab_snap(context_tab);
1737 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1738 struct isl_tab_var *var;
1742 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1744 if (tab->row_sign[row] != isl_tab_row_any)
1747 ineq = get_row_parameter_ineq(tab, row);
1750 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
1752 var = &context_tab->con[context_tab->n_con - 1];
1753 if (!context_tab->empty &&
1754 !isl_tab_min_at_most_neg_one(context_tab, var))
1756 if (isl_tab_rollback(context_tab, snap2) < 0)
1759 if (best == -1 || r > best_r) {
1763 if (isl_tab_rollback(context_tab, snap) < 0)
1770 static struct isl_basic_set *context_lex_peek_basic_set(
1771 struct isl_context *context)
1773 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1776 return clex->tab->bset;
1779 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
1781 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1785 static void context_lex_extend(struct isl_context *context, int n)
1787 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1790 if (isl_tab_extend_cons(clex->tab, n) >= 0)
1792 isl_tab_free(clex->tab);
1796 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
1797 int check, int update)
1799 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1800 if (isl_tab_extend_cons(clex->tab, 2) < 0)
1802 clex->tab = add_lexmin_eq(clex->tab, eq);
1804 int v = tab_has_valid_sample(clex->tab, eq, 1);
1808 clex->tab = check_integer_feasible(clex->tab);
1811 clex->tab = check_samples(clex->tab, eq, 1);
1814 isl_tab_free(clex->tab);
1818 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
1819 int check, int update)
1821 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1822 if (isl_tab_extend_cons(clex->tab, 1) < 0)
1824 clex->tab = add_lexmin_ineq(clex->tab, ineq);
1826 int v = tab_has_valid_sample(clex->tab, ineq, 0);
1830 clex->tab = check_integer_feasible(clex->tab);
1833 clex->tab = check_samples(clex->tab, ineq, 0);
1836 isl_tab_free(clex->tab);
1840 /* Check which signs can be obtained by "ineq" on all the currently
1841 * active sample values. See row_sign for more information.
1843 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
1849 int res = isl_tab_row_unknown;
1851 isl_assert(tab->mat->ctx, tab->samples, return 0);
1852 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return 0);
1855 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1856 isl_seq_inner_product(tab->samples->row[i], ineq,
1857 1 + tab->n_var, &tmp);
1858 sgn = isl_int_sgn(tmp);
1859 if (sgn > 0 || (sgn == 0 && strict)) {
1860 if (res == isl_tab_row_unknown)
1861 res = isl_tab_row_pos;
1862 if (res == isl_tab_row_neg)
1863 res = isl_tab_row_any;
1866 if (res == isl_tab_row_unknown)
1867 res = isl_tab_row_neg;
1868 if (res == isl_tab_row_pos)
1869 res = isl_tab_row_any;
1871 if (res == isl_tab_row_any)
1879 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
1880 isl_int *ineq, int strict)
1882 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1883 return tab_ineq_sign(clex->tab, ineq, strict);
1886 /* Check whether "ineq" can be added to the tableau without rendering
1889 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
1891 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1892 struct isl_tab_undo *snap;
1898 if (isl_tab_extend_cons(clex->tab, 1) < 0)
1901 snap = isl_tab_snap(clex->tab);
1902 isl_tab_push_basis(clex->tab);
1903 clex->tab = add_lexmin_ineq(clex->tab, ineq);
1904 clex->tab = check_integer_feasible(clex->tab);
1907 feasible = !clex->tab->empty;
1908 if (isl_tab_rollback(clex->tab, snap) < 0)
1914 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
1915 struct isl_vec *div)
1917 return get_div(tab, context, div);
1920 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div,
1923 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1924 return context_tab_add_div(clex->tab, div, nonneg);
1927 static int context_lex_best_split(struct isl_context *context,
1928 struct isl_tab *tab)
1930 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1931 struct isl_tab_undo *snap;
1934 snap = isl_tab_snap(clex->tab);
1935 isl_tab_push_basis(clex->tab);
1936 r = best_split(tab, clex->tab);
1938 if (isl_tab_rollback(clex->tab, snap) < 0)
1944 static int context_lex_is_empty(struct isl_context *context)
1946 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1949 return clex->tab->empty;
1952 static void *context_lex_save(struct isl_context *context)
1954 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1955 struct isl_tab_undo *snap;
1957 snap = isl_tab_snap(clex->tab);
1958 isl_tab_push_basis(clex->tab);
1959 isl_tab_save_samples(clex->tab);
1964 static void context_lex_restore(struct isl_context *context, void *save)
1966 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1967 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
1968 isl_tab_free(clex->tab);
1973 static int context_lex_is_ok(struct isl_context *context)
1975 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1979 /* For each variable in the context tableau, check if the variable can
1980 * only attain non-negative values. If so, mark the parameter as non-negative
1981 * in the main tableau. This allows for a more direct identification of some
1982 * cases of violated constraints.
1984 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
1985 struct isl_tab *context_tab)
1988 struct isl_tab_undo *snap;
1989 struct isl_vec *ineq = NULL;
1990 struct isl_tab_var *var;
1993 if (context_tab->n_var == 0)
1996 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2000 if (isl_tab_extend_cons(context_tab, 1) < 0)
2003 snap = isl_tab_snap(context_tab);
2006 isl_seq_clr(ineq->el, ineq->size);
2007 for (i = 0; i < context_tab->n_var; ++i) {
2008 isl_int_set_si(ineq->el[1 + i], 1);
2009 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
2010 var = &context_tab->con[context_tab->n_con - 1];
2011 if (!context_tab->empty &&
2012 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2014 if (i >= tab->n_param)
2015 j = i - tab->n_param + tab->n_var - tab->n_div;
2016 tab->var[j].is_nonneg = 1;
2019 isl_int_set_si(ineq->el[1 + i], 0);
2020 if (isl_tab_rollback(context_tab, snap) < 0)
2024 if (context_tab->M && n == context_tab->n_var) {
2025 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2037 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2038 struct isl_context *context, struct isl_tab *tab)
2040 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2041 struct isl_tab_undo *snap;
2043 snap = isl_tab_snap(clex->tab);
2044 isl_tab_push_basis(clex->tab);
2046 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2048 if (isl_tab_rollback(clex->tab, snap) < 0)
2057 static void context_lex_invalidate(struct isl_context *context)
2059 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2060 isl_tab_free(clex->tab);
2064 static void context_lex_free(struct isl_context *context)
2066 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2067 isl_tab_free(clex->tab);
2071 struct isl_context_op isl_context_lex_op = {
2072 context_lex_detect_nonnegative_parameters,
2073 context_lex_peek_basic_set,
2074 context_lex_peek_tab,
2076 context_lex_add_ineq,
2077 context_lex_ineq_sign,
2078 context_lex_test_ineq,
2079 context_lex_get_div,
2080 context_lex_add_div,
2081 context_lex_best_split,
2082 context_lex_is_empty,
2085 context_lex_restore,
2086 context_lex_invalidate,
2090 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2092 struct isl_tab *tab;
2094 bset = isl_basic_set_cow(bset);
2097 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2101 tab = isl_tab_init_samples(tab);
2104 isl_basic_set_free(bset);
2108 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2110 struct isl_context_lex *clex;
2115 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2119 clex->context.op = &isl_context_lex_op;
2121 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2122 clex->tab = restore_lexmin(clex->tab);
2123 clex->tab = check_integer_feasible(clex->tab);
2127 return &clex->context;
2129 clex->context.op->free(&clex->context);
2133 struct isl_context_gbr {
2134 struct isl_context context;
2135 struct isl_tab *tab;
2136 struct isl_tab *shifted;
2139 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2140 struct isl_context *context, struct isl_tab *tab)
2142 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2143 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2146 static struct isl_basic_set *context_gbr_peek_basic_set(
2147 struct isl_context *context)
2149 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2152 return cgbr->tab->bset;
2155 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2157 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2161 /* Initialize the "shifted" tableau of the context, which
2162 * contains the constraints of the original tableau shifted
2163 * by the sum of all negative coefficients. This ensures
2164 * that any rational point in the shifted tableau can
2165 * be rounded up to yield an integer point in the original tableau.
2167 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2170 struct isl_vec *cst;
2171 struct isl_basic_set *bset = cgbr->tab->bset;
2172 unsigned dim = isl_basic_set_total_dim(bset);
2174 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2178 for (i = 0; i < bset->n_ineq; ++i) {
2179 isl_int_set(cst->el[i], bset->ineq[i][0]);
2180 for (j = 0; j < dim; ++j) {
2181 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2183 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2184 bset->ineq[i][1 + j]);
2188 cgbr->shifted = isl_tab_from_basic_set(bset);
2190 for (i = 0; i < bset->n_ineq; ++i)
2191 isl_int_set(bset->ineq[i][0], cst->el[i]);
2196 /* Check if the shifted tableau is non-empty, and if so
2197 * use the sample point to construct an integer point
2198 * of the context tableau.
2200 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2202 struct isl_vec *sample;
2205 gbr_init_shifted(cgbr);
2208 if (cgbr->shifted->empty)
2209 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2211 sample = isl_tab_get_sample_value(cgbr->shifted);
2212 sample = isl_vec_ceil(sample);
2217 static int use_shifted(struct isl_context_gbr *cgbr)
2219 return cgbr->tab->bset->n_eq == 0 && cgbr->tab->bset->n_div == 0;
2222 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2224 struct isl_basic_set *bset;
2226 if (isl_tab_sample_is_integer(cgbr->tab))
2227 return isl_tab_get_sample_value(cgbr->tab);
2229 if (use_shifted(cgbr)) {
2230 struct isl_vec *sample;
2232 sample = gbr_get_shifted_sample(cgbr);
2233 if (!sample || sample->size > 0)
2236 isl_vec_free(sample);
2239 bset = isl_basic_set_underlying_set(isl_basic_set_copy(cgbr->tab->bset));
2240 return isl_basic_set_sample_vec(bset);
2243 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2245 struct isl_vec *sample;
2250 if (cgbr->tab->empty)
2253 sample = gbr_get_sample(cgbr);
2257 if (sample->size == 0) {
2258 isl_vec_free(sample);
2259 cgbr->tab = isl_tab_mark_empty(cgbr->tab);
2263 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2267 isl_tab_free(cgbr->tab);
2271 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2278 if (isl_tab_extend_cons(tab, 2) < 0)
2281 tab = isl_tab_add_eq(tab, eq);
2289 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2290 int check, int update)
2292 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2294 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2297 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2301 check_gbr_integer_feasible(cgbr);
2304 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2307 isl_tab_free(cgbr->tab);
2311 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2316 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2319 cgbr->tab = isl_tab_add_ineq(cgbr->tab, ineq);
2321 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2324 dim = isl_basic_set_total_dim(cgbr->tab->bset);
2326 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2329 for (i = 0; i < dim; ++i) {
2330 if (!isl_int_is_neg(ineq[1 + i]))
2332 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2335 cgbr->shifted = isl_tab_add_ineq(cgbr->shifted, ineq);
2337 for (i = 0; i < dim; ++i) {
2338 if (!isl_int_is_neg(ineq[1 + i]))
2340 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2346 isl_tab_free(cgbr->tab);
2350 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2351 int check, int update)
2353 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2355 add_gbr_ineq(cgbr, ineq);
2360 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2364 check_gbr_integer_feasible(cgbr);
2367 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2370 isl_tab_free(cgbr->tab);
2374 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2375 isl_int *ineq, int strict)
2377 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2378 return tab_ineq_sign(cgbr->tab, ineq, strict);
2381 /* Check whether "ineq" can be added to the tableau without rendering
2384 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2386 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2387 struct isl_tab_undo *snap;
2388 struct isl_tab_undo *shifted_snap = NULL;
2394 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2397 snap = isl_tab_snap(cgbr->tab);
2399 shifted_snap = isl_tab_snap(cgbr->shifted);
2400 add_gbr_ineq(cgbr, ineq);
2401 check_gbr_integer_feasible(cgbr);
2404 feasible = !cgbr->tab->empty;
2405 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2408 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2410 } else if (cgbr->shifted) {
2411 isl_tab_free(cgbr->shifted);
2412 cgbr->shifted = NULL;
2418 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
2419 struct isl_vec *div)
2421 return get_div(tab, context, div);
2424 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div,
2427 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2428 return context_tab_add_div(cgbr->tab, div, nonneg);
2431 static int context_gbr_best_split(struct isl_context *context,
2432 struct isl_tab *tab)
2434 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2435 struct isl_tab_undo *snap;
2438 snap = isl_tab_snap(cgbr->tab);
2439 r = best_split(tab, cgbr->tab);
2441 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2447 static int context_gbr_is_empty(struct isl_context *context)
2449 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2452 return cgbr->tab->empty;
2455 struct isl_gbr_tab_undo {
2456 struct isl_tab_undo *tab_snap;
2457 struct isl_tab_undo *shifted_snap;
2460 static void *context_gbr_save(struct isl_context *context)
2462 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2463 struct isl_gbr_tab_undo *snap;
2465 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
2469 snap->tab_snap = isl_tab_snap(cgbr->tab);
2470 isl_tab_save_samples(cgbr->tab);
2473 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
2475 snap->shifted_snap = NULL;
2480 static void context_gbr_restore(struct isl_context *context, void *save)
2482 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2483 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
2484 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
2485 isl_tab_free(cgbr->tab);
2488 if (snap->shifted_snap)
2489 isl_tab_rollback(cgbr->shifted, snap->shifted_snap);
2490 else if (cgbr->shifted) {
2491 isl_tab_free(cgbr->shifted);
2492 cgbr->shifted = NULL;
2497 static int context_gbr_is_ok(struct isl_context *context)
2499 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2503 static void context_gbr_invalidate(struct isl_context *context)
2505 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2506 isl_tab_free(cgbr->tab);
2510 static void context_gbr_free(struct isl_context *context)
2512 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2513 isl_tab_free(cgbr->tab);
2514 isl_tab_free(cgbr->shifted);
2518 struct isl_context_op isl_context_gbr_op = {
2519 context_gbr_detect_nonnegative_parameters,
2520 context_gbr_peek_basic_set,
2521 context_gbr_peek_tab,
2523 context_gbr_add_ineq,
2524 context_gbr_ineq_sign,
2525 context_gbr_test_ineq,
2526 context_gbr_get_div,
2527 context_gbr_add_div,
2528 context_gbr_best_split,
2529 context_gbr_is_empty,
2532 context_gbr_restore,
2533 context_gbr_invalidate,
2537 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
2539 struct isl_context_gbr *cgbr;
2544 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
2548 cgbr->context.op = &isl_context_gbr_op;
2550 cgbr->shifted = NULL;
2551 cgbr->tab = isl_tab_from_basic_set(dom);
2552 cgbr->tab = isl_tab_init_samples(cgbr->tab);
2555 cgbr->tab->bset = isl_basic_set_cow(isl_basic_set_copy(dom));
2556 if (!cgbr->tab->bset)
2558 check_gbr_integer_feasible(cgbr);
2560 return &cgbr->context;
2562 cgbr->context.op->free(&cgbr->context);
2566 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
2571 if (dom->ctx->context == ISL_CONTEXT_LEXMIN)
2572 return isl_context_lex_alloc(dom);
2574 return isl_context_gbr_alloc(dom);
2577 /* Construct an isl_sol_map structure for accumulating the solution.
2578 * If track_empty is set, then we also keep track of the parts
2579 * of the context where there is no solution.
2580 * If max is set, then we are solving a maximization, rather than
2581 * a minimization problem, which means that the variables in the
2582 * tableau have value "M - x" rather than "M + x".
2584 static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
2585 struct isl_basic_set *dom, int track_empty, int max)
2587 struct isl_sol_map *sol_map;
2589 sol_map = isl_calloc_type(bset->ctx, struct isl_sol_map);
2594 sol_map->sol.add = &sol_map_add_wrap;
2595 sol_map->sol.free = &sol_map_free_wrap;
2596 sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
2601 sol_map->sol.context = isl_context_alloc(dom);
2602 if (!sol_map->sol.context)
2606 sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
2607 1, ISL_SET_DISJOINT);
2608 if (!sol_map->empty)
2612 isl_basic_set_free(dom);
2615 isl_basic_set_free(dom);
2616 sol_map_free(sol_map);
2620 /* Check whether all coefficients of (non-parameter) variables
2621 * are non-positive, meaning that no pivots can be performed on the row.
2623 static int is_critical(struct isl_tab *tab, int row)
2626 unsigned off = 2 + tab->M;
2628 for (j = tab->n_dead; j < tab->n_col; ++j) {
2629 if (tab->col_var[j] >= 0 &&
2630 (tab->col_var[j] < tab->n_param ||
2631 tab->col_var[j] >= tab->n_var - tab->n_div))
2634 if (isl_int_is_pos(tab->mat->row[row][off + j]))
2641 /* Check whether the inequality represented by vec is strict over the integers,
2642 * i.e., there are no integer values satisfying the constraint with
2643 * equality. This happens if the gcd of the coefficients is not a divisor
2644 * of the constant term. If so, scale the constraint down by the gcd
2645 * of the coefficients.
2647 static int is_strict(struct isl_vec *vec)
2653 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
2654 if (!isl_int_is_one(gcd)) {
2655 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
2656 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
2657 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
2664 /* Determine the sign of the given row of the main tableau.
2665 * The result is one of
2666 * isl_tab_row_pos: always non-negative; no pivot needed
2667 * isl_tab_row_neg: always non-positive; pivot
2668 * isl_tab_row_any: can be both positive and negative; split
2670 * We first handle some simple cases
2671 * - the row sign may be known already
2672 * - the row may be obviously non-negative
2673 * - the parametric constant may be equal to that of another row
2674 * for which we know the sign. This sign will be either "pos" or
2675 * "any". If it had been "neg" then we would have pivoted before.
2677 * If none of these cases hold, we check the value of the row for each
2678 * of the currently active samples. Based on the signs of these values
2679 * we make an initial determination of the sign of the row.
2681 * all zero -> unk(nown)
2682 * all non-negative -> pos
2683 * all non-positive -> neg
2684 * both negative and positive -> all
2686 * If we end up with "all", we are done.
2687 * Otherwise, we perform a check for positive and/or negative
2688 * values as follows.
2690 * samples neg unk pos
2696 * There is no special sign for "zero", because we can usually treat zero
2697 * as either non-negative or non-positive, whatever works out best.
2698 * However, if the row is "critical", meaning that pivoting is impossible
2699 * then we don't want to limp zero with the non-positive case, because
2700 * then we we would lose the solution for those values of the parameters
2701 * where the value of the row is zero. Instead, we treat 0 as non-negative
2702 * ensuring a split if the row can attain both zero and negative values.
2703 * The same happens when the original constraint was one that could not
2704 * be satisfied with equality by any integer values of the parameters.
2705 * In this case, we normalize the constraint, but then a value of zero
2706 * for the normalized constraint is actually a positive value for the
2707 * original constraint, so again we need to treat zero as non-negative.
2708 * In both these cases, we have the following decision tree instead:
2710 * all non-negative -> pos
2711 * all negative -> neg
2712 * both negative and non-negative -> all
2720 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
2721 struct isl_sol *sol, int row)
2723 struct isl_vec *ineq = NULL;
2724 int res = isl_tab_row_unknown;
2729 if (tab->row_sign[row] != isl_tab_row_unknown)
2730 return tab->row_sign[row];
2731 if (is_obviously_nonneg(tab, row))
2732 return isl_tab_row_pos;
2733 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
2734 if (tab->row_sign[row2] == isl_tab_row_unknown)
2736 if (identical_parameter_line(tab, row, row2))
2737 return tab->row_sign[row2];
2740 critical = is_critical(tab, row);
2742 ineq = get_row_parameter_ineq(tab, row);
2746 strict = is_strict(ineq);
2748 res = sol->context->op->ineq_sign(sol->context, ineq->el,
2749 critical || strict);
2751 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
2752 /* test for negative values */
2754 isl_seq_neg(ineq->el, ineq->el, ineq->size);
2755 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2757 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
2761 res = isl_tab_row_pos;
2763 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
2765 if (res == isl_tab_row_neg) {
2766 isl_seq_neg(ineq->el, ineq->el, ineq->size);
2767 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2771 if (res == isl_tab_row_neg) {
2772 /* test for positive values */
2774 if (!critical && !strict)
2775 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2777 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
2781 res = isl_tab_row_any;
2791 static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab);
2793 /* Find solutions for values of the parameters that satisfy the given
2796 * We currently take a snapshot of the context tableau that is reset
2797 * when we return from this function, while we make a copy of the main
2798 * tableau, leaving the original main tableau untouched.
2799 * These are fairly arbitrary choices. Making a copy also of the context
2800 * tableau would obviate the need to undo any changes made to it later,
2801 * while taking a snapshot of the main tableau could reduce memory usage.
2802 * If we were to switch to taking a snapshot of the main tableau,
2803 * we would have to keep in mind that we need to save the row signs
2804 * and that we need to do this before saving the current basis
2805 * such that the basis has been restore before we restore the row signs.
2807 static struct isl_sol *find_in_pos(struct isl_sol *sol,
2808 struct isl_tab *tab, isl_int *ineq)
2814 saved = sol->context->op->save(sol->context);
2816 tab = isl_tab_dup(tab);
2820 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
2822 sol = find_solutions(sol, tab);
2824 sol->context->op->restore(sol->context, saved);
2831 /* Record the absence of solutions for those values of the parameters
2832 * that do not satisfy the given inequality with equality.
2834 static struct isl_sol *no_sol_in_strict(struct isl_sol *sol,
2835 struct isl_tab *tab, struct isl_vec *ineq)
2842 saved = sol->context->op->save(sol->context);
2844 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2846 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
2852 sol = sol->add(sol, tab);
2855 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
2857 sol->context->op->restore(sol->context, saved);
2864 /* Compute the lexicographic minimum of the set represented by the main
2865 * tableau "tab" within the context "sol->context_tab".
2866 * On entry the sample value of the main tableau is lexicographically
2867 * less than or equal to this lexicographic minimum.
2868 * Pivots are performed until a feasible point is found, which is then
2869 * necessarily equal to the minimum, or until the tableau is found to
2870 * be infeasible. Some pivots may need to be performed for only some
2871 * feasible values of the context tableau. If so, the context tableau
2872 * is split into a part where the pivot is needed and a part where it is not.
2874 * Whenever we enter the main loop, the main tableau is such that no
2875 * "obvious" pivots need to be performed on it, where "obvious" means
2876 * that the given row can be seen to be negative without looking at
2877 * the context tableau. In particular, for non-parametric problems,
2878 * no pivots need to be performed on the main tableau.
2879 * The caller of find_solutions is responsible for making this property
2880 * hold prior to the first iteration of the loop, while restore_lexmin
2881 * is called before every other iteration.
2883 * Inside the main loop, we first examine the signs of the rows of
2884 * the main tableau within the context of the context tableau.
2885 * If we find a row that is always non-positive for all values of
2886 * the parameters satisfying the context tableau and negative for at
2887 * least one value of the parameters, we perform the appropriate pivot
2888 * and start over. An exception is the case where no pivot can be
2889 * performed on the row. In this case, we require that the sign of
2890 * the row is negative for all values of the parameters (rather than just
2891 * non-positive). This special case is handled inside row_sign, which
2892 * will say that the row can have any sign if it determines that it can
2893 * attain both negative and zero values.
2895 * If we can't find a row that always requires a pivot, but we can find
2896 * one or more rows that require a pivot for some values of the parameters
2897 * (i.e., the row can attain both positive and negative signs), then we split
2898 * the context tableau into two parts, one where we force the sign to be
2899 * non-negative and one where we force is to be negative.
2900 * The non-negative part is handled by a recursive call (through find_in_pos).
2901 * Upon returning from this call, we continue with the negative part and
2902 * perform the required pivot.
2904 * If no such rows can be found, all rows are non-negative and we have
2905 * found a (rational) feasible point. If we only wanted a rational point
2907 * Otherwise, we check if all values of the sample point of the tableau
2908 * are integral for the variables. If so, we have found the minimal
2909 * integral point and we are done.
2910 * If the sample point is not integral, then we need to make a distinction
2911 * based on whether the constant term is non-integral or the coefficients
2912 * of the parameters. Furthermore, in order to decide how to handle
2913 * the non-integrality, we also need to know whether the coefficients
2914 * of the other columns in the tableau are integral. This leads
2915 * to the following table. The first two rows do not correspond
2916 * to a non-integral sample point and are only mentioned for completeness.
2918 * constant parameters other
2921 * int int rat | -> no problem
2923 * rat int int -> fail
2925 * rat int rat -> cut
2928 * rat rat rat | -> parametric cut
2931 * rat rat int | -> split context
2933 * If the parametric constant is completely integral, then there is nothing
2934 * to be done. If the constant term is non-integral, but all the other
2935 * coefficient are integral, then there is nothing that can be done
2936 * and the tableau has no integral solution.
2937 * If, on the other hand, one or more of the other columns have rational
2938 * coeffcients, but the parameter coefficients are all integral, then
2939 * we can perform a regular (non-parametric) cut.
2940 * Finally, if there is any parameter coefficient that is non-integral,
2941 * then we need to involve the context tableau. There are two cases here.
2942 * If at least one other column has a rational coefficient, then we
2943 * can perform a parametric cut in the main tableau by adding a new
2944 * integer division in the context tableau.
2945 * If all other columns have integral coefficients, then we need to
2946 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
2947 * is always integral. We do this by introducing an integer division
2948 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
2949 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
2950 * Since q is expressed in the tableau as
2951 * c + \sum a_i y_i - m q >= 0
2952 * -c - \sum a_i y_i + m q + m - 1 >= 0
2953 * it is sufficient to add the inequality
2954 * -c - \sum a_i y_i + m q >= 0
2955 * In the part of the context where this inequality does not hold, the
2956 * main tableau is marked as being empty.
2958 static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab)
2960 struct isl_context *context;
2965 context = sol->context;
2969 if (context->op->is_empty(context))
2972 for (; tab && !tab->empty; tab = restore_lexmin(tab)) {
2979 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2980 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2982 sgn = row_sign(tab, sol, row);
2985 tab->row_sign[row] = sgn;
2986 if (sgn == isl_tab_row_any)
2988 if (sgn == isl_tab_row_any && split == -1)
2990 if (sgn == isl_tab_row_neg)
2993 if (row < tab->n_row)
2996 struct isl_vec *ineq;
2998 split = context->op->best_split(context, tab);
3001 ineq = get_row_parameter_ineq(tab, split);
3005 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3006 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3008 if (tab->row_sign[row] == isl_tab_row_any)
3009 tab->row_sign[row] = isl_tab_row_unknown;
3011 tab->row_sign[split] = isl_tab_row_pos;
3012 sol = find_in_pos(sol, tab, ineq->el);
3013 tab->row_sign[split] = isl_tab_row_neg;
3015 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3016 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3017 context->op->add_ineq(context, ineq->el, 0, 1);
3025 row = first_non_integer(tab, &flags);
3028 if (ISL_FL_ISSET(flags, I_PAR)) {
3029 if (ISL_FL_ISSET(flags, I_VAR)) {
3030 tab = isl_tab_mark_empty(tab);
3033 row = add_cut(tab, row);
3034 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3035 struct isl_vec *div;
3036 struct isl_vec *ineq;
3038 div = get_row_split_div(tab, row);
3041 d = context->op->get_div(context, tab, div);
3045 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3046 sol = no_sol_in_strict(sol, tab, ineq);
3047 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3048 context->op->add_ineq(context, ineq->el, 1, 1);
3050 if (!sol || !context->op->is_ok(context))
3052 tab = set_row_cst_to_div(tab, row, d);
3054 row = add_parametric_cut(tab, row, context);
3059 sol = sol->add(sol, tab);
3068 /* Compute the lexicographic minimum of the set represented by the main
3069 * tableau "tab" within the context "sol->context_tab".
3071 * As a preprocessing step, we first transfer all the purely parametric
3072 * equalities from the main tableau to the context tableau, i.e.,
3073 * parameters that have been pivoted to a row.
3074 * These equalities are ignored by the main algorithm, because the
3075 * corresponding rows may not be marked as being non-negative.
3076 * In parts of the context where the added equality does not hold,
3077 * the main tableau is marked as being empty.
3079 static struct isl_sol *find_solutions_main(struct isl_sol *sol,
3080 struct isl_tab *tab)
3084 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3088 if (tab->row_var[row] < 0)
3090 if (tab->row_var[row] >= tab->n_param &&
3091 tab->row_var[row] < tab->n_var - tab->n_div)
3093 if (tab->row_var[row] < tab->n_param)
3094 p = tab->row_var[row];
3096 p = tab->row_var[row]
3097 + tab->n_param - (tab->n_var - tab->n_div);
3099 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3100 get_row_parameter_line(tab, row, eq->el);
3101 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3102 eq = isl_vec_normalize(eq);
3104 sol = no_sol_in_strict(sol, tab, eq);
3106 isl_seq_neg(eq->el, eq->el, eq->size);
3107 sol = no_sol_in_strict(sol, tab, eq);
3108 isl_seq_neg(eq->el, eq->el, eq->size);
3110 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3114 isl_tab_mark_redundant(tab, row);
3116 if (sol->context->op->is_empty(sol->context))
3119 row = tab->n_redundant - 1;
3122 return find_solutions(sol, tab);
3129 static struct isl_sol_map *sol_map_find_solutions(struct isl_sol_map *sol_map,
3130 struct isl_tab *tab)
3132 return (struct isl_sol_map *)find_solutions_main(&sol_map->sol, tab);
3135 /* Check if integer division "div" of "dom" also occurs in "bmap".
3136 * If so, return its position within the divs.
3137 * If not, return -1.
3139 static int find_context_div(struct isl_basic_map *bmap,
3140 struct isl_basic_set *dom, unsigned div)
3143 unsigned b_dim = isl_dim_total(bmap->dim);
3144 unsigned d_dim = isl_dim_total(dom->dim);
3146 if (isl_int_is_zero(dom->div[div][0]))
3148 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3151 for (i = 0; i < bmap->n_div; ++i) {
3152 if (isl_int_is_zero(bmap->div[i][0]))
3154 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3155 (b_dim - d_dim) + bmap->n_div) != -1)
3157 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3163 /* The correspondence between the variables in the main tableau,
3164 * the context tableau, and the input map and domain is as follows.
3165 * The first n_param and the last n_div variables of the main tableau
3166 * form the variables of the context tableau.
3167 * In the basic map, these n_param variables correspond to the
3168 * parameters and the input dimensions. In the domain, they correspond
3169 * to the parameters and the set dimensions.
3170 * The n_div variables correspond to the integer divisions in the domain.
3171 * To ensure that everything lines up, we may need to copy some of the
3172 * integer divisions of the domain to the map. These have to be placed
3173 * in the same order as those in the context and they have to be placed
3174 * after any other integer divisions that the map may have.
3175 * This function performs the required reordering.
3177 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3178 struct isl_basic_set *dom)
3184 for (i = 0; i < dom->n_div; ++i)
3185 if (find_context_div(bmap, dom, i) != -1)
3187 other = bmap->n_div - common;
3188 if (dom->n_div - common > 0) {
3189 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
3190 dom->n_div - common, 0, 0);
3194 for (i = 0; i < dom->n_div; ++i) {
3195 int pos = find_context_div(bmap, dom, i);
3197 pos = isl_basic_map_alloc_div(bmap);
3200 isl_int_set_si(bmap->div[pos][0], 0);
3202 if (pos != other + i)
3203 isl_basic_map_swap_div(bmap, pos, other + i);
3207 isl_basic_map_free(bmap);
3211 /* Compute the lexicographic minimum (or maximum if "max" is set)
3212 * of "bmap" over the domain "dom" and return the result as a map.
3213 * If "empty" is not NULL, then *empty is assigned a set that
3214 * contains those parts of the domain where there is no solution.
3215 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
3216 * then we compute the rational optimum. Otherwise, we compute
3217 * the integral optimum.
3219 * We perform some preprocessing. As the PILP solver does not
3220 * handle implicit equalities very well, we first make sure all
3221 * the equalities are explicitly available.
3222 * We also make sure the divs in the domain are properly order,
3223 * because they will be added one by one in the given order
3224 * during the construction of the solution map.
3226 struct isl_map *isl_tab_basic_map_partial_lexopt(
3227 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3228 struct isl_set **empty, int max)
3230 struct isl_tab *tab;
3231 struct isl_map *result = NULL;
3232 struct isl_sol_map *sol_map = NULL;
3233 struct isl_context *context;
3240 isl_assert(bmap->ctx,
3241 isl_basic_map_compatible_domain(bmap, dom), goto error);
3243 bmap = isl_basic_map_detect_equalities(bmap);
3246 dom = isl_basic_set_order_divs(dom);
3247 bmap = align_context_divs(bmap, dom);
3249 sol_map = sol_map_init(bmap, dom, !!empty, max);
3253 context = sol_map->sol.context;
3254 if (isl_basic_set_fast_is_empty(context->op->peek_basic_set(context)))
3256 else if (isl_basic_map_fast_is_empty(bmap))
3257 sol_map = add_empty(sol_map);
3259 tab = tab_for_lexmin(bmap,
3260 context->op->peek_basic_set(context), 1, max);
3261 tab = context->op->detect_nonnegative_parameters(context, tab);
3262 sol_map = sol_map_find_solutions(sol_map, tab);
3267 result = isl_map_copy(sol_map->map);
3269 *empty = isl_set_copy(sol_map->empty);
3270 sol_map_free(sol_map);
3271 isl_basic_map_free(bmap);
3274 sol_map_free(sol_map);
3275 isl_basic_map_free(bmap);
3279 struct isl_sol_for {
3281 int (*fn)(__isl_take isl_basic_set *dom,
3282 __isl_take isl_mat *map, void *user);
3287 static void sol_for_free(struct isl_sol_for *sol_for)
3289 if (sol_for->sol.context)
3290 sol_for->sol.context->op->free(sol_for->sol.context);
3294 static void sol_for_free_wrap(struct isl_sol *sol)
3296 sol_for_free((struct isl_sol_for *)sol);
3299 /* Add the solution identified by the tableau and the context tableau.
3301 * See documentation of sol_map_add for more details.
3303 * Instead of constructing a basic map, this function calls a user
3304 * defined function with the current context as a basic set and
3305 * an affine matrix reprenting the relation between the input and output.
3306 * The number of rows in this matrix is equal to one plus the number
3307 * of output variables. The number of columns is equal to one plus
3308 * the total dimension of the context, i.e., the number of parameters,
3309 * input variables and divs. Since some of the columns in the matrix
3310 * may refer to the divs, the basic set is not simplified.
3311 * (Simplification may reorder or remove divs.)
3313 static struct isl_sol_for *sol_for_add(struct isl_sol_for *sol,
3314 struct isl_tab *tab)
3316 struct isl_basic_set *bset;
3317 struct isl_mat *mat = NULL;
3330 n_out = tab->n_var - tab->n_param - tab->n_div;
3331 mat = isl_mat_alloc(tab->mat->ctx, 1 + n_out, 1 + tab->n_param + tab->n_div);
3335 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
3336 isl_int_set_si(mat->row[0][0], 1);
3337 for (row = 0; row < n_out; ++row) {
3338 int i = tab->n_param + row;
3341 isl_seq_clr(mat->row[1 + row], mat->n_col);
3342 if (!tab->var[i].is_row)
3345 r = tab->var[i].index;
3348 isl_assert(mat->ctx, isl_int_eq(tab->mat->row[r][2],
3349 tab->mat->row[r][0]),
3351 isl_int_set(mat->row[1 + row][0], tab->mat->row[r][1]);
3352 for (j = 0; j < tab->n_param; ++j) {
3354 if (tab->var[j].is_row)
3356 col = tab->var[j].index;
3357 isl_int_set(mat->row[1 + row][1 + j],
3358 tab->mat->row[r][off + col]);
3360 for (j = 0; j < tab->n_div; ++j) {
3362 if (tab->var[tab->n_var - tab->n_div+j].is_row)
3364 col = tab->var[tab->n_var - tab->n_div+j].index;
3365 isl_int_set(mat->row[1 + row][1 + tab->n_param + j],
3366 tab->mat->row[r][off + col]);
3368 if (!isl_int_is_one(tab->mat->row[r][0]))
3369 isl_seq_scale_down(mat->row[1 + row], mat->row[1 + row],
3370 tab->mat->row[r][0], mat->n_col);
3372 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
3376 bset = sol->sol.context->op->peek_basic_set(sol->sol.context);
3377 bset = isl_basic_set_dup(bset);
3378 bset = isl_basic_set_finalize(bset);
3380 if (sol->fn(bset, isl_mat_copy(mat), sol->user) < 0)
3387 sol_free(&sol->sol);
3391 static struct isl_sol *sol_for_add_wrap(struct isl_sol *sol,
3392 struct isl_tab *tab)
3394 return (struct isl_sol *)sol_for_add((struct isl_sol_for *)sol, tab);
3397 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
3398 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3402 struct isl_sol_for *sol_for = NULL;
3403 struct isl_dim *dom_dim;
3404 struct isl_basic_set *dom = NULL;
3406 sol_for = isl_calloc_type(bset->ctx, struct isl_sol_for);
3410 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3411 dom = isl_basic_set_universe(dom_dim);
3414 sol_for->user = user;
3416 sol_for->sol.add = &sol_for_add_wrap;
3417 sol_for->sol.free = &sol_for_free_wrap;
3419 sol_for->sol.context = isl_context_alloc(dom);
3420 if (!sol_for->sol.context)
3423 isl_basic_set_free(dom);
3426 isl_basic_set_free(dom);
3427 sol_for_free(sol_for);
3431 static struct isl_sol_for *sol_for_find_solutions(struct isl_sol_for *sol_for,
3432 struct isl_tab *tab)
3434 return (struct isl_sol_for *)find_solutions_main(&sol_for->sol, tab);
3437 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
3438 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3442 struct isl_sol_for *sol_for = NULL;
3444 bmap = isl_basic_map_copy(bmap);
3448 bmap = isl_basic_map_detect_equalities(bmap);
3449 sol_for = sol_for_init(bmap, max, fn, user);
3451 if (isl_basic_map_fast_is_empty(bmap))
3454 struct isl_tab *tab;
3455 struct isl_context *context = sol_for->sol.context;
3456 tab = tab_for_lexmin(bmap,
3457 context->op->peek_basic_set(context), 1, max);
3458 tab = context->op->detect_nonnegative_parameters(context, tab);
3459 sol_for = sol_for_find_solutions(sol_for, tab);
3464 sol_for_free(sol_for);
3465 isl_basic_map_free(bmap);
3468 sol_for_free(sol_for);
3469 isl_basic_map_free(bmap);
3473 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map *bmap,
3474 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3478 return isl_basic_map_foreach_lexopt(bmap, 0, fn, user);
3481 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map *bmap,
3482 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3486 return isl_basic_map_foreach_lexopt(bmap, 1, fn, user);