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
123 struct isl_context *context;
124 struct isl_sol *(*add)(struct isl_sol *sol,
125 struct isl_basic_set *dom, struct isl_mat *M);
126 struct isl_sol *(*add_empty)(struct isl_sol *sol,
127 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 struct isl_sol *sol_add(struct isl_sol *sol, struct isl_tab *tab)
200 struct isl_basic_set *bset = NULL;
201 struct isl_mat *mat = NULL;
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 return sol->add_empty(sol, bset);
223 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
224 1 + tab->n_param + tab->n_div);
230 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
231 isl_int_set_si(mat->row[0][0], 1);
232 for (row = 0; row < sol->n_out; ++row) {
233 int i = tab->n_param + row;
236 isl_seq_clr(mat->row[1 + row], mat->n_col);
237 if (!tab->var[i].is_row) {
239 isl_assert(mat->ctx, !tab->M, goto error2);
243 r = tab->var[i].index;
246 isl_assert(mat->ctx, isl_int_eq(tab->mat->row[r][2],
247 tab->mat->row[r][0]),
249 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
250 isl_int_divexact(m, tab->mat->row[r][0], m);
251 scale_rows(mat, m, 1 + row);
252 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
253 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
254 for (j = 0; j < tab->n_param; ++j) {
256 if (tab->var[j].is_row)
258 col = tab->var[j].index;
259 isl_int_mul(mat->row[1 + row][1 + j], m,
260 tab->mat->row[r][off + col]);
262 for (j = 0; j < tab->n_div; ++j) {
264 if (tab->var[tab->n_var - tab->n_div+j].is_row)
266 col = tab->var[tab->n_var - tab->n_div+j].index;
267 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
268 tab->mat->row[r][off + col]);
271 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
277 return sol->add(sol, bset, mat);
281 isl_basic_set_free(bset);
290 struct isl_set *empty;
293 static void sol_map_free(struct isl_sol_map *sol_map)
295 if (sol_map->sol.context)
296 sol_map->sol.context->op->free(sol_map->sol.context);
297 isl_map_free(sol_map->map);
298 isl_set_free(sol_map->empty);
302 static void sol_map_free_wrap(struct isl_sol *sol)
304 sol_map_free((struct isl_sol_map *)sol);
307 /* This function is called for parts of the context where there is
308 * no solution, with "bset" corresponding to the context tableau.
309 * Simply add the basic set to the set "empty".
311 static struct isl_sol_map *sol_map_add_empty(struct isl_sol_map *sol,
312 struct isl_basic_set *bset)
316 isl_assert(bset->ctx, sol->empty, goto error);
318 sol->empty = isl_set_grow(sol->empty, 1);
319 bset = isl_basic_set_simplify(bset);
320 bset = isl_basic_set_finalize(bset);
321 sol->empty = isl_set_add(sol->empty, isl_basic_set_copy(bset));
324 isl_basic_set_free(bset);
327 isl_basic_set_free(bset);
332 static struct isl_sol *sol_map_add_empty_wrap(struct isl_sol *sol,
333 struct isl_basic_set *bset)
335 return (struct isl_sol *)
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 struct isl_sol_map *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 || !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);
435 static struct isl_sol *sol_map_add_wrap(struct isl_sol *sol,
436 struct isl_basic_set *dom, struct isl_mat *M)
438 return (struct isl_sol *)sol_map_add((struct isl_sol_map *)sol, dom, M);
442 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
443 * i.e., the constant term and the coefficients of all variables that
444 * appear in the context tableau.
445 * Note that the coefficient of the big parameter M is NOT copied.
446 * The context tableau may not have a big parameter and even when it
447 * does, it is a different big parameter.
449 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
452 unsigned off = 2 + tab->M;
454 isl_int_set(line[0], tab->mat->row[row][1]);
455 for (i = 0; i < tab->n_param; ++i) {
456 if (tab->var[i].is_row)
457 isl_int_set_si(line[1 + i], 0);
459 int col = tab->var[i].index;
460 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
463 for (i = 0; i < tab->n_div; ++i) {
464 if (tab->var[tab->n_var - tab->n_div + i].is_row)
465 isl_int_set_si(line[1 + tab->n_param + i], 0);
467 int col = tab->var[tab->n_var - tab->n_div + i].index;
468 isl_int_set(line[1 + tab->n_param + i],
469 tab->mat->row[row][off + col]);
474 /* Check if rows "row1" and "row2" have identical "parametric constants",
475 * as explained above.
476 * In this case, we also insist that the coefficients of the big parameter
477 * be the same as the values of the constants will only be the same
478 * if these coefficients are also the same.
480 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
483 unsigned off = 2 + tab->M;
485 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
488 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
489 tab->mat->row[row2][2]))
492 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
493 int pos = i < tab->n_param ? i :
494 tab->n_var - tab->n_div + i - tab->n_param;
497 if (tab->var[pos].is_row)
499 col = tab->var[pos].index;
500 if (isl_int_ne(tab->mat->row[row1][off + col],
501 tab->mat->row[row2][off + col]))
507 /* Return an inequality that expresses that the "parametric constant"
508 * should be non-negative.
509 * This function is only called when the coefficient of the big parameter
512 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
514 struct isl_vec *ineq;
516 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
520 get_row_parameter_line(tab, row, ineq->el);
522 ineq = isl_vec_normalize(ineq);
527 /* Return a integer division for use in a parametric cut based on the given row.
528 * In particular, let the parametric constant of the row be
532 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
533 * The div returned is equal to
535 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
537 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
541 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
545 isl_int_set(div->el[0], tab->mat->row[row][0]);
546 get_row_parameter_line(tab, row, div->el + 1);
547 div = isl_vec_normalize(div);
548 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
549 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
554 /* Return a integer division for use in transferring an integrality constraint
556 * In particular, let the parametric constant of the row be
560 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
561 * The the returned div is equal to
563 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
565 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
569 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
573 isl_int_set(div->el[0], tab->mat->row[row][0]);
574 get_row_parameter_line(tab, row, div->el + 1);
575 div = isl_vec_normalize(div);
576 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
581 /* Construct and return an inequality that expresses an upper bound
583 * In particular, if the div is given by
587 * then the inequality expresses
591 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
595 struct isl_vec *ineq;
600 total = isl_basic_set_total_dim(bset);
601 div_pos = 1 + total - bset->n_div + div;
603 ineq = isl_vec_alloc(bset->ctx, 1 + total);
607 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
608 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
612 /* Given a row in the tableau and a div that was created
613 * using get_row_split_div and that been constrained to equality, i.e.,
615 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
617 * replace the expression "\sum_i {a_i} y_i" in the row by d,
618 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
619 * The coefficients of the non-parameters in the tableau have been
620 * verified to be integral. We can therefore simply replace coefficient b
621 * by floor(b). For the coefficients of the parameters we have
622 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
625 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
627 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
628 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
630 isl_int_set_si(tab->mat->row[row][0], 1);
632 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
633 int drow = tab->var[tab->n_var - tab->n_div + div].index;
635 isl_assert(tab->mat->ctx,
636 isl_int_is_one(tab->mat->row[drow][0]), goto error);
637 isl_seq_combine(tab->mat->row[row] + 1,
638 tab->mat->ctx->one, tab->mat->row[row] + 1,
639 tab->mat->ctx->one, tab->mat->row[drow] + 1,
640 1 + tab->M + tab->n_col);
642 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
644 isl_int_set_si(tab->mat->row[row][2 + tab->M + dcol], 1);
653 /* Check if the (parametric) constant of the given row is obviously
654 * negative, meaning that we don't need to consult the context tableau.
655 * If there is a big parameter and its coefficient is non-zero,
656 * then this coefficient determines the outcome.
657 * Otherwise, we check whether the constant is negative and
658 * all non-zero coefficients of parameters are negative and
659 * belong to non-negative parameters.
661 static int is_obviously_neg(struct isl_tab *tab, int row)
665 unsigned off = 2 + tab->M;
668 if (isl_int_is_pos(tab->mat->row[row][2]))
670 if (isl_int_is_neg(tab->mat->row[row][2]))
674 if (isl_int_is_nonneg(tab->mat->row[row][1]))
676 for (i = 0; i < tab->n_param; ++i) {
677 /* Eliminated parameter */
678 if (tab->var[i].is_row)
680 col = tab->var[i].index;
681 if (isl_int_is_zero(tab->mat->row[row][off + col]))
683 if (!tab->var[i].is_nonneg)
685 if (isl_int_is_pos(tab->mat->row[row][off + col]))
688 for (i = 0; i < tab->n_div; ++i) {
689 if (tab->var[tab->n_var - tab->n_div + i].is_row)
691 col = tab->var[tab->n_var - tab->n_div + i].index;
692 if (isl_int_is_zero(tab->mat->row[row][off + col]))
694 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
696 if (isl_int_is_pos(tab->mat->row[row][off + col]))
702 /* Check if the (parametric) constant of the given row is obviously
703 * non-negative, meaning that we don't need to consult the context tableau.
704 * If there is a big parameter and its coefficient is non-zero,
705 * then this coefficient determines the outcome.
706 * Otherwise, we check whether the constant is non-negative and
707 * all non-zero coefficients of parameters are positive and
708 * belong to non-negative parameters.
710 static int is_obviously_nonneg(struct isl_tab *tab, int row)
714 unsigned off = 2 + tab->M;
717 if (isl_int_is_pos(tab->mat->row[row][2]))
719 if (isl_int_is_neg(tab->mat->row[row][2]))
723 if (isl_int_is_neg(tab->mat->row[row][1]))
725 for (i = 0; i < tab->n_param; ++i) {
726 /* Eliminated parameter */
727 if (tab->var[i].is_row)
729 col = tab->var[i].index;
730 if (isl_int_is_zero(tab->mat->row[row][off + col]))
732 if (!tab->var[i].is_nonneg)
734 if (isl_int_is_neg(tab->mat->row[row][off + col]))
737 for (i = 0; i < tab->n_div; ++i) {
738 if (tab->var[tab->n_var - tab->n_div + i].is_row)
740 col = tab->var[tab->n_var - tab->n_div + i].index;
741 if (isl_int_is_zero(tab->mat->row[row][off + col]))
743 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
745 if (isl_int_is_neg(tab->mat->row[row][off + col]))
751 /* Given a row r and two columns, return the column that would
752 * lead to the lexicographically smallest increment in the sample
753 * solution when leaving the basis in favor of the row.
754 * Pivoting with column c will increment the sample value by a non-negative
755 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
756 * corresponding to the non-parametric variables.
757 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
758 * with all other entries in this virtual row equal to zero.
759 * If variable v appears in a row, then a_{v,c} is the element in column c
762 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
763 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
764 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
765 * increment. Otherwise, it's c2.
767 static int lexmin_col_pair(struct isl_tab *tab,
768 int row, int col1, int col2, isl_int tmp)
773 tr = tab->mat->row[row] + 2 + tab->M;
775 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
779 if (!tab->var[i].is_row) {
780 if (tab->var[i].index == col1)
782 if (tab->var[i].index == col2)
787 if (tab->var[i].index == row)
790 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
791 s1 = isl_int_sgn(r[col1]);
792 s2 = isl_int_sgn(r[col2]);
793 if (s1 == 0 && s2 == 0)
800 isl_int_mul(tmp, r[col2], tr[col1]);
801 isl_int_submul(tmp, r[col1], tr[col2]);
802 if (isl_int_is_pos(tmp))
804 if (isl_int_is_neg(tmp))
810 /* Given a row in the tableau, find and return the column that would
811 * result in the lexicographically smallest, but positive, increment
812 * in the sample point.
813 * If there is no such column, then return tab->n_col.
814 * If anything goes wrong, return -1.
816 static int lexmin_pivot_col(struct isl_tab *tab, int row)
819 int col = tab->n_col;
823 tr = tab->mat->row[row] + 2 + tab->M;
827 for (j = tab->n_dead; j < tab->n_col; ++j) {
828 if (tab->col_var[j] >= 0 &&
829 (tab->col_var[j] < tab->n_param ||
830 tab->col_var[j] >= tab->n_var - tab->n_div))
833 if (!isl_int_is_pos(tr[j]))
836 if (col == tab->n_col)
839 col = lexmin_col_pair(tab, row, col, j, tmp);
840 isl_assert(tab->mat->ctx, col >= 0, goto error);
850 /* Return the first known violated constraint, i.e., a non-negative
851 * contraint that currently has an either obviously negative value
852 * or a previously determined to be negative value.
854 * If any constraint has a negative coefficient for the big parameter,
855 * if any, then we return one of these first.
857 static int first_neg(struct isl_tab *tab)
862 for (row = tab->n_redundant; row < tab->n_row; ++row) {
863 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
865 if (isl_int_is_neg(tab->mat->row[row][2]))
868 for (row = tab->n_redundant; row < tab->n_row; ++row) {
869 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
872 if (tab->row_sign[row] == 0 &&
873 is_obviously_neg(tab, row))
874 tab->row_sign[row] = isl_tab_row_neg;
875 if (tab->row_sign[row] != isl_tab_row_neg)
877 } else if (!is_obviously_neg(tab, row))
884 /* Resolve all known or obviously violated constraints through pivoting.
885 * In particular, as long as we can find any violated constraint, we
886 * look for a pivoting column that would result in the lexicographicallly
887 * smallest increment in the sample point. If there is no such column
888 * then the tableau is infeasible.
890 static struct isl_tab *restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
891 static struct isl_tab *restore_lexmin(struct isl_tab *tab)
899 while ((row = first_neg(tab)) != -1) {
900 col = lexmin_pivot_col(tab, row);
901 if (col >= tab->n_col)
902 return isl_tab_mark_empty(tab);
905 if (isl_tab_pivot(tab, row, col) < 0)
914 /* Given a row that represents an equality, look for an appropriate
916 * In particular, if there are any non-zero coefficients among
917 * the non-parameter variables, then we take the last of these
918 * variables. Eliminating this variable in terms of the other
919 * variables and/or parameters does not influence the property
920 * that all column in the initial tableau are lexicographically
921 * positive. The row corresponding to the eliminated variable
922 * will only have non-zero entries below the diagonal of the
923 * initial tableau. That is, we transform
929 * If there is no such non-parameter variable, then we are dealing with
930 * pure parameter equality and we pick any parameter with coefficient 1 or -1
931 * for elimination. This will ensure that the eliminated parameter
932 * always has an integer value whenever all the other parameters are integral.
933 * If there is no such parameter then we return -1.
935 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
937 unsigned off = 2 + tab->M;
940 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
942 if (tab->var[i].is_row)
944 col = tab->var[i].index;
945 if (col <= tab->n_dead)
947 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
950 for (i = tab->n_dead; i < tab->n_col; ++i) {
951 if (isl_int_is_one(tab->mat->row[row][off + i]))
953 if (isl_int_is_negone(tab->mat->row[row][off + i]))
959 /* Add an equality that is known to be valid to the tableau.
960 * We first check if we can eliminate a variable or a parameter.
961 * If not, we add the equality as two inequalities.
962 * In this case, the equality was a pure parameter equality and there
963 * is no need to resolve any constraint violations.
965 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
972 r = isl_tab_add_row(tab, eq);
976 r = tab->con[r].index;
977 i = last_var_col_or_int_par_col(tab, r);
979 tab->con[r].is_nonneg = 1;
980 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
982 isl_seq_neg(eq, eq, 1 + tab->n_var);
983 r = isl_tab_add_row(tab, eq);
986 tab->con[r].is_nonneg = 1;
987 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
990 if (isl_tab_pivot(tab, r, i) < 0)
992 if (isl_tab_kill_col(tab, i) < 0)
996 tab = restore_lexmin(tab);
1005 /* Check if the given row is a pure constant.
1007 static int is_constant(struct isl_tab *tab, int row)
1009 unsigned off = 2 + tab->M;
1011 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1012 tab->n_col - tab->n_dead) == -1;
1015 /* Add an equality that may or may not be valid to the tableau.
1016 * If the resulting row is a pure constant, then it must be zero.
1017 * Otherwise, the resulting tableau is empty.
1019 * If the row is not a pure constant, then we add two inequalities,
1020 * each time checking that they can be satisfied.
1021 * In the end we try to use one of the two constraints to eliminate
1024 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1025 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1029 struct isl_tab_undo *snap;
1033 snap = isl_tab_snap(tab);
1034 r1 = isl_tab_add_row(tab, eq);
1037 tab->con[r1].is_nonneg = 1;
1038 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1041 row = tab->con[r1].index;
1042 if (is_constant(tab, row)) {
1043 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1044 (tab->M && !isl_int_is_zero(tab->mat->row[row][2])))
1045 return isl_tab_mark_empty(tab);
1046 if (isl_tab_rollback(tab, snap) < 0)
1051 tab = restore_lexmin(tab);
1052 if (!tab || tab->empty)
1055 isl_seq_neg(eq, eq, 1 + tab->n_var);
1057 r2 = isl_tab_add_row(tab, eq);
1060 tab->con[r2].is_nonneg = 1;
1061 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1064 tab = restore_lexmin(tab);
1065 if (!tab || tab->empty)
1068 if (!tab->con[r1].is_row) {
1069 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1071 } else if (!tab->con[r2].is_row) {
1072 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1074 } else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
1075 unsigned off = 2 + tab->M;
1077 int row = tab->con[r1].index;
1078 i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
1079 tab->n_col - tab->n_dead);
1081 if (isl_tab_pivot(tab, row, tab->n_dead + i) < 0)
1083 if (isl_tab_kill_col(tab, tab->n_dead + i) < 0)
1089 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1090 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1092 isl_seq_neg(eq, eq, 1 + tab->n_var);
1093 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1094 isl_seq_neg(eq, eq, 1 + tab->n_var);
1095 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1107 /* Add an inequality to the tableau, resolving violations using
1110 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1117 tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
1118 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1123 r = isl_tab_add_row(tab, ineq);
1126 tab->con[r].is_nonneg = 1;
1127 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1129 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1130 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1135 tab = restore_lexmin(tab);
1136 if (tab && !tab->empty && tab->con[r].is_row &&
1137 isl_tab_row_is_redundant(tab, tab->con[r].index))
1138 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1146 /* Check if the coefficients of the parameters are all integral.
1148 static int integer_parameter(struct isl_tab *tab, int row)
1152 unsigned off = 2 + tab->M;
1154 for (i = 0; i < tab->n_param; ++i) {
1155 /* Eliminated parameter */
1156 if (tab->var[i].is_row)
1158 col = tab->var[i].index;
1159 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1160 tab->mat->row[row][0]))
1163 for (i = 0; i < tab->n_div; ++i) {
1164 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1166 col = tab->var[tab->n_var - tab->n_div + i].index;
1167 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1168 tab->mat->row[row][0]))
1174 /* Check if the coefficients of the non-parameter variables are all integral.
1176 static int integer_variable(struct isl_tab *tab, int row)
1179 unsigned off = 2 + tab->M;
1181 for (i = 0; i < tab->n_col; ++i) {
1182 if (tab->col_var[i] >= 0 &&
1183 (tab->col_var[i] < tab->n_param ||
1184 tab->col_var[i] >= tab->n_var - tab->n_div))
1186 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1187 tab->mat->row[row][0]))
1193 /* Check if the constant term is integral.
1195 static int integer_constant(struct isl_tab *tab, int row)
1197 return isl_int_is_divisible_by(tab->mat->row[row][1],
1198 tab->mat->row[row][0]);
1201 #define I_CST 1 << 0
1202 #define I_PAR 1 << 1
1203 #define I_VAR 1 << 2
1205 /* Check for first (non-parameter) variable that is non-integer and
1206 * therefore requires a cut.
1207 * For parametric tableaus, there are three parts in a row,
1208 * the constant, the coefficients of the parameters and the rest.
1209 * For each part, we check whether the coefficients in that part
1210 * are all integral and if so, set the corresponding flag in *f.
1211 * If the constant and the parameter part are integral, then the
1212 * current sample value is integral and no cut is required
1213 * (irrespective of whether the variable part is integral).
1215 static int first_non_integer(struct isl_tab *tab, int *f)
1219 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1222 if (!tab->var[i].is_row)
1224 row = tab->var[i].index;
1225 if (integer_constant(tab, row))
1226 ISL_FL_SET(flags, I_CST);
1227 if (integer_parameter(tab, row))
1228 ISL_FL_SET(flags, I_PAR);
1229 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1231 if (integer_variable(tab, row))
1232 ISL_FL_SET(flags, I_VAR);
1239 /* Add a (non-parametric) cut to cut away the non-integral sample
1240 * value of the given row.
1242 * If the row is given by
1244 * m r = f + \sum_i a_i y_i
1248 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1250 * The big parameter, if any, is ignored, since it is assumed to be big
1251 * enough to be divisible by any integer.
1252 * If the tableau is actually a parametric tableau, then this function
1253 * is only called when all coefficients of the parameters are integral.
1254 * The cut therefore has zero coefficients for the parameters.
1256 * The current value is known to be negative, so row_sign, if it
1257 * exists, is set accordingly.
1259 * Return the row of the cut or -1.
1261 static int add_cut(struct isl_tab *tab, int row)
1266 unsigned off = 2 + tab->M;
1268 if (isl_tab_extend_cons(tab, 1) < 0)
1270 r = isl_tab_allocate_con(tab);
1274 r_row = tab->mat->row[tab->con[r].index];
1275 isl_int_set(r_row[0], tab->mat->row[row][0]);
1276 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1277 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1278 isl_int_neg(r_row[1], r_row[1]);
1280 isl_int_set_si(r_row[2], 0);
1281 for (i = 0; i < tab->n_col; ++i)
1282 isl_int_fdiv_r(r_row[off + i],
1283 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1285 tab->con[r].is_nonneg = 1;
1286 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1289 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1291 return tab->con[r].index;
1294 /* Given a non-parametric tableau, add cuts until an integer
1295 * sample point is obtained or until the tableau is determined
1296 * to be integer infeasible.
1297 * As long as there is any non-integer value in the sample point,
1298 * we add an appropriate cut, if possible and resolve the violated
1299 * cut constraint using restore_lexmin.
1300 * If one of the corresponding rows is equal to an integral
1301 * combination of variables/constraints plus a non-integral constant,
1302 * then there is no way to obtain an integer point an we return
1303 * a tableau that is marked empty.
1305 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
1315 while ((row = first_non_integer(tab, &flags)) != -1) {
1316 if (ISL_FL_ISSET(flags, I_VAR))
1317 return isl_tab_mark_empty(tab);
1318 row = add_cut(tab, row);
1321 tab = restore_lexmin(tab);
1322 if (!tab || tab->empty)
1331 /* Check whether all the currently active samples also satisfy the inequality
1332 * "ineq" (treated as an equality if eq is set).
1333 * Remove those samples that do not.
1335 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1343 isl_assert(tab->mat->ctx, tab->bset, goto error);
1344 isl_assert(tab->mat->ctx, tab->samples, goto error);
1345 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1348 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1350 isl_seq_inner_product(ineq, tab->samples->row[i],
1351 1 + tab->n_var, &v);
1352 sgn = isl_int_sgn(v);
1353 if (eq ? (sgn == 0) : (sgn >= 0))
1355 tab = isl_tab_drop_sample(tab, i);
1367 /* Check whether the sample value of the tableau is finite,
1368 * i.e., either the tableau does not use a big parameter, or
1369 * all values of the variables are equal to the big parameter plus
1370 * some constant. This constant is the actual sample value.
1372 static int sample_is_finite(struct isl_tab *tab)
1379 for (i = 0; i < tab->n_var; ++i) {
1381 if (!tab->var[i].is_row)
1383 row = tab->var[i].index;
1384 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1390 /* Check if the context tableau of sol has any integer points.
1391 * Leave tab in empty state if no integer point can be found.
1392 * If an integer point can be found and if moreover it is finite,
1393 * then it is added to the list of sample values.
1395 * This function is only called when none of the currently active sample
1396 * values satisfies the most recently added constraint.
1398 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1400 struct isl_tab_undo *snap;
1406 snap = isl_tab_snap(tab);
1407 if (isl_tab_push_basis(tab) < 0)
1410 tab = cut_to_integer_lexmin(tab);
1414 if (!tab->empty && sample_is_finite(tab)) {
1415 struct isl_vec *sample;
1417 sample = isl_tab_get_sample_value(tab);
1419 tab = isl_tab_add_sample(tab, sample);
1422 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1431 /* Check if any of the currently active sample values satisfies
1432 * the inequality "ineq" (an equality if eq is set).
1434 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1442 isl_assert(tab->mat->ctx, tab->bset, return -1);
1443 isl_assert(tab->mat->ctx, tab->samples, return -1);
1444 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1447 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1449 isl_seq_inner_product(ineq, tab->samples->row[i],
1450 1 + tab->n_var, &v);
1451 sgn = isl_int_sgn(v);
1452 if (eq ? (sgn == 0) : (sgn >= 0))
1457 return i < tab->n_sample;
1460 /* For a div d = floor(f/m), add the constraints
1463 * -(f-(m-1)) + m d >= 0
1465 * Note that the second constraint is the negation of
1469 static void add_div_constraints(struct isl_context *context, unsigned div)
1473 struct isl_vec *ineq;
1474 struct isl_basic_set *bset;
1476 bset = context->op->peek_basic_set(context);
1480 total = isl_basic_set_total_dim(bset);
1481 div_pos = 1 + total - bset->n_div + div;
1483 ineq = ineq_for_div(bset, div);
1487 context->op->add_ineq(context, ineq->el, 0, 0);
1489 isl_seq_neg(ineq->el, bset->div[div] + 1, 1 + total);
1490 isl_int_set(ineq->el[div_pos], bset->div[div][0]);
1491 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
1492 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
1494 context->op->add_ineq(context, ineq->el, 0, 0);
1500 context->op->invalidate(context);
1503 /* Add a div specifed by "div" to the tableau "tab" and return
1504 * the index of the new div. *nonneg is set to 1 if the div
1505 * is obviously non-negative.
1507 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1513 struct isl_mat *samples;
1515 for (i = 0; i < tab->n_var; ++i) {
1516 if (isl_int_is_zero(div->el[2 + i]))
1518 if (!tab->var[i].is_nonneg)
1521 *nonneg = i == tab->n_var;
1523 if (isl_tab_extend_cons(tab, 3) < 0)
1525 if (isl_tab_extend_vars(tab, 1) < 0)
1527 r = isl_tab_allocate_var(tab);
1531 tab->var[r].is_nonneg = 1;
1532 tab->var[r].frozen = 1;
1534 samples = isl_mat_extend(tab->samples,
1535 tab->n_sample, 1 + tab->n_var);
1536 tab->samples = samples;
1539 for (i = tab->n_outside; i < samples->n_row; ++i) {
1540 isl_seq_inner_product(div->el + 1, samples->row[i],
1541 div->size - 1, &samples->row[i][samples->n_col - 1]);
1542 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1543 samples->row[i][samples->n_col - 1], div->el[0]);
1546 tab->bset = isl_basic_set_extend_dim(tab->bset,
1547 isl_basic_set_get_dim(tab->bset), 1, 0, 2);
1548 k = isl_basic_set_alloc_div(tab->bset);
1551 isl_seq_cpy(tab->bset->div[k], div->el, div->size);
1552 if (isl_tab_push(tab, isl_tab_undo_bset_div) < 0)
1558 /* Add a div specified by "div" to both the main tableau and
1559 * the context tableau. In case of the main tableau, we only
1560 * need to add an extra div. In the context tableau, we also
1561 * need to express the meaning of the div.
1562 * Return the index of the div or -1 if anything went wrong.
1564 static int add_div(struct isl_tab *tab, struct isl_context *context,
1565 struct isl_vec *div)
1571 k = context->op->add_div(context, div, &nonneg);
1575 add_div_constraints(context, k);
1576 if (!context->op->is_ok(context))
1579 if (isl_tab_extend_vars(tab, 1) < 0)
1581 r = isl_tab_allocate_var(tab);
1585 tab->var[r].is_nonneg = 1;
1586 tab->var[r].frozen = 1;
1589 return tab->n_div - 1;
1591 context->op->invalidate(context);
1595 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1598 unsigned total = isl_basic_set_total_dim(tab->bset);
1600 for (i = 0; i < tab->bset->n_div; ++i) {
1601 if (isl_int_ne(tab->bset->div[i][0], denom))
1603 if (!isl_seq_eq(tab->bset->div[i] + 1, div, total))
1610 /* Return the index of a div that corresponds to "div".
1611 * We first check if we already have such a div and if not, we create one.
1613 static int get_div(struct isl_tab *tab, struct isl_context *context,
1614 struct isl_vec *div)
1617 struct isl_tab *context_tab = context->op->peek_tab(context);
1622 d = find_div(context_tab, div->el + 1, div->el[0]);
1626 return add_div(tab, context, div);
1629 /* Add a parametric cut to cut away the non-integral sample value
1631 * Let a_i be the coefficients of the constant term and the parameters
1632 * and let b_i be the coefficients of the variables or constraints
1633 * in basis of the tableau.
1634 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1636 * The cut is expressed as
1638 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1640 * If q did not already exist in the context tableau, then it is added first.
1641 * If q is in a column of the main tableau then the "+ q" can be accomplished
1642 * by setting the corresponding entry to the denominator of the constraint.
1643 * If q happens to be in a row of the main tableau, then the corresponding
1644 * row needs to be added instead (taking care of the denominators).
1645 * Note that this is very unlikely, but perhaps not entirely impossible.
1647 * The current value of the cut is known to be negative (or at least
1648 * non-positive), so row_sign is set accordingly.
1650 * Return the row of the cut or -1.
1652 static int add_parametric_cut(struct isl_tab *tab, int row,
1653 struct isl_context *context)
1655 struct isl_vec *div;
1662 unsigned off = 2 + tab->M;
1667 div = get_row_parameter_div(tab, row);
1672 d = context->op->get_div(context, tab, div);
1676 if (isl_tab_extend_cons(tab, 1) < 0)
1678 r = isl_tab_allocate_con(tab);
1682 r_row = tab->mat->row[tab->con[r].index];
1683 isl_int_set(r_row[0], tab->mat->row[row][0]);
1684 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1685 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1686 isl_int_neg(r_row[1], r_row[1]);
1688 isl_int_set_si(r_row[2], 0);
1689 for (i = 0; i < tab->n_param; ++i) {
1690 if (tab->var[i].is_row)
1692 col = tab->var[i].index;
1693 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1694 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1695 tab->mat->row[row][0]);
1696 isl_int_neg(r_row[off + col], r_row[off + col]);
1698 for (i = 0; i < tab->n_div; ++i) {
1699 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1701 col = tab->var[tab->n_var - tab->n_div + i].index;
1702 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1703 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1704 tab->mat->row[row][0]);
1705 isl_int_neg(r_row[off + col], r_row[off + col]);
1707 for (i = 0; i < tab->n_col; ++i) {
1708 if (tab->col_var[i] >= 0 &&
1709 (tab->col_var[i] < tab->n_param ||
1710 tab->col_var[i] >= tab->n_var - tab->n_div))
1712 isl_int_fdiv_r(r_row[off + i],
1713 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1715 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1717 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1719 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1720 isl_int_divexact(r_row[0], r_row[0], gcd);
1721 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1722 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1723 r_row[0], tab->mat->row[d_row] + 1,
1724 off - 1 + tab->n_col);
1725 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1728 col = tab->var[tab->n_var - tab->n_div + d].index;
1729 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1732 tab->con[r].is_nonneg = 1;
1733 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1736 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1740 row = tab->con[r].index;
1742 if (d >= n && context->op->detect_equalities(context, tab) < 0)
1748 /* Construct a tableau for bmap that can be used for computing
1749 * the lexicographic minimum (or maximum) of bmap.
1750 * If not NULL, then dom is the domain where the minimum
1751 * should be computed. In this case, we set up a parametric
1752 * tableau with row signs (initialized to "unknown").
1753 * If M is set, then the tableau will use a big parameter.
1754 * If max is set, then a maximum should be computed instead of a minimum.
1755 * This means that for each variable x, the tableau will contain the variable
1756 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1757 * of the variables in all constraints are negated prior to adding them
1760 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
1761 struct isl_basic_set *dom, unsigned M, int max)
1764 struct isl_tab *tab;
1766 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
1767 isl_basic_map_total_dim(bmap), M);
1771 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1773 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
1774 tab->n_div = dom->n_div;
1775 tab->row_sign = isl_calloc_array(bmap->ctx,
1776 enum isl_tab_row_sign, tab->mat->n_row);
1780 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1781 return isl_tab_mark_empty(tab);
1783 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1784 tab->var[i].is_nonneg = 1;
1785 tab->var[i].frozen = 1;
1787 for (i = 0; i < bmap->n_eq; ++i) {
1789 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1790 bmap->eq[i] + 1 + tab->n_param,
1791 tab->n_var - tab->n_param - tab->n_div);
1792 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
1794 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1795 bmap->eq[i] + 1 + tab->n_param,
1796 tab->n_var - tab->n_param - tab->n_div);
1797 if (!tab || tab->empty)
1800 for (i = 0; i < bmap->n_ineq; ++i) {
1802 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1803 bmap->ineq[i] + 1 + tab->n_param,
1804 tab->n_var - tab->n_param - tab->n_div);
1805 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
1807 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1808 bmap->ineq[i] + 1 + tab->n_param,
1809 tab->n_var - tab->n_param - tab->n_div);
1810 if (!tab || tab->empty)
1819 /* Given a main tableau where more than one row requires a split,
1820 * determine and return the "best" row to split on.
1822 * Given two rows in the main tableau, if the inequality corresponding
1823 * to the first row is redundant with respect to that of the second row
1824 * in the current tableau, then it is better to split on the second row,
1825 * since in the positive part, both row will be positive.
1826 * (In the negative part a pivot will have to be performed and just about
1827 * anything can happen to the sign of the other row.)
1829 * As a simple heuristic, we therefore select the row that makes the most
1830 * of the other rows redundant.
1832 * Perhaps it would also be useful to look at the number of constraints
1833 * that conflict with any given constraint.
1835 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
1837 struct isl_tab_undo *snap;
1843 if (isl_tab_extend_cons(context_tab, 2) < 0)
1846 snap = isl_tab_snap(context_tab);
1848 for (split = tab->n_redundant; split < tab->n_row; ++split) {
1849 struct isl_tab_undo *snap2;
1850 struct isl_vec *ineq = NULL;
1853 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
1855 if (tab->row_sign[split] != isl_tab_row_any)
1858 ineq = get_row_parameter_ineq(tab, split);
1861 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
1864 snap2 = isl_tab_snap(context_tab);
1866 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1867 struct isl_tab_var *var;
1871 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1873 if (tab->row_sign[row] != isl_tab_row_any)
1876 ineq = get_row_parameter_ineq(tab, row);
1879 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
1881 var = &context_tab->con[context_tab->n_con - 1];
1882 if (!context_tab->empty &&
1883 !isl_tab_min_at_most_neg_one(context_tab, var))
1885 if (isl_tab_rollback(context_tab, snap2) < 0)
1888 if (best == -1 || r > best_r) {
1892 if (isl_tab_rollback(context_tab, snap) < 0)
1899 static struct isl_basic_set *context_lex_peek_basic_set(
1900 struct isl_context *context)
1902 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1905 return clex->tab->bset;
1908 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
1910 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1914 static void context_lex_extend(struct isl_context *context, int n)
1916 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1919 if (isl_tab_extend_cons(clex->tab, n) >= 0)
1921 isl_tab_free(clex->tab);
1925 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
1926 int check, int update)
1928 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1929 if (isl_tab_extend_cons(clex->tab, 2) < 0)
1931 clex->tab = add_lexmin_eq(clex->tab, eq);
1933 int v = tab_has_valid_sample(clex->tab, eq, 1);
1937 clex->tab = check_integer_feasible(clex->tab);
1940 clex->tab = check_samples(clex->tab, eq, 1);
1943 isl_tab_free(clex->tab);
1947 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
1948 int check, int update)
1950 struct isl_context_lex *clex = (struct isl_context_lex *)context;
1951 if (isl_tab_extend_cons(clex->tab, 1) < 0)
1953 clex->tab = add_lexmin_ineq(clex->tab, ineq);
1955 int v = tab_has_valid_sample(clex->tab, ineq, 0);
1959 clex->tab = check_integer_feasible(clex->tab);
1962 clex->tab = check_samples(clex->tab, ineq, 0);
1965 isl_tab_free(clex->tab);
1969 /* Check which signs can be obtained by "ineq" on all the currently
1970 * active sample values. See row_sign for more information.
1972 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
1978 int res = isl_tab_row_unknown;
1980 isl_assert(tab->mat->ctx, tab->samples, return 0);
1981 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return 0);
1984 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1985 isl_seq_inner_product(tab->samples->row[i], ineq,
1986 1 + tab->n_var, &tmp);
1987 sgn = isl_int_sgn(tmp);
1988 if (sgn > 0 || (sgn == 0 && strict)) {
1989 if (res == isl_tab_row_unknown)
1990 res = isl_tab_row_pos;
1991 if (res == isl_tab_row_neg)
1992 res = isl_tab_row_any;
1995 if (res == isl_tab_row_unknown)
1996 res = isl_tab_row_neg;
1997 if (res == isl_tab_row_pos)
1998 res = isl_tab_row_any;
2000 if (res == isl_tab_row_any)
2008 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2009 isl_int *ineq, int strict)
2011 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2012 return tab_ineq_sign(clex->tab, ineq, strict);
2015 /* Check whether "ineq" can be added to the tableau without rendering
2018 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2020 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2021 struct isl_tab_undo *snap;
2027 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2030 snap = isl_tab_snap(clex->tab);
2031 if (isl_tab_push_basis(clex->tab) < 0)
2033 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2034 clex->tab = check_integer_feasible(clex->tab);
2037 feasible = !clex->tab->empty;
2038 if (isl_tab_rollback(clex->tab, snap) < 0)
2044 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2045 struct isl_vec *div)
2047 return get_div(tab, context, div);
2050 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div,
2053 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2054 return context_tab_add_div(clex->tab, div, nonneg);
2057 static int context_lex_detect_equalities(struct isl_context *context,
2058 struct isl_tab *tab)
2063 static int context_lex_best_split(struct isl_context *context,
2064 struct isl_tab *tab)
2066 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2067 struct isl_tab_undo *snap;
2070 snap = isl_tab_snap(clex->tab);
2071 if (isl_tab_push_basis(clex->tab) < 0)
2073 r = best_split(tab, clex->tab);
2075 if (isl_tab_rollback(clex->tab, snap) < 0)
2081 static int context_lex_is_empty(struct isl_context *context)
2083 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2086 return clex->tab->empty;
2089 static void *context_lex_save(struct isl_context *context)
2091 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2092 struct isl_tab_undo *snap;
2094 snap = isl_tab_snap(clex->tab);
2095 if (isl_tab_push_basis(clex->tab) < 0)
2097 if (isl_tab_save_samples(clex->tab) < 0)
2103 static void context_lex_restore(struct isl_context *context, void *save)
2105 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2106 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2107 isl_tab_free(clex->tab);
2112 static int context_lex_is_ok(struct isl_context *context)
2114 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2118 /* For each variable in the context tableau, check if the variable can
2119 * only attain non-negative values. If so, mark the parameter as non-negative
2120 * in the main tableau. This allows for a more direct identification of some
2121 * cases of violated constraints.
2123 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2124 struct isl_tab *context_tab)
2127 struct isl_tab_undo *snap;
2128 struct isl_vec *ineq = NULL;
2129 struct isl_tab_var *var;
2132 if (context_tab->n_var == 0)
2135 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2139 if (isl_tab_extend_cons(context_tab, 1) < 0)
2142 snap = isl_tab_snap(context_tab);
2145 isl_seq_clr(ineq->el, ineq->size);
2146 for (i = 0; i < context_tab->n_var; ++i) {
2147 isl_int_set_si(ineq->el[1 + i], 1);
2148 context_tab = isl_tab_add_ineq(context_tab, ineq->el);
2149 var = &context_tab->con[context_tab->n_con - 1];
2150 if (!context_tab->empty &&
2151 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2153 if (i >= tab->n_param)
2154 j = i - tab->n_param + tab->n_var - tab->n_div;
2155 tab->var[j].is_nonneg = 1;
2158 isl_int_set_si(ineq->el[1 + i], 0);
2159 if (isl_tab_rollback(context_tab, snap) < 0)
2163 if (context_tab->M && n == context_tab->n_var) {
2164 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2176 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2177 struct isl_context *context, struct isl_tab *tab)
2179 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2180 struct isl_tab_undo *snap;
2182 snap = isl_tab_snap(clex->tab);
2183 if (isl_tab_push_basis(clex->tab) < 0)
2186 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2188 if (isl_tab_rollback(clex->tab, snap) < 0)
2197 static void context_lex_invalidate(struct isl_context *context)
2199 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2200 isl_tab_free(clex->tab);
2204 static void context_lex_free(struct isl_context *context)
2206 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2207 isl_tab_free(clex->tab);
2211 struct isl_context_op isl_context_lex_op = {
2212 context_lex_detect_nonnegative_parameters,
2213 context_lex_peek_basic_set,
2214 context_lex_peek_tab,
2216 context_lex_add_ineq,
2217 context_lex_ineq_sign,
2218 context_lex_test_ineq,
2219 context_lex_get_div,
2220 context_lex_add_div,
2221 context_lex_detect_equalities,
2222 context_lex_best_split,
2223 context_lex_is_empty,
2226 context_lex_restore,
2227 context_lex_invalidate,
2231 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2233 struct isl_tab *tab;
2235 bset = isl_basic_set_cow(bset);
2238 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2242 tab = isl_tab_init_samples(tab);
2245 isl_basic_set_free(bset);
2249 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2251 struct isl_context_lex *clex;
2256 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2260 clex->context.op = &isl_context_lex_op;
2262 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2263 clex->tab = restore_lexmin(clex->tab);
2264 clex->tab = check_integer_feasible(clex->tab);
2268 return &clex->context;
2270 clex->context.op->free(&clex->context);
2274 struct isl_context_gbr {
2275 struct isl_context context;
2276 struct isl_tab *tab;
2277 struct isl_tab *shifted;
2278 struct isl_tab *cone;
2281 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2282 struct isl_context *context, struct isl_tab *tab)
2284 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2285 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2288 static struct isl_basic_set *context_gbr_peek_basic_set(
2289 struct isl_context *context)
2291 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2294 return cgbr->tab->bset;
2297 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2299 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2303 /* Initialize the "shifted" tableau of the context, which
2304 * contains the constraints of the original tableau shifted
2305 * by the sum of all negative coefficients. This ensures
2306 * that any rational point in the shifted tableau can
2307 * be rounded up to yield an integer point in the original tableau.
2309 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2312 struct isl_vec *cst;
2313 struct isl_basic_set *bset = cgbr->tab->bset;
2314 unsigned dim = isl_basic_set_total_dim(bset);
2316 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2320 for (i = 0; i < bset->n_ineq; ++i) {
2321 isl_int_set(cst->el[i], bset->ineq[i][0]);
2322 for (j = 0; j < dim; ++j) {
2323 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2325 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2326 bset->ineq[i][1 + j]);
2330 cgbr->shifted = isl_tab_from_basic_set(bset);
2332 for (i = 0; i < bset->n_ineq; ++i)
2333 isl_int_set(bset->ineq[i][0], cst->el[i]);
2338 /* Check if the shifted tableau is non-empty, and if so
2339 * use the sample point to construct an integer point
2340 * of the context tableau.
2342 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2344 struct isl_vec *sample;
2347 gbr_init_shifted(cgbr);
2350 if (cgbr->shifted->empty)
2351 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2353 sample = isl_tab_get_sample_value(cgbr->shifted);
2354 sample = isl_vec_ceil(sample);
2359 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2366 for (i = 0; i < bset->n_eq; ++i)
2367 isl_int_set_si(bset->eq[i][0], 0);
2369 for (i = 0; i < bset->n_ineq; ++i)
2370 isl_int_set_si(bset->ineq[i][0], 0);
2375 static int use_shifted(struct isl_context_gbr *cgbr)
2377 return cgbr->tab->bset->n_eq == 0 && cgbr->tab->bset->n_div == 0;
2380 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2382 struct isl_basic_set *bset;
2383 struct isl_basic_set *cone;
2385 if (isl_tab_sample_is_integer(cgbr->tab))
2386 return isl_tab_get_sample_value(cgbr->tab);
2388 if (use_shifted(cgbr)) {
2389 struct isl_vec *sample;
2391 sample = gbr_get_shifted_sample(cgbr);
2392 if (!sample || sample->size > 0)
2395 isl_vec_free(sample);
2399 cgbr->cone = isl_tab_from_recession_cone(cgbr->tab->bset);
2402 cgbr->cone->bset = isl_basic_set_dup(cgbr->tab->bset);
2404 cgbr->cone = isl_tab_detect_implicit_equalities(cgbr->cone);
2408 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2409 struct isl_vec *sample;
2410 struct isl_tab_undo *snap;
2412 if (cgbr->tab->basis) {
2413 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2414 isl_mat_free(cgbr->tab->basis);
2415 cgbr->tab->basis = NULL;
2417 cgbr->tab->n_zero = 0;
2418 cgbr->tab->n_unbounded = 0;
2422 snap = isl_tab_snap(cgbr->tab);
2424 sample = isl_tab_sample(cgbr->tab);
2426 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2427 isl_vec_free(sample);
2434 cone = isl_basic_set_dup(cgbr->cone->bset);
2435 cone = drop_constant_terms(cone);
2436 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2437 cone = isl_basic_set_underlying_set(cone);
2438 cone = isl_basic_set_gauss(cone, NULL);
2440 bset = isl_basic_set_dup(cgbr->tab->bset);
2441 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2442 bset = isl_basic_set_underlying_set(bset);
2443 bset = isl_basic_set_gauss(bset, NULL);
2445 return isl_basic_set_sample_with_cone(bset, cone);
2448 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2450 struct isl_vec *sample;
2455 if (cgbr->tab->empty)
2458 sample = gbr_get_sample(cgbr);
2462 if (sample->size == 0) {
2463 isl_vec_free(sample);
2464 cgbr->tab = isl_tab_mark_empty(cgbr->tab);
2468 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2472 isl_tab_free(cgbr->tab);
2476 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2483 if (isl_tab_extend_cons(tab, 2) < 0)
2486 tab = isl_tab_add_eq(tab, eq);
2494 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2495 int check, int update)
2497 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2499 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2501 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2502 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2504 cgbr->cone = isl_tab_add_eq(cgbr->cone, eq);
2508 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2512 check_gbr_integer_feasible(cgbr);
2515 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2518 isl_tab_free(cgbr->tab);
2522 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2527 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2530 cgbr->tab = isl_tab_add_ineq(cgbr->tab, ineq);
2532 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2535 dim = isl_basic_set_total_dim(cgbr->tab->bset);
2537 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2540 for (i = 0; i < dim; ++i) {
2541 if (!isl_int_is_neg(ineq[1 + i]))
2543 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2546 cgbr->shifted = isl_tab_add_ineq(cgbr->shifted, ineq);
2548 for (i = 0; i < dim; ++i) {
2549 if (!isl_int_is_neg(ineq[1 + i]))
2551 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2555 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2556 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2558 cgbr->cone = isl_tab_add_ineq(cgbr->cone, ineq);
2563 isl_tab_free(cgbr->tab);
2567 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2568 int check, int update)
2570 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2572 add_gbr_ineq(cgbr, ineq);
2577 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2581 check_gbr_integer_feasible(cgbr);
2584 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2587 isl_tab_free(cgbr->tab);
2591 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2592 isl_int *ineq, int strict)
2594 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2595 return tab_ineq_sign(cgbr->tab, ineq, strict);
2598 /* Check whether "ineq" can be added to the tableau without rendering
2601 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2603 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2604 struct isl_tab_undo *snap;
2605 struct isl_tab_undo *shifted_snap = NULL;
2606 struct isl_tab_undo *cone_snap = NULL;
2612 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2615 snap = isl_tab_snap(cgbr->tab);
2617 shifted_snap = isl_tab_snap(cgbr->shifted);
2619 cone_snap = isl_tab_snap(cgbr->cone);
2620 add_gbr_ineq(cgbr, ineq);
2621 check_gbr_integer_feasible(cgbr);
2624 feasible = !cgbr->tab->empty;
2625 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2628 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2630 } else if (cgbr->shifted) {
2631 isl_tab_free(cgbr->shifted);
2632 cgbr->shifted = NULL;
2635 if (isl_tab_rollback(cgbr->cone, cone_snap))
2637 } else if (cgbr->cone) {
2638 isl_tab_free(cgbr->cone);
2645 /* Return the column of the last of the variables associated to
2646 * a column that has a non-zero coefficient.
2647 * This function is called in a context where only coefficients
2648 * of parameters or divs can be non-zero.
2650 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
2654 unsigned dim = tab->n_var - tab->n_param - tab->n_div;
2656 if (tab->n_var == 0)
2659 for (i = tab->n_var - 1; i >= 0; --i) {
2660 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
2662 if (tab->var[i].is_row)
2664 col = tab->var[i].index;
2665 if (!isl_int_is_zero(p[col]))
2672 /* Look through all the recently added equalities in the context
2673 * to see if we can propagate any of them to the main tableau.
2675 * The newly added equalities in the context are encoded as pairs
2676 * of inequalities starting at inequality "first".
2678 * We tentatively add each of these equalities to the main tableau
2679 * and if this happens to result in a row with a final coefficient
2680 * that is one or negative one, we use it to kill a column
2681 * in the main tableau. Otherwise, we discard the tentatively
2684 static void propagate_equalities(struct isl_context_gbr *cgbr,
2685 struct isl_tab *tab, unsigned first)
2688 struct isl_vec *eq = NULL;
2690 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2694 if (isl_tab_extend_cons(tab, (cgbr->tab->bset->n_ineq - first)/2) < 0)
2697 isl_seq_clr(eq->el + 1 + tab->n_param,
2698 tab->n_var - tab->n_param - tab->n_div);
2699 for (i = first; i < cgbr->tab->bset->n_ineq; i += 2) {
2702 struct isl_tab_undo *snap;
2703 snap = isl_tab_snap(tab);
2705 isl_seq_cpy(eq->el, cgbr->tab->bset->ineq[i], 1 + tab->n_param);
2706 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
2707 cgbr->tab->bset->ineq[i] + 1 + tab->n_param,
2710 r = isl_tab_add_row(tab, eq->el);
2713 r = tab->con[r].index;
2714 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
2715 if (j < 0 || j < tab->n_dead ||
2716 !isl_int_is_one(tab->mat->row[r][0]) ||
2717 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
2718 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
2719 if (isl_tab_rollback(tab, snap) < 0)
2723 if (isl_tab_pivot(tab, r, j) < 0)
2725 if (isl_tab_kill_col(tab, j) < 0)
2728 tab = restore_lexmin(tab);
2736 isl_tab_free(cgbr->tab);
2740 static int context_gbr_detect_equalities(struct isl_context *context,
2741 struct isl_tab *tab)
2743 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2744 struct isl_ctx *ctx;
2746 enum isl_lp_result res;
2749 ctx = cgbr->tab->mat->ctx;
2752 cgbr->cone = isl_tab_from_recession_cone(cgbr->tab->bset);
2755 cgbr->cone->bset = isl_basic_set_dup(cgbr->tab->bset);
2757 cgbr->cone = isl_tab_detect_implicit_equalities(cgbr->cone);
2759 n_ineq = cgbr->tab->bset->n_ineq;
2760 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
2761 if (cgbr->tab && cgbr->tab->bset->n_ineq > n_ineq)
2762 propagate_equalities(cgbr, tab, n_ineq);
2766 isl_tab_free(cgbr->tab);
2771 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
2772 struct isl_vec *div)
2774 return get_div(tab, context, div);
2777 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div,
2780 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2784 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
2786 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
2788 if (isl_tab_allocate_var(cgbr->cone) <0)
2791 cgbr->cone->bset = isl_basic_set_extend_dim(cgbr->cone->bset,
2792 isl_basic_set_get_dim(cgbr->cone->bset), 1, 0, 2);
2793 k = isl_basic_set_alloc_div(cgbr->cone->bset);
2796 isl_seq_cpy(cgbr->cone->bset->div[k], div->el, div->size);
2797 if (isl_tab_push(cgbr->cone, isl_tab_undo_bset_div) < 0)
2800 return context_tab_add_div(cgbr->tab, div, nonneg);
2803 static int context_gbr_best_split(struct isl_context *context,
2804 struct isl_tab *tab)
2806 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2807 struct isl_tab_undo *snap;
2810 snap = isl_tab_snap(cgbr->tab);
2811 r = best_split(tab, cgbr->tab);
2813 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2819 static int context_gbr_is_empty(struct isl_context *context)
2821 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2824 return cgbr->tab->empty;
2827 struct isl_gbr_tab_undo {
2828 struct isl_tab_undo *tab_snap;
2829 struct isl_tab_undo *shifted_snap;
2830 struct isl_tab_undo *cone_snap;
2833 static void *context_gbr_save(struct isl_context *context)
2835 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2836 struct isl_gbr_tab_undo *snap;
2838 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
2842 snap->tab_snap = isl_tab_snap(cgbr->tab);
2843 if (isl_tab_save_samples(cgbr->tab) < 0)
2847 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
2849 snap->shifted_snap = NULL;
2852 snap->cone_snap = isl_tab_snap(cgbr->cone);
2854 snap->cone_snap = NULL;
2862 static void context_gbr_restore(struct isl_context *context, void *save)
2864 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2865 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
2868 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
2869 isl_tab_free(cgbr->tab);
2873 if (snap->shifted_snap) {
2874 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
2876 } else if (cgbr->shifted) {
2877 isl_tab_free(cgbr->shifted);
2878 cgbr->shifted = NULL;
2881 if (snap->cone_snap) {
2882 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
2884 } else if (cgbr->cone) {
2885 isl_tab_free(cgbr->cone);
2894 isl_tab_free(cgbr->tab);
2898 static int context_gbr_is_ok(struct isl_context *context)
2900 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2904 static void context_gbr_invalidate(struct isl_context *context)
2906 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2907 isl_tab_free(cgbr->tab);
2911 static void context_gbr_free(struct isl_context *context)
2913 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2914 isl_tab_free(cgbr->tab);
2915 isl_tab_free(cgbr->shifted);
2916 isl_tab_free(cgbr->cone);
2920 struct isl_context_op isl_context_gbr_op = {
2921 context_gbr_detect_nonnegative_parameters,
2922 context_gbr_peek_basic_set,
2923 context_gbr_peek_tab,
2925 context_gbr_add_ineq,
2926 context_gbr_ineq_sign,
2927 context_gbr_test_ineq,
2928 context_gbr_get_div,
2929 context_gbr_add_div,
2930 context_gbr_detect_equalities,
2931 context_gbr_best_split,
2932 context_gbr_is_empty,
2935 context_gbr_restore,
2936 context_gbr_invalidate,
2940 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
2942 struct isl_context_gbr *cgbr;
2947 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
2951 cgbr->context.op = &isl_context_gbr_op;
2953 cgbr->shifted = NULL;
2955 cgbr->tab = isl_tab_from_basic_set(dom);
2956 cgbr->tab = isl_tab_init_samples(cgbr->tab);
2959 cgbr->tab->bset = isl_basic_set_cow(isl_basic_set_copy(dom));
2960 if (!cgbr->tab->bset)
2962 check_gbr_integer_feasible(cgbr);
2964 return &cgbr->context;
2966 cgbr->context.op->free(&cgbr->context);
2970 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
2975 if (dom->ctx->context == ISL_CONTEXT_LEXMIN)
2976 return isl_context_lex_alloc(dom);
2978 return isl_context_gbr_alloc(dom);
2981 /* Construct an isl_sol_map structure for accumulating the solution.
2982 * If track_empty is set, then we also keep track of the parts
2983 * of the context where there is no solution.
2984 * If max is set, then we are solving a maximization, rather than
2985 * a minimization problem, which means that the variables in the
2986 * tableau have value "M - x" rather than "M + x".
2988 static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
2989 struct isl_basic_set *dom, int track_empty, int max)
2991 struct isl_sol_map *sol_map;
2993 sol_map = isl_calloc_type(bset->ctx, struct isl_sol_map);
2997 sol_map->sol.max = max;
2998 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
2999 sol_map->sol.add = &sol_map_add_wrap;
3000 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3001 sol_map->sol.free = &sol_map_free_wrap;
3002 sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
3007 sol_map->sol.context = isl_context_alloc(dom);
3008 if (!sol_map->sol.context)
3012 sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
3013 1, ISL_SET_DISJOINT);
3014 if (!sol_map->empty)
3018 isl_basic_set_free(dom);
3021 isl_basic_set_free(dom);
3022 sol_map_free(sol_map);
3026 /* Check whether all coefficients of (non-parameter) variables
3027 * are non-positive, meaning that no pivots can be performed on the row.
3029 static int is_critical(struct isl_tab *tab, int row)
3032 unsigned off = 2 + tab->M;
3034 for (j = tab->n_dead; j < tab->n_col; ++j) {
3035 if (tab->col_var[j] >= 0 &&
3036 (tab->col_var[j] < tab->n_param ||
3037 tab->col_var[j] >= tab->n_var - tab->n_div))
3040 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3047 /* Check whether the inequality represented by vec is strict over the integers,
3048 * i.e., there are no integer values satisfying the constraint with
3049 * equality. This happens if the gcd of the coefficients is not a divisor
3050 * of the constant term. If so, scale the constraint down by the gcd
3051 * of the coefficients.
3053 static int is_strict(struct isl_vec *vec)
3059 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3060 if (!isl_int_is_one(gcd)) {
3061 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3062 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3063 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3070 /* Determine the sign of the given row of the main tableau.
3071 * The result is one of
3072 * isl_tab_row_pos: always non-negative; no pivot needed
3073 * isl_tab_row_neg: always non-positive; pivot
3074 * isl_tab_row_any: can be both positive and negative; split
3076 * We first handle some simple cases
3077 * - the row sign may be known already
3078 * - the row may be obviously non-negative
3079 * - the parametric constant may be equal to that of another row
3080 * for which we know the sign. This sign will be either "pos" or
3081 * "any". If it had been "neg" then we would have pivoted before.
3083 * If none of these cases hold, we check the value of the row for each
3084 * of the currently active samples. Based on the signs of these values
3085 * we make an initial determination of the sign of the row.
3087 * all zero -> unk(nown)
3088 * all non-negative -> pos
3089 * all non-positive -> neg
3090 * both negative and positive -> all
3092 * If we end up with "all", we are done.
3093 * Otherwise, we perform a check for positive and/or negative
3094 * values as follows.
3096 * samples neg unk pos
3102 * There is no special sign for "zero", because we can usually treat zero
3103 * as either non-negative or non-positive, whatever works out best.
3104 * However, if the row is "critical", meaning that pivoting is impossible
3105 * then we don't want to limp zero with the non-positive case, because
3106 * then we we would lose the solution for those values of the parameters
3107 * where the value of the row is zero. Instead, we treat 0 as non-negative
3108 * ensuring a split if the row can attain both zero and negative values.
3109 * The same happens when the original constraint was one that could not
3110 * be satisfied with equality by any integer values of the parameters.
3111 * In this case, we normalize the constraint, but then a value of zero
3112 * for the normalized constraint is actually a positive value for the
3113 * original constraint, so again we need to treat zero as non-negative.
3114 * In both these cases, we have the following decision tree instead:
3116 * all non-negative -> pos
3117 * all negative -> neg
3118 * both negative and non-negative -> all
3126 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3127 struct isl_sol *sol, int row)
3129 struct isl_vec *ineq = NULL;
3130 int res = isl_tab_row_unknown;
3135 if (tab->row_sign[row] != isl_tab_row_unknown)
3136 return tab->row_sign[row];
3137 if (is_obviously_nonneg(tab, row))
3138 return isl_tab_row_pos;
3139 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3140 if (tab->row_sign[row2] == isl_tab_row_unknown)
3142 if (identical_parameter_line(tab, row, row2))
3143 return tab->row_sign[row2];
3146 critical = is_critical(tab, row);
3148 ineq = get_row_parameter_ineq(tab, row);
3152 strict = is_strict(ineq);
3154 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3155 critical || strict);
3157 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3158 /* test for negative values */
3160 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3161 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3163 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3167 res = isl_tab_row_pos;
3169 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3171 if (res == isl_tab_row_neg) {
3172 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3173 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3177 if (res == isl_tab_row_neg) {
3178 /* test for positive values */
3180 if (!critical && !strict)
3181 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3183 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3187 res = isl_tab_row_any;
3197 static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3199 /* Find solutions for values of the parameters that satisfy the given
3202 * We currently take a snapshot of the context tableau that is reset
3203 * when we return from this function, while we make a copy of the main
3204 * tableau, leaving the original main tableau untouched.
3205 * These are fairly arbitrary choices. Making a copy also of the context
3206 * tableau would obviate the need to undo any changes made to it later,
3207 * while taking a snapshot of the main tableau could reduce memory usage.
3208 * If we were to switch to taking a snapshot of the main tableau,
3209 * we would have to keep in mind that we need to save the row signs
3210 * and that we need to do this before saving the current basis
3211 * such that the basis has been restore before we restore the row signs.
3213 static struct isl_sol *find_in_pos(struct isl_sol *sol,
3214 struct isl_tab *tab, isl_int *ineq)
3220 saved = sol->context->op->save(sol->context);
3222 tab = isl_tab_dup(tab);
3226 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3228 sol = find_solutions(sol, tab);
3230 sol->context->op->restore(sol->context, saved);
3237 /* Record the absence of solutions for those values of the parameters
3238 * that do not satisfy the given inequality with equality.
3240 static struct isl_sol *no_sol_in_strict(struct isl_sol *sol,
3241 struct isl_tab *tab, struct isl_vec *ineq)
3248 saved = sol->context->op->save(sol->context);
3250 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3252 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3258 sol = sol_add(sol, tab);
3261 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3263 sol->context->op->restore(sol->context, saved);
3270 /* Compute the lexicographic minimum of the set represented by the main
3271 * tableau "tab" within the context "sol->context_tab".
3272 * On entry the sample value of the main tableau is lexicographically
3273 * less than or equal to this lexicographic minimum.
3274 * Pivots are performed until a feasible point is found, which is then
3275 * necessarily equal to the minimum, or until the tableau is found to
3276 * be infeasible. Some pivots may need to be performed for only some
3277 * feasible values of the context tableau. If so, the context tableau
3278 * is split into a part where the pivot is needed and a part where it is not.
3280 * Whenever we enter the main loop, the main tableau is such that no
3281 * "obvious" pivots need to be performed on it, where "obvious" means
3282 * that the given row can be seen to be negative without looking at
3283 * the context tableau. In particular, for non-parametric problems,
3284 * no pivots need to be performed on the main tableau.
3285 * The caller of find_solutions is responsible for making this property
3286 * hold prior to the first iteration of the loop, while restore_lexmin
3287 * is called before every other iteration.
3289 * Inside the main loop, we first examine the signs of the rows of
3290 * the main tableau within the context of the context tableau.
3291 * If we find a row that is always non-positive for all values of
3292 * the parameters satisfying the context tableau and negative for at
3293 * least one value of the parameters, we perform the appropriate pivot
3294 * and start over. An exception is the case where no pivot can be
3295 * performed on the row. In this case, we require that the sign of
3296 * the row is negative for all values of the parameters (rather than just
3297 * non-positive). This special case is handled inside row_sign, which
3298 * will say that the row can have any sign if it determines that it can
3299 * attain both negative and zero values.
3301 * If we can't find a row that always requires a pivot, but we can find
3302 * one or more rows that require a pivot for some values of the parameters
3303 * (i.e., the row can attain both positive and negative signs), then we split
3304 * the context tableau into two parts, one where we force the sign to be
3305 * non-negative and one where we force is to be negative.
3306 * The non-negative part is handled by a recursive call (through find_in_pos).
3307 * Upon returning from this call, we continue with the negative part and
3308 * perform the required pivot.
3310 * If no such rows can be found, all rows are non-negative and we have
3311 * found a (rational) feasible point. If we only wanted a rational point
3313 * Otherwise, we check if all values of the sample point of the tableau
3314 * are integral for the variables. If so, we have found the minimal
3315 * integral point and we are done.
3316 * If the sample point is not integral, then we need to make a distinction
3317 * based on whether the constant term is non-integral or the coefficients
3318 * of the parameters. Furthermore, in order to decide how to handle
3319 * the non-integrality, we also need to know whether the coefficients
3320 * of the other columns in the tableau are integral. This leads
3321 * to the following table. The first two rows do not correspond
3322 * to a non-integral sample point and are only mentioned for completeness.
3324 * constant parameters other
3327 * int int rat | -> no problem
3329 * rat int int -> fail
3331 * rat int rat -> cut
3334 * rat rat rat | -> parametric cut
3337 * rat rat int | -> split context
3339 * If the parametric constant is completely integral, then there is nothing
3340 * to be done. If the constant term is non-integral, but all the other
3341 * coefficient are integral, then there is nothing that can be done
3342 * and the tableau has no integral solution.
3343 * If, on the other hand, one or more of the other columns have rational
3344 * coeffcients, but the parameter coefficients are all integral, then
3345 * we can perform a regular (non-parametric) cut.
3346 * Finally, if there is any parameter coefficient that is non-integral,
3347 * then we need to involve the context tableau. There are two cases here.
3348 * If at least one other column has a rational coefficient, then we
3349 * can perform a parametric cut in the main tableau by adding a new
3350 * integer division in the context tableau.
3351 * If all other columns have integral coefficients, then we need to
3352 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3353 * is always integral. We do this by introducing an integer division
3354 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3355 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3356 * Since q is expressed in the tableau as
3357 * c + \sum a_i y_i - m q >= 0
3358 * -c - \sum a_i y_i + m q + m - 1 >= 0
3359 * it is sufficient to add the inequality
3360 * -c - \sum a_i y_i + m q >= 0
3361 * In the part of the context where this inequality does not hold, the
3362 * main tableau is marked as being empty.
3364 static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3366 struct isl_context *context;
3371 context = sol->context;
3375 if (context->op->is_empty(context))
3378 for (; tab && !tab->empty; tab = restore_lexmin(tab)) {
3385 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3386 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3388 sgn = row_sign(tab, sol, row);
3391 tab->row_sign[row] = sgn;
3392 if (sgn == isl_tab_row_any)
3394 if (sgn == isl_tab_row_any && split == -1)
3396 if (sgn == isl_tab_row_neg)
3399 if (row < tab->n_row)
3402 struct isl_vec *ineq;
3404 split = context->op->best_split(context, tab);
3407 ineq = get_row_parameter_ineq(tab, split);
3411 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3412 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3414 if (tab->row_sign[row] == isl_tab_row_any)
3415 tab->row_sign[row] = isl_tab_row_unknown;
3417 tab->row_sign[split] = isl_tab_row_pos;
3418 sol = find_in_pos(sol, tab, ineq->el);
3419 tab->row_sign[split] = isl_tab_row_neg;
3421 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3422 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3423 context->op->add_ineq(context, ineq->el, 0, 1);
3431 row = first_non_integer(tab, &flags);
3434 if (ISL_FL_ISSET(flags, I_PAR)) {
3435 if (ISL_FL_ISSET(flags, I_VAR)) {
3436 tab = isl_tab_mark_empty(tab);
3439 row = add_cut(tab, row);
3440 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3441 struct isl_vec *div;
3442 struct isl_vec *ineq;
3444 div = get_row_split_div(tab, row);
3447 d = context->op->get_div(context, tab, div);
3451 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3452 sol = no_sol_in_strict(sol, tab, ineq);
3453 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3454 context->op->add_ineq(context, ineq->el, 1, 1);
3456 if (!sol || !context->op->is_ok(context))
3458 tab = set_row_cst_to_div(tab, row, d);
3460 row = add_parametric_cut(tab, row, context);
3465 sol = sol_add(sol, tab);
3474 /* Compute the lexicographic minimum of the set represented by the main
3475 * tableau "tab" within the context "sol->context_tab".
3477 * As a preprocessing step, we first transfer all the purely parametric
3478 * equalities from the main tableau to the context tableau, i.e.,
3479 * parameters that have been pivoted to a row.
3480 * These equalities are ignored by the main algorithm, because the
3481 * corresponding rows may not be marked as being non-negative.
3482 * In parts of the context where the added equality does not hold,
3483 * the main tableau is marked as being empty.
3485 static struct isl_sol *find_solutions_main(struct isl_sol *sol,
3486 struct isl_tab *tab)
3490 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3494 if (tab->row_var[row] < 0)
3496 if (tab->row_var[row] >= tab->n_param &&
3497 tab->row_var[row] < tab->n_var - tab->n_div)
3499 if (tab->row_var[row] < tab->n_param)
3500 p = tab->row_var[row];
3502 p = tab->row_var[row]
3503 + tab->n_param - (tab->n_var - tab->n_div);
3505 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3506 get_row_parameter_line(tab, row, eq->el);
3507 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3508 eq = isl_vec_normalize(eq);
3510 sol = no_sol_in_strict(sol, tab, eq);
3512 isl_seq_neg(eq->el, eq->el, eq->size);
3513 sol = no_sol_in_strict(sol, tab, eq);
3514 isl_seq_neg(eq->el, eq->el, eq->size);
3516 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3520 if (isl_tab_mark_redundant(tab, row) < 0)
3523 if (sol->context->op->is_empty(sol->context))
3526 row = tab->n_redundant - 1;
3529 return find_solutions(sol, tab);
3536 static struct isl_sol_map *sol_map_find_solutions(struct isl_sol_map *sol_map,
3537 struct isl_tab *tab)
3539 return (struct isl_sol_map *)find_solutions_main(&sol_map->sol, tab);
3542 /* Check if integer division "div" of "dom" also occurs in "bmap".
3543 * If so, return its position within the divs.
3544 * If not, return -1.
3546 static int find_context_div(struct isl_basic_map *bmap,
3547 struct isl_basic_set *dom, unsigned div)
3550 unsigned b_dim = isl_dim_total(bmap->dim);
3551 unsigned d_dim = isl_dim_total(dom->dim);
3553 if (isl_int_is_zero(dom->div[div][0]))
3555 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3558 for (i = 0; i < bmap->n_div; ++i) {
3559 if (isl_int_is_zero(bmap->div[i][0]))
3561 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3562 (b_dim - d_dim) + bmap->n_div) != -1)
3564 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3570 /* The correspondence between the variables in the main tableau,
3571 * the context tableau, and the input map and domain is as follows.
3572 * The first n_param and the last n_div variables of the main tableau
3573 * form the variables of the context tableau.
3574 * In the basic map, these n_param variables correspond to the
3575 * parameters and the input dimensions. In the domain, they correspond
3576 * to the parameters and the set dimensions.
3577 * The n_div variables correspond to the integer divisions in the domain.
3578 * To ensure that everything lines up, we may need to copy some of the
3579 * integer divisions of the domain to the map. These have to be placed
3580 * in the same order as those in the context and they have to be placed
3581 * after any other integer divisions that the map may have.
3582 * This function performs the required reordering.
3584 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3585 struct isl_basic_set *dom)
3591 for (i = 0; i < dom->n_div; ++i)
3592 if (find_context_div(bmap, dom, i) != -1)
3594 other = bmap->n_div - common;
3595 if (dom->n_div - common > 0) {
3596 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
3597 dom->n_div - common, 0, 0);
3601 for (i = 0; i < dom->n_div; ++i) {
3602 int pos = find_context_div(bmap, dom, i);
3604 pos = isl_basic_map_alloc_div(bmap);
3607 isl_int_set_si(bmap->div[pos][0], 0);
3609 if (pos != other + i)
3610 isl_basic_map_swap_div(bmap, pos, other + i);
3614 isl_basic_map_free(bmap);
3618 /* Compute the lexicographic minimum (or maximum if "max" is set)
3619 * of "bmap" over the domain "dom" and return the result as a map.
3620 * If "empty" is not NULL, then *empty is assigned a set that
3621 * contains those parts of the domain where there is no solution.
3622 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
3623 * then we compute the rational optimum. Otherwise, we compute
3624 * the integral optimum.
3626 * We perform some preprocessing. As the PILP solver does not
3627 * handle implicit equalities very well, we first make sure all
3628 * the equalities are explicitly available.
3629 * We also make sure the divs in the domain are properly order,
3630 * because they will be added one by one in the given order
3631 * during the construction of the solution map.
3633 struct isl_map *isl_tab_basic_map_partial_lexopt(
3634 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3635 struct isl_set **empty, int max)
3637 struct isl_tab *tab;
3638 struct isl_map *result = NULL;
3639 struct isl_sol_map *sol_map = NULL;
3640 struct isl_context *context;
3647 isl_assert(bmap->ctx,
3648 isl_basic_map_compatible_domain(bmap, dom), goto error);
3650 bmap = isl_basic_map_detect_equalities(bmap);
3653 dom = isl_basic_set_order_divs(dom);
3654 bmap = align_context_divs(bmap, dom);
3656 sol_map = sol_map_init(bmap, dom, !!empty, max);
3660 context = sol_map->sol.context;
3661 if (isl_basic_set_fast_is_empty(context->op->peek_basic_set(context)))
3663 else if (isl_basic_map_fast_is_empty(bmap))
3664 sol_map = sol_map_add_empty(sol_map,
3665 isl_basic_set_dup(context->op->peek_basic_set(context)));
3667 tab = tab_for_lexmin(bmap,
3668 context->op->peek_basic_set(context), 1, max);
3669 tab = context->op->detect_nonnegative_parameters(context, tab);
3670 sol_map = sol_map_find_solutions(sol_map, tab);
3675 result = isl_map_copy(sol_map->map);
3677 *empty = isl_set_copy(sol_map->empty);
3678 sol_map_free(sol_map);
3679 isl_basic_map_free(bmap);
3682 sol_map_free(sol_map);
3683 isl_basic_map_free(bmap);
3687 struct isl_sol_for {
3689 int (*fn)(__isl_take isl_basic_set *dom,
3690 __isl_take isl_mat *map, void *user);
3694 static void sol_for_free(struct isl_sol_for *sol_for)
3696 if (sol_for->sol.context)
3697 sol_for->sol.context->op->free(sol_for->sol.context);
3701 static void sol_for_free_wrap(struct isl_sol *sol)
3703 sol_for_free((struct isl_sol_for *)sol);
3706 /* Add the solution identified by the tableau and the context tableau.
3708 * See documentation of sol_add for more details.
3710 * Instead of constructing a basic map, this function calls a user
3711 * defined function with the current context as a basic set and
3712 * an affine matrix reprenting the relation between the input and output.
3713 * The number of rows in this matrix is equal to one plus the number
3714 * of output variables. The number of columns is equal to one plus
3715 * the total dimension of the context, i.e., the number of parameters,
3716 * input variables and divs. Since some of the columns in the matrix
3717 * may refer to the divs, the basic set is not simplified.
3718 * (Simplification may reorder or remove divs.)
3720 static struct isl_sol_for *sol_for_add(struct isl_sol_for *sol,
3721 struct isl_basic_set *dom, struct isl_mat *M)
3723 if (!sol || !dom || !M)
3726 dom = isl_basic_set_simplify(dom);
3727 dom = isl_basic_set_finalize(dom);
3729 if (sol->fn(isl_basic_set_copy(dom), isl_mat_copy(M), sol->user) < 0)
3732 isl_basic_set_free(dom);
3736 isl_basic_set_free(dom);
3738 sol_free(&sol->sol);
3742 static struct isl_sol *sol_for_add_wrap(struct isl_sol *sol,
3743 struct isl_basic_set *dom, struct isl_mat *M)
3745 return (struct isl_sol *)sol_for_add((struct isl_sol_for *)sol, dom, M);
3748 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
3749 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3753 struct isl_sol_for *sol_for = NULL;
3754 struct isl_dim *dom_dim;
3755 struct isl_basic_set *dom = NULL;
3757 sol_for = isl_calloc_type(bset->ctx, struct isl_sol_for);
3761 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3762 dom = isl_basic_set_universe(dom_dim);
3765 sol_for->user = user;
3766 sol_for->sol.max = max;
3767 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3768 sol_for->sol.add = &sol_for_add_wrap;
3769 sol_for->sol.add_empty = NULL;
3770 sol_for->sol.free = &sol_for_free_wrap;
3772 sol_for->sol.context = isl_context_alloc(dom);
3773 if (!sol_for->sol.context)
3776 isl_basic_set_free(dom);
3779 isl_basic_set_free(dom);
3780 sol_for_free(sol_for);
3784 static struct isl_sol_for *sol_for_find_solutions(struct isl_sol_for *sol_for,
3785 struct isl_tab *tab)
3787 return (struct isl_sol_for *)find_solutions_main(&sol_for->sol, tab);
3790 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
3791 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3795 struct isl_sol_for *sol_for = NULL;
3797 bmap = isl_basic_map_copy(bmap);
3801 bmap = isl_basic_map_detect_equalities(bmap);
3802 sol_for = sol_for_init(bmap, max, fn, user);
3804 if (isl_basic_map_fast_is_empty(bmap))
3807 struct isl_tab *tab;
3808 struct isl_context *context = sol_for->sol.context;
3809 tab = tab_for_lexmin(bmap,
3810 context->op->peek_basic_set(context), 1, max);
3811 tab = context->op->detect_nonnegative_parameters(context, tab);
3812 sol_for = sol_for_find_solutions(sol_for, tab);
3817 sol_for_free(sol_for);
3818 isl_basic_map_free(bmap);
3821 sol_for_free(sol_for);
3822 isl_basic_map_free(bmap);
3826 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map *bmap,
3827 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3831 return isl_basic_map_foreach_lexopt(bmap, 0, fn, user);
3834 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map *bmap,
3835 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
3839 return isl_basic_map_foreach_lexopt(bmap, 1, fn, user);