2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include "isl_map_private.h"
13 #include "isl_sample.h"
16 * The implementation of parametric integer linear programming in this file
17 * was inspired by the paper "Parametric Integer Programming" and the
18 * report "Solving systems of affine (in)equalities" by Paul Feautrier
21 * The strategy used for obtaining a feasible solution is different
22 * from the one used in isl_tab.c. In particular, in isl_tab.c,
23 * upon finding a constraint that is not yet satisfied, we pivot
24 * in a row that increases the constant term of row holding the
25 * constraint, making sure the sample solution remains feasible
26 * for all the constraints it already satisfied.
27 * Here, we always pivot in the row holding the constraint,
28 * choosing a column that induces the lexicographically smallest
29 * increment to the sample solution.
31 * By starting out from a sample value that is lexicographically
32 * smaller than any integer point in the problem space, the first
33 * feasible integer sample point we find will also be the lexicographically
34 * smallest. If all variables can be assumed to be non-negative,
35 * then the initial sample value may be chosen equal to zero.
36 * However, we will not make this assumption. Instead, we apply
37 * the "big parameter" trick. Any variable x is then not directly
38 * used in the tableau, but instead it its represented by another
39 * variable x' = M + x, where M is an arbitrarily large (positive)
40 * value. x' is therefore always non-negative, whatever the value of x.
41 * Taking as initial smaple value x' = 0 corresponds to x = -M,
42 * which is always smaller than any possible value of x.
44 * The big parameter trick is used in the main tableau and
45 * also in the context tableau if isl_context_lex is used.
46 * In this case, each tableaus has its own big parameter.
47 * Before doing any real work, we check if all the parameters
48 * happen to be non-negative. If so, we drop the column corresponding
49 * to M from the initial context tableau.
50 * If isl_context_gbr is used, then the big parameter trick is only
51 * used in the main tableau.
55 struct isl_context_op {
56 /* detect nonnegative parameters in context and mark them in tab */
57 struct isl_tab *(*detect_nonnegative_parameters)(
58 struct isl_context *context, struct isl_tab *tab);
59 /* return temporary reference to basic set representation of context */
60 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
61 /* return temporary reference to tableau representation of context */
62 struct isl_tab *(*peek_tab)(struct isl_context *context);
63 /* add equality; check is 1 if eq may not be valid;
64 * update is 1 if we may want to call ineq_sign on context later.
66 void (*add_eq)(struct isl_context *context, isl_int *eq,
67 int check, int update);
68 /* add inequality; check is 1 if ineq may not be valid;
69 * update is 1 if we may want to call ineq_sign on context later.
71 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
72 int check, int update);
73 /* check sign of ineq based on previous information.
74 * strict is 1 if saturation should be treated as a positive sign.
76 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
77 isl_int *ineq, int strict);
78 /* check if inequality maintains feasibility */
79 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
80 /* return index of a div that corresponds to "div" */
81 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
83 /* add div "div" to context and return index and non-negativity */
84 int (*add_div)(struct isl_context *context, struct isl_vec *div,
86 int (*detect_equalities)(struct isl_context *context,
88 /* return row index of "best" split */
89 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
90 /* check if context has already been determined to be empty */
91 int (*is_empty)(struct isl_context *context);
92 /* check if context is still usable */
93 int (*is_ok)(struct isl_context *context);
94 /* save a copy/snapshot of context */
95 void *(*save)(struct isl_context *context);
96 /* restore saved context */
97 void (*restore)(struct isl_context *context, void *);
98 /* invalidate context */
99 void (*invalidate)(struct isl_context *context);
101 void (*free)(struct isl_context *context);
105 struct isl_context_op *op;
108 struct isl_context_lex {
109 struct isl_context context;
113 struct isl_partial_sol {
115 struct isl_basic_set *dom;
118 struct isl_partial_sol *next;
122 struct isl_sol_callback {
123 struct isl_tab_callback callback;
127 /* isl_sol is an interface for constructing a solution to
128 * a parametric integer linear programming problem.
129 * Every time the algorithm reaches a state where a solution
130 * can be read off from the tableau (including cases where the tableau
131 * is empty), the function "add" is called on the isl_sol passed
132 * to find_solutions_main.
134 * The context tableau is owned by isl_sol and is updated incrementally.
136 * There are currently two implementations of this interface,
137 * isl_sol_map, which simply collects the solutions in an isl_map
138 * and (optionally) the parts of the context where there is no solution
140 * isl_sol_for, which calls a user-defined function for each part of
149 struct isl_context *context;
150 struct isl_partial_sol *partial;
151 void (*add)(struct isl_sol *sol,
152 struct isl_basic_set *dom, struct isl_mat *M);
153 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
154 void (*free)(struct isl_sol *sol);
155 struct isl_sol_callback dec_level;
158 static void sol_free(struct isl_sol *sol)
160 struct isl_partial_sol *partial, *next;
163 for (partial = sol->partial; partial; partial = next) {
164 next = partial->next;
165 isl_basic_set_free(partial->dom);
166 isl_mat_free(partial->M);
172 /* Push a partial solution represented by a domain and mapping M
173 * onto the stack of partial solutions.
175 static void sol_push_sol(struct isl_sol *sol,
176 struct isl_basic_set *dom, struct isl_mat *M)
178 struct isl_partial_sol *partial;
180 if (sol->error || !dom)
183 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
187 partial->level = sol->level;
190 partial->next = sol->partial;
192 sol->partial = partial;
196 isl_basic_set_free(dom);
200 /* Pop one partial solution from the partial solution stack and
201 * pass it on to sol->add or sol->add_empty.
203 static void sol_pop_one(struct isl_sol *sol)
205 struct isl_partial_sol *partial;
207 partial = sol->partial;
208 sol->partial = partial->next;
211 sol->add(sol, partial->dom, partial->M);
213 sol->add_empty(sol, partial->dom);
217 /* Return a fresh copy of the domain represented by the context tableau.
219 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
221 struct isl_basic_set *bset;
226 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
227 bset = isl_basic_set_update_from_tab(bset,
228 sol->context->op->peek_tab(sol->context));
233 /* Check whether two partial solutions have the same mapping, where n_div
234 * is the number of divs that the two partial solutions have in common.
236 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
242 if (!s1->M != !s2->M)
247 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
249 for (i = 0; i < s1->M->n_row; ++i) {
250 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
251 s1->M->n_col-1-dim-n_div) != -1)
253 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
254 s2->M->n_col-1-dim-n_div) != -1)
256 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
262 /* Pop all solutions from the partial solution stack that were pushed onto
263 * the stack at levels that are deeper than the current level.
264 * If the two topmost elements on the stack have the same level
265 * and represent the same solution, then their domains are combined.
266 * This combined domain is the same as the current context domain
267 * as sol_pop is called each time we move back to a higher level.
269 static void sol_pop(struct isl_sol *sol)
271 struct isl_partial_sol *partial;
277 if (sol->level == 0) {
278 for (partial = sol->partial; partial; partial = sol->partial)
283 partial = sol->partial;
287 if (partial->level <= sol->level)
290 if (partial->next && partial->next->level == partial->level) {
291 n_div = isl_basic_set_dim(
292 sol->context->op->peek_basic_set(sol->context),
295 if (!same_solution(partial, partial->next, n_div)) {
299 struct isl_basic_set *bset;
301 bset = sol_domain(sol);
303 isl_basic_set_free(partial->next->dom);
304 partial->next->dom = bset;
305 partial->next->level = sol->level;
307 sol->partial = partial->next;
308 isl_basic_set_free(partial->dom);
309 isl_mat_free(partial->M);
316 static void sol_dec_level(struct isl_sol *sol)
326 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
328 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
330 sol_dec_level(callback->sol);
332 return callback->sol->error ? -1 : 0;
335 /* Move down to next level and push callback onto context tableau
336 * to decrease the level again when it gets rolled back across
337 * the current state. That is, dec_level will be called with
338 * the context tableau in the same state as it is when inc_level
341 static void sol_inc_level(struct isl_sol *sol)
349 tab = sol->context->op->peek_tab(sol->context);
350 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
354 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
358 if (isl_int_is_one(m))
361 for (i = 0; i < n_row; ++i)
362 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
365 /* Add the solution identified by the tableau and the context tableau.
367 * The layout of the variables is as follows.
368 * tab->n_var is equal to the total number of variables in the input
369 * map (including divs that were copied from the context)
370 * + the number of extra divs constructed
371 * Of these, the first tab->n_param and the last tab->n_div variables
372 * correspond to the variables in the context, i.e.,
373 * tab->n_param + tab->n_div = context_tab->n_var
374 * tab->n_param is equal to the number of parameters and input
375 * dimensions in the input map
376 * tab->n_div is equal to the number of divs in the context
378 * If there is no solution, then call add_empty with a basic set
379 * that corresponds to the context tableau. (If add_empty is NULL,
382 * If there is a solution, then first construct a matrix that maps
383 * all dimensions of the context to the output variables, i.e.,
384 * the output dimensions in the input map.
385 * The divs in the input map (if any) that do not correspond to any
386 * div in the context do not appear in the solution.
387 * The algorithm will make sure that they have an integer value,
388 * but these values themselves are of no interest.
389 * We have to be careful not to drop or rearrange any divs in the
390 * context because that would change the meaning of the matrix.
392 * To extract the value of the output variables, it should be noted
393 * that we always use a big parameter M in the main tableau and so
394 * the variable stored in this tableau is not an output variable x itself, but
395 * x' = M + x (in case of minimization)
397 * x' = M - x (in case of maximization)
398 * If x' appears in a column, then its optimal value is zero,
399 * which means that the optimal value of x is an unbounded number
400 * (-M for minimization and M for maximization).
401 * We currently assume that the output dimensions in the original map
402 * are bounded, so this cannot occur.
403 * Similarly, when x' appears in a row, then the coefficient of M in that
404 * row is necessarily 1.
405 * If the row in the tableau represents
406 * d x' = c + d M + e(y)
407 * then, in case of minimization, the corresponding row in the matrix
410 * with a d = m, the (updated) common denominator of the matrix.
411 * In case of maximization, the row will be
414 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
416 struct isl_basic_set *bset = NULL;
417 struct isl_mat *mat = NULL;
422 if (sol->error || !tab)
425 if (tab->empty && !sol->add_empty)
428 bset = sol_domain(sol);
431 sol_push_sol(sol, bset, NULL);
437 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
438 1 + tab->n_param + tab->n_div);
444 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
445 isl_int_set_si(mat->row[0][0], 1);
446 for (row = 0; row < sol->n_out; ++row) {
447 int i = tab->n_param + row;
450 isl_seq_clr(mat->row[1 + row], mat->n_col);
451 if (!tab->var[i].is_row) {
453 isl_assert(mat->ctx, !tab->M, goto error2);
457 r = tab->var[i].index;
460 isl_assert(mat->ctx, isl_int_eq(tab->mat->row[r][2],
461 tab->mat->row[r][0]),
463 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
464 isl_int_divexact(m, tab->mat->row[r][0], m);
465 scale_rows(mat, m, 1 + row);
466 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
467 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
468 for (j = 0; j < tab->n_param; ++j) {
470 if (tab->var[j].is_row)
472 col = tab->var[j].index;
473 isl_int_mul(mat->row[1 + row][1 + j], m,
474 tab->mat->row[r][off + col]);
476 for (j = 0; j < tab->n_div; ++j) {
478 if (tab->var[tab->n_var - tab->n_div+j].is_row)
480 col = tab->var[tab->n_var - tab->n_div+j].index;
481 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
482 tab->mat->row[r][off + col]);
485 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
491 sol_push_sol(sol, bset, mat);
496 isl_basic_set_free(bset);
504 struct isl_set *empty;
507 static void sol_map_free(struct isl_sol_map *sol_map)
509 if (sol_map->sol.context)
510 sol_map->sol.context->op->free(sol_map->sol.context);
511 isl_map_free(sol_map->map);
512 isl_set_free(sol_map->empty);
516 static void sol_map_free_wrap(struct isl_sol *sol)
518 sol_map_free((struct isl_sol_map *)sol);
521 /* This function is called for parts of the context where there is
522 * no solution, with "bset" corresponding to the context tableau.
523 * Simply add the basic set to the set "empty".
525 static void sol_map_add_empty(struct isl_sol_map *sol,
526 struct isl_basic_set *bset)
530 isl_assert(bset->ctx, sol->empty, goto error);
532 sol->empty = isl_set_grow(sol->empty, 1);
533 bset = isl_basic_set_simplify(bset);
534 bset = isl_basic_set_finalize(bset);
535 sol->empty = isl_set_add(sol->empty, isl_basic_set_copy(bset));
538 isl_basic_set_free(bset);
541 isl_basic_set_free(bset);
545 static void sol_map_add_empty_wrap(struct isl_sol *sol,
546 struct isl_basic_set *bset)
548 sol_map_add_empty((struct isl_sol_map *)sol, bset);
551 /* Add bset to sol's empty, but only if we are actually collecting
554 static void sol_map_add_empty_if_needed(struct isl_sol_map *sol,
555 struct isl_basic_set *bset)
558 sol_map_add_empty(sol, bset);
560 isl_basic_set_free(bset);
563 /* Given a basic map "dom" that represents the context and an affine
564 * matrix "M" that maps the dimensions of the context to the
565 * output variables, construct a basic map with the same parameters
566 * and divs as the context, the dimensions of the context as input
567 * dimensions and a number of output dimensions that is equal to
568 * the number of output dimensions in the input map.
570 * The constraints and divs of the context are simply copied
571 * from "dom". For each row
575 * is added, with d the common denominator of M.
577 static void sol_map_add(struct isl_sol_map *sol,
578 struct isl_basic_set *dom, struct isl_mat *M)
581 struct isl_basic_map *bmap = NULL;
582 isl_basic_set *context_bset;
590 if (sol->sol.error || !dom || !M)
593 n_out = sol->sol.n_out;
594 n_eq = dom->n_eq + n_out;
595 n_ineq = dom->n_ineq;
597 nparam = isl_basic_set_total_dim(dom) - n_div;
598 total = isl_map_dim(sol->map, isl_dim_all);
599 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
600 n_div, n_eq, 2 * n_div + n_ineq);
603 if (sol->sol.rational)
604 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
605 for (i = 0; i < dom->n_div; ++i) {
606 int k = isl_basic_map_alloc_div(bmap);
609 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
610 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
611 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
612 dom->div[i] + 1 + 1 + nparam, i);
614 for (i = 0; i < dom->n_eq; ++i) {
615 int k = isl_basic_map_alloc_equality(bmap);
618 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
619 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
620 isl_seq_cpy(bmap->eq[k] + 1 + total,
621 dom->eq[i] + 1 + nparam, n_div);
623 for (i = 0; i < dom->n_ineq; ++i) {
624 int k = isl_basic_map_alloc_inequality(bmap);
627 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
628 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
629 isl_seq_cpy(bmap->ineq[k] + 1 + total,
630 dom->ineq[i] + 1 + nparam, n_div);
632 for (i = 0; i < M->n_row - 1; ++i) {
633 int k = isl_basic_map_alloc_equality(bmap);
636 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
637 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
638 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
639 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
640 M->row[1 + i] + 1 + nparam, n_div);
642 bmap = isl_basic_map_simplify(bmap);
643 bmap = isl_basic_map_finalize(bmap);
644 sol->map = isl_map_grow(sol->map, 1);
645 sol->map = isl_map_add(sol->map, bmap);
648 isl_basic_set_free(dom);
652 isl_basic_set_free(dom);
654 isl_basic_map_free(bmap);
658 static void sol_map_add_wrap(struct isl_sol *sol,
659 struct isl_basic_set *dom, struct isl_mat *M)
661 sol_map_add((struct isl_sol_map *)sol, dom, M);
665 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
666 * i.e., the constant term and the coefficients of all variables that
667 * appear in the context tableau.
668 * Note that the coefficient of the big parameter M is NOT copied.
669 * The context tableau may not have a big parameter and even when it
670 * does, it is a different big parameter.
672 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
675 unsigned off = 2 + tab->M;
677 isl_int_set(line[0], tab->mat->row[row][1]);
678 for (i = 0; i < tab->n_param; ++i) {
679 if (tab->var[i].is_row)
680 isl_int_set_si(line[1 + i], 0);
682 int col = tab->var[i].index;
683 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
686 for (i = 0; i < tab->n_div; ++i) {
687 if (tab->var[tab->n_var - tab->n_div + i].is_row)
688 isl_int_set_si(line[1 + tab->n_param + i], 0);
690 int col = tab->var[tab->n_var - tab->n_div + i].index;
691 isl_int_set(line[1 + tab->n_param + i],
692 tab->mat->row[row][off + col]);
697 /* Check if rows "row1" and "row2" have identical "parametric constants",
698 * as explained above.
699 * In this case, we also insist that the coefficients of the big parameter
700 * be the same as the values of the constants will only be the same
701 * if these coefficients are also the same.
703 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
706 unsigned off = 2 + tab->M;
708 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
711 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
712 tab->mat->row[row2][2]))
715 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
716 int pos = i < tab->n_param ? i :
717 tab->n_var - tab->n_div + i - tab->n_param;
720 if (tab->var[pos].is_row)
722 col = tab->var[pos].index;
723 if (isl_int_ne(tab->mat->row[row1][off + col],
724 tab->mat->row[row2][off + col]))
730 /* Return an inequality that expresses that the "parametric constant"
731 * should be non-negative.
732 * This function is only called when the coefficient of the big parameter
735 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
737 struct isl_vec *ineq;
739 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
743 get_row_parameter_line(tab, row, ineq->el);
745 ineq = isl_vec_normalize(ineq);
750 /* Return a integer division for use in a parametric cut based on the given row.
751 * In particular, let the parametric constant of the row be
755 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
756 * The div returned is equal to
758 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
760 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
764 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
768 isl_int_set(div->el[0], tab->mat->row[row][0]);
769 get_row_parameter_line(tab, row, div->el + 1);
770 div = isl_vec_normalize(div);
771 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
772 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
777 /* Return a integer division for use in transferring an integrality constraint
779 * In particular, let the parametric constant of the row be
783 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
784 * The the returned div is equal to
786 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
788 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
792 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
796 isl_int_set(div->el[0], tab->mat->row[row][0]);
797 get_row_parameter_line(tab, row, div->el + 1);
798 div = isl_vec_normalize(div);
799 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
804 /* Construct and return an inequality that expresses an upper bound
806 * In particular, if the div is given by
810 * then the inequality expresses
814 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
818 struct isl_vec *ineq;
823 total = isl_basic_set_total_dim(bset);
824 div_pos = 1 + total - bset->n_div + div;
826 ineq = isl_vec_alloc(bset->ctx, 1 + total);
830 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
831 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
835 /* Given a row in the tableau and a div that was created
836 * using get_row_split_div and that been constrained to equality, i.e.,
838 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
840 * replace the expression "\sum_i {a_i} y_i" in the row by d,
841 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
842 * The coefficients of the non-parameters in the tableau have been
843 * verified to be integral. We can therefore simply replace coefficient b
844 * by floor(b). For the coefficients of the parameters we have
845 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
848 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
850 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
851 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
853 isl_int_set_si(tab->mat->row[row][0], 1);
855 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
856 int drow = tab->var[tab->n_var - tab->n_div + div].index;
858 isl_assert(tab->mat->ctx,
859 isl_int_is_one(tab->mat->row[drow][0]), goto error);
860 isl_seq_combine(tab->mat->row[row] + 1,
861 tab->mat->ctx->one, tab->mat->row[row] + 1,
862 tab->mat->ctx->one, tab->mat->row[drow] + 1,
863 1 + tab->M + tab->n_col);
865 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
867 isl_int_set_si(tab->mat->row[row][2 + tab->M + dcol], 1);
876 /* Check if the (parametric) constant of the given row is obviously
877 * negative, meaning that we don't need to consult the context tableau.
878 * If there is a big parameter and its coefficient is non-zero,
879 * then this coefficient determines the outcome.
880 * Otherwise, we check whether the constant is negative and
881 * all non-zero coefficients of parameters are negative and
882 * belong to non-negative parameters.
884 static int is_obviously_neg(struct isl_tab *tab, int row)
888 unsigned off = 2 + tab->M;
891 if (isl_int_is_pos(tab->mat->row[row][2]))
893 if (isl_int_is_neg(tab->mat->row[row][2]))
897 if (isl_int_is_nonneg(tab->mat->row[row][1]))
899 for (i = 0; i < tab->n_param; ++i) {
900 /* Eliminated parameter */
901 if (tab->var[i].is_row)
903 col = tab->var[i].index;
904 if (isl_int_is_zero(tab->mat->row[row][off + col]))
906 if (!tab->var[i].is_nonneg)
908 if (isl_int_is_pos(tab->mat->row[row][off + col]))
911 for (i = 0; i < tab->n_div; ++i) {
912 if (tab->var[tab->n_var - tab->n_div + i].is_row)
914 col = tab->var[tab->n_var - tab->n_div + i].index;
915 if (isl_int_is_zero(tab->mat->row[row][off + col]))
917 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
919 if (isl_int_is_pos(tab->mat->row[row][off + col]))
925 /* Check if the (parametric) constant of the given row is obviously
926 * non-negative, meaning that we don't need to consult the context tableau.
927 * If there is a big parameter and its coefficient is non-zero,
928 * then this coefficient determines the outcome.
929 * Otherwise, we check whether the constant is non-negative and
930 * all non-zero coefficients of parameters are positive and
931 * belong to non-negative parameters.
933 static int is_obviously_nonneg(struct isl_tab *tab, int row)
937 unsigned off = 2 + tab->M;
940 if (isl_int_is_pos(tab->mat->row[row][2]))
942 if (isl_int_is_neg(tab->mat->row[row][2]))
946 if (isl_int_is_neg(tab->mat->row[row][1]))
948 for (i = 0; i < tab->n_param; ++i) {
949 /* Eliminated parameter */
950 if (tab->var[i].is_row)
952 col = tab->var[i].index;
953 if (isl_int_is_zero(tab->mat->row[row][off + col]))
955 if (!tab->var[i].is_nonneg)
957 if (isl_int_is_neg(tab->mat->row[row][off + col]))
960 for (i = 0; i < tab->n_div; ++i) {
961 if (tab->var[tab->n_var - tab->n_div + i].is_row)
963 col = tab->var[tab->n_var - tab->n_div + i].index;
964 if (isl_int_is_zero(tab->mat->row[row][off + col]))
966 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
968 if (isl_int_is_neg(tab->mat->row[row][off + col]))
974 /* Given a row r and two columns, return the column that would
975 * lead to the lexicographically smallest increment in the sample
976 * solution when leaving the basis in favor of the row.
977 * Pivoting with column c will increment the sample value by a non-negative
978 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
979 * corresponding to the non-parametric variables.
980 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
981 * with all other entries in this virtual row equal to zero.
982 * If variable v appears in a row, then a_{v,c} is the element in column c
985 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
986 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
987 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
988 * increment. Otherwise, it's c2.
990 static int lexmin_col_pair(struct isl_tab *tab,
991 int row, int col1, int col2, isl_int tmp)
996 tr = tab->mat->row[row] + 2 + tab->M;
998 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1002 if (!tab->var[i].is_row) {
1003 if (tab->var[i].index == col1)
1005 if (tab->var[i].index == col2)
1010 if (tab->var[i].index == row)
1013 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1014 s1 = isl_int_sgn(r[col1]);
1015 s2 = isl_int_sgn(r[col2]);
1016 if (s1 == 0 && s2 == 0)
1023 isl_int_mul(tmp, r[col2], tr[col1]);
1024 isl_int_submul(tmp, r[col1], tr[col2]);
1025 if (isl_int_is_pos(tmp))
1027 if (isl_int_is_neg(tmp))
1033 /* Given a row in the tableau, find and return the column that would
1034 * result in the lexicographically smallest, but positive, increment
1035 * in the sample point.
1036 * If there is no such column, then return tab->n_col.
1037 * If anything goes wrong, return -1.
1039 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1042 int col = tab->n_col;
1046 tr = tab->mat->row[row] + 2 + tab->M;
1050 for (j = tab->n_dead; j < tab->n_col; ++j) {
1051 if (tab->col_var[j] >= 0 &&
1052 (tab->col_var[j] < tab->n_param ||
1053 tab->col_var[j] >= tab->n_var - tab->n_div))
1056 if (!isl_int_is_pos(tr[j]))
1059 if (col == tab->n_col)
1062 col = lexmin_col_pair(tab, row, col, j, tmp);
1063 isl_assert(tab->mat->ctx, col >= 0, goto error);
1073 /* Return the first known violated constraint, i.e., a non-negative
1074 * contraint that currently has an either obviously negative value
1075 * or a previously determined to be negative value.
1077 * If any constraint has a negative coefficient for the big parameter,
1078 * if any, then we return one of these first.
1080 static int first_neg(struct isl_tab *tab)
1085 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1086 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1088 if (isl_int_is_neg(tab->mat->row[row][2]))
1091 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1092 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1094 if (tab->row_sign) {
1095 if (tab->row_sign[row] == 0 &&
1096 is_obviously_neg(tab, row))
1097 tab->row_sign[row] = isl_tab_row_neg;
1098 if (tab->row_sign[row] != isl_tab_row_neg)
1100 } else if (!is_obviously_neg(tab, row))
1107 /* Resolve all known or obviously violated constraints through pivoting.
1108 * In particular, as long as we can find any violated constraint, we
1109 * look for a pivoting column that would result in the lexicographicallly
1110 * smallest increment in the sample point. If there is no such column
1111 * then the tableau is infeasible.
1113 static struct isl_tab *restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1114 static struct isl_tab *restore_lexmin(struct isl_tab *tab)
1122 while ((row = first_neg(tab)) != -1) {
1123 col = lexmin_pivot_col(tab, row);
1124 if (col >= tab->n_col) {
1125 if (isl_tab_mark_empty(tab) < 0)
1131 if (isl_tab_pivot(tab, row, col) < 0)
1140 /* Given a row that represents an equality, look for an appropriate
1142 * In particular, if there are any non-zero coefficients among
1143 * the non-parameter variables, then we take the last of these
1144 * variables. Eliminating this variable in terms of the other
1145 * variables and/or parameters does not influence the property
1146 * that all column in the initial tableau are lexicographically
1147 * positive. The row corresponding to the eliminated variable
1148 * will only have non-zero entries below the diagonal of the
1149 * initial tableau. That is, we transform
1155 * If there is no such non-parameter variable, then we are dealing with
1156 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1157 * for elimination. This will ensure that the eliminated parameter
1158 * always has an integer value whenever all the other parameters are integral.
1159 * If there is no such parameter then we return -1.
1161 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1163 unsigned off = 2 + tab->M;
1166 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1168 if (tab->var[i].is_row)
1170 col = tab->var[i].index;
1171 if (col <= tab->n_dead)
1173 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1176 for (i = tab->n_dead; i < tab->n_col; ++i) {
1177 if (isl_int_is_one(tab->mat->row[row][off + i]))
1179 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1185 /* Add an equality that is known to be valid to the tableau.
1186 * We first check if we can eliminate a variable or a parameter.
1187 * If not, we add the equality as two inequalities.
1188 * In this case, the equality was a pure parameter equality and there
1189 * is no need to resolve any constraint violations.
1191 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1198 r = isl_tab_add_row(tab, eq);
1202 r = tab->con[r].index;
1203 i = last_var_col_or_int_par_col(tab, r);
1205 tab->con[r].is_nonneg = 1;
1206 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1208 isl_seq_neg(eq, eq, 1 + tab->n_var);
1209 r = isl_tab_add_row(tab, eq);
1212 tab->con[r].is_nonneg = 1;
1213 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1216 if (isl_tab_pivot(tab, r, i) < 0)
1218 if (isl_tab_kill_col(tab, i) < 0)
1222 tab = restore_lexmin(tab);
1231 /* Check if the given row is a pure constant.
1233 static int is_constant(struct isl_tab *tab, int row)
1235 unsigned off = 2 + tab->M;
1237 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1238 tab->n_col - tab->n_dead) == -1;
1241 /* Add an equality that may or may not be valid to the tableau.
1242 * If the resulting row is a pure constant, then it must be zero.
1243 * Otherwise, the resulting tableau is empty.
1245 * If the row is not a pure constant, then we add two inequalities,
1246 * each time checking that they can be satisfied.
1247 * In the end we try to use one of the two constraints to eliminate
1250 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1251 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1255 struct isl_tab_undo *snap;
1259 snap = isl_tab_snap(tab);
1260 r1 = isl_tab_add_row(tab, eq);
1263 tab->con[r1].is_nonneg = 1;
1264 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1267 row = tab->con[r1].index;
1268 if (is_constant(tab, row)) {
1269 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1270 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1271 if (isl_tab_mark_empty(tab) < 0)
1275 if (isl_tab_rollback(tab, snap) < 0)
1280 tab = restore_lexmin(tab);
1281 if (!tab || tab->empty)
1284 isl_seq_neg(eq, eq, 1 + tab->n_var);
1286 r2 = isl_tab_add_row(tab, eq);
1289 tab->con[r2].is_nonneg = 1;
1290 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1293 tab = restore_lexmin(tab);
1294 if (!tab || tab->empty)
1297 if (!tab->con[r1].is_row) {
1298 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1300 } else if (!tab->con[r2].is_row) {
1301 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1303 } else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
1304 unsigned off = 2 + tab->M;
1306 int row = tab->con[r1].index;
1307 i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
1308 tab->n_col - tab->n_dead);
1310 if (isl_tab_pivot(tab, row, tab->n_dead + i) < 0)
1312 if (isl_tab_kill_col(tab, tab->n_dead + i) < 0)
1318 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1319 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1321 isl_seq_neg(eq, eq, 1 + tab->n_var);
1322 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1323 isl_seq_neg(eq, eq, 1 + tab->n_var);
1324 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1336 /* Add an inequality to the tableau, resolving violations using
1339 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1346 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1347 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1352 r = isl_tab_add_row(tab, ineq);
1355 tab->con[r].is_nonneg = 1;
1356 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1358 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1359 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1364 tab = restore_lexmin(tab);
1365 if (tab && !tab->empty && tab->con[r].is_row &&
1366 isl_tab_row_is_redundant(tab, tab->con[r].index))
1367 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1375 /* Check if the coefficients of the parameters are all integral.
1377 static int integer_parameter(struct isl_tab *tab, int row)
1381 unsigned off = 2 + tab->M;
1383 for (i = 0; i < tab->n_param; ++i) {
1384 /* Eliminated parameter */
1385 if (tab->var[i].is_row)
1387 col = tab->var[i].index;
1388 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1389 tab->mat->row[row][0]))
1392 for (i = 0; i < tab->n_div; ++i) {
1393 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1395 col = tab->var[tab->n_var - tab->n_div + i].index;
1396 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1397 tab->mat->row[row][0]))
1403 /* Check if the coefficients of the non-parameter variables are all integral.
1405 static int integer_variable(struct isl_tab *tab, int row)
1408 unsigned off = 2 + tab->M;
1410 for (i = tab->n_dead; i < tab->n_col; ++i) {
1411 if (tab->col_var[i] >= 0 &&
1412 (tab->col_var[i] < tab->n_param ||
1413 tab->col_var[i] >= tab->n_var - tab->n_div))
1415 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1416 tab->mat->row[row][0]))
1422 /* Check if the constant term is integral.
1424 static int integer_constant(struct isl_tab *tab, int row)
1426 return isl_int_is_divisible_by(tab->mat->row[row][1],
1427 tab->mat->row[row][0]);
1430 #define I_CST 1 << 0
1431 #define I_PAR 1 << 1
1432 #define I_VAR 1 << 2
1434 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1435 * that is non-integer and therefore requires a cut and return
1436 * the index of the variable.
1437 * For parametric tableaus, there are three parts in a row,
1438 * the constant, the coefficients of the parameters and the rest.
1439 * For each part, we check whether the coefficients in that part
1440 * are all integral and if so, set the corresponding flag in *f.
1441 * If the constant and the parameter part are integral, then the
1442 * current sample value is integral and no cut is required
1443 * (irrespective of whether the variable part is integral).
1445 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1447 var = var < 0 ? tab->n_param : var + 1;
1449 for (; var < tab->n_var - tab->n_div; ++var) {
1452 if (!tab->var[var].is_row)
1454 row = tab->var[var].index;
1455 if (integer_constant(tab, row))
1456 ISL_FL_SET(flags, I_CST);
1457 if (integer_parameter(tab, row))
1458 ISL_FL_SET(flags, I_PAR);
1459 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1461 if (integer_variable(tab, row))
1462 ISL_FL_SET(flags, I_VAR);
1469 /* Check for first (non-parameter) variable that is non-integer and
1470 * therefore requires a cut and return the corresponding row.
1471 * For parametric tableaus, there are three parts in a row,
1472 * the constant, the coefficients of the parameters and the rest.
1473 * For each part, we check whether the coefficients in that part
1474 * are all integral and if so, set the corresponding flag in *f.
1475 * If the constant and the parameter part are integral, then the
1476 * current sample value is integral and no cut is required
1477 * (irrespective of whether the variable part is integral).
1479 static int first_non_integer_row(struct isl_tab *tab, int *f)
1481 int var = next_non_integer_var(tab, -1, f);
1483 return var < 0 ? -1 : tab->var[var].index;
1486 /* Add a (non-parametric) cut to cut away the non-integral sample
1487 * value of the given row.
1489 * If the row is given by
1491 * m r = f + \sum_i a_i y_i
1495 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1497 * The big parameter, if any, is ignored, since it is assumed to be big
1498 * enough to be divisible by any integer.
1499 * If the tableau is actually a parametric tableau, then this function
1500 * is only called when all coefficients of the parameters are integral.
1501 * The cut therefore has zero coefficients for the parameters.
1503 * The current value is known to be negative, so row_sign, if it
1504 * exists, is set accordingly.
1506 * Return the row of the cut or -1.
1508 static int add_cut(struct isl_tab *tab, int row)
1513 unsigned off = 2 + tab->M;
1515 if (isl_tab_extend_cons(tab, 1) < 0)
1517 r = isl_tab_allocate_con(tab);
1521 r_row = tab->mat->row[tab->con[r].index];
1522 isl_int_set(r_row[0], tab->mat->row[row][0]);
1523 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1524 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1525 isl_int_neg(r_row[1], r_row[1]);
1527 isl_int_set_si(r_row[2], 0);
1528 for (i = 0; i < tab->n_col; ++i)
1529 isl_int_fdiv_r(r_row[off + i],
1530 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1532 tab->con[r].is_nonneg = 1;
1533 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1536 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1538 return tab->con[r].index;
1541 /* Given a non-parametric tableau, add cuts until an integer
1542 * sample point is obtained or until the tableau is determined
1543 * to be integer infeasible.
1544 * As long as there is any non-integer value in the sample point,
1545 * we add appropriate cuts, if possible, for each of these
1546 * non-integer values and then resolve the violated
1547 * cut constraints using restore_lexmin.
1548 * If one of the corresponding rows is equal to an integral
1549 * combination of variables/constraints plus a non-integral constant,
1550 * then there is no way to obtain an integer point and we return
1551 * a tableau that is marked empty.
1553 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
1564 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1566 if (ISL_FL_ISSET(flags, I_VAR)) {
1567 if (isl_tab_mark_empty(tab) < 0)
1571 row = tab->var[var].index;
1572 row = add_cut(tab, row);
1575 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1576 tab = restore_lexmin(tab);
1577 if (!tab || tab->empty)
1586 /* Check whether all the currently active samples also satisfy the inequality
1587 * "ineq" (treated as an equality if eq is set).
1588 * Remove those samples that do not.
1590 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1598 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1599 isl_assert(tab->mat->ctx, tab->samples, goto error);
1600 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1603 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1605 isl_seq_inner_product(ineq, tab->samples->row[i],
1606 1 + tab->n_var, &v);
1607 sgn = isl_int_sgn(v);
1608 if (eq ? (sgn == 0) : (sgn >= 0))
1610 tab = isl_tab_drop_sample(tab, i);
1622 /* Check whether the sample value of the tableau is finite,
1623 * i.e., either the tableau does not use a big parameter, or
1624 * all values of the variables are equal to the big parameter plus
1625 * some constant. This constant is the actual sample value.
1627 static int sample_is_finite(struct isl_tab *tab)
1634 for (i = 0; i < tab->n_var; ++i) {
1636 if (!tab->var[i].is_row)
1638 row = tab->var[i].index;
1639 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1645 /* Check if the context tableau of sol has any integer points.
1646 * Leave tab in empty state if no integer point can be found.
1647 * If an integer point can be found and if moreover it is finite,
1648 * then it is added to the list of sample values.
1650 * This function is only called when none of the currently active sample
1651 * values satisfies the most recently added constraint.
1653 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1655 struct isl_tab_undo *snap;
1661 snap = isl_tab_snap(tab);
1662 if (isl_tab_push_basis(tab) < 0)
1665 tab = cut_to_integer_lexmin(tab);
1669 if (!tab->empty && sample_is_finite(tab)) {
1670 struct isl_vec *sample;
1672 sample = isl_tab_get_sample_value(tab);
1674 tab = isl_tab_add_sample(tab, sample);
1677 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1686 /* Check if any of the currently active sample values satisfies
1687 * the inequality "ineq" (an equality if eq is set).
1689 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1697 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1698 isl_assert(tab->mat->ctx, tab->samples, return -1);
1699 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1702 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1704 isl_seq_inner_product(ineq, tab->samples->row[i],
1705 1 + tab->n_var, &v);
1706 sgn = isl_int_sgn(v);
1707 if (eq ? (sgn == 0) : (sgn >= 0))
1712 return i < tab->n_sample;
1715 /* For a div d = floor(f/m), add the constraints
1718 * -(f-(m-1)) + m d >= 0
1720 * Note that the second constraint is the negation of
1724 static void add_div_constraints(struct isl_context *context, unsigned div)
1728 struct isl_vec *ineq;
1729 struct isl_basic_set *bset;
1731 bset = context->op->peek_basic_set(context);
1735 total = isl_basic_set_total_dim(bset);
1736 div_pos = 1 + total - bset->n_div + div;
1738 ineq = ineq_for_div(bset, div);
1742 context->op->add_ineq(context, ineq->el, 0, 0);
1744 isl_seq_neg(ineq->el, bset->div[div] + 1, 1 + total);
1745 isl_int_set(ineq->el[div_pos], bset->div[div][0]);
1746 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
1747 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
1749 context->op->add_ineq(context, ineq->el, 0, 0);
1755 context->op->invalidate(context);
1758 /* Add a div specifed by "div" to the tableau "tab" and return
1759 * the index of the new div. *nonneg is set to 1 if the div
1760 * is obviously non-negative.
1762 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1768 struct isl_mat *samples;
1770 for (i = 0; i < tab->n_var; ++i) {
1771 if (isl_int_is_zero(div->el[2 + i]))
1773 if (!tab->var[i].is_nonneg)
1776 *nonneg = i == tab->n_var;
1778 if (isl_tab_extend_cons(tab, 3) < 0)
1780 if (isl_tab_extend_vars(tab, 1) < 0)
1782 r = isl_tab_allocate_var(tab);
1786 tab->var[r].is_nonneg = 1;
1787 tab->var[r].frozen = 1;
1789 samples = isl_mat_extend(tab->samples,
1790 tab->n_sample, 1 + tab->n_var);
1791 tab->samples = samples;
1794 for (i = tab->n_outside; i < samples->n_row; ++i) {
1795 isl_seq_inner_product(div->el + 1, samples->row[i],
1796 div->size - 1, &samples->row[i][samples->n_col - 1]);
1797 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1798 samples->row[i][samples->n_col - 1], div->el[0]);
1801 tab->bmap = isl_basic_map_extend_dim(tab->bmap,
1802 isl_basic_map_get_dim(tab->bmap), 1, 0, 2);
1803 k = isl_basic_map_alloc_div(tab->bmap);
1806 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
1807 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
1813 /* Add a div specified by "div" to both the main tableau and
1814 * the context tableau. In case of the main tableau, we only
1815 * need to add an extra div. In the context tableau, we also
1816 * need to express the meaning of the div.
1817 * Return the index of the div or -1 if anything went wrong.
1819 static int add_div(struct isl_tab *tab, struct isl_context *context,
1820 struct isl_vec *div)
1826 k = context->op->add_div(context, div, &nonneg);
1830 add_div_constraints(context, k);
1831 if (!context->op->is_ok(context))
1834 if (isl_tab_extend_vars(tab, 1) < 0)
1836 r = isl_tab_allocate_var(tab);
1840 tab->var[r].is_nonneg = 1;
1841 tab->var[r].frozen = 1;
1844 return tab->n_div - 1;
1846 context->op->invalidate(context);
1850 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1853 unsigned total = isl_basic_map_total_dim(tab->bmap);
1855 for (i = 0; i < tab->bmap->n_div; ++i) {
1856 if (isl_int_ne(tab->bmap->div[i][0], denom))
1858 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, total))
1865 /* Return the index of a div that corresponds to "div".
1866 * We first check if we already have such a div and if not, we create one.
1868 static int get_div(struct isl_tab *tab, struct isl_context *context,
1869 struct isl_vec *div)
1872 struct isl_tab *context_tab = context->op->peek_tab(context);
1877 d = find_div(context_tab, div->el + 1, div->el[0]);
1881 return add_div(tab, context, div);
1884 /* Add a parametric cut to cut away the non-integral sample value
1886 * Let a_i be the coefficients of the constant term and the parameters
1887 * and let b_i be the coefficients of the variables or constraints
1888 * in basis of the tableau.
1889 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1891 * The cut is expressed as
1893 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1895 * If q did not already exist in the context tableau, then it is added first.
1896 * If q is in a column of the main tableau then the "+ q" can be accomplished
1897 * by setting the corresponding entry to the denominator of the constraint.
1898 * If q happens to be in a row of the main tableau, then the corresponding
1899 * row needs to be added instead (taking care of the denominators).
1900 * Note that this is very unlikely, but perhaps not entirely impossible.
1902 * The current value of the cut is known to be negative (or at least
1903 * non-positive), so row_sign is set accordingly.
1905 * Return the row of the cut or -1.
1907 static int add_parametric_cut(struct isl_tab *tab, int row,
1908 struct isl_context *context)
1910 struct isl_vec *div;
1917 unsigned off = 2 + tab->M;
1922 div = get_row_parameter_div(tab, row);
1927 d = context->op->get_div(context, tab, div);
1931 if (isl_tab_extend_cons(tab, 1) < 0)
1933 r = isl_tab_allocate_con(tab);
1937 r_row = tab->mat->row[tab->con[r].index];
1938 isl_int_set(r_row[0], tab->mat->row[row][0]);
1939 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1940 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1941 isl_int_neg(r_row[1], r_row[1]);
1943 isl_int_set_si(r_row[2], 0);
1944 for (i = 0; i < tab->n_param; ++i) {
1945 if (tab->var[i].is_row)
1947 col = tab->var[i].index;
1948 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1949 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1950 tab->mat->row[row][0]);
1951 isl_int_neg(r_row[off + col], r_row[off + col]);
1953 for (i = 0; i < tab->n_div; ++i) {
1954 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1956 col = tab->var[tab->n_var - tab->n_div + i].index;
1957 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1958 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1959 tab->mat->row[row][0]);
1960 isl_int_neg(r_row[off + col], r_row[off + col]);
1962 for (i = 0; i < tab->n_col; ++i) {
1963 if (tab->col_var[i] >= 0 &&
1964 (tab->col_var[i] < tab->n_param ||
1965 tab->col_var[i] >= tab->n_var - tab->n_div))
1967 isl_int_fdiv_r(r_row[off + i],
1968 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1970 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1972 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1974 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1975 isl_int_divexact(r_row[0], r_row[0], gcd);
1976 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1977 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1978 r_row[0], tab->mat->row[d_row] + 1,
1979 off - 1 + tab->n_col);
1980 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1983 col = tab->var[tab->n_var - tab->n_div + d].index;
1984 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1987 tab->con[r].is_nonneg = 1;
1988 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1991 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1995 row = tab->con[r].index;
1997 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2003 /* Construct a tableau for bmap that can be used for computing
2004 * the lexicographic minimum (or maximum) of bmap.
2005 * If not NULL, then dom is the domain where the minimum
2006 * should be computed. In this case, we set up a parametric
2007 * tableau with row signs (initialized to "unknown").
2008 * If M is set, then the tableau will use a big parameter.
2009 * If max is set, then a maximum should be computed instead of a minimum.
2010 * This means that for each variable x, the tableau will contain the variable
2011 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2012 * of the variables in all constraints are negated prior to adding them
2015 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2016 struct isl_basic_set *dom, unsigned M, int max)
2019 struct isl_tab *tab;
2021 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2022 isl_basic_map_total_dim(bmap), M);
2026 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2028 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2029 tab->n_div = dom->n_div;
2030 tab->row_sign = isl_calloc_array(bmap->ctx,
2031 enum isl_tab_row_sign, tab->mat->n_row);
2035 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2036 if (isl_tab_mark_empty(tab) < 0)
2041 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2042 tab->var[i].is_nonneg = 1;
2043 tab->var[i].frozen = 1;
2045 for (i = 0; i < bmap->n_eq; ++i) {
2047 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2048 bmap->eq[i] + 1 + tab->n_param,
2049 tab->n_var - tab->n_param - tab->n_div);
2050 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2052 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2053 bmap->eq[i] + 1 + tab->n_param,
2054 tab->n_var - tab->n_param - tab->n_div);
2055 if (!tab || tab->empty)
2058 for (i = 0; i < bmap->n_ineq; ++i) {
2060 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2061 bmap->ineq[i] + 1 + tab->n_param,
2062 tab->n_var - tab->n_param - tab->n_div);
2063 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2065 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2066 bmap->ineq[i] + 1 + tab->n_param,
2067 tab->n_var - tab->n_param - tab->n_div);
2068 if (!tab || tab->empty)
2077 /* Given a main tableau where more than one row requires a split,
2078 * determine and return the "best" row to split on.
2080 * Given two rows in the main tableau, if the inequality corresponding
2081 * to the first row is redundant with respect to that of the second row
2082 * in the current tableau, then it is better to split on the second row,
2083 * since in the positive part, both row will be positive.
2084 * (In the negative part a pivot will have to be performed and just about
2085 * anything can happen to the sign of the other row.)
2087 * As a simple heuristic, we therefore select the row that makes the most
2088 * of the other rows redundant.
2090 * Perhaps it would also be useful to look at the number of constraints
2091 * that conflict with any given constraint.
2093 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2095 struct isl_tab_undo *snap;
2101 if (isl_tab_extend_cons(context_tab, 2) < 0)
2104 snap = isl_tab_snap(context_tab);
2106 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2107 struct isl_tab_undo *snap2;
2108 struct isl_vec *ineq = NULL;
2112 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2114 if (tab->row_sign[split] != isl_tab_row_any)
2117 ineq = get_row_parameter_ineq(tab, split);
2120 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2125 snap2 = isl_tab_snap(context_tab);
2127 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2128 struct isl_tab_var *var;
2132 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2134 if (tab->row_sign[row] != isl_tab_row_any)
2137 ineq = get_row_parameter_ineq(tab, row);
2140 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2144 var = &context_tab->con[context_tab->n_con - 1];
2145 if (!context_tab->empty &&
2146 !isl_tab_min_at_most_neg_one(context_tab, var))
2148 if (isl_tab_rollback(context_tab, snap2) < 0)
2151 if (best == -1 || r > best_r) {
2155 if (isl_tab_rollback(context_tab, snap) < 0)
2162 static struct isl_basic_set *context_lex_peek_basic_set(
2163 struct isl_context *context)
2165 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2168 return isl_tab_peek_bset(clex->tab);
2171 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2173 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2177 static void context_lex_extend(struct isl_context *context, int n)
2179 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2182 if (isl_tab_extend_cons(clex->tab, n) >= 0)
2184 isl_tab_free(clex->tab);
2188 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2189 int check, int update)
2191 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2192 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2194 clex->tab = add_lexmin_eq(clex->tab, eq);
2196 int v = tab_has_valid_sample(clex->tab, eq, 1);
2200 clex->tab = check_integer_feasible(clex->tab);
2203 clex->tab = check_samples(clex->tab, eq, 1);
2206 isl_tab_free(clex->tab);
2210 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2211 int check, int update)
2213 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2214 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2216 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2218 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2222 clex->tab = check_integer_feasible(clex->tab);
2225 clex->tab = check_samples(clex->tab, ineq, 0);
2228 isl_tab_free(clex->tab);
2232 /* Check which signs can be obtained by "ineq" on all the currently
2233 * active sample values. See row_sign for more information.
2235 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2241 int res = isl_tab_row_unknown;
2243 isl_assert(tab->mat->ctx, tab->samples, return 0);
2244 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return 0);
2247 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2248 isl_seq_inner_product(tab->samples->row[i], ineq,
2249 1 + tab->n_var, &tmp);
2250 sgn = isl_int_sgn(tmp);
2251 if (sgn > 0 || (sgn == 0 && strict)) {
2252 if (res == isl_tab_row_unknown)
2253 res = isl_tab_row_pos;
2254 if (res == isl_tab_row_neg)
2255 res = isl_tab_row_any;
2258 if (res == isl_tab_row_unknown)
2259 res = isl_tab_row_neg;
2260 if (res == isl_tab_row_pos)
2261 res = isl_tab_row_any;
2263 if (res == isl_tab_row_any)
2271 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2272 isl_int *ineq, int strict)
2274 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2275 return tab_ineq_sign(clex->tab, ineq, strict);
2278 /* Check whether "ineq" can be added to the tableau without rendering
2281 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2283 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2284 struct isl_tab_undo *snap;
2290 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2293 snap = isl_tab_snap(clex->tab);
2294 if (isl_tab_push_basis(clex->tab) < 0)
2296 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2297 clex->tab = check_integer_feasible(clex->tab);
2300 feasible = !clex->tab->empty;
2301 if (isl_tab_rollback(clex->tab, snap) < 0)
2307 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2308 struct isl_vec *div)
2310 return get_div(tab, context, div);
2313 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div,
2316 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2317 return context_tab_add_div(clex->tab, div, nonneg);
2320 static int context_lex_detect_equalities(struct isl_context *context,
2321 struct isl_tab *tab)
2326 static int context_lex_best_split(struct isl_context *context,
2327 struct isl_tab *tab)
2329 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2330 struct isl_tab_undo *snap;
2333 snap = isl_tab_snap(clex->tab);
2334 if (isl_tab_push_basis(clex->tab) < 0)
2336 r = best_split(tab, clex->tab);
2338 if (isl_tab_rollback(clex->tab, snap) < 0)
2344 static int context_lex_is_empty(struct isl_context *context)
2346 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2349 return clex->tab->empty;
2352 static void *context_lex_save(struct isl_context *context)
2354 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2355 struct isl_tab_undo *snap;
2357 snap = isl_tab_snap(clex->tab);
2358 if (isl_tab_push_basis(clex->tab) < 0)
2360 if (isl_tab_save_samples(clex->tab) < 0)
2366 static void context_lex_restore(struct isl_context *context, void *save)
2368 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2369 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2370 isl_tab_free(clex->tab);
2375 static int context_lex_is_ok(struct isl_context *context)
2377 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2381 /* For each variable in the context tableau, check if the variable can
2382 * only attain non-negative values. If so, mark the parameter as non-negative
2383 * in the main tableau. This allows for a more direct identification of some
2384 * cases of violated constraints.
2386 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2387 struct isl_tab *context_tab)
2390 struct isl_tab_undo *snap;
2391 struct isl_vec *ineq = NULL;
2392 struct isl_tab_var *var;
2395 if (context_tab->n_var == 0)
2398 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2402 if (isl_tab_extend_cons(context_tab, 1) < 0)
2405 snap = isl_tab_snap(context_tab);
2408 isl_seq_clr(ineq->el, ineq->size);
2409 for (i = 0; i < context_tab->n_var; ++i) {
2410 isl_int_set_si(ineq->el[1 + i], 1);
2411 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2413 var = &context_tab->con[context_tab->n_con - 1];
2414 if (!context_tab->empty &&
2415 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2417 if (i >= tab->n_param)
2418 j = i - tab->n_param + tab->n_var - tab->n_div;
2419 tab->var[j].is_nonneg = 1;
2422 isl_int_set_si(ineq->el[1 + i], 0);
2423 if (isl_tab_rollback(context_tab, snap) < 0)
2427 if (context_tab->M && n == context_tab->n_var) {
2428 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2440 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2441 struct isl_context *context, struct isl_tab *tab)
2443 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2444 struct isl_tab_undo *snap;
2446 snap = isl_tab_snap(clex->tab);
2447 if (isl_tab_push_basis(clex->tab) < 0)
2450 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2452 if (isl_tab_rollback(clex->tab, snap) < 0)
2461 static void context_lex_invalidate(struct isl_context *context)
2463 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2464 isl_tab_free(clex->tab);
2468 static void context_lex_free(struct isl_context *context)
2470 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2471 isl_tab_free(clex->tab);
2475 struct isl_context_op isl_context_lex_op = {
2476 context_lex_detect_nonnegative_parameters,
2477 context_lex_peek_basic_set,
2478 context_lex_peek_tab,
2480 context_lex_add_ineq,
2481 context_lex_ineq_sign,
2482 context_lex_test_ineq,
2483 context_lex_get_div,
2484 context_lex_add_div,
2485 context_lex_detect_equalities,
2486 context_lex_best_split,
2487 context_lex_is_empty,
2490 context_lex_restore,
2491 context_lex_invalidate,
2495 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2497 struct isl_tab *tab;
2499 bset = isl_basic_set_cow(bset);
2502 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2505 if (isl_tab_track_bset(tab, bset) < 0)
2507 tab = isl_tab_init_samples(tab);
2510 isl_basic_set_free(bset);
2514 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2516 struct isl_context_lex *clex;
2521 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2525 clex->context.op = &isl_context_lex_op;
2527 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2528 clex->tab = restore_lexmin(clex->tab);
2529 clex->tab = check_integer_feasible(clex->tab);
2533 return &clex->context;
2535 clex->context.op->free(&clex->context);
2539 struct isl_context_gbr {
2540 struct isl_context context;
2541 struct isl_tab *tab;
2542 struct isl_tab *shifted;
2543 struct isl_tab *cone;
2546 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2547 struct isl_context *context, struct isl_tab *tab)
2549 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2550 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2553 static struct isl_basic_set *context_gbr_peek_basic_set(
2554 struct isl_context *context)
2556 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2559 return isl_tab_peek_bset(cgbr->tab);
2562 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2564 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2568 /* Initialize the "shifted" tableau of the context, which
2569 * contains the constraints of the original tableau shifted
2570 * by the sum of all negative coefficients. This ensures
2571 * that any rational point in the shifted tableau can
2572 * be rounded up to yield an integer point in the original tableau.
2574 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2577 struct isl_vec *cst;
2578 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2579 unsigned dim = isl_basic_set_total_dim(bset);
2581 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2585 for (i = 0; i < bset->n_ineq; ++i) {
2586 isl_int_set(cst->el[i], bset->ineq[i][0]);
2587 for (j = 0; j < dim; ++j) {
2588 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2590 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2591 bset->ineq[i][1 + j]);
2595 cgbr->shifted = isl_tab_from_basic_set(bset);
2597 for (i = 0; i < bset->n_ineq; ++i)
2598 isl_int_set(bset->ineq[i][0], cst->el[i]);
2603 /* Check if the shifted tableau is non-empty, and if so
2604 * use the sample point to construct an integer point
2605 * of the context tableau.
2607 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2609 struct isl_vec *sample;
2612 gbr_init_shifted(cgbr);
2615 if (cgbr->shifted->empty)
2616 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2618 sample = isl_tab_get_sample_value(cgbr->shifted);
2619 sample = isl_vec_ceil(sample);
2624 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2631 for (i = 0; i < bset->n_eq; ++i)
2632 isl_int_set_si(bset->eq[i][0], 0);
2634 for (i = 0; i < bset->n_ineq; ++i)
2635 isl_int_set_si(bset->ineq[i][0], 0);
2640 static int use_shifted(struct isl_context_gbr *cgbr)
2642 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2645 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2647 struct isl_basic_set *bset;
2648 struct isl_basic_set *cone;
2650 if (isl_tab_sample_is_integer(cgbr->tab))
2651 return isl_tab_get_sample_value(cgbr->tab);
2653 if (use_shifted(cgbr)) {
2654 struct isl_vec *sample;
2656 sample = gbr_get_shifted_sample(cgbr);
2657 if (!sample || sample->size > 0)
2660 isl_vec_free(sample);
2664 bset = isl_tab_peek_bset(cgbr->tab);
2665 cgbr->cone = isl_tab_from_recession_cone(bset);
2668 if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
2671 cgbr->cone = isl_tab_detect_implicit_equalities(cgbr->cone);
2675 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2676 struct isl_vec *sample;
2677 struct isl_tab_undo *snap;
2679 if (cgbr->tab->basis) {
2680 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2681 isl_mat_free(cgbr->tab->basis);
2682 cgbr->tab->basis = NULL;
2684 cgbr->tab->n_zero = 0;
2685 cgbr->tab->n_unbounded = 0;
2689 snap = isl_tab_snap(cgbr->tab);
2691 sample = isl_tab_sample(cgbr->tab);
2693 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2694 isl_vec_free(sample);
2701 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2702 cone = drop_constant_terms(cone);
2703 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2704 cone = isl_basic_set_underlying_set(cone);
2705 cone = isl_basic_set_gauss(cone, NULL);
2707 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2708 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2709 bset = isl_basic_set_underlying_set(bset);
2710 bset = isl_basic_set_gauss(bset, NULL);
2712 return isl_basic_set_sample_with_cone(bset, cone);
2715 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2717 struct isl_vec *sample;
2722 if (cgbr->tab->empty)
2725 sample = gbr_get_sample(cgbr);
2729 if (sample->size == 0) {
2730 isl_vec_free(sample);
2731 if (isl_tab_mark_empty(cgbr->tab) < 0)
2736 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2740 isl_tab_free(cgbr->tab);
2744 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2751 if (isl_tab_extend_cons(tab, 2) < 0)
2754 tab = isl_tab_add_eq(tab, eq);
2762 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2763 int check, int update)
2765 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2767 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2769 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2770 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2772 cgbr->cone = isl_tab_add_eq(cgbr->cone, eq);
2776 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2780 check_gbr_integer_feasible(cgbr);
2783 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2786 isl_tab_free(cgbr->tab);
2790 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2795 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2798 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2801 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2804 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2806 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2809 for (i = 0; i < dim; ++i) {
2810 if (!isl_int_is_neg(ineq[1 + i]))
2812 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2815 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2818 for (i = 0; i < dim; ++i) {
2819 if (!isl_int_is_neg(ineq[1 + i]))
2821 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2825 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2826 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2828 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2834 isl_tab_free(cgbr->tab);
2838 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2839 int check, int update)
2841 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2843 add_gbr_ineq(cgbr, ineq);
2848 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2852 check_gbr_integer_feasible(cgbr);
2855 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2858 isl_tab_free(cgbr->tab);
2862 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2863 isl_int *ineq, int strict)
2865 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2866 return tab_ineq_sign(cgbr->tab, ineq, strict);
2869 /* Check whether "ineq" can be added to the tableau without rendering
2872 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2874 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2875 struct isl_tab_undo *snap;
2876 struct isl_tab_undo *shifted_snap = NULL;
2877 struct isl_tab_undo *cone_snap = NULL;
2883 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2886 snap = isl_tab_snap(cgbr->tab);
2888 shifted_snap = isl_tab_snap(cgbr->shifted);
2890 cone_snap = isl_tab_snap(cgbr->cone);
2891 add_gbr_ineq(cgbr, ineq);
2892 check_gbr_integer_feasible(cgbr);
2895 feasible = !cgbr->tab->empty;
2896 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2899 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2901 } else if (cgbr->shifted) {
2902 isl_tab_free(cgbr->shifted);
2903 cgbr->shifted = NULL;
2906 if (isl_tab_rollback(cgbr->cone, cone_snap))
2908 } else if (cgbr->cone) {
2909 isl_tab_free(cgbr->cone);
2916 /* Return the column of the last of the variables associated to
2917 * a column that has a non-zero coefficient.
2918 * This function is called in a context where only coefficients
2919 * of parameters or divs can be non-zero.
2921 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
2925 unsigned dim = tab->n_var - tab->n_param - tab->n_div;
2927 if (tab->n_var == 0)
2930 for (i = tab->n_var - 1; i >= 0; --i) {
2931 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
2933 if (tab->var[i].is_row)
2935 col = tab->var[i].index;
2936 if (!isl_int_is_zero(p[col]))
2943 /* Look through all the recently added equalities in the context
2944 * to see if we can propagate any of them to the main tableau.
2946 * The newly added equalities in the context are encoded as pairs
2947 * of inequalities starting at inequality "first".
2949 * We tentatively add each of these equalities to the main tableau
2950 * and if this happens to result in a row with a final coefficient
2951 * that is one or negative one, we use it to kill a column
2952 * in the main tableau. Otherwise, we discard the tentatively
2955 static void propagate_equalities(struct isl_context_gbr *cgbr,
2956 struct isl_tab *tab, unsigned first)
2959 struct isl_vec *eq = NULL;
2961 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2965 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
2968 isl_seq_clr(eq->el + 1 + tab->n_param,
2969 tab->n_var - tab->n_param - tab->n_div);
2970 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
2973 struct isl_tab_undo *snap;
2974 snap = isl_tab_snap(tab);
2976 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
2977 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
2978 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
2981 r = isl_tab_add_row(tab, eq->el);
2984 r = tab->con[r].index;
2985 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
2986 if (j < 0 || j < tab->n_dead ||
2987 !isl_int_is_one(tab->mat->row[r][0]) ||
2988 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
2989 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
2990 if (isl_tab_rollback(tab, snap) < 0)
2994 if (isl_tab_pivot(tab, r, j) < 0)
2996 if (isl_tab_kill_col(tab, j) < 0)
2999 tab = restore_lexmin(tab);
3007 isl_tab_free(cgbr->tab);
3011 static int context_gbr_detect_equalities(struct isl_context *context,
3012 struct isl_tab *tab)
3014 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3015 struct isl_ctx *ctx;
3017 enum isl_lp_result res;
3020 ctx = cgbr->tab->mat->ctx;
3023 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3024 cgbr->cone = isl_tab_from_recession_cone(bset);
3027 if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
3030 cgbr->cone = isl_tab_detect_implicit_equalities(cgbr->cone);
3032 n_ineq = cgbr->tab->bmap->n_ineq;
3033 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3034 if (cgbr->tab && cgbr->tab->bmap->n_ineq > n_ineq)
3035 propagate_equalities(cgbr, tab, n_ineq);
3039 isl_tab_free(cgbr->tab);
3044 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3045 struct isl_vec *div)
3047 return get_div(tab, context, div);
3050 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div,
3053 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3057 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3059 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3061 if (isl_tab_allocate_var(cgbr->cone) <0)
3064 cgbr->cone->bmap = isl_basic_map_extend_dim(cgbr->cone->bmap,
3065 isl_basic_map_get_dim(cgbr->cone->bmap), 1, 0, 2);
3066 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3069 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3070 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3073 return context_tab_add_div(cgbr->tab, div, nonneg);
3076 static int context_gbr_best_split(struct isl_context *context,
3077 struct isl_tab *tab)
3079 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3080 struct isl_tab_undo *snap;
3083 snap = isl_tab_snap(cgbr->tab);
3084 r = best_split(tab, cgbr->tab);
3086 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3092 static int context_gbr_is_empty(struct isl_context *context)
3094 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3097 return cgbr->tab->empty;
3100 struct isl_gbr_tab_undo {
3101 struct isl_tab_undo *tab_snap;
3102 struct isl_tab_undo *shifted_snap;
3103 struct isl_tab_undo *cone_snap;
3106 static void *context_gbr_save(struct isl_context *context)
3108 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3109 struct isl_gbr_tab_undo *snap;
3111 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3115 snap->tab_snap = isl_tab_snap(cgbr->tab);
3116 if (isl_tab_save_samples(cgbr->tab) < 0)
3120 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3122 snap->shifted_snap = NULL;
3125 snap->cone_snap = isl_tab_snap(cgbr->cone);
3127 snap->cone_snap = NULL;
3135 static void context_gbr_restore(struct isl_context *context, void *save)
3137 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3138 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3141 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3142 isl_tab_free(cgbr->tab);
3146 if (snap->shifted_snap) {
3147 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3149 } else if (cgbr->shifted) {
3150 isl_tab_free(cgbr->shifted);
3151 cgbr->shifted = NULL;
3154 if (snap->cone_snap) {
3155 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3157 } else if (cgbr->cone) {
3158 isl_tab_free(cgbr->cone);
3167 isl_tab_free(cgbr->tab);
3171 static int context_gbr_is_ok(struct isl_context *context)
3173 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3177 static void context_gbr_invalidate(struct isl_context *context)
3179 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3180 isl_tab_free(cgbr->tab);
3184 static void context_gbr_free(struct isl_context *context)
3186 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3187 isl_tab_free(cgbr->tab);
3188 isl_tab_free(cgbr->shifted);
3189 isl_tab_free(cgbr->cone);
3193 struct isl_context_op isl_context_gbr_op = {
3194 context_gbr_detect_nonnegative_parameters,
3195 context_gbr_peek_basic_set,
3196 context_gbr_peek_tab,
3198 context_gbr_add_ineq,
3199 context_gbr_ineq_sign,
3200 context_gbr_test_ineq,
3201 context_gbr_get_div,
3202 context_gbr_add_div,
3203 context_gbr_detect_equalities,
3204 context_gbr_best_split,
3205 context_gbr_is_empty,
3208 context_gbr_restore,
3209 context_gbr_invalidate,
3213 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3215 struct isl_context_gbr *cgbr;
3220 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3224 cgbr->context.op = &isl_context_gbr_op;
3226 cgbr->shifted = NULL;
3228 cgbr->tab = isl_tab_from_basic_set(dom);
3229 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3232 if (isl_tab_track_bset(cgbr->tab,
3233 isl_basic_set_cow(isl_basic_set_copy(dom))) < 0)
3235 check_gbr_integer_feasible(cgbr);
3237 return &cgbr->context;
3239 cgbr->context.op->free(&cgbr->context);
3243 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3248 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3249 return isl_context_lex_alloc(dom);
3251 return isl_context_gbr_alloc(dom);
3254 /* Construct an isl_sol_map structure for accumulating the solution.
3255 * If track_empty is set, then we also keep track of the parts
3256 * of the context where there is no solution.
3257 * If max is set, then we are solving a maximization, rather than
3258 * a minimization problem, which means that the variables in the
3259 * tableau have value "M - x" rather than "M + x".
3261 static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
3262 struct isl_basic_set *dom, int track_empty, int max)
3264 struct isl_sol_map *sol_map;
3266 sol_map = isl_calloc_type(bset->ctx, struct isl_sol_map);
3270 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3271 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3272 sol_map->sol.dec_level.sol = &sol_map->sol;
3273 sol_map->sol.max = max;
3274 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3275 sol_map->sol.add = &sol_map_add_wrap;
3276 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3277 sol_map->sol.free = &sol_map_free_wrap;
3278 sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
3283 sol_map->sol.context = isl_context_alloc(dom);
3284 if (!sol_map->sol.context)
3288 sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
3289 1, ISL_SET_DISJOINT);
3290 if (!sol_map->empty)
3294 isl_basic_set_free(dom);
3297 isl_basic_set_free(dom);
3298 sol_map_free(sol_map);
3302 /* Check whether all coefficients of (non-parameter) variables
3303 * are non-positive, meaning that no pivots can be performed on the row.
3305 static int is_critical(struct isl_tab *tab, int row)
3308 unsigned off = 2 + tab->M;
3310 for (j = tab->n_dead; j < tab->n_col; ++j) {
3311 if (tab->col_var[j] >= 0 &&
3312 (tab->col_var[j] < tab->n_param ||
3313 tab->col_var[j] >= tab->n_var - tab->n_div))
3316 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3323 /* Check whether the inequality represented by vec is strict over the integers,
3324 * i.e., there are no integer values satisfying the constraint with
3325 * equality. This happens if the gcd of the coefficients is not a divisor
3326 * of the constant term. If so, scale the constraint down by the gcd
3327 * of the coefficients.
3329 static int is_strict(struct isl_vec *vec)
3335 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3336 if (!isl_int_is_one(gcd)) {
3337 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3338 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3339 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3346 /* Determine the sign of the given row of the main tableau.
3347 * The result is one of
3348 * isl_tab_row_pos: always non-negative; no pivot needed
3349 * isl_tab_row_neg: always non-positive; pivot
3350 * isl_tab_row_any: can be both positive and negative; split
3352 * We first handle some simple cases
3353 * - the row sign may be known already
3354 * - the row may be obviously non-negative
3355 * - the parametric constant may be equal to that of another row
3356 * for which we know the sign. This sign will be either "pos" or
3357 * "any". If it had been "neg" then we would have pivoted before.
3359 * If none of these cases hold, we check the value of the row for each
3360 * of the currently active samples. Based on the signs of these values
3361 * we make an initial determination of the sign of the row.
3363 * all zero -> unk(nown)
3364 * all non-negative -> pos
3365 * all non-positive -> neg
3366 * both negative and positive -> all
3368 * If we end up with "all", we are done.
3369 * Otherwise, we perform a check for positive and/or negative
3370 * values as follows.
3372 * samples neg unk pos
3378 * There is no special sign for "zero", because we can usually treat zero
3379 * as either non-negative or non-positive, whatever works out best.
3380 * However, if the row is "critical", meaning that pivoting is impossible
3381 * then we don't want to limp zero with the non-positive case, because
3382 * then we we would lose the solution for those values of the parameters
3383 * where the value of the row is zero. Instead, we treat 0 as non-negative
3384 * ensuring a split if the row can attain both zero and negative values.
3385 * The same happens when the original constraint was one that could not
3386 * be satisfied with equality by any integer values of the parameters.
3387 * In this case, we normalize the constraint, but then a value of zero
3388 * for the normalized constraint is actually a positive value for the
3389 * original constraint, so again we need to treat zero as non-negative.
3390 * In both these cases, we have the following decision tree instead:
3392 * all non-negative -> pos
3393 * all negative -> neg
3394 * both negative and non-negative -> all
3402 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3403 struct isl_sol *sol, int row)
3405 struct isl_vec *ineq = NULL;
3406 int res = isl_tab_row_unknown;
3411 if (tab->row_sign[row] != isl_tab_row_unknown)
3412 return tab->row_sign[row];
3413 if (is_obviously_nonneg(tab, row))
3414 return isl_tab_row_pos;
3415 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3416 if (tab->row_sign[row2] == isl_tab_row_unknown)
3418 if (identical_parameter_line(tab, row, row2))
3419 return tab->row_sign[row2];
3422 critical = is_critical(tab, row);
3424 ineq = get_row_parameter_ineq(tab, row);
3428 strict = is_strict(ineq);
3430 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3431 critical || strict);
3433 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3434 /* test for negative values */
3436 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3437 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3439 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3443 res = isl_tab_row_pos;
3445 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3447 if (res == isl_tab_row_neg) {
3448 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3449 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3453 if (res == isl_tab_row_neg) {
3454 /* test for positive values */
3456 if (!critical && !strict)
3457 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3459 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3463 res = isl_tab_row_any;
3473 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3475 /* Find solutions for values of the parameters that satisfy the given
3478 * We currently take a snapshot of the context tableau that is reset
3479 * when we return from this function, while we make a copy of the main
3480 * tableau, leaving the original main tableau untouched.
3481 * These are fairly arbitrary choices. Making a copy also of the context
3482 * tableau would obviate the need to undo any changes made to it later,
3483 * while taking a snapshot of the main tableau could reduce memory usage.
3484 * If we were to switch to taking a snapshot of the main tableau,
3485 * we would have to keep in mind that we need to save the row signs
3486 * and that we need to do this before saving the current basis
3487 * such that the basis has been restore before we restore the row signs.
3489 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3495 saved = sol->context->op->save(sol->context);
3497 tab = isl_tab_dup(tab);
3501 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3503 find_solutions(sol, tab);
3505 sol->context->op->restore(sol->context, saved);
3511 /* Record the absence of solutions for those values of the parameters
3512 * that do not satisfy the given inequality with equality.
3514 static void no_sol_in_strict(struct isl_sol *sol,
3515 struct isl_tab *tab, struct isl_vec *ineq)
3522 saved = sol->context->op->save(sol->context);
3524 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3526 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3535 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3537 sol->context->op->restore(sol->context, saved);
3543 /* Compute the lexicographic minimum of the set represented by the main
3544 * tableau "tab" within the context "sol->context_tab".
3545 * On entry the sample value of the main tableau is lexicographically
3546 * less than or equal to this lexicographic minimum.
3547 * Pivots are performed until a feasible point is found, which is then
3548 * necessarily equal to the minimum, or until the tableau is found to
3549 * be infeasible. Some pivots may need to be performed for only some
3550 * feasible values of the context tableau. If so, the context tableau
3551 * is split into a part where the pivot is needed and a part where it is not.
3553 * Whenever we enter the main loop, the main tableau is such that no
3554 * "obvious" pivots need to be performed on it, where "obvious" means
3555 * that the given row can be seen to be negative without looking at
3556 * the context tableau. In particular, for non-parametric problems,
3557 * no pivots need to be performed on the main tableau.
3558 * The caller of find_solutions is responsible for making this property
3559 * hold prior to the first iteration of the loop, while restore_lexmin
3560 * is called before every other iteration.
3562 * Inside the main loop, we first examine the signs of the rows of
3563 * the main tableau within the context of the context tableau.
3564 * If we find a row that is always non-positive for all values of
3565 * the parameters satisfying the context tableau and negative for at
3566 * least one value of the parameters, we perform the appropriate pivot
3567 * and start over. An exception is the case where no pivot can be
3568 * performed on the row. In this case, we require that the sign of
3569 * the row is negative for all values of the parameters (rather than just
3570 * non-positive). This special case is handled inside row_sign, which
3571 * will say that the row can have any sign if it determines that it can
3572 * attain both negative and zero values.
3574 * If we can't find a row that always requires a pivot, but we can find
3575 * one or more rows that require a pivot for some values of the parameters
3576 * (i.e., the row can attain both positive and negative signs), then we split
3577 * the context tableau into two parts, one where we force the sign to be
3578 * non-negative and one where we force is to be negative.
3579 * The non-negative part is handled by a recursive call (through find_in_pos).
3580 * Upon returning from this call, we continue with the negative part and
3581 * perform the required pivot.
3583 * If no such rows can be found, all rows are non-negative and we have
3584 * found a (rational) feasible point. If we only wanted a rational point
3586 * Otherwise, we check if all values of the sample point of the tableau
3587 * are integral for the variables. If so, we have found the minimal
3588 * integral point and we are done.
3589 * If the sample point is not integral, then we need to make a distinction
3590 * based on whether the constant term is non-integral or the coefficients
3591 * of the parameters. Furthermore, in order to decide how to handle
3592 * the non-integrality, we also need to know whether the coefficients
3593 * of the other columns in the tableau are integral. This leads
3594 * to the following table. The first two rows do not correspond
3595 * to a non-integral sample point and are only mentioned for completeness.
3597 * constant parameters other
3600 * int int rat | -> no problem
3602 * rat int int -> fail
3604 * rat int rat -> cut
3607 * rat rat rat | -> parametric cut
3610 * rat rat int | -> split context
3612 * If the parametric constant is completely integral, then there is nothing
3613 * to be done. If the constant term is non-integral, but all the other
3614 * coefficient are integral, then there is nothing that can be done
3615 * and the tableau has no integral solution.
3616 * If, on the other hand, one or more of the other columns have rational
3617 * coeffcients, but the parameter coefficients are all integral, then
3618 * we can perform a regular (non-parametric) cut.
3619 * Finally, if there is any parameter coefficient that is non-integral,
3620 * then we need to involve the context tableau. There are two cases here.
3621 * If at least one other column has a rational coefficient, then we
3622 * can perform a parametric cut in the main tableau by adding a new
3623 * integer division in the context tableau.
3624 * If all other columns have integral coefficients, then we need to
3625 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3626 * is always integral. We do this by introducing an integer division
3627 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3628 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3629 * Since q is expressed in the tableau as
3630 * c + \sum a_i y_i - m q >= 0
3631 * -c - \sum a_i y_i + m q + m - 1 >= 0
3632 * it is sufficient to add the inequality
3633 * -c - \sum a_i y_i + m q >= 0
3634 * In the part of the context where this inequality does not hold, the
3635 * main tableau is marked as being empty.
3637 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3639 struct isl_context *context;
3641 if (!tab || sol->error)
3644 context = sol->context;
3648 if (context->op->is_empty(context))
3651 for (; tab && !tab->empty; tab = restore_lexmin(tab)) {
3658 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3659 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3661 sgn = row_sign(tab, sol, row);
3664 tab->row_sign[row] = sgn;
3665 if (sgn == isl_tab_row_any)
3667 if (sgn == isl_tab_row_any && split == -1)
3669 if (sgn == isl_tab_row_neg)
3672 if (row < tab->n_row)
3675 struct isl_vec *ineq;
3677 split = context->op->best_split(context, tab);
3680 ineq = get_row_parameter_ineq(tab, split);
3684 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3685 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3687 if (tab->row_sign[row] == isl_tab_row_any)
3688 tab->row_sign[row] = isl_tab_row_unknown;
3690 tab->row_sign[split] = isl_tab_row_pos;
3692 find_in_pos(sol, tab, ineq->el);
3693 tab->row_sign[split] = isl_tab_row_neg;
3695 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3696 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3697 context->op->add_ineq(context, ineq->el, 0, 1);
3705 row = first_non_integer_row(tab, &flags);
3708 if (ISL_FL_ISSET(flags, I_PAR)) {
3709 if (ISL_FL_ISSET(flags, I_VAR)) {
3710 if (isl_tab_mark_empty(tab) < 0)
3714 row = add_cut(tab, row);
3715 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3716 struct isl_vec *div;
3717 struct isl_vec *ineq;
3719 div = get_row_split_div(tab, row);
3722 d = context->op->get_div(context, tab, div);
3726 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3728 no_sol_in_strict(sol, tab, ineq);
3729 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3730 context->op->add_ineq(context, ineq->el, 1, 1);
3732 if (sol->error || !context->op->is_ok(context))
3734 tab = set_row_cst_to_div(tab, row, d);
3736 row = add_parametric_cut(tab, row, context);
3749 /* Compute the lexicographic minimum of the set represented by the main
3750 * tableau "tab" within the context "sol->context_tab".
3752 * As a preprocessing step, we first transfer all the purely parametric
3753 * equalities from the main tableau to the context tableau, i.e.,
3754 * parameters that have been pivoted to a row.
3755 * These equalities are ignored by the main algorithm, because the
3756 * corresponding rows may not be marked as being non-negative.
3757 * In parts of the context where the added equality does not hold,
3758 * the main tableau is marked as being empty.
3760 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3766 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3770 if (tab->row_var[row] < 0)
3772 if (tab->row_var[row] >= tab->n_param &&
3773 tab->row_var[row] < tab->n_var - tab->n_div)
3775 if (tab->row_var[row] < tab->n_param)
3776 p = tab->row_var[row];
3778 p = tab->row_var[row]
3779 + tab->n_param - (tab->n_var - tab->n_div);
3781 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3782 get_row_parameter_line(tab, row, eq->el);
3783 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3784 eq = isl_vec_normalize(eq);
3787 no_sol_in_strict(sol, tab, eq);
3789 isl_seq_neg(eq->el, eq->el, eq->size);
3791 no_sol_in_strict(sol, tab, eq);
3792 isl_seq_neg(eq->el, eq->el, eq->size);
3794 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3798 if (isl_tab_mark_redundant(tab, row) < 0)
3801 if (sol->context->op->is_empty(sol->context))
3804 row = tab->n_redundant - 1;
3807 find_solutions(sol, tab);
3818 static void sol_map_find_solutions(struct isl_sol_map *sol_map,
3819 struct isl_tab *tab)
3821 find_solutions_main(&sol_map->sol, tab);
3824 /* Check if integer division "div" of "dom" also occurs in "bmap".
3825 * If so, return its position within the divs.
3826 * If not, return -1.
3828 static int find_context_div(struct isl_basic_map *bmap,
3829 struct isl_basic_set *dom, unsigned div)
3832 unsigned b_dim = isl_dim_total(bmap->dim);
3833 unsigned d_dim = isl_dim_total(dom->dim);
3835 if (isl_int_is_zero(dom->div[div][0]))
3837 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3840 for (i = 0; i < bmap->n_div; ++i) {
3841 if (isl_int_is_zero(bmap->div[i][0]))
3843 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3844 (b_dim - d_dim) + bmap->n_div) != -1)
3846 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3852 /* The correspondence between the variables in the main tableau,
3853 * the context tableau, and the input map and domain is as follows.
3854 * The first n_param and the last n_div variables of the main tableau
3855 * form the variables of the context tableau.
3856 * In the basic map, these n_param variables correspond to the
3857 * parameters and the input dimensions. In the domain, they correspond
3858 * to the parameters and the set dimensions.
3859 * The n_div variables correspond to the integer divisions in the domain.
3860 * To ensure that everything lines up, we may need to copy some of the
3861 * integer divisions of the domain to the map. These have to be placed
3862 * in the same order as those in the context and they have to be placed
3863 * after any other integer divisions that the map may have.
3864 * This function performs the required reordering.
3866 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3867 struct isl_basic_set *dom)
3873 for (i = 0; i < dom->n_div; ++i)
3874 if (find_context_div(bmap, dom, i) != -1)
3876 other = bmap->n_div - common;
3877 if (dom->n_div - common > 0) {
3878 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
3879 dom->n_div - common, 0, 0);
3883 for (i = 0; i < dom->n_div; ++i) {
3884 int pos = find_context_div(bmap, dom, i);
3886 pos = isl_basic_map_alloc_div(bmap);
3889 isl_int_set_si(bmap->div[pos][0], 0);
3891 if (pos != other + i)
3892 isl_basic_map_swap_div(bmap, pos, other + i);
3896 isl_basic_map_free(bmap);
3900 /* Compute the lexicographic minimum (or maximum if "max" is set)
3901 * of "bmap" over the domain "dom" and return the result as a map.
3902 * If "empty" is not NULL, then *empty is assigned a set that
3903 * contains those parts of the domain where there is no solution.
3904 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
3905 * then we compute the rational optimum. Otherwise, we compute
3906 * the integral optimum.
3908 * We perform some preprocessing. As the PILP solver does not
3909 * handle implicit equalities very well, we first make sure all
3910 * the equalities are explicitly available.
3911 * We also make sure the divs in the domain are properly order,
3912 * because they will be added one by one in the given order
3913 * during the construction of the solution map.
3915 struct isl_map *isl_tab_basic_map_partial_lexopt(
3916 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3917 struct isl_set **empty, int max)
3919 struct isl_tab *tab;
3920 struct isl_map *result = NULL;
3921 struct isl_sol_map *sol_map = NULL;
3922 struct isl_context *context;
3923 struct isl_basic_map *eq;
3930 isl_assert(bmap->ctx,
3931 isl_basic_map_compatible_domain(bmap, dom), goto error);
3933 eq = isl_basic_map_copy(bmap);
3934 eq = isl_basic_map_intersect_domain(eq, isl_basic_set_copy(dom));
3935 eq = isl_basic_map_affine_hull(eq);
3936 bmap = isl_basic_map_intersect(bmap, eq);
3939 dom = isl_basic_set_order_divs(dom);
3940 bmap = align_context_divs(bmap, dom);
3942 sol_map = sol_map_init(bmap, dom, !!empty, max);
3946 context = sol_map->sol.context;
3947 if (isl_basic_set_fast_is_empty(context->op->peek_basic_set(context)))
3949 else if (isl_basic_map_fast_is_empty(bmap))
3950 sol_map_add_empty_if_needed(sol_map,
3951 isl_basic_set_copy(context->op->peek_basic_set(context)));
3953 tab = tab_for_lexmin(bmap,
3954 context->op->peek_basic_set(context), 1, max);
3955 tab = context->op->detect_nonnegative_parameters(context, tab);
3956 sol_map_find_solutions(sol_map, tab);
3958 if (sol_map->sol.error)
3961 result = isl_map_copy(sol_map->map);
3963 *empty = isl_set_copy(sol_map->empty);
3964 sol_free(&sol_map->sol);
3965 isl_basic_map_free(bmap);
3968 sol_free(&sol_map->sol);
3969 isl_basic_map_free(bmap);
3973 struct isl_sol_for {
3975 int (*fn)(__isl_take isl_basic_set *dom,
3976 __isl_take isl_mat *map, void *user);
3980 static void sol_for_free(struct isl_sol_for *sol_for)
3982 if (sol_for->sol.context)
3983 sol_for->sol.context->op->free(sol_for->sol.context);
3987 static void sol_for_free_wrap(struct isl_sol *sol)
3989 sol_for_free((struct isl_sol_for *)sol);
3992 /* Add the solution identified by the tableau and the context tableau.
3994 * See documentation of sol_add for more details.
3996 * Instead of constructing a basic map, this function calls a user
3997 * defined function with the current context as a basic set and
3998 * an affine matrix reprenting the relation between the input and output.
3999 * The number of rows in this matrix is equal to one plus the number
4000 * of output variables. The number of columns is equal to one plus
4001 * the total dimension of the context, i.e., the number of parameters,
4002 * input variables and divs. Since some of the columns in the matrix
4003 * may refer to the divs, the basic set is not simplified.
4004 * (Simplification may reorder or remove divs.)
4006 static void sol_for_add(struct isl_sol_for *sol,
4007 struct isl_basic_set *dom, struct isl_mat *M)
4009 if (sol->sol.error || !dom || !M)
4012 dom = isl_basic_set_simplify(dom);
4013 dom = isl_basic_set_finalize(dom);
4015 if (sol->fn(isl_basic_set_copy(dom), isl_mat_copy(M), sol->user) < 0)
4018 isl_basic_set_free(dom);
4022 isl_basic_set_free(dom);
4027 static void sol_for_add_wrap(struct isl_sol *sol,
4028 struct isl_basic_set *dom, struct isl_mat *M)
4030 sol_for_add((struct isl_sol_for *)sol, dom, M);
4033 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4034 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4038 struct isl_sol_for *sol_for = NULL;
4039 struct isl_dim *dom_dim;
4040 struct isl_basic_set *dom = NULL;
4042 sol_for = isl_calloc_type(bset->ctx, struct isl_sol_for);
4046 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4047 dom = isl_basic_set_universe(dom_dim);
4049 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4050 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4051 sol_for->sol.dec_level.sol = &sol_for->sol;
4053 sol_for->user = user;
4054 sol_for->sol.max = max;
4055 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4056 sol_for->sol.add = &sol_for_add_wrap;
4057 sol_for->sol.add_empty = NULL;
4058 sol_for->sol.free = &sol_for_free_wrap;
4060 sol_for->sol.context = isl_context_alloc(dom);
4061 if (!sol_for->sol.context)
4064 isl_basic_set_free(dom);
4067 isl_basic_set_free(dom);
4068 sol_for_free(sol_for);
4072 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4073 struct isl_tab *tab)
4075 find_solutions_main(&sol_for->sol, tab);
4078 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4079 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4083 struct isl_sol_for *sol_for = NULL;
4085 bmap = isl_basic_map_copy(bmap);
4089 bmap = isl_basic_map_detect_equalities(bmap);
4090 sol_for = sol_for_init(bmap, max, fn, user);
4092 if (isl_basic_map_fast_is_empty(bmap))
4095 struct isl_tab *tab;
4096 struct isl_context *context = sol_for->sol.context;
4097 tab = tab_for_lexmin(bmap,
4098 context->op->peek_basic_set(context), 1, max);
4099 tab = context->op->detect_nonnegative_parameters(context, tab);
4100 sol_for_find_solutions(sol_for, tab);
4101 if (sol_for->sol.error)
4105 sol_free(&sol_for->sol);
4106 isl_basic_map_free(bmap);
4109 sol_free(&sol_for->sol);
4110 isl_basic_map_free(bmap);
4114 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map *bmap,
4115 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4119 return isl_basic_map_foreach_lexopt(bmap, 0, fn, user);
4122 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map *bmap,
4123 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4127 return isl_basic_map_foreach_lexopt(bmap, 1, fn, user);