2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include "isl_map_private.h"
13 #include "isl_sample.h"
14 #include <isl_mat_private.h>
17 * The implementation of parametric integer linear programming in this file
18 * was inspired by the paper "Parametric Integer Programming" and the
19 * report "Solving systems of affine (in)equalities" by Paul Feautrier
22 * The strategy used for obtaining a feasible solution is different
23 * from the one used in isl_tab.c. In particular, in isl_tab.c,
24 * upon finding a constraint that is not yet satisfied, we pivot
25 * in a row that increases the constant term of row holding the
26 * constraint, making sure the sample solution remains feasible
27 * for all the constraints it already satisfied.
28 * Here, we always pivot in the row holding the constraint,
29 * choosing a column that induces the lexicographically smallest
30 * increment to the sample solution.
32 * By starting out from a sample value that is lexicographically
33 * smaller than any integer point in the problem space, the first
34 * feasible integer sample point we find will also be the lexicographically
35 * smallest. If all variables can be assumed to be non-negative,
36 * then the initial sample value may be chosen equal to zero.
37 * However, we will not make this assumption. Instead, we apply
38 * the "big parameter" trick. Any variable x is then not directly
39 * used in the tableau, but instead it its represented by another
40 * variable x' = M + x, where M is an arbitrarily large (positive)
41 * value. x' is therefore always non-negative, whatever the value of x.
42 * Taking as initial smaple value x' = 0 corresponds to x = -M,
43 * which is always smaller than any possible value of x.
45 * The big parameter trick is used in the main tableau and
46 * also in the context tableau if isl_context_lex is used.
47 * In this case, each tableaus has its own big parameter.
48 * Before doing any real work, we check if all the parameters
49 * happen to be non-negative. If so, we drop the column corresponding
50 * to M from the initial context tableau.
51 * If isl_context_gbr is used, then the big parameter trick is only
52 * used in the main tableau.
56 struct isl_context_op {
57 /* detect nonnegative parameters in context and mark them in tab */
58 struct isl_tab *(*detect_nonnegative_parameters)(
59 struct isl_context *context, struct isl_tab *tab);
60 /* return temporary reference to basic set representation of context */
61 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
62 /* return temporary reference to tableau representation of context */
63 struct isl_tab *(*peek_tab)(struct isl_context *context);
64 /* add equality; check is 1 if eq may not be valid;
65 * update is 1 if we may want to call ineq_sign on context later.
67 void (*add_eq)(struct isl_context *context, isl_int *eq,
68 int check, int update);
69 /* add inequality; check is 1 if ineq may not be valid;
70 * update is 1 if we may want to call ineq_sign on context later.
72 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
73 int check, int update);
74 /* check sign of ineq based on previous information.
75 * strict is 1 if saturation should be treated as a positive sign.
77 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
78 isl_int *ineq, int strict);
79 /* check if inequality maintains feasibility */
80 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
81 /* return index of a div that corresponds to "div" */
82 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
84 /* add div "div" to context and return non-negativity */
85 int (*add_div)(struct isl_context *context, struct isl_vec *div);
86 int (*detect_equalities)(struct isl_context *context,
88 /* return row index of "best" split */
89 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
90 /* check if context has already been determined to be empty */
91 int (*is_empty)(struct isl_context *context);
92 /* check if context is still usable */
93 int (*is_ok)(struct isl_context *context);
94 /* save a copy/snapshot of context */
95 void *(*save)(struct isl_context *context);
96 /* restore saved context */
97 void (*restore)(struct isl_context *context, void *);
98 /* invalidate context */
99 void (*invalidate)(struct isl_context *context);
101 void (*free)(struct isl_context *context);
105 struct isl_context_op *op;
108 struct isl_context_lex {
109 struct isl_context context;
113 struct isl_partial_sol {
115 struct isl_basic_set *dom;
118 struct isl_partial_sol *next;
122 struct isl_sol_callback {
123 struct isl_tab_callback callback;
127 /* isl_sol is an interface for constructing a solution to
128 * a parametric integer linear programming problem.
129 * Every time the algorithm reaches a state where a solution
130 * can be read off from the tableau (including cases where the tableau
131 * is empty), the function "add" is called on the isl_sol passed
132 * to find_solutions_main.
134 * The context tableau is owned by isl_sol and is updated incrementally.
136 * There are currently two implementations of this interface,
137 * isl_sol_map, which simply collects the solutions in an isl_map
138 * and (optionally) the parts of the context where there is no solution
140 * isl_sol_for, which calls a user-defined function for each part of
149 struct isl_context *context;
150 struct isl_partial_sol *partial;
151 void (*add)(struct isl_sol *sol,
152 struct isl_basic_set *dom, struct isl_mat *M);
153 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
154 void (*free)(struct isl_sol *sol);
155 struct isl_sol_callback dec_level;
158 static void sol_free(struct isl_sol *sol)
160 struct isl_partial_sol *partial, *next;
163 for (partial = sol->partial; partial; partial = next) {
164 next = partial->next;
165 isl_basic_set_free(partial->dom);
166 isl_mat_free(partial->M);
172 /* Push a partial solution represented by a domain and mapping M
173 * onto the stack of partial solutions.
175 static void sol_push_sol(struct isl_sol *sol,
176 struct isl_basic_set *dom, struct isl_mat *M)
178 struct isl_partial_sol *partial;
180 if (sol->error || !dom)
183 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
187 partial->level = sol->level;
190 partial->next = sol->partial;
192 sol->partial = partial;
196 isl_basic_set_free(dom);
200 /* Pop one partial solution from the partial solution stack and
201 * pass it on to sol->add or sol->add_empty.
203 static void sol_pop_one(struct isl_sol *sol)
205 struct isl_partial_sol *partial;
207 partial = sol->partial;
208 sol->partial = partial->next;
211 sol->add(sol, partial->dom, partial->M);
213 sol->add_empty(sol, partial->dom);
217 /* Return a fresh copy of the domain represented by the context tableau.
219 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
221 struct isl_basic_set *bset;
226 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
227 bset = isl_basic_set_update_from_tab(bset,
228 sol->context->op->peek_tab(sol->context));
233 /* Check whether two partial solutions have the same mapping, where n_div
234 * is the number of divs that the two partial solutions have in common.
236 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
242 if (!s1->M != !s2->M)
247 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
249 for (i = 0; i < s1->M->n_row; ++i) {
250 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
251 s1->M->n_col-1-dim-n_div) != -1)
253 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
254 s2->M->n_col-1-dim-n_div) != -1)
256 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
262 /* Pop all solutions from the partial solution stack that were pushed onto
263 * the stack at levels that are deeper than the current level.
264 * If the two topmost elements on the stack have the same level
265 * and represent the same solution, then their domains are combined.
266 * This combined domain is the same as the current context domain
267 * as sol_pop is called each time we move back to a higher level.
269 static void sol_pop(struct isl_sol *sol)
271 struct isl_partial_sol *partial;
277 if (sol->level == 0) {
278 for (partial = sol->partial; partial; partial = sol->partial)
283 partial = sol->partial;
287 if (partial->level <= sol->level)
290 if (partial->next && partial->next->level == partial->level) {
291 n_div = isl_basic_set_dim(
292 sol->context->op->peek_basic_set(sol->context),
295 if (!same_solution(partial, partial->next, n_div)) {
299 struct isl_basic_set *bset;
301 bset = sol_domain(sol);
303 isl_basic_set_free(partial->next->dom);
304 partial->next->dom = bset;
305 partial->next->level = sol->level;
307 sol->partial = partial->next;
308 isl_basic_set_free(partial->dom);
309 isl_mat_free(partial->M);
316 static void sol_dec_level(struct isl_sol *sol)
326 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
328 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
330 sol_dec_level(callback->sol);
332 return callback->sol->error ? -1 : 0;
335 /* Move down to next level and push callback onto context tableau
336 * to decrease the level again when it gets rolled back across
337 * the current state. That is, dec_level will be called with
338 * the context tableau in the same state as it is when inc_level
341 static void sol_inc_level(struct isl_sol *sol)
349 tab = sol->context->op->peek_tab(sol->context);
350 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
354 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
358 if (isl_int_is_one(m))
361 for (i = 0; i < n_row; ++i)
362 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
365 /* Add the solution identified by the tableau and the context tableau.
367 * The layout of the variables is as follows.
368 * tab->n_var is equal to the total number of variables in the input
369 * map (including divs that were copied from the context)
370 * + the number of extra divs constructed
371 * Of these, the first tab->n_param and the last tab->n_div variables
372 * correspond to the variables in the context, i.e.,
373 * tab->n_param + tab->n_div = context_tab->n_var
374 * tab->n_param is equal to the number of parameters and input
375 * dimensions in the input map
376 * tab->n_div is equal to the number of divs in the context
378 * If there is no solution, then call add_empty with a basic set
379 * that corresponds to the context tableau. (If add_empty is NULL,
382 * If there is a solution, then first construct a matrix that maps
383 * all dimensions of the context to the output variables, i.e.,
384 * the output dimensions in the input map.
385 * The divs in the input map (if any) that do not correspond to any
386 * div in the context do not appear in the solution.
387 * The algorithm will make sure that they have an integer value,
388 * but these values themselves are of no interest.
389 * We have to be careful not to drop or rearrange any divs in the
390 * context because that would change the meaning of the matrix.
392 * To extract the value of the output variables, it should be noted
393 * that we always use a big parameter M in the main tableau and so
394 * the variable stored in this tableau is not an output variable x itself, but
395 * x' = M + x (in case of minimization)
397 * x' = M - x (in case of maximization)
398 * If x' appears in a column, then its optimal value is zero,
399 * which means that the optimal value of x is an unbounded number
400 * (-M for minimization and M for maximization).
401 * We currently assume that the output dimensions in the original map
402 * are bounded, so this cannot occur.
403 * Similarly, when x' appears in a row, then the coefficient of M in that
404 * row is necessarily 1.
405 * If the row in the tableau represents
406 * d x' = c + d M + e(y)
407 * then, in case of minimization, the corresponding row in the matrix
410 * with a d = m, the (updated) common denominator of the matrix.
411 * In case of maximization, the row will be
414 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
416 struct isl_basic_set *bset = NULL;
417 struct isl_mat *mat = NULL;
422 if (sol->error || !tab)
425 if (tab->empty && !sol->add_empty)
428 bset = sol_domain(sol);
431 sol_push_sol(sol, bset, NULL);
437 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
438 1 + tab->n_param + tab->n_div);
444 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
445 isl_int_set_si(mat->row[0][0], 1);
446 for (row = 0; row < sol->n_out; ++row) {
447 int i = tab->n_param + row;
450 isl_seq_clr(mat->row[1 + row], mat->n_col);
451 if (!tab->var[i].is_row) {
453 isl_die(mat->ctx, isl_error_invalid,
454 "unbounded optimum", goto error2);
458 r = tab->var[i].index;
460 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
461 isl_die(mat->ctx, isl_error_invalid,
462 "unbounded optimum", goto error2);
463 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
464 isl_int_divexact(m, tab->mat->row[r][0], m);
465 scale_rows(mat, m, 1 + row);
466 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
467 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
468 for (j = 0; j < tab->n_param; ++j) {
470 if (tab->var[j].is_row)
472 col = tab->var[j].index;
473 isl_int_mul(mat->row[1 + row][1 + j], m,
474 tab->mat->row[r][off + col]);
476 for (j = 0; j < tab->n_div; ++j) {
478 if (tab->var[tab->n_var - tab->n_div+j].is_row)
480 col = tab->var[tab->n_var - tab->n_div+j].index;
481 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
482 tab->mat->row[r][off + col]);
485 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
491 sol_push_sol(sol, bset, mat);
496 isl_basic_set_free(bset);
504 struct isl_set *empty;
507 static void sol_map_free(struct isl_sol_map *sol_map)
511 if (sol_map->sol.context)
512 sol_map->sol.context->op->free(sol_map->sol.context);
513 isl_map_free(sol_map->map);
514 isl_set_free(sol_map->empty);
518 static void sol_map_free_wrap(struct isl_sol *sol)
520 sol_map_free((struct isl_sol_map *)sol);
523 /* This function is called for parts of the context where there is
524 * no solution, with "bset" corresponding to the context tableau.
525 * Simply add the basic set to the set "empty".
527 static void sol_map_add_empty(struct isl_sol_map *sol,
528 struct isl_basic_set *bset)
532 isl_assert(bset->ctx, sol->empty, goto error);
534 sol->empty = isl_set_grow(sol->empty, 1);
535 bset = isl_basic_set_simplify(bset);
536 bset = isl_basic_set_finalize(bset);
537 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
540 isl_basic_set_free(bset);
543 isl_basic_set_free(bset);
547 static void sol_map_add_empty_wrap(struct isl_sol *sol,
548 struct isl_basic_set *bset)
550 sol_map_add_empty((struct isl_sol_map *)sol, bset);
553 /* Add bset to sol's empty, but only if we are actually collecting
556 static void sol_map_add_empty_if_needed(struct isl_sol_map *sol,
557 struct isl_basic_set *bset)
560 sol_map_add_empty(sol, bset);
562 isl_basic_set_free(bset);
565 /* Given a basic map "dom" that represents the context and an affine
566 * matrix "M" that maps the dimensions of the context to the
567 * output variables, construct a basic map with the same parameters
568 * and divs as the context, the dimensions of the context as input
569 * dimensions and a number of output dimensions that is equal to
570 * the number of output dimensions in the input map.
572 * The constraints and divs of the context are simply copied
573 * from "dom". For each row
577 * is added, with d the common denominator of M.
579 static void sol_map_add(struct isl_sol_map *sol,
580 struct isl_basic_set *dom, struct isl_mat *M)
583 struct isl_basic_map *bmap = NULL;
584 isl_basic_set *context_bset;
592 if (sol->sol.error || !dom || !M)
595 n_out = sol->sol.n_out;
596 n_eq = dom->n_eq + n_out;
597 n_ineq = dom->n_ineq;
599 nparam = isl_basic_set_total_dim(dom) - n_div;
600 total = isl_map_dim(sol->map, isl_dim_all);
601 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
602 n_div, n_eq, 2 * n_div + n_ineq);
605 if (sol->sol.rational)
606 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
607 for (i = 0; i < dom->n_div; ++i) {
608 int k = isl_basic_map_alloc_div(bmap);
611 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
612 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
613 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
614 dom->div[i] + 1 + 1 + nparam, i);
616 for (i = 0; i < dom->n_eq; ++i) {
617 int k = isl_basic_map_alloc_equality(bmap);
620 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
621 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
622 isl_seq_cpy(bmap->eq[k] + 1 + total,
623 dom->eq[i] + 1 + nparam, n_div);
625 for (i = 0; i < dom->n_ineq; ++i) {
626 int k = isl_basic_map_alloc_inequality(bmap);
629 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
630 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
631 isl_seq_cpy(bmap->ineq[k] + 1 + total,
632 dom->ineq[i] + 1 + nparam, n_div);
634 for (i = 0; i < M->n_row - 1; ++i) {
635 int k = isl_basic_map_alloc_equality(bmap);
638 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
639 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
640 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
641 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
642 M->row[1 + i] + 1 + nparam, n_div);
644 bmap = isl_basic_map_simplify(bmap);
645 bmap = isl_basic_map_finalize(bmap);
646 sol->map = isl_map_grow(sol->map, 1);
647 sol->map = isl_map_add_basic_map(sol->map, bmap);
650 isl_basic_set_free(dom);
654 isl_basic_set_free(dom);
656 isl_basic_map_free(bmap);
660 static void sol_map_add_wrap(struct isl_sol *sol,
661 struct isl_basic_set *dom, struct isl_mat *M)
663 sol_map_add((struct isl_sol_map *)sol, dom, M);
667 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
668 * i.e., the constant term and the coefficients of all variables that
669 * appear in the context tableau.
670 * Note that the coefficient of the big parameter M is NOT copied.
671 * The context tableau may not have a big parameter and even when it
672 * does, it is a different big parameter.
674 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
677 unsigned off = 2 + tab->M;
679 isl_int_set(line[0], tab->mat->row[row][1]);
680 for (i = 0; i < tab->n_param; ++i) {
681 if (tab->var[i].is_row)
682 isl_int_set_si(line[1 + i], 0);
684 int col = tab->var[i].index;
685 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
688 for (i = 0; i < tab->n_div; ++i) {
689 if (tab->var[tab->n_var - tab->n_div + i].is_row)
690 isl_int_set_si(line[1 + tab->n_param + i], 0);
692 int col = tab->var[tab->n_var - tab->n_div + i].index;
693 isl_int_set(line[1 + tab->n_param + i],
694 tab->mat->row[row][off + col]);
699 /* Check if rows "row1" and "row2" have identical "parametric constants",
700 * as explained above.
701 * In this case, we also insist that the coefficients of the big parameter
702 * be the same as the values of the constants will only be the same
703 * if these coefficients are also the same.
705 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
708 unsigned off = 2 + tab->M;
710 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
713 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
714 tab->mat->row[row2][2]))
717 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
718 int pos = i < tab->n_param ? i :
719 tab->n_var - tab->n_div + i - tab->n_param;
722 if (tab->var[pos].is_row)
724 col = tab->var[pos].index;
725 if (isl_int_ne(tab->mat->row[row1][off + col],
726 tab->mat->row[row2][off + col]))
732 /* Return an inequality that expresses that the "parametric constant"
733 * should be non-negative.
734 * This function is only called when the coefficient of the big parameter
737 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
739 struct isl_vec *ineq;
741 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
745 get_row_parameter_line(tab, row, ineq->el);
747 ineq = isl_vec_normalize(ineq);
752 /* Return a integer division for use in a parametric cut based on the given row.
753 * In particular, let the parametric constant of the row be
757 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
758 * The div returned is equal to
760 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
762 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
766 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
770 isl_int_set(div->el[0], tab->mat->row[row][0]);
771 get_row_parameter_line(tab, row, div->el + 1);
772 div = isl_vec_normalize(div);
773 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
774 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
779 /* Return a integer division for use in transferring an integrality constraint
781 * In particular, let the parametric constant of the row be
785 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
786 * The the returned div is equal to
788 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
790 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
794 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
798 isl_int_set(div->el[0], tab->mat->row[row][0]);
799 get_row_parameter_line(tab, row, div->el + 1);
800 div = isl_vec_normalize(div);
801 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
806 /* Construct and return an inequality that expresses an upper bound
808 * In particular, if the div is given by
812 * then the inequality expresses
816 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
820 struct isl_vec *ineq;
825 total = isl_basic_set_total_dim(bset);
826 div_pos = 1 + total - bset->n_div + div;
828 ineq = isl_vec_alloc(bset->ctx, 1 + total);
832 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
833 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
837 /* Given a row in the tableau and a div that was created
838 * using get_row_split_div and that been constrained to equality, i.e.,
840 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
842 * replace the expression "\sum_i {a_i} y_i" in the row by d,
843 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
844 * The coefficients of the non-parameters in the tableau have been
845 * verified to be integral. We can therefore simply replace coefficient b
846 * by floor(b). For the coefficients of the parameters we have
847 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
850 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
852 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
853 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
855 isl_int_set_si(tab->mat->row[row][0], 1);
857 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
858 int drow = tab->var[tab->n_var - tab->n_div + div].index;
860 isl_assert(tab->mat->ctx,
861 isl_int_is_one(tab->mat->row[drow][0]), goto error);
862 isl_seq_combine(tab->mat->row[row] + 1,
863 tab->mat->ctx->one, tab->mat->row[row] + 1,
864 tab->mat->ctx->one, tab->mat->row[drow] + 1,
865 1 + tab->M + tab->n_col);
867 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
869 isl_int_set_si(tab->mat->row[row][2 + tab->M + dcol], 1);
878 /* Check if the (parametric) constant of the given row is obviously
879 * negative, meaning that we don't need to consult the context tableau.
880 * If there is a big parameter and its coefficient is non-zero,
881 * then this coefficient determines the outcome.
882 * Otherwise, we check whether the constant is negative and
883 * all non-zero coefficients of parameters are negative and
884 * belong to non-negative parameters.
886 static int is_obviously_neg(struct isl_tab *tab, int row)
890 unsigned off = 2 + tab->M;
893 if (isl_int_is_pos(tab->mat->row[row][2]))
895 if (isl_int_is_neg(tab->mat->row[row][2]))
899 if (isl_int_is_nonneg(tab->mat->row[row][1]))
901 for (i = 0; i < tab->n_param; ++i) {
902 /* Eliminated parameter */
903 if (tab->var[i].is_row)
905 col = tab->var[i].index;
906 if (isl_int_is_zero(tab->mat->row[row][off + col]))
908 if (!tab->var[i].is_nonneg)
910 if (isl_int_is_pos(tab->mat->row[row][off + col]))
913 for (i = 0; i < tab->n_div; ++i) {
914 if (tab->var[tab->n_var - tab->n_div + i].is_row)
916 col = tab->var[tab->n_var - tab->n_div + i].index;
917 if (isl_int_is_zero(tab->mat->row[row][off + col]))
919 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
921 if (isl_int_is_pos(tab->mat->row[row][off + col]))
927 /* Check if the (parametric) constant of the given row is obviously
928 * non-negative, meaning that we don't need to consult the context tableau.
929 * If there is a big parameter and its coefficient is non-zero,
930 * then this coefficient determines the outcome.
931 * Otherwise, we check whether the constant is non-negative and
932 * all non-zero coefficients of parameters are positive and
933 * belong to non-negative parameters.
935 static int is_obviously_nonneg(struct isl_tab *tab, int row)
939 unsigned off = 2 + tab->M;
942 if (isl_int_is_pos(tab->mat->row[row][2]))
944 if (isl_int_is_neg(tab->mat->row[row][2]))
948 if (isl_int_is_neg(tab->mat->row[row][1]))
950 for (i = 0; i < tab->n_param; ++i) {
951 /* Eliminated parameter */
952 if (tab->var[i].is_row)
954 col = tab->var[i].index;
955 if (isl_int_is_zero(tab->mat->row[row][off + col]))
957 if (!tab->var[i].is_nonneg)
959 if (isl_int_is_neg(tab->mat->row[row][off + col]))
962 for (i = 0; i < tab->n_div; ++i) {
963 if (tab->var[tab->n_var - tab->n_div + i].is_row)
965 col = tab->var[tab->n_var - tab->n_div + i].index;
966 if (isl_int_is_zero(tab->mat->row[row][off + col]))
968 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
970 if (isl_int_is_neg(tab->mat->row[row][off + col]))
976 /* Given a row r and two columns, return the column that would
977 * lead to the lexicographically smallest increment in the sample
978 * solution when leaving the basis in favor of the row.
979 * Pivoting with column c will increment the sample value by a non-negative
980 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
981 * corresponding to the non-parametric variables.
982 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
983 * with all other entries in this virtual row equal to zero.
984 * If variable v appears in a row, then a_{v,c} is the element in column c
987 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
988 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
989 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
990 * increment. Otherwise, it's c2.
992 static int lexmin_col_pair(struct isl_tab *tab,
993 int row, int col1, int col2, isl_int tmp)
998 tr = tab->mat->row[row] + 2 + tab->M;
1000 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1004 if (!tab->var[i].is_row) {
1005 if (tab->var[i].index == col1)
1007 if (tab->var[i].index == col2)
1012 if (tab->var[i].index == row)
1015 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1016 s1 = isl_int_sgn(r[col1]);
1017 s2 = isl_int_sgn(r[col2]);
1018 if (s1 == 0 && s2 == 0)
1025 isl_int_mul(tmp, r[col2], tr[col1]);
1026 isl_int_submul(tmp, r[col1], tr[col2]);
1027 if (isl_int_is_pos(tmp))
1029 if (isl_int_is_neg(tmp))
1035 /* Given a row in the tableau, find and return the column that would
1036 * result in the lexicographically smallest, but positive, increment
1037 * in the sample point.
1038 * If there is no such column, then return tab->n_col.
1039 * If anything goes wrong, return -1.
1041 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1044 int col = tab->n_col;
1048 tr = tab->mat->row[row] + 2 + tab->M;
1052 for (j = tab->n_dead; j < tab->n_col; ++j) {
1053 if (tab->col_var[j] >= 0 &&
1054 (tab->col_var[j] < tab->n_param ||
1055 tab->col_var[j] >= tab->n_var - tab->n_div))
1058 if (!isl_int_is_pos(tr[j]))
1061 if (col == tab->n_col)
1064 col = lexmin_col_pair(tab, row, col, j, tmp);
1065 isl_assert(tab->mat->ctx, col >= 0, goto error);
1075 /* Return the first known violated constraint, i.e., a non-negative
1076 * contraint that currently has an either obviously negative value
1077 * or a previously determined to be negative value.
1079 * If any constraint has a negative coefficient for the big parameter,
1080 * if any, then we return one of these first.
1082 static int first_neg(struct isl_tab *tab)
1087 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1088 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1090 if (!isl_int_is_neg(tab->mat->row[row][2]))
1093 tab->row_sign[row] = isl_tab_row_neg;
1096 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1097 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1099 if (tab->row_sign) {
1100 if (tab->row_sign[row] == 0 &&
1101 is_obviously_neg(tab, row))
1102 tab->row_sign[row] = isl_tab_row_neg;
1103 if (tab->row_sign[row] != isl_tab_row_neg)
1105 } else if (!is_obviously_neg(tab, row))
1112 /* Resolve all known or obviously violated constraints through pivoting.
1113 * In particular, as long as we can find any violated constraint, we
1114 * look for a pivoting column that would result in the lexicographicallly
1115 * smallest increment in the sample point. If there is no such column
1116 * then the tableau is infeasible.
1118 static struct isl_tab *restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1119 static struct isl_tab *restore_lexmin(struct isl_tab *tab)
1127 while ((row = first_neg(tab)) != -1) {
1128 col = lexmin_pivot_col(tab, row);
1129 if (col >= tab->n_col) {
1130 if (isl_tab_mark_empty(tab) < 0)
1136 if (isl_tab_pivot(tab, row, col) < 0)
1145 /* Given a row that represents an equality, look for an appropriate
1147 * In particular, if there are any non-zero coefficients among
1148 * the non-parameter variables, then we take the last of these
1149 * variables. Eliminating this variable in terms of the other
1150 * variables and/or parameters does not influence the property
1151 * that all column in the initial tableau are lexicographically
1152 * positive. The row corresponding to the eliminated variable
1153 * will only have non-zero entries below the diagonal of the
1154 * initial tableau. That is, we transform
1160 * If there is no such non-parameter variable, then we are dealing with
1161 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1162 * for elimination. This will ensure that the eliminated parameter
1163 * always has an integer value whenever all the other parameters are integral.
1164 * If there is no such parameter then we return -1.
1166 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1168 unsigned off = 2 + tab->M;
1171 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1173 if (tab->var[i].is_row)
1175 col = tab->var[i].index;
1176 if (col <= tab->n_dead)
1178 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1181 for (i = tab->n_dead; i < tab->n_col; ++i) {
1182 if (isl_int_is_one(tab->mat->row[row][off + i]))
1184 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1190 /* Add an equality that is known to be valid to the tableau.
1191 * We first check if we can eliminate a variable or a parameter.
1192 * If not, we add the equality as two inequalities.
1193 * In this case, the equality was a pure parameter equality and there
1194 * is no need to resolve any constraint violations.
1196 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1203 r = isl_tab_add_row(tab, eq);
1207 r = tab->con[r].index;
1208 i = last_var_col_or_int_par_col(tab, r);
1210 tab->con[r].is_nonneg = 1;
1211 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1213 isl_seq_neg(eq, eq, 1 + tab->n_var);
1214 r = isl_tab_add_row(tab, eq);
1217 tab->con[r].is_nonneg = 1;
1218 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1221 if (isl_tab_pivot(tab, r, i) < 0)
1223 if (isl_tab_kill_col(tab, i) < 0)
1227 tab = restore_lexmin(tab);
1236 /* Check if the given row is a pure constant.
1238 static int is_constant(struct isl_tab *tab, int row)
1240 unsigned off = 2 + tab->M;
1242 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1243 tab->n_col - tab->n_dead) == -1;
1246 /* Add an equality that may or may not be valid to the tableau.
1247 * If the resulting row is a pure constant, then it must be zero.
1248 * Otherwise, the resulting tableau is empty.
1250 * If the row is not a pure constant, then we add two inequalities,
1251 * each time checking that they can be satisfied.
1252 * In the end we try to use one of the two constraints to eliminate
1255 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1256 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1260 struct isl_tab_undo *snap;
1264 snap = isl_tab_snap(tab);
1265 r1 = isl_tab_add_row(tab, eq);
1268 tab->con[r1].is_nonneg = 1;
1269 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1272 row = tab->con[r1].index;
1273 if (is_constant(tab, row)) {
1274 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1275 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1276 if (isl_tab_mark_empty(tab) < 0)
1280 if (isl_tab_rollback(tab, snap) < 0)
1285 tab = restore_lexmin(tab);
1286 if (!tab || tab->empty)
1289 isl_seq_neg(eq, eq, 1 + tab->n_var);
1291 r2 = isl_tab_add_row(tab, eq);
1294 tab->con[r2].is_nonneg = 1;
1295 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1298 tab = restore_lexmin(tab);
1299 if (!tab || tab->empty)
1302 if (!tab->con[r1].is_row) {
1303 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1305 } else if (!tab->con[r2].is_row) {
1306 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1308 } else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
1309 unsigned off = 2 + tab->M;
1311 int row = tab->con[r1].index;
1312 i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
1313 tab->n_col - tab->n_dead);
1315 if (isl_tab_pivot(tab, row, tab->n_dead + i) < 0)
1317 if (isl_tab_kill_col(tab, tab->n_dead + i) < 0)
1323 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1324 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1326 isl_seq_neg(eq, eq, 1 + tab->n_var);
1327 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1328 isl_seq_neg(eq, eq, 1 + tab->n_var);
1329 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1341 /* Add an inequality to the tableau, resolving violations using
1344 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1351 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1352 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1357 r = isl_tab_add_row(tab, ineq);
1360 tab->con[r].is_nonneg = 1;
1361 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1363 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1364 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1369 tab = restore_lexmin(tab);
1370 if (tab && !tab->empty && tab->con[r].is_row &&
1371 isl_tab_row_is_redundant(tab, tab->con[r].index))
1372 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1380 /* Check if the coefficients of the parameters are all integral.
1382 static int integer_parameter(struct isl_tab *tab, int row)
1386 unsigned off = 2 + tab->M;
1388 for (i = 0; i < tab->n_param; ++i) {
1389 /* Eliminated parameter */
1390 if (tab->var[i].is_row)
1392 col = tab->var[i].index;
1393 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1394 tab->mat->row[row][0]))
1397 for (i = 0; i < tab->n_div; ++i) {
1398 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1400 col = tab->var[tab->n_var - tab->n_div + i].index;
1401 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1402 tab->mat->row[row][0]))
1408 /* Check if the coefficients of the non-parameter variables are all integral.
1410 static int integer_variable(struct isl_tab *tab, int row)
1413 unsigned off = 2 + tab->M;
1415 for (i = tab->n_dead; i < tab->n_col; ++i) {
1416 if (tab->col_var[i] >= 0 &&
1417 (tab->col_var[i] < tab->n_param ||
1418 tab->col_var[i] >= tab->n_var - tab->n_div))
1420 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1421 tab->mat->row[row][0]))
1427 /* Check if the constant term is integral.
1429 static int integer_constant(struct isl_tab *tab, int row)
1431 return isl_int_is_divisible_by(tab->mat->row[row][1],
1432 tab->mat->row[row][0]);
1435 #define I_CST 1 << 0
1436 #define I_PAR 1 << 1
1437 #define I_VAR 1 << 2
1439 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1440 * that is non-integer and therefore requires a cut and return
1441 * the index of the variable.
1442 * For parametric tableaus, there are three parts in a row,
1443 * the constant, the coefficients of the parameters and the rest.
1444 * For each part, we check whether the coefficients in that part
1445 * are all integral and if so, set the corresponding flag in *f.
1446 * If the constant and the parameter part are integral, then the
1447 * current sample value is integral and no cut is required
1448 * (irrespective of whether the variable part is integral).
1450 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1452 var = var < 0 ? tab->n_param : var + 1;
1454 for (; var < tab->n_var - tab->n_div; ++var) {
1457 if (!tab->var[var].is_row)
1459 row = tab->var[var].index;
1460 if (integer_constant(tab, row))
1461 ISL_FL_SET(flags, I_CST);
1462 if (integer_parameter(tab, row))
1463 ISL_FL_SET(flags, I_PAR);
1464 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1466 if (integer_variable(tab, row))
1467 ISL_FL_SET(flags, I_VAR);
1474 /* Check for first (non-parameter) variable that is non-integer and
1475 * therefore requires a cut and return the corresponding row.
1476 * For parametric tableaus, there are three parts in a row,
1477 * the constant, the coefficients of the parameters and the rest.
1478 * For each part, we check whether the coefficients in that part
1479 * are all integral and if so, set the corresponding flag in *f.
1480 * If the constant and the parameter part are integral, then the
1481 * current sample value is integral and no cut is required
1482 * (irrespective of whether the variable part is integral).
1484 static int first_non_integer_row(struct isl_tab *tab, int *f)
1486 int var = next_non_integer_var(tab, -1, f);
1488 return var < 0 ? -1 : tab->var[var].index;
1491 /* Add a (non-parametric) cut to cut away the non-integral sample
1492 * value of the given row.
1494 * If the row is given by
1496 * m r = f + \sum_i a_i y_i
1500 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1502 * The big parameter, if any, is ignored, since it is assumed to be big
1503 * enough to be divisible by any integer.
1504 * If the tableau is actually a parametric tableau, then this function
1505 * is only called when all coefficients of the parameters are integral.
1506 * The cut therefore has zero coefficients for the parameters.
1508 * The current value is known to be negative, so row_sign, if it
1509 * exists, is set accordingly.
1511 * Return the row of the cut or -1.
1513 static int add_cut(struct isl_tab *tab, int row)
1518 unsigned off = 2 + tab->M;
1520 if (isl_tab_extend_cons(tab, 1) < 0)
1522 r = isl_tab_allocate_con(tab);
1526 r_row = tab->mat->row[tab->con[r].index];
1527 isl_int_set(r_row[0], tab->mat->row[row][0]);
1528 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1529 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1530 isl_int_neg(r_row[1], r_row[1]);
1532 isl_int_set_si(r_row[2], 0);
1533 for (i = 0; i < tab->n_col; ++i)
1534 isl_int_fdiv_r(r_row[off + i],
1535 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1537 tab->con[r].is_nonneg = 1;
1538 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1541 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1543 return tab->con[r].index;
1546 /* Given a non-parametric tableau, add cuts until an integer
1547 * sample point is obtained or until the tableau is determined
1548 * to be integer infeasible.
1549 * As long as there is any non-integer value in the sample point,
1550 * we add appropriate cuts, if possible, for each of these
1551 * non-integer values and then resolve the violated
1552 * cut constraints using restore_lexmin.
1553 * If one of the corresponding rows is equal to an integral
1554 * combination of variables/constraints plus a non-integral constant,
1555 * then there is no way to obtain an integer point and we return
1556 * a tableau that is marked empty.
1558 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
1569 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1571 if (ISL_FL_ISSET(flags, I_VAR)) {
1572 if (isl_tab_mark_empty(tab) < 0)
1576 row = tab->var[var].index;
1577 row = add_cut(tab, row);
1580 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1581 tab = restore_lexmin(tab);
1582 if (!tab || tab->empty)
1591 /* Check whether all the currently active samples also satisfy the inequality
1592 * "ineq" (treated as an equality if eq is set).
1593 * Remove those samples that do not.
1595 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1603 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1604 isl_assert(tab->mat->ctx, tab->samples, goto error);
1605 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1608 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1610 isl_seq_inner_product(ineq, tab->samples->row[i],
1611 1 + tab->n_var, &v);
1612 sgn = isl_int_sgn(v);
1613 if (eq ? (sgn == 0) : (sgn >= 0))
1615 tab = isl_tab_drop_sample(tab, i);
1627 /* Check whether the sample value of the tableau is finite,
1628 * i.e., either the tableau does not use a big parameter, or
1629 * all values of the variables are equal to the big parameter plus
1630 * some constant. This constant is the actual sample value.
1632 static int sample_is_finite(struct isl_tab *tab)
1639 for (i = 0; i < tab->n_var; ++i) {
1641 if (!tab->var[i].is_row)
1643 row = tab->var[i].index;
1644 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1650 /* Check if the context tableau of sol has any integer points.
1651 * Leave tab in empty state if no integer point can be found.
1652 * If an integer point can be found and if moreover it is finite,
1653 * then it is added to the list of sample values.
1655 * This function is only called when none of the currently active sample
1656 * values satisfies the most recently added constraint.
1658 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1660 struct isl_tab_undo *snap;
1666 snap = isl_tab_snap(tab);
1667 if (isl_tab_push_basis(tab) < 0)
1670 tab = cut_to_integer_lexmin(tab);
1674 if (!tab->empty && sample_is_finite(tab)) {
1675 struct isl_vec *sample;
1677 sample = isl_tab_get_sample_value(tab);
1679 tab = isl_tab_add_sample(tab, sample);
1682 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1691 /* Check if any of the currently active sample values satisfies
1692 * the inequality "ineq" (an equality if eq is set).
1694 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1702 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1703 isl_assert(tab->mat->ctx, tab->samples, return -1);
1704 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1707 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1709 isl_seq_inner_product(ineq, tab->samples->row[i],
1710 1 + tab->n_var, &v);
1711 sgn = isl_int_sgn(v);
1712 if (eq ? (sgn == 0) : (sgn >= 0))
1717 return i < tab->n_sample;
1720 /* Add a div specifed by "div" to the tableau "tab" and return
1721 * 1 if the div is obviously non-negative.
1723 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1724 int (*add_ineq)(void *user, isl_int *), void *user)
1728 struct isl_mat *samples;
1731 r = isl_tab_add_div(tab, div, add_ineq, user);
1734 nonneg = tab->var[r].is_nonneg;
1735 tab->var[r].frozen = 1;
1737 samples = isl_mat_extend(tab->samples,
1738 tab->n_sample, 1 + tab->n_var);
1739 tab->samples = samples;
1742 for (i = tab->n_outside; i < samples->n_row; ++i) {
1743 isl_seq_inner_product(div->el + 1, samples->row[i],
1744 div->size - 1, &samples->row[i][samples->n_col - 1]);
1745 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1746 samples->row[i][samples->n_col - 1], div->el[0]);
1752 /* Add a div specified by "div" to both the main tableau and
1753 * the context tableau. In case of the main tableau, we only
1754 * need to add an extra div. In the context tableau, we also
1755 * need to express the meaning of the div.
1756 * Return the index of the div or -1 if anything went wrong.
1758 static int add_div(struct isl_tab *tab, struct isl_context *context,
1759 struct isl_vec *div)
1764 if ((nonneg = context->op->add_div(context, div)) < 0)
1767 if (!context->op->is_ok(context))
1770 if (isl_tab_extend_vars(tab, 1) < 0)
1772 r = isl_tab_allocate_var(tab);
1776 tab->var[r].is_nonneg = 1;
1777 tab->var[r].frozen = 1;
1780 return tab->n_div - 1;
1782 context->op->invalidate(context);
1786 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1789 unsigned total = isl_basic_map_total_dim(tab->bmap);
1791 for (i = 0; i < tab->bmap->n_div; ++i) {
1792 if (isl_int_ne(tab->bmap->div[i][0], denom))
1794 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1801 /* Return the index of a div that corresponds to "div".
1802 * We first check if we already have such a div and if not, we create one.
1804 static int get_div(struct isl_tab *tab, struct isl_context *context,
1805 struct isl_vec *div)
1808 struct isl_tab *context_tab = context->op->peek_tab(context);
1813 d = find_div(context_tab, div->el + 1, div->el[0]);
1817 return add_div(tab, context, div);
1820 /* Add a parametric cut to cut away the non-integral sample value
1822 * Let a_i be the coefficients of the constant term and the parameters
1823 * and let b_i be the coefficients of the variables or constraints
1824 * in basis of the tableau.
1825 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1827 * The cut is expressed as
1829 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1831 * If q did not already exist in the context tableau, then it is added first.
1832 * If q is in a column of the main tableau then the "+ q" can be accomplished
1833 * by setting the corresponding entry to the denominator of the constraint.
1834 * If q happens to be in a row of the main tableau, then the corresponding
1835 * row needs to be added instead (taking care of the denominators).
1836 * Note that this is very unlikely, but perhaps not entirely impossible.
1838 * The current value of the cut is known to be negative (or at least
1839 * non-positive), so row_sign is set accordingly.
1841 * Return the row of the cut or -1.
1843 static int add_parametric_cut(struct isl_tab *tab, int row,
1844 struct isl_context *context)
1846 struct isl_vec *div;
1853 unsigned off = 2 + tab->M;
1858 div = get_row_parameter_div(tab, row);
1863 d = context->op->get_div(context, tab, div);
1867 if (isl_tab_extend_cons(tab, 1) < 0)
1869 r = isl_tab_allocate_con(tab);
1873 r_row = tab->mat->row[tab->con[r].index];
1874 isl_int_set(r_row[0], tab->mat->row[row][0]);
1875 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1876 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1877 isl_int_neg(r_row[1], r_row[1]);
1879 isl_int_set_si(r_row[2], 0);
1880 for (i = 0; i < tab->n_param; ++i) {
1881 if (tab->var[i].is_row)
1883 col = tab->var[i].index;
1884 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1885 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1886 tab->mat->row[row][0]);
1887 isl_int_neg(r_row[off + col], r_row[off + col]);
1889 for (i = 0; i < tab->n_div; ++i) {
1890 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1892 col = tab->var[tab->n_var - tab->n_div + i].index;
1893 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1894 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1895 tab->mat->row[row][0]);
1896 isl_int_neg(r_row[off + col], r_row[off + col]);
1898 for (i = 0; i < tab->n_col; ++i) {
1899 if (tab->col_var[i] >= 0 &&
1900 (tab->col_var[i] < tab->n_param ||
1901 tab->col_var[i] >= tab->n_var - tab->n_div))
1903 isl_int_fdiv_r(r_row[off + i],
1904 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1906 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1908 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1910 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1911 isl_int_divexact(r_row[0], r_row[0], gcd);
1912 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1913 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1914 r_row[0], tab->mat->row[d_row] + 1,
1915 off - 1 + tab->n_col);
1916 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1919 col = tab->var[tab->n_var - tab->n_div + d].index;
1920 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1923 tab->con[r].is_nonneg = 1;
1924 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1927 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1931 row = tab->con[r].index;
1933 if (d >= n && context->op->detect_equalities(context, tab) < 0)
1939 /* Construct a tableau for bmap that can be used for computing
1940 * the lexicographic minimum (or maximum) of bmap.
1941 * If not NULL, then dom is the domain where the minimum
1942 * should be computed. In this case, we set up a parametric
1943 * tableau with row signs (initialized to "unknown").
1944 * If M is set, then the tableau will use a big parameter.
1945 * If max is set, then a maximum should be computed instead of a minimum.
1946 * This means that for each variable x, the tableau will contain the variable
1947 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1948 * of the variables in all constraints are negated prior to adding them
1951 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
1952 struct isl_basic_set *dom, unsigned M, int max)
1955 struct isl_tab *tab;
1957 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
1958 isl_basic_map_total_dim(bmap), M);
1962 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1964 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
1965 tab->n_div = dom->n_div;
1966 tab->row_sign = isl_calloc_array(bmap->ctx,
1967 enum isl_tab_row_sign, tab->mat->n_row);
1971 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
1972 if (isl_tab_mark_empty(tab) < 0)
1977 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1978 tab->var[i].is_nonneg = 1;
1979 tab->var[i].frozen = 1;
1981 for (i = 0; i < bmap->n_eq; ++i) {
1983 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1984 bmap->eq[i] + 1 + tab->n_param,
1985 tab->n_var - tab->n_param - tab->n_div);
1986 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
1988 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
1989 bmap->eq[i] + 1 + tab->n_param,
1990 tab->n_var - tab->n_param - tab->n_div);
1991 if (!tab || tab->empty)
1994 for (i = 0; i < bmap->n_ineq; ++i) {
1996 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
1997 bmap->ineq[i] + 1 + tab->n_param,
1998 tab->n_var - tab->n_param - tab->n_div);
1999 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2001 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2002 bmap->ineq[i] + 1 + tab->n_param,
2003 tab->n_var - tab->n_param - tab->n_div);
2004 if (!tab || tab->empty)
2013 /* Given a main tableau where more than one row requires a split,
2014 * determine and return the "best" row to split on.
2016 * Given two rows in the main tableau, if the inequality corresponding
2017 * to the first row is redundant with respect to that of the second row
2018 * in the current tableau, then it is better to split on the second row,
2019 * since in the positive part, both row will be positive.
2020 * (In the negative part a pivot will have to be performed and just about
2021 * anything can happen to the sign of the other row.)
2023 * As a simple heuristic, we therefore select the row that makes the most
2024 * of the other rows redundant.
2026 * Perhaps it would also be useful to look at the number of constraints
2027 * that conflict with any given constraint.
2029 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2031 struct isl_tab_undo *snap;
2037 if (isl_tab_extend_cons(context_tab, 2) < 0)
2040 snap = isl_tab_snap(context_tab);
2042 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2043 struct isl_tab_undo *snap2;
2044 struct isl_vec *ineq = NULL;
2048 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2050 if (tab->row_sign[split] != isl_tab_row_any)
2053 ineq = get_row_parameter_ineq(tab, split);
2056 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2061 snap2 = isl_tab_snap(context_tab);
2063 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2064 struct isl_tab_var *var;
2068 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2070 if (tab->row_sign[row] != isl_tab_row_any)
2073 ineq = get_row_parameter_ineq(tab, row);
2076 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2080 var = &context_tab->con[context_tab->n_con - 1];
2081 if (!context_tab->empty &&
2082 !isl_tab_min_at_most_neg_one(context_tab, var))
2084 if (isl_tab_rollback(context_tab, snap2) < 0)
2087 if (best == -1 || r > best_r) {
2091 if (isl_tab_rollback(context_tab, snap) < 0)
2098 static struct isl_basic_set *context_lex_peek_basic_set(
2099 struct isl_context *context)
2101 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2104 return isl_tab_peek_bset(clex->tab);
2107 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2109 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2113 static void context_lex_extend(struct isl_context *context, int n)
2115 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2118 if (isl_tab_extend_cons(clex->tab, n) >= 0)
2120 isl_tab_free(clex->tab);
2124 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2125 int check, int update)
2127 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2128 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2130 clex->tab = add_lexmin_eq(clex->tab, eq);
2132 int v = tab_has_valid_sample(clex->tab, eq, 1);
2136 clex->tab = check_integer_feasible(clex->tab);
2139 clex->tab = check_samples(clex->tab, eq, 1);
2142 isl_tab_free(clex->tab);
2146 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2147 int check, int update)
2149 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2150 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2152 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2154 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2158 clex->tab = check_integer_feasible(clex->tab);
2161 clex->tab = check_samples(clex->tab, ineq, 0);
2164 isl_tab_free(clex->tab);
2168 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2170 struct isl_context *context = (struct isl_context *)user;
2171 context_lex_add_ineq(context, ineq, 0, 0);
2172 return context->op->is_ok(context) ? 0 : -1;
2175 /* Check which signs can be obtained by "ineq" on all the currently
2176 * active sample values. See row_sign for more information.
2178 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2184 enum isl_tab_row_sign res = isl_tab_row_unknown;
2186 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2187 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2188 return isl_tab_row_unknown);
2191 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2192 isl_seq_inner_product(tab->samples->row[i], ineq,
2193 1 + tab->n_var, &tmp);
2194 sgn = isl_int_sgn(tmp);
2195 if (sgn > 0 || (sgn == 0 && strict)) {
2196 if (res == isl_tab_row_unknown)
2197 res = isl_tab_row_pos;
2198 if (res == isl_tab_row_neg)
2199 res = isl_tab_row_any;
2202 if (res == isl_tab_row_unknown)
2203 res = isl_tab_row_neg;
2204 if (res == isl_tab_row_pos)
2205 res = isl_tab_row_any;
2207 if (res == isl_tab_row_any)
2215 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2216 isl_int *ineq, int strict)
2218 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2219 return tab_ineq_sign(clex->tab, ineq, strict);
2222 /* Check whether "ineq" can be added to the tableau without rendering
2225 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2227 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2228 struct isl_tab_undo *snap;
2234 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2237 snap = isl_tab_snap(clex->tab);
2238 if (isl_tab_push_basis(clex->tab) < 0)
2240 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2241 clex->tab = check_integer_feasible(clex->tab);
2244 feasible = !clex->tab->empty;
2245 if (isl_tab_rollback(clex->tab, snap) < 0)
2251 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2252 struct isl_vec *div)
2254 return get_div(tab, context, div);
2257 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2259 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2260 return context_tab_add_div(clex->tab, div,
2261 context_lex_add_ineq_wrap, context);
2264 static int context_lex_detect_equalities(struct isl_context *context,
2265 struct isl_tab *tab)
2270 static int context_lex_best_split(struct isl_context *context,
2271 struct isl_tab *tab)
2273 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2274 struct isl_tab_undo *snap;
2277 snap = isl_tab_snap(clex->tab);
2278 if (isl_tab_push_basis(clex->tab) < 0)
2280 r = best_split(tab, clex->tab);
2282 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2288 static int context_lex_is_empty(struct isl_context *context)
2290 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2293 return clex->tab->empty;
2296 static void *context_lex_save(struct isl_context *context)
2298 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2299 struct isl_tab_undo *snap;
2301 snap = isl_tab_snap(clex->tab);
2302 if (isl_tab_push_basis(clex->tab) < 0)
2304 if (isl_tab_save_samples(clex->tab) < 0)
2310 static void context_lex_restore(struct isl_context *context, void *save)
2312 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2313 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2314 isl_tab_free(clex->tab);
2319 static int context_lex_is_ok(struct isl_context *context)
2321 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2325 /* For each variable in the context tableau, check if the variable can
2326 * only attain non-negative values. If so, mark the parameter as non-negative
2327 * in the main tableau. This allows for a more direct identification of some
2328 * cases of violated constraints.
2330 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2331 struct isl_tab *context_tab)
2334 struct isl_tab_undo *snap;
2335 struct isl_vec *ineq = NULL;
2336 struct isl_tab_var *var;
2339 if (context_tab->n_var == 0)
2342 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2346 if (isl_tab_extend_cons(context_tab, 1) < 0)
2349 snap = isl_tab_snap(context_tab);
2352 isl_seq_clr(ineq->el, ineq->size);
2353 for (i = 0; i < context_tab->n_var; ++i) {
2354 isl_int_set_si(ineq->el[1 + i], 1);
2355 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2357 var = &context_tab->con[context_tab->n_con - 1];
2358 if (!context_tab->empty &&
2359 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2361 if (i >= tab->n_param)
2362 j = i - tab->n_param + tab->n_var - tab->n_div;
2363 tab->var[j].is_nonneg = 1;
2366 isl_int_set_si(ineq->el[1 + i], 0);
2367 if (isl_tab_rollback(context_tab, snap) < 0)
2371 if (context_tab->M && n == context_tab->n_var) {
2372 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2384 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2385 struct isl_context *context, struct isl_tab *tab)
2387 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2388 struct isl_tab_undo *snap;
2393 snap = isl_tab_snap(clex->tab);
2394 if (isl_tab_push_basis(clex->tab) < 0)
2397 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2399 if (isl_tab_rollback(clex->tab, snap) < 0)
2408 static void context_lex_invalidate(struct isl_context *context)
2410 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2411 isl_tab_free(clex->tab);
2415 static void context_lex_free(struct isl_context *context)
2417 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2418 isl_tab_free(clex->tab);
2422 struct isl_context_op isl_context_lex_op = {
2423 context_lex_detect_nonnegative_parameters,
2424 context_lex_peek_basic_set,
2425 context_lex_peek_tab,
2427 context_lex_add_ineq,
2428 context_lex_ineq_sign,
2429 context_lex_test_ineq,
2430 context_lex_get_div,
2431 context_lex_add_div,
2432 context_lex_detect_equalities,
2433 context_lex_best_split,
2434 context_lex_is_empty,
2437 context_lex_restore,
2438 context_lex_invalidate,
2442 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2444 struct isl_tab *tab;
2446 bset = isl_basic_set_cow(bset);
2449 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2452 if (isl_tab_track_bset(tab, bset) < 0)
2454 tab = isl_tab_init_samples(tab);
2457 isl_basic_set_free(bset);
2461 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2463 struct isl_context_lex *clex;
2468 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2472 clex->context.op = &isl_context_lex_op;
2474 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2475 clex->tab = restore_lexmin(clex->tab);
2476 clex->tab = check_integer_feasible(clex->tab);
2480 return &clex->context;
2482 clex->context.op->free(&clex->context);
2486 struct isl_context_gbr {
2487 struct isl_context context;
2488 struct isl_tab *tab;
2489 struct isl_tab *shifted;
2490 struct isl_tab *cone;
2493 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2494 struct isl_context *context, struct isl_tab *tab)
2496 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2499 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2502 static struct isl_basic_set *context_gbr_peek_basic_set(
2503 struct isl_context *context)
2505 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2508 return isl_tab_peek_bset(cgbr->tab);
2511 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2513 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2517 /* Initialize the "shifted" tableau of the context, which
2518 * contains the constraints of the original tableau shifted
2519 * by the sum of all negative coefficients. This ensures
2520 * that any rational point in the shifted tableau can
2521 * be rounded up to yield an integer point in the original tableau.
2523 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2526 struct isl_vec *cst;
2527 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2528 unsigned dim = isl_basic_set_total_dim(bset);
2530 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2534 for (i = 0; i < bset->n_ineq; ++i) {
2535 isl_int_set(cst->el[i], bset->ineq[i][0]);
2536 for (j = 0; j < dim; ++j) {
2537 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2539 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2540 bset->ineq[i][1 + j]);
2544 cgbr->shifted = isl_tab_from_basic_set(bset);
2546 for (i = 0; i < bset->n_ineq; ++i)
2547 isl_int_set(bset->ineq[i][0], cst->el[i]);
2552 /* Check if the shifted tableau is non-empty, and if so
2553 * use the sample point to construct an integer point
2554 * of the context tableau.
2556 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2558 struct isl_vec *sample;
2561 gbr_init_shifted(cgbr);
2564 if (cgbr->shifted->empty)
2565 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2567 sample = isl_tab_get_sample_value(cgbr->shifted);
2568 sample = isl_vec_ceil(sample);
2573 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2580 for (i = 0; i < bset->n_eq; ++i)
2581 isl_int_set_si(bset->eq[i][0], 0);
2583 for (i = 0; i < bset->n_ineq; ++i)
2584 isl_int_set_si(bset->ineq[i][0], 0);
2589 static int use_shifted(struct isl_context_gbr *cgbr)
2591 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2594 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2596 struct isl_basic_set *bset;
2597 struct isl_basic_set *cone;
2599 if (isl_tab_sample_is_integer(cgbr->tab))
2600 return isl_tab_get_sample_value(cgbr->tab);
2602 if (use_shifted(cgbr)) {
2603 struct isl_vec *sample;
2605 sample = gbr_get_shifted_sample(cgbr);
2606 if (!sample || sample->size > 0)
2609 isl_vec_free(sample);
2613 bset = isl_tab_peek_bset(cgbr->tab);
2614 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2617 if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
2620 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2623 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2624 struct isl_vec *sample;
2625 struct isl_tab_undo *snap;
2627 if (cgbr->tab->basis) {
2628 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2629 isl_mat_free(cgbr->tab->basis);
2630 cgbr->tab->basis = NULL;
2632 cgbr->tab->n_zero = 0;
2633 cgbr->tab->n_unbounded = 0;
2636 snap = isl_tab_snap(cgbr->tab);
2638 sample = isl_tab_sample(cgbr->tab);
2640 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2641 isl_vec_free(sample);
2648 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2649 cone = drop_constant_terms(cone);
2650 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2651 cone = isl_basic_set_underlying_set(cone);
2652 cone = isl_basic_set_gauss(cone, NULL);
2654 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2655 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2656 bset = isl_basic_set_underlying_set(bset);
2657 bset = isl_basic_set_gauss(bset, NULL);
2659 return isl_basic_set_sample_with_cone(bset, cone);
2662 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2664 struct isl_vec *sample;
2669 if (cgbr->tab->empty)
2672 sample = gbr_get_sample(cgbr);
2676 if (sample->size == 0) {
2677 isl_vec_free(sample);
2678 if (isl_tab_mark_empty(cgbr->tab) < 0)
2683 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2687 isl_tab_free(cgbr->tab);
2691 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2698 if (isl_tab_extend_cons(tab, 2) < 0)
2701 if (isl_tab_add_eq(tab, eq) < 0)
2710 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2711 int check, int update)
2713 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2715 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2717 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2718 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2720 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2725 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2729 check_gbr_integer_feasible(cgbr);
2732 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2735 isl_tab_free(cgbr->tab);
2739 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2744 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2747 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2750 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2753 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2755 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2758 for (i = 0; i < dim; ++i) {
2759 if (!isl_int_is_neg(ineq[1 + i]))
2761 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2764 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2767 for (i = 0; i < dim; ++i) {
2768 if (!isl_int_is_neg(ineq[1 + i]))
2770 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2774 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2775 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2777 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2783 isl_tab_free(cgbr->tab);
2787 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2788 int check, int update)
2790 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2792 add_gbr_ineq(cgbr, ineq);
2797 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2801 check_gbr_integer_feasible(cgbr);
2804 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2807 isl_tab_free(cgbr->tab);
2811 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2813 struct isl_context *context = (struct isl_context *)user;
2814 context_gbr_add_ineq(context, ineq, 0, 0);
2815 return context->op->is_ok(context) ? 0 : -1;
2818 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2819 isl_int *ineq, int strict)
2821 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2822 return tab_ineq_sign(cgbr->tab, ineq, strict);
2825 /* Check whether "ineq" can be added to the tableau without rendering
2828 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2830 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2831 struct isl_tab_undo *snap;
2832 struct isl_tab_undo *shifted_snap = NULL;
2833 struct isl_tab_undo *cone_snap = NULL;
2839 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2842 snap = isl_tab_snap(cgbr->tab);
2844 shifted_snap = isl_tab_snap(cgbr->shifted);
2846 cone_snap = isl_tab_snap(cgbr->cone);
2847 add_gbr_ineq(cgbr, ineq);
2848 check_gbr_integer_feasible(cgbr);
2851 feasible = !cgbr->tab->empty;
2852 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2855 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2857 } else if (cgbr->shifted) {
2858 isl_tab_free(cgbr->shifted);
2859 cgbr->shifted = NULL;
2862 if (isl_tab_rollback(cgbr->cone, cone_snap))
2864 } else if (cgbr->cone) {
2865 isl_tab_free(cgbr->cone);
2872 /* Return the column of the last of the variables associated to
2873 * a column that has a non-zero coefficient.
2874 * This function is called in a context where only coefficients
2875 * of parameters or divs can be non-zero.
2877 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
2881 unsigned dim = tab->n_var - tab->n_param - tab->n_div;
2883 if (tab->n_var == 0)
2886 for (i = tab->n_var - 1; i >= 0; --i) {
2887 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
2889 if (tab->var[i].is_row)
2891 col = tab->var[i].index;
2892 if (!isl_int_is_zero(p[col]))
2899 /* Look through all the recently added equalities in the context
2900 * to see if we can propagate any of them to the main tableau.
2902 * The newly added equalities in the context are encoded as pairs
2903 * of inequalities starting at inequality "first".
2905 * We tentatively add each of these equalities to the main tableau
2906 * and if this happens to result in a row with a final coefficient
2907 * that is one or negative one, we use it to kill a column
2908 * in the main tableau. Otherwise, we discard the tentatively
2911 static void propagate_equalities(struct isl_context_gbr *cgbr,
2912 struct isl_tab *tab, unsigned first)
2915 struct isl_vec *eq = NULL;
2917 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2921 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
2924 isl_seq_clr(eq->el + 1 + tab->n_param,
2925 tab->n_var - tab->n_param - tab->n_div);
2926 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
2929 struct isl_tab_undo *snap;
2930 snap = isl_tab_snap(tab);
2932 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
2933 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
2934 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
2937 r = isl_tab_add_row(tab, eq->el);
2940 r = tab->con[r].index;
2941 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
2942 if (j < 0 || j < tab->n_dead ||
2943 !isl_int_is_one(tab->mat->row[r][0]) ||
2944 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
2945 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
2946 if (isl_tab_rollback(tab, snap) < 0)
2950 if (isl_tab_pivot(tab, r, j) < 0)
2952 if (isl_tab_kill_col(tab, j) < 0)
2955 tab = restore_lexmin(tab);
2963 isl_tab_free(cgbr->tab);
2967 static int context_gbr_detect_equalities(struct isl_context *context,
2968 struct isl_tab *tab)
2970 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2971 struct isl_ctx *ctx;
2973 enum isl_lp_result res;
2976 ctx = cgbr->tab->mat->ctx;
2979 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2980 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2983 if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
2986 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2989 n_ineq = cgbr->tab->bmap->n_ineq;
2990 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
2991 if (cgbr->tab && cgbr->tab->bmap->n_ineq > n_ineq)
2992 propagate_equalities(cgbr, tab, n_ineq);
2996 isl_tab_free(cgbr->tab);
3001 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3002 struct isl_vec *div)
3004 return get_div(tab, context, div);
3007 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3009 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3013 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3015 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3017 if (isl_tab_allocate_var(cgbr->cone) <0)
3020 cgbr->cone->bmap = isl_basic_map_extend_dim(cgbr->cone->bmap,
3021 isl_basic_map_get_dim(cgbr->cone->bmap), 1, 0, 2);
3022 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3025 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3026 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3029 return context_tab_add_div(cgbr->tab, div,
3030 context_gbr_add_ineq_wrap, context);
3033 static int context_gbr_best_split(struct isl_context *context,
3034 struct isl_tab *tab)
3036 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3037 struct isl_tab_undo *snap;
3040 snap = isl_tab_snap(cgbr->tab);
3041 r = best_split(tab, cgbr->tab);
3043 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3049 static int context_gbr_is_empty(struct isl_context *context)
3051 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3054 return cgbr->tab->empty;
3057 struct isl_gbr_tab_undo {
3058 struct isl_tab_undo *tab_snap;
3059 struct isl_tab_undo *shifted_snap;
3060 struct isl_tab_undo *cone_snap;
3063 static void *context_gbr_save(struct isl_context *context)
3065 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3066 struct isl_gbr_tab_undo *snap;
3068 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3072 snap->tab_snap = isl_tab_snap(cgbr->tab);
3073 if (isl_tab_save_samples(cgbr->tab) < 0)
3077 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3079 snap->shifted_snap = NULL;
3082 snap->cone_snap = isl_tab_snap(cgbr->cone);
3084 snap->cone_snap = NULL;
3092 static void context_gbr_restore(struct isl_context *context, void *save)
3094 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3095 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3098 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3099 isl_tab_free(cgbr->tab);
3103 if (snap->shifted_snap) {
3104 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3106 } else if (cgbr->shifted) {
3107 isl_tab_free(cgbr->shifted);
3108 cgbr->shifted = NULL;
3111 if (snap->cone_snap) {
3112 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3114 } else if (cgbr->cone) {
3115 isl_tab_free(cgbr->cone);
3124 isl_tab_free(cgbr->tab);
3128 static int context_gbr_is_ok(struct isl_context *context)
3130 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3134 static void context_gbr_invalidate(struct isl_context *context)
3136 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3137 isl_tab_free(cgbr->tab);
3141 static void context_gbr_free(struct isl_context *context)
3143 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3144 isl_tab_free(cgbr->tab);
3145 isl_tab_free(cgbr->shifted);
3146 isl_tab_free(cgbr->cone);
3150 struct isl_context_op isl_context_gbr_op = {
3151 context_gbr_detect_nonnegative_parameters,
3152 context_gbr_peek_basic_set,
3153 context_gbr_peek_tab,
3155 context_gbr_add_ineq,
3156 context_gbr_ineq_sign,
3157 context_gbr_test_ineq,
3158 context_gbr_get_div,
3159 context_gbr_add_div,
3160 context_gbr_detect_equalities,
3161 context_gbr_best_split,
3162 context_gbr_is_empty,
3165 context_gbr_restore,
3166 context_gbr_invalidate,
3170 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3172 struct isl_context_gbr *cgbr;
3177 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3181 cgbr->context.op = &isl_context_gbr_op;
3183 cgbr->shifted = NULL;
3185 cgbr->tab = isl_tab_from_basic_set(dom);
3186 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3189 if (isl_tab_track_bset(cgbr->tab,
3190 isl_basic_set_cow(isl_basic_set_copy(dom))) < 0)
3192 check_gbr_integer_feasible(cgbr);
3194 return &cgbr->context;
3196 cgbr->context.op->free(&cgbr->context);
3200 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3205 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3206 return isl_context_lex_alloc(dom);
3208 return isl_context_gbr_alloc(dom);
3211 /* Construct an isl_sol_map structure for accumulating the solution.
3212 * If track_empty is set, then we also keep track of the parts
3213 * of the context where there is no solution.
3214 * If max is set, then we are solving a maximization, rather than
3215 * a minimization problem, which means that the variables in the
3216 * tableau have value "M - x" rather than "M + x".
3218 static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
3219 struct isl_basic_set *dom, int track_empty, int max)
3221 struct isl_sol_map *sol_map = NULL;
3226 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3230 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3231 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3232 sol_map->sol.dec_level.sol = &sol_map->sol;
3233 sol_map->sol.max = max;
3234 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3235 sol_map->sol.add = &sol_map_add_wrap;
3236 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3237 sol_map->sol.free = &sol_map_free_wrap;
3238 sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
3243 sol_map->sol.context = isl_context_alloc(dom);
3244 if (!sol_map->sol.context)
3248 sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
3249 1, ISL_SET_DISJOINT);
3250 if (!sol_map->empty)
3254 isl_basic_set_free(dom);
3257 isl_basic_set_free(dom);
3258 sol_map_free(sol_map);
3262 /* Check whether all coefficients of (non-parameter) variables
3263 * are non-positive, meaning that no pivots can be performed on the row.
3265 static int is_critical(struct isl_tab *tab, int row)
3268 unsigned off = 2 + tab->M;
3270 for (j = tab->n_dead; j < tab->n_col; ++j) {
3271 if (tab->col_var[j] >= 0 &&
3272 (tab->col_var[j] < tab->n_param ||
3273 tab->col_var[j] >= tab->n_var - tab->n_div))
3276 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3283 /* Check whether the inequality represented by vec is strict over the integers,
3284 * i.e., there are no integer values satisfying the constraint with
3285 * equality. This happens if the gcd of the coefficients is not a divisor
3286 * of the constant term. If so, scale the constraint down by the gcd
3287 * of the coefficients.
3289 static int is_strict(struct isl_vec *vec)
3295 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3296 if (!isl_int_is_one(gcd)) {
3297 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3298 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3299 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3306 /* Determine the sign of the given row of the main tableau.
3307 * The result is one of
3308 * isl_tab_row_pos: always non-negative; no pivot needed
3309 * isl_tab_row_neg: always non-positive; pivot
3310 * isl_tab_row_any: can be both positive and negative; split
3312 * We first handle some simple cases
3313 * - the row sign may be known already
3314 * - the row may be obviously non-negative
3315 * - the parametric constant may be equal to that of another row
3316 * for which we know the sign. This sign will be either "pos" or
3317 * "any". If it had been "neg" then we would have pivoted before.
3319 * If none of these cases hold, we check the value of the row for each
3320 * of the currently active samples. Based on the signs of these values
3321 * we make an initial determination of the sign of the row.
3323 * all zero -> unk(nown)
3324 * all non-negative -> pos
3325 * all non-positive -> neg
3326 * both negative and positive -> all
3328 * If we end up with "all", we are done.
3329 * Otherwise, we perform a check for positive and/or negative
3330 * values as follows.
3332 * samples neg unk pos
3338 * There is no special sign for "zero", because we can usually treat zero
3339 * as either non-negative or non-positive, whatever works out best.
3340 * However, if the row is "critical", meaning that pivoting is impossible
3341 * then we don't want to limp zero with the non-positive case, because
3342 * then we we would lose the solution for those values of the parameters
3343 * where the value of the row is zero. Instead, we treat 0 as non-negative
3344 * ensuring a split if the row can attain both zero and negative values.
3345 * The same happens when the original constraint was one that could not
3346 * be satisfied with equality by any integer values of the parameters.
3347 * In this case, we normalize the constraint, but then a value of zero
3348 * for the normalized constraint is actually a positive value for the
3349 * original constraint, so again we need to treat zero as non-negative.
3350 * In both these cases, we have the following decision tree instead:
3352 * all non-negative -> pos
3353 * all negative -> neg
3354 * both negative and non-negative -> all
3362 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3363 struct isl_sol *sol, int row)
3365 struct isl_vec *ineq = NULL;
3366 enum isl_tab_row_sign res = isl_tab_row_unknown;
3371 if (tab->row_sign[row] != isl_tab_row_unknown)
3372 return tab->row_sign[row];
3373 if (is_obviously_nonneg(tab, row))
3374 return isl_tab_row_pos;
3375 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3376 if (tab->row_sign[row2] == isl_tab_row_unknown)
3378 if (identical_parameter_line(tab, row, row2))
3379 return tab->row_sign[row2];
3382 critical = is_critical(tab, row);
3384 ineq = get_row_parameter_ineq(tab, row);
3388 strict = is_strict(ineq);
3390 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3391 critical || strict);
3393 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3394 /* test for negative values */
3396 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3397 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3399 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3403 res = isl_tab_row_pos;
3405 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3407 if (res == isl_tab_row_neg) {
3408 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3409 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3413 if (res == isl_tab_row_neg) {
3414 /* test for positive values */
3416 if (!critical && !strict)
3417 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3419 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3423 res = isl_tab_row_any;
3430 return isl_tab_row_unknown;
3433 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3435 /* Find solutions for values of the parameters that satisfy the given
3438 * We currently take a snapshot of the context tableau that is reset
3439 * when we return from this function, while we make a copy of the main
3440 * tableau, leaving the original main tableau untouched.
3441 * These are fairly arbitrary choices. Making a copy also of the context
3442 * tableau would obviate the need to undo any changes made to it later,
3443 * while taking a snapshot of the main tableau could reduce memory usage.
3444 * If we were to switch to taking a snapshot of the main tableau,
3445 * we would have to keep in mind that we need to save the row signs
3446 * and that we need to do this before saving the current basis
3447 * such that the basis has been restore before we restore the row signs.
3449 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3455 saved = sol->context->op->save(sol->context);
3457 tab = isl_tab_dup(tab);
3461 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3463 find_solutions(sol, tab);
3466 sol->context->op->restore(sol->context, saved);
3472 /* Record the absence of solutions for those values of the parameters
3473 * that do not satisfy the given inequality with equality.
3475 static void no_sol_in_strict(struct isl_sol *sol,
3476 struct isl_tab *tab, struct isl_vec *ineq)
3481 if (!sol->context || sol->error)
3483 saved = sol->context->op->save(sol->context);
3485 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3487 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3496 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3498 sol->context->op->restore(sol->context, saved);
3504 /* Compute the lexicographic minimum of the set represented by the main
3505 * tableau "tab" within the context "sol->context_tab".
3506 * On entry the sample value of the main tableau is lexicographically
3507 * less than or equal to this lexicographic minimum.
3508 * Pivots are performed until a feasible point is found, which is then
3509 * necessarily equal to the minimum, or until the tableau is found to
3510 * be infeasible. Some pivots may need to be performed for only some
3511 * feasible values of the context tableau. If so, the context tableau
3512 * is split into a part where the pivot is needed and a part where it is not.
3514 * Whenever we enter the main loop, the main tableau is such that no
3515 * "obvious" pivots need to be performed on it, where "obvious" means
3516 * that the given row can be seen to be negative without looking at
3517 * the context tableau. In particular, for non-parametric problems,
3518 * no pivots need to be performed on the main tableau.
3519 * The caller of find_solutions is responsible for making this property
3520 * hold prior to the first iteration of the loop, while restore_lexmin
3521 * is called before every other iteration.
3523 * Inside the main loop, we first examine the signs of the rows of
3524 * the main tableau within the context of the context tableau.
3525 * If we find a row that is always non-positive for all values of
3526 * the parameters satisfying the context tableau and negative for at
3527 * least one value of the parameters, we perform the appropriate pivot
3528 * and start over. An exception is the case where no pivot can be
3529 * performed on the row. In this case, we require that the sign of
3530 * the row is negative for all values of the parameters (rather than just
3531 * non-positive). This special case is handled inside row_sign, which
3532 * will say that the row can have any sign if it determines that it can
3533 * attain both negative and zero values.
3535 * If we can't find a row that always requires a pivot, but we can find
3536 * one or more rows that require a pivot for some values of the parameters
3537 * (i.e., the row can attain both positive and negative signs), then we split
3538 * the context tableau into two parts, one where we force the sign to be
3539 * non-negative and one where we force is to be negative.
3540 * The non-negative part is handled by a recursive call (through find_in_pos).
3541 * Upon returning from this call, we continue with the negative part and
3542 * perform the required pivot.
3544 * If no such rows can be found, all rows are non-negative and we have
3545 * found a (rational) feasible point. If we only wanted a rational point
3547 * Otherwise, we check if all values of the sample point of the tableau
3548 * are integral for the variables. If so, we have found the minimal
3549 * integral point and we are done.
3550 * If the sample point is not integral, then we need to make a distinction
3551 * based on whether the constant term is non-integral or the coefficients
3552 * of the parameters. Furthermore, in order to decide how to handle
3553 * the non-integrality, we also need to know whether the coefficients
3554 * of the other columns in the tableau are integral. This leads
3555 * to the following table. The first two rows do not correspond
3556 * to a non-integral sample point and are only mentioned for completeness.
3558 * constant parameters other
3561 * int int rat | -> no problem
3563 * rat int int -> fail
3565 * rat int rat -> cut
3568 * rat rat rat | -> parametric cut
3571 * rat rat int | -> split context
3573 * If the parametric constant is completely integral, then there is nothing
3574 * to be done. If the constant term is non-integral, but all the other
3575 * coefficient are integral, then there is nothing that can be done
3576 * and the tableau has no integral solution.
3577 * If, on the other hand, one or more of the other columns have rational
3578 * coeffcients, but the parameter coefficients are all integral, then
3579 * we can perform a regular (non-parametric) cut.
3580 * Finally, if there is any parameter coefficient that is non-integral,
3581 * then we need to involve the context tableau. There are two cases here.
3582 * If at least one other column has a rational coefficient, then we
3583 * can perform a parametric cut in the main tableau by adding a new
3584 * integer division in the context tableau.
3585 * If all other columns have integral coefficients, then we need to
3586 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3587 * is always integral. We do this by introducing an integer division
3588 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3589 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3590 * Since q is expressed in the tableau as
3591 * c + \sum a_i y_i - m q >= 0
3592 * -c - \sum a_i y_i + m q + m - 1 >= 0
3593 * it is sufficient to add the inequality
3594 * -c - \sum a_i y_i + m q >= 0
3595 * In the part of the context where this inequality does not hold, the
3596 * main tableau is marked as being empty.
3598 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3600 struct isl_context *context;
3602 if (!tab || sol->error)
3605 context = sol->context;
3609 if (context->op->is_empty(context))
3612 for (; tab && !tab->empty; tab = restore_lexmin(tab)) {
3615 enum isl_tab_row_sign sgn;
3619 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3620 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3622 sgn = row_sign(tab, sol, row);
3625 tab->row_sign[row] = sgn;
3626 if (sgn == isl_tab_row_any)
3628 if (sgn == isl_tab_row_any && split == -1)
3630 if (sgn == isl_tab_row_neg)
3633 if (row < tab->n_row)
3636 struct isl_vec *ineq;
3638 split = context->op->best_split(context, tab);
3641 ineq = get_row_parameter_ineq(tab, split);
3645 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3646 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3648 if (tab->row_sign[row] == isl_tab_row_any)
3649 tab->row_sign[row] = isl_tab_row_unknown;
3651 tab->row_sign[split] = isl_tab_row_pos;
3653 find_in_pos(sol, tab, ineq->el);
3654 tab->row_sign[split] = isl_tab_row_neg;
3656 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3657 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3659 context->op->add_ineq(context, ineq->el, 0, 1);
3667 row = first_non_integer_row(tab, &flags);
3670 if (ISL_FL_ISSET(flags, I_PAR)) {
3671 if (ISL_FL_ISSET(flags, I_VAR)) {
3672 if (isl_tab_mark_empty(tab) < 0)
3676 row = add_cut(tab, row);
3677 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3678 struct isl_vec *div;
3679 struct isl_vec *ineq;
3681 div = get_row_split_div(tab, row);
3684 d = context->op->get_div(context, tab, div);
3688 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3692 no_sol_in_strict(sol, tab, ineq);
3693 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3694 context->op->add_ineq(context, ineq->el, 1, 1);
3696 if (sol->error || !context->op->is_ok(context))
3698 tab = set_row_cst_to_div(tab, row, d);
3699 if (context->op->is_empty(context))
3702 row = add_parametric_cut(tab, row, context);
3715 /* Compute the lexicographic minimum of the set represented by the main
3716 * tableau "tab" within the context "sol->context_tab".
3718 * As a preprocessing step, we first transfer all the purely parametric
3719 * equalities from the main tableau to the context tableau, i.e.,
3720 * parameters that have been pivoted to a row.
3721 * These equalities are ignored by the main algorithm, because the
3722 * corresponding rows may not be marked as being non-negative.
3723 * In parts of the context where the added equality does not hold,
3724 * the main tableau is marked as being empty.
3726 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3735 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3739 if (tab->row_var[row] < 0)
3741 if (tab->row_var[row] >= tab->n_param &&
3742 tab->row_var[row] < tab->n_var - tab->n_div)
3744 if (tab->row_var[row] < tab->n_param)
3745 p = tab->row_var[row];
3747 p = tab->row_var[row]
3748 + tab->n_param - (tab->n_var - tab->n_div);
3750 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3753 get_row_parameter_line(tab, row, eq->el);
3754 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3755 eq = isl_vec_normalize(eq);
3758 no_sol_in_strict(sol, tab, eq);
3760 isl_seq_neg(eq->el, eq->el, eq->size);
3762 no_sol_in_strict(sol, tab, eq);
3763 isl_seq_neg(eq->el, eq->el, eq->size);
3765 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3769 if (isl_tab_mark_redundant(tab, row) < 0)
3772 if (sol->context->op->is_empty(sol->context))
3775 row = tab->n_redundant - 1;
3778 find_solutions(sol, tab);
3789 static void sol_map_find_solutions(struct isl_sol_map *sol_map,
3790 struct isl_tab *tab)
3792 find_solutions_main(&sol_map->sol, tab);
3795 /* Check if integer division "div" of "dom" also occurs in "bmap".
3796 * If so, return its position within the divs.
3797 * If not, return -1.
3799 static int find_context_div(struct isl_basic_map *bmap,
3800 struct isl_basic_set *dom, unsigned div)
3803 unsigned b_dim = isl_dim_total(bmap->dim);
3804 unsigned d_dim = isl_dim_total(dom->dim);
3806 if (isl_int_is_zero(dom->div[div][0]))
3808 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3811 for (i = 0; i < bmap->n_div; ++i) {
3812 if (isl_int_is_zero(bmap->div[i][0]))
3814 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3815 (b_dim - d_dim) + bmap->n_div) != -1)
3817 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3823 /* The correspondence between the variables in the main tableau,
3824 * the context tableau, and the input map and domain is as follows.
3825 * The first n_param and the last n_div variables of the main tableau
3826 * form the variables of the context tableau.
3827 * In the basic map, these n_param variables correspond to the
3828 * parameters and the input dimensions. In the domain, they correspond
3829 * to the parameters and the set dimensions.
3830 * The n_div variables correspond to the integer divisions in the domain.
3831 * To ensure that everything lines up, we may need to copy some of the
3832 * integer divisions of the domain to the map. These have to be placed
3833 * in the same order as those in the context and they have to be placed
3834 * after any other integer divisions that the map may have.
3835 * This function performs the required reordering.
3837 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3838 struct isl_basic_set *dom)
3844 for (i = 0; i < dom->n_div; ++i)
3845 if (find_context_div(bmap, dom, i) != -1)
3847 other = bmap->n_div - common;
3848 if (dom->n_div - common > 0) {
3849 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
3850 dom->n_div - common, 0, 0);
3854 for (i = 0; i < dom->n_div; ++i) {
3855 int pos = find_context_div(bmap, dom, i);
3857 pos = isl_basic_map_alloc_div(bmap);
3860 isl_int_set_si(bmap->div[pos][0], 0);
3862 if (pos != other + i)
3863 isl_basic_map_swap_div(bmap, pos, other + i);
3867 isl_basic_map_free(bmap);
3871 /* Compute the lexicographic minimum (or maximum if "max" is set)
3872 * of "bmap" over the domain "dom" and return the result as a map.
3873 * If "empty" is not NULL, then *empty is assigned a set that
3874 * contains those parts of the domain where there is no solution.
3875 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
3876 * then we compute the rational optimum. Otherwise, we compute
3877 * the integral optimum.
3879 * We perform some preprocessing. As the PILP solver does not
3880 * handle implicit equalities very well, we first make sure all
3881 * the equalities are explicitly available.
3882 * We also make sure the divs in the domain are properly order,
3883 * because they will be added one by one in the given order
3884 * during the construction of the solution map.
3886 struct isl_map *isl_tab_basic_map_partial_lexopt(
3887 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3888 struct isl_set **empty, int max)
3890 struct isl_tab *tab;
3891 struct isl_map *result = NULL;
3892 struct isl_sol_map *sol_map = NULL;
3893 struct isl_context *context;
3894 struct isl_basic_map *eq;
3901 isl_assert(bmap->ctx,
3902 isl_basic_map_compatible_domain(bmap, dom), goto error);
3904 eq = isl_basic_map_copy(bmap);
3905 eq = isl_basic_map_intersect_domain(eq, isl_basic_set_copy(dom));
3906 eq = isl_basic_map_affine_hull(eq);
3907 bmap = isl_basic_map_intersect(bmap, eq);
3910 dom = isl_basic_set_order_divs(dom);
3911 bmap = align_context_divs(bmap, dom);
3913 sol_map = sol_map_init(bmap, dom, !!empty, max);
3917 context = sol_map->sol.context;
3918 if (isl_basic_set_fast_is_empty(context->op->peek_basic_set(context)))
3920 else if (isl_basic_map_fast_is_empty(bmap))
3921 sol_map_add_empty_if_needed(sol_map,
3922 isl_basic_set_copy(context->op->peek_basic_set(context)));
3924 tab = tab_for_lexmin(bmap,
3925 context->op->peek_basic_set(context), 1, max);
3926 tab = context->op->detect_nonnegative_parameters(context, tab);
3927 sol_map_find_solutions(sol_map, tab);
3929 if (sol_map->sol.error)
3932 result = isl_map_copy(sol_map->map);
3934 *empty = isl_set_copy(sol_map->empty);
3935 sol_free(&sol_map->sol);
3936 isl_basic_map_free(bmap);
3939 sol_free(&sol_map->sol);
3940 isl_basic_map_free(bmap);
3944 struct isl_sol_for {
3946 int (*fn)(__isl_take isl_basic_set *dom,
3947 __isl_take isl_mat *map, void *user);
3951 static void sol_for_free(struct isl_sol_for *sol_for)
3953 if (sol_for->sol.context)
3954 sol_for->sol.context->op->free(sol_for->sol.context);
3958 static void sol_for_free_wrap(struct isl_sol *sol)
3960 sol_for_free((struct isl_sol_for *)sol);
3963 /* Add the solution identified by the tableau and the context tableau.
3965 * See documentation of sol_add for more details.
3967 * Instead of constructing a basic map, this function calls a user
3968 * defined function with the current context as a basic set and
3969 * an affine matrix reprenting the relation between the input and output.
3970 * The number of rows in this matrix is equal to one plus the number
3971 * of output variables. The number of columns is equal to one plus
3972 * the total dimension of the context, i.e., the number of parameters,
3973 * input variables and divs. Since some of the columns in the matrix
3974 * may refer to the divs, the basic set is not simplified.
3975 * (Simplification may reorder or remove divs.)
3977 static void sol_for_add(struct isl_sol_for *sol,
3978 struct isl_basic_set *dom, struct isl_mat *M)
3980 if (sol->sol.error || !dom || !M)
3983 dom = isl_basic_set_simplify(dom);
3984 dom = isl_basic_set_finalize(dom);
3986 if (sol->fn(isl_basic_set_copy(dom), isl_mat_copy(M), sol->user) < 0)
3989 isl_basic_set_free(dom);
3993 isl_basic_set_free(dom);
3998 static void sol_for_add_wrap(struct isl_sol *sol,
3999 struct isl_basic_set *dom, struct isl_mat *M)
4001 sol_for_add((struct isl_sol_for *)sol, dom, M);
4004 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4005 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4009 struct isl_sol_for *sol_for = NULL;
4010 struct isl_dim *dom_dim;
4011 struct isl_basic_set *dom = NULL;
4013 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4017 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4018 dom = isl_basic_set_universe(dom_dim);
4020 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4021 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4022 sol_for->sol.dec_level.sol = &sol_for->sol;
4024 sol_for->user = user;
4025 sol_for->sol.max = max;
4026 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4027 sol_for->sol.add = &sol_for_add_wrap;
4028 sol_for->sol.add_empty = NULL;
4029 sol_for->sol.free = &sol_for_free_wrap;
4031 sol_for->sol.context = isl_context_alloc(dom);
4032 if (!sol_for->sol.context)
4035 isl_basic_set_free(dom);
4038 isl_basic_set_free(dom);
4039 sol_for_free(sol_for);
4043 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4044 struct isl_tab *tab)
4046 find_solutions_main(&sol_for->sol, tab);
4049 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4050 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4054 struct isl_sol_for *sol_for = NULL;
4056 bmap = isl_basic_map_copy(bmap);
4060 bmap = isl_basic_map_detect_equalities(bmap);
4061 sol_for = sol_for_init(bmap, max, fn, user);
4063 if (isl_basic_map_fast_is_empty(bmap))
4066 struct isl_tab *tab;
4067 struct isl_context *context = sol_for->sol.context;
4068 tab = tab_for_lexmin(bmap,
4069 context->op->peek_basic_set(context), 1, max);
4070 tab = context->op->detect_nonnegative_parameters(context, tab);
4071 sol_for_find_solutions(sol_for, tab);
4072 if (sol_for->sol.error)
4076 sol_free(&sol_for->sol);
4077 isl_basic_map_free(bmap);
4080 sol_free(&sol_for->sol);
4081 isl_basic_map_free(bmap);
4085 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map *bmap,
4086 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4090 return isl_basic_map_foreach_lexopt(bmap, 0, fn, user);
4093 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map *bmap,
4094 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_mat *map,
4098 return isl_basic_map_foreach_lexopt(bmap, 1, fn, user);