2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
17 #include "isl_sample.h"
18 #include <isl_mat_private.h>
19 #include <isl_aff_private.h>
20 #include <isl_config.h>
23 * The implementation of parametric integer linear programming in this file
24 * was inspired by the paper "Parametric Integer Programming" and the
25 * report "Solving systems of affine (in)equalities" by Paul Feautrier
28 * The strategy used for obtaining a feasible solution is different
29 * from the one used in isl_tab.c. In particular, in isl_tab.c,
30 * upon finding a constraint that is not yet satisfied, we pivot
31 * in a row that increases the constant term of the row holding the
32 * constraint, making sure the sample solution remains feasible
33 * for all the constraints it already satisfied.
34 * Here, we always pivot in the row holding the constraint,
35 * choosing a column that induces the lexicographically smallest
36 * increment to the sample solution.
38 * By starting out from a sample value that is lexicographically
39 * smaller than any integer point in the problem space, the first
40 * feasible integer sample point we find will also be the lexicographically
41 * smallest. If all variables can be assumed to be non-negative,
42 * then the initial sample value may be chosen equal to zero.
43 * However, we will not make this assumption. Instead, we apply
44 * the "big parameter" trick. Any variable x is then not directly
45 * used in the tableau, but instead it is represented by another
46 * variable x' = M + x, where M is an arbitrarily large (positive)
47 * value. x' is therefore always non-negative, whatever the value of x.
48 * Taking as initial sample value x' = 0 corresponds to x = -M,
49 * which is always smaller than any possible value of x.
51 * The big parameter trick is used in the main tableau and
52 * also in the context tableau if isl_context_lex is used.
53 * In this case, each tableaus has its own big parameter.
54 * Before doing any real work, we check if all the parameters
55 * happen to be non-negative. If so, we drop the column corresponding
56 * to M from the initial context tableau.
57 * If isl_context_gbr is used, then the big parameter trick is only
58 * used in the main tableau.
62 struct isl_context_op {
63 /* detect nonnegative parameters in context and mark them in tab */
64 struct isl_tab *(*detect_nonnegative_parameters)(
65 struct isl_context *context, struct isl_tab *tab);
66 /* return temporary reference to basic set representation of context */
67 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
68 /* return temporary reference to tableau representation of context */
69 struct isl_tab *(*peek_tab)(struct isl_context *context);
70 /* add equality; check is 1 if eq may not be valid;
71 * update is 1 if we may want to call ineq_sign on context later.
73 void (*add_eq)(struct isl_context *context, isl_int *eq,
74 int check, int update);
75 /* add inequality; check is 1 if ineq may not be valid;
76 * update is 1 if we may want to call ineq_sign on context later.
78 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
79 int check, int update);
80 /* check sign of ineq based on previous information.
81 * strict is 1 if saturation should be treated as a positive sign.
83 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
84 isl_int *ineq, int strict);
85 /* check if inequality maintains feasibility */
86 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
87 /* return index of a div that corresponds to "div" */
88 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
90 /* add div "div" to context and return non-negativity */
91 int (*add_div)(struct isl_context *context, struct isl_vec *div);
92 int (*detect_equalities)(struct isl_context *context,
94 /* return row index of "best" split */
95 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
96 /* check if context has already been determined to be empty */
97 int (*is_empty)(struct isl_context *context);
98 /* check if context is still usable */
99 int (*is_ok)(struct isl_context *context);
100 /* save a copy/snapshot of context */
101 void *(*save)(struct isl_context *context);
102 /* restore saved context */
103 void (*restore)(struct isl_context *context, void *);
104 /* invalidate context */
105 void (*invalidate)(struct isl_context *context);
107 void (*free)(struct isl_context *context);
111 struct isl_context_op *op;
114 struct isl_context_lex {
115 struct isl_context context;
119 struct isl_partial_sol {
121 struct isl_basic_set *dom;
124 struct isl_partial_sol *next;
128 struct isl_sol_callback {
129 struct isl_tab_callback callback;
133 /* isl_sol is an interface for constructing a solution to
134 * a parametric integer linear programming problem.
135 * Every time the algorithm reaches a state where a solution
136 * can be read off from the tableau (including cases where the tableau
137 * is empty), the function "add" is called on the isl_sol passed
138 * to find_solutions_main.
140 * The context tableau is owned by isl_sol and is updated incrementally.
142 * There are currently two implementations of this interface,
143 * isl_sol_map, which simply collects the solutions in an isl_map
144 * and (optionally) the parts of the context where there is no solution
146 * isl_sol_for, which calls a user-defined function for each part of
155 struct isl_context *context;
156 struct isl_partial_sol *partial;
157 void (*add)(struct isl_sol *sol,
158 struct isl_basic_set *dom, struct isl_mat *M);
159 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
160 void (*free)(struct isl_sol *sol);
161 struct isl_sol_callback dec_level;
164 static void sol_free(struct isl_sol *sol)
166 struct isl_partial_sol *partial, *next;
169 for (partial = sol->partial; partial; partial = next) {
170 next = partial->next;
171 isl_basic_set_free(partial->dom);
172 isl_mat_free(partial->M);
178 /* Push a partial solution represented by a domain and mapping M
179 * onto the stack of partial solutions.
181 static void sol_push_sol(struct isl_sol *sol,
182 struct isl_basic_set *dom, struct isl_mat *M)
184 struct isl_partial_sol *partial;
186 if (sol->error || !dom)
189 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
193 partial->level = sol->level;
196 partial->next = sol->partial;
198 sol->partial = partial;
202 isl_basic_set_free(dom);
206 /* Pop one partial solution from the partial solution stack and
207 * pass it on to sol->add or sol->add_empty.
209 static void sol_pop_one(struct isl_sol *sol)
211 struct isl_partial_sol *partial;
213 partial = sol->partial;
214 sol->partial = partial->next;
217 sol->add(sol, partial->dom, partial->M);
219 sol->add_empty(sol, partial->dom);
223 /* Return a fresh copy of the domain represented by the context tableau.
225 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
227 struct isl_basic_set *bset;
232 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
233 bset = isl_basic_set_update_from_tab(bset,
234 sol->context->op->peek_tab(sol->context));
239 /* Check whether two partial solutions have the same mapping, where n_div
240 * is the number of divs that the two partial solutions have in common.
242 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
248 if (!s1->M != !s2->M)
253 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
255 for (i = 0; i < s1->M->n_row; ++i) {
256 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
257 s1->M->n_col-1-dim-n_div) != -1)
259 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
260 s2->M->n_col-1-dim-n_div) != -1)
262 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
268 /* Pop all solutions from the partial solution stack that were pushed onto
269 * the stack at levels that are deeper than the current level.
270 * If the two topmost elements on the stack have the same level
271 * and represent the same solution, then their domains are combined.
272 * This combined domain is the same as the current context domain
273 * as sol_pop is called each time we move back to a higher level.
275 static void sol_pop(struct isl_sol *sol)
277 struct isl_partial_sol *partial;
283 if (sol->level == 0) {
284 for (partial = sol->partial; partial; partial = sol->partial)
289 partial = sol->partial;
293 if (partial->level <= sol->level)
296 if (partial->next && partial->next->level == partial->level) {
297 n_div = isl_basic_set_dim(
298 sol->context->op->peek_basic_set(sol->context),
301 if (!same_solution(partial, partial->next, n_div)) {
305 struct isl_basic_set *bset;
307 bset = sol_domain(sol);
309 isl_basic_set_free(partial->next->dom);
310 partial->next->dom = bset;
311 partial->next->level = sol->level;
313 sol->partial = partial->next;
314 isl_basic_set_free(partial->dom);
315 isl_mat_free(partial->M);
322 static void sol_dec_level(struct isl_sol *sol)
332 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
334 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
336 sol_dec_level(callback->sol);
338 return callback->sol->error ? -1 : 0;
341 /* Move down to next level and push callback onto context tableau
342 * to decrease the level again when it gets rolled back across
343 * the current state. That is, dec_level will be called with
344 * the context tableau in the same state as it is when inc_level
347 static void sol_inc_level(struct isl_sol *sol)
355 tab = sol->context->op->peek_tab(sol->context);
356 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
360 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
364 if (isl_int_is_one(m))
367 for (i = 0; i < n_row; ++i)
368 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
371 /* Add the solution identified by the tableau and the context tableau.
373 * The layout of the variables is as follows.
374 * tab->n_var is equal to the total number of variables in the input
375 * map (including divs that were copied from the context)
376 * + the number of extra divs constructed
377 * Of these, the first tab->n_param and the last tab->n_div variables
378 * correspond to the variables in the context, i.e.,
379 * tab->n_param + tab->n_div = context_tab->n_var
380 * tab->n_param is equal to the number of parameters and input
381 * dimensions in the input map
382 * tab->n_div is equal to the number of divs in the context
384 * If there is no solution, then call add_empty with a basic set
385 * that corresponds to the context tableau. (If add_empty is NULL,
388 * If there is a solution, then first construct a matrix that maps
389 * all dimensions of the context to the output variables, i.e.,
390 * the output dimensions in the input map.
391 * The divs in the input map (if any) that do not correspond to any
392 * div in the context do not appear in the solution.
393 * The algorithm will make sure that they have an integer value,
394 * but these values themselves are of no interest.
395 * We have to be careful not to drop or rearrange any divs in the
396 * context because that would change the meaning of the matrix.
398 * To extract the value of the output variables, it should be noted
399 * that we always use a big parameter M in the main tableau and so
400 * the variable stored in this tableau is not an output variable x itself, but
401 * x' = M + x (in case of minimization)
403 * x' = M - x (in case of maximization)
404 * If x' appears in a column, then its optimal value is zero,
405 * which means that the optimal value of x is an unbounded number
406 * (-M for minimization and M for maximization).
407 * We currently assume that the output dimensions in the original map
408 * are bounded, so this cannot occur.
409 * Similarly, when x' appears in a row, then the coefficient of M in that
410 * row is necessarily 1.
411 * If the row in the tableau represents
412 * d x' = c + d M + e(y)
413 * then, in case of minimization, the corresponding row in the matrix
416 * with a d = m, the (updated) common denominator of the matrix.
417 * In case of maximization, the row will be
420 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
422 struct isl_basic_set *bset = NULL;
423 struct isl_mat *mat = NULL;
428 if (sol->error || !tab)
431 if (tab->empty && !sol->add_empty)
434 bset = sol_domain(sol);
437 sol_push_sol(sol, bset, NULL);
443 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
444 1 + tab->n_param + tab->n_div);
450 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
451 isl_int_set_si(mat->row[0][0], 1);
452 for (row = 0; row < sol->n_out; ++row) {
453 int i = tab->n_param + row;
456 isl_seq_clr(mat->row[1 + row], mat->n_col);
457 if (!tab->var[i].is_row) {
459 isl_die(mat->ctx, isl_error_invalid,
460 "unbounded optimum", goto error2);
464 r = tab->var[i].index;
466 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
467 isl_die(mat->ctx, isl_error_invalid,
468 "unbounded optimum", goto error2);
469 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
470 isl_int_divexact(m, tab->mat->row[r][0], m);
471 scale_rows(mat, m, 1 + row);
472 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
473 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
474 for (j = 0; j < tab->n_param; ++j) {
476 if (tab->var[j].is_row)
478 col = tab->var[j].index;
479 isl_int_mul(mat->row[1 + row][1 + j], m,
480 tab->mat->row[r][off + col]);
482 for (j = 0; j < tab->n_div; ++j) {
484 if (tab->var[tab->n_var - tab->n_div+j].is_row)
486 col = tab->var[tab->n_var - tab->n_div+j].index;
487 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
488 tab->mat->row[r][off + col]);
491 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
497 sol_push_sol(sol, bset, mat);
502 isl_basic_set_free(bset);
510 struct isl_set *empty;
513 static void sol_map_free(struct isl_sol_map *sol_map)
517 if (sol_map->sol.context)
518 sol_map->sol.context->op->free(sol_map->sol.context);
519 isl_map_free(sol_map->map);
520 isl_set_free(sol_map->empty);
524 static void sol_map_free_wrap(struct isl_sol *sol)
526 sol_map_free((struct isl_sol_map *)sol);
529 /* This function is called for parts of the context where there is
530 * no solution, with "bset" corresponding to the context tableau.
531 * Simply add the basic set to the set "empty".
533 static void sol_map_add_empty(struct isl_sol_map *sol,
534 struct isl_basic_set *bset)
538 isl_assert(bset->ctx, sol->empty, goto error);
540 sol->empty = isl_set_grow(sol->empty, 1);
541 bset = isl_basic_set_simplify(bset);
542 bset = isl_basic_set_finalize(bset);
543 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
546 isl_basic_set_free(bset);
549 isl_basic_set_free(bset);
553 static void sol_map_add_empty_wrap(struct isl_sol *sol,
554 struct isl_basic_set *bset)
556 sol_map_add_empty((struct isl_sol_map *)sol, bset);
559 /* Add bset to sol's empty, but only if we are actually collecting
562 static void sol_map_add_empty_if_needed(struct isl_sol_map *sol,
563 struct isl_basic_set *bset)
566 sol_map_add_empty(sol, bset);
568 isl_basic_set_free(bset);
571 /* Given a basic map "dom" that represents the context and an affine
572 * matrix "M" that maps the dimensions of the context to the
573 * output variables, construct a basic map with the same parameters
574 * and divs as the context, the dimensions of the context as input
575 * dimensions and a number of output dimensions that is equal to
576 * the number of output dimensions in the input map.
578 * The constraints and divs of the context are simply copied
579 * from "dom". For each row
583 * is added, with d the common denominator of M.
585 static void sol_map_add(struct isl_sol_map *sol,
586 struct isl_basic_set *dom, struct isl_mat *M)
589 struct isl_basic_map *bmap = NULL;
597 if (sol->sol.error || !dom || !M)
600 n_out = sol->sol.n_out;
601 n_eq = dom->n_eq + n_out;
602 n_ineq = dom->n_ineq;
604 nparam = isl_basic_set_total_dim(dom) - n_div;
605 total = isl_map_dim(sol->map, isl_dim_all);
606 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
607 n_div, n_eq, 2 * n_div + n_ineq);
610 if (sol->sol.rational)
611 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
612 for (i = 0; i < dom->n_div; ++i) {
613 int k = isl_basic_map_alloc_div(bmap);
616 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
617 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
618 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
619 dom->div[i] + 1 + 1 + nparam, i);
621 for (i = 0; i < dom->n_eq; ++i) {
622 int k = isl_basic_map_alloc_equality(bmap);
625 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
626 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
627 isl_seq_cpy(bmap->eq[k] + 1 + total,
628 dom->eq[i] + 1 + nparam, n_div);
630 for (i = 0; i < dom->n_ineq; ++i) {
631 int k = isl_basic_map_alloc_inequality(bmap);
634 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
635 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
636 isl_seq_cpy(bmap->ineq[k] + 1 + total,
637 dom->ineq[i] + 1 + nparam, n_div);
639 for (i = 0; i < M->n_row - 1; ++i) {
640 int k = isl_basic_map_alloc_equality(bmap);
643 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
644 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
645 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
646 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
647 M->row[1 + i] + 1 + nparam, n_div);
649 bmap = isl_basic_map_simplify(bmap);
650 bmap = isl_basic_map_finalize(bmap);
651 sol->map = isl_map_grow(sol->map, 1);
652 sol->map = isl_map_add_basic_map(sol->map, bmap);
653 isl_basic_set_free(dom);
659 isl_basic_set_free(dom);
661 isl_basic_map_free(bmap);
665 static void sol_map_add_wrap(struct isl_sol *sol,
666 struct isl_basic_set *dom, struct isl_mat *M)
668 sol_map_add((struct isl_sol_map *)sol, dom, M);
672 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
673 * i.e., the constant term and the coefficients of all variables that
674 * appear in the context tableau.
675 * Note that the coefficient of the big parameter M is NOT copied.
676 * The context tableau may not have a big parameter and even when it
677 * does, it is a different big parameter.
679 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
682 unsigned off = 2 + tab->M;
684 isl_int_set(line[0], tab->mat->row[row][1]);
685 for (i = 0; i < tab->n_param; ++i) {
686 if (tab->var[i].is_row)
687 isl_int_set_si(line[1 + i], 0);
689 int col = tab->var[i].index;
690 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
693 for (i = 0; i < tab->n_div; ++i) {
694 if (tab->var[tab->n_var - tab->n_div + i].is_row)
695 isl_int_set_si(line[1 + tab->n_param + i], 0);
697 int col = tab->var[tab->n_var - tab->n_div + i].index;
698 isl_int_set(line[1 + tab->n_param + i],
699 tab->mat->row[row][off + col]);
704 /* Check if rows "row1" and "row2" have identical "parametric constants",
705 * as explained above.
706 * In this case, we also insist that the coefficients of the big parameter
707 * be the same as the values of the constants will only be the same
708 * if these coefficients are also the same.
710 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
713 unsigned off = 2 + tab->M;
715 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
718 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
719 tab->mat->row[row2][2]))
722 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
723 int pos = i < tab->n_param ? i :
724 tab->n_var - tab->n_div + i - tab->n_param;
727 if (tab->var[pos].is_row)
729 col = tab->var[pos].index;
730 if (isl_int_ne(tab->mat->row[row1][off + col],
731 tab->mat->row[row2][off + col]))
737 /* Return an inequality that expresses that the "parametric constant"
738 * should be non-negative.
739 * This function is only called when the coefficient of the big parameter
742 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
744 struct isl_vec *ineq;
746 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
750 get_row_parameter_line(tab, row, ineq->el);
752 ineq = isl_vec_normalize(ineq);
757 /* Return a integer division for use in a parametric cut based on the given row.
758 * In particular, let the parametric constant of the row be
762 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
763 * The div returned is equal to
765 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
767 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
771 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
775 isl_int_set(div->el[0], tab->mat->row[row][0]);
776 get_row_parameter_line(tab, row, div->el + 1);
777 div = isl_vec_normalize(div);
778 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
779 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
784 /* Return a integer division for use in transferring an integrality constraint
786 * In particular, let the parametric constant of the row be
790 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
791 * The the returned div is equal to
793 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
795 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
799 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
803 isl_int_set(div->el[0], tab->mat->row[row][0]);
804 get_row_parameter_line(tab, row, div->el + 1);
805 div = isl_vec_normalize(div);
806 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
811 /* Construct and return an inequality that expresses an upper bound
813 * In particular, if the div is given by
817 * then the inequality expresses
821 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
825 struct isl_vec *ineq;
830 total = isl_basic_set_total_dim(bset);
831 div_pos = 1 + total - bset->n_div + div;
833 ineq = isl_vec_alloc(bset->ctx, 1 + total);
837 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
838 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
842 /* Given a row in the tableau and a div that was created
843 * using get_row_split_div and that been constrained to equality, i.e.,
845 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
847 * replace the expression "\sum_i {a_i} y_i" in the row by d,
848 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
849 * The coefficients of the non-parameters in the tableau have been
850 * verified to be integral. We can therefore simply replace coefficient b
851 * by floor(b). For the coefficients of the parameters we have
852 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
855 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
857 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
858 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
860 isl_int_set_si(tab->mat->row[row][0], 1);
862 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
863 int drow = tab->var[tab->n_var - tab->n_div + div].index;
865 isl_assert(tab->mat->ctx,
866 isl_int_is_one(tab->mat->row[drow][0]), goto error);
867 isl_seq_combine(tab->mat->row[row] + 1,
868 tab->mat->ctx->one, tab->mat->row[row] + 1,
869 tab->mat->ctx->one, tab->mat->row[drow] + 1,
870 1 + tab->M + tab->n_col);
872 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
874 isl_int_set_si(tab->mat->row[row][2 + tab->M + dcol], 1);
883 /* Check if the (parametric) constant of the given row is obviously
884 * negative, meaning that we don't need to consult the context tableau.
885 * If there is a big parameter and its coefficient is non-zero,
886 * then this coefficient determines the outcome.
887 * Otherwise, we check whether the constant is negative and
888 * all non-zero coefficients of parameters are negative and
889 * belong to non-negative parameters.
891 static int is_obviously_neg(struct isl_tab *tab, int row)
895 unsigned off = 2 + tab->M;
898 if (isl_int_is_pos(tab->mat->row[row][2]))
900 if (isl_int_is_neg(tab->mat->row[row][2]))
904 if (isl_int_is_nonneg(tab->mat->row[row][1]))
906 for (i = 0; i < tab->n_param; ++i) {
907 /* Eliminated parameter */
908 if (tab->var[i].is_row)
910 col = tab->var[i].index;
911 if (isl_int_is_zero(tab->mat->row[row][off + col]))
913 if (!tab->var[i].is_nonneg)
915 if (isl_int_is_pos(tab->mat->row[row][off + col]))
918 for (i = 0; i < tab->n_div; ++i) {
919 if (tab->var[tab->n_var - tab->n_div + i].is_row)
921 col = tab->var[tab->n_var - tab->n_div + i].index;
922 if (isl_int_is_zero(tab->mat->row[row][off + col]))
924 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
926 if (isl_int_is_pos(tab->mat->row[row][off + col]))
932 /* Check if the (parametric) constant of the given row is obviously
933 * non-negative, meaning that we don't need to consult the context tableau.
934 * If there is a big parameter and its coefficient is non-zero,
935 * then this coefficient determines the outcome.
936 * Otherwise, we check whether the constant is non-negative and
937 * all non-zero coefficients of parameters are positive and
938 * belong to non-negative parameters.
940 static int is_obviously_nonneg(struct isl_tab *tab, int row)
944 unsigned off = 2 + tab->M;
947 if (isl_int_is_pos(tab->mat->row[row][2]))
949 if (isl_int_is_neg(tab->mat->row[row][2]))
953 if (isl_int_is_neg(tab->mat->row[row][1]))
955 for (i = 0; i < tab->n_param; ++i) {
956 /* Eliminated parameter */
957 if (tab->var[i].is_row)
959 col = tab->var[i].index;
960 if (isl_int_is_zero(tab->mat->row[row][off + col]))
962 if (!tab->var[i].is_nonneg)
964 if (isl_int_is_neg(tab->mat->row[row][off + col]))
967 for (i = 0; i < tab->n_div; ++i) {
968 if (tab->var[tab->n_var - tab->n_div + i].is_row)
970 col = tab->var[tab->n_var - tab->n_div + i].index;
971 if (isl_int_is_zero(tab->mat->row[row][off + col]))
973 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
975 if (isl_int_is_neg(tab->mat->row[row][off + col]))
981 /* Given a row r and two columns, return the column that would
982 * lead to the lexicographically smallest increment in the sample
983 * solution when leaving the basis in favor of the row.
984 * Pivoting with column c will increment the sample value by a non-negative
985 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
986 * corresponding to the non-parametric variables.
987 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
988 * with all other entries in this virtual row equal to zero.
989 * If variable v appears in a row, then a_{v,c} is the element in column c
992 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
993 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
994 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
995 * increment. Otherwise, it's c2.
997 static int lexmin_col_pair(struct isl_tab *tab,
998 int row, int col1, int col2, isl_int tmp)
1003 tr = tab->mat->row[row] + 2 + tab->M;
1005 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1009 if (!tab->var[i].is_row) {
1010 if (tab->var[i].index == col1)
1012 if (tab->var[i].index == col2)
1017 if (tab->var[i].index == row)
1020 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1021 s1 = isl_int_sgn(r[col1]);
1022 s2 = isl_int_sgn(r[col2]);
1023 if (s1 == 0 && s2 == 0)
1030 isl_int_mul(tmp, r[col2], tr[col1]);
1031 isl_int_submul(tmp, r[col1], tr[col2]);
1032 if (isl_int_is_pos(tmp))
1034 if (isl_int_is_neg(tmp))
1040 /* Given a row in the tableau, find and return the column that would
1041 * result in the lexicographically smallest, but positive, increment
1042 * in the sample point.
1043 * If there is no such column, then return tab->n_col.
1044 * If anything goes wrong, return -1.
1046 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1049 int col = tab->n_col;
1053 tr = tab->mat->row[row] + 2 + tab->M;
1057 for (j = tab->n_dead; j < tab->n_col; ++j) {
1058 if (tab->col_var[j] >= 0 &&
1059 (tab->col_var[j] < tab->n_param ||
1060 tab->col_var[j] >= tab->n_var - tab->n_div))
1063 if (!isl_int_is_pos(tr[j]))
1066 if (col == tab->n_col)
1069 col = lexmin_col_pair(tab, row, col, j, tmp);
1070 isl_assert(tab->mat->ctx, col >= 0, goto error);
1080 /* Return the first known violated constraint, i.e., a non-negative
1081 * constraint that currently has an either obviously negative value
1082 * or a previously determined to be negative value.
1084 * If any constraint has a negative coefficient for the big parameter,
1085 * if any, then we return one of these first.
1087 static int first_neg(struct isl_tab *tab)
1092 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1093 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1095 if (!isl_int_is_neg(tab->mat->row[row][2]))
1098 tab->row_sign[row] = isl_tab_row_neg;
1101 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1102 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1104 if (tab->row_sign) {
1105 if (tab->row_sign[row] == 0 &&
1106 is_obviously_neg(tab, row))
1107 tab->row_sign[row] = isl_tab_row_neg;
1108 if (tab->row_sign[row] != isl_tab_row_neg)
1110 } else if (!is_obviously_neg(tab, row))
1117 /* Check whether the invariant that all columns are lexico-positive
1118 * is satisfied. This function is not called from the current code
1119 * but is useful during debugging.
1121 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1122 static void check_lexpos(struct isl_tab *tab)
1124 unsigned off = 2 + tab->M;
1129 for (col = tab->n_dead; col < tab->n_col; ++col) {
1130 if (tab->col_var[col] >= 0 &&
1131 (tab->col_var[col] < tab->n_param ||
1132 tab->col_var[col] >= tab->n_var - tab->n_div))
1134 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1135 if (!tab->var[var].is_row) {
1136 if (tab->var[var].index == col)
1141 row = tab->var[var].index;
1142 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1144 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1146 fprintf(stderr, "lexneg column %d (row %d)\n",
1149 if (var >= tab->n_var - tab->n_div)
1150 fprintf(stderr, "zero column %d\n", col);
1154 /* Report to the caller that the given constraint is part of an encountered
1157 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1159 return tab->conflict(con, tab->conflict_user);
1162 /* Given a conflicting row in the tableau, report all constraints
1163 * involved in the row to the caller. That is, the row itself
1164 * (if represents a constraint) and all constraint columns with
1165 * non-zero (and therefore negative) coefficient.
1167 static int report_conflict(struct isl_tab *tab, int row)
1175 if (tab->row_var[row] < 0 &&
1176 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1179 tr = tab->mat->row[row] + 2 + tab->M;
1181 for (j = tab->n_dead; j < tab->n_col; ++j) {
1182 if (tab->col_var[j] >= 0 &&
1183 (tab->col_var[j] < tab->n_param ||
1184 tab->col_var[j] >= tab->n_var - tab->n_div))
1187 if (!isl_int_is_neg(tr[j]))
1190 if (tab->col_var[j] < 0 &&
1191 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1198 /* Resolve all known or obviously violated constraints through pivoting.
1199 * In particular, as long as we can find any violated constraint, we
1200 * look for a pivoting column that would result in the lexicographically
1201 * smallest increment in the sample point. If there is no such column
1202 * then the tableau is infeasible.
1204 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1205 static int restore_lexmin(struct isl_tab *tab)
1213 while ((row = first_neg(tab)) != -1) {
1214 col = lexmin_pivot_col(tab, row);
1215 if (col >= tab->n_col) {
1216 if (report_conflict(tab, row) < 0)
1218 if (isl_tab_mark_empty(tab) < 0)
1224 if (isl_tab_pivot(tab, row, col) < 0)
1230 /* Given a row that represents an equality, look for an appropriate
1232 * In particular, if there are any non-zero coefficients among
1233 * the non-parameter variables, then we take the last of these
1234 * variables. Eliminating this variable in terms of the other
1235 * variables and/or parameters does not influence the property
1236 * that all column in the initial tableau are lexicographically
1237 * positive. The row corresponding to the eliminated variable
1238 * will only have non-zero entries below the diagonal of the
1239 * initial tableau. That is, we transform
1245 * If there is no such non-parameter variable, then we are dealing with
1246 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1247 * for elimination. This will ensure that the eliminated parameter
1248 * always has an integer value whenever all the other parameters are integral.
1249 * If there is no such parameter then we return -1.
1251 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1253 unsigned off = 2 + tab->M;
1256 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1258 if (tab->var[i].is_row)
1260 col = tab->var[i].index;
1261 if (col <= tab->n_dead)
1263 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1266 for (i = tab->n_dead; i < tab->n_col; ++i) {
1267 if (isl_int_is_one(tab->mat->row[row][off + i]))
1269 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1275 /* Add an equality that is known to be valid to the tableau.
1276 * We first check if we can eliminate a variable or a parameter.
1277 * If not, we add the equality as two inequalities.
1278 * In this case, the equality was a pure parameter equality and there
1279 * is no need to resolve any constraint violations.
1281 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1288 r = isl_tab_add_row(tab, eq);
1292 r = tab->con[r].index;
1293 i = last_var_col_or_int_par_col(tab, r);
1295 tab->con[r].is_nonneg = 1;
1296 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1298 isl_seq_neg(eq, eq, 1 + tab->n_var);
1299 r = isl_tab_add_row(tab, eq);
1302 tab->con[r].is_nonneg = 1;
1303 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1306 if (isl_tab_pivot(tab, r, i) < 0)
1308 if (isl_tab_kill_col(tab, i) < 0)
1319 /* Check if the given row is a pure constant.
1321 static int is_constant(struct isl_tab *tab, int row)
1323 unsigned off = 2 + tab->M;
1325 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1326 tab->n_col - tab->n_dead) == -1;
1329 /* Add an equality that may or may not be valid to the tableau.
1330 * If the resulting row is a pure constant, then it must be zero.
1331 * Otherwise, the resulting tableau is empty.
1333 * If the row is not a pure constant, then we add two inequalities,
1334 * each time checking that they can be satisfied.
1335 * In the end we try to use one of the two constraints to eliminate
1338 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1339 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1343 struct isl_tab_undo *snap;
1347 snap = isl_tab_snap(tab);
1348 r1 = isl_tab_add_row(tab, eq);
1351 tab->con[r1].is_nonneg = 1;
1352 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1355 row = tab->con[r1].index;
1356 if (is_constant(tab, row)) {
1357 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1358 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1359 if (isl_tab_mark_empty(tab) < 0)
1363 if (isl_tab_rollback(tab, snap) < 0)
1368 if (restore_lexmin(tab) < 0)
1373 isl_seq_neg(eq, eq, 1 + tab->n_var);
1375 r2 = isl_tab_add_row(tab, eq);
1378 tab->con[r2].is_nonneg = 1;
1379 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1382 if (restore_lexmin(tab) < 0)
1387 if (!tab->con[r1].is_row) {
1388 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1390 } else if (!tab->con[r2].is_row) {
1391 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1396 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1397 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1399 isl_seq_neg(eq, eq, 1 + tab->n_var);
1400 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1401 isl_seq_neg(eq, eq, 1 + tab->n_var);
1402 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1411 /* Add an inequality to the tableau, resolving violations using
1414 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1421 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1422 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1427 r = isl_tab_add_row(tab, ineq);
1430 tab->con[r].is_nonneg = 1;
1431 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1433 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1434 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1439 if (restore_lexmin(tab) < 0)
1441 if (!tab->empty && tab->con[r].is_row &&
1442 isl_tab_row_is_redundant(tab, tab->con[r].index))
1443 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1451 /* Check if the coefficients of the parameters are all integral.
1453 static int integer_parameter(struct isl_tab *tab, int row)
1457 unsigned off = 2 + tab->M;
1459 for (i = 0; i < tab->n_param; ++i) {
1460 /* Eliminated parameter */
1461 if (tab->var[i].is_row)
1463 col = tab->var[i].index;
1464 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1465 tab->mat->row[row][0]))
1468 for (i = 0; i < tab->n_div; ++i) {
1469 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1471 col = tab->var[tab->n_var - tab->n_div + i].index;
1472 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1473 tab->mat->row[row][0]))
1479 /* Check if the coefficients of the non-parameter variables are all integral.
1481 static int integer_variable(struct isl_tab *tab, int row)
1484 unsigned off = 2 + tab->M;
1486 for (i = tab->n_dead; i < tab->n_col; ++i) {
1487 if (tab->col_var[i] >= 0 &&
1488 (tab->col_var[i] < tab->n_param ||
1489 tab->col_var[i] >= tab->n_var - tab->n_div))
1491 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1492 tab->mat->row[row][0]))
1498 /* Check if the constant term is integral.
1500 static int integer_constant(struct isl_tab *tab, int row)
1502 return isl_int_is_divisible_by(tab->mat->row[row][1],
1503 tab->mat->row[row][0]);
1506 #define I_CST 1 << 0
1507 #define I_PAR 1 << 1
1508 #define I_VAR 1 << 2
1510 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1511 * that is non-integer and therefore requires a cut and return
1512 * the index of the variable.
1513 * For parametric tableaus, there are three parts in a row,
1514 * the constant, the coefficients of the parameters and the rest.
1515 * For each part, we check whether the coefficients in that part
1516 * are all integral and if so, set the corresponding flag in *f.
1517 * If the constant and the parameter part are integral, then the
1518 * current sample value is integral and no cut is required
1519 * (irrespective of whether the variable part is integral).
1521 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1523 var = var < 0 ? tab->n_param : var + 1;
1525 for (; var < tab->n_var - tab->n_div; ++var) {
1528 if (!tab->var[var].is_row)
1530 row = tab->var[var].index;
1531 if (integer_constant(tab, row))
1532 ISL_FL_SET(flags, I_CST);
1533 if (integer_parameter(tab, row))
1534 ISL_FL_SET(flags, I_PAR);
1535 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1537 if (integer_variable(tab, row))
1538 ISL_FL_SET(flags, I_VAR);
1545 /* Check for first (non-parameter) variable that is non-integer and
1546 * therefore requires a cut and return the corresponding row.
1547 * For parametric tableaus, there are three parts in a row,
1548 * the constant, the coefficients of the parameters and the rest.
1549 * For each part, we check whether the coefficients in that part
1550 * are all integral and if so, set the corresponding flag in *f.
1551 * If the constant and the parameter part are integral, then the
1552 * current sample value is integral and no cut is required
1553 * (irrespective of whether the variable part is integral).
1555 static int first_non_integer_row(struct isl_tab *tab, int *f)
1557 int var = next_non_integer_var(tab, -1, f);
1559 return var < 0 ? -1 : tab->var[var].index;
1562 /* Add a (non-parametric) cut to cut away the non-integral sample
1563 * value of the given row.
1565 * If the row is given by
1567 * m r = f + \sum_i a_i y_i
1571 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1573 * The big parameter, if any, is ignored, since it is assumed to be big
1574 * enough to be divisible by any integer.
1575 * If the tableau is actually a parametric tableau, then this function
1576 * is only called when all coefficients of the parameters are integral.
1577 * The cut therefore has zero coefficients for the parameters.
1579 * The current value is known to be negative, so row_sign, if it
1580 * exists, is set accordingly.
1582 * Return the row of the cut or -1.
1584 static int add_cut(struct isl_tab *tab, int row)
1589 unsigned off = 2 + tab->M;
1591 if (isl_tab_extend_cons(tab, 1) < 0)
1593 r = isl_tab_allocate_con(tab);
1597 r_row = tab->mat->row[tab->con[r].index];
1598 isl_int_set(r_row[0], tab->mat->row[row][0]);
1599 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1600 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1601 isl_int_neg(r_row[1], r_row[1]);
1603 isl_int_set_si(r_row[2], 0);
1604 for (i = 0; i < tab->n_col; ++i)
1605 isl_int_fdiv_r(r_row[off + i],
1606 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1608 tab->con[r].is_nonneg = 1;
1609 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1612 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1614 return tab->con[r].index;
1617 /* Given a non-parametric tableau, add cuts until an integer
1618 * sample point is obtained or until the tableau is determined
1619 * to be integer infeasible.
1620 * As long as there is any non-integer value in the sample point,
1621 * we add appropriate cuts, if possible, for each of these
1622 * non-integer values and then resolve the violated
1623 * cut constraints using restore_lexmin.
1624 * If one of the corresponding rows is equal to an integral
1625 * combination of variables/constraints plus a non-integral constant,
1626 * then there is no way to obtain an integer point and we return
1627 * a tableau that is marked empty.
1629 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
1640 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1642 if (ISL_FL_ISSET(flags, I_VAR)) {
1643 if (isl_tab_mark_empty(tab) < 0)
1647 row = tab->var[var].index;
1648 row = add_cut(tab, row);
1651 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1652 if (restore_lexmin(tab) < 0)
1663 /* Check whether all the currently active samples also satisfy the inequality
1664 * "ineq" (treated as an equality if eq is set).
1665 * Remove those samples that do not.
1667 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1675 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1676 isl_assert(tab->mat->ctx, tab->samples, goto error);
1677 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1680 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1682 isl_seq_inner_product(ineq, tab->samples->row[i],
1683 1 + tab->n_var, &v);
1684 sgn = isl_int_sgn(v);
1685 if (eq ? (sgn == 0) : (sgn >= 0))
1687 tab = isl_tab_drop_sample(tab, i);
1699 /* Check whether the sample value of the tableau is finite,
1700 * i.e., either the tableau does not use a big parameter, or
1701 * all values of the variables are equal to the big parameter plus
1702 * some constant. This constant is the actual sample value.
1704 static int sample_is_finite(struct isl_tab *tab)
1711 for (i = 0; i < tab->n_var; ++i) {
1713 if (!tab->var[i].is_row)
1715 row = tab->var[i].index;
1716 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1722 /* Check if the context tableau of sol has any integer points.
1723 * Leave tab in empty state if no integer point can be found.
1724 * If an integer point can be found and if moreover it is finite,
1725 * then it is added to the list of sample values.
1727 * This function is only called when none of the currently active sample
1728 * values satisfies the most recently added constraint.
1730 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1732 struct isl_tab_undo *snap;
1737 snap = isl_tab_snap(tab);
1738 if (isl_tab_push_basis(tab) < 0)
1741 tab = cut_to_integer_lexmin(tab);
1745 if (!tab->empty && sample_is_finite(tab)) {
1746 struct isl_vec *sample;
1748 sample = isl_tab_get_sample_value(tab);
1750 tab = isl_tab_add_sample(tab, sample);
1753 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1762 /* Check if any of the currently active sample values satisfies
1763 * the inequality "ineq" (an equality if eq is set).
1765 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1773 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1774 isl_assert(tab->mat->ctx, tab->samples, return -1);
1775 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1778 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1780 isl_seq_inner_product(ineq, tab->samples->row[i],
1781 1 + tab->n_var, &v);
1782 sgn = isl_int_sgn(v);
1783 if (eq ? (sgn == 0) : (sgn >= 0))
1788 return i < tab->n_sample;
1791 /* Add a div specified by "div" to the tableau "tab" and return
1792 * 1 if the div is obviously non-negative.
1794 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1795 int (*add_ineq)(void *user, isl_int *), void *user)
1799 struct isl_mat *samples;
1802 r = isl_tab_add_div(tab, div, add_ineq, user);
1805 nonneg = tab->var[r].is_nonneg;
1806 tab->var[r].frozen = 1;
1808 samples = isl_mat_extend(tab->samples,
1809 tab->n_sample, 1 + tab->n_var);
1810 tab->samples = samples;
1813 for (i = tab->n_outside; i < samples->n_row; ++i) {
1814 isl_seq_inner_product(div->el + 1, samples->row[i],
1815 div->size - 1, &samples->row[i][samples->n_col - 1]);
1816 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1817 samples->row[i][samples->n_col - 1], div->el[0]);
1823 /* Add a div specified by "div" to both the main tableau and
1824 * the context tableau. In case of the main tableau, we only
1825 * need to add an extra div. In the context tableau, we also
1826 * need to express the meaning of the div.
1827 * Return the index of the div or -1 if anything went wrong.
1829 static int add_div(struct isl_tab *tab, struct isl_context *context,
1830 struct isl_vec *div)
1835 if ((nonneg = context->op->add_div(context, div)) < 0)
1838 if (!context->op->is_ok(context))
1841 if (isl_tab_extend_vars(tab, 1) < 0)
1843 r = isl_tab_allocate_var(tab);
1847 tab->var[r].is_nonneg = 1;
1848 tab->var[r].frozen = 1;
1851 return tab->n_div - 1;
1853 context->op->invalidate(context);
1857 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1860 unsigned total = isl_basic_map_total_dim(tab->bmap);
1862 for (i = 0; i < tab->bmap->n_div; ++i) {
1863 if (isl_int_ne(tab->bmap->div[i][0], denom))
1865 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1872 /* Return the index of a div that corresponds to "div".
1873 * We first check if we already have such a div and if not, we create one.
1875 static int get_div(struct isl_tab *tab, struct isl_context *context,
1876 struct isl_vec *div)
1879 struct isl_tab *context_tab = context->op->peek_tab(context);
1884 d = find_div(context_tab, div->el + 1, div->el[0]);
1888 return add_div(tab, context, div);
1891 /* Add a parametric cut to cut away the non-integral sample value
1893 * Let a_i be the coefficients of the constant term and the parameters
1894 * and let b_i be the coefficients of the variables or constraints
1895 * in basis of the tableau.
1896 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1898 * The cut is expressed as
1900 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1902 * If q did not already exist in the context tableau, then it is added first.
1903 * If q is in a column of the main tableau then the "+ q" can be accomplished
1904 * by setting the corresponding entry to the denominator of the constraint.
1905 * If q happens to be in a row of the main tableau, then the corresponding
1906 * row needs to be added instead (taking care of the denominators).
1907 * Note that this is very unlikely, but perhaps not entirely impossible.
1909 * The current value of the cut is known to be negative (or at least
1910 * non-positive), so row_sign is set accordingly.
1912 * Return the row of the cut or -1.
1914 static int add_parametric_cut(struct isl_tab *tab, int row,
1915 struct isl_context *context)
1917 struct isl_vec *div;
1924 unsigned off = 2 + tab->M;
1929 div = get_row_parameter_div(tab, row);
1934 d = context->op->get_div(context, tab, div);
1938 if (isl_tab_extend_cons(tab, 1) < 0)
1940 r = isl_tab_allocate_con(tab);
1944 r_row = tab->mat->row[tab->con[r].index];
1945 isl_int_set(r_row[0], tab->mat->row[row][0]);
1946 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1947 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1948 isl_int_neg(r_row[1], r_row[1]);
1950 isl_int_set_si(r_row[2], 0);
1951 for (i = 0; i < tab->n_param; ++i) {
1952 if (tab->var[i].is_row)
1954 col = tab->var[i].index;
1955 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1956 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1957 tab->mat->row[row][0]);
1958 isl_int_neg(r_row[off + col], r_row[off + col]);
1960 for (i = 0; i < tab->n_div; ++i) {
1961 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1963 col = tab->var[tab->n_var - tab->n_div + i].index;
1964 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1965 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1966 tab->mat->row[row][0]);
1967 isl_int_neg(r_row[off + col], r_row[off + col]);
1969 for (i = 0; i < tab->n_col; ++i) {
1970 if (tab->col_var[i] >= 0 &&
1971 (tab->col_var[i] < tab->n_param ||
1972 tab->col_var[i] >= tab->n_var - tab->n_div))
1974 isl_int_fdiv_r(r_row[off + i],
1975 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1977 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1979 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1981 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1982 isl_int_divexact(r_row[0], r_row[0], gcd);
1983 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1984 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1985 r_row[0], tab->mat->row[d_row] + 1,
1986 off - 1 + tab->n_col);
1987 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1990 col = tab->var[tab->n_var - tab->n_div + d].index;
1991 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1994 tab->con[r].is_nonneg = 1;
1995 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1998 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2002 row = tab->con[r].index;
2004 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2010 /* Construct a tableau for bmap that can be used for computing
2011 * the lexicographic minimum (or maximum) of bmap.
2012 * If not NULL, then dom is the domain where the minimum
2013 * should be computed. In this case, we set up a parametric
2014 * tableau with row signs (initialized to "unknown").
2015 * If M is set, then the tableau will use a big parameter.
2016 * If max is set, then a maximum should be computed instead of a minimum.
2017 * This means that for each variable x, the tableau will contain the variable
2018 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2019 * of the variables in all constraints are negated prior to adding them
2022 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2023 struct isl_basic_set *dom, unsigned M, int max)
2026 struct isl_tab *tab;
2028 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2029 isl_basic_map_total_dim(bmap), M);
2033 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2035 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2036 tab->n_div = dom->n_div;
2037 tab->row_sign = isl_calloc_array(bmap->ctx,
2038 enum isl_tab_row_sign, tab->mat->n_row);
2042 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2043 if (isl_tab_mark_empty(tab) < 0)
2048 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2049 tab->var[i].is_nonneg = 1;
2050 tab->var[i].frozen = 1;
2052 for (i = 0; i < bmap->n_eq; ++i) {
2054 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2055 bmap->eq[i] + 1 + tab->n_param,
2056 tab->n_var - tab->n_param - tab->n_div);
2057 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2059 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2060 bmap->eq[i] + 1 + tab->n_param,
2061 tab->n_var - tab->n_param - tab->n_div);
2062 if (!tab || tab->empty)
2065 if (bmap->n_eq && restore_lexmin(tab) < 0)
2067 for (i = 0; i < bmap->n_ineq; ++i) {
2069 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2070 bmap->ineq[i] + 1 + tab->n_param,
2071 tab->n_var - tab->n_param - tab->n_div);
2072 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2074 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2075 bmap->ineq[i] + 1 + tab->n_param,
2076 tab->n_var - tab->n_param - tab->n_div);
2077 if (!tab || tab->empty)
2086 /* Given a main tableau where more than one row requires a split,
2087 * determine and return the "best" row to split on.
2089 * Given two rows in the main tableau, if the inequality corresponding
2090 * to the first row is redundant with respect to that of the second row
2091 * in the current tableau, then it is better to split on the second row,
2092 * since in the positive part, both row will be positive.
2093 * (In the negative part a pivot will have to be performed and just about
2094 * anything can happen to the sign of the other row.)
2096 * As a simple heuristic, we therefore select the row that makes the most
2097 * of the other rows redundant.
2099 * Perhaps it would also be useful to look at the number of constraints
2100 * that conflict with any given constraint.
2102 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2104 struct isl_tab_undo *snap;
2110 if (isl_tab_extend_cons(context_tab, 2) < 0)
2113 snap = isl_tab_snap(context_tab);
2115 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2116 struct isl_tab_undo *snap2;
2117 struct isl_vec *ineq = NULL;
2121 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2123 if (tab->row_sign[split] != isl_tab_row_any)
2126 ineq = get_row_parameter_ineq(tab, split);
2129 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2134 snap2 = isl_tab_snap(context_tab);
2136 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2137 struct isl_tab_var *var;
2141 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2143 if (tab->row_sign[row] != isl_tab_row_any)
2146 ineq = get_row_parameter_ineq(tab, row);
2149 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2153 var = &context_tab->con[context_tab->n_con - 1];
2154 if (!context_tab->empty &&
2155 !isl_tab_min_at_most_neg_one(context_tab, var))
2157 if (isl_tab_rollback(context_tab, snap2) < 0)
2160 if (best == -1 || r > best_r) {
2164 if (isl_tab_rollback(context_tab, snap) < 0)
2171 static struct isl_basic_set *context_lex_peek_basic_set(
2172 struct isl_context *context)
2174 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2177 return isl_tab_peek_bset(clex->tab);
2180 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2182 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2186 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2187 int check, int update)
2189 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2190 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2192 if (add_lexmin_eq(clex->tab, eq) < 0)
2195 int v = tab_has_valid_sample(clex->tab, eq, 1);
2199 clex->tab = check_integer_feasible(clex->tab);
2202 clex->tab = check_samples(clex->tab, eq, 1);
2205 isl_tab_free(clex->tab);
2209 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2210 int check, int update)
2212 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2213 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2215 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2217 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2221 clex->tab = check_integer_feasible(clex->tab);
2224 clex->tab = check_samples(clex->tab, ineq, 0);
2227 isl_tab_free(clex->tab);
2231 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2233 struct isl_context *context = (struct isl_context *)user;
2234 context_lex_add_ineq(context, ineq, 0, 0);
2235 return context->op->is_ok(context) ? 0 : -1;
2238 /* Check which signs can be obtained by "ineq" on all the currently
2239 * active sample values. See row_sign for more information.
2241 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2247 enum isl_tab_row_sign res = isl_tab_row_unknown;
2249 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2250 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2251 return isl_tab_row_unknown);
2254 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2255 isl_seq_inner_product(tab->samples->row[i], ineq,
2256 1 + tab->n_var, &tmp);
2257 sgn = isl_int_sgn(tmp);
2258 if (sgn > 0 || (sgn == 0 && strict)) {
2259 if (res == isl_tab_row_unknown)
2260 res = isl_tab_row_pos;
2261 if (res == isl_tab_row_neg)
2262 res = isl_tab_row_any;
2265 if (res == isl_tab_row_unknown)
2266 res = isl_tab_row_neg;
2267 if (res == isl_tab_row_pos)
2268 res = isl_tab_row_any;
2270 if (res == isl_tab_row_any)
2278 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2279 isl_int *ineq, int strict)
2281 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2282 return tab_ineq_sign(clex->tab, ineq, strict);
2285 /* Check whether "ineq" can be added to the tableau without rendering
2288 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2290 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2291 struct isl_tab_undo *snap;
2297 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2300 snap = isl_tab_snap(clex->tab);
2301 if (isl_tab_push_basis(clex->tab) < 0)
2303 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2304 clex->tab = check_integer_feasible(clex->tab);
2307 feasible = !clex->tab->empty;
2308 if (isl_tab_rollback(clex->tab, snap) < 0)
2314 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2315 struct isl_vec *div)
2317 return get_div(tab, context, div);
2320 /* Add a div specified by "div" to the context tableau and return
2321 * 1 if the div is obviously non-negative.
2322 * context_tab_add_div will always return 1, because all variables
2323 * in a isl_context_lex tableau are non-negative.
2324 * However, if we are using a big parameter in the context, then this only
2325 * reflects the non-negativity of the variable used to _encode_ the
2326 * div, i.e., div' = M + div, so we can't draw any conclusions.
2328 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2330 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2332 nonneg = context_tab_add_div(clex->tab, div,
2333 context_lex_add_ineq_wrap, context);
2341 static int context_lex_detect_equalities(struct isl_context *context,
2342 struct isl_tab *tab)
2347 static int context_lex_best_split(struct isl_context *context,
2348 struct isl_tab *tab)
2350 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2351 struct isl_tab_undo *snap;
2354 snap = isl_tab_snap(clex->tab);
2355 if (isl_tab_push_basis(clex->tab) < 0)
2357 r = best_split(tab, clex->tab);
2359 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2365 static int context_lex_is_empty(struct isl_context *context)
2367 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2370 return clex->tab->empty;
2373 static void *context_lex_save(struct isl_context *context)
2375 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2376 struct isl_tab_undo *snap;
2378 snap = isl_tab_snap(clex->tab);
2379 if (isl_tab_push_basis(clex->tab) < 0)
2381 if (isl_tab_save_samples(clex->tab) < 0)
2387 static void context_lex_restore(struct isl_context *context, void *save)
2389 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2390 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2391 isl_tab_free(clex->tab);
2396 static int context_lex_is_ok(struct isl_context *context)
2398 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2402 /* For each variable in the context tableau, check if the variable can
2403 * only attain non-negative values. If so, mark the parameter as non-negative
2404 * in the main tableau. This allows for a more direct identification of some
2405 * cases of violated constraints.
2407 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2408 struct isl_tab *context_tab)
2411 struct isl_tab_undo *snap;
2412 struct isl_vec *ineq = NULL;
2413 struct isl_tab_var *var;
2416 if (context_tab->n_var == 0)
2419 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2423 if (isl_tab_extend_cons(context_tab, 1) < 0)
2426 snap = isl_tab_snap(context_tab);
2429 isl_seq_clr(ineq->el, ineq->size);
2430 for (i = 0; i < context_tab->n_var; ++i) {
2431 isl_int_set_si(ineq->el[1 + i], 1);
2432 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2434 var = &context_tab->con[context_tab->n_con - 1];
2435 if (!context_tab->empty &&
2436 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2438 if (i >= tab->n_param)
2439 j = i - tab->n_param + tab->n_var - tab->n_div;
2440 tab->var[j].is_nonneg = 1;
2443 isl_int_set_si(ineq->el[1 + i], 0);
2444 if (isl_tab_rollback(context_tab, snap) < 0)
2448 if (context_tab->M && n == context_tab->n_var) {
2449 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2461 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2462 struct isl_context *context, struct isl_tab *tab)
2464 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2465 struct isl_tab_undo *snap;
2470 snap = isl_tab_snap(clex->tab);
2471 if (isl_tab_push_basis(clex->tab) < 0)
2474 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2476 if (isl_tab_rollback(clex->tab, snap) < 0)
2485 static void context_lex_invalidate(struct isl_context *context)
2487 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2488 isl_tab_free(clex->tab);
2492 static void context_lex_free(struct isl_context *context)
2494 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2495 isl_tab_free(clex->tab);
2499 struct isl_context_op isl_context_lex_op = {
2500 context_lex_detect_nonnegative_parameters,
2501 context_lex_peek_basic_set,
2502 context_lex_peek_tab,
2504 context_lex_add_ineq,
2505 context_lex_ineq_sign,
2506 context_lex_test_ineq,
2507 context_lex_get_div,
2508 context_lex_add_div,
2509 context_lex_detect_equalities,
2510 context_lex_best_split,
2511 context_lex_is_empty,
2514 context_lex_restore,
2515 context_lex_invalidate,
2519 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2521 struct isl_tab *tab;
2523 bset = isl_basic_set_cow(bset);
2526 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2529 if (isl_tab_track_bset(tab, bset) < 0)
2531 tab = isl_tab_init_samples(tab);
2534 isl_basic_set_free(bset);
2538 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2540 struct isl_context_lex *clex;
2545 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2549 clex->context.op = &isl_context_lex_op;
2551 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2552 if (restore_lexmin(clex->tab) < 0)
2554 clex->tab = check_integer_feasible(clex->tab);
2558 return &clex->context;
2560 clex->context.op->free(&clex->context);
2564 struct isl_context_gbr {
2565 struct isl_context context;
2566 struct isl_tab *tab;
2567 struct isl_tab *shifted;
2568 struct isl_tab *cone;
2571 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2572 struct isl_context *context, struct isl_tab *tab)
2574 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2577 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2580 static struct isl_basic_set *context_gbr_peek_basic_set(
2581 struct isl_context *context)
2583 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2586 return isl_tab_peek_bset(cgbr->tab);
2589 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2591 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2595 /* Initialize the "shifted" tableau of the context, which
2596 * contains the constraints of the original tableau shifted
2597 * by the sum of all negative coefficients. This ensures
2598 * that any rational point in the shifted tableau can
2599 * be rounded up to yield an integer point in the original tableau.
2601 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2604 struct isl_vec *cst;
2605 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2606 unsigned dim = isl_basic_set_total_dim(bset);
2608 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2612 for (i = 0; i < bset->n_ineq; ++i) {
2613 isl_int_set(cst->el[i], bset->ineq[i][0]);
2614 for (j = 0; j < dim; ++j) {
2615 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2617 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2618 bset->ineq[i][1 + j]);
2622 cgbr->shifted = isl_tab_from_basic_set(bset);
2624 for (i = 0; i < bset->n_ineq; ++i)
2625 isl_int_set(bset->ineq[i][0], cst->el[i]);
2630 /* Check if the shifted tableau is non-empty, and if so
2631 * use the sample point to construct an integer point
2632 * of the context tableau.
2634 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2636 struct isl_vec *sample;
2639 gbr_init_shifted(cgbr);
2642 if (cgbr->shifted->empty)
2643 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2645 sample = isl_tab_get_sample_value(cgbr->shifted);
2646 sample = isl_vec_ceil(sample);
2651 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2658 for (i = 0; i < bset->n_eq; ++i)
2659 isl_int_set_si(bset->eq[i][0], 0);
2661 for (i = 0; i < bset->n_ineq; ++i)
2662 isl_int_set_si(bset->ineq[i][0], 0);
2667 static int use_shifted(struct isl_context_gbr *cgbr)
2669 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2672 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2674 struct isl_basic_set *bset;
2675 struct isl_basic_set *cone;
2677 if (isl_tab_sample_is_integer(cgbr->tab))
2678 return isl_tab_get_sample_value(cgbr->tab);
2680 if (use_shifted(cgbr)) {
2681 struct isl_vec *sample;
2683 sample = gbr_get_shifted_sample(cgbr);
2684 if (!sample || sample->size > 0)
2687 isl_vec_free(sample);
2691 bset = isl_tab_peek_bset(cgbr->tab);
2692 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2695 if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
2698 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2701 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2702 struct isl_vec *sample;
2703 struct isl_tab_undo *snap;
2705 if (cgbr->tab->basis) {
2706 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2707 isl_mat_free(cgbr->tab->basis);
2708 cgbr->tab->basis = NULL;
2710 cgbr->tab->n_zero = 0;
2711 cgbr->tab->n_unbounded = 0;
2714 snap = isl_tab_snap(cgbr->tab);
2716 sample = isl_tab_sample(cgbr->tab);
2718 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2719 isl_vec_free(sample);
2726 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2727 cone = drop_constant_terms(cone);
2728 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2729 cone = isl_basic_set_underlying_set(cone);
2730 cone = isl_basic_set_gauss(cone, NULL);
2732 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2733 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2734 bset = isl_basic_set_underlying_set(bset);
2735 bset = isl_basic_set_gauss(bset, NULL);
2737 return isl_basic_set_sample_with_cone(bset, cone);
2740 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2742 struct isl_vec *sample;
2747 if (cgbr->tab->empty)
2750 sample = gbr_get_sample(cgbr);
2754 if (sample->size == 0) {
2755 isl_vec_free(sample);
2756 if (isl_tab_mark_empty(cgbr->tab) < 0)
2761 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2765 isl_tab_free(cgbr->tab);
2769 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2774 if (isl_tab_extend_cons(tab, 2) < 0)
2777 if (isl_tab_add_eq(tab, eq) < 0)
2786 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2787 int check, int update)
2789 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2791 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2793 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2794 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2796 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2801 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2805 check_gbr_integer_feasible(cgbr);
2808 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2811 isl_tab_free(cgbr->tab);
2815 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2820 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2823 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2826 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2829 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2831 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2834 for (i = 0; i < dim; ++i) {
2835 if (!isl_int_is_neg(ineq[1 + i]))
2837 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2840 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2843 for (i = 0; i < dim; ++i) {
2844 if (!isl_int_is_neg(ineq[1 + i]))
2846 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2850 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2851 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2853 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2859 isl_tab_free(cgbr->tab);
2863 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2864 int check, int update)
2866 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2868 add_gbr_ineq(cgbr, ineq);
2873 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2877 check_gbr_integer_feasible(cgbr);
2880 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2883 isl_tab_free(cgbr->tab);
2887 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2889 struct isl_context *context = (struct isl_context *)user;
2890 context_gbr_add_ineq(context, ineq, 0, 0);
2891 return context->op->is_ok(context) ? 0 : -1;
2894 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2895 isl_int *ineq, int strict)
2897 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2898 return tab_ineq_sign(cgbr->tab, ineq, strict);
2901 /* Check whether "ineq" can be added to the tableau without rendering
2904 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2906 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2907 struct isl_tab_undo *snap;
2908 struct isl_tab_undo *shifted_snap = NULL;
2909 struct isl_tab_undo *cone_snap = NULL;
2915 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2918 snap = isl_tab_snap(cgbr->tab);
2920 shifted_snap = isl_tab_snap(cgbr->shifted);
2922 cone_snap = isl_tab_snap(cgbr->cone);
2923 add_gbr_ineq(cgbr, ineq);
2924 check_gbr_integer_feasible(cgbr);
2927 feasible = !cgbr->tab->empty;
2928 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2931 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2933 } else if (cgbr->shifted) {
2934 isl_tab_free(cgbr->shifted);
2935 cgbr->shifted = NULL;
2938 if (isl_tab_rollback(cgbr->cone, cone_snap))
2940 } else if (cgbr->cone) {
2941 isl_tab_free(cgbr->cone);
2948 /* Return the column of the last of the variables associated to
2949 * a column that has a non-zero coefficient.
2950 * This function is called in a context where only coefficients
2951 * of parameters or divs can be non-zero.
2953 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
2958 if (tab->n_var == 0)
2961 for (i = tab->n_var - 1; i >= 0; --i) {
2962 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
2964 if (tab->var[i].is_row)
2966 col = tab->var[i].index;
2967 if (!isl_int_is_zero(p[col]))
2974 /* Look through all the recently added equalities in the context
2975 * to see if we can propagate any of them to the main tableau.
2977 * The newly added equalities in the context are encoded as pairs
2978 * of inequalities starting at inequality "first".
2980 * We tentatively add each of these equalities to the main tableau
2981 * and if this happens to result in a row with a final coefficient
2982 * that is one or negative one, we use it to kill a column
2983 * in the main tableau. Otherwise, we discard the tentatively
2986 static void propagate_equalities(struct isl_context_gbr *cgbr,
2987 struct isl_tab *tab, unsigned first)
2990 struct isl_vec *eq = NULL;
2992 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2996 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
2999 isl_seq_clr(eq->el + 1 + tab->n_param,
3000 tab->n_var - tab->n_param - tab->n_div);
3001 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3004 struct isl_tab_undo *snap;
3005 snap = isl_tab_snap(tab);
3007 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3008 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3009 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3012 r = isl_tab_add_row(tab, eq->el);
3015 r = tab->con[r].index;
3016 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3017 if (j < 0 || j < tab->n_dead ||
3018 !isl_int_is_one(tab->mat->row[r][0]) ||
3019 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3020 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3021 if (isl_tab_rollback(tab, snap) < 0)
3025 if (isl_tab_pivot(tab, r, j) < 0)
3027 if (isl_tab_kill_col(tab, j) < 0)
3030 if (restore_lexmin(tab) < 0)
3039 isl_tab_free(cgbr->tab);
3043 static int context_gbr_detect_equalities(struct isl_context *context,
3044 struct isl_tab *tab)
3046 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3047 struct isl_ctx *ctx;
3050 ctx = cgbr->tab->mat->ctx;
3053 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3054 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3057 if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
3060 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3063 n_ineq = cgbr->tab->bmap->n_ineq;
3064 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3065 if (cgbr->tab && cgbr->tab->bmap->n_ineq > n_ineq)
3066 propagate_equalities(cgbr, tab, n_ineq);
3070 isl_tab_free(cgbr->tab);
3075 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3076 struct isl_vec *div)
3078 return get_div(tab, context, div);
3081 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3083 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3087 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3089 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3091 if (isl_tab_allocate_var(cgbr->cone) <0)
3094 cgbr->cone->bmap = isl_basic_map_extend_dim(cgbr->cone->bmap,
3095 isl_basic_map_get_dim(cgbr->cone->bmap), 1, 0, 2);
3096 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3099 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3100 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3103 return context_tab_add_div(cgbr->tab, div,
3104 context_gbr_add_ineq_wrap, context);
3107 static int context_gbr_best_split(struct isl_context *context,
3108 struct isl_tab *tab)
3110 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3111 struct isl_tab_undo *snap;
3114 snap = isl_tab_snap(cgbr->tab);
3115 r = best_split(tab, cgbr->tab);
3117 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3123 static int context_gbr_is_empty(struct isl_context *context)
3125 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3128 return cgbr->tab->empty;
3131 struct isl_gbr_tab_undo {
3132 struct isl_tab_undo *tab_snap;
3133 struct isl_tab_undo *shifted_snap;
3134 struct isl_tab_undo *cone_snap;
3137 static void *context_gbr_save(struct isl_context *context)
3139 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3140 struct isl_gbr_tab_undo *snap;
3142 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3146 snap->tab_snap = isl_tab_snap(cgbr->tab);
3147 if (isl_tab_save_samples(cgbr->tab) < 0)
3151 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3153 snap->shifted_snap = NULL;
3156 snap->cone_snap = isl_tab_snap(cgbr->cone);
3158 snap->cone_snap = NULL;
3166 static void context_gbr_restore(struct isl_context *context, void *save)
3168 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3169 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3172 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3173 isl_tab_free(cgbr->tab);
3177 if (snap->shifted_snap) {
3178 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3180 } else if (cgbr->shifted) {
3181 isl_tab_free(cgbr->shifted);
3182 cgbr->shifted = NULL;
3185 if (snap->cone_snap) {
3186 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3188 } else if (cgbr->cone) {
3189 isl_tab_free(cgbr->cone);
3198 isl_tab_free(cgbr->tab);
3202 static int context_gbr_is_ok(struct isl_context *context)
3204 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3208 static void context_gbr_invalidate(struct isl_context *context)
3210 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3211 isl_tab_free(cgbr->tab);
3215 static void context_gbr_free(struct isl_context *context)
3217 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3218 isl_tab_free(cgbr->tab);
3219 isl_tab_free(cgbr->shifted);
3220 isl_tab_free(cgbr->cone);
3224 struct isl_context_op isl_context_gbr_op = {
3225 context_gbr_detect_nonnegative_parameters,
3226 context_gbr_peek_basic_set,
3227 context_gbr_peek_tab,
3229 context_gbr_add_ineq,
3230 context_gbr_ineq_sign,
3231 context_gbr_test_ineq,
3232 context_gbr_get_div,
3233 context_gbr_add_div,
3234 context_gbr_detect_equalities,
3235 context_gbr_best_split,
3236 context_gbr_is_empty,
3239 context_gbr_restore,
3240 context_gbr_invalidate,
3244 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3246 struct isl_context_gbr *cgbr;
3251 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3255 cgbr->context.op = &isl_context_gbr_op;
3257 cgbr->shifted = NULL;
3259 cgbr->tab = isl_tab_from_basic_set(dom);
3260 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3263 if (isl_tab_track_bset(cgbr->tab,
3264 isl_basic_set_cow(isl_basic_set_copy(dom))) < 0)
3266 check_gbr_integer_feasible(cgbr);
3268 return &cgbr->context;
3270 cgbr->context.op->free(&cgbr->context);
3274 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3279 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3280 return isl_context_lex_alloc(dom);
3282 return isl_context_gbr_alloc(dom);
3285 /* Construct an isl_sol_map structure for accumulating the solution.
3286 * If track_empty is set, then we also keep track of the parts
3287 * of the context where there is no solution.
3288 * If max is set, then we are solving a maximization, rather than
3289 * a minimization problem, which means that the variables in the
3290 * tableau have value "M - x" rather than "M + x".
3292 static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
3293 struct isl_basic_set *dom, int track_empty, int max)
3295 struct isl_sol_map *sol_map = NULL;
3300 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3304 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3305 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3306 sol_map->sol.dec_level.sol = &sol_map->sol;
3307 sol_map->sol.max = max;
3308 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3309 sol_map->sol.add = &sol_map_add_wrap;
3310 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3311 sol_map->sol.free = &sol_map_free_wrap;
3312 sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
3317 sol_map->sol.context = isl_context_alloc(dom);
3318 if (!sol_map->sol.context)
3322 sol_map->empty = isl_set_alloc_dim(isl_basic_set_get_dim(dom),
3323 1, ISL_SET_DISJOINT);
3324 if (!sol_map->empty)
3328 isl_basic_set_free(dom);
3331 isl_basic_set_free(dom);
3332 sol_map_free(sol_map);
3336 /* Check whether all coefficients of (non-parameter) variables
3337 * are non-positive, meaning that no pivots can be performed on the row.
3339 static int is_critical(struct isl_tab *tab, int row)
3342 unsigned off = 2 + tab->M;
3344 for (j = tab->n_dead; j < tab->n_col; ++j) {
3345 if (tab->col_var[j] >= 0 &&
3346 (tab->col_var[j] < tab->n_param ||
3347 tab->col_var[j] >= tab->n_var - tab->n_div))
3350 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3357 /* Check whether the inequality represented by vec is strict over the integers,
3358 * i.e., there are no integer values satisfying the constraint with
3359 * equality. This happens if the gcd of the coefficients is not a divisor
3360 * of the constant term. If so, scale the constraint down by the gcd
3361 * of the coefficients.
3363 static int is_strict(struct isl_vec *vec)
3369 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3370 if (!isl_int_is_one(gcd)) {
3371 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3372 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3373 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3380 /* Determine the sign of the given row of the main tableau.
3381 * The result is one of
3382 * isl_tab_row_pos: always non-negative; no pivot needed
3383 * isl_tab_row_neg: always non-positive; pivot
3384 * isl_tab_row_any: can be both positive and negative; split
3386 * We first handle some simple cases
3387 * - the row sign may be known already
3388 * - the row may be obviously non-negative
3389 * - the parametric constant may be equal to that of another row
3390 * for which we know the sign. This sign will be either "pos" or
3391 * "any". If it had been "neg" then we would have pivoted before.
3393 * If none of these cases hold, we check the value of the row for each
3394 * of the currently active samples. Based on the signs of these values
3395 * we make an initial determination of the sign of the row.
3397 * all zero -> unk(nown)
3398 * all non-negative -> pos
3399 * all non-positive -> neg
3400 * both negative and positive -> all
3402 * If we end up with "all", we are done.
3403 * Otherwise, we perform a check for positive and/or negative
3404 * values as follows.
3406 * samples neg unk pos
3412 * There is no special sign for "zero", because we can usually treat zero
3413 * as either non-negative or non-positive, whatever works out best.
3414 * However, if the row is "critical", meaning that pivoting is impossible
3415 * then we don't want to limp zero with the non-positive case, because
3416 * then we we would lose the solution for those values of the parameters
3417 * where the value of the row is zero. Instead, we treat 0 as non-negative
3418 * ensuring a split if the row can attain both zero and negative values.
3419 * The same happens when the original constraint was one that could not
3420 * be satisfied with equality by any integer values of the parameters.
3421 * In this case, we normalize the constraint, but then a value of zero
3422 * for the normalized constraint is actually a positive value for the
3423 * original constraint, so again we need to treat zero as non-negative.
3424 * In both these cases, we have the following decision tree instead:
3426 * all non-negative -> pos
3427 * all negative -> neg
3428 * both negative and non-negative -> all
3436 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3437 struct isl_sol *sol, int row)
3439 struct isl_vec *ineq = NULL;
3440 enum isl_tab_row_sign res = isl_tab_row_unknown;
3445 if (tab->row_sign[row] != isl_tab_row_unknown)
3446 return tab->row_sign[row];
3447 if (is_obviously_nonneg(tab, row))
3448 return isl_tab_row_pos;
3449 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3450 if (tab->row_sign[row2] == isl_tab_row_unknown)
3452 if (identical_parameter_line(tab, row, row2))
3453 return tab->row_sign[row2];
3456 critical = is_critical(tab, row);
3458 ineq = get_row_parameter_ineq(tab, row);
3462 strict = is_strict(ineq);
3464 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3465 critical || strict);
3467 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3468 /* test for negative values */
3470 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3471 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3473 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3477 res = isl_tab_row_pos;
3479 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3481 if (res == isl_tab_row_neg) {
3482 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3483 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3487 if (res == isl_tab_row_neg) {
3488 /* test for positive values */
3490 if (!critical && !strict)
3491 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3493 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3497 res = isl_tab_row_any;
3504 return isl_tab_row_unknown;
3507 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3509 /* Find solutions for values of the parameters that satisfy the given
3512 * We currently take a snapshot of the context tableau that is reset
3513 * when we return from this function, while we make a copy of the main
3514 * tableau, leaving the original main tableau untouched.
3515 * These are fairly arbitrary choices. Making a copy also of the context
3516 * tableau would obviate the need to undo any changes made to it later,
3517 * while taking a snapshot of the main tableau could reduce memory usage.
3518 * If we were to switch to taking a snapshot of the main tableau,
3519 * we would have to keep in mind that we need to save the row signs
3520 * and that we need to do this before saving the current basis
3521 * such that the basis has been restore before we restore the row signs.
3523 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3529 saved = sol->context->op->save(sol->context);
3531 tab = isl_tab_dup(tab);
3535 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3537 find_solutions(sol, tab);
3540 sol->context->op->restore(sol->context, saved);
3546 /* Record the absence of solutions for those values of the parameters
3547 * that do not satisfy the given inequality with equality.
3549 static void no_sol_in_strict(struct isl_sol *sol,
3550 struct isl_tab *tab, struct isl_vec *ineq)
3555 if (!sol->context || sol->error)
3557 saved = sol->context->op->save(sol->context);
3559 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3561 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3570 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3572 sol->context->op->restore(sol->context, saved);
3578 /* Compute the lexicographic minimum of the set represented by the main
3579 * tableau "tab" within the context "sol->context_tab".
3580 * On entry the sample value of the main tableau is lexicographically
3581 * less than or equal to this lexicographic minimum.
3582 * Pivots are performed until a feasible point is found, which is then
3583 * necessarily equal to the minimum, or until the tableau is found to
3584 * be infeasible. Some pivots may need to be performed for only some
3585 * feasible values of the context tableau. If so, the context tableau
3586 * is split into a part where the pivot is needed and a part where it is not.
3588 * Whenever we enter the main loop, the main tableau is such that no
3589 * "obvious" pivots need to be performed on it, where "obvious" means
3590 * that the given row can be seen to be negative without looking at
3591 * the context tableau. In particular, for non-parametric problems,
3592 * no pivots need to be performed on the main tableau.
3593 * The caller of find_solutions is responsible for making this property
3594 * hold prior to the first iteration of the loop, while restore_lexmin
3595 * is called before every other iteration.
3597 * Inside the main loop, we first examine the signs of the rows of
3598 * the main tableau within the context of the context tableau.
3599 * If we find a row that is always non-positive for all values of
3600 * the parameters satisfying the context tableau and negative for at
3601 * least one value of the parameters, we perform the appropriate pivot
3602 * and start over. An exception is the case where no pivot can be
3603 * performed on the row. In this case, we require that the sign of
3604 * the row is negative for all values of the parameters (rather than just
3605 * non-positive). This special case is handled inside row_sign, which
3606 * will say that the row can have any sign if it determines that it can
3607 * attain both negative and zero values.
3609 * If we can't find a row that always requires a pivot, but we can find
3610 * one or more rows that require a pivot for some values of the parameters
3611 * (i.e., the row can attain both positive and negative signs), then we split
3612 * the context tableau into two parts, one where we force the sign to be
3613 * non-negative and one where we force is to be negative.
3614 * The non-negative part is handled by a recursive call (through find_in_pos).
3615 * Upon returning from this call, we continue with the negative part and
3616 * perform the required pivot.
3618 * If no such rows can be found, all rows are non-negative and we have
3619 * found a (rational) feasible point. If we only wanted a rational point
3621 * Otherwise, we check if all values of the sample point of the tableau
3622 * are integral for the variables. If so, we have found the minimal
3623 * integral point and we are done.
3624 * If the sample point is not integral, then we need to make a distinction
3625 * based on whether the constant term is non-integral or the coefficients
3626 * of the parameters. Furthermore, in order to decide how to handle
3627 * the non-integrality, we also need to know whether the coefficients
3628 * of the other columns in the tableau are integral. This leads
3629 * to the following table. The first two rows do not correspond
3630 * to a non-integral sample point and are only mentioned for completeness.
3632 * constant parameters other
3635 * int int rat | -> no problem
3637 * rat int int -> fail
3639 * rat int rat -> cut
3642 * rat rat rat | -> parametric cut
3645 * rat rat int | -> split context
3647 * If the parametric constant is completely integral, then there is nothing
3648 * to be done. If the constant term is non-integral, but all the other
3649 * coefficient are integral, then there is nothing that can be done
3650 * and the tableau has no integral solution.
3651 * If, on the other hand, one or more of the other columns have rational
3652 * coefficients, but the parameter coefficients are all integral, then
3653 * we can perform a regular (non-parametric) cut.
3654 * Finally, if there is any parameter coefficient that is non-integral,
3655 * then we need to involve the context tableau. There are two cases here.
3656 * If at least one other column has a rational coefficient, then we
3657 * can perform a parametric cut in the main tableau by adding a new
3658 * integer division in the context tableau.
3659 * If all other columns have integral coefficients, then we need to
3660 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3661 * is always integral. We do this by introducing an integer division
3662 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3663 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3664 * Since q is expressed in the tableau as
3665 * c + \sum a_i y_i - m q >= 0
3666 * -c - \sum a_i y_i + m q + m - 1 >= 0
3667 * it is sufficient to add the inequality
3668 * -c - \sum a_i y_i + m q >= 0
3669 * In the part of the context where this inequality does not hold, the
3670 * main tableau is marked as being empty.
3672 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3674 struct isl_context *context;
3677 if (!tab || sol->error)
3680 context = sol->context;
3684 if (context->op->is_empty(context))
3687 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3690 enum isl_tab_row_sign sgn;
3694 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3695 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3697 sgn = row_sign(tab, sol, row);
3700 tab->row_sign[row] = sgn;
3701 if (sgn == isl_tab_row_any)
3703 if (sgn == isl_tab_row_any && split == -1)
3705 if (sgn == isl_tab_row_neg)
3708 if (row < tab->n_row)
3711 struct isl_vec *ineq;
3713 split = context->op->best_split(context, tab);
3716 ineq = get_row_parameter_ineq(tab, split);
3720 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3721 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3723 if (tab->row_sign[row] == isl_tab_row_any)
3724 tab->row_sign[row] = isl_tab_row_unknown;
3726 tab->row_sign[split] = isl_tab_row_pos;
3728 find_in_pos(sol, tab, ineq->el);
3729 tab->row_sign[split] = isl_tab_row_neg;
3731 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3732 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3734 context->op->add_ineq(context, ineq->el, 0, 1);
3742 row = first_non_integer_row(tab, &flags);
3745 if (ISL_FL_ISSET(flags, I_PAR)) {
3746 if (ISL_FL_ISSET(flags, I_VAR)) {
3747 if (isl_tab_mark_empty(tab) < 0)
3751 row = add_cut(tab, row);
3752 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3753 struct isl_vec *div;
3754 struct isl_vec *ineq;
3756 div = get_row_split_div(tab, row);
3759 d = context->op->get_div(context, tab, div);
3763 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3767 no_sol_in_strict(sol, tab, ineq);
3768 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3769 context->op->add_ineq(context, ineq->el, 1, 1);
3771 if (sol->error || !context->op->is_ok(context))
3773 tab = set_row_cst_to_div(tab, row, d);
3774 if (context->op->is_empty(context))
3777 row = add_parametric_cut(tab, row, context);
3792 /* Compute the lexicographic minimum of the set represented by the main
3793 * tableau "tab" within the context "sol->context_tab".
3795 * As a preprocessing step, we first transfer all the purely parametric
3796 * equalities from the main tableau to the context tableau, i.e.,
3797 * parameters that have been pivoted to a row.
3798 * These equalities are ignored by the main algorithm, because the
3799 * corresponding rows may not be marked as being non-negative.
3800 * In parts of the context where the added equality does not hold,
3801 * the main tableau is marked as being empty.
3803 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3812 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3816 if (tab->row_var[row] < 0)
3818 if (tab->row_var[row] >= tab->n_param &&
3819 tab->row_var[row] < tab->n_var - tab->n_div)
3821 if (tab->row_var[row] < tab->n_param)
3822 p = tab->row_var[row];
3824 p = tab->row_var[row]
3825 + tab->n_param - (tab->n_var - tab->n_div);
3827 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3830 get_row_parameter_line(tab, row, eq->el);
3831 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3832 eq = isl_vec_normalize(eq);
3835 no_sol_in_strict(sol, tab, eq);
3837 isl_seq_neg(eq->el, eq->el, eq->size);
3839 no_sol_in_strict(sol, tab, eq);
3840 isl_seq_neg(eq->el, eq->el, eq->size);
3842 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3846 if (isl_tab_mark_redundant(tab, row) < 0)
3849 if (sol->context->op->is_empty(sol->context))
3852 row = tab->n_redundant - 1;
3855 find_solutions(sol, tab);
3866 static void sol_map_find_solutions(struct isl_sol_map *sol_map,
3867 struct isl_tab *tab)
3869 find_solutions_main(&sol_map->sol, tab);
3872 /* Check if integer division "div" of "dom" also occurs in "bmap".
3873 * If so, return its position within the divs.
3874 * If not, return -1.
3876 static int find_context_div(struct isl_basic_map *bmap,
3877 struct isl_basic_set *dom, unsigned div)
3880 unsigned b_dim = isl_dim_total(bmap->dim);
3881 unsigned d_dim = isl_dim_total(dom->dim);
3883 if (isl_int_is_zero(dom->div[div][0]))
3885 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3888 for (i = 0; i < bmap->n_div; ++i) {
3889 if (isl_int_is_zero(bmap->div[i][0]))
3891 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3892 (b_dim - d_dim) + bmap->n_div) != -1)
3894 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3900 /* The correspondence between the variables in the main tableau,
3901 * the context tableau, and the input map and domain is as follows.
3902 * The first n_param and the last n_div variables of the main tableau
3903 * form the variables of the context tableau.
3904 * In the basic map, these n_param variables correspond to the
3905 * parameters and the input dimensions. In the domain, they correspond
3906 * to the parameters and the set dimensions.
3907 * The n_div variables correspond to the integer divisions in the domain.
3908 * To ensure that everything lines up, we may need to copy some of the
3909 * integer divisions of the domain to the map. These have to be placed
3910 * in the same order as those in the context and they have to be placed
3911 * after any other integer divisions that the map may have.
3912 * This function performs the required reordering.
3914 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3915 struct isl_basic_set *dom)
3921 for (i = 0; i < dom->n_div; ++i)
3922 if (find_context_div(bmap, dom, i) != -1)
3924 other = bmap->n_div - common;
3925 if (dom->n_div - common > 0) {
3926 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
3927 dom->n_div - common, 0, 0);
3931 for (i = 0; i < dom->n_div; ++i) {
3932 int pos = find_context_div(bmap, dom, i);
3934 pos = isl_basic_map_alloc_div(bmap);
3937 isl_int_set_si(bmap->div[pos][0], 0);
3939 if (pos != other + i)
3940 isl_basic_map_swap_div(bmap, pos, other + i);
3944 isl_basic_map_free(bmap);
3948 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3949 * some obvious symmetries.
3951 * We make sure the divs in the domain are properly ordered,
3952 * because they will be added one by one in the given order
3953 * during the construction of the solution map.
3955 static __isl_give isl_map *basic_map_partial_lexopt_base(
3956 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
3957 __isl_give isl_set **empty, int max)
3959 isl_map *result = NULL;
3960 struct isl_tab *tab;
3961 struct isl_sol_map *sol_map = NULL;
3962 struct isl_context *context;
3965 dom = isl_basic_set_order_divs(dom);
3966 bmap = align_context_divs(bmap, dom);
3968 sol_map = sol_map_init(bmap, dom, !!empty, max);
3972 context = sol_map->sol.context;
3973 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
3975 else if (isl_basic_map_plain_is_empty(bmap))
3976 sol_map_add_empty_if_needed(sol_map,
3977 isl_basic_set_copy(context->op->peek_basic_set(context)));
3979 tab = tab_for_lexmin(bmap,
3980 context->op->peek_basic_set(context), 1, max);
3981 tab = context->op->detect_nonnegative_parameters(context, tab);
3982 sol_map_find_solutions(sol_map, tab);
3984 if (sol_map->sol.error)
3987 result = isl_map_copy(sol_map->map);
3989 *empty = isl_set_copy(sol_map->empty);
3990 sol_free(&sol_map->sol);
3991 isl_basic_map_free(bmap);
3994 sol_free(&sol_map->sol);
3995 isl_basic_map_free(bmap);
3999 /* Structure used during detection of parallel constraints.
4000 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4001 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4002 * val: the coefficients of the output variables
4004 struct isl_constraint_equal_info {
4005 isl_basic_map *bmap;
4011 /* Check whether the coefficients of the output variables
4012 * of the constraint in "entry" are equal to info->val.
4014 static int constraint_equal(const void *entry, const void *val)
4016 isl_int **row = (isl_int **)entry;
4017 const struct isl_constraint_equal_info *info = val;
4019 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4022 /* Check whether "bmap" has a pair of constraints that have
4023 * the same coefficients for the output variables.
4024 * Note that the coefficients of the existentially quantified
4025 * variables need to be zero since the existentially quantified
4026 * of the result are usually not the same as those of the input.
4027 * the isl_dim_out and isl_dim_div dimensions.
4028 * If so, return 1 and return the row indices of the two constraints
4029 * in *first and *second.
4031 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4032 int *first, int *second)
4035 isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
4036 struct isl_hash_table *table = NULL;
4037 struct isl_hash_table_entry *entry;
4038 struct isl_constraint_equal_info info;
4042 ctx = isl_basic_map_get_ctx(bmap);
4043 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4047 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4048 isl_basic_map_dim(bmap, isl_dim_in);
4050 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4051 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4052 info.n_out = n_out + n_div;
4053 for (i = 0; i < bmap->n_ineq; ++i) {
4056 info.val = bmap->ineq[i] + 1 + info.n_in;
4057 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4059 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4061 hash = isl_seq_get_hash(info.val, info.n_out);
4062 entry = isl_hash_table_find(ctx, table, hash,
4063 constraint_equal, &info, 1);
4068 entry->data = &bmap->ineq[i];
4071 if (i < bmap->n_ineq) {
4072 *first = ((isl_int **)entry->data) - bmap->ineq;
4076 isl_hash_table_free(ctx, table);
4078 return i < bmap->n_ineq;
4080 isl_hash_table_free(ctx, table);
4084 /* Given a set of upper bounds on the last "input" variable m,
4085 * construct a set that assigns the minimal upper bound to m, i.e.,
4086 * construct a set that divides the space into cells where one
4087 * of the upper bounds is smaller than all the others and assign
4088 * this upper bound to m.
4090 * In particular, if there are n bounds b_i, then the result
4091 * consists of n basic sets, each one of the form
4094 * b_i <= b_j for j > i
4095 * b_i < b_j for j < i
4097 static __isl_give isl_set *set_minimum(__isl_take isl_dim *dim,
4098 __isl_take isl_mat *var)
4101 isl_basic_set *bset = NULL;
4103 isl_set *set = NULL;
4108 ctx = isl_dim_get_ctx(dim);
4109 set = isl_set_alloc_dim(isl_dim_copy(dim),
4110 var->n_row, ISL_SET_DISJOINT);
4112 for (i = 0; i < var->n_row; ++i) {
4113 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0,
4115 k = isl_basic_set_alloc_equality(bset);
4118 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4119 isl_int_set_si(bset->eq[k][var->n_col], -1);
4120 for (j = 0; j < var->n_row; ++j) {
4123 k = isl_basic_set_alloc_inequality(bset);
4126 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4127 ctx->negone, var->row[i],
4129 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4131 isl_int_sub_ui(bset->ineq[k][0],
4132 bset->ineq[k][0], 1);
4134 bset = isl_basic_set_finalize(bset);
4135 set = isl_set_add_basic_set(set, bset);
4142 isl_basic_set_free(bset);
4149 /* Given that the last input variable of "bmap" represents the minimum
4150 * of the bounds in "cst", check whether we need to split the domain
4151 * based on which bound attains the minimum.
4153 * A split is needed when the minimum appears in an integer division
4154 * or in an equality. Otherwise, it is only needed if it appears in
4155 * an upper bound that is different from the upper bounds on which it
4158 static int need_split_map(__isl_keep isl_basic_map *bmap,
4159 __isl_keep isl_mat *cst)
4165 pos = cst->n_col - 1;
4166 total = isl_basic_map_dim(bmap, isl_dim_all);
4168 for (i = 0; i < bmap->n_div; ++i)
4169 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4172 for (i = 0; i < bmap->n_eq; ++i)
4173 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4176 for (i = 0; i < bmap->n_ineq; ++i) {
4177 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4179 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4181 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4182 total - pos - 1) >= 0)
4185 for (j = 0; j < cst->n_row; ++j)
4186 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4188 if (j >= cst->n_row)
4195 static int need_split_set(__isl_keep isl_basic_set *bset,
4196 __isl_keep isl_mat *cst)
4198 return need_split_map((isl_basic_map *)bset, cst);
4201 /* Given a set of which the last set variable is the minimum
4202 * of the bounds in "cst", split each basic set in the set
4203 * in pieces where one of the bounds is (strictly) smaller than the others.
4204 * This subdivision is given in "min_expr".
4205 * The variable is subsequently projected out.
4207 * We only do the split when it is needed.
4208 * For example if the last input variable m = min(a,b) and the only
4209 * constraints in the given basic set are lower bounds on m,
4210 * i.e., l <= m = min(a,b), then we can simply project out m
4211 * to obtain l <= a and l <= b, without having to split on whether
4212 * m is equal to a or b.
4214 static __isl_give isl_set *split(__isl_take isl_set *empty,
4215 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4222 if (!empty || !min_expr || !cst)
4225 n_in = isl_set_dim(empty, isl_dim_set);
4226 dim = isl_set_get_dim(empty);
4227 dim = isl_dim_drop(dim, isl_dim_set, n_in - 1, 1);
4228 res = isl_set_empty(dim);
4230 for (i = 0; i < empty->n; ++i) {
4233 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4234 if (need_split_set(empty->p[i], cst))
4235 set = isl_set_intersect(set, isl_set_copy(min_expr));
4236 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4238 res = isl_set_union_disjoint(res, set);
4241 isl_set_free(empty);
4242 isl_set_free(min_expr);
4246 isl_set_free(empty);
4247 isl_set_free(min_expr);
4252 /* Given a map of which the last input variable is the minimum
4253 * of the bounds in "cst", split each basic set in the set
4254 * in pieces where one of the bounds is (strictly) smaller than the others.
4255 * This subdivision is given in "min_expr".
4256 * The variable is subsequently projected out.
4258 * The implementation is essentially the same as that of "split".
4260 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4261 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4268 if (!opt || !min_expr || !cst)
4271 n_in = isl_map_dim(opt, isl_dim_in);
4272 dim = isl_map_get_dim(opt);
4273 dim = isl_dim_drop(dim, isl_dim_in, n_in - 1, 1);
4274 res = isl_map_empty(dim);
4276 for (i = 0; i < opt->n; ++i) {
4279 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4280 if (need_split_map(opt->p[i], cst))
4281 map = isl_map_intersect_domain(map,
4282 isl_set_copy(min_expr));
4283 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4285 res = isl_map_union_disjoint(res, map);
4289 isl_set_free(min_expr);
4294 isl_set_free(min_expr);
4299 static __isl_give isl_map *basic_map_partial_lexopt(
4300 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4301 __isl_give isl_set **empty, int max);
4303 /* Given a basic map with at least two parallel constraints (as found
4304 * by the function parallel_constraints), first look for more constraints
4305 * parallel to the two constraint and replace the found list of parallel
4306 * constraints by a single constraint with as "input" part the minimum
4307 * of the input parts of the list of constraints. Then, recursively call
4308 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4309 * and plug in the definition of the minimum in the result.
4311 * More specifically, given a set of constraints
4315 * Replace this set by a single constraint
4319 * with u a new parameter with constraints
4323 * Any solution to the new system is also a solution for the original system
4326 * a x >= -u >= -b_i(p)
4328 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4329 * therefore be plugged into the solution.
4331 static __isl_give isl_map *basic_map_partial_lexopt_symm(
4332 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4333 __isl_give isl_set **empty, int max, int first, int second)
4337 unsigned n_in, n_out, n_div;
4339 isl_vec *var = NULL;
4340 isl_mat *cst = NULL;
4343 isl_dim *map_dim, *set_dim;
4345 map_dim = isl_basic_map_get_dim(bmap);
4346 set_dim = empty ? isl_basic_set_get_dim(dom) : NULL;
4348 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4349 isl_basic_map_dim(bmap, isl_dim_in);
4350 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4352 ctx = isl_basic_map_get_ctx(bmap);
4353 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4354 var = isl_vec_alloc(ctx, n_out);
4360 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4361 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4362 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4366 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4370 for (i = 0; i < n; ++i)
4371 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4373 bmap = isl_basic_map_cow(bmap);
4376 for (i = n - 1; i >= 0; --i)
4377 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4380 bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4381 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4382 k = isl_basic_map_alloc_inequality(bmap);
4385 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4386 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4387 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4388 bmap = isl_basic_map_finalize(bmap);
4390 n_div = isl_basic_set_dim(dom, isl_dim_div);
4391 dom = isl_basic_set_add(dom, isl_dim_set, 1);
4392 dom = isl_basic_set_extend_constraints(dom, 0, n);
4393 for (i = 0; i < n; ++i) {
4394 k = isl_basic_set_alloc_inequality(dom);
4397 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4398 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4399 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4402 min_expr = set_minimum(isl_basic_set_get_dim(dom), isl_mat_copy(cst));
4407 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4410 *empty = split(*empty,
4411 isl_set_copy(min_expr), isl_mat_copy(cst));
4412 *empty = isl_set_reset_dim(*empty, set_dim);
4415 opt = split_domain(opt, min_expr, cst);
4416 opt = isl_map_reset_dim(opt, map_dim);
4420 isl_dim_free(map_dim);
4421 isl_dim_free(set_dim);
4425 isl_basic_set_free(dom);
4426 isl_basic_map_free(bmap);
4430 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4431 * equalities and removing redundant constraints.
4433 * We first check if there are any parallel constraints (left).
4434 * If not, we are in the base case.
4435 * If there are parallel constraints, we replace them by a single
4436 * constraint in basic_map_partial_lexopt_symm and then call
4437 * this function recursively to look for more parallel constraints.
4439 static __isl_give isl_map *basic_map_partial_lexopt(
4440 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4441 __isl_give isl_set **empty, int max)
4449 if (bmap->ctx->opt->pip_symmetry)
4450 par = parallel_constraints(bmap, &first, &second);
4454 return basic_map_partial_lexopt_base(bmap, dom, empty, max);
4456 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4459 isl_basic_set_free(dom);
4460 isl_basic_map_free(bmap);
4464 /* Compute the lexicographic minimum (or maximum if "max" is set)
4465 * of "bmap" over the domain "dom" and return the result as a map.
4466 * If "empty" is not NULL, then *empty is assigned a set that
4467 * contains those parts of the domain where there is no solution.
4468 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4469 * then we compute the rational optimum. Otherwise, we compute
4470 * the integral optimum.
4472 * We perform some preprocessing. As the PILP solver does not
4473 * handle implicit equalities very well, we first make sure all
4474 * the equalities are explicitly available.
4476 * We also add context constraints to the basic map and remove
4477 * redundant constraints. This is only needed because of the
4478 * way we handle simple symmetries. In particular, we currently look
4479 * for symmetries on the constraints, before we set up the main tableau.
4480 * It is then no good to look for symmetries on possibly redundant constraints.
4482 struct isl_map *isl_tab_basic_map_partial_lexopt(
4483 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4484 struct isl_set **empty, int max)
4491 isl_assert(bmap->ctx,
4492 isl_basic_map_compatible_domain(bmap, dom), goto error);
4494 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4495 return basic_map_partial_lexopt(bmap, dom, empty, max);
4497 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4498 bmap = isl_basic_map_detect_equalities(bmap);
4499 bmap = isl_basic_map_remove_redundancies(bmap);
4501 return basic_map_partial_lexopt(bmap, dom, empty, max);
4503 isl_basic_set_free(dom);
4504 isl_basic_map_free(bmap);
4508 struct isl_sol_for {
4510 int (*fn)(__isl_take isl_basic_set *dom,
4511 __isl_take isl_aff_list *list, void *user);
4515 static void sol_for_free(struct isl_sol_for *sol_for)
4517 if (sol_for->sol.context)
4518 sol_for->sol.context->op->free(sol_for->sol.context);
4522 static void sol_for_free_wrap(struct isl_sol *sol)
4524 sol_for_free((struct isl_sol_for *)sol);
4527 /* Add the solution identified by the tableau and the context tableau.
4529 * See documentation of sol_add for more details.
4531 * Instead of constructing a basic map, this function calls a user
4532 * defined function with the current context as a basic set and
4533 * a list of affine expressions representing the relation between
4534 * the input and output. The space over which the affine expressions
4535 * are defined is the same as that of the domain. The number of
4536 * affine expressions in the list is equal to the number of output variables.
4538 static void sol_for_add(struct isl_sol_for *sol,
4539 struct isl_basic_set *dom, struct isl_mat *M)
4543 isl_local_space *ls;
4547 if (sol->sol.error || !dom || !M)
4550 ctx = isl_basic_set_get_ctx(dom);
4551 ls = isl_basic_set_get_local_space(dom);
4552 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4553 for (i = 1; i < M->n_row; ++i) {
4554 aff = isl_aff_alloc(isl_local_space_copy(ls));
4556 isl_int_set(aff->v->el[0], M->row[0][0]);
4557 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4559 list = isl_aff_list_add(list, aff);
4561 isl_local_space_free(ls);
4563 dom = isl_basic_set_finalize(dom);
4565 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4568 isl_basic_set_free(dom);
4572 isl_basic_set_free(dom);
4577 static void sol_for_add_wrap(struct isl_sol *sol,
4578 struct isl_basic_set *dom, struct isl_mat *M)
4580 sol_for_add((struct isl_sol_for *)sol, dom, M);
4583 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4584 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4588 struct isl_sol_for *sol_for = NULL;
4589 struct isl_dim *dom_dim;
4590 struct isl_basic_set *dom = NULL;
4592 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4596 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4597 dom = isl_basic_set_universe(dom_dim);
4599 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4600 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4601 sol_for->sol.dec_level.sol = &sol_for->sol;
4603 sol_for->user = user;
4604 sol_for->sol.max = max;
4605 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4606 sol_for->sol.add = &sol_for_add_wrap;
4607 sol_for->sol.add_empty = NULL;
4608 sol_for->sol.free = &sol_for_free_wrap;
4610 sol_for->sol.context = isl_context_alloc(dom);
4611 if (!sol_for->sol.context)
4614 isl_basic_set_free(dom);
4617 isl_basic_set_free(dom);
4618 sol_for_free(sol_for);
4622 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4623 struct isl_tab *tab)
4625 find_solutions_main(&sol_for->sol, tab);
4628 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4629 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4633 struct isl_sol_for *sol_for = NULL;
4635 bmap = isl_basic_map_copy(bmap);
4639 bmap = isl_basic_map_detect_equalities(bmap);
4640 sol_for = sol_for_init(bmap, max, fn, user);
4642 if (isl_basic_map_plain_is_empty(bmap))
4645 struct isl_tab *tab;
4646 struct isl_context *context = sol_for->sol.context;
4647 tab = tab_for_lexmin(bmap,
4648 context->op->peek_basic_set(context), 1, max);
4649 tab = context->op->detect_nonnegative_parameters(context, tab);
4650 sol_for_find_solutions(sol_for, tab);
4651 if (sol_for->sol.error)
4655 sol_free(&sol_for->sol);
4656 isl_basic_map_free(bmap);
4659 sol_free(&sol_for->sol);
4660 isl_basic_map_free(bmap);
4664 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map *bmap,
4665 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4669 return isl_basic_map_foreach_lexopt(bmap, 0, fn, user);
4672 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map *bmap,
4673 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4677 return isl_basic_map_foreach_lexopt(bmap, 1, fn, user);
4680 int isl_basic_set_foreach_lexmax(__isl_keep isl_basic_set *bset,
4681 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4685 return isl_basic_map_foreach_lexmax(bset, fn, user);
4688 /* Check if the given sequence of len variables starting at pos
4689 * represents a trivial (i.e., zero) solution.
4690 * The variables are assumed to be non-negative and to come in pairs,
4691 * with each pair representing a variable of unrestricted sign.
4692 * The solution is trivial if each such pair in the sequence consists
4693 * of two identical values, meaning that the variable being represented
4696 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4703 for (i = 0; i < len; i += 2) {
4707 neg_row = tab->var[pos + i].is_row ?
4708 tab->var[pos + i].index : -1;
4709 pos_row = tab->var[pos + i + 1].is_row ?
4710 tab->var[pos + i + 1].index : -1;
4713 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4715 isl_int_is_zero(tab->mat->row[pos_row][1])))
4718 if (neg_row < 0 || pos_row < 0)
4720 if (isl_int_ne(tab->mat->row[neg_row][1],
4721 tab->mat->row[pos_row][1]))
4728 /* Return the index of the first trivial region or -1 if all regions
4731 static int first_trivial_region(struct isl_tab *tab,
4732 int n_region, struct isl_region *region)
4736 for (i = 0; i < n_region; ++i) {
4737 if (region_is_trivial(tab, region[i].pos, region[i].len))
4744 /* Check if the solution is optimal, i.e., whether the first
4745 * n_op entries are zero.
4747 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4751 for (i = 0; i < n_op; ++i)
4752 if (!isl_int_is_zero(sol->el[1 + i]))
4757 /* Add constraints to "tab" that ensure that any solution is significantly
4758 * better that that represented by "sol". That is, find the first
4759 * relevant (within first n_op) non-zero coefficient and force it (along
4760 * with all previous coefficients) to be zero.
4761 * If the solution is already optimal (all relevant coefficients are zero),
4762 * then just mark the table as empty.
4764 static int force_better_solution(struct isl_tab *tab,
4765 __isl_keep isl_vec *sol, int n_op)
4774 for (i = 0; i < n_op; ++i)
4775 if (!isl_int_is_zero(sol->el[1 + i]))
4779 if (isl_tab_mark_empty(tab) < 0)
4784 ctx = isl_vec_get_ctx(sol);
4785 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4789 for (; i >= 0; --i) {
4791 isl_int_set_si(v->el[1 + i], -1);
4792 if (add_lexmin_eq(tab, v->el) < 0)
4803 struct isl_trivial {
4807 struct isl_tab_undo *snap;
4810 /* Return the lexicographically smallest non-trivial solution of the
4811 * given ILP problem.
4813 * All variables are assumed to be non-negative.
4815 * n_op is the number of initial coordinates to optimize.
4816 * That is, once a solution has been found, we will only continue looking
4817 * for solution that result in significantly better values for those
4818 * initial coordinates. That is, we only continue looking for solutions
4819 * that increase the number of initial zeros in this sequence.
4821 * A solution is non-trivial, if it is non-trivial on each of the
4822 * specified regions. Each region represents a sequence of pairs
4823 * of variables. A solution is non-trivial on such a region if
4824 * at least one of these pairs consists of different values, i.e.,
4825 * such that the non-negative variable represented by the pair is non-zero.
4827 * Whenever a conflict is encountered, all constraints involved are
4828 * reported to the caller through a call to "conflict".
4830 * We perform a simple branch-and-bound backtracking search.
4831 * Each level in the search represents initially trivial region that is forced
4832 * to be non-trivial.
4833 * At each level we consider n cases, where n is the length of the region.
4834 * In terms of the n/2 variables of unrestricted signs being encoded by
4835 * the region, we consider the cases
4838 * x_0 = 0 and x_1 >= 1
4839 * x_0 = 0 and x_1 <= -1
4840 * x_0 = 0 and x_1 = 0 and x_2 >= 1
4841 * x_0 = 0 and x_1 = 0 and x_2 <= -1
4843 * The cases are considered in this order, assuming that each pair
4844 * x_i_a x_i_b represents the value x_i_b - x_i_a.
4845 * That is, x_0 >= 1 is enforced by adding the constraint
4846 * x_0_b - x_0_a >= 1
4848 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
4849 __isl_take isl_basic_set *bset, int n_op, int n_region,
4850 struct isl_region *region,
4851 int (*conflict)(int con, void *user), void *user)
4855 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
4857 isl_vec *sol = isl_vec_alloc(ctx, 0);
4858 struct isl_tab *tab;
4859 struct isl_trivial *triv = NULL;
4862 tab = tab_for_lexmin(isl_basic_map_from_range(bset), NULL, 0, 0);
4865 tab->conflict = conflict;
4866 tab->conflict_user = user;
4868 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4869 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
4876 while (level >= 0) {
4880 tab = cut_to_integer_lexmin(tab);
4885 r = first_trivial_region(tab, n_region, region);
4887 for (i = 0; i < level; ++i)
4890 sol = isl_tab_get_sample_value(tab);
4893 if (is_optimal(sol, n_op))
4897 if (level >= n_region)
4898 isl_die(ctx, isl_error_internal,
4899 "nesting level too deep", goto error);
4900 if (isl_tab_extend_cons(tab,
4901 2 * region[r].len + 2 * n_op) < 0)
4903 triv[level].region = r;
4904 triv[level].side = 0;
4907 r = triv[level].region;
4908 side = triv[level].side;
4909 base = 2 * (side/2);
4911 if (side >= region[r].len) {
4916 if (isl_tab_rollback(tab, triv[level].snap) < 0)
4921 if (triv[level].update) {
4922 if (force_better_solution(tab, sol, n_op) < 0)
4924 triv[level].update = 0;
4927 if (side == base && base >= 2) {
4928 for (j = base - 2; j < base; ++j) {
4930 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
4931 if (add_lexmin_eq(tab, v->el) < 0)
4936 triv[level].snap = isl_tab_snap(tab);
4937 if (isl_tab_push_basis(tab) < 0)
4941 isl_int_set_si(v->el[0], -1);
4942 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
4943 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
4944 tab = add_lexmin_ineq(tab, v->el);
4954 isl_basic_set_free(bset);
4961 isl_basic_set_free(bset);
4966 /* Return the lexicographically smallest rational point in "bset",
4967 * assuming that all variables are non-negative.
4968 * If "bset" is empty, then return a zero-length vector.
4970 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
4971 __isl_take isl_basic_set *bset)
4973 struct isl_tab *tab;
4974 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
4977 tab = tab_for_lexmin(isl_basic_map_from_range(bset), NULL, 0, 0);
4981 sol = isl_vec_alloc(ctx, 0);
4983 sol = isl_tab_get_sample_value(tab);
4985 isl_basic_set_free(bset);
4989 isl_basic_set_free(bset);