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 int (*detect_equalities)(struct isl_context *context,
79 /* return row index of "best" split */
80 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
81 /* check if context has already been determined to be empty */
82 int (*is_empty)(struct isl_context *context);
83 /* check if context is still usable */
84 int (*is_ok)(struct isl_context *context);
85 /* save a copy/snapshot of context */
86 void *(*save)(struct isl_context *context);
87 /* restore saved context */
88 void (*restore)(struct isl_context *context, void *);
89 /* invalidate context */
90 void (*invalidate)(struct isl_context *context);
92 void (*free)(struct isl_context *context);
96 struct isl_context_op *op;
99 struct isl_context_lex {
100 struct isl_context context;
104 /* isl_sol is an interface for constructing a solution to
105 * a parametric integer linear programming problem.
106 * Every time the algorithm reaches a state where a solution
107 * can be read off from the tableau (including cases where the tableau
108 * is empty), the function "add" is called on the isl_sol passed
109 * to find_solutions_main.
111 * The context tableau is owned by isl_sol and is updated incrementally.
113 * There are currently two implementations of this interface,
114 * isl_sol_map, which simply collects the solutions in an isl_map
115 * and (optionally) the parts of the context where there is no solution
117 * isl_sol_for, which calls a user-defined function for each part of
124 struct isl_context *context;
125 void (*add)(struct isl_sol *sol,
126 struct isl_basic_set *dom, struct isl_mat *M);
127 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
128 void (*free)(struct isl_sol *sol);
131 static void sol_free(struct isl_sol *sol)
138 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
142 if (isl_int_is_one(m))
145 for (i = 0; i < n_row; ++i)
146 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
149 /* Add the solution identified by the tableau and the context tableau.
151 * The layout of the variables is as follows.
152 * tab->n_var is equal to the total number of variables in the input
153 * map (including divs that were copied from the context)
154 * + the number of extra divs constructed
155 * Of these, the first tab->n_param and the last tab->n_div variables
156 * correspond to the variables in the context, i.e.,
157 * tab->n_param + tab->n_div = context_tab->n_var
158 * tab->n_param is equal to the number of parameters and input
159 * dimensions in the input map
160 * tab->n_div is equal to the number of divs in the context
162 * If there is no solution, then call add_empty with a basic set
163 * that corresponds to the context tableau. (If add_empty is NULL,
166 * If there is a solution, then first construct a matrix that maps
167 * all dimensions of the context to the output variables, i.e.,
168 * the output dimensions in the input map.
169 * The divs in the input map (if any) that do not correspond to any
170 * div in the context do not appear in the solution.
171 * The algorithm will make sure that they have an integer value,
172 * but these values themselves are of no interest.
173 * We have to be careful not to drop or rearrange any divs in the
174 * context because that would change the meaning of the matrix.
176 * To extract the value of the output variables, it should be noted
177 * that we always use a big parameter M in the main tableau and so
178 * the variable stored in this tableau is not an output variable x itself, but
179 * x' = M + x (in case of minimization)
181 * x' = M - x (in case of maximization)
182 * If x' appears in a column, then its optimal value is zero,
183 * which means that the optimal value of x is an unbounded number
184 * (-M for minimization and M for maximization).
185 * We currently assume that the output dimensions in the original map
186 * are bounded, so this cannot occur.
187 * Similarly, when x' appears in a row, then the coefficient of M in that
188 * row is necessarily 1.
189 * If the row in the tableau represents
190 * d x' = c + d M + e(y)
191 * then, in case of minimization, the corresponding row in the matrix
194 * with a d = m, the (updated) common denominator of the matrix.
195 * In case of maximization, the row will be
198 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
200 struct isl_basic_set *bset = NULL;
201 struct isl_mat *mat = NULL;
206 if (sol->error || !tab)
209 if (tab->empty && !sol->add_empty)
212 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
213 bset = isl_basic_set_update_from_tab(bset,
214 sol->context->op->peek_tab(sol->context));
216 ISL_F_SET(bset, ISL_BASIC_SET_RATIONAL);
219 sol->add_empty(sol, bset);
225 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
226 1 + tab->n_param + tab->n_div);
232 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
233 isl_int_set_si(mat->row[0][0], 1);
234 for (row = 0; row < sol->n_out; ++row) {
235 int i = tab->n_param + row;
238 isl_seq_clr(mat->row[1 + row], mat->n_col);
239 if (!tab->var[i].is_row) {
241 isl_assert(mat->ctx, !tab->M, goto error2);
245 r = tab->var[i].index;
248 isl_assert(mat->ctx, isl_int_eq(tab->mat->row[r][2],
249 tab->mat->row[r][0]),
251 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
252 isl_int_divexact(m, tab->mat->row[r][0], m);
253 scale_rows(mat, m, 1 + row);
254 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
255 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
256 for (j = 0; j < tab->n_param; ++j) {
258 if (tab->var[j].is_row)
260 col = tab->var[j].index;
261 isl_int_mul(mat->row[1 + row][1 + j], m,
262 tab->mat->row[r][off + col]);
264 for (j = 0; j < tab->n_div; ++j) {
266 if (tab->var[tab->n_var - tab->n_div+j].is_row)
268 col = tab->var[tab->n_var - tab->n_div+j].index;
269 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
270 tab->mat->row[r][off + col]);
273 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
279 sol->add(sol, bset, mat);
284 isl_basic_set_free(bset);
292 struct isl_set *empty;
295 static void sol_map_free(struct isl_sol_map *sol_map)
297 if (sol_map->sol.context)
298 sol_map->sol.context->op->free(sol_map->sol.context);
299 isl_map_free(sol_map->map);
300 isl_set_free(sol_map->empty);
304 static void sol_map_free_wrap(struct isl_sol *sol)
306 sol_map_free((struct isl_sol_map *)sol);
309 /* This function is called for parts of the context where there is
310 * no solution, with "bset" corresponding to the context tableau.
311 * Simply add the basic set to the set "empty".
313 static void sol_map_add_empty(struct isl_sol_map *sol,
314 struct isl_basic_set *bset)
318 isl_assert(bset->ctx, sol->empty, goto error);
320 sol->empty = isl_set_grow(sol->empty, 1);
321 bset = isl_basic_set_simplify(bset);
322 bset = isl_basic_set_finalize(bset);
323 sol->empty = isl_set_add(sol->empty, isl_basic_set_copy(bset));
326 isl_basic_set_free(bset);
329 isl_basic_set_free(bset);
333 static void sol_map_add_empty_wrap(struct isl_sol *sol,
334 struct isl_basic_set *bset)
336 sol_map_add_empty((struct isl_sol_map *)sol, bset);
339 /* Given a basic map "dom" that represents the context and an affine
340 * matrix "M" that maps the dimensions of the context to the
341 * output variables, construct a basic map with the same parameters
342 * and divs as the context, the dimensions of the context as input
343 * dimensions and a number of output dimensions that is equal to
344 * the number of output dimensions in the input map.
346 * The constraints and divs of the context are simply copied
347 * from "dom". For each row
351 * is added, with d the common denominator of M.
353 static void sol_map_add(struct isl_sol_map *sol,
354 struct isl_basic_set *dom, struct isl_mat *M)
357 struct isl_basic_map *bmap = NULL;
358 isl_basic_set *context_bset;
366 if (sol->sol.error || !dom || !M)
369 n_out = sol->sol.n_out;
370 n_eq = dom->n_eq + n_out;
371 n_ineq = dom->n_ineq;
373 nparam = isl_basic_set_total_dim(dom) - n_div;
374 total = isl_map_dim(sol->map, isl_dim_all);
375 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
376 n_div, n_eq, 2 * n_div + n_ineq);
379 if (ISL_F_ISSET(dom, ISL_BASIC_SET_RATIONAL))
380 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
381 for (i = 0; i < dom->n_div; ++i) {
382 int k = isl_basic_map_alloc_div(bmap);
385 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
386 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
387 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
388 dom->div[i] + 1 + 1 + nparam, i);
390 for (i = 0; i < dom->n_eq; ++i) {
391 int k = isl_basic_map_alloc_equality(bmap);
394 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
395 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
396 isl_seq_cpy(bmap->eq[k] + 1 + total,
397 dom->eq[i] + 1 + nparam, n_div);
399 for (i = 0; i < dom->n_ineq; ++i) {
400 int k = isl_basic_map_alloc_inequality(bmap);
403 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
404 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
405 isl_seq_cpy(bmap->ineq[k] + 1 + total,
406 dom->ineq[i] + 1 + nparam, n_div);
408 for (i = 0; i < M->n_row - 1; ++i) {
409 int k = isl_basic_map_alloc_equality(bmap);
412 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
413 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
414 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
415 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
416 M->row[1 + i] + 1 + nparam, n_div);
418 bmap = isl_basic_map_simplify(bmap);
419 bmap = isl_basic_map_finalize(bmap);
420 sol->map = isl_map_grow(sol->map, 1);
421 sol->map = isl_map_add(sol->map, bmap);
424 isl_basic_set_free(dom);
428 isl_basic_set_free(dom);
430 isl_basic_map_free(bmap);
434 static void sol_map_add_wrap(struct isl_sol *sol,
435 struct isl_basic_set *dom, struct isl_mat *M)
437 sol_map_add((struct isl_sol_map *)sol, dom, M);
441 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
442 * i.e., the constant term and the coefficients of all variables that
443 * appear in the context tableau.
444 * Note that the coefficient of the big parameter M is NOT copied.
445 * The context tableau may not have a big parameter and even when it
446 * does, it is a different big parameter.
448 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
451 unsigned off = 2 + tab->M;
453 isl_int_set(line[0], tab->mat->row[row][1]);
454 for (i = 0; i < tab->n_param; ++i) {
455 if (tab->var[i].is_row)
456 isl_int_set_si(line[1 + i], 0);
458 int col = tab->var[i].index;
459 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
462 for (i = 0; i < tab->n_div; ++i) {
463 if (tab->var[tab->n_var - tab->n_div + i].is_row)
464 isl_int_set_si(line[1 + tab->n_param + i], 0);
466 int col = tab->var[tab->n_var - tab->n_div + i].index;
467 isl_int_set(line[1 + tab->n_param + i],
468 tab->mat->row[row][off + col]);
473 /* Check if rows "row1" and "row2" have identical "parametric constants",
474 * as explained above.
475 * In this case, we also insist that the coefficients of the big parameter
476 * be the same as the values of the constants will only be the same
477 * if these coefficients are also the same.
479 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
482 unsigned off = 2 + tab->M;
484 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
487 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
488 tab->mat->row[row2][2]))
491 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
492 int pos = i < tab->n_param ? i :
493 tab->n_var - tab->n_div + i - tab->n_param;
496 if (tab->var[pos].is_row)
498 col = tab->var[pos].index;
499 if (isl_int_ne(tab->mat->row[row1][off + col],
500 tab->mat->row[row2][off + col]))
506 /* Return an inequality that expresses that the "parametric constant"
507 * should be non-negative.
508 * This function is only called when the coefficient of the big parameter
511 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
513 struct isl_vec *ineq;
515 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
519 get_row_parameter_line(tab, row, ineq->el);
521 ineq = isl_vec_normalize(ineq);
526 /* Return a integer division for use in a parametric cut based on the given row.
527 * In particular, let the parametric constant of the row be
531 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
532 * The div returned is equal to
534 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
536 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
540 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
544 isl_int_set(div->el[0], tab->mat->row[row][0]);
545 get_row_parameter_line(tab, row, div->el + 1);
546 div = isl_vec_normalize(div);
547 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
548 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
553 /* Return a integer division for use in transferring an integrality constraint
555 * In particular, let the parametric constant of the row be
559 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
560 * The the returned div is equal to
562 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
564 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
568 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
572 isl_int_set(div->el[0], tab->mat->row[row][0]);
573 get_row_parameter_line(tab, row, div->el + 1);
574 div = isl_vec_normalize(div);
575 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
580 /* Construct and return an inequality that expresses an upper bound
582 * In particular, if the div is given by
586 * then the inequality expresses
590 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
594 struct isl_vec *ineq;
599 total = isl_basic_set_total_dim(bset);
600 div_pos = 1 + total - bset->n_div + div;
602 ineq = isl_vec_alloc(bset->ctx, 1 + total);
606 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
607 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
611 /* Given a row in the tableau and a div that was created
612 * using get_row_split_div and that been constrained to equality, i.e.,
614 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
616 * replace the expression "\sum_i {a_i} y_i" in the row by d,
617 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
618 * The coefficients of the non-parameters in the tableau have been
619 * verified to be integral. We can therefore simply replace coefficient b
620 * by floor(b). For the coefficients of the parameters we have
621 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
624 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
626 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
627 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
629 isl_int_set_si(tab->mat->row[row][0], 1);
631 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
632 int drow = tab->var[tab->n_var - tab->n_div + div].index;
634 isl_assert(tab->mat->ctx,
635 isl_int_is_one(tab->mat->row[drow][0]), goto error);
636 isl_seq_combine(tab->mat->row[row] + 1,
637 tab->mat->ctx->one, tab->mat->row[row] + 1,
638 tab->mat->ctx->one, tab->mat->row[drow] + 1,
639 1 + tab->M + tab->n_col);
641 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
643 isl_int_set_si(tab->mat->row[row][2 + tab->M + dcol], 1);
652 /* Check if the (parametric) constant of the given row is obviously
653 * negative, meaning that we don't need to consult the context tableau.
654 * If there is a big parameter and its coefficient is non-zero,
655 * then this coefficient determines the outcome.
656 * Otherwise, we check whether the constant is negative and
657 * all non-zero coefficients of parameters are negative and
658 * belong to non-negative parameters.
660 static int is_obviously_neg(struct isl_tab *tab, int row)
664 unsigned off = 2 + tab->M;
667 if (isl_int_is_pos(tab->mat->row[row][2]))
669 if (isl_int_is_neg(tab->mat->row[row][2]))
673 if (isl_int_is_nonneg(tab->mat->row[row][1]))
675 for (i = 0; i < tab->n_param; ++i) {
676 /* Eliminated parameter */
677 if (tab->var[i].is_row)
679 col = tab->var[i].index;
680 if (isl_int_is_zero(tab->mat->row[row][off + col]))
682 if (!tab->var[i].is_nonneg)
684 if (isl_int_is_pos(tab->mat->row[row][off + col]))
687 for (i = 0; i < tab->n_div; ++i) {
688 if (tab->var[tab->n_var - tab->n_div + i].is_row)
690 col = tab->var[tab->n_var - tab->n_div + i].index;
691 if (isl_int_is_zero(tab->mat->row[row][off + col]))
693 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
695 if (isl_int_is_pos(tab->mat->row[row][off + col]))
701 /* Check if the (parametric) constant of the given row is obviously
702 * non-negative, meaning that we don't need to consult the context tableau.
703 * If there is a big parameter and its coefficient is non-zero,
704 * then this coefficient determines the outcome.
705 * Otherwise, we check whether the constant is non-negative and
706 * all non-zero coefficients of parameters are positive and
707 * belong to non-negative parameters.
709 static int is_obviously_nonneg(struct isl_tab *tab, int row)
713 unsigned off = 2 + tab->M;
716 if (isl_int_is_pos(tab->mat->row[row][2]))
718 if (isl_int_is_neg(tab->mat->row[row][2]))
722 if (isl_int_is_neg(tab->mat->row[row][1]))
724 for (i = 0; i < tab->n_param; ++i) {
725 /* Eliminated parameter */
726 if (tab->var[i].is_row)
728 col = tab->var[i].index;
729 if (isl_int_is_zero(tab->mat->row[row][off + col]))
731 if (!tab->var[i].is_nonneg)
733 if (isl_int_is_neg(tab->mat->row[row][off + col]))
736 for (i = 0; i < tab->n_div; ++i) {
737 if (tab->var[tab->n_var - tab->n_div + i].is_row)
739 col = tab->var[tab->n_var - tab->n_div + i].index;
740 if (isl_int_is_zero(tab->mat->row[row][off + col]))
742 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
744 if (isl_int_is_neg(tab->mat->row[row][off + col]))
750 /* Given a row r and two columns, return the column that would
751 * lead to the lexicographically smallest increment in the sample
752 * solution when leaving the basis in favor of the row.
753 * Pivoting with column c will increment the sample value by a non-negative
754 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
755 * corresponding to the non-parametric variables.
756 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
757 * with all other entries in this virtual row equal to zero.
758 * If variable v appears in a row, then a_{v,c} is the element in column c
761 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
762 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
763 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
764 * increment. Otherwise, it's c2.
766 static int lexmin_col_pair(struct isl_tab *tab,
767 int row, int col1, int col2, isl_int tmp)
772 tr = tab->mat->row[row] + 2 + tab->M;
774 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
778 if (!tab->var[i].is_row) {
779 if (tab->var[i].index == col1)
781 if (tab->var[i].index == col2)
786 if (tab->var[i].index == row)
789 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
790 s1 = isl_int_sgn(r[col1]);
791 s2 = isl_int_sgn(r[col2]);
792 if (s1 == 0 && s2 == 0)
799 isl_int_mul(tmp, r[col2], tr[col1]);
800 isl_int_submul(tmp, r[col1], tr[col2]);
801 if (isl_int_is_pos(tmp))
803 if (isl_int_is_neg(tmp))
809 /* Given a row in the tableau, find and return the column that would
810 * result in the lexicographically smallest, but positive, increment
811 * in the sample point.
812 * If there is no such column, then return tab->n_col.
813 * If anything goes wrong, return -1.
815 static int lexmin_pivot_col(struct isl_tab *tab, int row)
818 int col = tab->n_col;
822 tr = tab->mat->row[row] + 2 + tab->M;
826 for (j = tab->n_dead; j < tab->n_col; ++j) {
827 if (tab->col_var[j] >= 0 &&
828 (tab->col_var[j] < tab->n_param ||
829 tab->col_var[j] >= tab->n_var - tab->n_div))
832 if (!isl_int_is_pos(tr[j]))
835 if (col == tab->n_col)
838 col = lexmin_col_pair(tab, row, col, j, tmp);
839 isl_assert(tab->mat->ctx, col >= 0, goto error);
849 /* Return the first known violated constraint, i.e., a non-negative
850 * contraint that currently has an either obviously negative value
851 * or a previously determined to be negative value.
853 * If any constraint has a negative coefficient for the big parameter,
854 * if any, then we return one of these first.
856 static int first_neg(struct isl_tab *tab)
861 for (row = tab->n_redundant; row < tab->n_row; ++row) {
862 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
864 if (isl_int_is_neg(tab->mat->row[row][2]))
867 for (row = tab->n_redundant; row < tab->n_row; ++row) {
868 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
871 if (tab->row_sign[row] == 0 &&
872 is_obviously_neg(tab, row))
873 tab->row_sign[row] = isl_tab_row_neg;
874 if (tab->row_sign[row] != isl_tab_row_neg)
876 } else if (!is_obviously_neg(tab, row))
883 /* Resolve all known or obviously violated constraints through pivoting.
884 * In particular, as long as we can find any violated constraint, we
885 * look for a pivoting column that would result in the lexicographicallly
886 * smallest increment in the sample point. If there is no such column
887 * then the tableau is infeasible.
889 static struct isl_tab *restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
890 static struct isl_tab *restore_lexmin(struct isl_tab *tab)
898 while ((row = first_neg(tab)) != -1) {
899 col = lexmin_pivot_col(tab, row);
900 if (col >= tab->n_col)
901 return isl_tab_mark_empty(tab);
904 if (isl_tab_pivot(tab, row, col) < 0)
913 /* Given a row that represents an equality, look for an appropriate
915 * In particular, if there are any non-zero coefficients among
916 * the non-parameter variables, then we take the last of these
917 * variables. Eliminating this variable in terms of the other
918 * variables and/or parameters does not influence the property
919 * that all column in the initial tableau are lexicographically
920 * positive. The row corresponding to the eliminated variable
921 * will only have non-zero entries below the diagonal of the
922 * initial tableau. That is, we transform
928 * If there is no such non-parameter variable, then we are dealing with
929 * pure parameter equality and we pick any parameter with coefficient 1 or -1
930 * for elimination. This will ensure that the eliminated parameter
931 * always has an integer value whenever all the other parameters are integral.
932 * If there is no such parameter then we return -1.
934 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
936 unsigned off = 2 + tab->M;
939 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
941 if (tab->var[i].is_row)
943 col = tab->var[i].index;
944 if (col <= tab->n_dead)
946 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
949 for (i = tab->n_dead; i < tab->n_col; ++i) {
950 if (isl_int_is_one(tab->mat->row[row][off + i]))
952 if (isl_int_is_negone(tab->mat->row[row][off + i]))
958 /* Add an equality that is known to be valid to the tableau.
959 * We first check if we can eliminate a variable or a parameter.
960 * If not, we add the equality as two inequalities.
961 * In this case, the equality was a pure parameter equality and there
962 * is no need to resolve any constraint violations.
964 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
971 r = isl_tab_add_row(tab, eq);
975 r = tab->con[r].index;
976 i = last_var_col_or_int_par_col(tab, r);
978 tab->con[r].is_nonneg = 1;
979 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
981 isl_seq_neg(eq, eq, 1 + tab->n_var);
982 r = isl_tab_add_row(tab, eq);
985 tab->con[r].is_nonneg = 1;
986 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
989 if (isl_tab_pivot(tab, r, i) < 0)
991 if (isl_tab_kill_col(tab, i) < 0)
995 tab = restore_lexmin(tab);
1004 /* Check if the given row is a pure constant.
1006 static int is_constant(struct isl_tab *tab, int row)
1008 unsigned off = 2 + tab->M;
1010 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1011 tab->n_col - tab->n_dead) == -1;
1014 /* Add an equality that may or may not be valid to the tableau.
1015 * If the resulting row is a pure constant, then it must be zero.
1016 * Otherwise, the resulting tableau is empty.
1018 * If the row is not a pure constant, then we add two inequalities,
1019 * each time checking that they can be satisfied.
1020 * In the end we try to use one of the two constraints to eliminate
1023 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1024 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1028 struct isl_tab_undo *snap;
1032 snap = isl_tab_snap(tab);
1033 r1 = isl_tab_add_row(tab, eq);
1036 tab->con[r1].is_nonneg = 1;
1037 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1040 row = tab->con[r1].index;
1041 if (is_constant(tab, row)) {
1042 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1043 (tab->M && !isl_int_is_zero(tab->mat->row[row][2])))
1044 return isl_tab_mark_empty(tab);
1045 if (isl_tab_rollback(tab, snap) < 0)
1050 tab = restore_lexmin(tab);
1051 if (!tab || tab->empty)
1054 isl_seq_neg(eq, eq, 1 + tab->n_var);
1056 r2 = isl_tab_add_row(tab, eq);
1059 tab->con[r2].is_nonneg = 1;
1060 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1063 tab = restore_lexmin(tab);
1064 if (!tab || tab->empty)
1067 if (!tab->con[r1].is_row) {
1068 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1070 } else if (!tab->con[r2].is_row) {
1071 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1073 } else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
1074 unsigned off = 2 + tab->M;
1076 int row = tab->con[r1].index;
1077 i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
1078 tab->n_col - tab->n_dead);
1080 if (isl_tab_pivot(tab, row, tab->n_dead + i) < 0)
1082 if (isl_tab_kill_col(tab, tab->n_dead + i) < 0)
1088 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1089 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1091 isl_seq_neg(eq, eq, 1 + tab->n_var);
1092 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1093 isl_seq_neg(eq, eq, 1 + tab->n_var);
1094 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1106 /* Add an inequality to the tableau, resolving violations using
1109 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1116 tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
1117 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1122 r = isl_tab_add_row(tab, ineq);
1125 tab->con[r].is_nonneg = 1;
1126 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1128 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1129 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1134 tab = restore_lexmin(tab);
1135 if (tab && !tab->empty && tab->con[r].is_row &&
1136 isl_tab_row_is_redundant(tab, tab->con[r].index))
1137 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1145 /* Check if the coefficients of the parameters are all integral.
1147 static int integer_parameter(struct isl_tab *tab, int row)
1151 unsigned off = 2 + tab->M;
1153 for (i = 0; i < tab->n_param; ++i) {
1154 /* Eliminated parameter */
1155 if (tab->var[i].is_row)
1157 col = tab->var[i].index;
1158 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1159 tab->mat->row[row][0]))
1162 for (i = 0; i < tab->n_div; ++i) {
1163 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1165 col = tab->var[tab->n_var - tab->n_div + i].index;
1166 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1167 tab->mat->row[row][0]))
1173 /* Check if the coefficients of the non-parameter variables are all integral.
1175 static int integer_variable(struct isl_tab *tab, int row)
1178 unsigned off = 2 + tab->M;
1180 for (i = 0; i < tab->n_col; ++i) {
1181 if (tab->col_var[i] >= 0 &&
1182 (tab->col_var[i] < tab->n_param ||
1183 tab->col_var[i] >= tab->n_var - tab->n_div))
1185 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1186 tab->mat->row[row][0]))
1192 /* Check if the constant term is integral.
1194 static int integer_constant(struct isl_tab *tab, int row)
1196 return isl_int_is_divisible_by(tab->mat->row[row][1],
1197 tab->mat->row[row][0]);
1200 #define I_CST 1 << 0
1201 #define I_PAR 1 << 1
1202 #define I_VAR 1 << 2
1204 /* Check for first (non-parameter) variable that is non-integer and
1205 * therefore requires a cut.
1206 * For parametric tableaus, there are three parts in a row,
1207 * the constant, the coefficients of the parameters and the rest.
1208 * For each part, we check whether the coefficients in that part
1209 * are all integral and if so, set the corresponding flag in *f.
1210 * If the constant and the parameter part are integral, then the
1211 * current sample value is integral and no cut is required
1212 * (irrespective of whether the variable part is integral).
1214 static int first_non_integer(struct isl_tab *tab, int *f)
1218 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1221 if (!tab->var[i].is_row)
1223 row = tab->var[i].index;
1224 if (integer_constant(tab, row))
1225 ISL_FL_SET(flags, I_CST);
1226 if (integer_parameter(tab, row))
1227 ISL_FL_SET(flags, I_PAR);
1228 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1230 if (integer_variable(tab, row))
1231 ISL_FL_SET(flags, I_VAR);
1238 /* Add a (non-parametric) cut to cut away the non-integral sample
1239 * value of the given row.
1241 * If the row is given by
1243 * m r = f + \sum_i a_i y_i
1247 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1249 * The big parameter, if any, is ignored, since it is assumed to be big
1250 * enough to be divisible by any integer.
1251 * If the tableau is actually a parametric tableau, then this function
1252 * is only called when all coefficients of the parameters are integral.
1253 * The cut therefore has zero coefficients for the parameters.
1255 * The current value is known to be negative, so row_sign, if it
1256 * exists, is set accordingly.
1258 * Return the row of the cut or -1.
1260 static int add_cut(struct isl_tab *tab, int row)
1265 unsigned off = 2 + tab->M;
1267 if (isl_tab_extend_cons(tab, 1) < 0)
1269 r = isl_tab_allocate_con(tab);
1273 r_row = tab->mat->row[tab->con[r].index];
1274 isl_int_set(r_row[0], tab->mat->row[row][0]);
1275 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1276 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1277 isl_int_neg(r_row[1], r_row[1]);
1279 isl_int_set_si(r_row[2], 0);
1280 for (i = 0; i < tab->n_col; ++i)
1281 isl_int_fdiv_r(r_row[off + i],
1282 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1284 tab->con[r].is_nonneg = 1;
1285 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1288 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1290 return tab->con[r].index;
1293 /* Given a non-parametric tableau, add cuts until an integer
1294 * sample point is obtained or until the tableau is determined
1295 * to be integer infeasible.
1296 * As long as there is any non-integer value in the sample point,
1297 * we add an appropriate cut, if possible and resolve the violated
1298 * cut constraint using restore_lexmin.
1299 * If one of the corresponding rows is equal to an integral
1300 * combination of variables/constraints plus a non-integral constant,
1301 * then there is no way to obtain an integer point an we return
1302 * a tableau that is marked empty.
1304 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
1314 while ((row = first_non_integer(tab, &flags)) != -1) {
1315 if (ISL_FL_ISSET(flags, I_VAR))
1316 return isl_tab_mark_empty(tab);
1317 row = add_cut(tab, row);
1320 tab = restore_lexmin(tab);
1321 if (!tab || tab->empty)
1330 /* Check whether all the currently active samples also satisfy the inequality
1331 * "ineq" (treated as an equality if eq is set).
1332 * Remove those samples that do not.
1334 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1342 isl_assert(tab->mat->ctx, tab->bset, goto error);
1343 isl_assert(tab->mat->ctx, tab->samples, goto error);
1344 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1347 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1349 isl_seq_inner_product(ineq, tab->samples->row[i],
1350 1 + tab->n_var, &v);
1351 sgn = isl_int_sgn(v);
1352 if (eq ? (sgn == 0) : (sgn >= 0))
1354 tab = isl_tab_drop_sample(tab, i);
1366 /* Check whether the sample value of the tableau is finite,
1367 * i.e., either the tableau does not use a big parameter, or
1368 * all values of the variables are equal to the big parameter plus
1369 * some constant. This constant is the actual sample value.
1371 static int sample_is_finite(struct isl_tab *tab)
1378 for (i = 0; i < tab->n_var; ++i) {
1380 if (!tab->var[i].is_row)
1382 row = tab->var[i].index;
1383 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1389 /* Check if the context tableau of sol has any integer points.
1390 * Leave tab in empty state if no integer point can be found.
1391 * If an integer point can be found and if moreover it is finite,
1392 * then it is added to the list of sample values.
1394 * This function is only called when none of the currently active sample
1395 * values satisfies the most recently added constraint.
1397 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1399 struct isl_tab_undo *snap;
1405 snap = isl_tab_snap(tab);
1406 if (isl_tab_push_basis(tab) < 0)
1409 tab = cut_to_integer_lexmin(tab);
1413 if (!tab->empty && sample_is_finite(tab)) {
1414 struct isl_vec *sample;
1416 sample = isl_tab_get_sample_value(tab);
1418 tab = isl_tab_add_sample(tab, sample);
1421 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1430 /* Check if any of the currently active sample values satisfies
1431 * the inequality "ineq" (an equality if eq is set).
1433 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1441 isl_assert(tab->mat->ctx, tab->bset, return -1);
1442 isl_assert(tab->mat->ctx, tab->samples, return -1);
1443 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1446 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1448 isl_seq_inner_product(ineq, tab->samples->row[i],
1449 1 + tab->n_var, &v);
1450 sgn = isl_int_sgn(v);
1451 if (eq ? (sgn == 0) : (sgn >= 0))
1456 return i < tab->n_sample;
1459 /* For a div d = floor(f/m), add the constraints
1462 * -(f-(m-1)) + m d >= 0
1464 * Note that the second constraint is the negation of
1468 static void add_div_constraints(struct isl_context *context, unsigned div)
1472 struct isl_vec *ineq;
1473 struct isl_basic_set *bset;
1475 bset = context->op->peek_basic_set(context);
1479 total = isl_basic_set_total_dim(bset);
1480 div_pos = 1 + total - bset->n_div + div;
1482 ineq = ineq_for_div(bset, div);
1486 context->op->add_ineq(context, ineq->el, 0, 0);
1488 isl_seq_neg(ineq->el, bset->div[div] + 1, 1 + total);
1489 isl_int_set(ineq->el[div_pos], bset->div[div][0]);
1490 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
1491 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
1493 context->op->add_ineq(context, ineq->el, 0, 0);
1499 context->op->invalidate(context);
1502 /* Add a div specifed by "div" to the tableau "tab" and return
1503 * the index of the new div. *nonneg is set to 1 if the div
1504 * is obviously non-negative.
1506 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1512 struct isl_mat *samples;
1514 for (i = 0; i < tab->n_var; ++i) {
1515 if (isl_int_is_zero(div->el[2 + i]))
1517 if (!tab->var[i].is_nonneg)
1520 *nonneg = i == tab->n_var;
1522 if (isl_tab_extend_cons(tab, 3) < 0)
1524 if (isl_tab_extend_vars(tab, 1) < 0)
1526 r = isl_tab_allocate_var(tab);
1530 tab->var[r].is_nonneg = 1;
1531 tab->var[r].frozen = 1;
1533 samples = isl_mat_extend(tab->samples,
1534 tab->n_sample, 1 + tab->n_var);
1535 tab->samples = samples;
1538 for (i = tab->n_outside; i < samples->n_row; ++i) {
1539 isl_seq_inner_product(div->el + 1, samples->row[i],
1540 div->size - 1, &samples->row[i][samples->n_col - 1]);
1541 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1542 samples->row[i][samples->n_col - 1], div->el[0]);
1545 tab->bset = isl_basic_set_extend_dim(tab->bset,
1546 isl_basic_set_get_dim(tab->bset), 1, 0, 2);
1547 k = isl_basic_set_alloc_div(tab->bset);
1550 isl_seq_cpy(tab->bset->div[k], div->el, div->size);
1551 if (isl_tab_push(tab, isl_tab_undo_bset_div) < 0)
1557 /* Add a div specified by "div" to both the main tableau and
1558 * the context tableau. In case of the main tableau, we only
1559 * need to add an extra div. In the context tableau, we also
1560 * need to express the meaning of the div.
1561 * Return the index of the div or -1 if anything went wrong.
1563 static int add_div(struct isl_tab *tab, struct isl_context *context,
1564 struct isl_vec *div)
1570 k = context->op->add_div(context, div, &nonneg);
1574 add_div_constraints(context, k);
1575 if (!context->op->is_ok(context))
1578 if (isl_tab_extend_vars(tab, 1) < 0)
1580 r = isl_tab_allocate_var(tab);
1584 tab->var[r].is_nonneg = 1;
1585 tab->var[r].frozen = 1;
1588 return tab->n_div - 1;
1590 context->op->invalidate(context);
1594 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1597 unsigned total = isl_basic_set_total_dim(tab->bset);
1599 for (i = 0; i < tab->bset->n_div; ++i) {
1600 if (isl_int_ne(tab->bset->div[i][0], denom))
1602 if (!isl_seq_eq(tab->bset->div[i] + 1, div, total))
1609 /* Return the index of a div that corresponds to "div".
1610 * We first check if we already have such a div and if not, we create one.
1612 static int get_div(struct isl_tab *tab, struct isl_context *context,
1613 struct isl_vec *div)
1616 struct isl_tab *context_tab = context->op->peek_tab(context);
1621 d = find_div(context_tab, div->el + 1, div->el[0]);
1625 return add_div(tab, context, div);
1628 /* Add a parametric cut to cut away the non-integral sample value
1630 * Let a_i be the coefficients of the constant term and the parameters
1631 * and let b_i be the coefficients of the variables or constraints
1632 * in basis of the tableau.
1633 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1635 * The cut is expressed as
1637 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1639 * If q did not already exist in the context tableau, then it is added first.
1640 * If q is in a column of the main tableau then the "+ q" can be accomplished
1641 * by setting the corresponding entry to the denominator of the constraint.
1642 * If q happens to be in a row of the main tableau, then the corresponding
1643 * row needs to be added instead (taking care of the denominators).
1644 * Note that this is very unlikely, but perhaps not entirely impossible.
1646 * The current value of the cut is known to be negative (or at least
1647 * non-positive), so row_sign is set accordingly.
1649 * Return the row of the cut or -1.
1651 static int add_parametric_cut(struct isl_tab *tab, int row,
1652 struct isl_context *context)
1654 struct isl_vec *div;
1661 unsigned off = 2 + tab->M;
1666 div = get_row_parameter_div(tab, row);
1671 d = context->op->get_div(context, tab, div);
1675 if (isl_tab_extend_cons(tab, 1) < 0)
1677 r = isl_tab_allocate_con(tab);
1681 r_row = tab->mat->row[tab->con[r].index];
1682 isl_int_set(r_row[0], tab->mat->row[row][0]);
1683 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1684 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1685 isl_int_neg(r_row[1], r_row[1]);
1687 isl_int_set_si(r_row[2], 0);
1688 for (i = 0; i < tab->n_param; ++i) {
1689 if (tab->var[i].is_row)
1691 col = tab->var[i].index;
1692 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1693 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1694 tab->mat->row[row][0]);
1695 isl_int_neg(r_row[off + col], r_row[off + col]);
1697 for (i = 0; i < tab->n_div; ++i) {
1698 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1700 col = tab->var[tab->n_var - tab->n_div + i].index;
1701 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1702 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1703 tab->mat->row[row][0]);
1704 isl_int_neg(r_row[off + col], r_row[off + col]);
1706 for (i = 0; i < tab->n_col; ++i) {
1707 if (tab->col_var[i] >= 0 &&
1708 (tab->col_var[i] < tab->n_param ||
1709 tab->col_var[i] >= tab->n_var - tab->n_div))
1711 isl_int_fdiv_r(r_row[off + i],
1712 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1714 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1716 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1718 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1719 isl_int_divexact(r_row[0], r_row[0], gcd);
1720 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1721 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1722 r_row[0], tab->mat->row[d_row] + 1,
1723 off - 1 + tab->n_col);
1724 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1727 col = tab->var[tab->n_var - tab->n_div + d].index;
1728 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1731 tab->con[r].is_nonneg = 1;
1732 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1735 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1739 row = tab->con[r].index;
1741 if (d >= n && context->op->detect_equalities(context, tab) < 0)
1747 /* Construct a tableau for bmap that can be used for computing
1748 * the lexicographic minimum (or maximum) of bmap.
1749 * If not NULL, then dom is the domain where the minimum
1750 * should be computed. In this case, we set up a parametric
1751 * tableau with row signs (initialized to "unknown").
1752 * If M is set, then the tableau will use a big parameter.
1753 * If max is set, then a maximum should be computed instead of a minimum.
1754 * This means that for each variable x, the tableau will contain the variable
1755 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1756 * of the variables in all constraints are negated prior to adding them
1759 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
1760 struct isl_basic_set *dom, unsigned M, int max)
1763 struct isl_tab *tab;
1765 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
1766 isl_basic_map_total_dim(bmap), M);
1770 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1772 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
1773 tab->n_div = dom->n_div;
1774 tab->row_sign = isl_calloc_array(bmap->ctx,
1775 enum isl_tab_row_sign, tab->mat->n_row);
1779 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1780 return isl_tab_mark_empty(tab);
1782 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1783 tab->var[i].is_nonneg = 1;
1784 tab->var[i].frozen = 1;
1786 for (i = 0; i < bmap->n_eq; ++i) {
1788 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1789 bmap->eq[i] + 1 + tab->n_param,
1790 tab->n_var - tab->n_param - tab->n_div);
1791 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
1793 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1794 bmap->eq[i] + 1 + tab->n_param,
1795 tab->n_var - tab->n_param - tab->n_div);
1796 if (!tab || tab->empty)
1799 for (i = 0; i < bmap->n_ineq; ++i) {
1801 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1802 bmap->ineq[i] + 1 + tab->n_param,
1803 tab->n_var - tab->n_param - tab->n_div);
1804 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
1806 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1807 bmap->ineq[i] + 1 + tab->n_param,
1808 tab->n_var - tab->n_param - tab->n_div);
1809 if (!tab || tab->empty)
1818 /* Given a main tableau where more than one row requires a split,
1819 * determine and return the "best" row to split on.
1821 * Given two rows in the main tableau, if the inequality corresponding
1822 * to the first row is redundant with respect to that of the second row
1823 * in the current tableau, then it is better to split on the second row,
1824 * since in the positive part, both row will be positive.
1825 * (In the negative part a pivot will have to be performed and just about
1826 * anything can happen to the sign of the other row.)
1828 * As a simple heuristic, we therefore select the row that makes the most
1829 * of the other rows redundant.
1831 * Perhaps it would also be useful to look at the number of constraints
1832 * that conflict with any given constraint.
1834 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
1836 struct isl_tab_undo *snap;
1842 if (isl_tab_extend_cons(context_tab, 2) < 0)
1845 snap = isl_tab_snap(context_tab);
1847 for (split = tab->n_redundant; split < tab->n_row; ++split) {
1848 struct isl_tab_undo *snap2;
1849 struct isl_vec *ineq = NULL;
1852 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
1854 if (tab->row_sign[split] != isl_tab_row_any)
1857 ineq = get_row_parameter_ineq(tab, split);
1860 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
1863 snap2 = isl_tab_snap(context_tab);
1865 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1866 struct isl_tab_var *var;
1870 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1872 if (tab->row_sign[row] != isl_tab_row_any)
1875 ineq = get_row_parameter_ineq(tab, row);
1878 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
1880 var = &context_tab->con[context_tab->n_con - 1];
1881 if (!context_tab->empty &&
1882 !isl_tab_min_at_most_neg_one(context_tab, var))
1884 if (isl_tab_rollback(context_tab, snap2) < 0)
1887 if (best == -1 || r > best_r) {
1891 if (isl_tab_rollback(context_tab, snap) < 0)
1898 static struct isl_basic_set *context_lex_peek_basic_set(
1899 struct isl_context *context)
1901 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1904 return clex->tab->bset;
1907 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
1909 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1913 static void context_lex_extend(struct isl_context *context, int n)
1915 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1918 if (isl_tab_extend_cons(clex->tab, n) >= 0)
1920 isl_tab_free(clex->tab);
1924 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
1925 int check, int update)
1927 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1928 if (isl_tab_extend_cons(clex->tab, 2) < 0)
1930 clex->tab = add_lexmin_eq(clex->tab, eq);
1932 int v = tab_has_valid_sample(clex->tab, eq, 1);
1936 clex->tab = check_integer_feasible(clex->tab);
1939 clex->tab = check_samples(clex->tab, eq, 1);
1942 isl_tab_free(clex->tab);
1946 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
1947 int check, int update)
1949 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1950 if (isl_tab_extend_cons(clex->tab, 1) < 0)
1952 clex->tab = add_lexmin_ineq(clex->tab, ineq);
1954 int v = tab_has_valid_sample(clex->tab, ineq, 0);
1958 clex->tab = check_integer_feasible(clex->tab);
1961 clex->tab = check_samples(clex->tab, ineq, 0);
1964 isl_tab_free(clex->tab);
1968 /* Check which signs can be obtained by "ineq" on all the currently
1969 * active sample values. See row_sign for more information.
1971 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
1977 int res = isl_tab_row_unknown;
1979 isl_assert(tab->mat->ctx, tab->samples, return 0);
1980 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return 0);
1983 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1984 isl_seq_inner_product(tab->samples->row[i], ineq,
1985 1 + tab->n_var, &tmp);
1986 sgn = isl_int_sgn(tmp);
1987 if (sgn > 0 || (sgn == 0 && strict)) {
1988 if (res == isl_tab_row_unknown)
1989 res = isl_tab_row_pos;
1990 if (res == isl_tab_row_neg)
1991 res = isl_tab_row_any;
1994 if (res == isl_tab_row_unknown)
1995 res = isl_tab_row_neg;
1996 if (res == isl_tab_row_pos)
1997 res = isl_tab_row_any;
1999 if (res == isl_tab_row_any)
2007 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2008 isl_int *ineq, int strict)
2010 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2011 return tab_ineq_sign(clex->tab, ineq, strict);
2014 /* Check whether "ineq" can be added to the tableau without rendering
2017 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2019 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2020 struct isl_tab_undo *snap;
2026 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2029 snap = isl_tab_snap(clex->tab);
2030 if (isl_tab_push_basis(clex->tab) < 0)
2032 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2033 clex->tab = check_integer_feasible(clex->tab);
2036 feasible = !clex->tab->empty;
2037 if (isl_tab_rollback(clex->tab, snap) < 0)
2043 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2044 struct isl_vec *div)
2046 return get_div(tab, context, div);
2049 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div,
2052 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2053 return context_tab_add_div(clex->tab, div, nonneg);
2056 static int context_lex_detect_equalities(struct isl_context *context,
2057 struct isl_tab *tab)
2062 static int context_lex_best_split(struct isl_context *context,
2063 struct isl_tab *tab)
2065 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2066 struct isl_tab_undo *snap;
2069 snap = isl_tab_snap(clex->tab);
2070 if (isl_tab_push_basis(clex->tab) < 0)
2072 r = best_split(tab, clex->tab);
2074 if (isl_tab_rollback(clex->tab, snap) < 0)
2080 static int context_lex_is_empty(struct isl_context *context)
2082 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2085 return clex->tab->empty;
2088 static void *context_lex_save(struct isl_context *context)
2090 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2091 struct isl_tab_undo *snap;
2093 snap = isl_tab_snap(clex->tab);
2094 if (isl_tab_push_basis(clex->tab) < 0)
2096 if (isl_tab_save_samples(clex->tab) < 0)
2102 static void context_lex_restore(struct isl_context *context, void *save)
2104 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2105 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2106 isl_tab_free(clex->tab);
2111 static int context_lex_is_ok(struct isl_context *context)
2113 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2117 /* For each variable in the context tableau, check if the variable can
2118 * only attain non-negative values. If so, mark the parameter as non-negative
2119 * in the main tableau. This allows for a more direct identification of some
2120 * cases of violated constraints.
2122 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2123 struct isl_tab *context_tab)
2126 struct isl_tab_undo *snap;
2127 struct isl_vec *ineq = NULL;
2128 struct isl_tab_var *var;
2131 if (context_tab->n_var == 0)
2134 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2138 if (isl_tab_extend_cons(context_tab, 1) < 0)
2141 snap = isl_tab_snap(context_tab);
2144 isl_seq_clr(ineq->el, ineq->size);
2145 for (i = 0; i < context_tab->n_var; ++i) {
2146 isl_int_set_si(ineq->el[1 + i], 1);
2147 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
2148 var = &context_tab->con[context_tab->n_con - 1];
2149 if (!context_tab->empty &&
2150 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2152 if (i >= tab->n_param)
2153 j = i - tab->n_param + tab->n_var - tab->n_div;
2154 tab->var[j].is_nonneg = 1;
2157 isl_int_set_si(ineq->el[1 + i], 0);
2158 if (isl_tab_rollback(context_tab, snap) < 0)
2162 if (context_tab->M && n == context_tab->n_var) {
2163 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2175 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2176 struct isl_context *context, struct isl_tab *tab)
2178 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2179 struct isl_tab_undo *snap;
2181 snap = isl_tab_snap(clex->tab);
2182 if (isl_tab_push_basis(clex->tab) < 0)
2185 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2187 if (isl_tab_rollback(clex->tab, snap) < 0)
2196 static void context_lex_invalidate(struct isl_context *context)
2198 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2199 isl_tab_free(clex->tab);
2203 static void context_lex_free(struct isl_context *context)
2205 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2206 isl_tab_free(clex->tab);
2210 struct isl_context_op isl_context_lex_op = {
2211 context_lex_detect_nonnegative_parameters,
2212 context_lex_peek_basic_set,
2213 context_lex_peek_tab,
2215 context_lex_add_ineq,
2216 context_lex_ineq_sign,
2217 context_lex_test_ineq,
2218 context_lex_get_div,
2219 context_lex_add_div,
2220 context_lex_detect_equalities,
2221 context_lex_best_split,
2222 context_lex_is_empty,
2225 context_lex_restore,
2226 context_lex_invalidate,
2230 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2232 struct isl_tab *tab;
2234 bset = isl_basic_set_cow(bset);
2237 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2241 tab = isl_tab_init_samples(tab);
2244 isl_basic_set_free(bset);
2248 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2250 struct isl_context_lex *clex;
2255 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2259 clex->context.op = &isl_context_lex_op;
2261 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2262 clex->tab = restore_lexmin(clex->tab);
2263 clex->tab = check_integer_feasible(clex->tab);
2267 return &clex->context;
2269 clex->context.op->free(&clex->context);
2273 struct isl_context_gbr {
2274 struct isl_context context;
2275 struct isl_tab *tab;
2276 struct isl_tab *shifted;
2277 struct isl_tab *cone;
2280 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2281 struct isl_context *context, struct isl_tab *tab)
2283 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2284 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2287 static struct isl_basic_set *context_gbr_peek_basic_set(
2288 struct isl_context *context)
2290 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2293 return cgbr->tab->bset;
2296 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2298 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2302 /* Initialize the "shifted" tableau of the context, which
2303 * contains the constraints of the original tableau shifted
2304 * by the sum of all negative coefficients. This ensures
2305 * that any rational point in the shifted tableau can
2306 * be rounded up to yield an integer point in the original tableau.
2308 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2311 struct isl_vec *cst;
2312 struct isl_basic_set *bset = cgbr->tab->bset;
2313 unsigned dim = isl_basic_set_total_dim(bset);
2315 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2319 for (i = 0; i < bset->n_ineq; ++i) {
2320 isl_int_set(cst->el[i], bset->ineq[i][0]);
2321 for (j = 0; j < dim; ++j) {
2322 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2324 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2325 bset->ineq[i][1 + j]);
2329 cgbr->shifted = isl_tab_from_basic_set(bset);
2331 for (i = 0; i < bset->n_ineq; ++i)
2332 isl_int_set(bset->ineq[i][0], cst->el[i]);
2337 /* Check if the shifted tableau is non-empty, and if so
2338 * use the sample point to construct an integer point
2339 * of the context tableau.
2341 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2343 struct isl_vec *sample;
2346 gbr_init_shifted(cgbr);
2349 if (cgbr->shifted->empty)
2350 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2352 sample = isl_tab_get_sample_value(cgbr->shifted);
2353 sample = isl_vec_ceil(sample);
2358 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2365 for (i = 0; i < bset->n_eq; ++i)
2366 isl_int_set_si(bset->eq[i][0], 0);
2368 for (i = 0; i < bset->n_ineq; ++i)
2369 isl_int_set_si(bset->ineq[i][0], 0);
2374 static int use_shifted(struct isl_context_gbr *cgbr)
2376 return cgbr->tab->bset->n_eq == 0 && cgbr->tab->bset->n_div == 0;
2379 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2381 struct isl_basic_set *bset;
2382 struct isl_basic_set *cone;
2384 if (isl_tab_sample_is_integer(cgbr->tab))
2385 return isl_tab_get_sample_value(cgbr->tab);
2387 if (use_shifted(cgbr)) {
2388 struct isl_vec *sample;
2390 sample = gbr_get_shifted_sample(cgbr);
2391 if (!sample || sample->size > 0)
2394 isl_vec_free(sample);
2398 cgbr->cone = isl_tab_from_recession_cone(cgbr->tab->bset);
2401 cgbr->cone->bset = isl_basic_set_dup(cgbr->tab->bset);
2403 cgbr->cone = isl_tab_detect_implicit_equalities(cgbr->cone);
2407 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2408 struct isl_vec *sample;
2409 struct isl_tab_undo *snap;
2411 if (cgbr->tab->basis) {
2412 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2413 isl_mat_free(cgbr->tab->basis);
2414 cgbr->tab->basis = NULL;
2416 cgbr->tab->n_zero = 0;
2417 cgbr->tab->n_unbounded = 0;
2421 snap = isl_tab_snap(cgbr->tab);
2423 sample = isl_tab_sample(cgbr->tab);
2425 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2426 isl_vec_free(sample);
2433 cone = isl_basic_set_dup(cgbr->cone->bset);
2434 cone = drop_constant_terms(cone);
2435 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2436 cone = isl_basic_set_underlying_set(cone);
2437 cone = isl_basic_set_gauss(cone, NULL);
2439 bset = isl_basic_set_dup(cgbr->tab->bset);
2440 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2441 bset = isl_basic_set_underlying_set(bset);
2442 bset = isl_basic_set_gauss(bset, NULL);
2444 return isl_basic_set_sample_with_cone(bset, cone);
2447 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2449 struct isl_vec *sample;
2454 if (cgbr->tab->empty)
2457 sample = gbr_get_sample(cgbr);
2461 if (sample->size == 0) {
2462 isl_vec_free(sample);
2463 cgbr->tab = isl_tab_mark_empty(cgbr->tab);
2467 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2471 isl_tab_free(cgbr->tab);
2475 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2482 if (isl_tab_extend_cons(tab, 2) < 0)
2485 tab = isl_tab_add_eq(tab, eq);
2493 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2494 int check, int update)
2496 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2498 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2500 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2501 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2503 cgbr->cone = isl_tab_add_eq(cgbr->cone, eq);
2507 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2511 check_gbr_integer_feasible(cgbr);
2514 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2517 isl_tab_free(cgbr->tab);
2521 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2526 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2529 cgbr->tab = isl_tab_add_ineq(cgbr->tab, ineq);
2531 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2534 dim = isl_basic_set_total_dim(cgbr->tab->bset);
2536 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2539 for (i = 0; i < dim; ++i) {
2540 if (!isl_int_is_neg(ineq[1 + i]))
2542 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2545 cgbr->shifted = isl_tab_add_ineq(cgbr->shifted, ineq);
2547 for (i = 0; i < dim; ++i) {
2548 if (!isl_int_is_neg(ineq[1 + i]))
2550 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2554 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2555 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2557 cgbr->cone = isl_tab_add_ineq(cgbr->cone, ineq);
2562 isl_tab_free(cgbr->tab);
2566 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2567 int check, int update)
2569 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2571 add_gbr_ineq(cgbr, ineq);
2576 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2580 check_gbr_integer_feasible(cgbr);
2583 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2586 isl_tab_free(cgbr->tab);
2590 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2591 isl_int *ineq, int strict)
2593 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2594 return tab_ineq_sign(cgbr->tab, ineq, strict);
2597 /* Check whether "ineq" can be added to the tableau without rendering
2600 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2602 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2603 struct isl_tab_undo *snap;
2604 struct isl_tab_undo *shifted_snap = NULL;
2605 struct isl_tab_undo *cone_snap = NULL;
2611 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2614 snap = isl_tab_snap(cgbr->tab);
2616 shifted_snap = isl_tab_snap(cgbr->shifted);
2618 cone_snap = isl_tab_snap(cgbr->cone);
2619 add_gbr_ineq(cgbr, ineq);
2620 check_gbr_integer_feasible(cgbr);
2623 feasible = !cgbr->tab->empty;
2624 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2627 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2629 } else if (cgbr->shifted) {
2630 isl_tab_free(cgbr->shifted);
2631 cgbr->shifted = NULL;
2634 if (isl_tab_rollback(cgbr->cone, cone_snap))
2636 } else if (cgbr->cone) {
2637 isl_tab_free(cgbr->cone);
2644 /* Return the column of the last of the variables associated to
2645 * a column that has a non-zero coefficient.
2646 * This function is called in a context where only coefficients
2647 * of parameters or divs can be non-zero.
2649 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
2653 unsigned dim = tab->n_var - tab->n_param - tab->n_div;
2655 if (tab->n_var == 0)
2658 for (i = tab->n_var - 1; i >= 0; --i) {
2659 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
2661 if (tab->var[i].is_row)
2663 col = tab->var[i].index;
2664 if (!isl_int_is_zero(p[col]))
2671 /* Look through all the recently added equalities in the context
2672 * to see if we can propagate any of them to the main tableau.
2674 * The newly added equalities in the context are encoded as pairs
2675 * of inequalities starting at inequality "first".
2677 * We tentatively add each of these equalities to the main tableau
2678 * and if this happens to result in a row with a final coefficient
2679 * that is one or negative one, we use it to kill a column
2680 * in the main tableau. Otherwise, we discard the tentatively
2683 static void propagate_equalities(struct isl_context_gbr *cgbr,
2684 struct isl_tab *tab, unsigned first)
2687 struct isl_vec *eq = NULL;
2689 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2693 if (isl_tab_extend_cons(tab, (cgbr->tab->bset->n_ineq - first)/2) < 0)
2696 isl_seq_clr(eq->el + 1 + tab->n_param,
2697 tab->n_var - tab->n_param - tab->n_div);
2698 for (i = first; i < cgbr->tab->bset->n_ineq; i += 2) {
2701 struct isl_tab_undo *snap;
2702 snap = isl_tab_snap(tab);
2704 isl_seq_cpy(eq->el, cgbr->tab->bset->ineq[i], 1 + tab->n_param);
2705 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
2706 cgbr->tab->bset->ineq[i] + 1 + tab->n_param,
2709 r = isl_tab_add_row(tab, eq->el);
2712 r = tab->con[r].index;
2713 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
2714 if (j < 0 || j < tab->n_dead ||
2715 !isl_int_is_one(tab->mat->row[r][0]) ||
2716 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
2717 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
2718 if (isl_tab_rollback(tab, snap) < 0)
2722 if (isl_tab_pivot(tab, r, j) < 0)
2724 if (isl_tab_kill_col(tab, j) < 0)
2727 tab = restore_lexmin(tab);
2735 isl_tab_free(cgbr->tab);
2739 static int context_gbr_detect_equalities(struct isl_context *context,
2740 struct isl_tab *tab)
2742 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2743 struct isl_ctx *ctx;
2745 enum isl_lp_result res;
2748 ctx = cgbr->tab->mat->ctx;
2751 cgbr->cone = isl_tab_from_recession_cone(cgbr->tab->bset);
2754 cgbr->cone->bset = isl_basic_set_dup(cgbr->tab->bset);
2756 cgbr->cone = isl_tab_detect_implicit_equalities(cgbr->cone);
2758 n_ineq = cgbr->tab->bset->n_ineq;
2759 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
2760 if (cgbr->tab && cgbr->tab->bset->n_ineq > n_ineq)
2761 propagate_equalities(cgbr, tab, n_ineq);
2765 isl_tab_free(cgbr->tab);
2770 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
2771 struct isl_vec *div)
2773 return get_div(tab, context, div);
2776 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div,
2779 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2783 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
2785 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
2787 if (isl_tab_allocate_var(cgbr->cone) <0)
2790 cgbr->cone->bset = isl_basic_set_extend_dim(cgbr->cone->bset,
2791 isl_basic_set_get_dim(cgbr->cone->bset), 1, 0, 2);
2792 k = isl_basic_set_alloc_div(cgbr->cone->bset);
2795 isl_seq_cpy(cgbr->cone->bset->div[k], div->el, div->size);
2796 if (isl_tab_push(cgbr->cone, isl_tab_undo_bset_div) < 0)
2799 return context_tab_add_div(cgbr->tab, div, nonneg);
2802 static int context_gbr_best_split(struct isl_context *context,
2803 struct isl_tab *tab)
2805 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2806 struct isl_tab_undo *snap;
2809 snap = isl_tab_snap(cgbr->tab);
2810 r = best_split(tab, cgbr->tab);
2812 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2818 static int context_gbr_is_empty(struct isl_context *context)
2820 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2823 return cgbr->tab->empty;
2826 struct isl_gbr_tab_undo {
2827 struct isl_tab_undo *tab_snap;
2828 struct isl_tab_undo *shifted_snap;
2829 struct isl_tab_undo *cone_snap;
2832 static void *context_gbr_save(struct isl_context *context)
2834 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2835 struct isl_gbr_tab_undo *snap;
2837 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
2841 snap->tab_snap = isl_tab_snap(cgbr->tab);
2842 if (isl_tab_save_samples(cgbr->tab) < 0)
2846 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
2848 snap->shifted_snap = NULL;
2851 snap->cone_snap = isl_tab_snap(cgbr->cone);
2853 snap->cone_snap = NULL;
2861 static void context_gbr_restore(struct isl_context *context, void *save)
2863 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2864 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
2867 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
2868 isl_tab_free(cgbr->tab);
2872 if (snap->shifted_snap) {
2873 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
2875 } else if (cgbr->shifted) {
2876 isl_tab_free(cgbr->shifted);
2877 cgbr->shifted = NULL;
2880 if (snap->cone_snap) {
2881 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
2883 } else if (cgbr->cone) {
2884 isl_tab_free(cgbr->cone);
2893 isl_tab_free(cgbr->tab);
2897 static int context_gbr_is_ok(struct isl_context *context)
2899 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2903 static void context_gbr_invalidate(struct isl_context *context)
2905 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2906 isl_tab_free(cgbr->tab);
2910 static void context_gbr_free(struct isl_context *context)
2912 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2913 isl_tab_free(cgbr->tab);
2914 isl_tab_free(cgbr->shifted);
2915 isl_tab_free(cgbr->cone);
2919 struct isl_context_op isl_context_gbr_op = {
2920 context_gbr_detect_nonnegative_parameters,
2921 context_gbr_peek_basic_set,
2922 context_gbr_peek_tab,
2924 context_gbr_add_ineq,
2925 context_gbr_ineq_sign,
2926 context_gbr_test_ineq,
2927 context_gbr_get_div,
2928 context_gbr_add_div,
2929 context_gbr_detect_equalities,
2930 context_gbr_best_split,
2931 context_gbr_is_empty,
2934 context_gbr_restore,
2935 context_gbr_invalidate,
2939 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
2941 struct isl_context_gbr *cgbr;
2946 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
2950 cgbr->context.op = &isl_context_gbr_op;
2952 cgbr->shifted = NULL;
2954 cgbr->tab = isl_tab_from_basic_set(dom);
2955 cgbr->tab = isl_tab_init_samples(cgbr->tab);
2958 cgbr->tab->bset = isl_basic_set_cow(isl_basic_set_copy(dom));
2959 if (!cgbr->tab->bset)
2961 check_gbr_integer_feasible(cgbr);
2963 return &cgbr->context;
2965 cgbr->context.op->free(&cgbr->context);
2969 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
2974 if (dom->ctx->context == ISL_CONTEXT_LEXMIN)
2975 return isl_context_lex_alloc(dom);
2977 return isl_context_gbr_alloc(dom);
2980 /* Construct an isl_sol_map structure for accumulating the solution.
2981 * If track_empty is set, then we also keep track of the parts
2982 * of the context where there is no solution.
2983 * If max is set, then we are solving a maximization, rather than
2984 * a minimization problem, which means that the variables in the
2985 * tableau have value "M - x" rather than "M + x".
2987 static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
2988 struct isl_basic_set *dom, int track_empty, int max)
2990 struct isl_sol_map *sol_map;
2992 sol_map = isl_calloc_type(bset->ctx, struct isl_sol_map);
2996 sol_map->sol.max = max;
2997 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
2998 sol_map->sol.add = &sol_map_add_wrap;
2999 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3000 sol_map->sol.free = &sol_map_free_wrap;
3001 sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
3006 sol_map->sol.context = isl_context_alloc(dom);
3007 if (!sol_map->sol.context)
3011 sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
3012 1, ISL_SET_DISJOINT);
3013 if (!sol_map->empty)
3017 isl_basic_set_free(dom);
3020 isl_basic_set_free(dom);
3021 sol_map_free(sol_map);
3025 /* Check whether all coefficients of (non-parameter) variables
3026 * are non-positive, meaning that no pivots can be performed on the row.
3028 static int is_critical(struct isl_tab *tab, int row)
3031 unsigned off = 2 + tab->M;
3033 for (j = tab->n_dead; j < tab->n_col; ++j) {
3034 if (tab->col_var[j] >= 0 &&
3035 (tab->col_var[j] < tab->n_param ||
3036 tab->col_var[j] >= tab->n_var - tab->n_div))
3039 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3046 /* Check whether the inequality represented by vec is strict over the integers,
3047 * i.e., there are no integer values satisfying the constraint with
3048 * equality. This happens if the gcd of the coefficients is not a divisor
3049 * of the constant term. If so, scale the constraint down by the gcd
3050 * of the coefficients.
3052 static int is_strict(struct isl_vec *vec)
3058 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3059 if (!isl_int_is_one(gcd)) {
3060 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3061 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3062 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3069 /* Determine the sign of the given row of the main tableau.
3070 * The result is one of
3071 * isl_tab_row_pos: always non-negative; no pivot needed
3072 * isl_tab_row_neg: always non-positive; pivot
3073 * isl_tab_row_any: can be both positive and negative; split
3075 * We first handle some simple cases
3076 * - the row sign may be known already
3077 * - the row may be obviously non-negative
3078 * - the parametric constant may be equal to that of another row
3079 * for which we know the sign. This sign will be either "pos" or
3080 * "any". If it had been "neg" then we would have pivoted before.
3082 * If none of these cases hold, we check the value of the row for each
3083 * of the currently active samples. Based on the signs of these values
3084 * we make an initial determination of the sign of the row.
3086 * all zero -> unk(nown)
3087 * all non-negative -> pos
3088 * all non-positive -> neg
3089 * both negative and positive -> all
3091 * If we end up with "all", we are done.
3092 * Otherwise, we perform a check for positive and/or negative
3093 * values as follows.
3095 * samples neg unk pos
3101 * There is no special sign for "zero", because we can usually treat zero
3102 * as either non-negative or non-positive, whatever works out best.
3103 * However, if the row is "critical", meaning that pivoting is impossible
3104 * then we don't want to limp zero with the non-positive case, because
3105 * then we we would lose the solution for those values of the parameters
3106 * where the value of the row is zero. Instead, we treat 0 as non-negative
3107 * ensuring a split if the row can attain both zero and negative values.
3108 * The same happens when the original constraint was one that could not
3109 * be satisfied with equality by any integer values of the parameters.
3110 * In this case, we normalize the constraint, but then a value of zero
3111 * for the normalized constraint is actually a positive value for the
3112 * original constraint, so again we need to treat zero as non-negative.
3113 * In both these cases, we have the following decision tree instead:
3115 * all non-negative -> pos
3116 * all negative -> neg
3117 * both negative and non-negative -> all
3125 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3126 struct isl_sol *sol, int row)
3128 struct isl_vec *ineq = NULL;
3129 int res = isl_tab_row_unknown;
3134 if (tab->row_sign[row] != isl_tab_row_unknown)
3135 return tab->row_sign[row];
3136 if (is_obviously_nonneg(tab, row))
3137 return isl_tab_row_pos;
3138 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3139 if (tab->row_sign[row2] == isl_tab_row_unknown)
3141 if (identical_parameter_line(tab, row, row2))
3142 return tab->row_sign[row2];
3145 critical = is_critical(tab, row);
3147 ineq = get_row_parameter_ineq(tab, row);
3151 strict = is_strict(ineq);
3153 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3154 critical || strict);
3156 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3157 /* test for negative values */
3159 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3160 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3162 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3166 res = isl_tab_row_pos;
3168 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3170 if (res == isl_tab_row_neg) {
3171 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3172 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3176 if (res == isl_tab_row_neg) {
3177 /* test for positive values */
3179 if (!critical && !strict)
3180 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3182 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3186 res = isl_tab_row_any;
3196 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3198 /* Find solutions for values of the parameters that satisfy the given
3201 * We currently take a snapshot of the context tableau that is reset
3202 * when we return from this function, while we make a copy of the main
3203 * tableau, leaving the original main tableau untouched.
3204 * These are fairly arbitrary choices. Making a copy also of the context
3205 * tableau would obviate the need to undo any changes made to it later,
3206 * while taking a snapshot of the main tableau could reduce memory usage.
3207 * If we were to switch to taking a snapshot of the main tableau,
3208 * we would have to keep in mind that we need to save the row signs
3209 * and that we need to do this before saving the current basis
3210 * such that the basis has been restore before we restore the row signs.
3212 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3218 saved = sol->context->op->save(sol->context);
3220 tab = isl_tab_dup(tab);
3224 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3226 find_solutions(sol, tab);
3228 sol->context->op->restore(sol->context, saved);
3234 /* Record the absence of solutions for those values of the parameters
3235 * that do not satisfy the given inequality with equality.
3237 static void no_sol_in_strict(struct isl_sol *sol,
3238 struct isl_tab *tab, struct isl_vec *ineq)
3245 saved = sol->context->op->save(sol->context);
3247 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3249 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3258 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3260 sol->context->op->restore(sol->context, saved);
3266 /* Compute the lexicographic minimum of the set represented by the main
3267 * tableau "tab" within the context "sol->context_tab".
3268 * On entry the sample value of the main tableau is lexicographically
3269 * less than or equal to this lexicographic minimum.
3270 * Pivots are performed until a feasible point is found, which is then
3271 * necessarily equal to the minimum, or until the tableau is found to
3272 * be infeasible. Some pivots may need to be performed for only some
3273 * feasible values of the context tableau. If so, the context tableau
3274 * is split into a part where the pivot is needed and a part where it is not.
3276 * Whenever we enter the main loop, the main tableau is such that no
3277 * "obvious" pivots need to be performed on it, where "obvious" means
3278 * that the given row can be seen to be negative without looking at
3279 * the context tableau. In particular, for non-parametric problems,
3280 * no pivots need to be performed on the main tableau.
3281 * The caller of find_solutions is responsible for making this property
3282 * hold prior to the first iteration of the loop, while restore_lexmin
3283 * is called before every other iteration.
3285 * Inside the main loop, we first examine the signs of the rows of
3286 * the main tableau within the context of the context tableau.
3287 * If we find a row that is always non-positive for all values of
3288 * the parameters satisfying the context tableau and negative for at
3289 * least one value of the parameters, we perform the appropriate pivot
3290 * and start over. An exception is the case where no pivot can be
3291 * performed on the row. In this case, we require that the sign of
3292 * the row is negative for all values of the parameters (rather than just
3293 * non-positive). This special case is handled inside row_sign, which
3294 * will say that the row can have any sign if it determines that it can
3295 * attain both negative and zero values.
3297 * If we can't find a row that always requires a pivot, but we can find
3298 * one or more rows that require a pivot for some values of the parameters
3299 * (i.e., the row can attain both positive and negative signs), then we split
3300 * the context tableau into two parts, one where we force the sign to be
3301 * non-negative and one where we force is to be negative.
3302 * The non-negative part is handled by a recursive call (through find_in_pos).
3303 * Upon returning from this call, we continue with the negative part and
3304 * perform the required pivot.
3306 * If no such rows can be found, all rows are non-negative and we have
3307 * found a (rational) feasible point. If we only wanted a rational point
3309 * Otherwise, we check if all values of the sample point of the tableau
3310 * are integral for the variables. If so, we have found the minimal
3311 * integral point and we are done.
3312 * If the sample point is not integral, then we need to make a distinction
3313 * based on whether the constant term is non-integral or the coefficients
3314 * of the parameters. Furthermore, in order to decide how to handle
3315 * the non-integrality, we also need to know whether the coefficients
3316 * of the other columns in the tableau are integral. This leads
3317 * to the following table. The first two rows do not correspond
3318 * to a non-integral sample point and are only mentioned for completeness.
3320 * constant parameters other
3323 * int int rat | -> no problem
3325 * rat int int -> fail
3327 * rat int rat -> cut
3330 * rat rat rat | -> parametric cut
3333 * rat rat int | -> split context
3335 * If the parametric constant is completely integral, then there is nothing
3336 * to be done. If the constant term is non-integral, but all the other
3337 * coefficient are integral, then there is nothing that can be done
3338 * and the tableau has no integral solution.
3339 * If, on the other hand, one or more of the other columns have rational
3340 * coeffcients, but the parameter coefficients are all integral, then
3341 * we can perform a regular (non-parametric) cut.
3342 * Finally, if there is any parameter coefficient that is non-integral,
3343 * then we need to involve the context tableau. There are two cases here.
3344 * If at least one other column has a rational coefficient, then we
3345 * can perform a parametric cut in the main tableau by adding a new
3346 * integer division in the context tableau.
3347 * If all other columns have integral coefficients, then we need to
3348 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3349 * is always integral. We do this by introducing an integer division
3350 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3351 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3352 * Since q is expressed in the tableau as
3353 * c + \sum a_i y_i - m q >= 0
3354 * -c - \sum a_i y_i + m q + m - 1 >= 0
3355 * it is sufficient to add the inequality
3356 * -c - \sum a_i y_i + m q >= 0
3357 * In the part of the context where this inequality does not hold, the
3358 * main tableau is marked as being empty.
3360 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3362 struct isl_context *context;
3364 if (!tab || sol->error)
3367 context = sol->context;
3371 if (context->op->is_empty(context))
3374 for (; tab && !tab->empty; tab = restore_lexmin(tab)) {
3381 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3382 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3384 sgn = row_sign(tab, sol, row);
3387 tab->row_sign[row] = sgn;
3388 if (sgn == isl_tab_row_any)
3390 if (sgn == isl_tab_row_any && split == -1)
3392 if (sgn == isl_tab_row_neg)
3395 if (row < tab->n_row)
3398 struct isl_vec *ineq;
3400 split = context->op->best_split(context, tab);
3403 ineq = get_row_parameter_ineq(tab, split);
3407 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3408 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3410 if (tab->row_sign[row] == isl_tab_row_any)
3411 tab->row_sign[row] = isl_tab_row_unknown;
3413 tab->row_sign[split] = isl_tab_row_pos;
3414 find_in_pos(sol, tab, ineq->el);
3415 tab->row_sign[split] = isl_tab_row_neg;
3417 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3418 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3419 context->op->add_ineq(context, ineq->el, 0, 1);
3427 row = first_non_integer(tab, &flags);
3430 if (ISL_FL_ISSET(flags, I_PAR)) {
3431 if (ISL_FL_ISSET(flags, I_VAR)) {
3432 tab = isl_tab_mark_empty(tab);
3435 row = add_cut(tab, row);
3436 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3437 struct isl_vec *div;
3438 struct isl_vec *ineq;
3440 div = get_row_split_div(tab, row);
3443 d = context->op->get_div(context, tab, div);
3447 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3448 no_sol_in_strict(sol, tab, ineq);
3449 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3450 context->op->add_ineq(context, ineq->el, 1, 1);
3452 if (sol->error || !context->op->is_ok(context))
3454 tab = set_row_cst_to_div(tab, row, d);
3456 row = add_parametric_cut(tab, row, context);
3469 /* Compute the lexicographic minimum of the set represented by the main
3470 * tableau "tab" within the context "sol->context_tab".
3472 * As a preprocessing step, we first transfer all the purely parametric
3473 * equalities from the main tableau to the context tableau, i.e.,
3474 * parameters that have been pivoted to a row.
3475 * These equalities are ignored by the main algorithm, because the
3476 * corresponding rows may not be marked as being non-negative.
3477 * In parts of the context where the added equality does not hold,
3478 * the main tableau is marked as being empty.
3480 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3484 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3488 if (tab->row_var[row] < 0)
3490 if (tab->row_var[row] >= tab->n_param &&
3491 tab->row_var[row] < tab->n_var - tab->n_div)
3493 if (tab->row_var[row] < tab->n_param)
3494 p = tab->row_var[row];
3496 p = tab->row_var[row]
3497 + tab->n_param - (tab->n_var - tab->n_div);
3499 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3500 get_row_parameter_line(tab, row, eq->el);
3501 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3502 eq = isl_vec_normalize(eq);
3504 no_sol_in_strict(sol, tab, eq);
3506 isl_seq_neg(eq->el, eq->el, eq->size);
3507 no_sol_in_strict(sol, tab, eq);
3508 isl_seq_neg(eq->el, eq->el, eq->size);
3510 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3514 if (isl_tab_mark_redundant(tab, row) < 0)
3517 if (sol->context->op->is_empty(sol->context))
3520 row = tab->n_redundant - 1;
3523 find_solutions(sol, tab);
3530 static void sol_map_find_solutions(struct isl_sol_map *sol_map,
3531 struct isl_tab *tab)
3533 find_solutions_main(&sol_map->sol, tab);
3536 /* Check if integer division "div" of "dom" also occurs in "bmap".
3537 * If so, return its position within the divs.
3538 * If not, return -1.
3540 static int find_context_div(struct isl_basic_map *bmap,
3541 struct isl_basic_set *dom, unsigned div)
3544 unsigned b_dim = isl_dim_total(bmap->dim);
3545 unsigned d_dim = isl_dim_total(dom->dim);
3547 if (isl_int_is_zero(dom->div[div][0]))
3549 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3552 for (i = 0; i < bmap->n_div; ++i) {
3553 if (isl_int_is_zero(bmap->div[i][0]))
3555 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3556 (b_dim - d_dim) + bmap->n_div) != -1)
3558 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3564 /* The correspondence between the variables in the main tableau,
3565 * the context tableau, and the input map and domain is as follows.
3566 * The first n_param and the last n_div variables of the main tableau
3567 * form the variables of the context tableau.
3568 * In the basic map, these n_param variables correspond to the
3569 * parameters and the input dimensions. In the domain, they correspond
3570 * to the parameters and the set dimensions.
3571 * The n_div variables correspond to the integer divisions in the domain.
3572 * To ensure that everything lines up, we may need to copy some of the
3573 * integer divisions of the domain to the map. These have to be placed
3574 * in the same order as those in the context and they have to be placed
3575 * after any other integer divisions that the map may have.
3576 * This function performs the required reordering.
3578 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3579 struct isl_basic_set *dom)
3585 for (i = 0; i < dom->n_div; ++i)
3586 if (find_context_div(bmap, dom, i) != -1)
3588 other = bmap->n_div - common;
3589 if (dom->n_div - common > 0) {
3590 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
3591 dom->n_div - common, 0, 0);
3595 for (i = 0; i < dom->n_div; ++i) {
3596 int pos = find_context_div(bmap, dom, i);
3598 pos = isl_basic_map_alloc_div(bmap);
3601 isl_int_set_si(bmap->div[pos][0], 0);
3603 if (pos != other + i)
3604 isl_basic_map_swap_div(bmap, pos, other + i);
3608 isl_basic_map_free(bmap);
3612 /* Compute the lexicographic minimum (or maximum if "max" is set)
3613 * of "bmap" over the domain "dom" and return the result as a map.
3614 * If "empty" is not NULL, then *empty is assigned a set that
3615 * contains those parts of the domain where there is no solution.
3616 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
3617 * then we compute the rational optimum. Otherwise, we compute
3618 * the integral optimum.
3620 * We perform some preprocessing. As the PILP solver does not
3621 * handle implicit equalities very well, we first make sure all
3622 * the equalities are explicitly available.
3623 * We also make sure the divs in the domain are properly order,
3624 * because they will be added one by one in the given order
3625 * during the construction of the solution map.
3627 struct isl_map *isl_tab_basic_map_partial_lexopt(
3628 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3629 struct isl_set **empty, int max)
3631 struct isl_tab *tab;
3632 struct isl_map *result = NULL;
3633 struct isl_sol_map *sol_map = NULL;
3634 struct isl_context *context;
3641 isl_assert(bmap->ctx,
3642 isl_basic_map_compatible_domain(bmap, dom), goto error);
3644 bmap = isl_basic_map_detect_equalities(bmap);
3647 dom = isl_basic_set_order_divs(dom);
3648 bmap = align_context_divs(bmap, dom);
3650 sol_map = sol_map_init(bmap, dom, !!empty, max);
3654 context = sol_map->sol.context;
3655 if (isl_basic_set_fast_is_empty(context->op->peek_basic_set(context)))
3657 else if (isl_basic_map_fast_is_empty(bmap))
3658 sol_map_add_empty(sol_map,
3659 isl_basic_set_dup(context->op->peek_basic_set(context)));
3661 tab = tab_for_lexmin(bmap,
3662 context->op->peek_basic_set(context), 1, max);
3663 tab = context->op->detect_nonnegative_parameters(context, tab);
3664 sol_map_find_solutions(sol_map, tab);
3666 if (sol_map->sol.error)
3669 result = isl_map_copy(sol_map->map);
3671 *empty = isl_set_copy(sol_map->empty);
3672 sol_free(&sol_map->sol);
3673 isl_basic_map_free(bmap);
3676 sol_free(&sol_map->sol);
3677 isl_basic_map_free(bmap);
3681 struct isl_sol_for {
3683 int (*fn)(__isl_take isl_basic_set *dom,
3684 __isl_take isl_mat *map, void *user);
3688 static void sol_for_free(struct isl_sol_for *sol_for)
3690 if (sol_for->sol.context)
3691 sol_for->sol.context->op->free(sol_for->sol.context);
3695 static void sol_for_free_wrap(struct isl_sol *sol)
3697 sol_for_free((struct isl_sol_for *)sol);
3700 /* Add the solution identified by the tableau and the context tableau.
3702 * See documentation of sol_add for more details.
3704 * Instead of constructing a basic map, this function calls a user
3705 * defined function with the current context as a basic set and
3706 * an affine matrix reprenting the relation between the input and output.
3707 * The number of rows in this matrix is equal to one plus the number
3708 * of output variables. The number of columns is equal to one plus
3709 * the total dimension of the context, i.e., the number of parameters,
3710 * input variables and divs. Since some of the columns in the matrix
3711 * may refer to the divs, the basic set is not simplified.
3712 * (Simplification may reorder or remove divs.)
3714 static void sol_for_add(struct isl_sol_for *sol,
3715 struct isl_basic_set *dom, struct isl_mat *M)
3717 if (sol->sol.error || !dom || !M)
3720 dom = isl_basic_set_simplify(dom);
3721 dom = isl_basic_set_finalize(dom);
3723 if (sol->fn(isl_basic_set_copy(dom), isl_mat_copy(M), sol->user) < 0)
3726 isl_basic_set_free(dom);
3730 isl_basic_set_free(dom);
3735 static void sol_for_add_wrap(struct isl_sol *sol,
3736 struct isl_basic_set *dom, struct isl_mat *M)
3738 sol_for_add((struct isl_sol_for *)sol, dom, M);
3741 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
3742 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3746 struct isl_sol_for *sol_for = NULL;
3747 struct isl_dim *dom_dim;
3748 struct isl_basic_set *dom = NULL;
3750 sol_for = isl_calloc_type(bset->ctx, struct isl_sol_for);
3754 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3755 dom = isl_basic_set_universe(dom_dim);
3758 sol_for->user = user;
3759 sol_for->sol.max = max;
3760 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3761 sol_for->sol.add = &sol_for_add_wrap;
3762 sol_for->sol.add_empty = NULL;
3763 sol_for->sol.free = &sol_for_free_wrap;
3765 sol_for->sol.context = isl_context_alloc(dom);
3766 if (!sol_for->sol.context)
3769 isl_basic_set_free(dom);
3772 isl_basic_set_free(dom);
3773 sol_for_free(sol_for);
3777 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
3778 struct isl_tab *tab)
3780 find_solutions_main(&sol_for->sol, tab);
3783 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
3784 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3788 struct isl_sol_for *sol_for = NULL;
3790 bmap = isl_basic_map_copy(bmap);
3794 bmap = isl_basic_map_detect_equalities(bmap);
3795 sol_for = sol_for_init(bmap, max, fn, user);
3797 if (isl_basic_map_fast_is_empty(bmap))
3800 struct isl_tab *tab;
3801 struct isl_context *context = sol_for->sol.context;
3802 tab = tab_for_lexmin(bmap,
3803 context->op->peek_basic_set(context), 1, max);
3804 tab = context->op->detect_nonnegative_parameters(context, tab);
3805 sol_for_find_solutions(sol_for, tab);
3806 if (sol_for->sol.error)
3810 sol_free(&sol_for->sol);
3811 isl_basic_map_free(bmap);
3814 sol_free(&sol_for->sol);
3815 isl_basic_map_free(bmap);
3819 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map *bmap,
3820 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3824 return isl_basic_map_foreach_lexopt(bmap, 0, fn, user);
3827 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map *bmap,
3828 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3832 return isl_basic_map_foreach_lexopt(bmap, 1, fn, user);