2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include "isl_map_private.h"
16 #include "isl_sample.h"
17 #include <isl_mat_private.h>
20 * The implementation of parametric integer linear programming in this file
21 * was inspired by the paper "Parametric Integer Programming" and the
22 * report "Solving systems of affine (in)equalities" by Paul Feautrier
25 * The strategy used for obtaining a feasible solution is different
26 * from the one used in isl_tab.c. In particular, in isl_tab.c,
27 * upon finding a constraint that is not yet satisfied, we pivot
28 * in a row that increases the constant term of row holding the
29 * constraint, making sure the sample solution remains feasible
30 * for all the constraints it already satisfied.
31 * Here, we always pivot in the row holding the constraint,
32 * choosing a column that induces the lexicographically smallest
33 * increment to the sample solution.
35 * By starting out from a sample value that is lexicographically
36 * smaller than any integer point in the problem space, the first
37 * feasible integer sample point we find will also be the lexicographically
38 * smallest. If all variables can be assumed to be non-negative,
39 * then the initial sample value may be chosen equal to zero.
40 * However, we will not make this assumption. Instead, we apply
41 * the "big parameter" trick. Any variable x is then not directly
42 * used in the tableau, but instead it its represented by another
43 * variable x' = M + x, where M is an arbitrarily large (positive)
44 * value. x' is therefore always non-negative, whatever the value of x.
45 * Taking as initial sample value x' = 0 corresponds to x = -M,
46 * which is always smaller than any possible value of x.
48 * The big parameter trick is used in the main tableau and
49 * also in the context tableau if isl_context_lex is used.
50 * In this case, each tableaus has its own big parameter.
51 * Before doing any real work, we check if all the parameters
52 * happen to be non-negative. If so, we drop the column corresponding
53 * to M from the initial context tableau.
54 * If isl_context_gbr is used, then the big parameter trick is only
55 * used in the main tableau.
59 struct isl_context_op {
60 /* detect nonnegative parameters in context and mark them in tab */
61 struct isl_tab *(*detect_nonnegative_parameters)(
62 struct isl_context *context, struct isl_tab *tab);
63 /* return temporary reference to basic set representation of context */
64 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
65 /* return temporary reference to tableau representation of context */
66 struct isl_tab *(*peek_tab)(struct isl_context *context);
67 /* add equality; check is 1 if eq may not be valid;
68 * update is 1 if we may want to call ineq_sign on context later.
70 void (*add_eq)(struct isl_context *context, isl_int *eq,
71 int check, int update);
72 /* add inequality; check is 1 if ineq may not be valid;
73 * update is 1 if we may want to call ineq_sign on context later.
75 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
76 int check, int update);
77 /* check sign of ineq based on previous information.
78 * strict is 1 if saturation should be treated as a positive sign.
80 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
81 isl_int *ineq, int strict);
82 /* check if inequality maintains feasibility */
83 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
84 /* return index of a div that corresponds to "div" */
85 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
87 /* add div "div" to context and return non-negativity */
88 int (*add_div)(struct isl_context *context, struct isl_vec *div);
89 int (*detect_equalities)(struct isl_context *context,
91 /* return row index of "best" split */
92 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
93 /* check if context has already been determined to be empty */
94 int (*is_empty)(struct isl_context *context);
95 /* check if context is still usable */
96 int (*is_ok)(struct isl_context *context);
97 /* save a copy/snapshot of context */
98 void *(*save)(struct isl_context *context);
99 /* restore saved context */
100 void (*restore)(struct isl_context *context, void *);
101 /* invalidate context */
102 void (*invalidate)(struct isl_context *context);
104 void (*free)(struct isl_context *context);
108 struct isl_context_op *op;
111 struct isl_context_lex {
112 struct isl_context context;
116 struct isl_partial_sol {
118 struct isl_basic_set *dom;
121 struct isl_partial_sol *next;
125 struct isl_sol_callback {
126 struct isl_tab_callback callback;
130 /* isl_sol is an interface for constructing a solution to
131 * a parametric integer linear programming problem.
132 * Every time the algorithm reaches a state where a solution
133 * can be read off from the tableau (including cases where the tableau
134 * is empty), the function "add" is called on the isl_sol passed
135 * to find_solutions_main.
137 * The context tableau is owned by isl_sol and is updated incrementally.
139 * There are currently two implementations of this interface,
140 * isl_sol_map, which simply collects the solutions in an isl_map
141 * and (optionally) the parts of the context where there is no solution
143 * isl_sol_for, which calls a user-defined function for each part of
152 struct isl_context *context;
153 struct isl_partial_sol *partial;
154 void (*add)(struct isl_sol *sol,
155 struct isl_basic_set *dom, struct isl_mat *M);
156 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
157 void (*free)(struct isl_sol *sol);
158 struct isl_sol_callback dec_level;
161 static void sol_free(struct isl_sol *sol)
163 struct isl_partial_sol *partial, *next;
166 for (partial = sol->partial; partial; partial = next) {
167 next = partial->next;
168 isl_basic_set_free(partial->dom);
169 isl_mat_free(partial->M);
175 /* Push a partial solution represented by a domain and mapping M
176 * onto the stack of partial solutions.
178 static void sol_push_sol(struct isl_sol *sol,
179 struct isl_basic_set *dom, struct isl_mat *M)
181 struct isl_partial_sol *partial;
183 if (sol->error || !dom)
186 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
190 partial->level = sol->level;
193 partial->next = sol->partial;
195 sol->partial = partial;
199 isl_basic_set_free(dom);
203 /* Pop one partial solution from the partial solution stack and
204 * pass it on to sol->add or sol->add_empty.
206 static void sol_pop_one(struct isl_sol *sol)
208 struct isl_partial_sol *partial;
210 partial = sol->partial;
211 sol->partial = partial->next;
214 sol->add(sol, partial->dom, partial->M);
216 sol->add_empty(sol, partial->dom);
220 /* Return a fresh copy of the domain represented by the context tableau.
222 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
224 struct isl_basic_set *bset;
229 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
230 bset = isl_basic_set_update_from_tab(bset,
231 sol->context->op->peek_tab(sol->context));
236 /* Check whether two partial solutions have the same mapping, where n_div
237 * is the number of divs that the two partial solutions have in common.
239 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
245 if (!s1->M != !s2->M)
250 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
252 for (i = 0; i < s1->M->n_row; ++i) {
253 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
254 s1->M->n_col-1-dim-n_div) != -1)
256 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
257 s2->M->n_col-1-dim-n_div) != -1)
259 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
265 /* Pop all solutions from the partial solution stack that were pushed onto
266 * the stack at levels that are deeper than the current level.
267 * If the two topmost elements on the stack have the same level
268 * and represent the same solution, then their domains are combined.
269 * This combined domain is the same as the current context domain
270 * as sol_pop is called each time we move back to a higher level.
272 static void sol_pop(struct isl_sol *sol)
274 struct isl_partial_sol *partial;
280 if (sol->level == 0) {
281 for (partial = sol->partial; partial; partial = sol->partial)
286 partial = sol->partial;
290 if (partial->level <= sol->level)
293 if (partial->next && partial->next->level == partial->level) {
294 n_div = isl_basic_set_dim(
295 sol->context->op->peek_basic_set(sol->context),
298 if (!same_solution(partial, partial->next, n_div)) {
302 struct isl_basic_set *bset;
304 bset = sol_domain(sol);
306 isl_basic_set_free(partial->next->dom);
307 partial->next->dom = bset;
308 partial->next->level = sol->level;
310 sol->partial = partial->next;
311 isl_basic_set_free(partial->dom);
312 isl_mat_free(partial->M);
319 static void sol_dec_level(struct isl_sol *sol)
329 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
331 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
333 sol_dec_level(callback->sol);
335 return callback->sol->error ? -1 : 0;
338 /* Move down to next level and push callback onto context tableau
339 * to decrease the level again when it gets rolled back across
340 * the current state. That is, dec_level will be called with
341 * the context tableau in the same state as it is when inc_level
344 static void sol_inc_level(struct isl_sol *sol)
352 tab = sol->context->op->peek_tab(sol->context);
353 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
357 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
361 if (isl_int_is_one(m))
364 for (i = 0; i < n_row; ++i)
365 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
368 /* Add the solution identified by the tableau and the context tableau.
370 * The layout of the variables is as follows.
371 * tab->n_var is equal to the total number of variables in the input
372 * map (including divs that were copied from the context)
373 * + the number of extra divs constructed
374 * Of these, the first tab->n_param and the last tab->n_div variables
375 * correspond to the variables in the context, i.e.,
376 * tab->n_param + tab->n_div = context_tab->n_var
377 * tab->n_param is equal to the number of parameters and input
378 * dimensions in the input map
379 * tab->n_div is equal to the number of divs in the context
381 * If there is no solution, then call add_empty with a basic set
382 * that corresponds to the context tableau. (If add_empty is NULL,
385 * If there is a solution, then first construct a matrix that maps
386 * all dimensions of the context to the output variables, i.e.,
387 * the output dimensions in the input map.
388 * The divs in the input map (if any) that do not correspond to any
389 * div in the context do not appear in the solution.
390 * The algorithm will make sure that they have an integer value,
391 * but these values themselves are of no interest.
392 * We have to be careful not to drop or rearrange any divs in the
393 * context because that would change the meaning of the matrix.
395 * To extract the value of the output variables, it should be noted
396 * that we always use a big parameter M in the main tableau and so
397 * the variable stored in this tableau is not an output variable x itself, but
398 * x' = M + x (in case of minimization)
400 * x' = M - x (in case of maximization)
401 * If x' appears in a column, then its optimal value is zero,
402 * which means that the optimal value of x is an unbounded number
403 * (-M for minimization and M for maximization).
404 * We currently assume that the output dimensions in the original map
405 * are bounded, so this cannot occur.
406 * Similarly, when x' appears in a row, then the coefficient of M in that
407 * row is necessarily 1.
408 * If the row in the tableau represents
409 * d x' = c + d M + e(y)
410 * then, in case of minimization, the corresponding row in the matrix
413 * with a d = m, the (updated) common denominator of the matrix.
414 * In case of maximization, the row will be
417 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
419 struct isl_basic_set *bset = NULL;
420 struct isl_mat *mat = NULL;
425 if (sol->error || !tab)
428 if (tab->empty && !sol->add_empty)
431 bset = sol_domain(sol);
434 sol_push_sol(sol, bset, NULL);
440 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
441 1 + tab->n_param + tab->n_div);
447 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
448 isl_int_set_si(mat->row[0][0], 1);
449 for (row = 0; row < sol->n_out; ++row) {
450 int i = tab->n_param + row;
453 isl_seq_clr(mat->row[1 + row], mat->n_col);
454 if (!tab->var[i].is_row) {
456 isl_die(mat->ctx, isl_error_invalid,
457 "unbounded optimum", goto error2);
461 r = tab->var[i].index;
463 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
464 isl_die(mat->ctx, isl_error_invalid,
465 "unbounded optimum", goto error2);
466 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
467 isl_int_divexact(m, tab->mat->row[r][0], m);
468 scale_rows(mat, m, 1 + row);
469 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
470 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
471 for (j = 0; j < tab->n_param; ++j) {
473 if (tab->var[j].is_row)
475 col = tab->var[j].index;
476 isl_int_mul(mat->row[1 + row][1 + j], m,
477 tab->mat->row[r][off + col]);
479 for (j = 0; j < tab->n_div; ++j) {
481 if (tab->var[tab->n_var - tab->n_div+j].is_row)
483 col = tab->var[tab->n_var - tab->n_div+j].index;
484 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
485 tab->mat->row[r][off + col]);
488 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
494 sol_push_sol(sol, bset, mat);
499 isl_basic_set_free(bset);
507 struct isl_set *empty;
510 static void sol_map_free(struct isl_sol_map *sol_map)
514 if (sol_map->sol.context)
515 sol_map->sol.context->op->free(sol_map->sol.context);
516 isl_map_free(sol_map->map);
517 isl_set_free(sol_map->empty);
521 static void sol_map_free_wrap(struct isl_sol *sol)
523 sol_map_free((struct isl_sol_map *)sol);
526 /* This function is called for parts of the context where there is
527 * no solution, with "bset" corresponding to the context tableau.
528 * Simply add the basic set to the set "empty".
530 static void sol_map_add_empty(struct isl_sol_map *sol,
531 struct isl_basic_set *bset)
535 isl_assert(bset->ctx, sol->empty, goto error);
537 sol->empty = isl_set_grow(sol->empty, 1);
538 bset = isl_basic_set_simplify(bset);
539 bset = isl_basic_set_finalize(bset);
540 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
543 isl_basic_set_free(bset);
546 isl_basic_set_free(bset);
550 static void sol_map_add_empty_wrap(struct isl_sol *sol,
551 struct isl_basic_set *bset)
553 sol_map_add_empty((struct isl_sol_map *)sol, bset);
556 /* Add bset to sol's empty, but only if we are actually collecting
559 static void sol_map_add_empty_if_needed(struct isl_sol_map *sol,
560 struct isl_basic_set *bset)
563 sol_map_add_empty(sol, bset);
565 isl_basic_set_free(bset);
568 /* Given a basic map "dom" that represents the context and an affine
569 * matrix "M" that maps the dimensions of the context to the
570 * output variables, construct a basic map with the same parameters
571 * and divs as the context, the dimensions of the context as input
572 * dimensions and a number of output dimensions that is equal to
573 * the number of output dimensions in the input map.
575 * The constraints and divs of the context are simply copied
576 * from "dom". For each row
580 * is added, with d the common denominator of M.
582 static void sol_map_add(struct isl_sol_map *sol,
583 struct isl_basic_set *dom, struct isl_mat *M)
586 struct isl_basic_map *bmap = NULL;
587 isl_basic_set *context_bset;
595 if (sol->sol.error || !dom || !M)
598 n_out = sol->sol.n_out;
599 n_eq = dom->n_eq + n_out;
600 n_ineq = dom->n_ineq;
602 nparam = isl_basic_set_total_dim(dom) - n_div;
603 total = isl_map_dim(sol->map, isl_dim_all);
604 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
605 n_div, n_eq, 2 * n_div + n_ineq);
608 if (sol->sol.rational)
609 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
610 for (i = 0; i < dom->n_div; ++i) {
611 int k = isl_basic_map_alloc_div(bmap);
614 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
615 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
616 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
617 dom->div[i] + 1 + 1 + nparam, i);
619 for (i = 0; i < dom->n_eq; ++i) {
620 int k = isl_basic_map_alloc_equality(bmap);
623 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
624 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
625 isl_seq_cpy(bmap->eq[k] + 1 + total,
626 dom->eq[i] + 1 + nparam, n_div);
628 for (i = 0; i < dom->n_ineq; ++i) {
629 int k = isl_basic_map_alloc_inequality(bmap);
632 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
633 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
634 isl_seq_cpy(bmap->ineq[k] + 1 + total,
635 dom->ineq[i] + 1 + nparam, n_div);
637 for (i = 0; i < M->n_row - 1; ++i) {
638 int k = isl_basic_map_alloc_equality(bmap);
641 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
642 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
643 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
644 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
645 M->row[1 + i] + 1 + nparam, n_div);
647 bmap = isl_basic_map_simplify(bmap);
648 bmap = isl_basic_map_finalize(bmap);
649 sol->map = isl_map_grow(sol->map, 1);
650 sol->map = isl_map_add_basic_map(sol->map, bmap);
653 isl_basic_set_free(dom);
657 isl_basic_set_free(dom);
659 isl_basic_map_free(bmap);
663 static void sol_map_add_wrap(struct isl_sol *sol,
664 struct isl_basic_set *dom, struct isl_mat *M)
666 sol_map_add((struct isl_sol_map *)sol, dom, M);
670 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
671 * i.e., the constant term and the coefficients of all variables that
672 * appear in the context tableau.
673 * Note that the coefficient of the big parameter M is NOT copied.
674 * The context tableau may not have a big parameter and even when it
675 * does, it is a different big parameter.
677 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
680 unsigned off = 2 + tab->M;
682 isl_int_set(line[0], tab->mat->row[row][1]);
683 for (i = 0; i < tab->n_param; ++i) {
684 if (tab->var[i].is_row)
685 isl_int_set_si(line[1 + i], 0);
687 int col = tab->var[i].index;
688 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
691 for (i = 0; i < tab->n_div; ++i) {
692 if (tab->var[tab->n_var - tab->n_div + i].is_row)
693 isl_int_set_si(line[1 + tab->n_param + i], 0);
695 int col = tab->var[tab->n_var - tab->n_div + i].index;
696 isl_int_set(line[1 + tab->n_param + i],
697 tab->mat->row[row][off + col]);
702 /* Check if rows "row1" and "row2" have identical "parametric constants",
703 * as explained above.
704 * In this case, we also insist that the coefficients of the big parameter
705 * be the same as the values of the constants will only be the same
706 * if these coefficients are also the same.
708 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
711 unsigned off = 2 + tab->M;
713 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
716 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
717 tab->mat->row[row2][2]))
720 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
721 int pos = i < tab->n_param ? i :
722 tab->n_var - tab->n_div + i - tab->n_param;
725 if (tab->var[pos].is_row)
727 col = tab->var[pos].index;
728 if (isl_int_ne(tab->mat->row[row1][off + col],
729 tab->mat->row[row2][off + col]))
735 /* Return an inequality that expresses that the "parametric constant"
736 * should be non-negative.
737 * This function is only called when the coefficient of the big parameter
740 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
742 struct isl_vec *ineq;
744 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
748 get_row_parameter_line(tab, row, ineq->el);
750 ineq = isl_vec_normalize(ineq);
755 /* Return a integer division for use in a parametric cut based on the given row.
756 * In particular, let the parametric constant of the row be
760 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
761 * The div returned is equal to
763 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
765 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
769 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
773 isl_int_set(div->el[0], tab->mat->row[row][0]);
774 get_row_parameter_line(tab, row, div->el + 1);
775 div = isl_vec_normalize(div);
776 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
777 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
782 /* Return a integer division for use in transferring an integrality constraint
784 * In particular, let the parametric constant of the row be
788 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
789 * The the returned div is equal to
791 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
793 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
797 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
801 isl_int_set(div->el[0], tab->mat->row[row][0]);
802 get_row_parameter_line(tab, row, div->el + 1);
803 div = isl_vec_normalize(div);
804 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
809 /* Construct and return an inequality that expresses an upper bound
811 * In particular, if the div is given by
815 * then the inequality expresses
819 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
823 struct isl_vec *ineq;
828 total = isl_basic_set_total_dim(bset);
829 div_pos = 1 + total - bset->n_div + div;
831 ineq = isl_vec_alloc(bset->ctx, 1 + total);
835 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
836 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
840 /* Given a row in the tableau and a div that was created
841 * using get_row_split_div and that been constrained to equality, i.e.,
843 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
845 * replace the expression "\sum_i {a_i} y_i" in the row by d,
846 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
847 * The coefficients of the non-parameters in the tableau have been
848 * verified to be integral. We can therefore simply replace coefficient b
849 * by floor(b). For the coefficients of the parameters we have
850 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
853 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
855 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
856 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
858 isl_int_set_si(tab->mat->row[row][0], 1);
860 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
861 int drow = tab->var[tab->n_var - tab->n_div + div].index;
863 isl_assert(tab->mat->ctx,
864 isl_int_is_one(tab->mat->row[drow][0]), goto error);
865 isl_seq_combine(tab->mat->row[row] + 1,
866 tab->mat->ctx->one, tab->mat->row[row] + 1,
867 tab->mat->ctx->one, tab->mat->row[drow] + 1,
868 1 + tab->M + tab->n_col);
870 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
872 isl_int_set_si(tab->mat->row[row][2 + tab->M + dcol], 1);
881 /* Check if the (parametric) constant of the given row is obviously
882 * negative, meaning that we don't need to consult the context tableau.
883 * If there is a big parameter and its coefficient is non-zero,
884 * then this coefficient determines the outcome.
885 * Otherwise, we check whether the constant is negative and
886 * all non-zero coefficients of parameters are negative and
887 * belong to non-negative parameters.
889 static int is_obviously_neg(struct isl_tab *tab, int row)
893 unsigned off = 2 + tab->M;
896 if (isl_int_is_pos(tab->mat->row[row][2]))
898 if (isl_int_is_neg(tab->mat->row[row][2]))
902 if (isl_int_is_nonneg(tab->mat->row[row][1]))
904 for (i = 0; i < tab->n_param; ++i) {
905 /* Eliminated parameter */
906 if (tab->var[i].is_row)
908 col = tab->var[i].index;
909 if (isl_int_is_zero(tab->mat->row[row][off + col]))
911 if (!tab->var[i].is_nonneg)
913 if (isl_int_is_pos(tab->mat->row[row][off + col]))
916 for (i = 0; i < tab->n_div; ++i) {
917 if (tab->var[tab->n_var - tab->n_div + i].is_row)
919 col = tab->var[tab->n_var - tab->n_div + i].index;
920 if (isl_int_is_zero(tab->mat->row[row][off + col]))
922 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
924 if (isl_int_is_pos(tab->mat->row[row][off + col]))
930 /* Check if the (parametric) constant of the given row is obviously
931 * non-negative, meaning that we don't need to consult the context tableau.
932 * If there is a big parameter and its coefficient is non-zero,
933 * then this coefficient determines the outcome.
934 * Otherwise, we check whether the constant is non-negative and
935 * all non-zero coefficients of parameters are positive and
936 * belong to non-negative parameters.
938 static int is_obviously_nonneg(struct isl_tab *tab, int row)
942 unsigned off = 2 + tab->M;
945 if (isl_int_is_pos(tab->mat->row[row][2]))
947 if (isl_int_is_neg(tab->mat->row[row][2]))
951 if (isl_int_is_neg(tab->mat->row[row][1]))
953 for (i = 0; i < tab->n_param; ++i) {
954 /* Eliminated parameter */
955 if (tab->var[i].is_row)
957 col = tab->var[i].index;
958 if (isl_int_is_zero(tab->mat->row[row][off + col]))
960 if (!tab->var[i].is_nonneg)
962 if (isl_int_is_neg(tab->mat->row[row][off + col]))
965 for (i = 0; i < tab->n_div; ++i) {
966 if (tab->var[tab->n_var - tab->n_div + i].is_row)
968 col = tab->var[tab->n_var - tab->n_div + i].index;
969 if (isl_int_is_zero(tab->mat->row[row][off + col]))
971 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
973 if (isl_int_is_neg(tab->mat->row[row][off + col]))
979 /* Given a row r and two columns, return the column that would
980 * lead to the lexicographically smallest increment in the sample
981 * solution when leaving the basis in favor of the row.
982 * Pivoting with column c will increment the sample value by a non-negative
983 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
984 * corresponding to the non-parametric variables.
985 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
986 * with all other entries in this virtual row equal to zero.
987 * If variable v appears in a row, then a_{v,c} is the element in column c
990 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
991 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
992 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
993 * increment. Otherwise, it's c2.
995 static int lexmin_col_pair(struct isl_tab *tab,
996 int row, int col1, int col2, isl_int tmp)
1001 tr = tab->mat->row[row] + 2 + tab->M;
1003 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1007 if (!tab->var[i].is_row) {
1008 if (tab->var[i].index == col1)
1010 if (tab->var[i].index == col2)
1015 if (tab->var[i].index == row)
1018 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1019 s1 = isl_int_sgn(r[col1]);
1020 s2 = isl_int_sgn(r[col2]);
1021 if (s1 == 0 && s2 == 0)
1028 isl_int_mul(tmp, r[col2], tr[col1]);
1029 isl_int_submul(tmp, r[col1], tr[col2]);
1030 if (isl_int_is_pos(tmp))
1032 if (isl_int_is_neg(tmp))
1038 /* Given a row in the tableau, find and return the column that would
1039 * result in the lexicographically smallest, but positive, increment
1040 * in the sample point.
1041 * If there is no such column, then return tab->n_col.
1042 * If anything goes wrong, return -1.
1044 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1047 int col = tab->n_col;
1051 tr = tab->mat->row[row] + 2 + tab->M;
1055 for (j = tab->n_dead; j < tab->n_col; ++j) {
1056 if (tab->col_var[j] >= 0 &&
1057 (tab->col_var[j] < tab->n_param ||
1058 tab->col_var[j] >= tab->n_var - tab->n_div))
1061 if (!isl_int_is_pos(tr[j]))
1064 if (col == tab->n_col)
1067 col = lexmin_col_pair(tab, row, col, j, tmp);
1068 isl_assert(tab->mat->ctx, col >= 0, goto error);
1078 /* Return the first known violated constraint, i.e., a non-negative
1079 * constraint that currently has an either obviously negative value
1080 * or a previously determined to be negative value.
1082 * If any constraint has a negative coefficient for the big parameter,
1083 * if any, then we return one of these first.
1085 static int first_neg(struct isl_tab *tab)
1090 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1091 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1093 if (!isl_int_is_neg(tab->mat->row[row][2]))
1096 tab->row_sign[row] = isl_tab_row_neg;
1099 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1100 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1102 if (tab->row_sign) {
1103 if (tab->row_sign[row] == 0 &&
1104 is_obviously_neg(tab, row))
1105 tab->row_sign[row] = isl_tab_row_neg;
1106 if (tab->row_sign[row] != isl_tab_row_neg)
1108 } else if (!is_obviously_neg(tab, row))
1115 /* Resolve all known or obviously violated constraints through pivoting.
1116 * In particular, as long as we can find any violated constraint, we
1117 * look for a pivoting column that would result in the lexicographically
1118 * smallest increment in the sample point. If there is no such column
1119 * then the tableau is infeasible.
1121 static struct isl_tab *restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1122 static struct isl_tab *restore_lexmin(struct isl_tab *tab)
1130 while ((row = first_neg(tab)) != -1) {
1131 col = lexmin_pivot_col(tab, row);
1132 if (col >= tab->n_col) {
1133 if (isl_tab_mark_empty(tab) < 0)
1139 if (isl_tab_pivot(tab, row, col) < 0)
1148 /* Given a row that represents an equality, look for an appropriate
1150 * In particular, if there are any non-zero coefficients among
1151 * the non-parameter variables, then we take the last of these
1152 * variables. Eliminating this variable in terms of the other
1153 * variables and/or parameters does not influence the property
1154 * that all column in the initial tableau are lexicographically
1155 * positive. The row corresponding to the eliminated variable
1156 * will only have non-zero entries below the diagonal of the
1157 * initial tableau. That is, we transform
1163 * If there is no such non-parameter variable, then we are dealing with
1164 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1165 * for elimination. This will ensure that the eliminated parameter
1166 * always has an integer value whenever all the other parameters are integral.
1167 * If there is no such parameter then we return -1.
1169 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1171 unsigned off = 2 + tab->M;
1174 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1176 if (tab->var[i].is_row)
1178 col = tab->var[i].index;
1179 if (col <= tab->n_dead)
1181 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1184 for (i = tab->n_dead; i < tab->n_col; ++i) {
1185 if (isl_int_is_one(tab->mat->row[row][off + i]))
1187 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1193 /* Add an equality that is known to be valid to the tableau.
1194 * We first check if we can eliminate a variable or a parameter.
1195 * If not, we add the equality as two inequalities.
1196 * In this case, the equality was a pure parameter equality and there
1197 * is no need to resolve any constraint violations.
1199 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1206 r = isl_tab_add_row(tab, eq);
1210 r = tab->con[r].index;
1211 i = last_var_col_or_int_par_col(tab, r);
1213 tab->con[r].is_nonneg = 1;
1214 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1216 isl_seq_neg(eq, eq, 1 + tab->n_var);
1217 r = isl_tab_add_row(tab, eq);
1220 tab->con[r].is_nonneg = 1;
1221 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1224 if (isl_tab_pivot(tab, r, i) < 0)
1226 if (isl_tab_kill_col(tab, i) < 0)
1237 /* Check if the given row is a pure constant.
1239 static int is_constant(struct isl_tab *tab, int row)
1241 unsigned off = 2 + tab->M;
1243 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1244 tab->n_col - tab->n_dead) == -1;
1247 /* Add an equality that may or may not be valid to the tableau.
1248 * If the resulting row is a pure constant, then it must be zero.
1249 * Otherwise, the resulting tableau is empty.
1251 * If the row is not a pure constant, then we add two inequalities,
1252 * each time checking that they can be satisfied.
1253 * In the end we try to use one of the two constraints to eliminate
1256 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1257 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1261 struct isl_tab_undo *snap;
1265 snap = isl_tab_snap(tab);
1266 r1 = isl_tab_add_row(tab, eq);
1269 tab->con[r1].is_nonneg = 1;
1270 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1273 row = tab->con[r1].index;
1274 if (is_constant(tab, row)) {
1275 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1276 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1277 if (isl_tab_mark_empty(tab) < 0)
1281 if (isl_tab_rollback(tab, snap) < 0)
1286 tab = restore_lexmin(tab);
1287 if (!tab || tab->empty)
1290 isl_seq_neg(eq, eq, 1 + tab->n_var);
1292 r2 = isl_tab_add_row(tab, eq);
1295 tab->con[r2].is_nonneg = 1;
1296 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1299 tab = restore_lexmin(tab);
1300 if (!tab || tab->empty)
1303 if (!tab->con[r1].is_row) {
1304 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1306 } else if (!tab->con[r2].is_row) {
1307 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1309 } else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
1310 unsigned off = 2 + tab->M;
1312 int row = tab->con[r1].index;
1313 i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
1314 tab->n_col - tab->n_dead);
1316 if (isl_tab_pivot(tab, row, tab->n_dead + i) < 0)
1318 if (isl_tab_kill_col(tab, tab->n_dead + i) < 0)
1324 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1325 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1327 isl_seq_neg(eq, eq, 1 + tab->n_var);
1328 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1329 isl_seq_neg(eq, eq, 1 + tab->n_var);
1330 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1342 /* Add an inequality to the tableau, resolving violations using
1345 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1352 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1353 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1358 r = isl_tab_add_row(tab, ineq);
1361 tab->con[r].is_nonneg = 1;
1362 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1364 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1365 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1370 tab = restore_lexmin(tab);
1371 if (tab && !tab->empty && tab->con[r].is_row &&
1372 isl_tab_row_is_redundant(tab, tab->con[r].index))
1373 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1381 /* Check if the coefficients of the parameters are all integral.
1383 static int integer_parameter(struct isl_tab *tab, int row)
1387 unsigned off = 2 + tab->M;
1389 for (i = 0; i < tab->n_param; ++i) {
1390 /* Eliminated parameter */
1391 if (tab->var[i].is_row)
1393 col = tab->var[i].index;
1394 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1395 tab->mat->row[row][0]))
1398 for (i = 0; i < tab->n_div; ++i) {
1399 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1401 col = tab->var[tab->n_var - tab->n_div + i].index;
1402 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1403 tab->mat->row[row][0]))
1409 /* Check if the coefficients of the non-parameter variables are all integral.
1411 static int integer_variable(struct isl_tab *tab, int row)
1414 unsigned off = 2 + tab->M;
1416 for (i = tab->n_dead; i < tab->n_col; ++i) {
1417 if (tab->col_var[i] >= 0 &&
1418 (tab->col_var[i] < tab->n_param ||
1419 tab->col_var[i] >= tab->n_var - tab->n_div))
1421 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1422 tab->mat->row[row][0]))
1428 /* Check if the constant term is integral.
1430 static int integer_constant(struct isl_tab *tab, int row)
1432 return isl_int_is_divisible_by(tab->mat->row[row][1],
1433 tab->mat->row[row][0]);
1436 #define I_CST 1 << 0
1437 #define I_PAR 1 << 1
1438 #define I_VAR 1 << 2
1440 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1441 * that is non-integer and therefore requires a cut and return
1442 * the index of the variable.
1443 * For parametric tableaus, there are three parts in a row,
1444 * the constant, the coefficients of the parameters and the rest.
1445 * For each part, we check whether the coefficients in that part
1446 * are all integral and if so, set the corresponding flag in *f.
1447 * If the constant and the parameter part are integral, then the
1448 * current sample value is integral and no cut is required
1449 * (irrespective of whether the variable part is integral).
1451 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1453 var = var < 0 ? tab->n_param : var + 1;
1455 for (; var < tab->n_var - tab->n_div; ++var) {
1458 if (!tab->var[var].is_row)
1460 row = tab->var[var].index;
1461 if (integer_constant(tab, row))
1462 ISL_FL_SET(flags, I_CST);
1463 if (integer_parameter(tab, row))
1464 ISL_FL_SET(flags, I_PAR);
1465 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1467 if (integer_variable(tab, row))
1468 ISL_FL_SET(flags, I_VAR);
1475 /* Check for first (non-parameter) variable that is non-integer and
1476 * therefore requires a cut and return the corresponding row.
1477 * For parametric tableaus, there are three parts in a row,
1478 * the constant, the coefficients of the parameters and the rest.
1479 * For each part, we check whether the coefficients in that part
1480 * are all integral and if so, set the corresponding flag in *f.
1481 * If the constant and the parameter part are integral, then the
1482 * current sample value is integral and no cut is required
1483 * (irrespective of whether the variable part is integral).
1485 static int first_non_integer_row(struct isl_tab *tab, int *f)
1487 int var = next_non_integer_var(tab, -1, f);
1489 return var < 0 ? -1 : tab->var[var].index;
1492 /* Add a (non-parametric) cut to cut away the non-integral sample
1493 * value of the given row.
1495 * If the row is given by
1497 * m r = f + \sum_i a_i y_i
1501 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1503 * The big parameter, if any, is ignored, since it is assumed to be big
1504 * enough to be divisible by any integer.
1505 * If the tableau is actually a parametric tableau, then this function
1506 * is only called when all coefficients of the parameters are integral.
1507 * The cut therefore has zero coefficients for the parameters.
1509 * The current value is known to be negative, so row_sign, if it
1510 * exists, is set accordingly.
1512 * Return the row of the cut or -1.
1514 static int add_cut(struct isl_tab *tab, int row)
1519 unsigned off = 2 + tab->M;
1521 if (isl_tab_extend_cons(tab, 1) < 0)
1523 r = isl_tab_allocate_con(tab);
1527 r_row = tab->mat->row[tab->con[r].index];
1528 isl_int_set(r_row[0], tab->mat->row[row][0]);
1529 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1530 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1531 isl_int_neg(r_row[1], r_row[1]);
1533 isl_int_set_si(r_row[2], 0);
1534 for (i = 0; i < tab->n_col; ++i)
1535 isl_int_fdiv_r(r_row[off + i],
1536 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1538 tab->con[r].is_nonneg = 1;
1539 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1542 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1544 return tab->con[r].index;
1547 /* Given a non-parametric tableau, add cuts until an integer
1548 * sample point is obtained or until the tableau is determined
1549 * to be integer infeasible.
1550 * As long as there is any non-integer value in the sample point,
1551 * we add appropriate cuts, if possible, for each of these
1552 * non-integer values and then resolve the violated
1553 * cut constraints using restore_lexmin.
1554 * If one of the corresponding rows is equal to an integral
1555 * combination of variables/constraints plus a non-integral constant,
1556 * then there is no way to obtain an integer point and we return
1557 * a tableau that is marked empty.
1559 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
1570 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1572 if (ISL_FL_ISSET(flags, I_VAR)) {
1573 if (isl_tab_mark_empty(tab) < 0)
1577 row = tab->var[var].index;
1578 row = add_cut(tab, row);
1581 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1582 tab = restore_lexmin(tab);
1583 if (!tab || tab->empty)
1592 /* Check whether all the currently active samples also satisfy the inequality
1593 * "ineq" (treated as an equality if eq is set).
1594 * Remove those samples that do not.
1596 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1604 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1605 isl_assert(tab->mat->ctx, tab->samples, goto error);
1606 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1609 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1611 isl_seq_inner_product(ineq, tab->samples->row[i],
1612 1 + tab->n_var, &v);
1613 sgn = isl_int_sgn(v);
1614 if (eq ? (sgn == 0) : (sgn >= 0))
1616 tab = isl_tab_drop_sample(tab, i);
1628 /* Check whether the sample value of the tableau is finite,
1629 * i.e., either the tableau does not use a big parameter, or
1630 * all values of the variables are equal to the big parameter plus
1631 * some constant. This constant is the actual sample value.
1633 static int sample_is_finite(struct isl_tab *tab)
1640 for (i = 0; i < tab->n_var; ++i) {
1642 if (!tab->var[i].is_row)
1644 row = tab->var[i].index;
1645 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1651 /* Check if the context tableau of sol has any integer points.
1652 * Leave tab in empty state if no integer point can be found.
1653 * If an integer point can be found and if moreover it is finite,
1654 * then it is added to the list of sample values.
1656 * This function is only called when none of the currently active sample
1657 * values satisfies the most recently added constraint.
1659 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1661 struct isl_tab_undo *snap;
1667 snap = isl_tab_snap(tab);
1668 if (isl_tab_push_basis(tab) < 0)
1671 tab = cut_to_integer_lexmin(tab);
1675 if (!tab->empty && sample_is_finite(tab)) {
1676 struct isl_vec *sample;
1678 sample = isl_tab_get_sample_value(tab);
1680 tab = isl_tab_add_sample(tab, sample);
1683 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1692 /* Check if any of the currently active sample values satisfies
1693 * the inequality "ineq" (an equality if eq is set).
1695 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1703 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1704 isl_assert(tab->mat->ctx, tab->samples, return -1);
1705 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1708 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1710 isl_seq_inner_product(ineq, tab->samples->row[i],
1711 1 + tab->n_var, &v);
1712 sgn = isl_int_sgn(v);
1713 if (eq ? (sgn == 0) : (sgn >= 0))
1718 return i < tab->n_sample;
1721 /* Add a div specified by "div" to the tableau "tab" and return
1722 * 1 if the div is obviously non-negative.
1724 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1725 int (*add_ineq)(void *user, isl_int *), void *user)
1729 struct isl_mat *samples;
1732 r = isl_tab_add_div(tab, div, add_ineq, user);
1735 nonneg = tab->var[r].is_nonneg;
1736 tab->var[r].frozen = 1;
1738 samples = isl_mat_extend(tab->samples,
1739 tab->n_sample, 1 + tab->n_var);
1740 tab->samples = samples;
1743 for (i = tab->n_outside; i < samples->n_row; ++i) {
1744 isl_seq_inner_product(div->el + 1, samples->row[i],
1745 div->size - 1, &samples->row[i][samples->n_col - 1]);
1746 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1747 samples->row[i][samples->n_col - 1], div->el[0]);
1753 /* Add a div specified by "div" to both the main tableau and
1754 * the context tableau. In case of the main tableau, we only
1755 * need to add an extra div. In the context tableau, we also
1756 * need to express the meaning of the div.
1757 * Return the index of the div or -1 if anything went wrong.
1759 static int add_div(struct isl_tab *tab, struct isl_context *context,
1760 struct isl_vec *div)
1765 if ((nonneg = context->op->add_div(context, div)) < 0)
1768 if (!context->op->is_ok(context))
1771 if (isl_tab_extend_vars(tab, 1) < 0)
1773 r = isl_tab_allocate_var(tab);
1777 tab->var[r].is_nonneg = 1;
1778 tab->var[r].frozen = 1;
1781 return tab->n_div - 1;
1783 context->op->invalidate(context);
1787 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1790 unsigned total = isl_basic_map_total_dim(tab->bmap);
1792 for (i = 0; i < tab->bmap->n_div; ++i) {
1793 if (isl_int_ne(tab->bmap->div[i][0], denom))
1795 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1802 /* Return the index of a div that corresponds to "div".
1803 * We first check if we already have such a div and if not, we create one.
1805 static int get_div(struct isl_tab *tab, struct isl_context *context,
1806 struct isl_vec *div)
1809 struct isl_tab *context_tab = context->op->peek_tab(context);
1814 d = find_div(context_tab, div->el + 1, div->el[0]);
1818 return add_div(tab, context, div);
1821 /* Add a parametric cut to cut away the non-integral sample value
1823 * Let a_i be the coefficients of the constant term and the parameters
1824 * and let b_i be the coefficients of the variables or constraints
1825 * in basis of the tableau.
1826 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1828 * The cut is expressed as
1830 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1832 * If q did not already exist in the context tableau, then it is added first.
1833 * If q is in a column of the main tableau then the "+ q" can be accomplished
1834 * by setting the corresponding entry to the denominator of the constraint.
1835 * If q happens to be in a row of the main tableau, then the corresponding
1836 * row needs to be added instead (taking care of the denominators).
1837 * Note that this is very unlikely, but perhaps not entirely impossible.
1839 * The current value of the cut is known to be negative (or at least
1840 * non-positive), so row_sign is set accordingly.
1842 * Return the row of the cut or -1.
1844 static int add_parametric_cut(struct isl_tab *tab, int row,
1845 struct isl_context *context)
1847 struct isl_vec *div;
1854 unsigned off = 2 + tab->M;
1859 div = get_row_parameter_div(tab, row);
1864 d = context->op->get_div(context, tab, div);
1868 if (isl_tab_extend_cons(tab, 1) < 0)
1870 r = isl_tab_allocate_con(tab);
1874 r_row = tab->mat->row[tab->con[r].index];
1875 isl_int_set(r_row[0], tab->mat->row[row][0]);
1876 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1877 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1878 isl_int_neg(r_row[1], r_row[1]);
1880 isl_int_set_si(r_row[2], 0);
1881 for (i = 0; i < tab->n_param; ++i) {
1882 if (tab->var[i].is_row)
1884 col = tab->var[i].index;
1885 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1886 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1887 tab->mat->row[row][0]);
1888 isl_int_neg(r_row[off + col], r_row[off + col]);
1890 for (i = 0; i < tab->n_div; ++i) {
1891 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1893 col = tab->var[tab->n_var - tab->n_div + i].index;
1894 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1895 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1896 tab->mat->row[row][0]);
1897 isl_int_neg(r_row[off + col], r_row[off + col]);
1899 for (i = 0; i < tab->n_col; ++i) {
1900 if (tab->col_var[i] >= 0 &&
1901 (tab->col_var[i] < tab->n_param ||
1902 tab->col_var[i] >= tab->n_var - tab->n_div))
1904 isl_int_fdiv_r(r_row[off + i],
1905 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1907 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1909 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1911 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1912 isl_int_divexact(r_row[0], r_row[0], gcd);
1913 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1914 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1915 r_row[0], tab->mat->row[d_row] + 1,
1916 off - 1 + tab->n_col);
1917 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1920 col = tab->var[tab->n_var - tab->n_div + d].index;
1921 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1924 tab->con[r].is_nonneg = 1;
1925 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1928 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1932 row = tab->con[r].index;
1934 if (d >= n && context->op->detect_equalities(context, tab) < 0)
1940 /* Construct a tableau for bmap that can be used for computing
1941 * the lexicographic minimum (or maximum) of bmap.
1942 * If not NULL, then dom is the domain where the minimum
1943 * should be computed. In this case, we set up a parametric
1944 * tableau with row signs (initialized to "unknown").
1945 * If M is set, then the tableau will use a big parameter.
1946 * If max is set, then a maximum should be computed instead of a minimum.
1947 * This means that for each variable x, the tableau will contain the variable
1948 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1949 * of the variables in all constraints are negated prior to adding them
1952 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
1953 struct isl_basic_set *dom, unsigned M, int max)
1956 struct isl_tab *tab;
1958 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
1959 isl_basic_map_total_dim(bmap), M);
1963 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1965 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
1966 tab->n_div = dom->n_div;
1967 tab->row_sign = isl_calloc_array(bmap->ctx,
1968 enum isl_tab_row_sign, tab->mat->n_row);
1972 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
1973 if (isl_tab_mark_empty(tab) < 0)
1978 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1979 tab->var[i].is_nonneg = 1;
1980 tab->var[i].frozen = 1;
1982 for (i = 0; i < bmap->n_eq; ++i) {
1984 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1985 bmap->eq[i] + 1 + tab->n_param,
1986 tab->n_var - tab->n_param - tab->n_div);
1987 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
1989 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1990 bmap->eq[i] + 1 + tab->n_param,
1991 tab->n_var - tab->n_param - tab->n_div);
1992 if (!tab || tab->empty)
1996 tab = restore_lexmin(tab);
1997 for (i = 0; i < bmap->n_ineq; ++i) {
1999 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2000 bmap->ineq[i] + 1 + tab->n_param,
2001 tab->n_var - tab->n_param - tab->n_div);
2002 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2004 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2005 bmap->ineq[i] + 1 + tab->n_param,
2006 tab->n_var - tab->n_param - tab->n_div);
2007 if (!tab || tab->empty)
2016 /* Given a main tableau where more than one row requires a split,
2017 * determine and return the "best" row to split on.
2019 * Given two rows in the main tableau, if the inequality corresponding
2020 * to the first row is redundant with respect to that of the second row
2021 * in the current tableau, then it is better to split on the second row,
2022 * since in the positive part, both row will be positive.
2023 * (In the negative part a pivot will have to be performed and just about
2024 * anything can happen to the sign of the other row.)
2026 * As a simple heuristic, we therefore select the row that makes the most
2027 * of the other rows redundant.
2029 * Perhaps it would also be useful to look at the number of constraints
2030 * that conflict with any given constraint.
2032 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2034 struct isl_tab_undo *snap;
2040 if (isl_tab_extend_cons(context_tab, 2) < 0)
2043 snap = isl_tab_snap(context_tab);
2045 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2046 struct isl_tab_undo *snap2;
2047 struct isl_vec *ineq = NULL;
2051 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2053 if (tab->row_sign[split] != isl_tab_row_any)
2056 ineq = get_row_parameter_ineq(tab, split);
2059 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2064 snap2 = isl_tab_snap(context_tab);
2066 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2067 struct isl_tab_var *var;
2071 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2073 if (tab->row_sign[row] != isl_tab_row_any)
2076 ineq = get_row_parameter_ineq(tab, row);
2079 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2083 var = &context_tab->con[context_tab->n_con - 1];
2084 if (!context_tab->empty &&
2085 !isl_tab_min_at_most_neg_one(context_tab, var))
2087 if (isl_tab_rollback(context_tab, snap2) < 0)
2090 if (best == -1 || r > best_r) {
2094 if (isl_tab_rollback(context_tab, snap) < 0)
2101 static struct isl_basic_set *context_lex_peek_basic_set(
2102 struct isl_context *context)
2104 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2107 return isl_tab_peek_bset(clex->tab);
2110 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2112 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2116 static void context_lex_extend(struct isl_context *context, int n)
2118 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2121 if (isl_tab_extend_cons(clex->tab, n) >= 0)
2123 isl_tab_free(clex->tab);
2127 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2128 int check, int update)
2130 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2131 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2133 clex->tab = add_lexmin_eq(clex->tab, eq);
2135 int v = tab_has_valid_sample(clex->tab, eq, 1);
2139 clex->tab = check_integer_feasible(clex->tab);
2142 clex->tab = check_samples(clex->tab, eq, 1);
2145 isl_tab_free(clex->tab);
2149 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2150 int check, int update)
2152 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2153 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2155 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2157 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2161 clex->tab = check_integer_feasible(clex->tab);
2164 clex->tab = check_samples(clex->tab, ineq, 0);
2167 isl_tab_free(clex->tab);
2171 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2173 struct isl_context *context = (struct isl_context *)user;
2174 context_lex_add_ineq(context, ineq, 0, 0);
2175 return context->op->is_ok(context) ? 0 : -1;
2178 /* Check which signs can be obtained by "ineq" on all the currently
2179 * active sample values. See row_sign for more information.
2181 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2187 enum isl_tab_row_sign res = isl_tab_row_unknown;
2189 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2190 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2191 return isl_tab_row_unknown);
2194 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2195 isl_seq_inner_product(tab->samples->row[i], ineq,
2196 1 + tab->n_var, &tmp);
2197 sgn = isl_int_sgn(tmp);
2198 if (sgn > 0 || (sgn == 0 && strict)) {
2199 if (res == isl_tab_row_unknown)
2200 res = isl_tab_row_pos;
2201 if (res == isl_tab_row_neg)
2202 res = isl_tab_row_any;
2205 if (res == isl_tab_row_unknown)
2206 res = isl_tab_row_neg;
2207 if (res == isl_tab_row_pos)
2208 res = isl_tab_row_any;
2210 if (res == isl_tab_row_any)
2218 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2219 isl_int *ineq, int strict)
2221 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2222 return tab_ineq_sign(clex->tab, ineq, strict);
2225 /* Check whether "ineq" can be added to the tableau without rendering
2228 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2230 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2231 struct isl_tab_undo *snap;
2237 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2240 snap = isl_tab_snap(clex->tab);
2241 if (isl_tab_push_basis(clex->tab) < 0)
2243 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2244 clex->tab = check_integer_feasible(clex->tab);
2247 feasible = !clex->tab->empty;
2248 if (isl_tab_rollback(clex->tab, snap) < 0)
2254 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2255 struct isl_vec *div)
2257 return get_div(tab, context, div);
2260 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2262 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2263 return context_tab_add_div(clex->tab, div,
2264 context_lex_add_ineq_wrap, context);
2267 static int context_lex_detect_equalities(struct isl_context *context,
2268 struct isl_tab *tab)
2273 static int context_lex_best_split(struct isl_context *context,
2274 struct isl_tab *tab)
2276 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2277 struct isl_tab_undo *snap;
2280 snap = isl_tab_snap(clex->tab);
2281 if (isl_tab_push_basis(clex->tab) < 0)
2283 r = best_split(tab, clex->tab);
2285 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2291 static int context_lex_is_empty(struct isl_context *context)
2293 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2296 return clex->tab->empty;
2299 static void *context_lex_save(struct isl_context *context)
2301 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2302 struct isl_tab_undo *snap;
2304 snap = isl_tab_snap(clex->tab);
2305 if (isl_tab_push_basis(clex->tab) < 0)
2307 if (isl_tab_save_samples(clex->tab) < 0)
2313 static void context_lex_restore(struct isl_context *context, void *save)
2315 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2316 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2317 isl_tab_free(clex->tab);
2322 static int context_lex_is_ok(struct isl_context *context)
2324 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2328 /* For each variable in the context tableau, check if the variable can
2329 * only attain non-negative values. If so, mark the parameter as non-negative
2330 * in the main tableau. This allows for a more direct identification of some
2331 * cases of violated constraints.
2333 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2334 struct isl_tab *context_tab)
2337 struct isl_tab_undo *snap;
2338 struct isl_vec *ineq = NULL;
2339 struct isl_tab_var *var;
2342 if (context_tab->n_var == 0)
2345 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2349 if (isl_tab_extend_cons(context_tab, 1) < 0)
2352 snap = isl_tab_snap(context_tab);
2355 isl_seq_clr(ineq->el, ineq->size);
2356 for (i = 0; i < context_tab->n_var; ++i) {
2357 isl_int_set_si(ineq->el[1 + i], 1);
2358 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2360 var = &context_tab->con[context_tab->n_con - 1];
2361 if (!context_tab->empty &&
2362 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2364 if (i >= tab->n_param)
2365 j = i - tab->n_param + tab->n_var - tab->n_div;
2366 tab->var[j].is_nonneg = 1;
2369 isl_int_set_si(ineq->el[1 + i], 0);
2370 if (isl_tab_rollback(context_tab, snap) < 0)
2374 if (context_tab->M && n == context_tab->n_var) {
2375 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2387 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2388 struct isl_context *context, struct isl_tab *tab)
2390 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2391 struct isl_tab_undo *snap;
2396 snap = isl_tab_snap(clex->tab);
2397 if (isl_tab_push_basis(clex->tab) < 0)
2400 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2402 if (isl_tab_rollback(clex->tab, snap) < 0)
2411 static void context_lex_invalidate(struct isl_context *context)
2413 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2414 isl_tab_free(clex->tab);
2418 static void context_lex_free(struct isl_context *context)
2420 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2421 isl_tab_free(clex->tab);
2425 struct isl_context_op isl_context_lex_op = {
2426 context_lex_detect_nonnegative_parameters,
2427 context_lex_peek_basic_set,
2428 context_lex_peek_tab,
2430 context_lex_add_ineq,
2431 context_lex_ineq_sign,
2432 context_lex_test_ineq,
2433 context_lex_get_div,
2434 context_lex_add_div,
2435 context_lex_detect_equalities,
2436 context_lex_best_split,
2437 context_lex_is_empty,
2440 context_lex_restore,
2441 context_lex_invalidate,
2445 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2447 struct isl_tab *tab;
2449 bset = isl_basic_set_cow(bset);
2452 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2455 if (isl_tab_track_bset(tab, bset) < 0)
2457 tab = isl_tab_init_samples(tab);
2460 isl_basic_set_free(bset);
2464 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2466 struct isl_context_lex *clex;
2471 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2475 clex->context.op = &isl_context_lex_op;
2477 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2478 clex->tab = restore_lexmin(clex->tab);
2479 clex->tab = check_integer_feasible(clex->tab);
2483 return &clex->context;
2485 clex->context.op->free(&clex->context);
2489 struct isl_context_gbr {
2490 struct isl_context context;
2491 struct isl_tab *tab;
2492 struct isl_tab *shifted;
2493 struct isl_tab *cone;
2496 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2497 struct isl_context *context, struct isl_tab *tab)
2499 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2502 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2505 static struct isl_basic_set *context_gbr_peek_basic_set(
2506 struct isl_context *context)
2508 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2511 return isl_tab_peek_bset(cgbr->tab);
2514 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2516 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2520 /* Initialize the "shifted" tableau of the context, which
2521 * contains the constraints of the original tableau shifted
2522 * by the sum of all negative coefficients. This ensures
2523 * that any rational point in the shifted tableau can
2524 * be rounded up to yield an integer point in the original tableau.
2526 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2529 struct isl_vec *cst;
2530 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2531 unsigned dim = isl_basic_set_total_dim(bset);
2533 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2537 for (i = 0; i < bset->n_ineq; ++i) {
2538 isl_int_set(cst->el[i], bset->ineq[i][0]);
2539 for (j = 0; j < dim; ++j) {
2540 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2542 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2543 bset->ineq[i][1 + j]);
2547 cgbr->shifted = isl_tab_from_basic_set(bset);
2549 for (i = 0; i < bset->n_ineq; ++i)
2550 isl_int_set(bset->ineq[i][0], cst->el[i]);
2555 /* Check if the shifted tableau is non-empty, and if so
2556 * use the sample point to construct an integer point
2557 * of the context tableau.
2559 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2561 struct isl_vec *sample;
2564 gbr_init_shifted(cgbr);
2567 if (cgbr->shifted->empty)
2568 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2570 sample = isl_tab_get_sample_value(cgbr->shifted);
2571 sample = isl_vec_ceil(sample);
2576 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2583 for (i = 0; i < bset->n_eq; ++i)
2584 isl_int_set_si(bset->eq[i][0], 0);
2586 for (i = 0; i < bset->n_ineq; ++i)
2587 isl_int_set_si(bset->ineq[i][0], 0);
2592 static int use_shifted(struct isl_context_gbr *cgbr)
2594 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2597 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2599 struct isl_basic_set *bset;
2600 struct isl_basic_set *cone;
2602 if (isl_tab_sample_is_integer(cgbr->tab))
2603 return isl_tab_get_sample_value(cgbr->tab);
2605 if (use_shifted(cgbr)) {
2606 struct isl_vec *sample;
2608 sample = gbr_get_shifted_sample(cgbr);
2609 if (!sample || sample->size > 0)
2612 isl_vec_free(sample);
2616 bset = isl_tab_peek_bset(cgbr->tab);
2617 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2620 if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
2623 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2626 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2627 struct isl_vec *sample;
2628 struct isl_tab_undo *snap;
2630 if (cgbr->tab->basis) {
2631 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2632 isl_mat_free(cgbr->tab->basis);
2633 cgbr->tab->basis = NULL;
2635 cgbr->tab->n_zero = 0;
2636 cgbr->tab->n_unbounded = 0;
2639 snap = isl_tab_snap(cgbr->tab);
2641 sample = isl_tab_sample(cgbr->tab);
2643 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2644 isl_vec_free(sample);
2651 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2652 cone = drop_constant_terms(cone);
2653 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2654 cone = isl_basic_set_underlying_set(cone);
2655 cone = isl_basic_set_gauss(cone, NULL);
2657 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2658 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2659 bset = isl_basic_set_underlying_set(bset);
2660 bset = isl_basic_set_gauss(bset, NULL);
2662 return isl_basic_set_sample_with_cone(bset, cone);
2665 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2667 struct isl_vec *sample;
2672 if (cgbr->tab->empty)
2675 sample = gbr_get_sample(cgbr);
2679 if (sample->size == 0) {
2680 isl_vec_free(sample);
2681 if (isl_tab_mark_empty(cgbr->tab) < 0)
2686 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2690 isl_tab_free(cgbr->tab);
2694 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2701 if (isl_tab_extend_cons(tab, 2) < 0)
2704 if (isl_tab_add_eq(tab, eq) < 0)
2713 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2714 int check, int update)
2716 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2718 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2720 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2721 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2723 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2728 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2732 check_gbr_integer_feasible(cgbr);
2735 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2738 isl_tab_free(cgbr->tab);
2742 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2747 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2750 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2753 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2756 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2758 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2761 for (i = 0; i < dim; ++i) {
2762 if (!isl_int_is_neg(ineq[1 + i]))
2764 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2767 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2770 for (i = 0; i < dim; ++i) {
2771 if (!isl_int_is_neg(ineq[1 + i]))
2773 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2777 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2778 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2780 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2786 isl_tab_free(cgbr->tab);
2790 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2791 int check, int update)
2793 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2795 add_gbr_ineq(cgbr, ineq);
2800 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2804 check_gbr_integer_feasible(cgbr);
2807 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2810 isl_tab_free(cgbr->tab);
2814 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2816 struct isl_context *context = (struct isl_context *)user;
2817 context_gbr_add_ineq(context, ineq, 0, 0);
2818 return context->op->is_ok(context) ? 0 : -1;
2821 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2822 isl_int *ineq, int strict)
2824 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2825 return tab_ineq_sign(cgbr->tab, ineq, strict);
2828 /* Check whether "ineq" can be added to the tableau without rendering
2831 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2833 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2834 struct isl_tab_undo *snap;
2835 struct isl_tab_undo *shifted_snap = NULL;
2836 struct isl_tab_undo *cone_snap = NULL;
2842 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2845 snap = isl_tab_snap(cgbr->tab);
2847 shifted_snap = isl_tab_snap(cgbr->shifted);
2849 cone_snap = isl_tab_snap(cgbr->cone);
2850 add_gbr_ineq(cgbr, ineq);
2851 check_gbr_integer_feasible(cgbr);
2854 feasible = !cgbr->tab->empty;
2855 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2858 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2860 } else if (cgbr->shifted) {
2861 isl_tab_free(cgbr->shifted);
2862 cgbr->shifted = NULL;
2865 if (isl_tab_rollback(cgbr->cone, cone_snap))
2867 } else if (cgbr->cone) {
2868 isl_tab_free(cgbr->cone);
2875 /* Return the column of the last of the variables associated to
2876 * a column that has a non-zero coefficient.
2877 * This function is called in a context where only coefficients
2878 * of parameters or divs can be non-zero.
2880 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
2884 unsigned dim = tab->n_var - tab->n_param - tab->n_div;
2886 if (tab->n_var == 0)
2889 for (i = tab->n_var - 1; i >= 0; --i) {
2890 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
2892 if (tab->var[i].is_row)
2894 col = tab->var[i].index;
2895 if (!isl_int_is_zero(p[col]))
2902 /* Look through all the recently added equalities in the context
2903 * to see if we can propagate any of them to the main tableau.
2905 * The newly added equalities in the context are encoded as pairs
2906 * of inequalities starting at inequality "first".
2908 * We tentatively add each of these equalities to the main tableau
2909 * and if this happens to result in a row with a final coefficient
2910 * that is one or negative one, we use it to kill a column
2911 * in the main tableau. Otherwise, we discard the tentatively
2914 static void propagate_equalities(struct isl_context_gbr *cgbr,
2915 struct isl_tab *tab, unsigned first)
2918 struct isl_vec *eq = NULL;
2920 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2924 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
2927 isl_seq_clr(eq->el + 1 + tab->n_param,
2928 tab->n_var - tab->n_param - tab->n_div);
2929 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
2932 struct isl_tab_undo *snap;
2933 snap = isl_tab_snap(tab);
2935 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
2936 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
2937 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
2940 r = isl_tab_add_row(tab, eq->el);
2943 r = tab->con[r].index;
2944 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
2945 if (j < 0 || j < tab->n_dead ||
2946 !isl_int_is_one(tab->mat->row[r][0]) ||
2947 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
2948 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
2949 if (isl_tab_rollback(tab, snap) < 0)
2953 if (isl_tab_pivot(tab, r, j) < 0)
2955 if (isl_tab_kill_col(tab, j) < 0)
2958 tab = restore_lexmin(tab);
2966 isl_tab_free(cgbr->tab);
2970 static int context_gbr_detect_equalities(struct isl_context *context,
2971 struct isl_tab *tab)
2973 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2974 struct isl_ctx *ctx;
2976 enum isl_lp_result res;
2979 ctx = cgbr->tab->mat->ctx;
2982 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2983 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2986 if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
2989 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2992 n_ineq = cgbr->tab->bmap->n_ineq;
2993 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
2994 if (cgbr->tab && cgbr->tab->bmap->n_ineq > n_ineq)
2995 propagate_equalities(cgbr, tab, n_ineq);
2999 isl_tab_free(cgbr->tab);
3004 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3005 struct isl_vec *div)
3007 return get_div(tab, context, div);
3010 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3012 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3016 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3018 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3020 if (isl_tab_allocate_var(cgbr->cone) <0)
3023 cgbr->cone->bmap = isl_basic_map_extend_dim(cgbr->cone->bmap,
3024 isl_basic_map_get_dim(cgbr->cone->bmap), 1, 0, 2);
3025 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3028 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3029 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3032 return context_tab_add_div(cgbr->tab, div,
3033 context_gbr_add_ineq_wrap, context);
3036 static int context_gbr_best_split(struct isl_context *context,
3037 struct isl_tab *tab)
3039 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3040 struct isl_tab_undo *snap;
3043 snap = isl_tab_snap(cgbr->tab);
3044 r = best_split(tab, cgbr->tab);
3046 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3052 static int context_gbr_is_empty(struct isl_context *context)
3054 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3057 return cgbr->tab->empty;
3060 struct isl_gbr_tab_undo {
3061 struct isl_tab_undo *tab_snap;
3062 struct isl_tab_undo *shifted_snap;
3063 struct isl_tab_undo *cone_snap;
3066 static void *context_gbr_save(struct isl_context *context)
3068 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3069 struct isl_gbr_tab_undo *snap;
3071 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3075 snap->tab_snap = isl_tab_snap(cgbr->tab);
3076 if (isl_tab_save_samples(cgbr->tab) < 0)
3080 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3082 snap->shifted_snap = NULL;
3085 snap->cone_snap = isl_tab_snap(cgbr->cone);
3087 snap->cone_snap = NULL;
3095 static void context_gbr_restore(struct isl_context *context, void *save)
3097 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3098 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3101 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3102 isl_tab_free(cgbr->tab);
3106 if (snap->shifted_snap) {
3107 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3109 } else if (cgbr->shifted) {
3110 isl_tab_free(cgbr->shifted);
3111 cgbr->shifted = NULL;
3114 if (snap->cone_snap) {
3115 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3117 } else if (cgbr->cone) {
3118 isl_tab_free(cgbr->cone);
3127 isl_tab_free(cgbr->tab);
3131 static int context_gbr_is_ok(struct isl_context *context)
3133 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3137 static void context_gbr_invalidate(struct isl_context *context)
3139 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3140 isl_tab_free(cgbr->tab);
3144 static void context_gbr_free(struct isl_context *context)
3146 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3147 isl_tab_free(cgbr->tab);
3148 isl_tab_free(cgbr->shifted);
3149 isl_tab_free(cgbr->cone);
3153 struct isl_context_op isl_context_gbr_op = {
3154 context_gbr_detect_nonnegative_parameters,
3155 context_gbr_peek_basic_set,
3156 context_gbr_peek_tab,
3158 context_gbr_add_ineq,
3159 context_gbr_ineq_sign,
3160 context_gbr_test_ineq,
3161 context_gbr_get_div,
3162 context_gbr_add_div,
3163 context_gbr_detect_equalities,
3164 context_gbr_best_split,
3165 context_gbr_is_empty,
3168 context_gbr_restore,
3169 context_gbr_invalidate,
3173 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3175 struct isl_context_gbr *cgbr;
3180 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3184 cgbr->context.op = &isl_context_gbr_op;
3186 cgbr->shifted = NULL;
3188 cgbr->tab = isl_tab_from_basic_set(dom);
3189 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3192 if (isl_tab_track_bset(cgbr->tab,
3193 isl_basic_set_cow(isl_basic_set_copy(dom))) < 0)
3195 check_gbr_integer_feasible(cgbr);
3197 return &cgbr->context;
3199 cgbr->context.op->free(&cgbr->context);
3203 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3208 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3209 return isl_context_lex_alloc(dom);
3211 return isl_context_gbr_alloc(dom);
3214 /* Construct an isl_sol_map structure for accumulating the solution.
3215 * If track_empty is set, then we also keep track of the parts
3216 * of the context where there is no solution.
3217 * If max is set, then we are solving a maximization, rather than
3218 * a minimization problem, which means that the variables in the
3219 * tableau have value "M - x" rather than "M + x".
3221 static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
3222 struct isl_basic_set *dom, int track_empty, int max)
3224 struct isl_sol_map *sol_map = NULL;
3229 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3233 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3234 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3235 sol_map->sol.dec_level.sol = &sol_map->sol;
3236 sol_map->sol.max = max;
3237 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3238 sol_map->sol.add = &sol_map_add_wrap;
3239 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3240 sol_map->sol.free = &sol_map_free_wrap;
3241 sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
3246 sol_map->sol.context = isl_context_alloc(dom);
3247 if (!sol_map->sol.context)
3251 sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
3252 1, ISL_SET_DISJOINT);
3253 if (!sol_map->empty)
3257 isl_basic_set_free(dom);
3260 isl_basic_set_free(dom);
3261 sol_map_free(sol_map);
3265 /* Check whether all coefficients of (non-parameter) variables
3266 * are non-positive, meaning that no pivots can be performed on the row.
3268 static int is_critical(struct isl_tab *tab, int row)
3271 unsigned off = 2 + tab->M;
3273 for (j = tab->n_dead; j < tab->n_col; ++j) {
3274 if (tab->col_var[j] >= 0 &&
3275 (tab->col_var[j] < tab->n_param ||
3276 tab->col_var[j] >= tab->n_var - tab->n_div))
3279 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3286 /* Check whether the inequality represented by vec is strict over the integers,
3287 * i.e., there are no integer values satisfying the constraint with
3288 * equality. This happens if the gcd of the coefficients is not a divisor
3289 * of the constant term. If so, scale the constraint down by the gcd
3290 * of the coefficients.
3292 static int is_strict(struct isl_vec *vec)
3298 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3299 if (!isl_int_is_one(gcd)) {
3300 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3301 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3302 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3309 /* Determine the sign of the given row of the main tableau.
3310 * The result is one of
3311 * isl_tab_row_pos: always non-negative; no pivot needed
3312 * isl_tab_row_neg: always non-positive; pivot
3313 * isl_tab_row_any: can be both positive and negative; split
3315 * We first handle some simple cases
3316 * - the row sign may be known already
3317 * - the row may be obviously non-negative
3318 * - the parametric constant may be equal to that of another row
3319 * for which we know the sign. This sign will be either "pos" or
3320 * "any". If it had been "neg" then we would have pivoted before.
3322 * If none of these cases hold, we check the value of the row for each
3323 * of the currently active samples. Based on the signs of these values
3324 * we make an initial determination of the sign of the row.
3326 * all zero -> unk(nown)
3327 * all non-negative -> pos
3328 * all non-positive -> neg
3329 * both negative and positive -> all
3331 * If we end up with "all", we are done.
3332 * Otherwise, we perform a check for positive and/or negative
3333 * values as follows.
3335 * samples neg unk pos
3341 * There is no special sign for "zero", because we can usually treat zero
3342 * as either non-negative or non-positive, whatever works out best.
3343 * However, if the row is "critical", meaning that pivoting is impossible
3344 * then we don't want to limp zero with the non-positive case, because
3345 * then we we would lose the solution for those values of the parameters
3346 * where the value of the row is zero. Instead, we treat 0 as non-negative
3347 * ensuring a split if the row can attain both zero and negative values.
3348 * The same happens when the original constraint was one that could not
3349 * be satisfied with equality by any integer values of the parameters.
3350 * In this case, we normalize the constraint, but then a value of zero
3351 * for the normalized constraint is actually a positive value for the
3352 * original constraint, so again we need to treat zero as non-negative.
3353 * In both these cases, we have the following decision tree instead:
3355 * all non-negative -> pos
3356 * all negative -> neg
3357 * both negative and non-negative -> all
3365 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3366 struct isl_sol *sol, int row)
3368 struct isl_vec *ineq = NULL;
3369 enum isl_tab_row_sign res = isl_tab_row_unknown;
3374 if (tab->row_sign[row] != isl_tab_row_unknown)
3375 return tab->row_sign[row];
3376 if (is_obviously_nonneg(tab, row))
3377 return isl_tab_row_pos;
3378 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3379 if (tab->row_sign[row2] == isl_tab_row_unknown)
3381 if (identical_parameter_line(tab, row, row2))
3382 return tab->row_sign[row2];
3385 critical = is_critical(tab, row);
3387 ineq = get_row_parameter_ineq(tab, row);
3391 strict = is_strict(ineq);
3393 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3394 critical || strict);
3396 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3397 /* test for negative values */
3399 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3400 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3402 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3406 res = isl_tab_row_pos;
3408 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3410 if (res == isl_tab_row_neg) {
3411 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3412 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3416 if (res == isl_tab_row_neg) {
3417 /* test for positive values */
3419 if (!critical && !strict)
3420 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3422 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3426 res = isl_tab_row_any;
3433 return isl_tab_row_unknown;
3436 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3438 /* Find solutions for values of the parameters that satisfy the given
3441 * We currently take a snapshot of the context tableau that is reset
3442 * when we return from this function, while we make a copy of the main
3443 * tableau, leaving the original main tableau untouched.
3444 * These are fairly arbitrary choices. Making a copy also of the context
3445 * tableau would obviate the need to undo any changes made to it later,
3446 * while taking a snapshot of the main tableau could reduce memory usage.
3447 * If we were to switch to taking a snapshot of the main tableau,
3448 * we would have to keep in mind that we need to save the row signs
3449 * and that we need to do this before saving the current basis
3450 * such that the basis has been restore before we restore the row signs.
3452 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3458 saved = sol->context->op->save(sol->context);
3460 tab = isl_tab_dup(tab);
3464 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3466 find_solutions(sol, tab);
3469 sol->context->op->restore(sol->context, saved);
3475 /* Record the absence of solutions for those values of the parameters
3476 * that do not satisfy the given inequality with equality.
3478 static void no_sol_in_strict(struct isl_sol *sol,
3479 struct isl_tab *tab, struct isl_vec *ineq)
3484 if (!sol->context || sol->error)
3486 saved = sol->context->op->save(sol->context);
3488 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3490 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3499 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3501 sol->context->op->restore(sol->context, saved);
3507 /* Compute the lexicographic minimum of the set represented by the main
3508 * tableau "tab" within the context "sol->context_tab".
3509 * On entry the sample value of the main tableau is lexicographically
3510 * less than or equal to this lexicographic minimum.
3511 * Pivots are performed until a feasible point is found, which is then
3512 * necessarily equal to the minimum, or until the tableau is found to
3513 * be infeasible. Some pivots may need to be performed for only some
3514 * feasible values of the context tableau. If so, the context tableau
3515 * is split into a part where the pivot is needed and a part where it is not.
3517 * Whenever we enter the main loop, the main tableau is such that no
3518 * "obvious" pivots need to be performed on it, where "obvious" means
3519 * that the given row can be seen to be negative without looking at
3520 * the context tableau. In particular, for non-parametric problems,
3521 * no pivots need to be performed on the main tableau.
3522 * The caller of find_solutions is responsible for making this property
3523 * hold prior to the first iteration of the loop, while restore_lexmin
3524 * is called before every other iteration.
3526 * Inside the main loop, we first examine the signs of the rows of
3527 * the main tableau within the context of the context tableau.
3528 * If we find a row that is always non-positive for all values of
3529 * the parameters satisfying the context tableau and negative for at
3530 * least one value of the parameters, we perform the appropriate pivot
3531 * and start over. An exception is the case where no pivot can be
3532 * performed on the row. In this case, we require that the sign of
3533 * the row is negative for all values of the parameters (rather than just
3534 * non-positive). This special case is handled inside row_sign, which
3535 * will say that the row can have any sign if it determines that it can
3536 * attain both negative and zero values.
3538 * If we can't find a row that always requires a pivot, but we can find
3539 * one or more rows that require a pivot for some values of the parameters
3540 * (i.e., the row can attain both positive and negative signs), then we split
3541 * the context tableau into two parts, one where we force the sign to be
3542 * non-negative and one where we force is to be negative.
3543 * The non-negative part is handled by a recursive call (through find_in_pos).
3544 * Upon returning from this call, we continue with the negative part and
3545 * perform the required pivot.
3547 * If no such rows can be found, all rows are non-negative and we have
3548 * found a (rational) feasible point. If we only wanted a rational point
3550 * Otherwise, we check if all values of the sample point of the tableau
3551 * are integral for the variables. If so, we have found the minimal
3552 * integral point and we are done.
3553 * If the sample point is not integral, then we need to make a distinction
3554 * based on whether the constant term is non-integral or the coefficients
3555 * of the parameters. Furthermore, in order to decide how to handle
3556 * the non-integrality, we also need to know whether the coefficients
3557 * of the other columns in the tableau are integral. This leads
3558 * to the following table. The first two rows do not correspond
3559 * to a non-integral sample point and are only mentioned for completeness.
3561 * constant parameters other
3564 * int int rat | -> no problem
3566 * rat int int -> fail
3568 * rat int rat -> cut
3571 * rat rat rat | -> parametric cut
3574 * rat rat int | -> split context
3576 * If the parametric constant is completely integral, then there is nothing
3577 * to be done. If the constant term is non-integral, but all the other
3578 * coefficient are integral, then there is nothing that can be done
3579 * and the tableau has no integral solution.
3580 * If, on the other hand, one or more of the other columns have rational
3581 * coefficients, but the parameter coefficients are all integral, then
3582 * we can perform a regular (non-parametric) cut.
3583 * Finally, if there is any parameter coefficient that is non-integral,
3584 * then we need to involve the context tableau. There are two cases here.
3585 * If at least one other column has a rational coefficient, then we
3586 * can perform a parametric cut in the main tableau by adding a new
3587 * integer division in the context tableau.
3588 * If all other columns have integral coefficients, then we need to
3589 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3590 * is always integral. We do this by introducing an integer division
3591 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3592 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3593 * Since q is expressed in the tableau as
3594 * c + \sum a_i y_i - m q >= 0
3595 * -c - \sum a_i y_i + m q + m - 1 >= 0
3596 * it is sufficient to add the inequality
3597 * -c - \sum a_i y_i + m q >= 0
3598 * In the part of the context where this inequality does not hold, the
3599 * main tableau is marked as being empty.
3601 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3603 struct isl_context *context;
3605 if (!tab || sol->error)
3608 context = sol->context;
3612 if (context->op->is_empty(context))
3615 for (; tab && !tab->empty; tab = restore_lexmin(tab)) {
3618 enum isl_tab_row_sign sgn;
3622 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3623 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3625 sgn = row_sign(tab, sol, row);
3628 tab->row_sign[row] = sgn;
3629 if (sgn == isl_tab_row_any)
3631 if (sgn == isl_tab_row_any && split == -1)
3633 if (sgn == isl_tab_row_neg)
3636 if (row < tab->n_row)
3639 struct isl_vec *ineq;
3641 split = context->op->best_split(context, tab);
3644 ineq = get_row_parameter_ineq(tab, split);
3648 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3649 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3651 if (tab->row_sign[row] == isl_tab_row_any)
3652 tab->row_sign[row] = isl_tab_row_unknown;
3654 tab->row_sign[split] = isl_tab_row_pos;
3656 find_in_pos(sol, tab, ineq->el);
3657 tab->row_sign[split] = isl_tab_row_neg;
3659 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3660 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3662 context->op->add_ineq(context, ineq->el, 0, 1);
3670 row = first_non_integer_row(tab, &flags);
3673 if (ISL_FL_ISSET(flags, I_PAR)) {
3674 if (ISL_FL_ISSET(flags, I_VAR)) {
3675 if (isl_tab_mark_empty(tab) < 0)
3679 row = add_cut(tab, row);
3680 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3681 struct isl_vec *div;
3682 struct isl_vec *ineq;
3684 div = get_row_split_div(tab, row);
3687 d = context->op->get_div(context, tab, div);
3691 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3695 no_sol_in_strict(sol, tab, ineq);
3696 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3697 context->op->add_ineq(context, ineq->el, 1, 1);
3699 if (sol->error || !context->op->is_ok(context))
3701 tab = set_row_cst_to_div(tab, row, d);
3702 if (context->op->is_empty(context))
3705 row = add_parametric_cut(tab, row, context);
3718 /* Compute the lexicographic minimum of the set represented by the main
3719 * tableau "tab" within the context "sol->context_tab".
3721 * As a preprocessing step, we first transfer all the purely parametric
3722 * equalities from the main tableau to the context tableau, i.e.,
3723 * parameters that have been pivoted to a row.
3724 * These equalities are ignored by the main algorithm, because the
3725 * corresponding rows may not be marked as being non-negative.
3726 * In parts of the context where the added equality does not hold,
3727 * the main tableau is marked as being empty.
3729 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3738 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3742 if (tab->row_var[row] < 0)
3744 if (tab->row_var[row] >= tab->n_param &&
3745 tab->row_var[row] < tab->n_var - tab->n_div)
3747 if (tab->row_var[row] < tab->n_param)
3748 p = tab->row_var[row];
3750 p = tab->row_var[row]
3751 + tab->n_param - (tab->n_var - tab->n_div);
3753 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3756 get_row_parameter_line(tab, row, eq->el);
3757 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3758 eq = isl_vec_normalize(eq);
3761 no_sol_in_strict(sol, tab, eq);
3763 isl_seq_neg(eq->el, eq->el, eq->size);
3765 no_sol_in_strict(sol, tab, eq);
3766 isl_seq_neg(eq->el, eq->el, eq->size);
3768 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3772 if (isl_tab_mark_redundant(tab, row) < 0)
3775 if (sol->context->op->is_empty(sol->context))
3778 row = tab->n_redundant - 1;
3781 find_solutions(sol, tab);
3792 static void sol_map_find_solutions(struct isl_sol_map *sol_map,
3793 struct isl_tab *tab)
3795 find_solutions_main(&sol_map->sol, tab);
3798 /* Check if integer division "div" of "dom" also occurs in "bmap".
3799 * If so, return its position within the divs.
3800 * If not, return -1.
3802 static int find_context_div(struct isl_basic_map *bmap,
3803 struct isl_basic_set *dom, unsigned div)
3806 unsigned b_dim = isl_dim_total(bmap->dim);
3807 unsigned d_dim = isl_dim_total(dom->dim);
3809 if (isl_int_is_zero(dom->div[div][0]))
3811 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3814 for (i = 0; i < bmap->n_div; ++i) {
3815 if (isl_int_is_zero(bmap->div[i][0]))
3817 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3818 (b_dim - d_dim) + bmap->n_div) != -1)
3820 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3826 /* The correspondence between the variables in the main tableau,
3827 * the context tableau, and the input map and domain is as follows.
3828 * The first n_param and the last n_div variables of the main tableau
3829 * form the variables of the context tableau.
3830 * In the basic map, these n_param variables correspond to the
3831 * parameters and the input dimensions. In the domain, they correspond
3832 * to the parameters and the set dimensions.
3833 * The n_div variables correspond to the integer divisions in the domain.
3834 * To ensure that everything lines up, we may need to copy some of the
3835 * integer divisions of the domain to the map. These have to be placed
3836 * in the same order as those in the context and they have to be placed
3837 * after any other integer divisions that the map may have.
3838 * This function performs the required reordering.
3840 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3841 struct isl_basic_set *dom)
3847 for (i = 0; i < dom->n_div; ++i)
3848 if (find_context_div(bmap, dom, i) != -1)
3850 other = bmap->n_div - common;
3851 if (dom->n_div - common > 0) {
3852 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
3853 dom->n_div - common, 0, 0);
3857 for (i = 0; i < dom->n_div; ++i) {
3858 int pos = find_context_div(bmap, dom, i);
3860 pos = isl_basic_map_alloc_div(bmap);
3863 isl_int_set_si(bmap->div[pos][0], 0);
3865 if (pos != other + i)
3866 isl_basic_map_swap_div(bmap, pos, other + i);
3870 isl_basic_map_free(bmap);
3874 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3875 * some obvious symmetries.
3877 * We make sure the divs in the domain are properly ordered,
3878 * because they will be added one by one in the given order
3879 * during the construction of the solution map.
3881 static __isl_give isl_map *basic_map_partial_lexopt_base(
3882 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
3883 __isl_give isl_set **empty, int max)
3885 isl_map *result = NULL;
3886 struct isl_tab *tab;
3887 struct isl_sol_map *sol_map = NULL;
3888 struct isl_context *context;
3891 dom = isl_basic_set_order_divs(dom);
3892 bmap = align_context_divs(bmap, dom);
3894 sol_map = sol_map_init(bmap, dom, !!empty, max);
3898 context = sol_map->sol.context;
3899 if (isl_basic_set_fast_is_empty(context->op->peek_basic_set(context)))
3901 else if (isl_basic_map_fast_is_empty(bmap))
3902 sol_map_add_empty_if_needed(sol_map,
3903 isl_basic_set_copy(context->op->peek_basic_set(context)));
3905 tab = tab_for_lexmin(bmap,
3906 context->op->peek_basic_set(context), 1, max);
3907 tab = context->op->detect_nonnegative_parameters(context, tab);
3908 sol_map_find_solutions(sol_map, tab);
3910 if (sol_map->sol.error)
3913 result = isl_map_copy(sol_map->map);
3915 *empty = isl_set_copy(sol_map->empty);
3916 sol_free(&sol_map->sol);
3917 isl_basic_map_free(bmap);
3920 sol_free(&sol_map->sol);
3921 isl_basic_map_free(bmap);
3925 /* Structure used during detection of parallel constraints.
3926 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
3927 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
3928 * val: the coefficients of the output variables
3930 struct isl_constraint_equal_info {
3931 isl_basic_map *bmap;
3937 /* Check whether the coefficients of the output variables
3938 * of the constraint in "entry" are equal to info->val.
3940 static int constraint_equal(const void *entry, const void *val)
3942 isl_int **row = (isl_int **)entry;
3943 const struct isl_constraint_equal_info *info = val;
3945 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
3948 /* Check whether "bmap" has a pair of constraints that have
3949 * the same coefficients for the output variables.
3950 * Note that the coefficients of the existentially quantified
3951 * variables need to be zero since the existentially quantified
3952 * of the result are usually not the same as those of the input.
3953 * the isl_dim_out and isl_dim_div dimensions.
3954 * If so, return 1 and return the row indices of the two constraints
3955 * in *first and *second.
3957 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
3958 int *first, int *second)
3961 isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
3962 struct isl_hash_table *table = NULL;
3963 struct isl_hash_table_entry *entry;
3964 struct isl_constraint_equal_info info;
3968 ctx = isl_basic_map_get_ctx(bmap);
3969 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
3973 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
3974 isl_basic_map_dim(bmap, isl_dim_in);
3976 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3977 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3978 info.n_out = n_out + n_div;
3979 for (i = 0; i < bmap->n_ineq; ++i) {
3982 info.val = bmap->ineq[i] + 1 + info.n_in;
3983 if (isl_seq_first_non_zero(info.val, n_out) < 0)
3985 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
3987 hash = isl_seq_get_hash(info.val, info.n_out);
3988 entry = isl_hash_table_find(ctx, table, hash,
3989 constraint_equal, &info, 1);
3994 entry->data = &bmap->ineq[i];
3997 if (i < bmap->n_ineq) {
3998 *first = ((isl_int **)entry->data) - bmap->ineq;
4002 isl_hash_table_free(ctx, table);
4004 return i < bmap->n_ineq;
4006 isl_hash_table_free(ctx, table);
4010 /* Given a set of upper bounds on the last "input" variable m,
4011 * construct a set that assigns the minimal upper bound to m, i.e.,
4012 * construct a set that divides the space into cells where one
4013 * of the upper bounds is smaller than all the others and assign
4014 * this upper bound to m.
4016 * In particular, if there are n bounds b_i, then the result
4017 * consists of n basic sets, each one of the form
4020 * b_i <= b_j for j > i
4021 * b_i < b_j for j < i
4023 static __isl_give isl_set *set_minimum(__isl_take isl_dim *dim,
4024 __isl_take isl_mat *var)
4027 isl_basic_set *bset = NULL;
4029 isl_set *set = NULL;
4034 ctx = isl_dim_get_ctx(dim);
4035 set = isl_set_alloc_dim(isl_dim_copy(dim),
4036 var->n_row, ISL_SET_DISJOINT);
4038 for (i = 0; i < var->n_row; ++i) {
4039 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0,
4041 k = isl_basic_set_alloc_equality(bset);
4044 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4045 isl_int_set_si(bset->eq[k][var->n_col], -1);
4046 for (j = 0; j < var->n_row; ++j) {
4049 k = isl_basic_set_alloc_inequality(bset);
4052 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4053 ctx->negone, var->row[i],
4055 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4057 isl_int_sub_ui(bset->ineq[k][0],
4058 bset->ineq[k][0], 1);
4060 bset = isl_basic_set_finalize(bset);
4061 set = isl_set_add_basic_set(set, bset);
4068 isl_basic_set_free(bset);
4075 /* Given that the last input variable of "bmap" represents the minimum
4076 * of the bounds in "cst", check whether we need to split the domain
4077 * based on which bound attains the minimum.
4079 * A split is needed when the minimum appears in an integer division
4080 * or in an equality. Otherwise, it is only needed if it appears in
4081 * an upper bound that is different from the upper bounds on which it
4084 static int need_split_map(__isl_keep isl_basic_map *bmap,
4085 __isl_keep isl_mat *cst)
4091 pos = cst->n_col - 1;
4092 total = isl_basic_map_dim(bmap, isl_dim_all);
4094 for (i = 0; i < bmap->n_div; ++i)
4095 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4098 for (i = 0; i < bmap->n_eq; ++i)
4099 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4102 for (i = 0; i < bmap->n_ineq; ++i) {
4103 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4105 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4107 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4108 total - pos - 1) >= 0)
4111 for (j = 0; j < cst->n_row; ++j)
4112 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4114 if (j >= cst->n_row)
4121 static int need_split_set(__isl_keep isl_basic_set *bset,
4122 __isl_keep isl_mat *cst)
4124 return need_split_map((isl_basic_map *)bset, cst);
4127 /* Given a set of which the last set variable is the minimum
4128 * of the bounds in "cst", split each basic set in the set
4129 * in pieces where one of the bounds is (strictly) smaller than the others.
4130 * This subdivision is given in "min_expr".
4131 * The variable is subsequently projected out.
4133 * We only do the split when it is needed.
4134 * For example if the last input variable m = min(a,b) and the only
4135 * constraints in the given basic set are lower bounds on m,
4136 * i.e., l <= m = min(a,b), then we can simply project out m
4137 * to obtain l <= a and l <= b, without having to split on whether
4138 * m is equal to a or b.
4140 static __isl_give isl_set *split(__isl_take isl_set *empty,
4141 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4148 if (!empty || !min_expr || !cst)
4151 n_in = isl_set_dim(empty, isl_dim_set);
4152 dim = isl_set_get_dim(empty);
4153 dim = isl_dim_drop(dim, isl_dim_set, n_in - 1, 1);
4154 res = isl_set_empty(dim);
4156 for (i = 0; i < empty->n; ++i) {
4159 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4160 if (need_split_set(empty->p[i], cst))
4161 set = isl_set_intersect(set, isl_set_copy(min_expr));
4162 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4164 res = isl_set_union_disjoint(res, set);
4167 isl_set_free(empty);
4168 isl_set_free(min_expr);
4172 isl_set_free(empty);
4173 isl_set_free(min_expr);
4178 /* Given a map of which the last input variable is the minimum
4179 * of the bounds in "cst", split each basic set in the set
4180 * in pieces where one of the bounds is (strictly) smaller than the others.
4181 * This subdivision is given in "min_expr".
4182 * The variable is subsequently projected out.
4184 * The implementation is essentially the same as that of "split".
4186 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4187 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4194 if (!opt || !min_expr || !cst)
4197 n_in = isl_map_dim(opt, isl_dim_in);
4198 dim = isl_map_get_dim(opt);
4199 dim = isl_dim_drop(dim, isl_dim_in, n_in - 1, 1);
4200 res = isl_map_empty(dim);
4202 for (i = 0; i < opt->n; ++i) {
4205 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4206 if (need_split_map(opt->p[i], cst))
4207 map = isl_map_intersect_domain(map,
4208 isl_set_copy(min_expr));
4209 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4211 res = isl_map_union_disjoint(res, map);
4215 isl_set_free(min_expr);
4220 isl_set_free(min_expr);
4225 static __isl_give isl_map *basic_map_partial_lexopt(
4226 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4227 __isl_give isl_set **empty, int max);
4229 /* Given a basic map with at least two parallel constraints (as found
4230 * by the function parallel_constraints), first look for more constraints
4231 * parallel to the two constraint and replace the found list of parallel
4232 * constraints by a single constraint with as "input" part the minimum
4233 * of the input parts of the list of constraints. Then, recursively call
4234 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4235 * and plug in the definition of the minimum in the result.
4237 * More specifically, given a set of constraints
4241 * Replace this set by a single constraint
4245 * with u a new parameter with constraints
4249 * Any solution to the new system is also a solution for the original system
4252 * a x >= -u >= -b_i(p)
4254 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4255 * therefore be plugged into the solution.
4257 static __isl_give isl_map *basic_map_partial_lexopt_symm(
4258 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4259 __isl_give isl_set **empty, int max, int first, int second)
4263 unsigned n_in, n_out, n_div;
4265 isl_vec *var = NULL;
4266 isl_mat *cst = NULL;
4269 isl_dim *map_dim, *set_dim;
4271 map_dim = isl_basic_map_get_dim(bmap);
4272 set_dim = empty ? isl_basic_set_get_dim(dom) : NULL;
4274 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4275 isl_basic_map_dim(bmap, isl_dim_in);
4276 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4278 ctx = isl_basic_map_get_ctx(bmap);
4279 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4280 var = isl_vec_alloc(ctx, n_out);
4286 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4287 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4288 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4292 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4296 for (i = 0; i < n; ++i)
4297 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4299 bmap = isl_basic_map_cow(bmap);
4302 for (i = n - 1; i >= 0; --i)
4303 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4306 bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4307 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4308 k = isl_basic_map_alloc_inequality(bmap);
4311 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4312 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4313 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4314 bmap = isl_basic_map_finalize(bmap);
4316 n_div = isl_basic_set_dim(dom, isl_dim_div);
4317 dom = isl_basic_set_add(dom, isl_dim_set, 1);
4318 dom = isl_basic_set_extend_constraints(dom, 0, n);
4319 for (i = 0; i < n; ++i) {
4320 k = isl_basic_set_alloc_inequality(dom);
4323 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4324 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4325 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4328 min_expr = set_minimum(isl_basic_set_get_dim(dom), isl_mat_copy(cst));
4333 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4336 *empty = split(*empty,
4337 isl_set_copy(min_expr), isl_mat_copy(cst));
4338 *empty = isl_set_reset_dim(*empty, set_dim);
4341 opt = split_domain(opt, min_expr, cst);
4342 opt = isl_map_reset_dim(opt, map_dim);
4346 isl_dim_free(map_dim);
4347 isl_dim_free(set_dim);
4351 isl_basic_set_free(dom);
4352 isl_basic_map_free(bmap);
4356 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4357 * equalities and removing redundant constraints.
4359 * We first check if there are any parallel constraints (left).
4360 * If not, we are in the base case.
4361 * If there are parallel constraints, we replace them by a single
4362 * constraint in basic_map_partial_lexopt_symm and then call
4363 * this function recursively to look for more parallel constraints.
4365 static __isl_give isl_map *basic_map_partial_lexopt(
4366 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4367 __isl_give isl_set **empty, int max)
4375 if (bmap->ctx->opt->pip_symmetry)
4376 par = parallel_constraints(bmap, &first, &second);
4380 return basic_map_partial_lexopt_base(bmap, dom, empty, max);
4382 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4385 isl_basic_set_free(dom);
4386 isl_basic_map_free(bmap);
4390 /* Compute the lexicographic minimum (or maximum if "max" is set)
4391 * of "bmap" over the domain "dom" and return the result as a map.
4392 * If "empty" is not NULL, then *empty is assigned a set that
4393 * contains those parts of the domain where there is no solution.
4394 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4395 * then we compute the rational optimum. Otherwise, we compute
4396 * the integral optimum.
4398 * We perform some preprocessing. As the PILP solver does not
4399 * handle implicit equalities very well, we first make sure all
4400 * the equalities are explicitly available.
4402 * We also add context constraints to the basic map and remove
4403 * redundant constraints. This is only needed because of the
4404 * way we handle simple symmetries. In particular, we currently look
4405 * for symmetries on the constraints, before we set up the main tableau.
4406 * It is then no good to look for symmetries on possibly redundant constraints.
4408 struct isl_map *isl_tab_basic_map_partial_lexopt(
4409 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4410 struct isl_set **empty, int max)
4417 isl_assert(bmap->ctx,
4418 isl_basic_map_compatible_domain(bmap, dom), goto error);
4420 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4421 bmap = isl_basic_map_detect_equalities(bmap);
4422 bmap = isl_basic_map_remove_redundancies(bmap);
4424 return basic_map_partial_lexopt(bmap, dom, empty, max);
4426 isl_basic_set_free(dom);
4427 isl_basic_map_free(bmap);
4431 struct isl_sol_for {
4433 int (*fn)(__isl_take isl_basic_set *dom,
4434 __isl_take isl_mat *map, void *user);
4438 static void sol_for_free(struct isl_sol_for *sol_for)
4440 if (sol_for->sol.context)
4441 sol_for->sol.context->op->free(sol_for->sol.context);
4445 static void sol_for_free_wrap(struct isl_sol *sol)
4447 sol_for_free((struct isl_sol_for *)sol);
4450 /* Add the solution identified by the tableau and the context tableau.
4452 * See documentation of sol_add for more details.
4454 * Instead of constructing a basic map, this function calls a user
4455 * defined function with the current context as a basic set and
4456 * an affine matrix representing the relation between the input and output.
4457 * The number of rows in this matrix is equal to one plus the number
4458 * of output variables. The number of columns is equal to one plus
4459 * the total dimension of the context, i.e., the number of parameters,
4460 * input variables and divs. Since some of the columns in the matrix
4461 * may refer to the divs, the basic set is not simplified.
4462 * (Simplification may reorder or remove divs.)
4464 static void sol_for_add(struct isl_sol_for *sol,
4465 struct isl_basic_set *dom, struct isl_mat *M)
4467 if (sol->sol.error || !dom || !M)
4470 dom = isl_basic_set_simplify(dom);
4471 dom = isl_basic_set_finalize(dom);
4473 if (sol->fn(isl_basic_set_copy(dom), isl_mat_copy(M), sol->user) < 0)
4476 isl_basic_set_free(dom);
4480 isl_basic_set_free(dom);
4485 static void sol_for_add_wrap(struct isl_sol *sol,
4486 struct isl_basic_set *dom, struct isl_mat *M)
4488 sol_for_add((struct isl_sol_for *)sol, dom, M);
4491 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4492 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4496 struct isl_sol_for *sol_for = NULL;
4497 struct isl_dim *dom_dim;
4498 struct isl_basic_set *dom = NULL;
4500 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4504 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4505 dom = isl_basic_set_universe(dom_dim);
4507 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4508 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4509 sol_for->sol.dec_level.sol = &sol_for->sol;
4511 sol_for->user = user;
4512 sol_for->sol.max = max;
4513 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4514 sol_for->sol.add = &sol_for_add_wrap;
4515 sol_for->sol.add_empty = NULL;
4516 sol_for->sol.free = &sol_for_free_wrap;
4518 sol_for->sol.context = isl_context_alloc(dom);
4519 if (!sol_for->sol.context)
4522 isl_basic_set_free(dom);
4525 isl_basic_set_free(dom);
4526 sol_for_free(sol_for);
4530 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4531 struct isl_tab *tab)
4533 find_solutions_main(&sol_for->sol, tab);
4536 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4537 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4541 struct isl_sol_for *sol_for = NULL;
4543 bmap = isl_basic_map_copy(bmap);
4547 bmap = isl_basic_map_detect_equalities(bmap);
4548 sol_for = sol_for_init(bmap, max, fn, user);
4550 if (isl_basic_map_fast_is_empty(bmap))
4553 struct isl_tab *tab;
4554 struct isl_context *context = sol_for->sol.context;
4555 tab = tab_for_lexmin(bmap,
4556 context->op->peek_basic_set(context), 1, max);
4557 tab = context->op->detect_nonnegative_parameters(context, tab);
4558 sol_for_find_solutions(sol_for, tab);
4559 if (sol_for->sol.error)
4563 sol_free(&sol_for->sol);
4564 isl_basic_map_free(bmap);
4567 sol_free(&sol_for->sol);
4568 isl_basic_map_free(bmap);
4572 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map *bmap,
4573 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4577 return isl_basic_map_foreach_lexopt(bmap, 0, fn, user);
4580 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map *bmap,
4581 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4585 return isl_basic_map_foreach_lexopt(bmap, 1, fn, user);