2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT 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_options_private.h>
21 #include <isl_config.h>
24 * The implementation of parametric integer linear programming in this file
25 * was inspired by the paper "Parametric Integer Programming" and the
26 * report "Solving systems of affine (in)equalities" by Paul Feautrier
29 * The strategy used for obtaining a feasible solution is different
30 * from the one used in isl_tab.c. In particular, in isl_tab.c,
31 * upon finding a constraint that is not yet satisfied, we pivot
32 * in a row that increases the constant term of the row holding the
33 * constraint, making sure the sample solution remains feasible
34 * for all the constraints it already satisfied.
35 * Here, we always pivot in the row holding the constraint,
36 * choosing a column that induces the lexicographically smallest
37 * increment to the sample solution.
39 * By starting out from a sample value that is lexicographically
40 * smaller than any integer point in the problem space, the first
41 * feasible integer sample point we find will also be the lexicographically
42 * smallest. If all variables can be assumed to be non-negative,
43 * then the initial sample value may be chosen equal to zero.
44 * However, we will not make this assumption. Instead, we apply
45 * the "big parameter" trick. Any variable x is then not directly
46 * used in the tableau, but instead it is represented by another
47 * variable x' = M + x, where M is an arbitrarily large (positive)
48 * value. x' is therefore always non-negative, whatever the value of x.
49 * Taking as initial sample value x' = 0 corresponds to x = -M,
50 * which is always smaller than any possible value of x.
52 * The big parameter trick is used in the main tableau and
53 * also in the context tableau if isl_context_lex is used.
54 * In this case, each tableaus has its own big parameter.
55 * Before doing any real work, we check if all the parameters
56 * happen to be non-negative. If so, we drop the column corresponding
57 * to M from the initial context tableau.
58 * If isl_context_gbr is used, then the big parameter trick is only
59 * used in the main tableau.
63 struct isl_context_op {
64 /* detect nonnegative parameters in context and mark them in tab */
65 struct isl_tab *(*detect_nonnegative_parameters)(
66 struct isl_context *context, struct isl_tab *tab);
67 /* return temporary reference to basic set representation of context */
68 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
69 /* return temporary reference to tableau representation of context */
70 struct isl_tab *(*peek_tab)(struct isl_context *context);
71 /* add equality; check is 1 if eq may not be valid;
72 * update is 1 if we may want to call ineq_sign on context later.
74 void (*add_eq)(struct isl_context *context, isl_int *eq,
75 int check, int update);
76 /* add inequality; check is 1 if ineq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
80 int check, int update);
81 /* check sign of ineq based on previous information.
82 * strict is 1 if saturation should be treated as a positive sign.
84 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
85 isl_int *ineq, int strict);
86 /* check if inequality maintains feasibility */
87 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
88 /* return index of a div that corresponds to "div" */
89 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
91 /* add div "div" to context and return non-negativity */
92 int (*add_div)(struct isl_context *context, struct isl_vec *div);
93 int (*detect_equalities)(struct isl_context *context,
95 /* return row index of "best" split */
96 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
97 /* check if context has already been determined to be empty */
98 int (*is_empty)(struct isl_context *context);
99 /* check if context is still usable */
100 int (*is_ok)(struct isl_context *context);
101 /* save a copy/snapshot of context */
102 void *(*save)(struct isl_context *context);
103 /* restore saved context */
104 void (*restore)(struct isl_context *context, void *);
105 /* invalidate context */
106 void (*invalidate)(struct isl_context *context);
108 void (*free)(struct isl_context *context);
112 struct isl_context_op *op;
115 struct isl_context_lex {
116 struct isl_context context;
120 struct isl_partial_sol {
122 struct isl_basic_set *dom;
125 struct isl_partial_sol *next;
129 struct isl_sol_callback {
130 struct isl_tab_callback callback;
134 /* isl_sol is an interface for constructing a solution to
135 * a parametric integer linear programming problem.
136 * Every time the algorithm reaches a state where a solution
137 * can be read off from the tableau (including cases where the tableau
138 * is empty), the function "add" is called on the isl_sol passed
139 * to find_solutions_main.
141 * The context tableau is owned by isl_sol and is updated incrementally.
143 * There are currently two implementations of this interface,
144 * isl_sol_map, which simply collects the solutions in an isl_map
145 * and (optionally) the parts of the context where there is no solution
147 * isl_sol_for, which calls a user-defined function for each part of
156 struct isl_context *context;
157 struct isl_partial_sol *partial;
158 void (*add)(struct isl_sol *sol,
159 struct isl_basic_set *dom, struct isl_mat *M);
160 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
161 void (*free)(struct isl_sol *sol);
162 struct isl_sol_callback dec_level;
165 static void sol_free(struct isl_sol *sol)
167 struct isl_partial_sol *partial, *next;
170 for (partial = sol->partial; partial; partial = next) {
171 next = partial->next;
172 isl_basic_set_free(partial->dom);
173 isl_mat_free(partial->M);
179 /* Push a partial solution represented by a domain and mapping M
180 * onto the stack of partial solutions.
182 static void sol_push_sol(struct isl_sol *sol,
183 struct isl_basic_set *dom, struct isl_mat *M)
185 struct isl_partial_sol *partial;
187 if (sol->error || !dom)
190 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
194 partial->level = sol->level;
197 partial->next = sol->partial;
199 sol->partial = partial;
203 isl_basic_set_free(dom);
208 /* Pop one partial solution from the partial solution stack and
209 * pass it on to sol->add or sol->add_empty.
211 static void sol_pop_one(struct isl_sol *sol)
213 struct isl_partial_sol *partial;
215 partial = sol->partial;
216 sol->partial = partial->next;
219 sol->add(sol, partial->dom, partial->M);
221 sol->add_empty(sol, partial->dom);
225 /* Return a fresh copy of the domain represented by the context tableau.
227 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
229 struct isl_basic_set *bset;
234 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
235 bset = isl_basic_set_update_from_tab(bset,
236 sol->context->op->peek_tab(sol->context));
241 /* Check whether two partial solutions have the same mapping, where n_div
242 * is the number of divs that the two partial solutions have in common.
244 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
250 if (!s1->M != !s2->M)
255 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
257 for (i = 0; i < s1->M->n_row; ++i) {
258 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
259 s1->M->n_col-1-dim-n_div) != -1)
261 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
262 s2->M->n_col-1-dim-n_div) != -1)
264 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
270 /* Pop all solutions from the partial solution stack that were pushed onto
271 * the stack at levels that are deeper than the current level.
272 * If the two topmost elements on the stack have the same level
273 * and represent the same solution, then their domains are combined.
274 * This combined domain is the same as the current context domain
275 * as sol_pop is called each time we move back to a higher level.
277 static void sol_pop(struct isl_sol *sol)
279 struct isl_partial_sol *partial;
285 if (sol->level == 0) {
286 for (partial = sol->partial; partial; partial = sol->partial)
291 partial = sol->partial;
295 if (partial->level <= sol->level)
298 if (partial->next && partial->next->level == partial->level) {
299 n_div = isl_basic_set_dim(
300 sol->context->op->peek_basic_set(sol->context),
303 if (!same_solution(partial, partial->next, n_div)) {
307 struct isl_basic_set *bset;
309 bset = sol_domain(sol);
311 isl_basic_set_free(partial->next->dom);
312 partial->next->dom = bset;
313 partial->next->level = sol->level;
315 sol->partial = partial->next;
316 isl_basic_set_free(partial->dom);
317 isl_mat_free(partial->M);
324 static void sol_dec_level(struct isl_sol *sol)
334 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
336 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
338 sol_dec_level(callback->sol);
340 return callback->sol->error ? -1 : 0;
343 /* Move down to next level and push callback onto context tableau
344 * to decrease the level again when it gets rolled back across
345 * the current state. That is, dec_level will be called with
346 * the context tableau in the same state as it is when inc_level
349 static void sol_inc_level(struct isl_sol *sol)
357 tab = sol->context->op->peek_tab(sol->context);
358 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
362 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
366 if (isl_int_is_one(m))
369 for (i = 0; i < n_row; ++i)
370 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
373 /* Add the solution identified by the tableau and the context tableau.
375 * The layout of the variables is as follows.
376 * tab->n_var is equal to the total number of variables in the input
377 * map (including divs that were copied from the context)
378 * + the number of extra divs constructed
379 * Of these, the first tab->n_param and the last tab->n_div variables
380 * correspond to the variables in the context, i.e.,
381 * tab->n_param + tab->n_div = context_tab->n_var
382 * tab->n_param is equal to the number of parameters and input
383 * dimensions in the input map
384 * tab->n_div is equal to the number of divs in the context
386 * If there is no solution, then call add_empty with a basic set
387 * that corresponds to the context tableau. (If add_empty is NULL,
390 * If there is a solution, then first construct a matrix that maps
391 * all dimensions of the context to the output variables, i.e.,
392 * the output dimensions in the input map.
393 * The divs in the input map (if any) that do not correspond to any
394 * div in the context do not appear in the solution.
395 * The algorithm will make sure that they have an integer value,
396 * but these values themselves are of no interest.
397 * We have to be careful not to drop or rearrange any divs in the
398 * context because that would change the meaning of the matrix.
400 * To extract the value of the output variables, it should be noted
401 * that we always use a big parameter M in the main tableau and so
402 * the variable stored in this tableau is not an output variable x itself, but
403 * x' = M + x (in case of minimization)
405 * x' = M - x (in case of maximization)
406 * If x' appears in a column, then its optimal value is zero,
407 * which means that the optimal value of x is an unbounded number
408 * (-M for minimization and M for maximization).
409 * We currently assume that the output dimensions in the original map
410 * are bounded, so this cannot occur.
411 * Similarly, when x' appears in a row, then the coefficient of M in that
412 * row is necessarily 1.
413 * If the row in the tableau represents
414 * d x' = c + d M + e(y)
415 * then, in case of minimization, the corresponding row in the matrix
418 * with a d = m, the (updated) common denominator of the matrix.
419 * In case of maximization, the row will be
422 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
424 struct isl_basic_set *bset = NULL;
425 struct isl_mat *mat = NULL;
430 if (sol->error || !tab)
433 if (tab->empty && !sol->add_empty)
435 if (sol->context->op->is_empty(sol->context))
438 bset = sol_domain(sol);
441 sol_push_sol(sol, bset, NULL);
447 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
448 1 + tab->n_param + tab->n_div);
454 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
455 isl_int_set_si(mat->row[0][0], 1);
456 for (row = 0; row < sol->n_out; ++row) {
457 int i = tab->n_param + row;
460 isl_seq_clr(mat->row[1 + row], mat->n_col);
461 if (!tab->var[i].is_row) {
463 isl_die(mat->ctx, isl_error_invalid,
464 "unbounded optimum", goto error2);
468 r = tab->var[i].index;
470 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
471 isl_die(mat->ctx, isl_error_invalid,
472 "unbounded optimum", goto error2);
473 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
474 isl_int_divexact(m, tab->mat->row[r][0], m);
475 scale_rows(mat, m, 1 + row);
476 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
477 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
478 for (j = 0; j < tab->n_param; ++j) {
480 if (tab->var[j].is_row)
482 col = tab->var[j].index;
483 isl_int_mul(mat->row[1 + row][1 + j], m,
484 tab->mat->row[r][off + col]);
486 for (j = 0; j < tab->n_div; ++j) {
488 if (tab->var[tab->n_var - tab->n_div+j].is_row)
490 col = tab->var[tab->n_var - tab->n_div+j].index;
491 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
492 tab->mat->row[r][off + col]);
495 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
501 sol_push_sol(sol, bset, mat);
506 isl_basic_set_free(bset);
514 struct isl_set *empty;
517 static void sol_map_free(struct isl_sol_map *sol_map)
521 if (sol_map->sol.context)
522 sol_map->sol.context->op->free(sol_map->sol.context);
523 isl_map_free(sol_map->map);
524 isl_set_free(sol_map->empty);
528 static void sol_map_free_wrap(struct isl_sol *sol)
530 sol_map_free((struct isl_sol_map *)sol);
533 /* This function is called for parts of the context where there is
534 * no solution, with "bset" corresponding to the context tableau.
535 * Simply add the basic set to the set "empty".
537 static void sol_map_add_empty(struct isl_sol_map *sol,
538 struct isl_basic_set *bset)
542 isl_assert(bset->ctx, sol->empty, goto error);
544 sol->empty = isl_set_grow(sol->empty, 1);
545 bset = isl_basic_set_simplify(bset);
546 bset = isl_basic_set_finalize(bset);
547 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
550 isl_basic_set_free(bset);
553 isl_basic_set_free(bset);
557 static void sol_map_add_empty_wrap(struct isl_sol *sol,
558 struct isl_basic_set *bset)
560 sol_map_add_empty((struct isl_sol_map *)sol, bset);
563 /* Given a basic map "dom" that represents the context and an affine
564 * matrix "M" that maps the dimensions of the context to the
565 * output variables, construct a basic map with the same parameters
566 * and divs as the context, the dimensions of the context as input
567 * dimensions and a number of output dimensions that is equal to
568 * the number of output dimensions in the input map.
570 * The constraints and divs of the context are simply copied
571 * from "dom". For each row
575 * is added, with d the common denominator of M.
577 static void sol_map_add(struct isl_sol_map *sol,
578 struct isl_basic_set *dom, struct isl_mat *M)
581 struct isl_basic_map *bmap = NULL;
589 if (sol->sol.error || !dom || !M)
592 n_out = sol->sol.n_out;
593 n_eq = dom->n_eq + n_out;
594 n_ineq = dom->n_ineq;
596 nparam = isl_basic_set_total_dim(dom) - n_div;
597 total = isl_map_dim(sol->map, isl_dim_all);
598 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
599 n_div, n_eq, 2 * n_div + n_ineq);
602 if (sol->sol.rational)
603 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
604 for (i = 0; i < dom->n_div; ++i) {
605 int k = isl_basic_map_alloc_div(bmap);
608 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
609 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
610 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
611 dom->div[i] + 1 + 1 + nparam, i);
613 for (i = 0; i < dom->n_eq; ++i) {
614 int k = isl_basic_map_alloc_equality(bmap);
617 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
618 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
619 isl_seq_cpy(bmap->eq[k] + 1 + total,
620 dom->eq[i] + 1 + nparam, n_div);
622 for (i = 0; i < dom->n_ineq; ++i) {
623 int k = isl_basic_map_alloc_inequality(bmap);
626 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
627 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
628 isl_seq_cpy(bmap->ineq[k] + 1 + total,
629 dom->ineq[i] + 1 + nparam, n_div);
631 for (i = 0; i < M->n_row - 1; ++i) {
632 int k = isl_basic_map_alloc_equality(bmap);
635 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
636 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
637 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
638 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
639 M->row[1 + i] + 1 + nparam, n_div);
641 bmap = isl_basic_map_simplify(bmap);
642 bmap = isl_basic_map_finalize(bmap);
643 sol->map = isl_map_grow(sol->map, 1);
644 sol->map = isl_map_add_basic_map(sol->map, bmap);
645 isl_basic_set_free(dom);
651 isl_basic_set_free(dom);
653 isl_basic_map_free(bmap);
657 static void sol_map_add_wrap(struct isl_sol *sol,
658 struct isl_basic_set *dom, struct isl_mat *M)
660 sol_map_add((struct isl_sol_map *)sol, dom, M);
664 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
665 * i.e., the constant term and the coefficients of all variables that
666 * appear in the context tableau.
667 * Note that the coefficient of the big parameter M is NOT copied.
668 * The context tableau may not have a big parameter and even when it
669 * does, it is a different big parameter.
671 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
674 unsigned off = 2 + tab->M;
676 isl_int_set(line[0], tab->mat->row[row][1]);
677 for (i = 0; i < tab->n_param; ++i) {
678 if (tab->var[i].is_row)
679 isl_int_set_si(line[1 + i], 0);
681 int col = tab->var[i].index;
682 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
685 for (i = 0; i < tab->n_div; ++i) {
686 if (tab->var[tab->n_var - tab->n_div + i].is_row)
687 isl_int_set_si(line[1 + tab->n_param + i], 0);
689 int col = tab->var[tab->n_var - tab->n_div + i].index;
690 isl_int_set(line[1 + tab->n_param + i],
691 tab->mat->row[row][off + col]);
696 /* Check if rows "row1" and "row2" have identical "parametric constants",
697 * as explained above.
698 * In this case, we also insist that the coefficients of the big parameter
699 * be the same as the values of the constants will only be the same
700 * if these coefficients are also the same.
702 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
705 unsigned off = 2 + tab->M;
707 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
710 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
711 tab->mat->row[row2][2]))
714 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
715 int pos = i < tab->n_param ? i :
716 tab->n_var - tab->n_div + i - tab->n_param;
719 if (tab->var[pos].is_row)
721 col = tab->var[pos].index;
722 if (isl_int_ne(tab->mat->row[row1][off + col],
723 tab->mat->row[row2][off + col]))
729 /* Return an inequality that expresses that the "parametric constant"
730 * should be non-negative.
731 * This function is only called when the coefficient of the big parameter
734 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
736 struct isl_vec *ineq;
738 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
742 get_row_parameter_line(tab, row, ineq->el);
744 ineq = isl_vec_normalize(ineq);
749 /* Normalize a div expression of the form
751 * [(g*f(x) + c)/(g * m)]
753 * with c the constant term and f(x) the remaining coefficients, to
757 static void normalize_div(__isl_keep isl_vec *div)
759 isl_ctx *ctx = isl_vec_get_ctx(div);
760 int len = div->size - 2;
762 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
763 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
765 if (isl_int_is_one(ctx->normalize_gcd))
768 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
769 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
770 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
773 /* Return a integer division for use in a parametric cut based on the given row.
774 * In particular, let the parametric constant of the row be
778 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
779 * The div returned is equal to
781 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
783 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
787 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
791 isl_int_set(div->el[0], tab->mat->row[row][0]);
792 get_row_parameter_line(tab, row, div->el + 1);
793 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
795 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
800 /* Return a integer division for use in transferring an integrality constraint
802 * In particular, let the parametric constant of the row be
806 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
807 * The the returned div is equal to
809 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
811 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
815 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
819 isl_int_set(div->el[0], tab->mat->row[row][0]);
820 get_row_parameter_line(tab, row, div->el + 1);
822 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
827 /* Construct and return an inequality that expresses an upper bound
829 * In particular, if the div is given by
833 * then the inequality expresses
837 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
841 struct isl_vec *ineq;
846 total = isl_basic_set_total_dim(bset);
847 div_pos = 1 + total - bset->n_div + div;
849 ineq = isl_vec_alloc(bset->ctx, 1 + total);
853 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
854 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
858 /* Given a row in the tableau and a div that was created
859 * using get_row_split_div and that has been constrained to equality, i.e.,
861 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
863 * replace the expression "\sum_i {a_i} y_i" in the row by d,
864 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
865 * The coefficients of the non-parameters in the tableau have been
866 * verified to be integral. We can therefore simply replace coefficient b
867 * by floor(b). For the coefficients of the parameters we have
868 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
871 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
873 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
874 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
876 isl_int_set_si(tab->mat->row[row][0], 1);
878 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
879 int drow = tab->var[tab->n_var - tab->n_div + div].index;
881 isl_assert(tab->mat->ctx,
882 isl_int_is_one(tab->mat->row[drow][0]), goto error);
883 isl_seq_combine(tab->mat->row[row] + 1,
884 tab->mat->ctx->one, tab->mat->row[row] + 1,
885 tab->mat->ctx->one, tab->mat->row[drow] + 1,
886 1 + tab->M + tab->n_col);
888 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
890 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
891 tab->mat->row[row][2 + tab->M + dcol], 1);
900 /* Check if the (parametric) constant of the given row is obviously
901 * negative, meaning that we don't need to consult the context tableau.
902 * If there is a big parameter and its coefficient is non-zero,
903 * then this coefficient determines the outcome.
904 * Otherwise, we check whether the constant is negative and
905 * all non-zero coefficients of parameters are negative and
906 * belong to non-negative parameters.
908 static int is_obviously_neg(struct isl_tab *tab, int row)
912 unsigned off = 2 + tab->M;
915 if (isl_int_is_pos(tab->mat->row[row][2]))
917 if (isl_int_is_neg(tab->mat->row[row][2]))
921 if (isl_int_is_nonneg(tab->mat->row[row][1]))
923 for (i = 0; i < tab->n_param; ++i) {
924 /* Eliminated parameter */
925 if (tab->var[i].is_row)
927 col = tab->var[i].index;
928 if (isl_int_is_zero(tab->mat->row[row][off + col]))
930 if (!tab->var[i].is_nonneg)
932 if (isl_int_is_pos(tab->mat->row[row][off + col]))
935 for (i = 0; i < tab->n_div; ++i) {
936 if (tab->var[tab->n_var - tab->n_div + i].is_row)
938 col = tab->var[tab->n_var - tab->n_div + i].index;
939 if (isl_int_is_zero(tab->mat->row[row][off + col]))
941 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
943 if (isl_int_is_pos(tab->mat->row[row][off + col]))
949 /* Check if the (parametric) constant of the given row is obviously
950 * non-negative, meaning that we don't need to consult the context tableau.
951 * If there is a big parameter and its coefficient is non-zero,
952 * then this coefficient determines the outcome.
953 * Otherwise, we check whether the constant is non-negative and
954 * all non-zero coefficients of parameters are positive and
955 * belong to non-negative parameters.
957 static int is_obviously_nonneg(struct isl_tab *tab, int row)
961 unsigned off = 2 + tab->M;
964 if (isl_int_is_pos(tab->mat->row[row][2]))
966 if (isl_int_is_neg(tab->mat->row[row][2]))
970 if (isl_int_is_neg(tab->mat->row[row][1]))
972 for (i = 0; i < tab->n_param; ++i) {
973 /* Eliminated parameter */
974 if (tab->var[i].is_row)
976 col = tab->var[i].index;
977 if (isl_int_is_zero(tab->mat->row[row][off + col]))
979 if (!tab->var[i].is_nonneg)
981 if (isl_int_is_neg(tab->mat->row[row][off + col]))
984 for (i = 0; i < tab->n_div; ++i) {
985 if (tab->var[tab->n_var - tab->n_div + i].is_row)
987 col = tab->var[tab->n_var - tab->n_div + i].index;
988 if (isl_int_is_zero(tab->mat->row[row][off + col]))
990 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
992 if (isl_int_is_neg(tab->mat->row[row][off + col]))
998 /* Given a row r and two columns, return the column that would
999 * lead to the lexicographically smallest increment in the sample
1000 * solution when leaving the basis in favor of the row.
1001 * Pivoting with column c will increment the sample value by a non-negative
1002 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1003 * corresponding to the non-parametric variables.
1004 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1005 * with all other entries in this virtual row equal to zero.
1006 * If variable v appears in a row, then a_{v,c} is the element in column c
1009 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1010 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1011 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1012 * increment. Otherwise, it's c2.
1014 static int lexmin_col_pair(struct isl_tab *tab,
1015 int row, int col1, int col2, isl_int tmp)
1020 tr = tab->mat->row[row] + 2 + tab->M;
1022 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1026 if (!tab->var[i].is_row) {
1027 if (tab->var[i].index == col1)
1029 if (tab->var[i].index == col2)
1034 if (tab->var[i].index == row)
1037 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1038 s1 = isl_int_sgn(r[col1]);
1039 s2 = isl_int_sgn(r[col2]);
1040 if (s1 == 0 && s2 == 0)
1047 isl_int_mul(tmp, r[col2], tr[col1]);
1048 isl_int_submul(tmp, r[col1], tr[col2]);
1049 if (isl_int_is_pos(tmp))
1051 if (isl_int_is_neg(tmp))
1057 /* Given a row in the tableau, find and return the column that would
1058 * result in the lexicographically smallest, but positive, increment
1059 * in the sample point.
1060 * If there is no such column, then return tab->n_col.
1061 * If anything goes wrong, return -1.
1063 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1066 int col = tab->n_col;
1070 tr = tab->mat->row[row] + 2 + tab->M;
1074 for (j = tab->n_dead; j < tab->n_col; ++j) {
1075 if (tab->col_var[j] >= 0 &&
1076 (tab->col_var[j] < tab->n_param ||
1077 tab->col_var[j] >= tab->n_var - tab->n_div))
1080 if (!isl_int_is_pos(tr[j]))
1083 if (col == tab->n_col)
1086 col = lexmin_col_pair(tab, row, col, j, tmp);
1087 isl_assert(tab->mat->ctx, col >= 0, goto error);
1097 /* Return the first known violated constraint, i.e., a non-negative
1098 * constraint that currently has an either obviously negative value
1099 * or a previously determined to be negative value.
1101 * If any constraint has a negative coefficient for the big parameter,
1102 * if any, then we return one of these first.
1104 static int first_neg(struct isl_tab *tab)
1109 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1110 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1112 if (!isl_int_is_neg(tab->mat->row[row][2]))
1115 tab->row_sign[row] = isl_tab_row_neg;
1118 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1119 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1121 if (tab->row_sign) {
1122 if (tab->row_sign[row] == 0 &&
1123 is_obviously_neg(tab, row))
1124 tab->row_sign[row] = isl_tab_row_neg;
1125 if (tab->row_sign[row] != isl_tab_row_neg)
1127 } else if (!is_obviously_neg(tab, row))
1134 /* Check whether the invariant that all columns are lexico-positive
1135 * is satisfied. This function is not called from the current code
1136 * but is useful during debugging.
1138 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1139 static void check_lexpos(struct isl_tab *tab)
1141 unsigned off = 2 + tab->M;
1146 for (col = tab->n_dead; col < tab->n_col; ++col) {
1147 if (tab->col_var[col] >= 0 &&
1148 (tab->col_var[col] < tab->n_param ||
1149 tab->col_var[col] >= tab->n_var - tab->n_div))
1151 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1152 if (!tab->var[var].is_row) {
1153 if (tab->var[var].index == col)
1158 row = tab->var[var].index;
1159 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1161 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1163 fprintf(stderr, "lexneg column %d (row %d)\n",
1166 if (var >= tab->n_var - tab->n_div)
1167 fprintf(stderr, "zero column %d\n", col);
1171 /* Report to the caller that the given constraint is part of an encountered
1174 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1176 return tab->conflict(con, tab->conflict_user);
1179 /* Given a conflicting row in the tableau, report all constraints
1180 * involved in the row to the caller. That is, the row itself
1181 * (if it represents a constraint) and all constraint columns with
1182 * non-zero (and therefore negative) coefficients.
1184 static int report_conflict(struct isl_tab *tab, int row)
1192 if (tab->row_var[row] < 0 &&
1193 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1196 tr = tab->mat->row[row] + 2 + tab->M;
1198 for (j = tab->n_dead; j < tab->n_col; ++j) {
1199 if (tab->col_var[j] >= 0 &&
1200 (tab->col_var[j] < tab->n_param ||
1201 tab->col_var[j] >= tab->n_var - tab->n_div))
1204 if (!isl_int_is_neg(tr[j]))
1207 if (tab->col_var[j] < 0 &&
1208 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1215 /* Resolve all known or obviously violated constraints through pivoting.
1216 * In particular, as long as we can find any violated constraint, we
1217 * look for a pivoting column that would result in the lexicographically
1218 * smallest increment in the sample point. If there is no such column
1219 * then the tableau is infeasible.
1221 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1222 static int restore_lexmin(struct isl_tab *tab)
1230 while ((row = first_neg(tab)) != -1) {
1231 col = lexmin_pivot_col(tab, row);
1232 if (col >= tab->n_col) {
1233 if (report_conflict(tab, row) < 0)
1235 if (isl_tab_mark_empty(tab) < 0)
1241 if (isl_tab_pivot(tab, row, col) < 0)
1247 /* Given a row that represents an equality, look for an appropriate
1249 * In particular, if there are any non-zero coefficients among
1250 * the non-parameter variables, then we take the last of these
1251 * variables. Eliminating this variable in terms of the other
1252 * variables and/or parameters does not influence the property
1253 * that all column in the initial tableau are lexicographically
1254 * positive. The row corresponding to the eliminated variable
1255 * will only have non-zero entries below the diagonal of the
1256 * initial tableau. That is, we transform
1262 * If there is no such non-parameter variable, then we are dealing with
1263 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1264 * for elimination. This will ensure that the eliminated parameter
1265 * always has an integer value whenever all the other parameters are integral.
1266 * If there is no such parameter then we return -1.
1268 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1270 unsigned off = 2 + tab->M;
1273 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1275 if (tab->var[i].is_row)
1277 col = tab->var[i].index;
1278 if (col <= tab->n_dead)
1280 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1283 for (i = tab->n_dead; i < tab->n_col; ++i) {
1284 if (isl_int_is_one(tab->mat->row[row][off + i]))
1286 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1292 /* Add an equality that is known to be valid to the tableau.
1293 * We first check if we can eliminate a variable or a parameter.
1294 * If not, we add the equality as two inequalities.
1295 * In this case, the equality was a pure parameter equality and there
1296 * is no need to resolve any constraint violations.
1298 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1305 r = isl_tab_add_row(tab, eq);
1309 r = tab->con[r].index;
1310 i = last_var_col_or_int_par_col(tab, r);
1312 tab->con[r].is_nonneg = 1;
1313 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1315 isl_seq_neg(eq, eq, 1 + tab->n_var);
1316 r = isl_tab_add_row(tab, eq);
1319 tab->con[r].is_nonneg = 1;
1320 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1323 if (isl_tab_pivot(tab, r, i) < 0)
1325 if (isl_tab_kill_col(tab, i) < 0)
1336 /* Check if the given row is a pure constant.
1338 static int is_constant(struct isl_tab *tab, int row)
1340 unsigned off = 2 + tab->M;
1342 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1343 tab->n_col - tab->n_dead) == -1;
1346 /* Add an equality that may or may not be valid to the tableau.
1347 * If the resulting row is a pure constant, then it must be zero.
1348 * Otherwise, the resulting tableau is empty.
1350 * If the row is not a pure constant, then we add two inequalities,
1351 * each time checking that they can be satisfied.
1352 * In the end we try to use one of the two constraints to eliminate
1355 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1356 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1360 struct isl_tab_undo *snap;
1364 snap = isl_tab_snap(tab);
1365 r1 = isl_tab_add_row(tab, eq);
1368 tab->con[r1].is_nonneg = 1;
1369 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1372 row = tab->con[r1].index;
1373 if (is_constant(tab, row)) {
1374 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1375 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1376 if (isl_tab_mark_empty(tab) < 0)
1380 if (isl_tab_rollback(tab, snap) < 0)
1385 if (restore_lexmin(tab) < 0)
1390 isl_seq_neg(eq, eq, 1 + tab->n_var);
1392 r2 = isl_tab_add_row(tab, eq);
1395 tab->con[r2].is_nonneg = 1;
1396 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1399 if (restore_lexmin(tab) < 0)
1404 if (!tab->con[r1].is_row) {
1405 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1407 } else if (!tab->con[r2].is_row) {
1408 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1413 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1414 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1416 isl_seq_neg(eq, eq, 1 + tab->n_var);
1417 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1418 isl_seq_neg(eq, eq, 1 + tab->n_var);
1419 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1428 /* Add an inequality to the tableau, resolving violations using
1431 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1438 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1439 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1444 r = isl_tab_add_row(tab, ineq);
1447 tab->con[r].is_nonneg = 1;
1448 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1450 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1451 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1456 if (restore_lexmin(tab) < 0)
1458 if (!tab->empty && tab->con[r].is_row &&
1459 isl_tab_row_is_redundant(tab, tab->con[r].index))
1460 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1468 /* Check if the coefficients of the parameters are all integral.
1470 static int integer_parameter(struct isl_tab *tab, int row)
1474 unsigned off = 2 + tab->M;
1476 for (i = 0; i < tab->n_param; ++i) {
1477 /* Eliminated parameter */
1478 if (tab->var[i].is_row)
1480 col = tab->var[i].index;
1481 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1482 tab->mat->row[row][0]))
1485 for (i = 0; i < tab->n_div; ++i) {
1486 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1488 col = tab->var[tab->n_var - tab->n_div + i].index;
1489 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1490 tab->mat->row[row][0]))
1496 /* Check if the coefficients of the non-parameter variables are all integral.
1498 static int integer_variable(struct isl_tab *tab, int row)
1501 unsigned off = 2 + tab->M;
1503 for (i = tab->n_dead; i < tab->n_col; ++i) {
1504 if (tab->col_var[i] >= 0 &&
1505 (tab->col_var[i] < tab->n_param ||
1506 tab->col_var[i] >= tab->n_var - tab->n_div))
1508 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1509 tab->mat->row[row][0]))
1515 /* Check if the constant term is integral.
1517 static int integer_constant(struct isl_tab *tab, int row)
1519 return isl_int_is_divisible_by(tab->mat->row[row][1],
1520 tab->mat->row[row][0]);
1523 #define I_CST 1 << 0
1524 #define I_PAR 1 << 1
1525 #define I_VAR 1 << 2
1527 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1528 * that is non-integer and therefore requires a cut and return
1529 * the index of the variable.
1530 * For parametric tableaus, there are three parts in a row,
1531 * the constant, the coefficients of the parameters and the rest.
1532 * For each part, we check whether the coefficients in that part
1533 * are all integral and if so, set the corresponding flag in *f.
1534 * If the constant and the parameter part are integral, then the
1535 * current sample value is integral and no cut is required
1536 * (irrespective of whether the variable part is integral).
1538 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1540 var = var < 0 ? tab->n_param : var + 1;
1542 for (; var < tab->n_var - tab->n_div; ++var) {
1545 if (!tab->var[var].is_row)
1547 row = tab->var[var].index;
1548 if (integer_constant(tab, row))
1549 ISL_FL_SET(flags, I_CST);
1550 if (integer_parameter(tab, row))
1551 ISL_FL_SET(flags, I_PAR);
1552 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1554 if (integer_variable(tab, row))
1555 ISL_FL_SET(flags, I_VAR);
1562 /* Check for first (non-parameter) variable that is non-integer and
1563 * therefore requires a cut and return the corresponding row.
1564 * For parametric tableaus, there are three parts in a row,
1565 * the constant, the coefficients of the parameters and the rest.
1566 * For each part, we check whether the coefficients in that part
1567 * are all integral and if so, set the corresponding flag in *f.
1568 * If the constant and the parameter part are integral, then the
1569 * current sample value is integral and no cut is required
1570 * (irrespective of whether the variable part is integral).
1572 static int first_non_integer_row(struct isl_tab *tab, int *f)
1574 int var = next_non_integer_var(tab, -1, f);
1576 return var < 0 ? -1 : tab->var[var].index;
1579 /* Add a (non-parametric) cut to cut away the non-integral sample
1580 * value of the given row.
1582 * If the row is given by
1584 * m r = f + \sum_i a_i y_i
1588 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1590 * The big parameter, if any, is ignored, since it is assumed to be big
1591 * enough to be divisible by any integer.
1592 * If the tableau is actually a parametric tableau, then this function
1593 * is only called when all coefficients of the parameters are integral.
1594 * The cut therefore has zero coefficients for the parameters.
1596 * The current value is known to be negative, so row_sign, if it
1597 * exists, is set accordingly.
1599 * Return the row of the cut or -1.
1601 static int add_cut(struct isl_tab *tab, int row)
1606 unsigned off = 2 + tab->M;
1608 if (isl_tab_extend_cons(tab, 1) < 0)
1610 r = isl_tab_allocate_con(tab);
1614 r_row = tab->mat->row[tab->con[r].index];
1615 isl_int_set(r_row[0], tab->mat->row[row][0]);
1616 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1617 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1618 isl_int_neg(r_row[1], r_row[1]);
1620 isl_int_set_si(r_row[2], 0);
1621 for (i = 0; i < tab->n_col; ++i)
1622 isl_int_fdiv_r(r_row[off + i],
1623 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1625 tab->con[r].is_nonneg = 1;
1626 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1629 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1631 return tab->con[r].index;
1637 /* Given a non-parametric tableau, add cuts until an integer
1638 * sample point is obtained or until the tableau is determined
1639 * to be integer infeasible.
1640 * As long as there is any non-integer value in the sample point,
1641 * we add appropriate cuts, if possible, for each of these
1642 * non-integer values and then resolve the violated
1643 * cut constraints using restore_lexmin.
1644 * If one of the corresponding rows is equal to an integral
1645 * combination of variables/constraints plus a non-integral constant,
1646 * then there is no way to obtain an integer point and we return
1647 * a tableau that is marked empty.
1648 * The parameter cutting_strategy controls the strategy used when adding cuts
1649 * to remove non-integer points. CUT_ALL adds all possible cuts
1650 * before continuing the search. CUT_ONE adds only one cut at a time.
1652 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1653 int cutting_strategy)
1664 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1666 if (ISL_FL_ISSET(flags, I_VAR)) {
1667 if (isl_tab_mark_empty(tab) < 0)
1671 row = tab->var[var].index;
1672 row = add_cut(tab, row);
1675 if (cutting_strategy == CUT_ONE)
1677 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1678 if (restore_lexmin(tab) < 0)
1689 /* Check whether all the currently active samples also satisfy the inequality
1690 * "ineq" (treated as an equality if eq is set).
1691 * Remove those samples that do not.
1693 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1701 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1702 isl_assert(tab->mat->ctx, tab->samples, goto error);
1703 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1706 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1708 isl_seq_inner_product(ineq, tab->samples->row[i],
1709 1 + tab->n_var, &v);
1710 sgn = isl_int_sgn(v);
1711 if (eq ? (sgn == 0) : (sgn >= 0))
1713 tab = isl_tab_drop_sample(tab, i);
1725 /* Check whether the sample value of the tableau is finite,
1726 * i.e., either the tableau does not use a big parameter, or
1727 * all values of the variables are equal to the big parameter plus
1728 * some constant. This constant is the actual sample value.
1730 static int sample_is_finite(struct isl_tab *tab)
1737 for (i = 0; i < tab->n_var; ++i) {
1739 if (!tab->var[i].is_row)
1741 row = tab->var[i].index;
1742 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1748 /* Check if the context tableau of sol has any integer points.
1749 * Leave tab in empty state if no integer point can be found.
1750 * If an integer point can be found and if moreover it is finite,
1751 * then it is added to the list of sample values.
1753 * This function is only called when none of the currently active sample
1754 * values satisfies the most recently added constraint.
1756 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1758 struct isl_tab_undo *snap;
1763 snap = isl_tab_snap(tab);
1764 if (isl_tab_push_basis(tab) < 0)
1767 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1771 if (!tab->empty && sample_is_finite(tab)) {
1772 struct isl_vec *sample;
1774 sample = isl_tab_get_sample_value(tab);
1776 tab = isl_tab_add_sample(tab, sample);
1779 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1788 /* Check if any of the currently active sample values satisfies
1789 * the inequality "ineq" (an equality if eq is set).
1791 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1799 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1800 isl_assert(tab->mat->ctx, tab->samples, return -1);
1801 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1804 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1806 isl_seq_inner_product(ineq, tab->samples->row[i],
1807 1 + tab->n_var, &v);
1808 sgn = isl_int_sgn(v);
1809 if (eq ? (sgn == 0) : (sgn >= 0))
1814 return i < tab->n_sample;
1817 /* Add a div specified by "div" to the tableau "tab" and return
1818 * 1 if the div is obviously non-negative.
1820 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1821 int (*add_ineq)(void *user, isl_int *), void *user)
1825 struct isl_mat *samples;
1828 r = isl_tab_add_div(tab, div, add_ineq, user);
1831 nonneg = tab->var[r].is_nonneg;
1832 tab->var[r].frozen = 1;
1834 samples = isl_mat_extend(tab->samples,
1835 tab->n_sample, 1 + tab->n_var);
1836 tab->samples = samples;
1839 for (i = tab->n_outside; i < samples->n_row; ++i) {
1840 isl_seq_inner_product(div->el + 1, samples->row[i],
1841 div->size - 1, &samples->row[i][samples->n_col - 1]);
1842 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1843 samples->row[i][samples->n_col - 1], div->el[0]);
1849 /* Add a div specified by "div" to both the main tableau and
1850 * the context tableau. In case of the main tableau, we only
1851 * need to add an extra div. In the context tableau, we also
1852 * need to express the meaning of the div.
1853 * Return the index of the div or -1 if anything went wrong.
1855 static int add_div(struct isl_tab *tab, struct isl_context *context,
1856 struct isl_vec *div)
1861 if ((nonneg = context->op->add_div(context, div)) < 0)
1864 if (!context->op->is_ok(context))
1867 if (isl_tab_extend_vars(tab, 1) < 0)
1869 r = isl_tab_allocate_var(tab);
1873 tab->var[r].is_nonneg = 1;
1874 tab->var[r].frozen = 1;
1877 return tab->n_div - 1;
1879 context->op->invalidate(context);
1883 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1886 unsigned total = isl_basic_map_total_dim(tab->bmap);
1888 for (i = 0; i < tab->bmap->n_div; ++i) {
1889 if (isl_int_ne(tab->bmap->div[i][0], denom))
1891 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1898 /* Return the index of a div that corresponds to "div".
1899 * We first check if we already have such a div and if not, we create one.
1901 static int get_div(struct isl_tab *tab, struct isl_context *context,
1902 struct isl_vec *div)
1905 struct isl_tab *context_tab = context->op->peek_tab(context);
1910 d = find_div(context_tab, div->el + 1, div->el[0]);
1914 return add_div(tab, context, div);
1917 /* Add a parametric cut to cut away the non-integral sample value
1919 * Let a_i be the coefficients of the constant term and the parameters
1920 * and let b_i be the coefficients of the variables or constraints
1921 * in basis of the tableau.
1922 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1924 * The cut is expressed as
1926 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1928 * If q did not already exist in the context tableau, then it is added first.
1929 * If q is in a column of the main tableau then the "+ q" can be accomplished
1930 * by setting the corresponding entry to the denominator of the constraint.
1931 * If q happens to be in a row of the main tableau, then the corresponding
1932 * row needs to be added instead (taking care of the denominators).
1933 * Note that this is very unlikely, but perhaps not entirely impossible.
1935 * The current value of the cut is known to be negative (or at least
1936 * non-positive), so row_sign is set accordingly.
1938 * Return the row of the cut or -1.
1940 static int add_parametric_cut(struct isl_tab *tab, int row,
1941 struct isl_context *context)
1943 struct isl_vec *div;
1950 unsigned off = 2 + tab->M;
1955 div = get_row_parameter_div(tab, row);
1960 d = context->op->get_div(context, tab, div);
1964 if (isl_tab_extend_cons(tab, 1) < 0)
1966 r = isl_tab_allocate_con(tab);
1970 r_row = tab->mat->row[tab->con[r].index];
1971 isl_int_set(r_row[0], tab->mat->row[row][0]);
1972 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1973 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1974 isl_int_neg(r_row[1], r_row[1]);
1976 isl_int_set_si(r_row[2], 0);
1977 for (i = 0; i < tab->n_param; ++i) {
1978 if (tab->var[i].is_row)
1980 col = tab->var[i].index;
1981 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1982 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1983 tab->mat->row[row][0]);
1984 isl_int_neg(r_row[off + col], r_row[off + col]);
1986 for (i = 0; i < tab->n_div; ++i) {
1987 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1989 col = tab->var[tab->n_var - tab->n_div + i].index;
1990 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1991 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1992 tab->mat->row[row][0]);
1993 isl_int_neg(r_row[off + col], r_row[off + col]);
1995 for (i = 0; i < tab->n_col; ++i) {
1996 if (tab->col_var[i] >= 0 &&
1997 (tab->col_var[i] < tab->n_param ||
1998 tab->col_var[i] >= tab->n_var - tab->n_div))
2000 isl_int_fdiv_r(r_row[off + i],
2001 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2003 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2005 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2007 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2008 isl_int_divexact(r_row[0], r_row[0], gcd);
2009 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2010 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2011 r_row[0], tab->mat->row[d_row] + 1,
2012 off - 1 + tab->n_col);
2013 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2016 col = tab->var[tab->n_var - tab->n_div + d].index;
2017 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2020 tab->con[r].is_nonneg = 1;
2021 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2024 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2028 row = tab->con[r].index;
2030 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2036 /* Construct a tableau for bmap that can be used for computing
2037 * the lexicographic minimum (or maximum) of bmap.
2038 * If not NULL, then dom is the domain where the minimum
2039 * should be computed. In this case, we set up a parametric
2040 * tableau with row signs (initialized to "unknown").
2041 * If M is set, then the tableau will use a big parameter.
2042 * If max is set, then a maximum should be computed instead of a minimum.
2043 * This means that for each variable x, the tableau will contain the variable
2044 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2045 * of the variables in all constraints are negated prior to adding them
2048 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2049 struct isl_basic_set *dom, unsigned M, int max)
2052 struct isl_tab *tab;
2054 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2055 isl_basic_map_total_dim(bmap), M);
2059 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2061 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2062 tab->n_div = dom->n_div;
2063 tab->row_sign = isl_calloc_array(bmap->ctx,
2064 enum isl_tab_row_sign, tab->mat->n_row);
2068 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2069 if (isl_tab_mark_empty(tab) < 0)
2074 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2075 tab->var[i].is_nonneg = 1;
2076 tab->var[i].frozen = 1;
2078 for (i = 0; i < bmap->n_eq; ++i) {
2080 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2081 bmap->eq[i] + 1 + tab->n_param,
2082 tab->n_var - tab->n_param - tab->n_div);
2083 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2085 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2086 bmap->eq[i] + 1 + tab->n_param,
2087 tab->n_var - tab->n_param - tab->n_div);
2088 if (!tab || tab->empty)
2091 if (bmap->n_eq && restore_lexmin(tab) < 0)
2093 for (i = 0; i < bmap->n_ineq; ++i) {
2095 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2096 bmap->ineq[i] + 1 + tab->n_param,
2097 tab->n_var - tab->n_param - tab->n_div);
2098 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2100 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2101 bmap->ineq[i] + 1 + tab->n_param,
2102 tab->n_var - tab->n_param - tab->n_div);
2103 if (!tab || tab->empty)
2112 /* Given a main tableau where more than one row requires a split,
2113 * determine and return the "best" row to split on.
2115 * Given two rows in the main tableau, if the inequality corresponding
2116 * to the first row is redundant with respect to that of the second row
2117 * in the current tableau, then it is better to split on the second row,
2118 * since in the positive part, both row will be positive.
2119 * (In the negative part a pivot will have to be performed and just about
2120 * anything can happen to the sign of the other row.)
2122 * As a simple heuristic, we therefore select the row that makes the most
2123 * of the other rows redundant.
2125 * Perhaps it would also be useful to look at the number of constraints
2126 * that conflict with any given constraint.
2128 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2130 struct isl_tab_undo *snap;
2136 if (isl_tab_extend_cons(context_tab, 2) < 0)
2139 snap = isl_tab_snap(context_tab);
2141 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2142 struct isl_tab_undo *snap2;
2143 struct isl_vec *ineq = NULL;
2147 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2149 if (tab->row_sign[split] != isl_tab_row_any)
2152 ineq = get_row_parameter_ineq(tab, split);
2155 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2160 snap2 = isl_tab_snap(context_tab);
2162 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2163 struct isl_tab_var *var;
2167 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2169 if (tab->row_sign[row] != isl_tab_row_any)
2172 ineq = get_row_parameter_ineq(tab, row);
2175 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2179 var = &context_tab->con[context_tab->n_con - 1];
2180 if (!context_tab->empty &&
2181 !isl_tab_min_at_most_neg_one(context_tab, var))
2183 if (isl_tab_rollback(context_tab, snap2) < 0)
2186 if (best == -1 || r > best_r) {
2190 if (isl_tab_rollback(context_tab, snap) < 0)
2197 static struct isl_basic_set *context_lex_peek_basic_set(
2198 struct isl_context *context)
2200 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2203 return isl_tab_peek_bset(clex->tab);
2206 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2208 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2212 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2213 int check, int update)
2215 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2216 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2218 if (add_lexmin_eq(clex->tab, eq) < 0)
2221 int v = tab_has_valid_sample(clex->tab, eq, 1);
2225 clex->tab = check_integer_feasible(clex->tab);
2228 clex->tab = check_samples(clex->tab, eq, 1);
2231 isl_tab_free(clex->tab);
2235 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2236 int check, int update)
2238 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2239 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2241 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2243 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2247 clex->tab = check_integer_feasible(clex->tab);
2250 clex->tab = check_samples(clex->tab, ineq, 0);
2253 isl_tab_free(clex->tab);
2257 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2259 struct isl_context *context = (struct isl_context *)user;
2260 context_lex_add_ineq(context, ineq, 0, 0);
2261 return context->op->is_ok(context) ? 0 : -1;
2264 /* Check which signs can be obtained by "ineq" on all the currently
2265 * active sample values. See row_sign for more information.
2267 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2273 enum isl_tab_row_sign res = isl_tab_row_unknown;
2275 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2276 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2277 return isl_tab_row_unknown);
2280 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2281 isl_seq_inner_product(tab->samples->row[i], ineq,
2282 1 + tab->n_var, &tmp);
2283 sgn = isl_int_sgn(tmp);
2284 if (sgn > 0 || (sgn == 0 && strict)) {
2285 if (res == isl_tab_row_unknown)
2286 res = isl_tab_row_pos;
2287 if (res == isl_tab_row_neg)
2288 res = isl_tab_row_any;
2291 if (res == isl_tab_row_unknown)
2292 res = isl_tab_row_neg;
2293 if (res == isl_tab_row_pos)
2294 res = isl_tab_row_any;
2296 if (res == isl_tab_row_any)
2304 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2305 isl_int *ineq, int strict)
2307 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2308 return tab_ineq_sign(clex->tab, ineq, strict);
2311 /* Check whether "ineq" can be added to the tableau without rendering
2314 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2316 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2317 struct isl_tab_undo *snap;
2323 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2326 snap = isl_tab_snap(clex->tab);
2327 if (isl_tab_push_basis(clex->tab) < 0)
2329 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2330 clex->tab = check_integer_feasible(clex->tab);
2333 feasible = !clex->tab->empty;
2334 if (isl_tab_rollback(clex->tab, snap) < 0)
2340 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2341 struct isl_vec *div)
2343 return get_div(tab, context, div);
2346 /* Add a div specified by "div" to the context tableau and return
2347 * 1 if the div is obviously non-negative.
2348 * context_tab_add_div will always return 1, because all variables
2349 * in a isl_context_lex tableau are non-negative.
2350 * However, if we are using a big parameter in the context, then this only
2351 * reflects the non-negativity of the variable used to _encode_ the
2352 * div, i.e., div' = M + div, so we can't draw any conclusions.
2354 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2356 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2358 nonneg = context_tab_add_div(clex->tab, div,
2359 context_lex_add_ineq_wrap, context);
2367 static int context_lex_detect_equalities(struct isl_context *context,
2368 struct isl_tab *tab)
2373 static int context_lex_best_split(struct isl_context *context,
2374 struct isl_tab *tab)
2376 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2377 struct isl_tab_undo *snap;
2380 snap = isl_tab_snap(clex->tab);
2381 if (isl_tab_push_basis(clex->tab) < 0)
2383 r = best_split(tab, clex->tab);
2385 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2391 static int context_lex_is_empty(struct isl_context *context)
2393 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2396 return clex->tab->empty;
2399 static void *context_lex_save(struct isl_context *context)
2401 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2402 struct isl_tab_undo *snap;
2404 snap = isl_tab_snap(clex->tab);
2405 if (isl_tab_push_basis(clex->tab) < 0)
2407 if (isl_tab_save_samples(clex->tab) < 0)
2413 static void context_lex_restore(struct isl_context *context, void *save)
2415 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2416 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2417 isl_tab_free(clex->tab);
2422 static int context_lex_is_ok(struct isl_context *context)
2424 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2428 /* For each variable in the context tableau, check if the variable can
2429 * only attain non-negative values. If so, mark the parameter as non-negative
2430 * in the main tableau. This allows for a more direct identification of some
2431 * cases of violated constraints.
2433 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2434 struct isl_tab *context_tab)
2437 struct isl_tab_undo *snap;
2438 struct isl_vec *ineq = NULL;
2439 struct isl_tab_var *var;
2442 if (context_tab->n_var == 0)
2445 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2449 if (isl_tab_extend_cons(context_tab, 1) < 0)
2452 snap = isl_tab_snap(context_tab);
2455 isl_seq_clr(ineq->el, ineq->size);
2456 for (i = 0; i < context_tab->n_var; ++i) {
2457 isl_int_set_si(ineq->el[1 + i], 1);
2458 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2460 var = &context_tab->con[context_tab->n_con - 1];
2461 if (!context_tab->empty &&
2462 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2464 if (i >= tab->n_param)
2465 j = i - tab->n_param + tab->n_var - tab->n_div;
2466 tab->var[j].is_nonneg = 1;
2469 isl_int_set_si(ineq->el[1 + i], 0);
2470 if (isl_tab_rollback(context_tab, snap) < 0)
2474 if (context_tab->M && n == context_tab->n_var) {
2475 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2487 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2488 struct isl_context *context, struct isl_tab *tab)
2490 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2491 struct isl_tab_undo *snap;
2496 snap = isl_tab_snap(clex->tab);
2497 if (isl_tab_push_basis(clex->tab) < 0)
2500 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2502 if (isl_tab_rollback(clex->tab, snap) < 0)
2511 static void context_lex_invalidate(struct isl_context *context)
2513 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2514 isl_tab_free(clex->tab);
2518 static void context_lex_free(struct isl_context *context)
2520 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2521 isl_tab_free(clex->tab);
2525 struct isl_context_op isl_context_lex_op = {
2526 context_lex_detect_nonnegative_parameters,
2527 context_lex_peek_basic_set,
2528 context_lex_peek_tab,
2530 context_lex_add_ineq,
2531 context_lex_ineq_sign,
2532 context_lex_test_ineq,
2533 context_lex_get_div,
2534 context_lex_add_div,
2535 context_lex_detect_equalities,
2536 context_lex_best_split,
2537 context_lex_is_empty,
2540 context_lex_restore,
2541 context_lex_invalidate,
2545 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2547 struct isl_tab *tab;
2551 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2554 if (isl_tab_track_bset(tab, bset) < 0)
2556 tab = isl_tab_init_samples(tab);
2559 isl_basic_set_free(bset);
2563 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2565 struct isl_context_lex *clex;
2570 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2574 clex->context.op = &isl_context_lex_op;
2576 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2577 if (restore_lexmin(clex->tab) < 0)
2579 clex->tab = check_integer_feasible(clex->tab);
2583 return &clex->context;
2585 clex->context.op->free(&clex->context);
2589 struct isl_context_gbr {
2590 struct isl_context context;
2591 struct isl_tab *tab;
2592 struct isl_tab *shifted;
2593 struct isl_tab *cone;
2596 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2597 struct isl_context *context, struct isl_tab *tab)
2599 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2602 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2605 static struct isl_basic_set *context_gbr_peek_basic_set(
2606 struct isl_context *context)
2608 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2611 return isl_tab_peek_bset(cgbr->tab);
2614 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2616 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2620 /* Initialize the "shifted" tableau of the context, which
2621 * contains the constraints of the original tableau shifted
2622 * by the sum of all negative coefficients. This ensures
2623 * that any rational point in the shifted tableau can
2624 * be rounded up to yield an integer point in the original tableau.
2626 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2629 struct isl_vec *cst;
2630 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2631 unsigned dim = isl_basic_set_total_dim(bset);
2633 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2637 for (i = 0; i < bset->n_ineq; ++i) {
2638 isl_int_set(cst->el[i], bset->ineq[i][0]);
2639 for (j = 0; j < dim; ++j) {
2640 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2642 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2643 bset->ineq[i][1 + j]);
2647 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2649 for (i = 0; i < bset->n_ineq; ++i)
2650 isl_int_set(bset->ineq[i][0], cst->el[i]);
2655 /* Check if the shifted tableau is non-empty, and if so
2656 * use the sample point to construct an integer point
2657 * of the context tableau.
2659 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2661 struct isl_vec *sample;
2664 gbr_init_shifted(cgbr);
2667 if (cgbr->shifted->empty)
2668 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2670 sample = isl_tab_get_sample_value(cgbr->shifted);
2671 sample = isl_vec_ceil(sample);
2676 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2683 for (i = 0; i < bset->n_eq; ++i)
2684 isl_int_set_si(bset->eq[i][0], 0);
2686 for (i = 0; i < bset->n_ineq; ++i)
2687 isl_int_set_si(bset->ineq[i][0], 0);
2692 static int use_shifted(struct isl_context_gbr *cgbr)
2694 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2697 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2699 struct isl_basic_set *bset;
2700 struct isl_basic_set *cone;
2702 if (isl_tab_sample_is_integer(cgbr->tab))
2703 return isl_tab_get_sample_value(cgbr->tab);
2705 if (use_shifted(cgbr)) {
2706 struct isl_vec *sample;
2708 sample = gbr_get_shifted_sample(cgbr);
2709 if (!sample || sample->size > 0)
2712 isl_vec_free(sample);
2716 bset = isl_tab_peek_bset(cgbr->tab);
2717 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2720 if (isl_tab_track_bset(cgbr->cone,
2721 isl_basic_set_copy(bset)) < 0)
2724 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2727 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2728 struct isl_vec *sample;
2729 struct isl_tab_undo *snap;
2731 if (cgbr->tab->basis) {
2732 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2733 isl_mat_free(cgbr->tab->basis);
2734 cgbr->tab->basis = NULL;
2736 cgbr->tab->n_zero = 0;
2737 cgbr->tab->n_unbounded = 0;
2740 snap = isl_tab_snap(cgbr->tab);
2742 sample = isl_tab_sample(cgbr->tab);
2744 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2745 isl_vec_free(sample);
2752 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2753 cone = drop_constant_terms(cone);
2754 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2755 cone = isl_basic_set_underlying_set(cone);
2756 cone = isl_basic_set_gauss(cone, NULL);
2758 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2759 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2760 bset = isl_basic_set_underlying_set(bset);
2761 bset = isl_basic_set_gauss(bset, NULL);
2763 return isl_basic_set_sample_with_cone(bset, cone);
2766 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2768 struct isl_vec *sample;
2773 if (cgbr->tab->empty)
2776 sample = gbr_get_sample(cgbr);
2780 if (sample->size == 0) {
2781 isl_vec_free(sample);
2782 if (isl_tab_mark_empty(cgbr->tab) < 0)
2787 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2791 isl_tab_free(cgbr->tab);
2795 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2800 if (isl_tab_extend_cons(tab, 2) < 0)
2803 if (isl_tab_add_eq(tab, eq) < 0)
2812 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2813 int check, int update)
2815 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2817 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2819 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2820 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2822 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2827 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2831 check_gbr_integer_feasible(cgbr);
2834 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2837 isl_tab_free(cgbr->tab);
2841 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2846 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2849 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2852 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2855 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2857 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2860 for (i = 0; i < dim; ++i) {
2861 if (!isl_int_is_neg(ineq[1 + i]))
2863 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2866 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2869 for (i = 0; i < dim; ++i) {
2870 if (!isl_int_is_neg(ineq[1 + i]))
2872 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2876 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2877 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2879 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2885 isl_tab_free(cgbr->tab);
2889 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2890 int check, int update)
2892 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2894 add_gbr_ineq(cgbr, ineq);
2899 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2903 check_gbr_integer_feasible(cgbr);
2906 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2909 isl_tab_free(cgbr->tab);
2913 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2915 struct isl_context *context = (struct isl_context *)user;
2916 context_gbr_add_ineq(context, ineq, 0, 0);
2917 return context->op->is_ok(context) ? 0 : -1;
2920 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2921 isl_int *ineq, int strict)
2923 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2924 return tab_ineq_sign(cgbr->tab, ineq, strict);
2927 /* Check whether "ineq" can be added to the tableau without rendering
2930 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2932 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2933 struct isl_tab_undo *snap;
2934 struct isl_tab_undo *shifted_snap = NULL;
2935 struct isl_tab_undo *cone_snap = NULL;
2941 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2944 snap = isl_tab_snap(cgbr->tab);
2946 shifted_snap = isl_tab_snap(cgbr->shifted);
2948 cone_snap = isl_tab_snap(cgbr->cone);
2949 add_gbr_ineq(cgbr, ineq);
2950 check_gbr_integer_feasible(cgbr);
2953 feasible = !cgbr->tab->empty;
2954 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2957 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2959 } else if (cgbr->shifted) {
2960 isl_tab_free(cgbr->shifted);
2961 cgbr->shifted = NULL;
2964 if (isl_tab_rollback(cgbr->cone, cone_snap))
2966 } else if (cgbr->cone) {
2967 isl_tab_free(cgbr->cone);
2974 /* Return the column of the last of the variables associated to
2975 * a column that has a non-zero coefficient.
2976 * This function is called in a context where only coefficients
2977 * of parameters or divs can be non-zero.
2979 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
2984 if (tab->n_var == 0)
2987 for (i = tab->n_var - 1; i >= 0; --i) {
2988 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
2990 if (tab->var[i].is_row)
2992 col = tab->var[i].index;
2993 if (!isl_int_is_zero(p[col]))
3000 /* Look through all the recently added equalities in the context
3001 * to see if we can propagate any of them to the main tableau.
3003 * The newly added equalities in the context are encoded as pairs
3004 * of inequalities starting at inequality "first".
3006 * We tentatively add each of these equalities to the main tableau
3007 * and if this happens to result in a row with a final coefficient
3008 * that is one or negative one, we use it to kill a column
3009 * in the main tableau. Otherwise, we discard the tentatively
3012 static void propagate_equalities(struct isl_context_gbr *cgbr,
3013 struct isl_tab *tab, unsigned first)
3016 struct isl_vec *eq = NULL;
3018 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3022 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3025 isl_seq_clr(eq->el + 1 + tab->n_param,
3026 tab->n_var - tab->n_param - tab->n_div);
3027 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3030 struct isl_tab_undo *snap;
3031 snap = isl_tab_snap(tab);
3033 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3034 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3035 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3038 r = isl_tab_add_row(tab, eq->el);
3041 r = tab->con[r].index;
3042 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3043 if (j < 0 || j < tab->n_dead ||
3044 !isl_int_is_one(tab->mat->row[r][0]) ||
3045 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3046 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3047 if (isl_tab_rollback(tab, snap) < 0)
3051 if (isl_tab_pivot(tab, r, j) < 0)
3053 if (isl_tab_kill_col(tab, j) < 0)
3056 if (restore_lexmin(tab) < 0)
3065 isl_tab_free(cgbr->tab);
3069 static int context_gbr_detect_equalities(struct isl_context *context,
3070 struct isl_tab *tab)
3072 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3073 struct isl_ctx *ctx;
3076 ctx = cgbr->tab->mat->ctx;
3079 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3080 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3083 if (isl_tab_track_bset(cgbr->cone,
3084 isl_basic_set_copy(bset)) < 0)
3087 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3090 n_ineq = cgbr->tab->bmap->n_ineq;
3091 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3092 if (cgbr->tab && cgbr->tab->bmap->n_ineq > n_ineq)
3093 propagate_equalities(cgbr, tab, n_ineq);
3097 isl_tab_free(cgbr->tab);
3102 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3103 struct isl_vec *div)
3105 return get_div(tab, context, div);
3108 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3110 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3114 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3116 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3118 if (isl_tab_allocate_var(cgbr->cone) <0)
3121 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3122 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3123 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3126 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3127 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3130 return context_tab_add_div(cgbr->tab, div,
3131 context_gbr_add_ineq_wrap, context);
3134 static int context_gbr_best_split(struct isl_context *context,
3135 struct isl_tab *tab)
3137 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3138 struct isl_tab_undo *snap;
3141 snap = isl_tab_snap(cgbr->tab);
3142 r = best_split(tab, cgbr->tab);
3144 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3150 static int context_gbr_is_empty(struct isl_context *context)
3152 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3155 return cgbr->tab->empty;
3158 struct isl_gbr_tab_undo {
3159 struct isl_tab_undo *tab_snap;
3160 struct isl_tab_undo *shifted_snap;
3161 struct isl_tab_undo *cone_snap;
3164 static void *context_gbr_save(struct isl_context *context)
3166 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3167 struct isl_gbr_tab_undo *snap;
3169 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3173 snap->tab_snap = isl_tab_snap(cgbr->tab);
3174 if (isl_tab_save_samples(cgbr->tab) < 0)
3178 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3180 snap->shifted_snap = NULL;
3183 snap->cone_snap = isl_tab_snap(cgbr->cone);
3185 snap->cone_snap = NULL;
3193 static void context_gbr_restore(struct isl_context *context, void *save)
3195 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3196 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3199 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3200 isl_tab_free(cgbr->tab);
3204 if (snap->shifted_snap) {
3205 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3207 } else if (cgbr->shifted) {
3208 isl_tab_free(cgbr->shifted);
3209 cgbr->shifted = NULL;
3212 if (snap->cone_snap) {
3213 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3215 } else if (cgbr->cone) {
3216 isl_tab_free(cgbr->cone);
3225 isl_tab_free(cgbr->tab);
3229 static int context_gbr_is_ok(struct isl_context *context)
3231 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3235 static void context_gbr_invalidate(struct isl_context *context)
3237 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3238 isl_tab_free(cgbr->tab);
3242 static void context_gbr_free(struct isl_context *context)
3244 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3245 isl_tab_free(cgbr->tab);
3246 isl_tab_free(cgbr->shifted);
3247 isl_tab_free(cgbr->cone);
3251 struct isl_context_op isl_context_gbr_op = {
3252 context_gbr_detect_nonnegative_parameters,
3253 context_gbr_peek_basic_set,
3254 context_gbr_peek_tab,
3256 context_gbr_add_ineq,
3257 context_gbr_ineq_sign,
3258 context_gbr_test_ineq,
3259 context_gbr_get_div,
3260 context_gbr_add_div,
3261 context_gbr_detect_equalities,
3262 context_gbr_best_split,
3263 context_gbr_is_empty,
3266 context_gbr_restore,
3267 context_gbr_invalidate,
3271 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3273 struct isl_context_gbr *cgbr;
3278 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3282 cgbr->context.op = &isl_context_gbr_op;
3284 cgbr->shifted = NULL;
3286 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3287 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3290 check_gbr_integer_feasible(cgbr);
3292 return &cgbr->context;
3294 cgbr->context.op->free(&cgbr->context);
3298 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3303 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3304 return isl_context_lex_alloc(dom);
3306 return isl_context_gbr_alloc(dom);
3309 /* Construct an isl_sol_map structure for accumulating the solution.
3310 * If track_empty is set, then we also keep track of the parts
3311 * of the context where there is no solution.
3312 * If max is set, then we are solving a maximization, rather than
3313 * a minimization problem, which means that the variables in the
3314 * tableau have value "M - x" rather than "M + x".
3316 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3317 struct isl_basic_set *dom, int track_empty, int max)
3319 struct isl_sol_map *sol_map = NULL;
3324 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3328 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3329 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3330 sol_map->sol.dec_level.sol = &sol_map->sol;
3331 sol_map->sol.max = max;
3332 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3333 sol_map->sol.add = &sol_map_add_wrap;
3334 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3335 sol_map->sol.free = &sol_map_free_wrap;
3336 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3341 sol_map->sol.context = isl_context_alloc(dom);
3342 if (!sol_map->sol.context)
3346 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3347 1, ISL_SET_DISJOINT);
3348 if (!sol_map->empty)
3352 isl_basic_set_free(dom);
3353 return &sol_map->sol;
3355 isl_basic_set_free(dom);
3356 sol_map_free(sol_map);
3360 /* Check whether all coefficients of (non-parameter) variables
3361 * are non-positive, meaning that no pivots can be performed on the row.
3363 static int is_critical(struct isl_tab *tab, int row)
3366 unsigned off = 2 + tab->M;
3368 for (j = tab->n_dead; j < tab->n_col; ++j) {
3369 if (tab->col_var[j] >= 0 &&
3370 (tab->col_var[j] < tab->n_param ||
3371 tab->col_var[j] >= tab->n_var - tab->n_div))
3374 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3381 /* Check whether the inequality represented by vec is strict over the integers,
3382 * i.e., there are no integer values satisfying the constraint with
3383 * equality. This happens if the gcd of the coefficients is not a divisor
3384 * of the constant term. If so, scale the constraint down by the gcd
3385 * of the coefficients.
3387 static int is_strict(struct isl_vec *vec)
3393 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3394 if (!isl_int_is_one(gcd)) {
3395 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3396 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3397 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3404 /* Determine the sign of the given row of the main tableau.
3405 * The result is one of
3406 * isl_tab_row_pos: always non-negative; no pivot needed
3407 * isl_tab_row_neg: always non-positive; pivot
3408 * isl_tab_row_any: can be both positive and negative; split
3410 * We first handle some simple cases
3411 * - the row sign may be known already
3412 * - the row may be obviously non-negative
3413 * - the parametric constant may be equal to that of another row
3414 * for which we know the sign. This sign will be either "pos" or
3415 * "any". If it had been "neg" then we would have pivoted before.
3417 * If none of these cases hold, we check the value of the row for each
3418 * of the currently active samples. Based on the signs of these values
3419 * we make an initial determination of the sign of the row.
3421 * all zero -> unk(nown)
3422 * all non-negative -> pos
3423 * all non-positive -> neg
3424 * both negative and positive -> all
3426 * If we end up with "all", we are done.
3427 * Otherwise, we perform a check for positive and/or negative
3428 * values as follows.
3430 * samples neg unk pos
3436 * There is no special sign for "zero", because we can usually treat zero
3437 * as either non-negative or non-positive, whatever works out best.
3438 * However, if the row is "critical", meaning that pivoting is impossible
3439 * then we don't want to limp zero with the non-positive case, because
3440 * then we we would lose the solution for those values of the parameters
3441 * where the value of the row is zero. Instead, we treat 0 as non-negative
3442 * ensuring a split if the row can attain both zero and negative values.
3443 * The same happens when the original constraint was one that could not
3444 * be satisfied with equality by any integer values of the parameters.
3445 * In this case, we normalize the constraint, but then a value of zero
3446 * for the normalized constraint is actually a positive value for the
3447 * original constraint, so again we need to treat zero as non-negative.
3448 * In both these cases, we have the following decision tree instead:
3450 * all non-negative -> pos
3451 * all negative -> neg
3452 * both negative and non-negative -> all
3460 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3461 struct isl_sol *sol, int row)
3463 struct isl_vec *ineq = NULL;
3464 enum isl_tab_row_sign res = isl_tab_row_unknown;
3469 if (tab->row_sign[row] != isl_tab_row_unknown)
3470 return tab->row_sign[row];
3471 if (is_obviously_nonneg(tab, row))
3472 return isl_tab_row_pos;
3473 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3474 if (tab->row_sign[row2] == isl_tab_row_unknown)
3476 if (identical_parameter_line(tab, row, row2))
3477 return tab->row_sign[row2];
3480 critical = is_critical(tab, row);
3482 ineq = get_row_parameter_ineq(tab, row);
3486 strict = is_strict(ineq);
3488 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3489 critical || strict);
3491 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3492 /* test for negative values */
3494 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3495 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3497 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3501 res = isl_tab_row_pos;
3503 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3505 if (res == isl_tab_row_neg) {
3506 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3507 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3511 if (res == isl_tab_row_neg) {
3512 /* test for positive values */
3514 if (!critical && !strict)
3515 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3517 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3521 res = isl_tab_row_any;
3528 return isl_tab_row_unknown;
3531 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3533 /* Find solutions for values of the parameters that satisfy the given
3536 * We currently take a snapshot of the context tableau that is reset
3537 * when we return from this function, while we make a copy of the main
3538 * tableau, leaving the original main tableau untouched.
3539 * These are fairly arbitrary choices. Making a copy also of the context
3540 * tableau would obviate the need to undo any changes made to it later,
3541 * while taking a snapshot of the main tableau could reduce memory usage.
3542 * If we were to switch to taking a snapshot of the main tableau,
3543 * we would have to keep in mind that we need to save the row signs
3544 * and that we need to do this before saving the current basis
3545 * such that the basis has been restore before we restore the row signs.
3547 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3553 saved = sol->context->op->save(sol->context);
3555 tab = isl_tab_dup(tab);
3559 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3561 find_solutions(sol, tab);
3564 sol->context->op->restore(sol->context, saved);
3570 /* Record the absence of solutions for those values of the parameters
3571 * that do not satisfy the given inequality with equality.
3573 static void no_sol_in_strict(struct isl_sol *sol,
3574 struct isl_tab *tab, struct isl_vec *ineq)
3579 if (!sol->context || sol->error)
3581 saved = sol->context->op->save(sol->context);
3583 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3585 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3594 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3596 sol->context->op->restore(sol->context, saved);
3602 /* Compute the lexicographic minimum of the set represented by the main
3603 * tableau "tab" within the context "sol->context_tab".
3604 * On entry the sample value of the main tableau is lexicographically
3605 * less than or equal to this lexicographic minimum.
3606 * Pivots are performed until a feasible point is found, which is then
3607 * necessarily equal to the minimum, or until the tableau is found to
3608 * be infeasible. Some pivots may need to be performed for only some
3609 * feasible values of the context tableau. If so, the context tableau
3610 * is split into a part where the pivot is needed and a part where it is not.
3612 * Whenever we enter the main loop, the main tableau is such that no
3613 * "obvious" pivots need to be performed on it, where "obvious" means
3614 * that the given row can be seen to be negative without looking at
3615 * the context tableau. In particular, for non-parametric problems,
3616 * no pivots need to be performed on the main tableau.
3617 * The caller of find_solutions is responsible for making this property
3618 * hold prior to the first iteration of the loop, while restore_lexmin
3619 * is called before every other iteration.
3621 * Inside the main loop, we first examine the signs of the rows of
3622 * the main tableau within the context of the context tableau.
3623 * If we find a row that is always non-positive for all values of
3624 * the parameters satisfying the context tableau and negative for at
3625 * least one value of the parameters, we perform the appropriate pivot
3626 * and start over. An exception is the case where no pivot can be
3627 * performed on the row. In this case, we require that the sign of
3628 * the row is negative for all values of the parameters (rather than just
3629 * non-positive). This special case is handled inside row_sign, which
3630 * will say that the row can have any sign if it determines that it can
3631 * attain both negative and zero values.
3633 * If we can't find a row that always requires a pivot, but we can find
3634 * one or more rows that require a pivot for some values of the parameters
3635 * (i.e., the row can attain both positive and negative signs), then we split
3636 * the context tableau into two parts, one where we force the sign to be
3637 * non-negative and one where we force is to be negative.
3638 * The non-negative part is handled by a recursive call (through find_in_pos).
3639 * Upon returning from this call, we continue with the negative part and
3640 * perform the required pivot.
3642 * If no such rows can be found, all rows are non-negative and we have
3643 * found a (rational) feasible point. If we only wanted a rational point
3645 * Otherwise, we check if all values of the sample point of the tableau
3646 * are integral for the variables. If so, we have found the minimal
3647 * integral point and we are done.
3648 * If the sample point is not integral, then we need to make a distinction
3649 * based on whether the constant term is non-integral or the coefficients
3650 * of the parameters. Furthermore, in order to decide how to handle
3651 * the non-integrality, we also need to know whether the coefficients
3652 * of the other columns in the tableau are integral. This leads
3653 * to the following table. The first two rows do not correspond
3654 * to a non-integral sample point and are only mentioned for completeness.
3656 * constant parameters other
3659 * int int rat | -> no problem
3661 * rat int int -> fail
3663 * rat int rat -> cut
3666 * rat rat rat | -> parametric cut
3669 * rat rat int | -> split context
3671 * If the parametric constant is completely integral, then there is nothing
3672 * to be done. If the constant term is non-integral, but all the other
3673 * coefficient are integral, then there is nothing that can be done
3674 * and the tableau has no integral solution.
3675 * If, on the other hand, one or more of the other columns have rational
3676 * coefficients, but the parameter coefficients are all integral, then
3677 * we can perform a regular (non-parametric) cut.
3678 * Finally, if there is any parameter coefficient that is non-integral,
3679 * then we need to involve the context tableau. There are two cases here.
3680 * If at least one other column has a rational coefficient, then we
3681 * can perform a parametric cut in the main tableau by adding a new
3682 * integer division in the context tableau.
3683 * If all other columns have integral coefficients, then we need to
3684 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3685 * is always integral. We do this by introducing an integer division
3686 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3687 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3688 * Since q is expressed in the tableau as
3689 * c + \sum a_i y_i - m q >= 0
3690 * -c - \sum a_i y_i + m q + m - 1 >= 0
3691 * it is sufficient to add the inequality
3692 * -c - \sum a_i y_i + m q >= 0
3693 * In the part of the context where this inequality does not hold, the
3694 * main tableau is marked as being empty.
3696 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3698 struct isl_context *context;
3701 if (!tab || sol->error)
3704 context = sol->context;
3708 if (context->op->is_empty(context))
3711 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3714 enum isl_tab_row_sign sgn;
3718 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3719 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3721 sgn = row_sign(tab, sol, row);
3724 tab->row_sign[row] = sgn;
3725 if (sgn == isl_tab_row_any)
3727 if (sgn == isl_tab_row_any && split == -1)
3729 if (sgn == isl_tab_row_neg)
3732 if (row < tab->n_row)
3735 struct isl_vec *ineq;
3737 split = context->op->best_split(context, tab);
3740 ineq = get_row_parameter_ineq(tab, split);
3744 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3745 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3747 if (tab->row_sign[row] == isl_tab_row_any)
3748 tab->row_sign[row] = isl_tab_row_unknown;
3750 tab->row_sign[split] = isl_tab_row_pos;
3752 find_in_pos(sol, tab, ineq->el);
3753 tab->row_sign[split] = isl_tab_row_neg;
3755 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3756 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3758 context->op->add_ineq(context, ineq->el, 0, 1);
3766 row = first_non_integer_row(tab, &flags);
3769 if (ISL_FL_ISSET(flags, I_PAR)) {
3770 if (ISL_FL_ISSET(flags, I_VAR)) {
3771 if (isl_tab_mark_empty(tab) < 0)
3775 row = add_cut(tab, row);
3776 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3777 struct isl_vec *div;
3778 struct isl_vec *ineq;
3780 div = get_row_split_div(tab, row);
3783 d = context->op->get_div(context, tab, div);
3787 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3791 no_sol_in_strict(sol, tab, ineq);
3792 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3793 context->op->add_ineq(context, ineq->el, 1, 1);
3795 if (sol->error || !context->op->is_ok(context))
3797 tab = set_row_cst_to_div(tab, row, d);
3798 if (context->op->is_empty(context))
3801 row = add_parametric_cut(tab, row, context);
3816 /* Compute the lexicographic minimum of the set represented by the main
3817 * tableau "tab" within the context "sol->context_tab".
3819 * As a preprocessing step, we first transfer all the purely parametric
3820 * equalities from the main tableau to the context tableau, i.e.,
3821 * parameters that have been pivoted to a row.
3822 * These equalities are ignored by the main algorithm, because the
3823 * corresponding rows may not be marked as being non-negative.
3824 * In parts of the context where the added equality does not hold,
3825 * the main tableau is marked as being empty.
3827 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3836 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3840 if (tab->row_var[row] < 0)
3842 if (tab->row_var[row] >= tab->n_param &&
3843 tab->row_var[row] < tab->n_var - tab->n_div)
3845 if (tab->row_var[row] < tab->n_param)
3846 p = tab->row_var[row];
3848 p = tab->row_var[row]
3849 + tab->n_param - (tab->n_var - tab->n_div);
3851 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3854 get_row_parameter_line(tab, row, eq->el);
3855 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3856 eq = isl_vec_normalize(eq);
3859 no_sol_in_strict(sol, tab, eq);
3861 isl_seq_neg(eq->el, eq->el, eq->size);
3863 no_sol_in_strict(sol, tab, eq);
3864 isl_seq_neg(eq->el, eq->el, eq->size);
3866 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3870 if (isl_tab_mark_redundant(tab, row) < 0)
3873 if (sol->context->op->is_empty(sol->context))
3876 row = tab->n_redundant - 1;
3879 find_solutions(sol, tab);
3890 /* Check if integer division "div" of "dom" also occurs in "bmap".
3891 * If so, return its position within the divs.
3892 * If not, return -1.
3894 static int find_context_div(struct isl_basic_map *bmap,
3895 struct isl_basic_set *dom, unsigned div)
3898 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
3899 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
3901 if (isl_int_is_zero(dom->div[div][0]))
3903 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3906 for (i = 0; i < bmap->n_div; ++i) {
3907 if (isl_int_is_zero(bmap->div[i][0]))
3909 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3910 (b_dim - d_dim) + bmap->n_div) != -1)
3912 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3918 /* The correspondence between the variables in the main tableau,
3919 * the context tableau, and the input map and domain is as follows.
3920 * The first n_param and the last n_div variables of the main tableau
3921 * form the variables of the context tableau.
3922 * In the basic map, these n_param variables correspond to the
3923 * parameters and the input dimensions. In the domain, they correspond
3924 * to the parameters and the set dimensions.
3925 * The n_div variables correspond to the integer divisions in the domain.
3926 * To ensure that everything lines up, we may need to copy some of the
3927 * integer divisions of the domain to the map. These have to be placed
3928 * in the same order as those in the context and they have to be placed
3929 * after any other integer divisions that the map may have.
3930 * This function performs the required reordering.
3932 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3933 struct isl_basic_set *dom)
3939 for (i = 0; i < dom->n_div; ++i)
3940 if (find_context_div(bmap, dom, i) != -1)
3942 other = bmap->n_div - common;
3943 if (dom->n_div - common > 0) {
3944 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3945 dom->n_div - common, 0, 0);
3949 for (i = 0; i < dom->n_div; ++i) {
3950 int pos = find_context_div(bmap, dom, i);
3952 pos = isl_basic_map_alloc_div(bmap);
3955 isl_int_set_si(bmap->div[pos][0], 0);
3957 if (pos != other + i)
3958 isl_basic_map_swap_div(bmap, pos, other + i);
3962 isl_basic_map_free(bmap);
3966 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3967 * some obvious symmetries.
3969 * We make sure the divs in the domain are properly ordered,
3970 * because they will be added one by one in the given order
3971 * during the construction of the solution map.
3973 static struct isl_sol *basic_map_partial_lexopt_base(
3974 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
3975 __isl_give isl_set **empty, int max,
3976 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
3977 __isl_take isl_basic_set *dom, int track_empty, int max))
3979 struct isl_tab *tab;
3980 struct isl_sol *sol = NULL;
3981 struct isl_context *context;
3984 dom = isl_basic_set_order_divs(dom);
3985 bmap = align_context_divs(bmap, dom);
3987 sol = init(bmap, dom, !!empty, max);
3991 context = sol->context;
3992 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
3994 else if (isl_basic_map_plain_is_empty(bmap)) {
3997 isl_basic_set_copy(context->op->peek_basic_set(context)));
3999 tab = tab_for_lexmin(bmap,
4000 context->op->peek_basic_set(context), 1, max);
4001 tab = context->op->detect_nonnegative_parameters(context, tab);
4002 find_solutions_main(sol, tab);
4007 isl_basic_map_free(bmap);
4011 isl_basic_map_free(bmap);
4015 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4016 * some obvious symmetries.
4018 * We call basic_map_partial_lexopt_base and extract the results.
4020 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
4021 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4022 __isl_give isl_set **empty, int max)
4024 isl_map *result = NULL;
4025 struct isl_sol *sol;
4026 struct isl_sol_map *sol_map;
4028 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4032 sol_map = (struct isl_sol_map *) sol;
4034 result = isl_map_copy(sol_map->map);
4036 *empty = isl_set_copy(sol_map->empty);
4037 sol_free(&sol_map->sol);
4041 /* Structure used during detection of parallel constraints.
4042 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4043 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4044 * val: the coefficients of the output variables
4046 struct isl_constraint_equal_info {
4047 isl_basic_map *bmap;
4053 /* Check whether the coefficients of the output variables
4054 * of the constraint in "entry" are equal to info->val.
4056 static int constraint_equal(const void *entry, const void *val)
4058 isl_int **row = (isl_int **)entry;
4059 const struct isl_constraint_equal_info *info = val;
4061 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4064 /* Check whether "bmap" has a pair of constraints that have
4065 * the same coefficients for the output variables.
4066 * Note that the coefficients of the existentially quantified
4067 * variables need to be zero since the existentially quantified
4068 * of the result are usually not the same as those of the input.
4069 * the isl_dim_out and isl_dim_div dimensions.
4070 * If so, return 1 and return the row indices of the two constraints
4071 * in *first and *second.
4073 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4074 int *first, int *second)
4077 isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
4078 struct isl_hash_table *table = NULL;
4079 struct isl_hash_table_entry *entry;
4080 struct isl_constraint_equal_info info;
4084 ctx = isl_basic_map_get_ctx(bmap);
4085 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4089 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4090 isl_basic_map_dim(bmap, isl_dim_in);
4092 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4093 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4094 info.n_out = n_out + n_div;
4095 for (i = 0; i < bmap->n_ineq; ++i) {
4098 info.val = bmap->ineq[i] + 1 + info.n_in;
4099 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4101 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4103 hash = isl_seq_get_hash(info.val, info.n_out);
4104 entry = isl_hash_table_find(ctx, table, hash,
4105 constraint_equal, &info, 1);
4110 entry->data = &bmap->ineq[i];
4113 if (i < bmap->n_ineq) {
4114 *first = ((isl_int **)entry->data) - bmap->ineq;
4118 isl_hash_table_free(ctx, table);
4120 return i < bmap->n_ineq;
4122 isl_hash_table_free(ctx, table);
4126 /* Given a set of upper bounds in "var", add constraints to "bset"
4127 * that make the i-th bound smallest.
4129 * In particular, if there are n bounds b_i, then add the constraints
4131 * b_i <= b_j for j > i
4132 * b_i < b_j for j < i
4134 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4135 __isl_keep isl_mat *var, int i)
4140 ctx = isl_mat_get_ctx(var);
4142 for (j = 0; j < var->n_row; ++j) {
4145 k = isl_basic_set_alloc_inequality(bset);
4148 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4149 ctx->negone, var->row[i], var->n_col);
4150 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4152 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4155 bset = isl_basic_set_finalize(bset);
4159 isl_basic_set_free(bset);
4163 /* Given a set of upper bounds on the last "input" variable m,
4164 * construct a set that assigns the minimal upper bound to m, i.e.,
4165 * construct a set that divides the space into cells where one
4166 * of the upper bounds is smaller than all the others and assign
4167 * this upper bound to m.
4169 * In particular, if there are n bounds b_i, then the result
4170 * consists of n basic sets, each one of the form
4173 * b_i <= b_j for j > i
4174 * b_i < b_j for j < i
4176 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4177 __isl_take isl_mat *var)
4180 isl_basic_set *bset = NULL;
4182 isl_set *set = NULL;
4187 ctx = isl_space_get_ctx(dim);
4188 set = isl_set_alloc_space(isl_space_copy(dim),
4189 var->n_row, ISL_SET_DISJOINT);
4191 for (i = 0; i < var->n_row; ++i) {
4192 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4194 k = isl_basic_set_alloc_equality(bset);
4197 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4198 isl_int_set_si(bset->eq[k][var->n_col], -1);
4199 bset = select_minimum(bset, var, i);
4200 set = isl_set_add_basic_set(set, bset);
4203 isl_space_free(dim);
4207 isl_basic_set_free(bset);
4209 isl_space_free(dim);
4214 /* Given that the last input variable of "bmap" represents the minimum
4215 * of the bounds in "cst", check whether we need to split the domain
4216 * based on which bound attains the minimum.
4218 * A split is needed when the minimum appears in an integer division
4219 * or in an equality. Otherwise, it is only needed if it appears in
4220 * an upper bound that is different from the upper bounds on which it
4223 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4224 __isl_keep isl_mat *cst)
4230 pos = cst->n_col - 1;
4231 total = isl_basic_map_dim(bmap, isl_dim_all);
4233 for (i = 0; i < bmap->n_div; ++i)
4234 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4237 for (i = 0; i < bmap->n_eq; ++i)
4238 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4241 for (i = 0; i < bmap->n_ineq; ++i) {
4242 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4244 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4246 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4247 total - pos - 1) >= 0)
4250 for (j = 0; j < cst->n_row; ++j)
4251 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4253 if (j >= cst->n_row)
4260 /* Given that the last set variable of "bset" represents the minimum
4261 * of the bounds in "cst", check whether we need to split the domain
4262 * based on which bound attains the minimum.
4264 * We simply call need_split_basic_map here. This is safe because
4265 * the position of the minimum is computed from "cst" and not
4268 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4269 __isl_keep isl_mat *cst)
4271 return need_split_basic_map((isl_basic_map *)bset, cst);
4274 /* Given that the last set variable of "set" represents the minimum
4275 * of the bounds in "cst", check whether we need to split the domain
4276 * based on which bound attains the minimum.
4278 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4282 for (i = 0; i < set->n; ++i)
4283 if (need_split_basic_set(set->p[i], cst))
4289 /* Given a set of which the last set variable is the minimum
4290 * of the bounds in "cst", split each basic set in the set
4291 * in pieces where one of the bounds is (strictly) smaller than the others.
4292 * This subdivision is given in "min_expr".
4293 * The variable is subsequently projected out.
4295 * We only do the split when it is needed.
4296 * For example if the last input variable m = min(a,b) and the only
4297 * constraints in the given basic set are lower bounds on m,
4298 * i.e., l <= m = min(a,b), then we can simply project out m
4299 * to obtain l <= a and l <= b, without having to split on whether
4300 * m is equal to a or b.
4302 static __isl_give isl_set *split(__isl_take isl_set *empty,
4303 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4310 if (!empty || !min_expr || !cst)
4313 n_in = isl_set_dim(empty, isl_dim_set);
4314 dim = isl_set_get_space(empty);
4315 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4316 res = isl_set_empty(dim);
4318 for (i = 0; i < empty->n; ++i) {
4321 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4322 if (need_split_basic_set(empty->p[i], cst))
4323 set = isl_set_intersect(set, isl_set_copy(min_expr));
4324 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4326 res = isl_set_union_disjoint(res, set);
4329 isl_set_free(empty);
4330 isl_set_free(min_expr);
4334 isl_set_free(empty);
4335 isl_set_free(min_expr);
4340 /* Given a map of which the last input variable is the minimum
4341 * of the bounds in "cst", split each basic set in the set
4342 * in pieces where one of the bounds is (strictly) smaller than the others.
4343 * This subdivision is given in "min_expr".
4344 * The variable is subsequently projected out.
4346 * The implementation is essentially the same as that of "split".
4348 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4349 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4356 if (!opt || !min_expr || !cst)
4359 n_in = isl_map_dim(opt, isl_dim_in);
4360 dim = isl_map_get_space(opt);
4361 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4362 res = isl_map_empty(dim);
4364 for (i = 0; i < opt->n; ++i) {
4367 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4368 if (need_split_basic_map(opt->p[i], cst))
4369 map = isl_map_intersect_domain(map,
4370 isl_set_copy(min_expr));
4371 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4373 res = isl_map_union_disjoint(res, map);
4377 isl_set_free(min_expr);
4382 isl_set_free(min_expr);
4387 static __isl_give isl_map *basic_map_partial_lexopt(
4388 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4389 __isl_give isl_set **empty, int max);
4394 isl_pw_multi_aff *pma;
4397 /* This function is called from basic_map_partial_lexopt_symm.
4398 * The last variable of "bmap" and "dom" corresponds to the minimum
4399 * of the bounds in "cst". "map_space" is the space of the original
4400 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4401 * is the space of the original domain.
4403 * We recursively call basic_map_partial_lexopt and then plug in
4404 * the definition of the minimum in the result.
4406 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4407 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4408 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4409 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4413 union isl_lex_res res;
4415 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4417 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4420 *empty = split(*empty,
4421 isl_set_copy(min_expr), isl_mat_copy(cst));
4422 *empty = isl_set_reset_space(*empty, set_space);
4425 opt = split_domain(opt, min_expr, cst);
4426 opt = isl_map_reset_space(opt, map_space);
4432 /* Given a basic map with at least two parallel constraints (as found
4433 * by the function parallel_constraints), first look for more constraints
4434 * parallel to the two constraint and replace the found list of parallel
4435 * constraints by a single constraint with as "input" part the minimum
4436 * of the input parts of the list of constraints. Then, recursively call
4437 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4438 * and plug in the definition of the minimum in the result.
4440 * More specifically, given a set of constraints
4444 * Replace this set by a single constraint
4448 * with u a new parameter with constraints
4452 * Any solution to the new system is also a solution for the original system
4455 * a x >= -u >= -b_i(p)
4457 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4458 * therefore be plugged into the solution.
4460 static union isl_lex_res basic_map_partial_lexopt_symm(
4461 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4462 __isl_give isl_set **empty, int max, int first, int second,
4463 __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4464 __isl_take isl_basic_set *dom,
4465 __isl_give isl_set **empty,
4466 int max, __isl_take isl_mat *cst,
4467 __isl_take isl_space *map_space,
4468 __isl_take isl_space *set_space))
4472 unsigned n_in, n_out, n_div;
4474 isl_vec *var = NULL;
4475 isl_mat *cst = NULL;
4476 isl_space *map_space, *set_space;
4477 union isl_lex_res res;
4479 map_space = isl_basic_map_get_space(bmap);
4480 set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4482 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4483 isl_basic_map_dim(bmap, isl_dim_in);
4484 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4486 ctx = isl_basic_map_get_ctx(bmap);
4487 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4488 var = isl_vec_alloc(ctx, n_out);
4494 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4495 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4496 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4500 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4504 for (i = 0; i < n; ++i)
4505 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4507 bmap = isl_basic_map_cow(bmap);
4510 for (i = n - 1; i >= 0; --i)
4511 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4514 bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4515 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4516 k = isl_basic_map_alloc_inequality(bmap);
4519 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4520 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4521 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4522 bmap = isl_basic_map_finalize(bmap);
4524 n_div = isl_basic_set_dim(dom, isl_dim_div);
4525 dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
4526 dom = isl_basic_set_extend_constraints(dom, 0, n);
4527 for (i = 0; i < n; ++i) {
4528 k = isl_basic_set_alloc_inequality(dom);
4531 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4532 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4533 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4539 return core(bmap, dom, empty, max, cst, map_space, set_space);
4541 isl_space_free(map_space);
4542 isl_space_free(set_space);
4546 isl_basic_set_free(dom);
4547 isl_basic_map_free(bmap);
4552 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4553 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4554 __isl_give isl_set **empty, int max, int first, int second)
4556 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4557 first, second, &basic_map_partial_lexopt_symm_map_core).map;
4560 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4561 * equalities and removing redundant constraints.
4563 * We first check if there are any parallel constraints (left).
4564 * If not, we are in the base case.
4565 * If there are parallel constraints, we replace them by a single
4566 * constraint in basic_map_partial_lexopt_symm and then call
4567 * this function recursively to look for more parallel constraints.
4569 static __isl_give isl_map *basic_map_partial_lexopt(
4570 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4571 __isl_give isl_set **empty, int max)
4579 if (bmap->ctx->opt->pip_symmetry)
4580 par = parallel_constraints(bmap, &first, &second);
4584 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4586 return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4589 isl_basic_set_free(dom);
4590 isl_basic_map_free(bmap);
4594 /* Compute the lexicographic minimum (or maximum if "max" is set)
4595 * of "bmap" over the domain "dom" and return the result as a map.
4596 * If "empty" is not NULL, then *empty is assigned a set that
4597 * contains those parts of the domain where there is no solution.
4598 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4599 * then we compute the rational optimum. Otherwise, we compute
4600 * the integral optimum.
4602 * We perform some preprocessing. As the PILP solver does not
4603 * handle implicit equalities very well, we first make sure all
4604 * the equalities are explicitly available.
4606 * We also add context constraints to the basic map and remove
4607 * redundant constraints. This is only needed because of the
4608 * way we handle simple symmetries. In particular, we currently look
4609 * for symmetries on the constraints, before we set up the main tableau.
4610 * It is then no good to look for symmetries on possibly redundant constraints.
4612 struct isl_map *isl_tab_basic_map_partial_lexopt(
4613 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4614 struct isl_set **empty, int max)
4621 isl_assert(bmap->ctx,
4622 isl_basic_map_compatible_domain(bmap, dom), goto error);
4624 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4625 return basic_map_partial_lexopt(bmap, dom, empty, max);
4627 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4628 bmap = isl_basic_map_detect_equalities(bmap);
4629 bmap = isl_basic_map_remove_redundancies(bmap);
4631 return basic_map_partial_lexopt(bmap, dom, empty, max);
4633 isl_basic_set_free(dom);
4634 isl_basic_map_free(bmap);
4638 struct isl_sol_for {
4640 int (*fn)(__isl_take isl_basic_set *dom,
4641 __isl_take isl_aff_list *list, void *user);
4645 static void sol_for_free(struct isl_sol_for *sol_for)
4647 if (sol_for->sol.context)
4648 sol_for->sol.context->op->free(sol_for->sol.context);
4652 static void sol_for_free_wrap(struct isl_sol *sol)
4654 sol_for_free((struct isl_sol_for *)sol);
4657 /* Add the solution identified by the tableau and the context tableau.
4659 * See documentation of sol_add for more details.
4661 * Instead of constructing a basic map, this function calls a user
4662 * defined function with the current context as a basic set and
4663 * a list of affine expressions representing the relation between
4664 * the input and output. The space over which the affine expressions
4665 * are defined is the same as that of the domain. The number of
4666 * affine expressions in the list is equal to the number of output variables.
4668 static void sol_for_add(struct isl_sol_for *sol,
4669 struct isl_basic_set *dom, struct isl_mat *M)
4673 isl_local_space *ls;
4677 if (sol->sol.error || !dom || !M)
4680 ctx = isl_basic_set_get_ctx(dom);
4681 ls = isl_basic_set_get_local_space(dom);
4682 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4683 for (i = 1; i < M->n_row; ++i) {
4684 aff = isl_aff_alloc(isl_local_space_copy(ls));
4686 isl_int_set(aff->v->el[0], M->row[0][0]);
4687 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4689 aff = isl_aff_normalize(aff);
4690 list = isl_aff_list_add(list, aff);
4692 isl_local_space_free(ls);
4694 dom = isl_basic_set_finalize(dom);
4696 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4699 isl_basic_set_free(dom);
4703 isl_basic_set_free(dom);
4708 static void sol_for_add_wrap(struct isl_sol *sol,
4709 struct isl_basic_set *dom, struct isl_mat *M)
4711 sol_for_add((struct isl_sol_for *)sol, dom, M);
4714 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4715 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4719 struct isl_sol_for *sol_for = NULL;
4721 struct isl_basic_set *dom = NULL;
4723 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4727 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4728 dom = isl_basic_set_universe(dom_dim);
4730 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4731 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4732 sol_for->sol.dec_level.sol = &sol_for->sol;
4734 sol_for->user = user;
4735 sol_for->sol.max = max;
4736 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4737 sol_for->sol.add = &sol_for_add_wrap;
4738 sol_for->sol.add_empty = NULL;
4739 sol_for->sol.free = &sol_for_free_wrap;
4741 sol_for->sol.context = isl_context_alloc(dom);
4742 if (!sol_for->sol.context)
4745 isl_basic_set_free(dom);
4748 isl_basic_set_free(dom);
4749 sol_for_free(sol_for);
4753 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4754 struct isl_tab *tab)
4756 find_solutions_main(&sol_for->sol, tab);
4759 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4760 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4764 struct isl_sol_for *sol_for = NULL;
4766 bmap = isl_basic_map_copy(bmap);
4770 bmap = isl_basic_map_detect_equalities(bmap);
4771 sol_for = sol_for_init(bmap, max, fn, user);
4773 if (isl_basic_map_plain_is_empty(bmap))
4776 struct isl_tab *tab;
4777 struct isl_context *context = sol_for->sol.context;
4778 tab = tab_for_lexmin(bmap,
4779 context->op->peek_basic_set(context), 1, max);
4780 tab = context->op->detect_nonnegative_parameters(context, tab);
4781 sol_for_find_solutions(sol_for, tab);
4782 if (sol_for->sol.error)
4786 sol_free(&sol_for->sol);
4787 isl_basic_map_free(bmap);
4790 sol_free(&sol_for->sol);
4791 isl_basic_map_free(bmap);
4795 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4796 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4800 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4803 /* Check if the given sequence of len variables starting at pos
4804 * represents a trivial (i.e., zero) solution.
4805 * The variables are assumed to be non-negative and to come in pairs,
4806 * with each pair representing a variable of unrestricted sign.
4807 * The solution is trivial if each such pair in the sequence consists
4808 * of two identical values, meaning that the variable being represented
4811 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4818 for (i = 0; i < len; i += 2) {
4822 neg_row = tab->var[pos + i].is_row ?
4823 tab->var[pos + i].index : -1;
4824 pos_row = tab->var[pos + i + 1].is_row ?
4825 tab->var[pos + i + 1].index : -1;
4828 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4830 isl_int_is_zero(tab->mat->row[pos_row][1])))
4833 if (neg_row < 0 || pos_row < 0)
4835 if (isl_int_ne(tab->mat->row[neg_row][1],
4836 tab->mat->row[pos_row][1]))
4843 /* Return the index of the first trivial region or -1 if all regions
4846 static int first_trivial_region(struct isl_tab *tab,
4847 int n_region, struct isl_region *region)
4851 for (i = 0; i < n_region; ++i) {
4852 if (region_is_trivial(tab, region[i].pos, region[i].len))
4859 /* Check if the solution is optimal, i.e., whether the first
4860 * n_op entries are zero.
4862 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4866 for (i = 0; i < n_op; ++i)
4867 if (!isl_int_is_zero(sol->el[1 + i]))
4872 /* Add constraints to "tab" that ensure that any solution is significantly
4873 * better that that represented by "sol". That is, find the first
4874 * relevant (within first n_op) non-zero coefficient and force it (along
4875 * with all previous coefficients) to be zero.
4876 * If the solution is already optimal (all relevant coefficients are zero),
4877 * then just mark the table as empty.
4879 static int force_better_solution(struct isl_tab *tab,
4880 __isl_keep isl_vec *sol, int n_op)
4889 for (i = 0; i < n_op; ++i)
4890 if (!isl_int_is_zero(sol->el[1 + i]))
4894 if (isl_tab_mark_empty(tab) < 0)
4899 ctx = isl_vec_get_ctx(sol);
4900 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4904 for (; i >= 0; --i) {
4906 isl_int_set_si(v->el[1 + i], -1);
4907 if (add_lexmin_eq(tab, v->el) < 0)
4918 struct isl_trivial {
4922 struct isl_tab_undo *snap;
4925 /* Return the lexicographically smallest non-trivial solution of the
4926 * given ILP problem.
4928 * All variables are assumed to be non-negative.
4930 * n_op is the number of initial coordinates to optimize.
4931 * That is, once a solution has been found, we will only continue looking
4932 * for solution that result in significantly better values for those
4933 * initial coordinates. That is, we only continue looking for solutions
4934 * that increase the number of initial zeros in this sequence.
4936 * A solution is non-trivial, if it is non-trivial on each of the
4937 * specified regions. Each region represents a sequence of pairs
4938 * of variables. A solution is non-trivial on such a region if
4939 * at least one of these pairs consists of different values, i.e.,
4940 * such that the non-negative variable represented by the pair is non-zero.
4942 * Whenever a conflict is encountered, all constraints involved are
4943 * reported to the caller through a call to "conflict".
4945 * We perform a simple branch-and-bound backtracking search.
4946 * Each level in the search represents initially trivial region that is forced
4947 * to be non-trivial.
4948 * At each level we consider n cases, where n is the length of the region.
4949 * In terms of the n/2 variables of unrestricted signs being encoded by
4950 * the region, we consider the cases
4953 * x_0 = 0 and x_1 >= 1
4954 * x_0 = 0 and x_1 <= -1
4955 * x_0 = 0 and x_1 = 0 and x_2 >= 1
4956 * x_0 = 0 and x_1 = 0 and x_2 <= -1
4958 * The cases are considered in this order, assuming that each pair
4959 * x_i_a x_i_b represents the value x_i_b - x_i_a.
4960 * That is, x_0 >= 1 is enforced by adding the constraint
4961 * x_0_b - x_0_a >= 1
4963 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
4964 __isl_take isl_basic_set *bset, int n_op, int n_region,
4965 struct isl_region *region,
4966 int (*conflict)(int con, void *user), void *user)
4970 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
4972 isl_vec *sol = isl_vec_alloc(ctx, 0);
4973 struct isl_tab *tab;
4974 struct isl_trivial *triv = NULL;
4977 tab = tab_for_lexmin(bset, NULL, 0, 0);
4980 tab->conflict = conflict;
4981 tab->conflict_user = user;
4983 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4984 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
4991 while (level >= 0) {
4995 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5000 r = first_trivial_region(tab, n_region, region);
5002 for (i = 0; i < level; ++i)
5005 sol = isl_tab_get_sample_value(tab);
5008 if (is_optimal(sol, n_op))
5012 if (level >= n_region)
5013 isl_die(ctx, isl_error_internal,
5014 "nesting level too deep", goto error);
5015 if (isl_tab_extend_cons(tab,
5016 2 * region[r].len + 2 * n_op) < 0)
5018 triv[level].region = r;
5019 triv[level].side = 0;
5022 r = triv[level].region;
5023 side = triv[level].side;
5024 base = 2 * (side/2);
5026 if (side >= region[r].len) {
5031 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5036 if (triv[level].update) {
5037 if (force_better_solution(tab, sol, n_op) < 0)
5039 triv[level].update = 0;
5042 if (side == base && base >= 2) {
5043 for (j = base - 2; j < base; ++j) {
5045 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5046 if (add_lexmin_eq(tab, v->el) < 0)
5051 triv[level].snap = isl_tab_snap(tab);
5052 if (isl_tab_push_basis(tab) < 0)
5056 isl_int_set_si(v->el[0], -1);
5057 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5058 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5059 tab = add_lexmin_ineq(tab, v->el);
5069 isl_basic_set_free(bset);
5076 isl_basic_set_free(bset);
5081 /* Return the lexicographically smallest rational point in "bset",
5082 * assuming that all variables are non-negative.
5083 * If "bset" is empty, then return a zero-length vector.
5085 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5086 __isl_take isl_basic_set *bset)
5088 struct isl_tab *tab;
5089 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
5092 tab = tab_for_lexmin(bset, NULL, 0, 0);
5096 sol = isl_vec_alloc(ctx, 0);
5098 sol = isl_tab_get_sample_value(tab);
5100 isl_basic_set_free(bset);
5104 isl_basic_set_free(bset);
5108 struct isl_sol_pma {
5110 isl_pw_multi_aff *pma;
5114 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5118 if (sol_pma->sol.context)
5119 sol_pma->sol.context->op->free(sol_pma->sol.context);
5120 isl_pw_multi_aff_free(sol_pma->pma);
5121 isl_set_free(sol_pma->empty);
5125 /* This function is called for parts of the context where there is
5126 * no solution, with "bset" corresponding to the context tableau.
5127 * Simply add the basic set to the set "empty".
5129 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5130 __isl_take isl_basic_set *bset)
5134 isl_assert(bset->ctx, sol->empty, goto error);
5136 sol->empty = isl_set_grow(sol->empty, 1);
5137 bset = isl_basic_set_simplify(bset);
5138 bset = isl_basic_set_finalize(bset);
5139 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5144 isl_basic_set_free(bset);
5148 /* Given a basic map "dom" that represents the context and an affine
5149 * matrix "M" that maps the dimensions of the context to the
5150 * output variables, construct an isl_pw_multi_aff with a single
5151 * cell corresponding to "dom" and affine expressions copied from "M".
5153 static void sol_pma_add(struct isl_sol_pma *sol,
5154 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5157 isl_local_space *ls;
5159 isl_multi_aff *maff;
5160 isl_pw_multi_aff *pma;
5162 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5163 ls = isl_basic_set_get_local_space(dom);
5164 for (i = 1; i < M->n_row; ++i) {
5165 aff = isl_aff_alloc(isl_local_space_copy(ls));
5167 isl_int_set(aff->v->el[0], M->row[0][0]);
5168 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5170 aff = isl_aff_normalize(aff);
5171 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5173 isl_local_space_free(ls);
5175 dom = isl_basic_set_simplify(dom);
5176 dom = isl_basic_set_finalize(dom);
5177 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5178 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5183 static void sol_pma_free_wrap(struct isl_sol *sol)
5185 sol_pma_free((struct isl_sol_pma *)sol);
5188 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5189 __isl_take isl_basic_set *bset)
5191 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5194 static void sol_pma_add_wrap(struct isl_sol *sol,
5195 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5197 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5200 /* Construct an isl_sol_pma structure for accumulating the solution.
5201 * If track_empty is set, then we also keep track of the parts
5202 * of the context where there is no solution.
5203 * If max is set, then we are solving a maximization, rather than
5204 * a minimization problem, which means that the variables in the
5205 * tableau have value "M - x" rather than "M + x".
5207 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5208 __isl_take isl_basic_set *dom, int track_empty, int max)
5210 struct isl_sol_pma *sol_pma = NULL;
5215 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5219 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5220 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5221 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5222 sol_pma->sol.max = max;
5223 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5224 sol_pma->sol.add = &sol_pma_add_wrap;
5225 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5226 sol_pma->sol.free = &sol_pma_free_wrap;
5227 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5231 sol_pma->sol.context = isl_context_alloc(dom);
5232 if (!sol_pma->sol.context)
5236 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5237 1, ISL_SET_DISJOINT);
5238 if (!sol_pma->empty)
5242 isl_basic_set_free(dom);
5243 return &sol_pma->sol;
5245 isl_basic_set_free(dom);
5246 sol_pma_free(sol_pma);
5250 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5251 * some obvious symmetries.
5253 * We call basic_map_partial_lexopt_base and extract the results.
5255 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5256 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5257 __isl_give isl_set **empty, int max)
5259 isl_pw_multi_aff *result = NULL;
5260 struct isl_sol *sol;
5261 struct isl_sol_pma *sol_pma;
5263 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5267 sol_pma = (struct isl_sol_pma *) sol;
5269 result = isl_pw_multi_aff_copy(sol_pma->pma);
5271 *empty = isl_set_copy(sol_pma->empty);
5272 sol_free(&sol_pma->sol);
5276 /* Given that the last input variable of "maff" represents the minimum
5277 * of some bounds, check whether we need to plug in the expression
5280 * In particular, check if the last input variable appears in any
5281 * of the expressions in "maff".
5283 static int need_substitution(__isl_keep isl_multi_aff *maff)
5288 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5290 for (i = 0; i < maff->n; ++i)
5291 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5297 /* Given a set of upper bounds on the last "input" variable m,
5298 * construct a piecewise affine expression that selects
5299 * the minimal upper bound to m, i.e.,
5300 * divide the space into cells where one
5301 * of the upper bounds is smaller than all the others and select
5302 * this upper bound on that cell.
5304 * In particular, if there are n bounds b_i, then the result
5305 * consists of n cell, each one of the form
5307 * b_i <= b_j for j > i
5308 * b_i < b_j for j < i
5310 * The affine expression on this cell is
5314 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5315 __isl_take isl_mat *var)
5318 isl_aff *aff = NULL;
5319 isl_basic_set *bset = NULL;
5321 isl_pw_aff *paff = NULL;
5322 isl_space *pw_space;
5323 isl_local_space *ls = NULL;
5328 ctx = isl_space_get_ctx(space);
5329 ls = isl_local_space_from_space(isl_space_copy(space));
5330 pw_space = isl_space_copy(space);
5331 pw_space = isl_space_from_domain(pw_space);
5332 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5333 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5335 for (i = 0; i < var->n_row; ++i) {
5338 aff = isl_aff_alloc(isl_local_space_copy(ls));
5339 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5343 isl_int_set_si(aff->v->el[0], 1);
5344 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5345 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5346 bset = select_minimum(bset, var, i);
5347 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5348 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5351 isl_local_space_free(ls);
5352 isl_space_free(space);
5357 isl_basic_set_free(bset);
5358 isl_pw_aff_free(paff);
5359 isl_local_space_free(ls);
5360 isl_space_free(space);
5365 /* Given a piecewise multi-affine expression of which the last input variable
5366 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5367 * This minimum expression is given in "min_expr_pa".
5368 * The set "min_expr" contains the same information, but in the form of a set.
5369 * The variable is subsequently projected out.
5371 * The implementation is similar to those of "split" and "split_domain".
5372 * If the variable appears in a given expression, then minimum expression
5373 * is plugged in. Otherwise, if the variable appears in the constraints
5374 * and a split is required, then the domain is split. Otherwise, no split
5377 static __isl_give isl_pw_multi_aff *split_domain_pma(
5378 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5379 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5384 isl_pw_multi_aff *res;
5386 if (!opt || !min_expr || !cst)
5389 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5390 space = isl_pw_multi_aff_get_space(opt);
5391 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5392 res = isl_pw_multi_aff_empty(space);
5394 for (i = 0; i < opt->n; ++i) {
5395 isl_pw_multi_aff *pma;
5397 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5398 isl_multi_aff_copy(opt->p[i].maff));
5399 if (need_substitution(opt->p[i].maff))
5400 pma = isl_pw_multi_aff_substitute(pma,
5401 isl_dim_in, n_in - 1, min_expr_pa);
5402 else if (need_split_set(opt->p[i].set, cst))
5403 pma = isl_pw_multi_aff_intersect_domain(pma,
5404 isl_set_copy(min_expr));
5405 pma = isl_pw_multi_aff_project_out(pma,
5406 isl_dim_in, n_in - 1, 1);
5408 res = isl_pw_multi_aff_add_disjoint(res, pma);
5411 isl_pw_multi_aff_free(opt);
5412 isl_pw_aff_free(min_expr_pa);
5413 isl_set_free(min_expr);
5417 isl_pw_multi_aff_free(opt);
5418 isl_pw_aff_free(min_expr_pa);
5419 isl_set_free(min_expr);
5424 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5425 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5426 __isl_give isl_set **empty, int max);
5428 /* This function is called from basic_map_partial_lexopt_symm.
5429 * The last variable of "bmap" and "dom" corresponds to the minimum
5430 * of the bounds in "cst". "map_space" is the space of the original
5431 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5432 * is the space of the original domain.
5434 * We recursively call basic_map_partial_lexopt and then plug in
5435 * the definition of the minimum in the result.
5437 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5438 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5439 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5440 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5442 isl_pw_multi_aff *opt;
5443 isl_pw_aff *min_expr_pa;
5445 union isl_lex_res res;
5447 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5448 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5451 opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5454 *empty = split(*empty,
5455 isl_set_copy(min_expr), isl_mat_copy(cst));
5456 *empty = isl_set_reset_space(*empty, set_space);
5459 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5460 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5466 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5467 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5468 __isl_give isl_set **empty, int max, int first, int second)
5470 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5471 first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5474 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5475 * equalities and removing redundant constraints.
5477 * We first check if there are any parallel constraints (left).
5478 * If not, we are in the base case.
5479 * If there are parallel constraints, we replace them by a single
5480 * constraint in basic_map_partial_lexopt_symm_pma and then call
5481 * this function recursively to look for more parallel constraints.
5483 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5484 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5485 __isl_give isl_set **empty, int max)
5493 if (bmap->ctx->opt->pip_symmetry)
5494 par = parallel_constraints(bmap, &first, &second);
5498 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5500 return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5503 isl_basic_set_free(dom);
5504 isl_basic_map_free(bmap);
5508 /* Compute the lexicographic minimum (or maximum if "max" is set)
5509 * of "bmap" over the domain "dom" and return the result as a piecewise
5510 * multi-affine expression.
5511 * If "empty" is not NULL, then *empty is assigned a set that
5512 * contains those parts of the domain where there is no solution.
5513 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5514 * then we compute the rational optimum. Otherwise, we compute
5515 * the integral optimum.
5517 * We perform some preprocessing. As the PILP solver does not
5518 * handle implicit equalities very well, we first make sure all
5519 * the equalities are explicitly available.
5521 * We also add context constraints to the basic map and remove
5522 * redundant constraints. This is only needed because of the
5523 * way we handle simple symmetries. In particular, we currently look
5524 * for symmetries on the constraints, before we set up the main tableau.
5525 * It is then no good to look for symmetries on possibly redundant constraints.
5527 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5528 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5529 __isl_give isl_set **empty, int max)
5536 isl_assert(bmap->ctx,
5537 isl_basic_map_compatible_domain(bmap, dom), goto error);
5539 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5540 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5542 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5543 bmap = isl_basic_map_detect_equalities(bmap);
5544 bmap = isl_basic_map_remove_redundancies(bmap);
5546 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5548 isl_basic_set_free(dom);
5549 isl_basic_map_free(bmap);