2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
17 #include "isl_sample.h"
18 #include <isl_mat_private.h>
19 #include <isl_aff_private.h>
20 #include <isl_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);
207 /* Pop one partial solution from the partial solution stack and
208 * pass it on to sol->add or sol->add_empty.
210 static void sol_pop_one(struct isl_sol *sol)
212 struct isl_partial_sol *partial;
214 partial = sol->partial;
215 sol->partial = partial->next;
218 sol->add(sol, partial->dom, partial->M);
220 sol->add_empty(sol, partial->dom);
224 /* Return a fresh copy of the domain represented by the context tableau.
226 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
228 struct isl_basic_set *bset;
233 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
234 bset = isl_basic_set_update_from_tab(bset,
235 sol->context->op->peek_tab(sol->context));
240 /* Check whether two partial solutions have the same mapping, where n_div
241 * is the number of divs that the two partial solutions have in common.
243 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
249 if (!s1->M != !s2->M)
254 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
256 for (i = 0; i < s1->M->n_row; ++i) {
257 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
258 s1->M->n_col-1-dim-n_div) != -1)
260 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
261 s2->M->n_col-1-dim-n_div) != -1)
263 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
269 /* Pop all solutions from the partial solution stack that were pushed onto
270 * the stack at levels that are deeper than the current level.
271 * If the two topmost elements on the stack have the same level
272 * and represent the same solution, then their domains are combined.
273 * This combined domain is the same as the current context domain
274 * as sol_pop is called each time we move back to a higher level.
276 static void sol_pop(struct isl_sol *sol)
278 struct isl_partial_sol *partial;
284 if (sol->level == 0) {
285 for (partial = sol->partial; partial; partial = sol->partial)
290 partial = sol->partial;
294 if (partial->level <= sol->level)
297 if (partial->next && partial->next->level == partial->level) {
298 n_div = isl_basic_set_dim(
299 sol->context->op->peek_basic_set(sol->context),
302 if (!same_solution(partial, partial->next, n_div)) {
306 struct isl_basic_set *bset;
308 bset = sol_domain(sol);
310 isl_basic_set_free(partial->next->dom);
311 partial->next->dom = bset;
312 partial->next->level = sol->level;
314 sol->partial = partial->next;
315 isl_basic_set_free(partial->dom);
316 isl_mat_free(partial->M);
323 static void sol_dec_level(struct isl_sol *sol)
333 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
335 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
337 sol_dec_level(callback->sol);
339 return callback->sol->error ? -1 : 0;
342 /* Move down to next level and push callback onto context tableau
343 * to decrease the level again when it gets rolled back across
344 * the current state. That is, dec_level will be called with
345 * the context tableau in the same state as it is when inc_level
348 static void sol_inc_level(struct isl_sol *sol)
356 tab = sol->context->op->peek_tab(sol->context);
357 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
361 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
365 if (isl_int_is_one(m))
368 for (i = 0; i < n_row; ++i)
369 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
372 /* Add the solution identified by the tableau and the context tableau.
374 * The layout of the variables is as follows.
375 * tab->n_var is equal to the total number of variables in the input
376 * map (including divs that were copied from the context)
377 * + the number of extra divs constructed
378 * Of these, the first tab->n_param and the last tab->n_div variables
379 * correspond to the variables in the context, i.e.,
380 * tab->n_param + tab->n_div = context_tab->n_var
381 * tab->n_param is equal to the number of parameters and input
382 * dimensions in the input map
383 * tab->n_div is equal to the number of divs in the context
385 * If there is no solution, then call add_empty with a basic set
386 * that corresponds to the context tableau. (If add_empty is NULL,
389 * If there is a solution, then first construct a matrix that maps
390 * all dimensions of the context to the output variables, i.e.,
391 * the output dimensions in the input map.
392 * The divs in the input map (if any) that do not correspond to any
393 * div in the context do not appear in the solution.
394 * The algorithm will make sure that they have an integer value,
395 * but these values themselves are of no interest.
396 * We have to be careful not to drop or rearrange any divs in the
397 * context because that would change the meaning of the matrix.
399 * To extract the value of the output variables, it should be noted
400 * that we always use a big parameter M in the main tableau and so
401 * the variable stored in this tableau is not an output variable x itself, but
402 * x' = M + x (in case of minimization)
404 * x' = M - x (in case of maximization)
405 * If x' appears in a column, then its optimal value is zero,
406 * which means that the optimal value of x is an unbounded number
407 * (-M for minimization and M for maximization).
408 * We currently assume that the output dimensions in the original map
409 * are bounded, so this cannot occur.
410 * Similarly, when x' appears in a row, then the coefficient of M in that
411 * row is necessarily 1.
412 * If the row in the tableau represents
413 * d x' = c + d M + e(y)
414 * then, in case of minimization, the corresponding row in the matrix
417 * with a d = m, the (updated) common denominator of the matrix.
418 * In case of maximization, the row will be
421 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
423 struct isl_basic_set *bset = NULL;
424 struct isl_mat *mat = NULL;
429 if (sol->error || !tab)
432 if (tab->empty && !sol->add_empty)
434 if (sol->context->op->is_empty(sol->context))
437 bset = sol_domain(sol);
440 sol_push_sol(sol, bset, NULL);
446 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
447 1 + tab->n_param + tab->n_div);
453 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
454 isl_int_set_si(mat->row[0][0], 1);
455 for (row = 0; row < sol->n_out; ++row) {
456 int i = tab->n_param + row;
459 isl_seq_clr(mat->row[1 + row], mat->n_col);
460 if (!tab->var[i].is_row) {
462 isl_die(mat->ctx, isl_error_invalid,
463 "unbounded optimum", goto error2);
467 r = tab->var[i].index;
469 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
470 isl_die(mat->ctx, isl_error_invalid,
471 "unbounded optimum", goto error2);
472 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
473 isl_int_divexact(m, tab->mat->row[r][0], m);
474 scale_rows(mat, m, 1 + row);
475 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
476 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
477 for (j = 0; j < tab->n_param; ++j) {
479 if (tab->var[j].is_row)
481 col = tab->var[j].index;
482 isl_int_mul(mat->row[1 + row][1 + j], m,
483 tab->mat->row[r][off + col]);
485 for (j = 0; j < tab->n_div; ++j) {
487 if (tab->var[tab->n_var - tab->n_div+j].is_row)
489 col = tab->var[tab->n_var - tab->n_div+j].index;
490 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
491 tab->mat->row[r][off + col]);
494 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
500 sol_push_sol(sol, bset, mat);
505 isl_basic_set_free(bset);
513 struct isl_set *empty;
516 static void sol_map_free(struct isl_sol_map *sol_map)
520 if (sol_map->sol.context)
521 sol_map->sol.context->op->free(sol_map->sol.context);
522 isl_map_free(sol_map->map);
523 isl_set_free(sol_map->empty);
527 static void sol_map_free_wrap(struct isl_sol *sol)
529 sol_map_free((struct isl_sol_map *)sol);
532 /* This function is called for parts of the context where there is
533 * no solution, with "bset" corresponding to the context tableau.
534 * Simply add the basic set to the set "empty".
536 static void sol_map_add_empty(struct isl_sol_map *sol,
537 struct isl_basic_set *bset)
541 isl_assert(bset->ctx, sol->empty, goto error);
543 sol->empty = isl_set_grow(sol->empty, 1);
544 bset = isl_basic_set_simplify(bset);
545 bset = isl_basic_set_finalize(bset);
546 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
549 isl_basic_set_free(bset);
552 isl_basic_set_free(bset);
556 static void sol_map_add_empty_wrap(struct isl_sol *sol,
557 struct isl_basic_set *bset)
559 sol_map_add_empty((struct isl_sol_map *)sol, bset);
562 /* Given a basic map "dom" that represents the context and an affine
563 * matrix "M" that maps the dimensions of the context to the
564 * output variables, construct a basic map with the same parameters
565 * and divs as the context, the dimensions of the context as input
566 * dimensions and a number of output dimensions that is equal to
567 * the number of output dimensions in the input map.
569 * The constraints and divs of the context are simply copied
570 * from "dom". For each row
574 * is added, with d the common denominator of M.
576 static void sol_map_add(struct isl_sol_map *sol,
577 struct isl_basic_set *dom, struct isl_mat *M)
580 struct isl_basic_map *bmap = NULL;
588 if (sol->sol.error || !dom || !M)
591 n_out = sol->sol.n_out;
592 n_eq = dom->n_eq + n_out;
593 n_ineq = dom->n_ineq;
595 nparam = isl_basic_set_total_dim(dom) - n_div;
596 total = isl_map_dim(sol->map, isl_dim_all);
597 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
598 n_div, n_eq, 2 * n_div + n_ineq);
601 if (sol->sol.rational)
602 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
603 for (i = 0; i < dom->n_div; ++i) {
604 int k = isl_basic_map_alloc_div(bmap);
607 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
608 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
609 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
610 dom->div[i] + 1 + 1 + nparam, i);
612 for (i = 0; i < dom->n_eq; ++i) {
613 int k = isl_basic_map_alloc_equality(bmap);
616 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
617 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
618 isl_seq_cpy(bmap->eq[k] + 1 + total,
619 dom->eq[i] + 1 + nparam, n_div);
621 for (i = 0; i < dom->n_ineq; ++i) {
622 int k = isl_basic_map_alloc_inequality(bmap);
625 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
626 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
627 isl_seq_cpy(bmap->ineq[k] + 1 + total,
628 dom->ineq[i] + 1 + nparam, n_div);
630 for (i = 0; i < M->n_row - 1; ++i) {
631 int k = isl_basic_map_alloc_equality(bmap);
634 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
635 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
636 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
637 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
638 M->row[1 + i] + 1 + nparam, n_div);
640 bmap = isl_basic_map_simplify(bmap);
641 bmap = isl_basic_map_finalize(bmap);
642 sol->map = isl_map_grow(sol->map, 1);
643 sol->map = isl_map_add_basic_map(sol->map, bmap);
644 isl_basic_set_free(dom);
650 isl_basic_set_free(dom);
652 isl_basic_map_free(bmap);
656 static void sol_map_add_wrap(struct isl_sol *sol,
657 struct isl_basic_set *dom, struct isl_mat *M)
659 sol_map_add((struct isl_sol_map *)sol, dom, M);
663 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
664 * i.e., the constant term and the coefficients of all variables that
665 * appear in the context tableau.
666 * Note that the coefficient of the big parameter M is NOT copied.
667 * The context tableau may not have a big parameter and even when it
668 * does, it is a different big parameter.
670 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
673 unsigned off = 2 + tab->M;
675 isl_int_set(line[0], tab->mat->row[row][1]);
676 for (i = 0; i < tab->n_param; ++i) {
677 if (tab->var[i].is_row)
678 isl_int_set_si(line[1 + i], 0);
680 int col = tab->var[i].index;
681 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
684 for (i = 0; i < tab->n_div; ++i) {
685 if (tab->var[tab->n_var - tab->n_div + i].is_row)
686 isl_int_set_si(line[1 + tab->n_param + i], 0);
688 int col = tab->var[tab->n_var - tab->n_div + i].index;
689 isl_int_set(line[1 + tab->n_param + i],
690 tab->mat->row[row][off + col]);
695 /* Check if rows "row1" and "row2" have identical "parametric constants",
696 * as explained above.
697 * In this case, we also insist that the coefficients of the big parameter
698 * be the same as the values of the constants will only be the same
699 * if these coefficients are also the same.
701 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
704 unsigned off = 2 + tab->M;
706 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
709 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
710 tab->mat->row[row2][2]))
713 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
714 int pos = i < tab->n_param ? i :
715 tab->n_var - tab->n_div + i - tab->n_param;
718 if (tab->var[pos].is_row)
720 col = tab->var[pos].index;
721 if (isl_int_ne(tab->mat->row[row1][off + col],
722 tab->mat->row[row2][off + col]))
728 /* Return an inequality that expresses that the "parametric constant"
729 * should be non-negative.
730 * This function is only called when the coefficient of the big parameter
733 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
735 struct isl_vec *ineq;
737 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
741 get_row_parameter_line(tab, row, ineq->el);
743 ineq = isl_vec_normalize(ineq);
748 /* Return a integer division for use in a parametric cut based on the given row.
749 * In particular, let the parametric constant of the row be
753 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
754 * The div returned is equal to
756 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
758 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
762 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
766 isl_int_set(div->el[0], tab->mat->row[row][0]);
767 get_row_parameter_line(tab, row, div->el + 1);
768 div = isl_vec_normalize(div);
769 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
770 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
775 /* Return a integer division for use in transferring an integrality constraint
777 * In particular, let the parametric constant of the row be
781 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
782 * The the returned div is equal to
784 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
786 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
790 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
794 isl_int_set(div->el[0], tab->mat->row[row][0]);
795 get_row_parameter_line(tab, row, div->el + 1);
796 div = isl_vec_normalize(div);
797 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
802 /* Construct and return an inequality that expresses an upper bound
804 * In particular, if the div is given by
808 * then the inequality expresses
812 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
816 struct isl_vec *ineq;
821 total = isl_basic_set_total_dim(bset);
822 div_pos = 1 + total - bset->n_div + div;
824 ineq = isl_vec_alloc(bset->ctx, 1 + total);
828 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
829 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
833 /* Given a row in the tableau and a div that was created
834 * using get_row_split_div and that has been constrained to equality, i.e.,
836 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
838 * replace the expression "\sum_i {a_i} y_i" in the row by d,
839 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
840 * The coefficients of the non-parameters in the tableau have been
841 * verified to be integral. We can therefore simply replace coefficient b
842 * by floor(b). For the coefficients of the parameters we have
843 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
846 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
848 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
849 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
851 isl_int_set_si(tab->mat->row[row][0], 1);
853 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
854 int drow = tab->var[tab->n_var - tab->n_div + div].index;
856 isl_assert(tab->mat->ctx,
857 isl_int_is_one(tab->mat->row[drow][0]), goto error);
858 isl_seq_combine(tab->mat->row[row] + 1,
859 tab->mat->ctx->one, tab->mat->row[row] + 1,
860 tab->mat->ctx->one, tab->mat->row[drow] + 1,
861 1 + tab->M + tab->n_col);
863 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
865 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
866 tab->mat->row[row][2 + tab->M + dcol], 1);
875 /* Check if the (parametric) constant of the given row is obviously
876 * negative, meaning that we don't need to consult the context tableau.
877 * If there is a big parameter and its coefficient is non-zero,
878 * then this coefficient determines the outcome.
879 * Otherwise, we check whether the constant is negative and
880 * all non-zero coefficients of parameters are negative and
881 * belong to non-negative parameters.
883 static int is_obviously_neg(struct isl_tab *tab, int row)
887 unsigned off = 2 + tab->M;
890 if (isl_int_is_pos(tab->mat->row[row][2]))
892 if (isl_int_is_neg(tab->mat->row[row][2]))
896 if (isl_int_is_nonneg(tab->mat->row[row][1]))
898 for (i = 0; i < tab->n_param; ++i) {
899 /* Eliminated parameter */
900 if (tab->var[i].is_row)
902 col = tab->var[i].index;
903 if (isl_int_is_zero(tab->mat->row[row][off + col]))
905 if (!tab->var[i].is_nonneg)
907 if (isl_int_is_pos(tab->mat->row[row][off + col]))
910 for (i = 0; i < tab->n_div; ++i) {
911 if (tab->var[tab->n_var - tab->n_div + i].is_row)
913 col = tab->var[tab->n_var - tab->n_div + i].index;
914 if (isl_int_is_zero(tab->mat->row[row][off + col]))
916 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
918 if (isl_int_is_pos(tab->mat->row[row][off + col]))
924 /* Check if the (parametric) constant of the given row is obviously
925 * non-negative, meaning that we don't need to consult the context tableau.
926 * If there is a big parameter and its coefficient is non-zero,
927 * then this coefficient determines the outcome.
928 * Otherwise, we check whether the constant is non-negative and
929 * all non-zero coefficients of parameters are positive and
930 * belong to non-negative parameters.
932 static int is_obviously_nonneg(struct isl_tab *tab, int row)
936 unsigned off = 2 + tab->M;
939 if (isl_int_is_pos(tab->mat->row[row][2]))
941 if (isl_int_is_neg(tab->mat->row[row][2]))
945 if (isl_int_is_neg(tab->mat->row[row][1]))
947 for (i = 0; i < tab->n_param; ++i) {
948 /* Eliminated parameter */
949 if (tab->var[i].is_row)
951 col = tab->var[i].index;
952 if (isl_int_is_zero(tab->mat->row[row][off + col]))
954 if (!tab->var[i].is_nonneg)
956 if (isl_int_is_neg(tab->mat->row[row][off + col]))
959 for (i = 0; i < tab->n_div; ++i) {
960 if (tab->var[tab->n_var - tab->n_div + i].is_row)
962 col = tab->var[tab->n_var - tab->n_div + i].index;
963 if (isl_int_is_zero(tab->mat->row[row][off + col]))
965 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
967 if (isl_int_is_neg(tab->mat->row[row][off + col]))
973 /* Given a row r and two columns, return the column that would
974 * lead to the lexicographically smallest increment in the sample
975 * solution when leaving the basis in favor of the row.
976 * Pivoting with column c will increment the sample value by a non-negative
977 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
978 * corresponding to the non-parametric variables.
979 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
980 * with all other entries in this virtual row equal to zero.
981 * If variable v appears in a row, then a_{v,c} is the element in column c
984 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
985 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
986 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
987 * increment. Otherwise, it's c2.
989 static int lexmin_col_pair(struct isl_tab *tab,
990 int row, int col1, int col2, isl_int tmp)
995 tr = tab->mat->row[row] + 2 + tab->M;
997 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1001 if (!tab->var[i].is_row) {
1002 if (tab->var[i].index == col1)
1004 if (tab->var[i].index == col2)
1009 if (tab->var[i].index == row)
1012 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1013 s1 = isl_int_sgn(r[col1]);
1014 s2 = isl_int_sgn(r[col2]);
1015 if (s1 == 0 && s2 == 0)
1022 isl_int_mul(tmp, r[col2], tr[col1]);
1023 isl_int_submul(tmp, r[col1], tr[col2]);
1024 if (isl_int_is_pos(tmp))
1026 if (isl_int_is_neg(tmp))
1032 /* Given a row in the tableau, find and return the column that would
1033 * result in the lexicographically smallest, but positive, increment
1034 * in the sample point.
1035 * If there is no such column, then return tab->n_col.
1036 * If anything goes wrong, return -1.
1038 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1041 int col = tab->n_col;
1045 tr = tab->mat->row[row] + 2 + tab->M;
1049 for (j = tab->n_dead; j < tab->n_col; ++j) {
1050 if (tab->col_var[j] >= 0 &&
1051 (tab->col_var[j] < tab->n_param ||
1052 tab->col_var[j] >= tab->n_var - tab->n_div))
1055 if (!isl_int_is_pos(tr[j]))
1058 if (col == tab->n_col)
1061 col = lexmin_col_pair(tab, row, col, j, tmp);
1062 isl_assert(tab->mat->ctx, col >= 0, goto error);
1072 /* Return the first known violated constraint, i.e., a non-negative
1073 * constraint that currently has an either obviously negative value
1074 * or a previously determined to be negative value.
1076 * If any constraint has a negative coefficient for the big parameter,
1077 * if any, then we return one of these first.
1079 static int first_neg(struct isl_tab *tab)
1084 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1085 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1087 if (!isl_int_is_neg(tab->mat->row[row][2]))
1090 tab->row_sign[row] = isl_tab_row_neg;
1093 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1094 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1096 if (tab->row_sign) {
1097 if (tab->row_sign[row] == 0 &&
1098 is_obviously_neg(tab, row))
1099 tab->row_sign[row] = isl_tab_row_neg;
1100 if (tab->row_sign[row] != isl_tab_row_neg)
1102 } else if (!is_obviously_neg(tab, row))
1109 /* Check whether the invariant that all columns are lexico-positive
1110 * is satisfied. This function is not called from the current code
1111 * but is useful during debugging.
1113 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1114 static void check_lexpos(struct isl_tab *tab)
1116 unsigned off = 2 + tab->M;
1121 for (col = tab->n_dead; col < tab->n_col; ++col) {
1122 if (tab->col_var[col] >= 0 &&
1123 (tab->col_var[col] < tab->n_param ||
1124 tab->col_var[col] >= tab->n_var - tab->n_div))
1126 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1127 if (!tab->var[var].is_row) {
1128 if (tab->var[var].index == col)
1133 row = tab->var[var].index;
1134 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1136 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1138 fprintf(stderr, "lexneg column %d (row %d)\n",
1141 if (var >= tab->n_var - tab->n_div)
1142 fprintf(stderr, "zero column %d\n", col);
1146 /* Report to the caller that the given constraint is part of an encountered
1149 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1151 return tab->conflict(con, tab->conflict_user);
1154 /* Given a conflicting row in the tableau, report all constraints
1155 * involved in the row to the caller. That is, the row itself
1156 * (if represents a constraint) and all constraint columns with
1157 * non-zero (and therefore negative) coefficient.
1159 static int report_conflict(struct isl_tab *tab, int row)
1167 if (tab->row_var[row] < 0 &&
1168 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1171 tr = tab->mat->row[row] + 2 + tab->M;
1173 for (j = tab->n_dead; j < tab->n_col; ++j) {
1174 if (tab->col_var[j] >= 0 &&
1175 (tab->col_var[j] < tab->n_param ||
1176 tab->col_var[j] >= tab->n_var - tab->n_div))
1179 if (!isl_int_is_neg(tr[j]))
1182 if (tab->col_var[j] < 0 &&
1183 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1190 /* Resolve all known or obviously violated constraints through pivoting.
1191 * In particular, as long as we can find any violated constraint, we
1192 * look for a pivoting column that would result in the lexicographically
1193 * smallest increment in the sample point. If there is no such column
1194 * then the tableau is infeasible.
1196 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1197 static int restore_lexmin(struct isl_tab *tab)
1205 while ((row = first_neg(tab)) != -1) {
1206 col = lexmin_pivot_col(tab, row);
1207 if (col >= tab->n_col) {
1208 if (report_conflict(tab, row) < 0)
1210 if (isl_tab_mark_empty(tab) < 0)
1216 if (isl_tab_pivot(tab, row, col) < 0)
1222 /* Given a row that represents an equality, look for an appropriate
1224 * In particular, if there are any non-zero coefficients among
1225 * the non-parameter variables, then we take the last of these
1226 * variables. Eliminating this variable in terms of the other
1227 * variables and/or parameters does not influence the property
1228 * that all column in the initial tableau are lexicographically
1229 * positive. The row corresponding to the eliminated variable
1230 * will only have non-zero entries below the diagonal of the
1231 * initial tableau. That is, we transform
1237 * If there is no such non-parameter variable, then we are dealing with
1238 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1239 * for elimination. This will ensure that the eliminated parameter
1240 * always has an integer value whenever all the other parameters are integral.
1241 * If there is no such parameter then we return -1.
1243 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1245 unsigned off = 2 + tab->M;
1248 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1250 if (tab->var[i].is_row)
1252 col = tab->var[i].index;
1253 if (col <= tab->n_dead)
1255 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1258 for (i = tab->n_dead; i < tab->n_col; ++i) {
1259 if (isl_int_is_one(tab->mat->row[row][off + i]))
1261 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1267 /* Add an equality that is known to be valid to the tableau.
1268 * We first check if we can eliminate a variable or a parameter.
1269 * If not, we add the equality as two inequalities.
1270 * In this case, the equality was a pure parameter equality and there
1271 * is no need to resolve any constraint violations.
1273 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1280 r = isl_tab_add_row(tab, eq);
1284 r = tab->con[r].index;
1285 i = last_var_col_or_int_par_col(tab, r);
1287 tab->con[r].is_nonneg = 1;
1288 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1290 isl_seq_neg(eq, eq, 1 + tab->n_var);
1291 r = isl_tab_add_row(tab, eq);
1294 tab->con[r].is_nonneg = 1;
1295 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1298 if (isl_tab_pivot(tab, r, i) < 0)
1300 if (isl_tab_kill_col(tab, i) < 0)
1311 /* Check if the given row is a pure constant.
1313 static int is_constant(struct isl_tab *tab, int row)
1315 unsigned off = 2 + tab->M;
1317 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1318 tab->n_col - tab->n_dead) == -1;
1321 /* Add an equality that may or may not be valid to the tableau.
1322 * If the resulting row is a pure constant, then it must be zero.
1323 * Otherwise, the resulting tableau is empty.
1325 * If the row is not a pure constant, then we add two inequalities,
1326 * each time checking that they can be satisfied.
1327 * In the end we try to use one of the two constraints to eliminate
1330 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1331 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1335 struct isl_tab_undo *snap;
1339 snap = isl_tab_snap(tab);
1340 r1 = isl_tab_add_row(tab, eq);
1343 tab->con[r1].is_nonneg = 1;
1344 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1347 row = tab->con[r1].index;
1348 if (is_constant(tab, row)) {
1349 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1350 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1351 if (isl_tab_mark_empty(tab) < 0)
1355 if (isl_tab_rollback(tab, snap) < 0)
1360 if (restore_lexmin(tab) < 0)
1365 isl_seq_neg(eq, eq, 1 + tab->n_var);
1367 r2 = isl_tab_add_row(tab, eq);
1370 tab->con[r2].is_nonneg = 1;
1371 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1374 if (restore_lexmin(tab) < 0)
1379 if (!tab->con[r1].is_row) {
1380 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1382 } else if (!tab->con[r2].is_row) {
1383 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1388 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1389 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1391 isl_seq_neg(eq, eq, 1 + tab->n_var);
1392 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1393 isl_seq_neg(eq, eq, 1 + tab->n_var);
1394 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1403 /* Add an inequality to the tableau, resolving violations using
1406 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1413 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1414 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1419 r = isl_tab_add_row(tab, ineq);
1422 tab->con[r].is_nonneg = 1;
1423 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1425 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1426 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1431 if (restore_lexmin(tab) < 0)
1433 if (!tab->empty && tab->con[r].is_row &&
1434 isl_tab_row_is_redundant(tab, tab->con[r].index))
1435 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1443 /* Check if the coefficients of the parameters are all integral.
1445 static int integer_parameter(struct isl_tab *tab, int row)
1449 unsigned off = 2 + tab->M;
1451 for (i = 0; i < tab->n_param; ++i) {
1452 /* Eliminated parameter */
1453 if (tab->var[i].is_row)
1455 col = tab->var[i].index;
1456 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1457 tab->mat->row[row][0]))
1460 for (i = 0; i < tab->n_div; ++i) {
1461 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1463 col = tab->var[tab->n_var - tab->n_div + i].index;
1464 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1465 tab->mat->row[row][0]))
1471 /* Check if the coefficients of the non-parameter variables are all integral.
1473 static int integer_variable(struct isl_tab *tab, int row)
1476 unsigned off = 2 + tab->M;
1478 for (i = tab->n_dead; i < tab->n_col; ++i) {
1479 if (tab->col_var[i] >= 0 &&
1480 (tab->col_var[i] < tab->n_param ||
1481 tab->col_var[i] >= tab->n_var - tab->n_div))
1483 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1484 tab->mat->row[row][0]))
1490 /* Check if the constant term is integral.
1492 static int integer_constant(struct isl_tab *tab, int row)
1494 return isl_int_is_divisible_by(tab->mat->row[row][1],
1495 tab->mat->row[row][0]);
1498 #define I_CST 1 << 0
1499 #define I_PAR 1 << 1
1500 #define I_VAR 1 << 2
1502 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1503 * that is non-integer and therefore requires a cut and return
1504 * the index of the variable.
1505 * For parametric tableaus, there are three parts in a row,
1506 * the constant, the coefficients of the parameters and the rest.
1507 * For each part, we check whether the coefficients in that part
1508 * are all integral and if so, set the corresponding flag in *f.
1509 * If the constant and the parameter part are integral, then the
1510 * current sample value is integral and no cut is required
1511 * (irrespective of whether the variable part is integral).
1513 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1515 var = var < 0 ? tab->n_param : var + 1;
1517 for (; var < tab->n_var - tab->n_div; ++var) {
1520 if (!tab->var[var].is_row)
1522 row = tab->var[var].index;
1523 if (integer_constant(tab, row))
1524 ISL_FL_SET(flags, I_CST);
1525 if (integer_parameter(tab, row))
1526 ISL_FL_SET(flags, I_PAR);
1527 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1529 if (integer_variable(tab, row))
1530 ISL_FL_SET(flags, I_VAR);
1537 /* Check for first (non-parameter) variable that is non-integer and
1538 * therefore requires a cut and return the corresponding row.
1539 * For parametric tableaus, there are three parts in a row,
1540 * the constant, the coefficients of the parameters and the rest.
1541 * For each part, we check whether the coefficients in that part
1542 * are all integral and if so, set the corresponding flag in *f.
1543 * If the constant and the parameter part are integral, then the
1544 * current sample value is integral and no cut is required
1545 * (irrespective of whether the variable part is integral).
1547 static int first_non_integer_row(struct isl_tab *tab, int *f)
1549 int var = next_non_integer_var(tab, -1, f);
1551 return var < 0 ? -1 : tab->var[var].index;
1554 /* Add a (non-parametric) cut to cut away the non-integral sample
1555 * value of the given row.
1557 * If the row is given by
1559 * m r = f + \sum_i a_i y_i
1563 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1565 * The big parameter, if any, is ignored, since it is assumed to be big
1566 * enough to be divisible by any integer.
1567 * If the tableau is actually a parametric tableau, then this function
1568 * is only called when all coefficients of the parameters are integral.
1569 * The cut therefore has zero coefficients for the parameters.
1571 * The current value is known to be negative, so row_sign, if it
1572 * exists, is set accordingly.
1574 * Return the row of the cut or -1.
1576 static int add_cut(struct isl_tab *tab, int row)
1581 unsigned off = 2 + tab->M;
1583 if (isl_tab_extend_cons(tab, 1) < 0)
1585 r = isl_tab_allocate_con(tab);
1589 r_row = tab->mat->row[tab->con[r].index];
1590 isl_int_set(r_row[0], tab->mat->row[row][0]);
1591 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1592 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1593 isl_int_neg(r_row[1], r_row[1]);
1595 isl_int_set_si(r_row[2], 0);
1596 for (i = 0; i < tab->n_col; ++i)
1597 isl_int_fdiv_r(r_row[off + i],
1598 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1600 tab->con[r].is_nonneg = 1;
1601 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1604 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1606 return tab->con[r].index;
1612 /* Given a non-parametric tableau, add cuts until an integer
1613 * sample point is obtained or until the tableau is determined
1614 * to be integer infeasible.
1615 * As long as there is any non-integer value in the sample point,
1616 * we add appropriate cuts, if possible, for each of these
1617 * non-integer values and then resolve the violated
1618 * cut constraints using restore_lexmin.
1619 * If one of the corresponding rows is equal to an integral
1620 * combination of variables/constraints plus a non-integral constant,
1621 * then there is no way to obtain an integer point and we return
1622 * a tableau that is marked empty.
1623 * The parameter cutting_strategy controls the strategy used when adding cuts
1624 * to remove non-integer points. CUT_ALL adds all possible cuts
1625 * before continuing the search. CUT_ONE adds only one cut at a time.
1627 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1628 int cutting_strategy)
1639 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1641 if (ISL_FL_ISSET(flags, I_VAR)) {
1642 if (isl_tab_mark_empty(tab) < 0)
1646 row = tab->var[var].index;
1647 row = add_cut(tab, row);
1650 if (cutting_strategy == CUT_ONE)
1652 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1653 if (restore_lexmin(tab) < 0)
1664 /* Check whether all the currently active samples also satisfy the inequality
1665 * "ineq" (treated as an equality if eq is set).
1666 * Remove those samples that do not.
1668 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1676 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1677 isl_assert(tab->mat->ctx, tab->samples, goto error);
1678 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1681 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1683 isl_seq_inner_product(ineq, tab->samples->row[i],
1684 1 + tab->n_var, &v);
1685 sgn = isl_int_sgn(v);
1686 if (eq ? (sgn == 0) : (sgn >= 0))
1688 tab = isl_tab_drop_sample(tab, i);
1700 /* Check whether the sample value of the tableau is finite,
1701 * i.e., either the tableau does not use a big parameter, or
1702 * all values of the variables are equal to the big parameter plus
1703 * some constant. This constant is the actual sample value.
1705 static int sample_is_finite(struct isl_tab *tab)
1712 for (i = 0; i < tab->n_var; ++i) {
1714 if (!tab->var[i].is_row)
1716 row = tab->var[i].index;
1717 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1723 /* Check if the context tableau of sol has any integer points.
1724 * Leave tab in empty state if no integer point can be found.
1725 * If an integer point can be found and if moreover it is finite,
1726 * then it is added to the list of sample values.
1728 * This function is only called when none of the currently active sample
1729 * values satisfies the most recently added constraint.
1731 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1733 struct isl_tab_undo *snap;
1738 snap = isl_tab_snap(tab);
1739 if (isl_tab_push_basis(tab) < 0)
1742 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1746 if (!tab->empty && sample_is_finite(tab)) {
1747 struct isl_vec *sample;
1749 sample = isl_tab_get_sample_value(tab);
1751 tab = isl_tab_add_sample(tab, sample);
1754 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1763 /* Check if any of the currently active sample values satisfies
1764 * the inequality "ineq" (an equality if eq is set).
1766 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1774 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1775 isl_assert(tab->mat->ctx, tab->samples, return -1);
1776 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1779 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1781 isl_seq_inner_product(ineq, tab->samples->row[i],
1782 1 + tab->n_var, &v);
1783 sgn = isl_int_sgn(v);
1784 if (eq ? (sgn == 0) : (sgn >= 0))
1789 return i < tab->n_sample;
1792 /* Add a div specified by "div" to the tableau "tab" and return
1793 * 1 if the div is obviously non-negative.
1795 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1796 int (*add_ineq)(void *user, isl_int *), void *user)
1800 struct isl_mat *samples;
1803 r = isl_tab_add_div(tab, div, add_ineq, user);
1806 nonneg = tab->var[r].is_nonneg;
1807 tab->var[r].frozen = 1;
1809 samples = isl_mat_extend(tab->samples,
1810 tab->n_sample, 1 + tab->n_var);
1811 tab->samples = samples;
1814 for (i = tab->n_outside; i < samples->n_row; ++i) {
1815 isl_seq_inner_product(div->el + 1, samples->row[i],
1816 div->size - 1, &samples->row[i][samples->n_col - 1]);
1817 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1818 samples->row[i][samples->n_col - 1], div->el[0]);
1824 /* Add a div specified by "div" to both the main tableau and
1825 * the context tableau. In case of the main tableau, we only
1826 * need to add an extra div. In the context tableau, we also
1827 * need to express the meaning of the div.
1828 * Return the index of the div or -1 if anything went wrong.
1830 static int add_div(struct isl_tab *tab, struct isl_context *context,
1831 struct isl_vec *div)
1836 if ((nonneg = context->op->add_div(context, div)) < 0)
1839 if (!context->op->is_ok(context))
1842 if (isl_tab_extend_vars(tab, 1) < 0)
1844 r = isl_tab_allocate_var(tab);
1848 tab->var[r].is_nonneg = 1;
1849 tab->var[r].frozen = 1;
1852 return tab->n_div - 1;
1854 context->op->invalidate(context);
1858 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1861 unsigned total = isl_basic_map_total_dim(tab->bmap);
1863 for (i = 0; i < tab->bmap->n_div; ++i) {
1864 if (isl_int_ne(tab->bmap->div[i][0], denom))
1866 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1873 /* Return the index of a div that corresponds to "div".
1874 * We first check if we already have such a div and if not, we create one.
1876 static int get_div(struct isl_tab *tab, struct isl_context *context,
1877 struct isl_vec *div)
1880 struct isl_tab *context_tab = context->op->peek_tab(context);
1885 d = find_div(context_tab, div->el + 1, div->el[0]);
1889 return add_div(tab, context, div);
1892 /* Add a parametric cut to cut away the non-integral sample value
1894 * Let a_i be the coefficients of the constant term and the parameters
1895 * and let b_i be the coefficients of the variables or constraints
1896 * in basis of the tableau.
1897 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1899 * The cut is expressed as
1901 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1903 * If q did not already exist in the context tableau, then it is added first.
1904 * If q is in a column of the main tableau then the "+ q" can be accomplished
1905 * by setting the corresponding entry to the denominator of the constraint.
1906 * If q happens to be in a row of the main tableau, then the corresponding
1907 * row needs to be added instead (taking care of the denominators).
1908 * Note that this is very unlikely, but perhaps not entirely impossible.
1910 * The current value of the cut is known to be negative (or at least
1911 * non-positive), so row_sign is set accordingly.
1913 * Return the row of the cut or -1.
1915 static int add_parametric_cut(struct isl_tab *tab, int row,
1916 struct isl_context *context)
1918 struct isl_vec *div;
1925 unsigned off = 2 + tab->M;
1930 div = get_row_parameter_div(tab, row);
1935 d = context->op->get_div(context, tab, div);
1939 if (isl_tab_extend_cons(tab, 1) < 0)
1941 r = isl_tab_allocate_con(tab);
1945 r_row = tab->mat->row[tab->con[r].index];
1946 isl_int_set(r_row[0], tab->mat->row[row][0]);
1947 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1948 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1949 isl_int_neg(r_row[1], r_row[1]);
1951 isl_int_set_si(r_row[2], 0);
1952 for (i = 0; i < tab->n_param; ++i) {
1953 if (tab->var[i].is_row)
1955 col = tab->var[i].index;
1956 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1957 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1958 tab->mat->row[row][0]);
1959 isl_int_neg(r_row[off + col], r_row[off + col]);
1961 for (i = 0; i < tab->n_div; ++i) {
1962 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1964 col = tab->var[tab->n_var - tab->n_div + i].index;
1965 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
1966 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
1967 tab->mat->row[row][0]);
1968 isl_int_neg(r_row[off + col], r_row[off + col]);
1970 for (i = 0; i < tab->n_col; ++i) {
1971 if (tab->col_var[i] >= 0 &&
1972 (tab->col_var[i] < tab->n_param ||
1973 tab->col_var[i] >= tab->n_var - tab->n_div))
1975 isl_int_fdiv_r(r_row[off + i],
1976 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1978 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
1980 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
1982 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
1983 isl_int_divexact(r_row[0], r_row[0], gcd);
1984 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
1985 isl_seq_combine(r_row + 1, gcd, r_row + 1,
1986 r_row[0], tab->mat->row[d_row] + 1,
1987 off - 1 + tab->n_col);
1988 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
1991 col = tab->var[tab->n_var - tab->n_div + d].index;
1992 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
1995 tab->con[r].is_nonneg = 1;
1996 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1999 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2003 row = tab->con[r].index;
2005 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2011 /* Construct a tableau for bmap that can be used for computing
2012 * the lexicographic minimum (or maximum) of bmap.
2013 * If not NULL, then dom is the domain where the minimum
2014 * should be computed. In this case, we set up a parametric
2015 * tableau with row signs (initialized to "unknown").
2016 * If M is set, then the tableau will use a big parameter.
2017 * If max is set, then a maximum should be computed instead of a minimum.
2018 * This means that for each variable x, the tableau will contain the variable
2019 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2020 * of the variables in all constraints are negated prior to adding them
2023 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2024 struct isl_basic_set *dom, unsigned M, int max)
2027 struct isl_tab *tab;
2029 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2030 isl_basic_map_total_dim(bmap), M);
2034 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2036 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2037 tab->n_div = dom->n_div;
2038 tab->row_sign = isl_calloc_array(bmap->ctx,
2039 enum isl_tab_row_sign, tab->mat->n_row);
2043 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2044 if (isl_tab_mark_empty(tab) < 0)
2049 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2050 tab->var[i].is_nonneg = 1;
2051 tab->var[i].frozen = 1;
2053 for (i = 0; i < bmap->n_eq; ++i) {
2055 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2056 bmap->eq[i] + 1 + tab->n_param,
2057 tab->n_var - tab->n_param - tab->n_div);
2058 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2060 isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
2061 bmap->eq[i] + 1 + tab->n_param,
2062 tab->n_var - tab->n_param - tab->n_div);
2063 if (!tab || tab->empty)
2066 if (bmap->n_eq && restore_lexmin(tab) < 0)
2068 for (i = 0; i < bmap->n_ineq; ++i) {
2070 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2071 bmap->ineq[i] + 1 + tab->n_param,
2072 tab->n_var - tab->n_param - tab->n_div);
2073 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2075 isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
2076 bmap->ineq[i] + 1 + tab->n_param,
2077 tab->n_var - tab->n_param - tab->n_div);
2078 if (!tab || tab->empty)
2087 /* Given a main tableau where more than one row requires a split,
2088 * determine and return the "best" row to split on.
2090 * Given two rows in the main tableau, if the inequality corresponding
2091 * to the first row is redundant with respect to that of the second row
2092 * in the current tableau, then it is better to split on the second row,
2093 * since in the positive part, both row will be positive.
2094 * (In the negative part a pivot will have to be performed and just about
2095 * anything can happen to the sign of the other row.)
2097 * As a simple heuristic, we therefore select the row that makes the most
2098 * of the other rows redundant.
2100 * Perhaps it would also be useful to look at the number of constraints
2101 * that conflict with any given constraint.
2103 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2105 struct isl_tab_undo *snap;
2111 if (isl_tab_extend_cons(context_tab, 2) < 0)
2114 snap = isl_tab_snap(context_tab);
2116 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2117 struct isl_tab_undo *snap2;
2118 struct isl_vec *ineq = NULL;
2122 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2124 if (tab->row_sign[split] != isl_tab_row_any)
2127 ineq = get_row_parameter_ineq(tab, split);
2130 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2135 snap2 = isl_tab_snap(context_tab);
2137 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2138 struct isl_tab_var *var;
2142 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2144 if (tab->row_sign[row] != isl_tab_row_any)
2147 ineq = get_row_parameter_ineq(tab, row);
2150 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2154 var = &context_tab->con[context_tab->n_con - 1];
2155 if (!context_tab->empty &&
2156 !isl_tab_min_at_most_neg_one(context_tab, var))
2158 if (isl_tab_rollback(context_tab, snap2) < 0)
2161 if (best == -1 || r > best_r) {
2165 if (isl_tab_rollback(context_tab, snap) < 0)
2172 static struct isl_basic_set *context_lex_peek_basic_set(
2173 struct isl_context *context)
2175 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2178 return isl_tab_peek_bset(clex->tab);
2181 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2183 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2187 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2188 int check, int update)
2190 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2191 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2193 if (add_lexmin_eq(clex->tab, eq) < 0)
2196 int v = tab_has_valid_sample(clex->tab, eq, 1);
2200 clex->tab = check_integer_feasible(clex->tab);
2203 clex->tab = check_samples(clex->tab, eq, 1);
2206 isl_tab_free(clex->tab);
2210 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2211 int check, int update)
2213 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2214 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2216 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2218 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2222 clex->tab = check_integer_feasible(clex->tab);
2225 clex->tab = check_samples(clex->tab, ineq, 0);
2228 isl_tab_free(clex->tab);
2232 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2234 struct isl_context *context = (struct isl_context *)user;
2235 context_lex_add_ineq(context, ineq, 0, 0);
2236 return context->op->is_ok(context) ? 0 : -1;
2239 /* Check which signs can be obtained by "ineq" on all the currently
2240 * active sample values. See row_sign for more information.
2242 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2248 enum isl_tab_row_sign res = isl_tab_row_unknown;
2250 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2251 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2252 return isl_tab_row_unknown);
2255 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2256 isl_seq_inner_product(tab->samples->row[i], ineq,
2257 1 + tab->n_var, &tmp);
2258 sgn = isl_int_sgn(tmp);
2259 if (sgn > 0 || (sgn == 0 && strict)) {
2260 if (res == isl_tab_row_unknown)
2261 res = isl_tab_row_pos;
2262 if (res == isl_tab_row_neg)
2263 res = isl_tab_row_any;
2266 if (res == isl_tab_row_unknown)
2267 res = isl_tab_row_neg;
2268 if (res == isl_tab_row_pos)
2269 res = isl_tab_row_any;
2271 if (res == isl_tab_row_any)
2279 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2280 isl_int *ineq, int strict)
2282 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2283 return tab_ineq_sign(clex->tab, ineq, strict);
2286 /* Check whether "ineq" can be added to the tableau without rendering
2289 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2291 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2292 struct isl_tab_undo *snap;
2298 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2301 snap = isl_tab_snap(clex->tab);
2302 if (isl_tab_push_basis(clex->tab) < 0)
2304 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2305 clex->tab = check_integer_feasible(clex->tab);
2308 feasible = !clex->tab->empty;
2309 if (isl_tab_rollback(clex->tab, snap) < 0)
2315 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2316 struct isl_vec *div)
2318 return get_div(tab, context, div);
2321 /* Add a div specified by "div" to the context tableau and return
2322 * 1 if the div is obviously non-negative.
2323 * context_tab_add_div will always return 1, because all variables
2324 * in a isl_context_lex tableau are non-negative.
2325 * However, if we are using a big parameter in the context, then this only
2326 * reflects the non-negativity of the variable used to _encode_ the
2327 * div, i.e., div' = M + div, so we can't draw any conclusions.
2329 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2331 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2333 nonneg = context_tab_add_div(clex->tab, div,
2334 context_lex_add_ineq_wrap, context);
2342 static int context_lex_detect_equalities(struct isl_context *context,
2343 struct isl_tab *tab)
2348 static int context_lex_best_split(struct isl_context *context,
2349 struct isl_tab *tab)
2351 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2352 struct isl_tab_undo *snap;
2355 snap = isl_tab_snap(clex->tab);
2356 if (isl_tab_push_basis(clex->tab) < 0)
2358 r = best_split(tab, clex->tab);
2360 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2366 static int context_lex_is_empty(struct isl_context *context)
2368 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2371 return clex->tab->empty;
2374 static void *context_lex_save(struct isl_context *context)
2376 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2377 struct isl_tab_undo *snap;
2379 snap = isl_tab_snap(clex->tab);
2380 if (isl_tab_push_basis(clex->tab) < 0)
2382 if (isl_tab_save_samples(clex->tab) < 0)
2388 static void context_lex_restore(struct isl_context *context, void *save)
2390 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2391 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2392 isl_tab_free(clex->tab);
2397 static int context_lex_is_ok(struct isl_context *context)
2399 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2403 /* For each variable in the context tableau, check if the variable can
2404 * only attain non-negative values. If so, mark the parameter as non-negative
2405 * in the main tableau. This allows for a more direct identification of some
2406 * cases of violated constraints.
2408 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2409 struct isl_tab *context_tab)
2412 struct isl_tab_undo *snap;
2413 struct isl_vec *ineq = NULL;
2414 struct isl_tab_var *var;
2417 if (context_tab->n_var == 0)
2420 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2424 if (isl_tab_extend_cons(context_tab, 1) < 0)
2427 snap = isl_tab_snap(context_tab);
2430 isl_seq_clr(ineq->el, ineq->size);
2431 for (i = 0; i < context_tab->n_var; ++i) {
2432 isl_int_set_si(ineq->el[1 + i], 1);
2433 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2435 var = &context_tab->con[context_tab->n_con - 1];
2436 if (!context_tab->empty &&
2437 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2439 if (i >= tab->n_param)
2440 j = i - tab->n_param + tab->n_var - tab->n_div;
2441 tab->var[j].is_nonneg = 1;
2444 isl_int_set_si(ineq->el[1 + i], 0);
2445 if (isl_tab_rollback(context_tab, snap) < 0)
2449 if (context_tab->M && n == context_tab->n_var) {
2450 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2462 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2463 struct isl_context *context, struct isl_tab *tab)
2465 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2466 struct isl_tab_undo *snap;
2471 snap = isl_tab_snap(clex->tab);
2472 if (isl_tab_push_basis(clex->tab) < 0)
2475 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2477 if (isl_tab_rollback(clex->tab, snap) < 0)
2486 static void context_lex_invalidate(struct isl_context *context)
2488 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2489 isl_tab_free(clex->tab);
2493 static void context_lex_free(struct isl_context *context)
2495 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2496 isl_tab_free(clex->tab);
2500 struct isl_context_op isl_context_lex_op = {
2501 context_lex_detect_nonnegative_parameters,
2502 context_lex_peek_basic_set,
2503 context_lex_peek_tab,
2505 context_lex_add_ineq,
2506 context_lex_ineq_sign,
2507 context_lex_test_ineq,
2508 context_lex_get_div,
2509 context_lex_add_div,
2510 context_lex_detect_equalities,
2511 context_lex_best_split,
2512 context_lex_is_empty,
2515 context_lex_restore,
2516 context_lex_invalidate,
2520 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2522 struct isl_tab *tab;
2526 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2529 if (isl_tab_track_bset(tab, bset) < 0)
2531 tab = isl_tab_init_samples(tab);
2534 isl_basic_set_free(bset);
2538 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2540 struct isl_context_lex *clex;
2545 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2549 clex->context.op = &isl_context_lex_op;
2551 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2552 if (restore_lexmin(clex->tab) < 0)
2554 clex->tab = check_integer_feasible(clex->tab);
2558 return &clex->context;
2560 clex->context.op->free(&clex->context);
2564 struct isl_context_gbr {
2565 struct isl_context context;
2566 struct isl_tab *tab;
2567 struct isl_tab *shifted;
2568 struct isl_tab *cone;
2571 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2572 struct isl_context *context, struct isl_tab *tab)
2574 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2577 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2580 static struct isl_basic_set *context_gbr_peek_basic_set(
2581 struct isl_context *context)
2583 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2586 return isl_tab_peek_bset(cgbr->tab);
2589 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2591 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2595 /* Initialize the "shifted" tableau of the context, which
2596 * contains the constraints of the original tableau shifted
2597 * by the sum of all negative coefficients. This ensures
2598 * that any rational point in the shifted tableau can
2599 * be rounded up to yield an integer point in the original tableau.
2601 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2604 struct isl_vec *cst;
2605 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2606 unsigned dim = isl_basic_set_total_dim(bset);
2608 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2612 for (i = 0; i < bset->n_ineq; ++i) {
2613 isl_int_set(cst->el[i], bset->ineq[i][0]);
2614 for (j = 0; j < dim; ++j) {
2615 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2617 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2618 bset->ineq[i][1 + j]);
2622 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2624 for (i = 0; i < bset->n_ineq; ++i)
2625 isl_int_set(bset->ineq[i][0], cst->el[i]);
2630 /* Check if the shifted tableau is non-empty, and if so
2631 * use the sample point to construct an integer point
2632 * of the context tableau.
2634 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2636 struct isl_vec *sample;
2639 gbr_init_shifted(cgbr);
2642 if (cgbr->shifted->empty)
2643 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2645 sample = isl_tab_get_sample_value(cgbr->shifted);
2646 sample = isl_vec_ceil(sample);
2651 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2658 for (i = 0; i < bset->n_eq; ++i)
2659 isl_int_set_si(bset->eq[i][0], 0);
2661 for (i = 0; i < bset->n_ineq; ++i)
2662 isl_int_set_si(bset->ineq[i][0], 0);
2667 static int use_shifted(struct isl_context_gbr *cgbr)
2669 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2672 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2674 struct isl_basic_set *bset;
2675 struct isl_basic_set *cone;
2677 if (isl_tab_sample_is_integer(cgbr->tab))
2678 return isl_tab_get_sample_value(cgbr->tab);
2680 if (use_shifted(cgbr)) {
2681 struct isl_vec *sample;
2683 sample = gbr_get_shifted_sample(cgbr);
2684 if (!sample || sample->size > 0)
2687 isl_vec_free(sample);
2691 bset = isl_tab_peek_bset(cgbr->tab);
2692 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2695 if (isl_tab_track_bset(cgbr->cone,
2696 isl_basic_set_copy(bset)) < 0)
2699 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2702 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2703 struct isl_vec *sample;
2704 struct isl_tab_undo *snap;
2706 if (cgbr->tab->basis) {
2707 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2708 isl_mat_free(cgbr->tab->basis);
2709 cgbr->tab->basis = NULL;
2711 cgbr->tab->n_zero = 0;
2712 cgbr->tab->n_unbounded = 0;
2715 snap = isl_tab_snap(cgbr->tab);
2717 sample = isl_tab_sample(cgbr->tab);
2719 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2720 isl_vec_free(sample);
2727 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2728 cone = drop_constant_terms(cone);
2729 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2730 cone = isl_basic_set_underlying_set(cone);
2731 cone = isl_basic_set_gauss(cone, NULL);
2733 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2734 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2735 bset = isl_basic_set_underlying_set(bset);
2736 bset = isl_basic_set_gauss(bset, NULL);
2738 return isl_basic_set_sample_with_cone(bset, cone);
2741 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2743 struct isl_vec *sample;
2748 if (cgbr->tab->empty)
2751 sample = gbr_get_sample(cgbr);
2755 if (sample->size == 0) {
2756 isl_vec_free(sample);
2757 if (isl_tab_mark_empty(cgbr->tab) < 0)
2762 cgbr->tab = isl_tab_add_sample(cgbr->tab, sample);
2766 isl_tab_free(cgbr->tab);
2770 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2775 if (isl_tab_extend_cons(tab, 2) < 0)
2778 if (isl_tab_add_eq(tab, eq) < 0)
2787 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2788 int check, int update)
2790 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2792 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2794 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2795 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2797 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2802 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2806 check_gbr_integer_feasible(cgbr);
2809 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2812 isl_tab_free(cgbr->tab);
2816 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2821 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2824 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2827 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2830 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2832 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2835 for (i = 0; i < dim; ++i) {
2836 if (!isl_int_is_neg(ineq[1 + i]))
2838 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2841 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2844 for (i = 0; i < dim; ++i) {
2845 if (!isl_int_is_neg(ineq[1 + i]))
2847 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2851 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2852 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2854 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2860 isl_tab_free(cgbr->tab);
2864 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2865 int check, int update)
2867 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2869 add_gbr_ineq(cgbr, ineq);
2874 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2878 check_gbr_integer_feasible(cgbr);
2881 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2884 isl_tab_free(cgbr->tab);
2888 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2890 struct isl_context *context = (struct isl_context *)user;
2891 context_gbr_add_ineq(context, ineq, 0, 0);
2892 return context->op->is_ok(context) ? 0 : -1;
2895 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2896 isl_int *ineq, int strict)
2898 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2899 return tab_ineq_sign(cgbr->tab, ineq, strict);
2902 /* Check whether "ineq" can be added to the tableau without rendering
2905 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2907 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2908 struct isl_tab_undo *snap;
2909 struct isl_tab_undo *shifted_snap = NULL;
2910 struct isl_tab_undo *cone_snap = NULL;
2916 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2919 snap = isl_tab_snap(cgbr->tab);
2921 shifted_snap = isl_tab_snap(cgbr->shifted);
2923 cone_snap = isl_tab_snap(cgbr->cone);
2924 add_gbr_ineq(cgbr, ineq);
2925 check_gbr_integer_feasible(cgbr);
2928 feasible = !cgbr->tab->empty;
2929 if (isl_tab_rollback(cgbr->tab, snap) < 0)
2932 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
2934 } else if (cgbr->shifted) {
2935 isl_tab_free(cgbr->shifted);
2936 cgbr->shifted = NULL;
2939 if (isl_tab_rollback(cgbr->cone, cone_snap))
2941 } else if (cgbr->cone) {
2942 isl_tab_free(cgbr->cone);
2949 /* Return the column of the last of the variables associated to
2950 * a column that has a non-zero coefficient.
2951 * This function is called in a context where only coefficients
2952 * of parameters or divs can be non-zero.
2954 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
2959 if (tab->n_var == 0)
2962 for (i = tab->n_var - 1; i >= 0; --i) {
2963 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
2965 if (tab->var[i].is_row)
2967 col = tab->var[i].index;
2968 if (!isl_int_is_zero(p[col]))
2975 /* Look through all the recently added equalities in the context
2976 * to see if we can propagate any of them to the main tableau.
2978 * The newly added equalities in the context are encoded as pairs
2979 * of inequalities starting at inequality "first".
2981 * We tentatively add each of these equalities to the main tableau
2982 * and if this happens to result in a row with a final coefficient
2983 * that is one or negative one, we use it to kill a column
2984 * in the main tableau. Otherwise, we discard the tentatively
2987 static void propagate_equalities(struct isl_context_gbr *cgbr,
2988 struct isl_tab *tab, unsigned first)
2991 struct isl_vec *eq = NULL;
2993 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2997 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3000 isl_seq_clr(eq->el + 1 + tab->n_param,
3001 tab->n_var - tab->n_param - tab->n_div);
3002 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3005 struct isl_tab_undo *snap;
3006 snap = isl_tab_snap(tab);
3008 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3009 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3010 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3013 r = isl_tab_add_row(tab, eq->el);
3016 r = tab->con[r].index;
3017 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3018 if (j < 0 || j < tab->n_dead ||
3019 !isl_int_is_one(tab->mat->row[r][0]) ||
3020 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3021 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3022 if (isl_tab_rollback(tab, snap) < 0)
3026 if (isl_tab_pivot(tab, r, j) < 0)
3028 if (isl_tab_kill_col(tab, j) < 0)
3031 if (restore_lexmin(tab) < 0)
3040 isl_tab_free(cgbr->tab);
3044 static int context_gbr_detect_equalities(struct isl_context *context,
3045 struct isl_tab *tab)
3047 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3048 struct isl_ctx *ctx;
3051 ctx = cgbr->tab->mat->ctx;
3054 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3055 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3058 if (isl_tab_track_bset(cgbr->cone,
3059 isl_basic_set_copy(bset)) < 0)
3062 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3065 n_ineq = cgbr->tab->bmap->n_ineq;
3066 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3067 if (cgbr->tab && cgbr->tab->bmap->n_ineq > n_ineq)
3068 propagate_equalities(cgbr, tab, n_ineq);
3072 isl_tab_free(cgbr->tab);
3077 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3078 struct isl_vec *div)
3080 return get_div(tab, context, div);
3083 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3085 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3089 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3091 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3093 if (isl_tab_allocate_var(cgbr->cone) <0)
3096 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3097 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3098 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3101 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3102 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3105 return context_tab_add_div(cgbr->tab, div,
3106 context_gbr_add_ineq_wrap, context);
3109 static int context_gbr_best_split(struct isl_context *context,
3110 struct isl_tab *tab)
3112 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3113 struct isl_tab_undo *snap;
3116 snap = isl_tab_snap(cgbr->tab);
3117 r = best_split(tab, cgbr->tab);
3119 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3125 static int context_gbr_is_empty(struct isl_context *context)
3127 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3130 return cgbr->tab->empty;
3133 struct isl_gbr_tab_undo {
3134 struct isl_tab_undo *tab_snap;
3135 struct isl_tab_undo *shifted_snap;
3136 struct isl_tab_undo *cone_snap;
3139 static void *context_gbr_save(struct isl_context *context)
3141 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3142 struct isl_gbr_tab_undo *snap;
3144 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3148 snap->tab_snap = isl_tab_snap(cgbr->tab);
3149 if (isl_tab_save_samples(cgbr->tab) < 0)
3153 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3155 snap->shifted_snap = NULL;
3158 snap->cone_snap = isl_tab_snap(cgbr->cone);
3160 snap->cone_snap = NULL;
3168 static void context_gbr_restore(struct isl_context *context, void *save)
3170 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3171 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3174 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3175 isl_tab_free(cgbr->tab);
3179 if (snap->shifted_snap) {
3180 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3182 } else if (cgbr->shifted) {
3183 isl_tab_free(cgbr->shifted);
3184 cgbr->shifted = NULL;
3187 if (snap->cone_snap) {
3188 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3190 } else if (cgbr->cone) {
3191 isl_tab_free(cgbr->cone);
3200 isl_tab_free(cgbr->tab);
3204 static int context_gbr_is_ok(struct isl_context *context)
3206 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3210 static void context_gbr_invalidate(struct isl_context *context)
3212 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3213 isl_tab_free(cgbr->tab);
3217 static void context_gbr_free(struct isl_context *context)
3219 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3220 isl_tab_free(cgbr->tab);
3221 isl_tab_free(cgbr->shifted);
3222 isl_tab_free(cgbr->cone);
3226 struct isl_context_op isl_context_gbr_op = {
3227 context_gbr_detect_nonnegative_parameters,
3228 context_gbr_peek_basic_set,
3229 context_gbr_peek_tab,
3231 context_gbr_add_ineq,
3232 context_gbr_ineq_sign,
3233 context_gbr_test_ineq,
3234 context_gbr_get_div,
3235 context_gbr_add_div,
3236 context_gbr_detect_equalities,
3237 context_gbr_best_split,
3238 context_gbr_is_empty,
3241 context_gbr_restore,
3242 context_gbr_invalidate,
3246 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3248 struct isl_context_gbr *cgbr;
3253 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3257 cgbr->context.op = &isl_context_gbr_op;
3259 cgbr->shifted = NULL;
3261 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3262 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3265 check_gbr_integer_feasible(cgbr);
3267 return &cgbr->context;
3269 cgbr->context.op->free(&cgbr->context);
3273 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3278 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3279 return isl_context_lex_alloc(dom);
3281 return isl_context_gbr_alloc(dom);
3284 /* Construct an isl_sol_map structure for accumulating the solution.
3285 * If track_empty is set, then we also keep track of the parts
3286 * of the context where there is no solution.
3287 * If max is set, then we are solving a maximization, rather than
3288 * a minimization problem, which means that the variables in the
3289 * tableau have value "M - x" rather than "M + x".
3291 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3292 struct isl_basic_set *dom, int track_empty, int max)
3294 struct isl_sol_map *sol_map = NULL;
3299 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3303 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3304 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3305 sol_map->sol.dec_level.sol = &sol_map->sol;
3306 sol_map->sol.max = max;
3307 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3308 sol_map->sol.add = &sol_map_add_wrap;
3309 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3310 sol_map->sol.free = &sol_map_free_wrap;
3311 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3316 sol_map->sol.context = isl_context_alloc(dom);
3317 if (!sol_map->sol.context)
3321 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3322 1, ISL_SET_DISJOINT);
3323 if (!sol_map->empty)
3327 isl_basic_set_free(dom);
3328 return &sol_map->sol;
3330 isl_basic_set_free(dom);
3331 sol_map_free(sol_map);
3335 /* Check whether all coefficients of (non-parameter) variables
3336 * are non-positive, meaning that no pivots can be performed on the row.
3338 static int is_critical(struct isl_tab *tab, int row)
3341 unsigned off = 2 + tab->M;
3343 for (j = tab->n_dead; j < tab->n_col; ++j) {
3344 if (tab->col_var[j] >= 0 &&
3345 (tab->col_var[j] < tab->n_param ||
3346 tab->col_var[j] >= tab->n_var - tab->n_div))
3349 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3356 /* Check whether the inequality represented by vec is strict over the integers,
3357 * i.e., there are no integer values satisfying the constraint with
3358 * equality. This happens if the gcd of the coefficients is not a divisor
3359 * of the constant term. If so, scale the constraint down by the gcd
3360 * of the coefficients.
3362 static int is_strict(struct isl_vec *vec)
3368 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3369 if (!isl_int_is_one(gcd)) {
3370 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3371 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3372 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3379 /* Determine the sign of the given row of the main tableau.
3380 * The result is one of
3381 * isl_tab_row_pos: always non-negative; no pivot needed
3382 * isl_tab_row_neg: always non-positive; pivot
3383 * isl_tab_row_any: can be both positive and negative; split
3385 * We first handle some simple cases
3386 * - the row sign may be known already
3387 * - the row may be obviously non-negative
3388 * - the parametric constant may be equal to that of another row
3389 * for which we know the sign. This sign will be either "pos" or
3390 * "any". If it had been "neg" then we would have pivoted before.
3392 * If none of these cases hold, we check the value of the row for each
3393 * of the currently active samples. Based on the signs of these values
3394 * we make an initial determination of the sign of the row.
3396 * all zero -> unk(nown)
3397 * all non-negative -> pos
3398 * all non-positive -> neg
3399 * both negative and positive -> all
3401 * If we end up with "all", we are done.
3402 * Otherwise, we perform a check for positive and/or negative
3403 * values as follows.
3405 * samples neg unk pos
3411 * There is no special sign for "zero", because we can usually treat zero
3412 * as either non-negative or non-positive, whatever works out best.
3413 * However, if the row is "critical", meaning that pivoting is impossible
3414 * then we don't want to limp zero with the non-positive case, because
3415 * then we we would lose the solution for those values of the parameters
3416 * where the value of the row is zero. Instead, we treat 0 as non-negative
3417 * ensuring a split if the row can attain both zero and negative values.
3418 * The same happens when the original constraint was one that could not
3419 * be satisfied with equality by any integer values of the parameters.
3420 * In this case, we normalize the constraint, but then a value of zero
3421 * for the normalized constraint is actually a positive value for the
3422 * original constraint, so again we need to treat zero as non-negative.
3423 * In both these cases, we have the following decision tree instead:
3425 * all non-negative -> pos
3426 * all negative -> neg
3427 * both negative and non-negative -> all
3435 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3436 struct isl_sol *sol, int row)
3438 struct isl_vec *ineq = NULL;
3439 enum isl_tab_row_sign res = isl_tab_row_unknown;
3444 if (tab->row_sign[row] != isl_tab_row_unknown)
3445 return tab->row_sign[row];
3446 if (is_obviously_nonneg(tab, row))
3447 return isl_tab_row_pos;
3448 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3449 if (tab->row_sign[row2] == isl_tab_row_unknown)
3451 if (identical_parameter_line(tab, row, row2))
3452 return tab->row_sign[row2];
3455 critical = is_critical(tab, row);
3457 ineq = get_row_parameter_ineq(tab, row);
3461 strict = is_strict(ineq);
3463 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3464 critical || strict);
3466 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3467 /* test for negative values */
3469 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3470 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3472 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3476 res = isl_tab_row_pos;
3478 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3480 if (res == isl_tab_row_neg) {
3481 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3482 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3486 if (res == isl_tab_row_neg) {
3487 /* test for positive values */
3489 if (!critical && !strict)
3490 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3492 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3496 res = isl_tab_row_any;
3503 return isl_tab_row_unknown;
3506 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3508 /* Find solutions for values of the parameters that satisfy the given
3511 * We currently take a snapshot of the context tableau that is reset
3512 * when we return from this function, while we make a copy of the main
3513 * tableau, leaving the original main tableau untouched.
3514 * These are fairly arbitrary choices. Making a copy also of the context
3515 * tableau would obviate the need to undo any changes made to it later,
3516 * while taking a snapshot of the main tableau could reduce memory usage.
3517 * If we were to switch to taking a snapshot of the main tableau,
3518 * we would have to keep in mind that we need to save the row signs
3519 * and that we need to do this before saving the current basis
3520 * such that the basis has been restore before we restore the row signs.
3522 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3528 saved = sol->context->op->save(sol->context);
3530 tab = isl_tab_dup(tab);
3534 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3536 find_solutions(sol, tab);
3539 sol->context->op->restore(sol->context, saved);
3545 /* Record the absence of solutions for those values of the parameters
3546 * that do not satisfy the given inequality with equality.
3548 static void no_sol_in_strict(struct isl_sol *sol,
3549 struct isl_tab *tab, struct isl_vec *ineq)
3554 if (!sol->context || sol->error)
3556 saved = sol->context->op->save(sol->context);
3558 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3560 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3569 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3571 sol->context->op->restore(sol->context, saved);
3577 /* Compute the lexicographic minimum of the set represented by the main
3578 * tableau "tab" within the context "sol->context_tab".
3579 * On entry the sample value of the main tableau is lexicographically
3580 * less than or equal to this lexicographic minimum.
3581 * Pivots are performed until a feasible point is found, which is then
3582 * necessarily equal to the minimum, or until the tableau is found to
3583 * be infeasible. Some pivots may need to be performed for only some
3584 * feasible values of the context tableau. If so, the context tableau
3585 * is split into a part where the pivot is needed and a part where it is not.
3587 * Whenever we enter the main loop, the main tableau is such that no
3588 * "obvious" pivots need to be performed on it, where "obvious" means
3589 * that the given row can be seen to be negative without looking at
3590 * the context tableau. In particular, for non-parametric problems,
3591 * no pivots need to be performed on the main tableau.
3592 * The caller of find_solutions is responsible for making this property
3593 * hold prior to the first iteration of the loop, while restore_lexmin
3594 * is called before every other iteration.
3596 * Inside the main loop, we first examine the signs of the rows of
3597 * the main tableau within the context of the context tableau.
3598 * If we find a row that is always non-positive for all values of
3599 * the parameters satisfying the context tableau and negative for at
3600 * least one value of the parameters, we perform the appropriate pivot
3601 * and start over. An exception is the case where no pivot can be
3602 * performed on the row. In this case, we require that the sign of
3603 * the row is negative for all values of the parameters (rather than just
3604 * non-positive). This special case is handled inside row_sign, which
3605 * will say that the row can have any sign if it determines that it can
3606 * attain both negative and zero values.
3608 * If we can't find a row that always requires a pivot, but we can find
3609 * one or more rows that require a pivot for some values of the parameters
3610 * (i.e., the row can attain both positive and negative signs), then we split
3611 * the context tableau into two parts, one where we force the sign to be
3612 * non-negative and one where we force is to be negative.
3613 * The non-negative part is handled by a recursive call (through find_in_pos).
3614 * Upon returning from this call, we continue with the negative part and
3615 * perform the required pivot.
3617 * If no such rows can be found, all rows are non-negative and we have
3618 * found a (rational) feasible point. If we only wanted a rational point
3620 * Otherwise, we check if all values of the sample point of the tableau
3621 * are integral for the variables. If so, we have found the minimal
3622 * integral point and we are done.
3623 * If the sample point is not integral, then we need to make a distinction
3624 * based on whether the constant term is non-integral or the coefficients
3625 * of the parameters. Furthermore, in order to decide how to handle
3626 * the non-integrality, we also need to know whether the coefficients
3627 * of the other columns in the tableau are integral. This leads
3628 * to the following table. The first two rows do not correspond
3629 * to a non-integral sample point and are only mentioned for completeness.
3631 * constant parameters other
3634 * int int rat | -> no problem
3636 * rat int int -> fail
3638 * rat int rat -> cut
3641 * rat rat rat | -> parametric cut
3644 * rat rat int | -> split context
3646 * If the parametric constant is completely integral, then there is nothing
3647 * to be done. If the constant term is non-integral, but all the other
3648 * coefficient are integral, then there is nothing that can be done
3649 * and the tableau has no integral solution.
3650 * If, on the other hand, one or more of the other columns have rational
3651 * coefficients, but the parameter coefficients are all integral, then
3652 * we can perform a regular (non-parametric) cut.
3653 * Finally, if there is any parameter coefficient that is non-integral,
3654 * then we need to involve the context tableau. There are two cases here.
3655 * If at least one other column has a rational coefficient, then we
3656 * can perform a parametric cut in the main tableau by adding a new
3657 * integer division in the context tableau.
3658 * If all other columns have integral coefficients, then we need to
3659 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3660 * is always integral. We do this by introducing an integer division
3661 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3662 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3663 * Since q is expressed in the tableau as
3664 * c + \sum a_i y_i - m q >= 0
3665 * -c - \sum a_i y_i + m q + m - 1 >= 0
3666 * it is sufficient to add the inequality
3667 * -c - \sum a_i y_i + m q >= 0
3668 * In the part of the context where this inequality does not hold, the
3669 * main tableau is marked as being empty.
3671 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3673 struct isl_context *context;
3676 if (!tab || sol->error)
3679 context = sol->context;
3683 if (context->op->is_empty(context))
3686 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3689 enum isl_tab_row_sign sgn;
3693 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3694 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3696 sgn = row_sign(tab, sol, row);
3699 tab->row_sign[row] = sgn;
3700 if (sgn == isl_tab_row_any)
3702 if (sgn == isl_tab_row_any && split == -1)
3704 if (sgn == isl_tab_row_neg)
3707 if (row < tab->n_row)
3710 struct isl_vec *ineq;
3712 split = context->op->best_split(context, tab);
3715 ineq = get_row_parameter_ineq(tab, split);
3719 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3720 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3722 if (tab->row_sign[row] == isl_tab_row_any)
3723 tab->row_sign[row] = isl_tab_row_unknown;
3725 tab->row_sign[split] = isl_tab_row_pos;
3727 find_in_pos(sol, tab, ineq->el);
3728 tab->row_sign[split] = isl_tab_row_neg;
3730 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3731 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3733 context->op->add_ineq(context, ineq->el, 0, 1);
3741 row = first_non_integer_row(tab, &flags);
3744 if (ISL_FL_ISSET(flags, I_PAR)) {
3745 if (ISL_FL_ISSET(flags, I_VAR)) {
3746 if (isl_tab_mark_empty(tab) < 0)
3750 row = add_cut(tab, row);
3751 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3752 struct isl_vec *div;
3753 struct isl_vec *ineq;
3755 div = get_row_split_div(tab, row);
3758 d = context->op->get_div(context, tab, div);
3762 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3766 no_sol_in_strict(sol, tab, ineq);
3767 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3768 context->op->add_ineq(context, ineq->el, 1, 1);
3770 if (sol->error || !context->op->is_ok(context))
3772 tab = set_row_cst_to_div(tab, row, d);
3773 if (context->op->is_empty(context))
3776 row = add_parametric_cut(tab, row, context);
3791 /* Compute the lexicographic minimum of the set represented by the main
3792 * tableau "tab" within the context "sol->context_tab".
3794 * As a preprocessing step, we first transfer all the purely parametric
3795 * equalities from the main tableau to the context tableau, i.e.,
3796 * parameters that have been pivoted to a row.
3797 * These equalities are ignored by the main algorithm, because the
3798 * corresponding rows may not be marked as being non-negative.
3799 * In parts of the context where the added equality does not hold,
3800 * the main tableau is marked as being empty.
3802 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3811 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3815 if (tab->row_var[row] < 0)
3817 if (tab->row_var[row] >= tab->n_param &&
3818 tab->row_var[row] < tab->n_var - tab->n_div)
3820 if (tab->row_var[row] < tab->n_param)
3821 p = tab->row_var[row];
3823 p = tab->row_var[row]
3824 + tab->n_param - (tab->n_var - tab->n_div);
3826 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3829 get_row_parameter_line(tab, row, eq->el);
3830 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3831 eq = isl_vec_normalize(eq);
3834 no_sol_in_strict(sol, tab, eq);
3836 isl_seq_neg(eq->el, eq->el, eq->size);
3838 no_sol_in_strict(sol, tab, eq);
3839 isl_seq_neg(eq->el, eq->el, eq->size);
3841 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3845 if (isl_tab_mark_redundant(tab, row) < 0)
3848 if (sol->context->op->is_empty(sol->context))
3851 row = tab->n_redundant - 1;
3854 find_solutions(sol, tab);
3865 /* Check if integer division "div" of "dom" also occurs in "bmap".
3866 * If so, return its position within the divs.
3867 * If not, return -1.
3869 static int find_context_div(struct isl_basic_map *bmap,
3870 struct isl_basic_set *dom, unsigned div)
3873 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
3874 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
3876 if (isl_int_is_zero(dom->div[div][0]))
3878 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
3881 for (i = 0; i < bmap->n_div; ++i) {
3882 if (isl_int_is_zero(bmap->div[i][0]))
3884 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
3885 (b_dim - d_dim) + bmap->n_div) != -1)
3887 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
3893 /* The correspondence between the variables in the main tableau,
3894 * the context tableau, and the input map and domain is as follows.
3895 * The first n_param and the last n_div variables of the main tableau
3896 * form the variables of the context tableau.
3897 * In the basic map, these n_param variables correspond to the
3898 * parameters and the input dimensions. In the domain, they correspond
3899 * to the parameters and the set dimensions.
3900 * The n_div variables correspond to the integer divisions in the domain.
3901 * To ensure that everything lines up, we may need to copy some of the
3902 * integer divisions of the domain to the map. These have to be placed
3903 * in the same order as those in the context and they have to be placed
3904 * after any other integer divisions that the map may have.
3905 * This function performs the required reordering.
3907 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
3908 struct isl_basic_set *dom)
3914 for (i = 0; i < dom->n_div; ++i)
3915 if (find_context_div(bmap, dom, i) != -1)
3917 other = bmap->n_div - common;
3918 if (dom->n_div - common > 0) {
3919 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3920 dom->n_div - common, 0, 0);
3924 for (i = 0; i < dom->n_div; ++i) {
3925 int pos = find_context_div(bmap, dom, i);
3927 pos = isl_basic_map_alloc_div(bmap);
3930 isl_int_set_si(bmap->div[pos][0], 0);
3932 if (pos != other + i)
3933 isl_basic_map_swap_div(bmap, pos, other + i);
3937 isl_basic_map_free(bmap);
3941 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3942 * some obvious symmetries.
3944 * We make sure the divs in the domain are properly ordered,
3945 * because they will be added one by one in the given order
3946 * during the construction of the solution map.
3948 static struct isl_sol *basic_map_partial_lexopt_base(
3949 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
3950 __isl_give isl_set **empty, int max,
3951 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
3952 __isl_take isl_basic_set *dom, int track_empty, int max))
3954 struct isl_tab *tab;
3955 struct isl_sol *sol = NULL;
3956 struct isl_context *context;
3959 dom = isl_basic_set_order_divs(dom);
3960 bmap = align_context_divs(bmap, dom);
3962 sol = init(bmap, dom, !!empty, max);
3966 context = sol->context;
3967 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
3969 else if (isl_basic_map_plain_is_empty(bmap)) {
3972 isl_basic_set_copy(context->op->peek_basic_set(context)));
3974 tab = tab_for_lexmin(bmap,
3975 context->op->peek_basic_set(context), 1, max);
3976 tab = context->op->detect_nonnegative_parameters(context, tab);
3977 find_solutions_main(sol, tab);
3982 isl_basic_map_free(bmap);
3986 isl_basic_map_free(bmap);
3990 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
3991 * some obvious symmetries.
3993 * We call basic_map_partial_lexopt_base and extract the results.
3995 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
3996 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
3997 __isl_give isl_set **empty, int max)
3999 isl_map *result = NULL;
4000 struct isl_sol *sol;
4001 struct isl_sol_map *sol_map;
4003 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4007 sol_map = (struct isl_sol_map *) sol;
4009 result = isl_map_copy(sol_map->map);
4011 *empty = isl_set_copy(sol_map->empty);
4012 sol_free(&sol_map->sol);
4016 /* Structure used during detection of parallel constraints.
4017 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4018 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4019 * val: the coefficients of the output variables
4021 struct isl_constraint_equal_info {
4022 isl_basic_map *bmap;
4028 /* Check whether the coefficients of the output variables
4029 * of the constraint in "entry" are equal to info->val.
4031 static int constraint_equal(const void *entry, const void *val)
4033 isl_int **row = (isl_int **)entry;
4034 const struct isl_constraint_equal_info *info = val;
4036 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4039 /* Check whether "bmap" has a pair of constraints that have
4040 * the same coefficients for the output variables.
4041 * Note that the coefficients of the existentially quantified
4042 * variables need to be zero since the existentially quantified
4043 * of the result are usually not the same as those of the input.
4044 * the isl_dim_out and isl_dim_div dimensions.
4045 * If so, return 1 and return the row indices of the two constraints
4046 * in *first and *second.
4048 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4049 int *first, int *second)
4052 isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
4053 struct isl_hash_table *table = NULL;
4054 struct isl_hash_table_entry *entry;
4055 struct isl_constraint_equal_info info;
4059 ctx = isl_basic_map_get_ctx(bmap);
4060 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4064 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4065 isl_basic_map_dim(bmap, isl_dim_in);
4067 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4068 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4069 info.n_out = n_out + n_div;
4070 for (i = 0; i < bmap->n_ineq; ++i) {
4073 info.val = bmap->ineq[i] + 1 + info.n_in;
4074 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4076 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4078 hash = isl_seq_get_hash(info.val, info.n_out);
4079 entry = isl_hash_table_find(ctx, table, hash,
4080 constraint_equal, &info, 1);
4085 entry->data = &bmap->ineq[i];
4088 if (i < bmap->n_ineq) {
4089 *first = ((isl_int **)entry->data) - bmap->ineq;
4093 isl_hash_table_free(ctx, table);
4095 return i < bmap->n_ineq;
4097 isl_hash_table_free(ctx, table);
4101 /* Given a set of upper bounds in "var", add constraints to "bset"
4102 * that make the i-th bound smallest.
4104 * In particular, if there are n bounds b_i, then add the constraints
4106 * b_i <= b_j for j > i
4107 * b_i < b_j for j < i
4109 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4110 __isl_keep isl_mat *var, int i)
4115 ctx = isl_mat_get_ctx(var);
4117 for (j = 0; j < var->n_row; ++j) {
4120 k = isl_basic_set_alloc_inequality(bset);
4123 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4124 ctx->negone, var->row[i], var->n_col);
4125 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4127 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4130 bset = isl_basic_set_finalize(bset);
4134 isl_basic_set_free(bset);
4138 /* Given a set of upper bounds on the last "input" variable m,
4139 * construct a set that assigns the minimal upper bound to m, i.e.,
4140 * construct a set that divides the space into cells where one
4141 * of the upper bounds is smaller than all the others and assign
4142 * this upper bound to m.
4144 * In particular, if there are n bounds b_i, then the result
4145 * consists of n basic sets, each one of the form
4148 * b_i <= b_j for j > i
4149 * b_i < b_j for j < i
4151 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4152 __isl_take isl_mat *var)
4155 isl_basic_set *bset = NULL;
4157 isl_set *set = NULL;
4162 ctx = isl_space_get_ctx(dim);
4163 set = isl_set_alloc_space(isl_space_copy(dim),
4164 var->n_row, ISL_SET_DISJOINT);
4166 for (i = 0; i < var->n_row; ++i) {
4167 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4169 k = isl_basic_set_alloc_equality(bset);
4172 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4173 isl_int_set_si(bset->eq[k][var->n_col], -1);
4174 bset = select_minimum(bset, var, i);
4175 set = isl_set_add_basic_set(set, bset);
4178 isl_space_free(dim);
4182 isl_basic_set_free(bset);
4184 isl_space_free(dim);
4189 /* Given that the last input variable of "bmap" represents the minimum
4190 * of the bounds in "cst", check whether we need to split the domain
4191 * based on which bound attains the minimum.
4193 * A split is needed when the minimum appears in an integer division
4194 * or in an equality. Otherwise, it is only needed if it appears in
4195 * an upper bound that is different from the upper bounds on which it
4198 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4199 __isl_keep isl_mat *cst)
4205 pos = cst->n_col - 1;
4206 total = isl_basic_map_dim(bmap, isl_dim_all);
4208 for (i = 0; i < bmap->n_div; ++i)
4209 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4212 for (i = 0; i < bmap->n_eq; ++i)
4213 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4216 for (i = 0; i < bmap->n_ineq; ++i) {
4217 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4219 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4221 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4222 total - pos - 1) >= 0)
4225 for (j = 0; j < cst->n_row; ++j)
4226 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4228 if (j >= cst->n_row)
4235 /* Given that the last set variable of "bset" represents the minimum
4236 * of the bounds in "cst", check whether we need to split the domain
4237 * based on which bound attains the minimum.
4239 * We simply call need_split_basic_map here. This is safe because
4240 * the position of the minimum is computed from "cst" and not
4243 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4244 __isl_keep isl_mat *cst)
4246 return need_split_basic_map((isl_basic_map *)bset, cst);
4249 /* Given that the last set variable of "set" represents the minimum
4250 * of the bounds in "cst", check whether we need to split the domain
4251 * based on which bound attains the minimum.
4253 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4257 for (i = 0; i < set->n; ++i)
4258 if (need_split_basic_set(set->p[i], cst))
4264 /* Given a set of which the last set variable is the minimum
4265 * of the bounds in "cst", split each basic set in the set
4266 * in pieces where one of the bounds is (strictly) smaller than the others.
4267 * This subdivision is given in "min_expr".
4268 * The variable is subsequently projected out.
4270 * We only do the split when it is needed.
4271 * For example if the last input variable m = min(a,b) and the only
4272 * constraints in the given basic set are lower bounds on m,
4273 * i.e., l <= m = min(a,b), then we can simply project out m
4274 * to obtain l <= a and l <= b, without having to split on whether
4275 * m is equal to a or b.
4277 static __isl_give isl_set *split(__isl_take isl_set *empty,
4278 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4285 if (!empty || !min_expr || !cst)
4288 n_in = isl_set_dim(empty, isl_dim_set);
4289 dim = isl_set_get_space(empty);
4290 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4291 res = isl_set_empty(dim);
4293 for (i = 0; i < empty->n; ++i) {
4296 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4297 if (need_split_basic_set(empty->p[i], cst))
4298 set = isl_set_intersect(set, isl_set_copy(min_expr));
4299 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4301 res = isl_set_union_disjoint(res, set);
4304 isl_set_free(empty);
4305 isl_set_free(min_expr);
4309 isl_set_free(empty);
4310 isl_set_free(min_expr);
4315 /* Given a map of which the last input variable is the minimum
4316 * of the bounds in "cst", split each basic set in the set
4317 * in pieces where one of the bounds is (strictly) smaller than the others.
4318 * This subdivision is given in "min_expr".
4319 * The variable is subsequently projected out.
4321 * The implementation is essentially the same as that of "split".
4323 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4324 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4331 if (!opt || !min_expr || !cst)
4334 n_in = isl_map_dim(opt, isl_dim_in);
4335 dim = isl_map_get_space(opt);
4336 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4337 res = isl_map_empty(dim);
4339 for (i = 0; i < opt->n; ++i) {
4342 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4343 if (need_split_basic_map(opt->p[i], cst))
4344 map = isl_map_intersect_domain(map,
4345 isl_set_copy(min_expr));
4346 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4348 res = isl_map_union_disjoint(res, map);
4352 isl_set_free(min_expr);
4357 isl_set_free(min_expr);
4362 static __isl_give isl_map *basic_map_partial_lexopt(
4363 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4364 __isl_give isl_set **empty, int max);
4369 isl_pw_multi_aff *pma;
4372 /* This function is called from basic_map_partial_lexopt_symm.
4373 * The last variable of "bmap" and "dom" corresponds to the minimum
4374 * of the bounds in "cst". "map_space" is the space of the original
4375 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4376 * is the space of the original domain.
4378 * We recursively call basic_map_partial_lexopt and then plug in
4379 * the definition of the minimum in the result.
4381 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4382 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4383 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4384 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4388 union isl_lex_res res;
4390 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4392 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4395 *empty = split(*empty,
4396 isl_set_copy(min_expr), isl_mat_copy(cst));
4397 *empty = isl_set_reset_space(*empty, set_space);
4400 opt = split_domain(opt, min_expr, cst);
4401 opt = isl_map_reset_space(opt, map_space);
4407 /* Given a basic map with at least two parallel constraints (as found
4408 * by the function parallel_constraints), first look for more constraints
4409 * parallel to the two constraint and replace the found list of parallel
4410 * constraints by a single constraint with as "input" part the minimum
4411 * of the input parts of the list of constraints. Then, recursively call
4412 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4413 * and plug in the definition of the minimum in the result.
4415 * More specifically, given a set of constraints
4419 * Replace this set by a single constraint
4423 * with u a new parameter with constraints
4427 * Any solution to the new system is also a solution for the original system
4430 * a x >= -u >= -b_i(p)
4432 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4433 * therefore be plugged into the solution.
4435 static union isl_lex_res basic_map_partial_lexopt_symm(
4436 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4437 __isl_give isl_set **empty, int max, int first, int second,
4438 __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4439 __isl_take isl_basic_set *dom,
4440 __isl_give isl_set **empty,
4441 int max, __isl_take isl_mat *cst,
4442 __isl_take isl_space *map_space,
4443 __isl_take isl_space *set_space))
4447 unsigned n_in, n_out, n_div;
4449 isl_vec *var = NULL;
4450 isl_mat *cst = NULL;
4451 isl_space *map_space, *set_space;
4452 union isl_lex_res res;
4454 map_space = isl_basic_map_get_space(bmap);
4455 set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4457 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4458 isl_basic_map_dim(bmap, isl_dim_in);
4459 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4461 ctx = isl_basic_map_get_ctx(bmap);
4462 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4463 var = isl_vec_alloc(ctx, n_out);
4469 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4470 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4471 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4475 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4479 for (i = 0; i < n; ++i)
4480 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4482 bmap = isl_basic_map_cow(bmap);
4485 for (i = n - 1; i >= 0; --i)
4486 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4489 bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4490 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4491 k = isl_basic_map_alloc_inequality(bmap);
4494 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4495 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4496 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4497 bmap = isl_basic_map_finalize(bmap);
4499 n_div = isl_basic_set_dim(dom, isl_dim_div);
4500 dom = isl_basic_set_add(dom, isl_dim_set, 1);
4501 dom = isl_basic_set_extend_constraints(dom, 0, n);
4502 for (i = 0; i < n; ++i) {
4503 k = isl_basic_set_alloc_inequality(dom);
4506 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4507 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4508 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4514 return core(bmap, dom, empty, max, cst, map_space, set_space);
4516 isl_space_free(map_space);
4517 isl_space_free(set_space);
4521 isl_basic_set_free(dom);
4522 isl_basic_map_free(bmap);
4527 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4528 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4529 __isl_give isl_set **empty, int max, int first, int second)
4531 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4532 first, second, &basic_map_partial_lexopt_symm_map_core).map;
4535 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4536 * equalities and removing redundant constraints.
4538 * We first check if there are any parallel constraints (left).
4539 * If not, we are in the base case.
4540 * If there are parallel constraints, we replace them by a single
4541 * constraint in basic_map_partial_lexopt_symm and then call
4542 * this function recursively to look for more parallel constraints.
4544 static __isl_give isl_map *basic_map_partial_lexopt(
4545 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4546 __isl_give isl_set **empty, int max)
4554 if (bmap->ctx->opt->pip_symmetry)
4555 par = parallel_constraints(bmap, &first, &second);
4559 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4561 return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4564 isl_basic_set_free(dom);
4565 isl_basic_map_free(bmap);
4569 /* Compute the lexicographic minimum (or maximum if "max" is set)
4570 * of "bmap" over the domain "dom" and return the result as a map.
4571 * If "empty" is not NULL, then *empty is assigned a set that
4572 * contains those parts of the domain where there is no solution.
4573 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4574 * then we compute the rational optimum. Otherwise, we compute
4575 * the integral optimum.
4577 * We perform some preprocessing. As the PILP solver does not
4578 * handle implicit equalities very well, we first make sure all
4579 * the equalities are explicitly available.
4581 * We also add context constraints to the basic map and remove
4582 * redundant constraints. This is only needed because of the
4583 * way we handle simple symmetries. In particular, we currently look
4584 * for symmetries on the constraints, before we set up the main tableau.
4585 * It is then no good to look for symmetries on possibly redundant constraints.
4587 struct isl_map *isl_tab_basic_map_partial_lexopt(
4588 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4589 struct isl_set **empty, int max)
4596 isl_assert(bmap->ctx,
4597 isl_basic_map_compatible_domain(bmap, dom), goto error);
4599 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4600 return basic_map_partial_lexopt(bmap, dom, empty, max);
4602 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4603 bmap = isl_basic_map_detect_equalities(bmap);
4604 bmap = isl_basic_map_remove_redundancies(bmap);
4606 return basic_map_partial_lexopt(bmap, dom, empty, max);
4608 isl_basic_set_free(dom);
4609 isl_basic_map_free(bmap);
4613 struct isl_sol_for {
4615 int (*fn)(__isl_take isl_basic_set *dom,
4616 __isl_take isl_aff_list *list, void *user);
4620 static void sol_for_free(struct isl_sol_for *sol_for)
4622 if (sol_for->sol.context)
4623 sol_for->sol.context->op->free(sol_for->sol.context);
4627 static void sol_for_free_wrap(struct isl_sol *sol)
4629 sol_for_free((struct isl_sol_for *)sol);
4632 /* Add the solution identified by the tableau and the context tableau.
4634 * See documentation of sol_add for more details.
4636 * Instead of constructing a basic map, this function calls a user
4637 * defined function with the current context as a basic set and
4638 * a list of affine expressions representing the relation between
4639 * the input and output. The space over which the affine expressions
4640 * are defined is the same as that of the domain. The number of
4641 * affine expressions in the list is equal to the number of output variables.
4643 static void sol_for_add(struct isl_sol_for *sol,
4644 struct isl_basic_set *dom, struct isl_mat *M)
4648 isl_local_space *ls;
4652 if (sol->sol.error || !dom || !M)
4655 ctx = isl_basic_set_get_ctx(dom);
4656 ls = isl_basic_set_get_local_space(dom);
4657 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4658 for (i = 1; i < M->n_row; ++i) {
4659 aff = isl_aff_alloc(isl_local_space_copy(ls));
4661 isl_int_set(aff->v->el[0], M->row[0][0]);
4662 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4664 aff = isl_aff_normalize(aff);
4665 list = isl_aff_list_add(list, aff);
4667 isl_local_space_free(ls);
4669 dom = isl_basic_set_finalize(dom);
4671 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4674 isl_basic_set_free(dom);
4678 isl_basic_set_free(dom);
4683 static void sol_for_add_wrap(struct isl_sol *sol,
4684 struct isl_basic_set *dom, struct isl_mat *M)
4686 sol_for_add((struct isl_sol_for *)sol, dom, M);
4689 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4690 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4694 struct isl_sol_for *sol_for = NULL;
4696 struct isl_basic_set *dom = NULL;
4698 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4702 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4703 dom = isl_basic_set_universe(dom_dim);
4705 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4706 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4707 sol_for->sol.dec_level.sol = &sol_for->sol;
4709 sol_for->user = user;
4710 sol_for->sol.max = max;
4711 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4712 sol_for->sol.add = &sol_for_add_wrap;
4713 sol_for->sol.add_empty = NULL;
4714 sol_for->sol.free = &sol_for_free_wrap;
4716 sol_for->sol.context = isl_context_alloc(dom);
4717 if (!sol_for->sol.context)
4720 isl_basic_set_free(dom);
4723 isl_basic_set_free(dom);
4724 sol_for_free(sol_for);
4728 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4729 struct isl_tab *tab)
4731 find_solutions_main(&sol_for->sol, tab);
4734 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4735 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4739 struct isl_sol_for *sol_for = NULL;
4741 bmap = isl_basic_map_copy(bmap);
4745 bmap = isl_basic_map_detect_equalities(bmap);
4746 sol_for = sol_for_init(bmap, max, fn, user);
4748 if (isl_basic_map_plain_is_empty(bmap))
4751 struct isl_tab *tab;
4752 struct isl_context *context = sol_for->sol.context;
4753 tab = tab_for_lexmin(bmap,
4754 context->op->peek_basic_set(context), 1, max);
4755 tab = context->op->detect_nonnegative_parameters(context, tab);
4756 sol_for_find_solutions(sol_for, tab);
4757 if (sol_for->sol.error)
4761 sol_free(&sol_for->sol);
4762 isl_basic_map_free(bmap);
4765 sol_free(&sol_for->sol);
4766 isl_basic_map_free(bmap);
4770 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4771 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4775 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4778 /* Check if the given sequence of len variables starting at pos
4779 * represents a trivial (i.e., zero) solution.
4780 * The variables are assumed to be non-negative and to come in pairs,
4781 * with each pair representing a variable of unrestricted sign.
4782 * The solution is trivial if each such pair in the sequence consists
4783 * of two identical values, meaning that the variable being represented
4786 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4793 for (i = 0; i < len; i += 2) {
4797 neg_row = tab->var[pos + i].is_row ?
4798 tab->var[pos + i].index : -1;
4799 pos_row = tab->var[pos + i + 1].is_row ?
4800 tab->var[pos + i + 1].index : -1;
4803 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4805 isl_int_is_zero(tab->mat->row[pos_row][1])))
4808 if (neg_row < 0 || pos_row < 0)
4810 if (isl_int_ne(tab->mat->row[neg_row][1],
4811 tab->mat->row[pos_row][1]))
4818 /* Return the index of the first trivial region or -1 if all regions
4821 static int first_trivial_region(struct isl_tab *tab,
4822 int n_region, struct isl_region *region)
4826 for (i = 0; i < n_region; ++i) {
4827 if (region_is_trivial(tab, region[i].pos, region[i].len))
4834 /* Check if the solution is optimal, i.e., whether the first
4835 * n_op entries are zero.
4837 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4841 for (i = 0; i < n_op; ++i)
4842 if (!isl_int_is_zero(sol->el[1 + i]))
4847 /* Add constraints to "tab" that ensure that any solution is significantly
4848 * better that that represented by "sol". That is, find the first
4849 * relevant (within first n_op) non-zero coefficient and force it (along
4850 * with all previous coefficients) to be zero.
4851 * If the solution is already optimal (all relevant coefficients are zero),
4852 * then just mark the table as empty.
4854 static int force_better_solution(struct isl_tab *tab,
4855 __isl_keep isl_vec *sol, int n_op)
4864 for (i = 0; i < n_op; ++i)
4865 if (!isl_int_is_zero(sol->el[1 + i]))
4869 if (isl_tab_mark_empty(tab) < 0)
4874 ctx = isl_vec_get_ctx(sol);
4875 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4879 for (; i >= 0; --i) {
4881 isl_int_set_si(v->el[1 + i], -1);
4882 if (add_lexmin_eq(tab, v->el) < 0)
4893 struct isl_trivial {
4897 struct isl_tab_undo *snap;
4900 /* Return the lexicographically smallest non-trivial solution of the
4901 * given ILP problem.
4903 * All variables are assumed to be non-negative.
4905 * n_op is the number of initial coordinates to optimize.
4906 * That is, once a solution has been found, we will only continue looking
4907 * for solution that result in significantly better values for those
4908 * initial coordinates. That is, we only continue looking for solutions
4909 * that increase the number of initial zeros in this sequence.
4911 * A solution is non-trivial, if it is non-trivial on each of the
4912 * specified regions. Each region represents a sequence of pairs
4913 * of variables. A solution is non-trivial on such a region if
4914 * at least one of these pairs consists of different values, i.e.,
4915 * such that the non-negative variable represented by the pair is non-zero.
4917 * Whenever a conflict is encountered, all constraints involved are
4918 * reported to the caller through a call to "conflict".
4920 * We perform a simple branch-and-bound backtracking search.
4921 * Each level in the search represents initially trivial region that is forced
4922 * to be non-trivial.
4923 * At each level we consider n cases, where n is the length of the region.
4924 * In terms of the n/2 variables of unrestricted signs being encoded by
4925 * the region, we consider the cases
4928 * x_0 = 0 and x_1 >= 1
4929 * x_0 = 0 and x_1 <= -1
4930 * x_0 = 0 and x_1 = 0 and x_2 >= 1
4931 * x_0 = 0 and x_1 = 0 and x_2 <= -1
4933 * The cases are considered in this order, assuming that each pair
4934 * x_i_a x_i_b represents the value x_i_b - x_i_a.
4935 * That is, x_0 >= 1 is enforced by adding the constraint
4936 * x_0_b - x_0_a >= 1
4938 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
4939 __isl_take isl_basic_set *bset, int n_op, int n_region,
4940 struct isl_region *region,
4941 int (*conflict)(int con, void *user), void *user)
4945 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
4947 isl_vec *sol = isl_vec_alloc(ctx, 0);
4948 struct isl_tab *tab;
4949 struct isl_trivial *triv = NULL;
4952 tab = tab_for_lexmin(bset, NULL, 0, 0);
4955 tab->conflict = conflict;
4956 tab->conflict_user = user;
4958 v = isl_vec_alloc(ctx, 1 + tab->n_var);
4959 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
4966 while (level >= 0) {
4970 tab = cut_to_integer_lexmin(tab, CUT_ONE);
4975 r = first_trivial_region(tab, n_region, region);
4977 for (i = 0; i < level; ++i)
4980 sol = isl_tab_get_sample_value(tab);
4983 if (is_optimal(sol, n_op))
4987 if (level >= n_region)
4988 isl_die(ctx, isl_error_internal,
4989 "nesting level too deep", goto error);
4990 if (isl_tab_extend_cons(tab,
4991 2 * region[r].len + 2 * n_op) < 0)
4993 triv[level].region = r;
4994 triv[level].side = 0;
4997 r = triv[level].region;
4998 side = triv[level].side;
4999 base = 2 * (side/2);
5001 if (side >= region[r].len) {
5006 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5011 if (triv[level].update) {
5012 if (force_better_solution(tab, sol, n_op) < 0)
5014 triv[level].update = 0;
5017 if (side == base && base >= 2) {
5018 for (j = base - 2; j < base; ++j) {
5020 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5021 if (add_lexmin_eq(tab, v->el) < 0)
5026 triv[level].snap = isl_tab_snap(tab);
5027 if (isl_tab_push_basis(tab) < 0)
5031 isl_int_set_si(v->el[0], -1);
5032 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5033 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5034 tab = add_lexmin_ineq(tab, v->el);
5044 isl_basic_set_free(bset);
5051 isl_basic_set_free(bset);
5056 /* Return the lexicographically smallest rational point in "bset",
5057 * assuming that all variables are non-negative.
5058 * If "bset" is empty, then return a zero-length vector.
5060 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5061 __isl_take isl_basic_set *bset)
5063 struct isl_tab *tab;
5064 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
5067 tab = tab_for_lexmin(bset, NULL, 0, 0);
5071 sol = isl_vec_alloc(ctx, 0);
5073 sol = isl_tab_get_sample_value(tab);
5075 isl_basic_set_free(bset);
5079 isl_basic_set_free(bset);
5083 struct isl_sol_pma {
5085 isl_pw_multi_aff *pma;
5089 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5093 if (sol_pma->sol.context)
5094 sol_pma->sol.context->op->free(sol_pma->sol.context);
5095 isl_pw_multi_aff_free(sol_pma->pma);
5096 isl_set_free(sol_pma->empty);
5100 /* This function is called for parts of the context where there is
5101 * no solution, with "bset" corresponding to the context tableau.
5102 * Simply add the basic set to the set "empty".
5104 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5105 __isl_take isl_basic_set *bset)
5109 isl_assert(bset->ctx, sol->empty, goto error);
5111 sol->empty = isl_set_grow(sol->empty, 1);
5112 bset = isl_basic_set_simplify(bset);
5113 bset = isl_basic_set_finalize(bset);
5114 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5119 isl_basic_set_free(bset);
5123 /* Given a basic map "dom" that represents the context and an affine
5124 * matrix "M" that maps the dimensions of the context to the
5125 * output variables, construct an isl_pw_multi_aff with a single
5126 * cell corresponding to "dom" and affine expressions copied from "M".
5128 static void sol_pma_add(struct isl_sol_pma *sol,
5129 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5132 isl_local_space *ls;
5134 isl_multi_aff *maff;
5135 isl_pw_multi_aff *pma;
5137 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5138 ls = isl_basic_set_get_local_space(dom);
5139 for (i = 1; i < M->n_row; ++i) {
5140 aff = isl_aff_alloc(isl_local_space_copy(ls));
5142 isl_int_set(aff->v->el[0], M->row[0][0]);
5143 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5145 aff = isl_aff_normalize(aff);
5146 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5148 isl_local_space_free(ls);
5150 dom = isl_basic_set_simplify(dom);
5151 dom = isl_basic_set_finalize(dom);
5152 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5153 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5158 static void sol_pma_free_wrap(struct isl_sol *sol)
5160 sol_pma_free((struct isl_sol_pma *)sol);
5163 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5164 __isl_take isl_basic_set *bset)
5166 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5169 static void sol_pma_add_wrap(struct isl_sol *sol,
5170 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5172 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5175 /* Construct an isl_sol_pma structure for accumulating the solution.
5176 * If track_empty is set, then we also keep track of the parts
5177 * of the context where there is no solution.
5178 * If max is set, then we are solving a maximization, rather than
5179 * a minimization problem, which means that the variables in the
5180 * tableau have value "M - x" rather than "M + x".
5182 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5183 __isl_take isl_basic_set *dom, int track_empty, int max)
5185 struct isl_sol_pma *sol_pma = NULL;
5190 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5194 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5195 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5196 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5197 sol_pma->sol.max = max;
5198 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5199 sol_pma->sol.add = &sol_pma_add_wrap;
5200 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5201 sol_pma->sol.free = &sol_pma_free_wrap;
5202 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5206 sol_pma->sol.context = isl_context_alloc(dom);
5207 if (!sol_pma->sol.context)
5211 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5212 1, ISL_SET_DISJOINT);
5213 if (!sol_pma->empty)
5217 isl_basic_set_free(dom);
5218 return &sol_pma->sol;
5220 isl_basic_set_free(dom);
5221 sol_pma_free(sol_pma);
5225 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5226 * some obvious symmetries.
5228 * We call basic_map_partial_lexopt_base and extract the results.
5230 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5231 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5232 __isl_give isl_set **empty, int max)
5234 isl_pw_multi_aff *result = NULL;
5235 struct isl_sol *sol;
5236 struct isl_sol_pma *sol_pma;
5238 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5242 sol_pma = (struct isl_sol_pma *) sol;
5244 result = isl_pw_multi_aff_copy(sol_pma->pma);
5246 *empty = isl_set_copy(sol_pma->empty);
5247 sol_free(&sol_pma->sol);
5251 /* Given that the last input variable of "maff" represents the minimum
5252 * of some bounds, check whether we need to plug in the expression
5255 * In particular, check if the last input variable appears in any
5256 * of the expressions in "maff".
5258 static int need_substitution(__isl_keep isl_multi_aff *maff)
5263 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5265 for (i = 0; i < maff->n; ++i)
5266 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5272 /* Given a set of upper bounds on the last "input" variable m,
5273 * construct a piecewise affine expression that selects
5274 * the minimal upper bound to m, i.e.,
5275 * divide the space into cells where one
5276 * of the upper bounds is smaller than all the others and select
5277 * this upper bound on that cell.
5279 * In particular, if there are n bounds b_i, then the result
5280 * consists of n cell, each one of the form
5282 * b_i <= b_j for j > i
5283 * b_i < b_j for j < i
5285 * The affine expression on this cell is
5289 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5290 __isl_take isl_mat *var)
5293 isl_aff *aff = NULL;
5294 isl_basic_set *bset = NULL;
5296 isl_pw_aff *paff = NULL;
5297 isl_space *pw_space;
5298 isl_local_space *ls = NULL;
5303 ctx = isl_space_get_ctx(space);
5304 ls = isl_local_space_from_space(isl_space_copy(space));
5305 pw_space = isl_space_copy(space);
5306 pw_space = isl_space_from_domain(pw_space);
5307 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5308 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5310 for (i = 0; i < var->n_row; ++i) {
5313 aff = isl_aff_alloc(isl_local_space_copy(ls));
5314 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5318 isl_int_set_si(aff->v->el[0], 1);
5319 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5320 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5321 bset = select_minimum(bset, var, i);
5322 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5323 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5326 isl_local_space_free(ls);
5327 isl_space_free(space);
5332 isl_basic_set_free(bset);
5333 isl_pw_aff_free(paff);
5334 isl_local_space_free(ls);
5335 isl_space_free(space);
5340 /* Given a piecewise multi-affine expression of which the last input variable
5341 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5342 * This minimum expression is given in "min_expr_pa".
5343 * The set "min_expr" contains the same information, but in the form of a set.
5344 * The variable is subsequently projected out.
5346 * The implementation is similar to those of "split" and "split_domain".
5347 * If the variable appears in a given expression, then minimum expression
5348 * is plugged in. Otherwise, if the variable appears in the constraints
5349 * and a split is required, then the domain is split. Otherwise, no split
5352 static __isl_give isl_pw_multi_aff *split_domain_pma(
5353 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5354 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5359 isl_pw_multi_aff *res;
5361 if (!opt || !min_expr || !cst)
5364 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5365 space = isl_pw_multi_aff_get_space(opt);
5366 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5367 res = isl_pw_multi_aff_empty(space);
5369 for (i = 0; i < opt->n; ++i) {
5370 isl_pw_multi_aff *pma;
5372 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5373 isl_multi_aff_copy(opt->p[i].maff));
5374 if (need_substitution(opt->p[i].maff))
5375 pma = isl_pw_multi_aff_substitute(pma,
5376 isl_dim_in, n_in - 1, min_expr_pa);
5377 else if (need_split_set(opt->p[i].set, cst))
5378 pma = isl_pw_multi_aff_intersect_domain(pma,
5379 isl_set_copy(min_expr));
5380 pma = isl_pw_multi_aff_project_out(pma,
5381 isl_dim_in, n_in - 1, 1);
5383 res = isl_pw_multi_aff_add_disjoint(res, pma);
5386 isl_pw_multi_aff_free(opt);
5387 isl_pw_aff_free(min_expr_pa);
5388 isl_set_free(min_expr);
5392 isl_pw_multi_aff_free(opt);
5393 isl_pw_aff_free(min_expr_pa);
5394 isl_set_free(min_expr);
5399 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5400 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5401 __isl_give isl_set **empty, int max);
5403 /* This function is called from basic_map_partial_lexopt_symm.
5404 * The last variable of "bmap" and "dom" corresponds to the minimum
5405 * of the bounds in "cst". "map_space" is the space of the original
5406 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5407 * is the space of the original domain.
5409 * We recursively call basic_map_partial_lexopt and then plug in
5410 * the definition of the minimum in the result.
5412 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5413 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5414 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5415 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5417 isl_pw_multi_aff *opt;
5418 isl_pw_aff *min_expr_pa;
5420 union isl_lex_res res;
5422 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5423 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5426 opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5429 *empty = split(*empty,
5430 isl_set_copy(min_expr), isl_mat_copy(cst));
5431 *empty = isl_set_reset_space(*empty, set_space);
5434 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5435 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5441 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5442 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5443 __isl_give isl_set **empty, int max, int first, int second)
5445 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5446 first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5449 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5450 * equalities and removing redundant constraints.
5452 * We first check if there are any parallel constraints (left).
5453 * If not, we are in the base case.
5454 * If there are parallel constraints, we replace them by a single
5455 * constraint in basic_map_partial_lexopt_symm_pma and then call
5456 * this function recursively to look for more parallel constraints.
5458 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5459 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5460 __isl_give isl_set **empty, int max)
5468 if (bmap->ctx->opt->pip_symmetry)
5469 par = parallel_constraints(bmap, &first, &second);
5473 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5475 return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5478 isl_basic_set_free(dom);
5479 isl_basic_map_free(bmap);
5483 /* Compute the lexicographic minimum (or maximum if "max" is set)
5484 * of "bmap" over the domain "dom" and return the result as a piecewise
5485 * multi-affine expression.
5486 * If "empty" is not NULL, then *empty is assigned a set that
5487 * contains those parts of the domain where there is no solution.
5488 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5489 * then we compute the rational optimum. Otherwise, we compute
5490 * the integral optimum.
5492 * We perform some preprocessing. As the PILP solver does not
5493 * handle implicit equalities very well, we first make sure all
5494 * the equalities are explicitly available.
5496 * We also add context constraints to the basic map and remove
5497 * redundant constraints. This is only needed because of the
5498 * way we handle simple symmetries. In particular, we currently look
5499 * for symmetries on the constraints, before we set up the main tableau.
5500 * It is then no good to look for symmetries on possibly redundant constraints.
5502 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5503 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5504 __isl_give isl_set **empty, int max)
5511 isl_assert(bmap->ctx,
5512 isl_basic_map_compatible_domain(bmap, dom), goto error);
5514 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5515 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5517 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5518 bmap = isl_basic_map_detect_equalities(bmap);
5519 bmap = isl_basic_map_remove_redundancies(bmap);
5521 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5523 isl_basic_set_free(dom);
5524 isl_basic_map_free(bmap);