isl_tab_pip.c: propagate_equalities: return indication of failure
[isl.git] / isl_tab_pip.c
blob1f823a32775864dea8058849724013ace8639bb2
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_ctx_private.h>
14 #include "isl_map_private.h"
15 #include <isl/seq.h>
16 #include "isl_tab.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
27 * (and others).
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.
62 struct isl_context;
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,
90 struct isl_vec *div);
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,
94 struct isl_tab *tab);
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 /* discard saved context */
106 void (*discard)(void *);
107 /* invalidate context */
108 void (*invalidate)(struct isl_context *context);
109 /* free context */
110 void (*free)(struct isl_context *context);
113 struct isl_context {
114 struct isl_context_op *op;
117 struct isl_context_lex {
118 struct isl_context context;
119 struct isl_tab *tab;
122 /* A stack (linked list) of solutions of subtrees of the search space.
124 * "M" describes the solution in terms of the dimensions of "dom".
125 * The number of columns of "M" is one more than the total number
126 * of dimensions of "dom".
128 * If "M" is NULL, then there is no solution on "dom".
130 struct isl_partial_sol {
131 int level;
132 struct isl_basic_set *dom;
133 struct isl_mat *M;
135 struct isl_partial_sol *next;
138 struct isl_sol;
139 struct isl_sol_callback {
140 struct isl_tab_callback callback;
141 struct isl_sol *sol;
144 /* isl_sol is an interface for constructing a solution to
145 * a parametric integer linear programming problem.
146 * Every time the algorithm reaches a state where a solution
147 * can be read off from the tableau (including cases where the tableau
148 * is empty), the function "add" is called on the isl_sol passed
149 * to find_solutions_main.
151 * The context tableau is owned by isl_sol and is updated incrementally.
153 * There are currently two implementations of this interface,
154 * isl_sol_map, which simply collects the solutions in an isl_map
155 * and (optionally) the parts of the context where there is no solution
156 * in an isl_set, and
157 * isl_sol_for, which calls a user-defined function for each part of
158 * the solution.
160 struct isl_sol {
161 int error;
162 int rational;
163 int level;
164 int max;
165 int n_out;
166 struct isl_context *context;
167 struct isl_partial_sol *partial;
168 void (*add)(struct isl_sol *sol,
169 struct isl_basic_set *dom, struct isl_mat *M);
170 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
171 void (*free)(struct isl_sol *sol);
172 struct isl_sol_callback dec_level;
175 static void sol_free(struct isl_sol *sol)
177 struct isl_partial_sol *partial, *next;
178 if (!sol)
179 return;
180 for (partial = sol->partial; partial; partial = next) {
181 next = partial->next;
182 isl_basic_set_free(partial->dom);
183 isl_mat_free(partial->M);
184 free(partial);
186 sol->free(sol);
189 /* Push a partial solution represented by a domain and mapping M
190 * onto the stack of partial solutions.
192 static void sol_push_sol(struct isl_sol *sol,
193 struct isl_basic_set *dom, struct isl_mat *M)
195 struct isl_partial_sol *partial;
197 if (sol->error || !dom)
198 goto error;
200 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
201 if (!partial)
202 goto error;
204 partial->level = sol->level;
205 partial->dom = dom;
206 partial->M = M;
207 partial->next = sol->partial;
209 sol->partial = partial;
211 return;
212 error:
213 isl_basic_set_free(dom);
214 isl_mat_free(M);
215 sol->error = 1;
218 /* Pop one partial solution from the partial solution stack and
219 * pass it on to sol->add or sol->add_empty.
221 static void sol_pop_one(struct isl_sol *sol)
223 struct isl_partial_sol *partial;
225 partial = sol->partial;
226 sol->partial = partial->next;
228 if (partial->M)
229 sol->add(sol, partial->dom, partial->M);
230 else
231 sol->add_empty(sol, partial->dom);
232 free(partial);
235 /* Return a fresh copy of the domain represented by the context tableau.
237 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
239 struct isl_basic_set *bset;
241 if (sol->error)
242 return NULL;
244 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
245 bset = isl_basic_set_update_from_tab(bset,
246 sol->context->op->peek_tab(sol->context));
248 return bset;
251 /* Check whether two partial solutions have the same mapping, where n_div
252 * is the number of divs that the two partial solutions have in common.
254 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
255 unsigned n_div)
257 int i;
258 unsigned dim;
260 if (!s1->M != !s2->M)
261 return 0;
262 if (!s1->M)
263 return 1;
265 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
267 for (i = 0; i < s1->M->n_row; ++i) {
268 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
269 s1->M->n_col-1-dim-n_div) != -1)
270 return 0;
271 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
272 s2->M->n_col-1-dim-n_div) != -1)
273 return 0;
274 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
275 return 0;
277 return 1;
280 /* Pop all solutions from the partial solution stack that were pushed onto
281 * the stack at levels that are deeper than the current level.
282 * If the two topmost elements on the stack have the same level
283 * and represent the same solution, then their domains are combined.
284 * This combined domain is the same as the current context domain
285 * as sol_pop is called each time we move back to a higher level.
287 static void sol_pop(struct isl_sol *sol)
289 struct isl_partial_sol *partial;
290 unsigned n_div;
292 if (sol->error)
293 return;
295 if (sol->level == 0) {
296 for (partial = sol->partial; partial; partial = sol->partial)
297 sol_pop_one(sol);
298 return;
301 partial = sol->partial;
302 if (!partial)
303 return;
305 if (partial->level <= sol->level)
306 return;
308 if (partial->next && partial->next->level == partial->level) {
309 n_div = isl_basic_set_dim(
310 sol->context->op->peek_basic_set(sol->context),
311 isl_dim_div);
313 if (!same_solution(partial, partial->next, n_div)) {
314 sol_pop_one(sol);
315 sol_pop_one(sol);
316 } else {
317 struct isl_basic_set *bset;
318 isl_mat *M;
319 unsigned n;
321 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
322 n -= n_div;
323 bset = sol_domain(sol);
324 isl_basic_set_free(partial->next->dom);
325 partial->next->dom = bset;
326 M = partial->next->M;
327 if (M) {
328 M = isl_mat_drop_cols(M, M->n_col - n, n);
329 partial->next->M = M;
330 if (!M)
331 goto error;
333 partial->next->level = sol->level;
335 if (!bset)
336 goto error;
338 sol->partial = partial->next;
339 isl_basic_set_free(partial->dom);
340 isl_mat_free(partial->M);
341 free(partial);
343 } else
344 sol_pop_one(sol);
346 if (0)
347 error: sol->error = 1;
350 static void sol_dec_level(struct isl_sol *sol)
352 if (sol->error)
353 return;
355 sol->level--;
357 sol_pop(sol);
360 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
362 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
364 sol_dec_level(callback->sol);
366 return callback->sol->error ? -1 : 0;
369 /* Move down to next level and push callback onto context tableau
370 * to decrease the level again when it gets rolled back across
371 * the current state. That is, dec_level will be called with
372 * the context tableau in the same state as it is when inc_level
373 * is called.
375 static void sol_inc_level(struct isl_sol *sol)
377 struct isl_tab *tab;
379 if (sol->error)
380 return;
382 sol->level++;
383 tab = sol->context->op->peek_tab(sol->context);
384 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
385 sol->error = 1;
388 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
390 int i;
392 if (isl_int_is_one(m))
393 return;
395 for (i = 0; i < n_row; ++i)
396 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
399 /* Add the solution identified by the tableau and the context tableau.
401 * The layout of the variables is as follows.
402 * tab->n_var is equal to the total number of variables in the input
403 * map (including divs that were copied from the context)
404 * + the number of extra divs constructed
405 * Of these, the first tab->n_param and the last tab->n_div variables
406 * correspond to the variables in the context, i.e.,
407 * tab->n_param + tab->n_div = context_tab->n_var
408 * tab->n_param is equal to the number of parameters and input
409 * dimensions in the input map
410 * tab->n_div is equal to the number of divs in the context
412 * If there is no solution, then call add_empty with a basic set
413 * that corresponds to the context tableau. (If add_empty is NULL,
414 * then do nothing).
416 * If there is a solution, then first construct a matrix that maps
417 * all dimensions of the context to the output variables, i.e.,
418 * the output dimensions in the input map.
419 * The divs in the input map (if any) that do not correspond to any
420 * div in the context do not appear in the solution.
421 * The algorithm will make sure that they have an integer value,
422 * but these values themselves are of no interest.
423 * We have to be careful not to drop or rearrange any divs in the
424 * context because that would change the meaning of the matrix.
426 * To extract the value of the output variables, it should be noted
427 * that we always use a big parameter M in the main tableau and so
428 * the variable stored in this tableau is not an output variable x itself, but
429 * x' = M + x (in case of minimization)
430 * or
431 * x' = M - x (in case of maximization)
432 * If x' appears in a column, then its optimal value is zero,
433 * which means that the optimal value of x is an unbounded number
434 * (-M for minimization and M for maximization).
435 * We currently assume that the output dimensions in the original map
436 * are bounded, so this cannot occur.
437 * Similarly, when x' appears in a row, then the coefficient of M in that
438 * row is necessarily 1.
439 * If the row in the tableau represents
440 * d x' = c + d M + e(y)
441 * then, in case of minimization, the corresponding row in the matrix
442 * will be
443 * a c + a e(y)
444 * with a d = m, the (updated) common denominator of the matrix.
445 * In case of maximization, the row will be
446 * -a c - a e(y)
448 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
450 struct isl_basic_set *bset = NULL;
451 struct isl_mat *mat = NULL;
452 unsigned off;
453 int row;
454 isl_int m;
456 if (sol->error || !tab)
457 goto error;
459 if (tab->empty && !sol->add_empty)
460 return;
461 if (sol->context->op->is_empty(sol->context))
462 return;
464 bset = sol_domain(sol);
466 if (tab->empty) {
467 sol_push_sol(sol, bset, NULL);
468 return;
471 off = 2 + tab->M;
473 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
474 1 + tab->n_param + tab->n_div);
475 if (!mat)
476 goto error;
478 isl_int_init(m);
480 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
481 isl_int_set_si(mat->row[0][0], 1);
482 for (row = 0; row < sol->n_out; ++row) {
483 int i = tab->n_param + row;
484 int r, j;
486 isl_seq_clr(mat->row[1 + row], mat->n_col);
487 if (!tab->var[i].is_row) {
488 if (tab->M)
489 isl_die(mat->ctx, isl_error_invalid,
490 "unbounded optimum", goto error2);
491 continue;
494 r = tab->var[i].index;
495 if (tab->M &&
496 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
497 isl_die(mat->ctx, isl_error_invalid,
498 "unbounded optimum", goto error2);
499 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
500 isl_int_divexact(m, tab->mat->row[r][0], m);
501 scale_rows(mat, m, 1 + row);
502 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
503 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
504 for (j = 0; j < tab->n_param; ++j) {
505 int col;
506 if (tab->var[j].is_row)
507 continue;
508 col = tab->var[j].index;
509 isl_int_mul(mat->row[1 + row][1 + j], m,
510 tab->mat->row[r][off + col]);
512 for (j = 0; j < tab->n_div; ++j) {
513 int col;
514 if (tab->var[tab->n_var - tab->n_div+j].is_row)
515 continue;
516 col = tab->var[tab->n_var - tab->n_div+j].index;
517 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
518 tab->mat->row[r][off + col]);
520 if (sol->max)
521 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
522 mat->n_col);
525 isl_int_clear(m);
527 sol_push_sol(sol, bset, mat);
528 return;
529 error2:
530 isl_int_clear(m);
531 error:
532 isl_basic_set_free(bset);
533 isl_mat_free(mat);
534 sol->error = 1;
537 struct isl_sol_map {
538 struct isl_sol sol;
539 struct isl_map *map;
540 struct isl_set *empty;
543 static void sol_map_free(struct isl_sol_map *sol_map)
545 if (!sol_map)
546 return;
547 if (sol_map->sol.context)
548 sol_map->sol.context->op->free(sol_map->sol.context);
549 isl_map_free(sol_map->map);
550 isl_set_free(sol_map->empty);
551 free(sol_map);
554 static void sol_map_free_wrap(struct isl_sol *sol)
556 sol_map_free((struct isl_sol_map *)sol);
559 /* This function is called for parts of the context where there is
560 * no solution, with "bset" corresponding to the context tableau.
561 * Simply add the basic set to the set "empty".
563 static void sol_map_add_empty(struct isl_sol_map *sol,
564 struct isl_basic_set *bset)
566 if (!bset)
567 goto error;
568 isl_assert(bset->ctx, sol->empty, goto error);
570 sol->empty = isl_set_grow(sol->empty, 1);
571 bset = isl_basic_set_simplify(bset);
572 bset = isl_basic_set_finalize(bset);
573 sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
574 if (!sol->empty)
575 goto error;
576 isl_basic_set_free(bset);
577 return;
578 error:
579 isl_basic_set_free(bset);
580 sol->sol.error = 1;
583 static void sol_map_add_empty_wrap(struct isl_sol *sol,
584 struct isl_basic_set *bset)
586 sol_map_add_empty((struct isl_sol_map *)sol, bset);
589 /* Given a basic map "dom" that represents the context and an affine
590 * matrix "M" that maps the dimensions of the context to the
591 * output variables, construct a basic map with the same parameters
592 * and divs as the context, the dimensions of the context as input
593 * dimensions and a number of output dimensions that is equal to
594 * the number of output dimensions in the input map.
596 * The constraints and divs of the context are simply copied
597 * from "dom". For each row
598 * x = c + e(y)
599 * an equality
600 * c + e(y) - d x = 0
601 * is added, with d the common denominator of M.
603 static void sol_map_add(struct isl_sol_map *sol,
604 struct isl_basic_set *dom, struct isl_mat *M)
606 int i;
607 struct isl_basic_map *bmap = NULL;
608 unsigned n_eq;
609 unsigned n_ineq;
610 unsigned nparam;
611 unsigned total;
612 unsigned n_div;
613 unsigned n_out;
615 if (sol->sol.error || !dom || !M)
616 goto error;
618 n_out = sol->sol.n_out;
619 n_eq = dom->n_eq + n_out;
620 n_ineq = dom->n_ineq;
621 n_div = dom->n_div;
622 nparam = isl_basic_set_total_dim(dom) - n_div;
623 total = isl_map_dim(sol->map, isl_dim_all);
624 bmap = isl_basic_map_alloc_space(isl_map_get_space(sol->map),
625 n_div, n_eq, 2 * n_div + n_ineq);
626 if (!bmap)
627 goto error;
628 if (sol->sol.rational)
629 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
630 for (i = 0; i < dom->n_div; ++i) {
631 int k = isl_basic_map_alloc_div(bmap);
632 if (k < 0)
633 goto error;
634 isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
635 isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
636 isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
637 dom->div[i] + 1 + 1 + nparam, i);
639 for (i = 0; i < dom->n_eq; ++i) {
640 int k = isl_basic_map_alloc_equality(bmap);
641 if (k < 0)
642 goto error;
643 isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
644 isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
645 isl_seq_cpy(bmap->eq[k] + 1 + total,
646 dom->eq[i] + 1 + nparam, n_div);
648 for (i = 0; i < dom->n_ineq; ++i) {
649 int k = isl_basic_map_alloc_inequality(bmap);
650 if (k < 0)
651 goto error;
652 isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
653 isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
654 isl_seq_cpy(bmap->ineq[k] + 1 + total,
655 dom->ineq[i] + 1 + nparam, n_div);
657 for (i = 0; i < M->n_row - 1; ++i) {
658 int k = isl_basic_map_alloc_equality(bmap);
659 if (k < 0)
660 goto error;
661 isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
662 isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
663 isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
664 isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
665 M->row[1 + i] + 1 + nparam, n_div);
667 bmap = isl_basic_map_simplify(bmap);
668 bmap = isl_basic_map_finalize(bmap);
669 sol->map = isl_map_grow(sol->map, 1);
670 sol->map = isl_map_add_basic_map(sol->map, bmap);
671 isl_basic_set_free(dom);
672 isl_mat_free(M);
673 if (!sol->map)
674 sol->sol.error = 1;
675 return;
676 error:
677 isl_basic_set_free(dom);
678 isl_mat_free(M);
679 isl_basic_map_free(bmap);
680 sol->sol.error = 1;
683 static void sol_map_add_wrap(struct isl_sol *sol,
684 struct isl_basic_set *dom, struct isl_mat *M)
686 sol_map_add((struct isl_sol_map *)sol, dom, M);
690 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
691 * i.e., the constant term and the coefficients of all variables that
692 * appear in the context tableau.
693 * Note that the coefficient of the big parameter M is NOT copied.
694 * The context tableau may not have a big parameter and even when it
695 * does, it is a different big parameter.
697 static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
699 int i;
700 unsigned off = 2 + tab->M;
702 isl_int_set(line[0], tab->mat->row[row][1]);
703 for (i = 0; i < tab->n_param; ++i) {
704 if (tab->var[i].is_row)
705 isl_int_set_si(line[1 + i], 0);
706 else {
707 int col = tab->var[i].index;
708 isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
711 for (i = 0; i < tab->n_div; ++i) {
712 if (tab->var[tab->n_var - tab->n_div + i].is_row)
713 isl_int_set_si(line[1 + tab->n_param + i], 0);
714 else {
715 int col = tab->var[tab->n_var - tab->n_div + i].index;
716 isl_int_set(line[1 + tab->n_param + i],
717 tab->mat->row[row][off + col]);
722 /* Check if rows "row1" and "row2" have identical "parametric constants",
723 * as explained above.
724 * In this case, we also insist that the coefficients of the big parameter
725 * be the same as the values of the constants will only be the same
726 * if these coefficients are also the same.
728 static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
730 int i;
731 unsigned off = 2 + tab->M;
733 if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
734 return 0;
736 if (tab->M && isl_int_ne(tab->mat->row[row1][2],
737 tab->mat->row[row2][2]))
738 return 0;
740 for (i = 0; i < tab->n_param + tab->n_div; ++i) {
741 int pos = i < tab->n_param ? i :
742 tab->n_var - tab->n_div + i - tab->n_param;
743 int col;
745 if (tab->var[pos].is_row)
746 continue;
747 col = tab->var[pos].index;
748 if (isl_int_ne(tab->mat->row[row1][off + col],
749 tab->mat->row[row2][off + col]))
750 return 0;
752 return 1;
755 /* Return an inequality that expresses that the "parametric constant"
756 * should be non-negative.
757 * This function is only called when the coefficient of the big parameter
758 * is equal to zero.
760 static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
762 struct isl_vec *ineq;
764 ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
765 if (!ineq)
766 return NULL;
768 get_row_parameter_line(tab, row, ineq->el);
769 if (ineq)
770 ineq = isl_vec_normalize(ineq);
772 return ineq;
775 /* Normalize a div expression of the form
777 * [(g*f(x) + c)/(g * m)]
779 * with c the constant term and f(x) the remaining coefficients, to
781 * [(f(x) + [c/g])/m]
783 static void normalize_div(__isl_keep isl_vec *div)
785 isl_ctx *ctx = isl_vec_get_ctx(div);
786 int len = div->size - 2;
788 isl_seq_gcd(div->el + 2, len, &ctx->normalize_gcd);
789 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, div->el[0]);
791 if (isl_int_is_one(ctx->normalize_gcd))
792 return;
794 isl_int_divexact(div->el[0], div->el[0], ctx->normalize_gcd);
795 isl_int_fdiv_q(div->el[1], div->el[1], ctx->normalize_gcd);
796 isl_seq_scale_down(div->el + 2, div->el + 2, ctx->normalize_gcd, len);
799 /* Return a integer division for use in a parametric cut based on the given row.
800 * In particular, let the parametric constant of the row be
802 * \sum_i a_i y_i
804 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
805 * The div returned is equal to
807 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
809 static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
811 struct isl_vec *div;
813 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
814 if (!div)
815 return NULL;
817 isl_int_set(div->el[0], tab->mat->row[row][0]);
818 get_row_parameter_line(tab, row, div->el + 1);
819 isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
820 normalize_div(div);
821 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
823 return div;
826 /* Return a integer division for use in transferring an integrality constraint
827 * to the context.
828 * In particular, let the parametric constant of the row be
830 * \sum_i a_i y_i
832 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
833 * The the returned div is equal to
835 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
837 static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
839 struct isl_vec *div;
841 div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
842 if (!div)
843 return NULL;
845 isl_int_set(div->el[0], tab->mat->row[row][0]);
846 get_row_parameter_line(tab, row, div->el + 1);
847 normalize_div(div);
848 isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
850 return div;
853 /* Construct and return an inequality that expresses an upper bound
854 * on the given div.
855 * In particular, if the div is given by
857 * d = floor(e/m)
859 * then the inequality expresses
861 * m d <= e
863 static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
865 unsigned total;
866 unsigned div_pos;
867 struct isl_vec *ineq;
869 if (!bset)
870 return NULL;
872 total = isl_basic_set_total_dim(bset);
873 div_pos = 1 + total - bset->n_div + div;
875 ineq = isl_vec_alloc(bset->ctx, 1 + total);
876 if (!ineq)
877 return NULL;
879 isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
880 isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
881 return ineq;
884 /* Given a row in the tableau and a div that was created
885 * using get_row_split_div and that has been constrained to equality, i.e.,
887 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
889 * replace the expression "\sum_i {a_i} y_i" in the row by d,
890 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
891 * The coefficients of the non-parameters in the tableau have been
892 * verified to be integral. We can therefore simply replace coefficient b
893 * by floor(b). For the coefficients of the parameters we have
894 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
895 * floor(b) = b.
897 static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
899 isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
900 tab->mat->row[row][0], 1 + tab->M + tab->n_col);
902 isl_int_set_si(tab->mat->row[row][0], 1);
904 if (tab->var[tab->n_var - tab->n_div + div].is_row) {
905 int drow = tab->var[tab->n_var - tab->n_div + div].index;
907 isl_assert(tab->mat->ctx,
908 isl_int_is_one(tab->mat->row[drow][0]), goto error);
909 isl_seq_combine(tab->mat->row[row] + 1,
910 tab->mat->ctx->one, tab->mat->row[row] + 1,
911 tab->mat->ctx->one, tab->mat->row[drow] + 1,
912 1 + tab->M + tab->n_col);
913 } else {
914 int dcol = tab->var[tab->n_var - tab->n_div + div].index;
916 isl_int_add_ui(tab->mat->row[row][2 + tab->M + dcol],
917 tab->mat->row[row][2 + tab->M + dcol], 1);
920 return tab;
921 error:
922 isl_tab_free(tab);
923 return NULL;
926 /* Check if the (parametric) constant of the given row is obviously
927 * negative, meaning that we don't need to consult the context tableau.
928 * If there is a big parameter and its coefficient is non-zero,
929 * then this coefficient determines the outcome.
930 * Otherwise, we check whether the constant is negative and
931 * all non-zero coefficients of parameters are negative and
932 * belong to non-negative parameters.
934 static int is_obviously_neg(struct isl_tab *tab, int row)
936 int i;
937 int col;
938 unsigned off = 2 + tab->M;
940 if (tab->M) {
941 if (isl_int_is_pos(tab->mat->row[row][2]))
942 return 0;
943 if (isl_int_is_neg(tab->mat->row[row][2]))
944 return 1;
947 if (isl_int_is_nonneg(tab->mat->row[row][1]))
948 return 0;
949 for (i = 0; i < tab->n_param; ++i) {
950 /* Eliminated parameter */
951 if (tab->var[i].is_row)
952 continue;
953 col = tab->var[i].index;
954 if (isl_int_is_zero(tab->mat->row[row][off + col]))
955 continue;
956 if (!tab->var[i].is_nonneg)
957 return 0;
958 if (isl_int_is_pos(tab->mat->row[row][off + col]))
959 return 0;
961 for (i = 0; i < tab->n_div; ++i) {
962 if (tab->var[tab->n_var - tab->n_div + i].is_row)
963 continue;
964 col = tab->var[tab->n_var - tab->n_div + i].index;
965 if (isl_int_is_zero(tab->mat->row[row][off + col]))
966 continue;
967 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
968 return 0;
969 if (isl_int_is_pos(tab->mat->row[row][off + col]))
970 return 0;
972 return 1;
975 /* Check if the (parametric) constant of the given row is obviously
976 * non-negative, meaning that we don't need to consult the context tableau.
977 * If there is a big parameter and its coefficient is non-zero,
978 * then this coefficient determines the outcome.
979 * Otherwise, we check whether the constant is non-negative and
980 * all non-zero coefficients of parameters are positive and
981 * belong to non-negative parameters.
983 static int is_obviously_nonneg(struct isl_tab *tab, int row)
985 int i;
986 int col;
987 unsigned off = 2 + tab->M;
989 if (tab->M) {
990 if (isl_int_is_pos(tab->mat->row[row][2]))
991 return 1;
992 if (isl_int_is_neg(tab->mat->row[row][2]))
993 return 0;
996 if (isl_int_is_neg(tab->mat->row[row][1]))
997 return 0;
998 for (i = 0; i < tab->n_param; ++i) {
999 /* Eliminated parameter */
1000 if (tab->var[i].is_row)
1001 continue;
1002 col = tab->var[i].index;
1003 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1004 continue;
1005 if (!tab->var[i].is_nonneg)
1006 return 0;
1007 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1008 return 0;
1010 for (i = 0; i < tab->n_div; ++i) {
1011 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1012 continue;
1013 col = tab->var[tab->n_var - tab->n_div + i].index;
1014 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1015 continue;
1016 if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
1017 return 0;
1018 if (isl_int_is_neg(tab->mat->row[row][off + col]))
1019 return 0;
1021 return 1;
1024 /* Given a row r and two columns, return the column that would
1025 * lead to the lexicographically smallest increment in the sample
1026 * solution when leaving the basis in favor of the row.
1027 * Pivoting with column c will increment the sample value by a non-negative
1028 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1029 * corresponding to the non-parametric variables.
1030 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1031 * with all other entries in this virtual row equal to zero.
1032 * If variable v appears in a row, then a_{v,c} is the element in column c
1033 * of that row.
1035 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1036 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1037 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1038 * increment. Otherwise, it's c2.
1040 static int lexmin_col_pair(struct isl_tab *tab,
1041 int row, int col1, int col2, isl_int tmp)
1043 int i;
1044 isl_int *tr;
1046 tr = tab->mat->row[row] + 2 + tab->M;
1048 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
1049 int s1, s2;
1050 isl_int *r;
1052 if (!tab->var[i].is_row) {
1053 if (tab->var[i].index == col1)
1054 return col2;
1055 if (tab->var[i].index == col2)
1056 return col1;
1057 continue;
1060 if (tab->var[i].index == row)
1061 continue;
1063 r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
1064 s1 = isl_int_sgn(r[col1]);
1065 s2 = isl_int_sgn(r[col2]);
1066 if (s1 == 0 && s2 == 0)
1067 continue;
1068 if (s1 < s2)
1069 return col1;
1070 if (s2 < s1)
1071 return col2;
1073 isl_int_mul(tmp, r[col2], tr[col1]);
1074 isl_int_submul(tmp, r[col1], tr[col2]);
1075 if (isl_int_is_pos(tmp))
1076 return col1;
1077 if (isl_int_is_neg(tmp))
1078 return col2;
1080 return -1;
1083 /* Given a row in the tableau, find and return the column that would
1084 * result in the lexicographically smallest, but positive, increment
1085 * in the sample point.
1086 * If there is no such column, then return tab->n_col.
1087 * If anything goes wrong, return -1.
1089 static int lexmin_pivot_col(struct isl_tab *tab, int row)
1091 int j;
1092 int col = tab->n_col;
1093 isl_int *tr;
1094 isl_int tmp;
1096 tr = tab->mat->row[row] + 2 + tab->M;
1098 isl_int_init(tmp);
1100 for (j = tab->n_dead; j < tab->n_col; ++j) {
1101 if (tab->col_var[j] >= 0 &&
1102 (tab->col_var[j] < tab->n_param ||
1103 tab->col_var[j] >= tab->n_var - tab->n_div))
1104 continue;
1106 if (!isl_int_is_pos(tr[j]))
1107 continue;
1109 if (col == tab->n_col)
1110 col = j;
1111 else
1112 col = lexmin_col_pair(tab, row, col, j, tmp);
1113 isl_assert(tab->mat->ctx, col >= 0, goto error);
1116 isl_int_clear(tmp);
1117 return col;
1118 error:
1119 isl_int_clear(tmp);
1120 return -1;
1123 /* Return the first known violated constraint, i.e., a non-negative
1124 * constraint that currently has an either obviously negative value
1125 * or a previously determined to be negative value.
1127 * If any constraint has a negative coefficient for the big parameter,
1128 * if any, then we return one of these first.
1130 static int first_neg(struct isl_tab *tab)
1132 int row;
1134 if (tab->M)
1135 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1136 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1137 continue;
1138 if (!isl_int_is_neg(tab->mat->row[row][2]))
1139 continue;
1140 if (tab->row_sign)
1141 tab->row_sign[row] = isl_tab_row_neg;
1142 return row;
1144 for (row = tab->n_redundant; row < tab->n_row; ++row) {
1145 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
1146 continue;
1147 if (tab->row_sign) {
1148 if (tab->row_sign[row] == 0 &&
1149 is_obviously_neg(tab, row))
1150 tab->row_sign[row] = isl_tab_row_neg;
1151 if (tab->row_sign[row] != isl_tab_row_neg)
1152 continue;
1153 } else if (!is_obviously_neg(tab, row))
1154 continue;
1155 return row;
1157 return -1;
1160 /* Check whether the invariant that all columns are lexico-positive
1161 * is satisfied. This function is not called from the current code
1162 * but is useful during debugging.
1164 static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
1165 static void check_lexpos(struct isl_tab *tab)
1167 unsigned off = 2 + tab->M;
1168 int col;
1169 int var;
1170 int row;
1172 for (col = tab->n_dead; col < tab->n_col; ++col) {
1173 if (tab->col_var[col] >= 0 &&
1174 (tab->col_var[col] < tab->n_param ||
1175 tab->col_var[col] >= tab->n_var - tab->n_div))
1176 continue;
1177 for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
1178 if (!tab->var[var].is_row) {
1179 if (tab->var[var].index == col)
1180 break;
1181 else
1182 continue;
1184 row = tab->var[var].index;
1185 if (isl_int_is_zero(tab->mat->row[row][off + col]))
1186 continue;
1187 if (isl_int_is_pos(tab->mat->row[row][off + col]))
1188 break;
1189 fprintf(stderr, "lexneg column %d (row %d)\n",
1190 col, row);
1192 if (var >= tab->n_var - tab->n_div)
1193 fprintf(stderr, "zero column %d\n", col);
1197 /* Report to the caller that the given constraint is part of an encountered
1198 * conflict.
1200 static int report_conflicting_constraint(struct isl_tab *tab, int con)
1202 return tab->conflict(con, tab->conflict_user);
1205 /* Given a conflicting row in the tableau, report all constraints
1206 * involved in the row to the caller. That is, the row itself
1207 * (if it represents a constraint) and all constraint columns with
1208 * non-zero (and therefore negative) coefficients.
1210 static int report_conflict(struct isl_tab *tab, int row)
1212 int j;
1213 isl_int *tr;
1215 if (!tab->conflict)
1216 return 0;
1218 if (tab->row_var[row] < 0 &&
1219 report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
1220 return -1;
1222 tr = tab->mat->row[row] + 2 + tab->M;
1224 for (j = tab->n_dead; j < tab->n_col; ++j) {
1225 if (tab->col_var[j] >= 0 &&
1226 (tab->col_var[j] < tab->n_param ||
1227 tab->col_var[j] >= tab->n_var - tab->n_div))
1228 continue;
1230 if (!isl_int_is_neg(tr[j]))
1231 continue;
1233 if (tab->col_var[j] < 0 &&
1234 report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
1235 return -1;
1238 return 0;
1241 /* Resolve all known or obviously violated constraints through pivoting.
1242 * In particular, as long as we can find any violated constraint, we
1243 * look for a pivoting column that would result in the lexicographically
1244 * smallest increment in the sample point. If there is no such column
1245 * then the tableau is infeasible.
1247 static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
1248 static int restore_lexmin(struct isl_tab *tab)
1250 int row, col;
1252 if (!tab)
1253 return -1;
1254 if (tab->empty)
1255 return 0;
1256 while ((row = first_neg(tab)) != -1) {
1257 col = lexmin_pivot_col(tab, row);
1258 if (col >= tab->n_col) {
1259 if (report_conflict(tab, row) < 0)
1260 return -1;
1261 if (isl_tab_mark_empty(tab) < 0)
1262 return -1;
1263 return 0;
1265 if (col < 0)
1266 return -1;
1267 if (isl_tab_pivot(tab, row, col) < 0)
1268 return -1;
1270 return 0;
1273 /* Given a row that represents an equality, look for an appropriate
1274 * pivoting column.
1275 * In particular, if there are any non-zero coefficients among
1276 * the non-parameter variables, then we take the last of these
1277 * variables. Eliminating this variable in terms of the other
1278 * variables and/or parameters does not influence the property
1279 * that all column in the initial tableau are lexicographically
1280 * positive. The row corresponding to the eliminated variable
1281 * will only have non-zero entries below the diagonal of the
1282 * initial tableau. That is, we transform
1284 * I I
1285 * 1 into a
1286 * I I
1288 * If there is no such non-parameter variable, then we are dealing with
1289 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1290 * for elimination. This will ensure that the eliminated parameter
1291 * always has an integer value whenever all the other parameters are integral.
1292 * If there is no such parameter then we return -1.
1294 static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
1296 unsigned off = 2 + tab->M;
1297 int i;
1299 for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
1300 int col;
1301 if (tab->var[i].is_row)
1302 continue;
1303 col = tab->var[i].index;
1304 if (col <= tab->n_dead)
1305 continue;
1306 if (!isl_int_is_zero(tab->mat->row[row][off + col]))
1307 return col;
1309 for (i = tab->n_dead; i < tab->n_col; ++i) {
1310 if (isl_int_is_one(tab->mat->row[row][off + i]))
1311 return i;
1312 if (isl_int_is_negone(tab->mat->row[row][off + i]))
1313 return i;
1315 return -1;
1318 /* Add an equality that is known to be valid to the tableau.
1319 * We first check if we can eliminate a variable or a parameter.
1320 * If not, we add the equality as two inequalities.
1321 * In this case, the equality was a pure parameter equality and there
1322 * is no need to resolve any constraint violations.
1324 static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
1326 int i;
1327 int r;
1329 if (!tab)
1330 return NULL;
1331 r = isl_tab_add_row(tab, eq);
1332 if (r < 0)
1333 goto error;
1335 r = tab->con[r].index;
1336 i = last_var_col_or_int_par_col(tab, r);
1337 if (i < 0) {
1338 tab->con[r].is_nonneg = 1;
1339 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1340 goto error;
1341 isl_seq_neg(eq, eq, 1 + tab->n_var);
1342 r = isl_tab_add_row(tab, eq);
1343 if (r < 0)
1344 goto error;
1345 tab->con[r].is_nonneg = 1;
1346 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1347 goto error;
1348 } else {
1349 if (isl_tab_pivot(tab, r, i) < 0)
1350 goto error;
1351 if (isl_tab_kill_col(tab, i) < 0)
1352 goto error;
1353 tab->n_eq++;
1356 return tab;
1357 error:
1358 isl_tab_free(tab);
1359 return NULL;
1362 /* Check if the given row is a pure constant.
1364 static int is_constant(struct isl_tab *tab, int row)
1366 unsigned off = 2 + tab->M;
1368 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1369 tab->n_col - tab->n_dead) == -1;
1372 /* Add an equality that may or may not be valid to the tableau.
1373 * If the resulting row is a pure constant, then it must be zero.
1374 * Otherwise, the resulting tableau is empty.
1376 * If the row is not a pure constant, then we add two inequalities,
1377 * each time checking that they can be satisfied.
1378 * In the end we try to use one of the two constraints to eliminate
1379 * a column.
1381 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
1382 static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
1384 int r1, r2;
1385 int row;
1386 struct isl_tab_undo *snap;
1388 if (!tab)
1389 return -1;
1390 snap = isl_tab_snap(tab);
1391 r1 = isl_tab_add_row(tab, eq);
1392 if (r1 < 0)
1393 return -1;
1394 tab->con[r1].is_nonneg = 1;
1395 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
1396 return -1;
1398 row = tab->con[r1].index;
1399 if (is_constant(tab, row)) {
1400 if (!isl_int_is_zero(tab->mat->row[row][1]) ||
1401 (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
1402 if (isl_tab_mark_empty(tab) < 0)
1403 return -1;
1404 return 0;
1406 if (isl_tab_rollback(tab, snap) < 0)
1407 return -1;
1408 return 0;
1411 if (restore_lexmin(tab) < 0)
1412 return -1;
1413 if (tab->empty)
1414 return 0;
1416 isl_seq_neg(eq, eq, 1 + tab->n_var);
1418 r2 = isl_tab_add_row(tab, eq);
1419 if (r2 < 0)
1420 return -1;
1421 tab->con[r2].is_nonneg = 1;
1422 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
1423 return -1;
1425 if (restore_lexmin(tab) < 0)
1426 return -1;
1427 if (tab->empty)
1428 return 0;
1430 if (!tab->con[r1].is_row) {
1431 if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
1432 return -1;
1433 } else if (!tab->con[r2].is_row) {
1434 if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
1435 return -1;
1438 if (tab->bmap) {
1439 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1440 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1441 return -1;
1442 isl_seq_neg(eq, eq, 1 + tab->n_var);
1443 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1444 isl_seq_neg(eq, eq, 1 + tab->n_var);
1445 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1446 return -1;
1447 if (!tab->bmap)
1448 return -1;
1451 return 0;
1454 /* Add an inequality to the tableau, resolving violations using
1455 * restore_lexmin.
1457 static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
1459 int r;
1461 if (!tab)
1462 return NULL;
1463 if (tab->bmap) {
1464 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1465 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1466 goto error;
1467 if (!tab->bmap)
1468 goto error;
1470 r = isl_tab_add_row(tab, ineq);
1471 if (r < 0)
1472 goto error;
1473 tab->con[r].is_nonneg = 1;
1474 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1475 goto error;
1476 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1477 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1478 goto error;
1479 return tab;
1482 if (restore_lexmin(tab) < 0)
1483 goto error;
1484 if (!tab->empty && tab->con[r].is_row &&
1485 isl_tab_row_is_redundant(tab, tab->con[r].index))
1486 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1487 goto error;
1488 return tab;
1489 error:
1490 isl_tab_free(tab);
1491 return NULL;
1494 /* Check if the coefficients of the parameters are all integral.
1496 static int integer_parameter(struct isl_tab *tab, int row)
1498 int i;
1499 int col;
1500 unsigned off = 2 + tab->M;
1502 for (i = 0; i < tab->n_param; ++i) {
1503 /* Eliminated parameter */
1504 if (tab->var[i].is_row)
1505 continue;
1506 col = tab->var[i].index;
1507 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1508 tab->mat->row[row][0]))
1509 return 0;
1511 for (i = 0; i < tab->n_div; ++i) {
1512 if (tab->var[tab->n_var - tab->n_div + i].is_row)
1513 continue;
1514 col = tab->var[tab->n_var - tab->n_div + i].index;
1515 if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
1516 tab->mat->row[row][0]))
1517 return 0;
1519 return 1;
1522 /* Check if the coefficients of the non-parameter variables are all integral.
1524 static int integer_variable(struct isl_tab *tab, int row)
1526 int i;
1527 unsigned off = 2 + tab->M;
1529 for (i = tab->n_dead; i < tab->n_col; ++i) {
1530 if (tab->col_var[i] >= 0 &&
1531 (tab->col_var[i] < tab->n_param ||
1532 tab->col_var[i] >= tab->n_var - tab->n_div))
1533 continue;
1534 if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
1535 tab->mat->row[row][0]))
1536 return 0;
1538 return 1;
1541 /* Check if the constant term is integral.
1543 static int integer_constant(struct isl_tab *tab, int row)
1545 return isl_int_is_divisible_by(tab->mat->row[row][1],
1546 tab->mat->row[row][0]);
1549 #define I_CST 1 << 0
1550 #define I_PAR 1 << 1
1551 #define I_VAR 1 << 2
1553 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1554 * that is non-integer and therefore requires a cut and return
1555 * the index of the variable.
1556 * For parametric tableaus, there are three parts in a row,
1557 * the constant, the coefficients of the parameters and the rest.
1558 * For each part, we check whether the coefficients in that part
1559 * are all integral and if so, set the corresponding flag in *f.
1560 * If the constant and the parameter part are integral, then the
1561 * current sample value is integral and no cut is required
1562 * (irrespective of whether the variable part is integral).
1564 static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
1566 var = var < 0 ? tab->n_param : var + 1;
1568 for (; var < tab->n_var - tab->n_div; ++var) {
1569 int flags = 0;
1570 int row;
1571 if (!tab->var[var].is_row)
1572 continue;
1573 row = tab->var[var].index;
1574 if (integer_constant(tab, row))
1575 ISL_FL_SET(flags, I_CST);
1576 if (integer_parameter(tab, row))
1577 ISL_FL_SET(flags, I_PAR);
1578 if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
1579 continue;
1580 if (integer_variable(tab, row))
1581 ISL_FL_SET(flags, I_VAR);
1582 *f = flags;
1583 return var;
1585 return -1;
1588 /* Check for first (non-parameter) variable that is non-integer and
1589 * therefore requires a cut and return the corresponding row.
1590 * For parametric tableaus, there are three parts in a row,
1591 * the constant, the coefficients of the parameters and the rest.
1592 * For each part, we check whether the coefficients in that part
1593 * are all integral and if so, set the corresponding flag in *f.
1594 * If the constant and the parameter part are integral, then the
1595 * current sample value is integral and no cut is required
1596 * (irrespective of whether the variable part is integral).
1598 static int first_non_integer_row(struct isl_tab *tab, int *f)
1600 int var = next_non_integer_var(tab, -1, f);
1602 return var < 0 ? -1 : tab->var[var].index;
1605 /* Add a (non-parametric) cut to cut away the non-integral sample
1606 * value of the given row.
1608 * If the row is given by
1610 * m r = f + \sum_i a_i y_i
1612 * then the cut is
1614 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1616 * The big parameter, if any, is ignored, since it is assumed to be big
1617 * enough to be divisible by any integer.
1618 * If the tableau is actually a parametric tableau, then this function
1619 * is only called when all coefficients of the parameters are integral.
1620 * The cut therefore has zero coefficients for the parameters.
1622 * The current value is known to be negative, so row_sign, if it
1623 * exists, is set accordingly.
1625 * Return the row of the cut or -1.
1627 static int add_cut(struct isl_tab *tab, int row)
1629 int i;
1630 int r;
1631 isl_int *r_row;
1632 unsigned off = 2 + tab->M;
1634 if (isl_tab_extend_cons(tab, 1) < 0)
1635 return -1;
1636 r = isl_tab_allocate_con(tab);
1637 if (r < 0)
1638 return -1;
1640 r_row = tab->mat->row[tab->con[r].index];
1641 isl_int_set(r_row[0], tab->mat->row[row][0]);
1642 isl_int_neg(r_row[1], tab->mat->row[row][1]);
1643 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
1644 isl_int_neg(r_row[1], r_row[1]);
1645 if (tab->M)
1646 isl_int_set_si(r_row[2], 0);
1647 for (i = 0; i < tab->n_col; ++i)
1648 isl_int_fdiv_r(r_row[off + i],
1649 tab->mat->row[row][off + i], tab->mat->row[row][0]);
1651 tab->con[r].is_nonneg = 1;
1652 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1653 return -1;
1654 if (tab->row_sign)
1655 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
1657 return tab->con[r].index;
1660 #define CUT_ALL 1
1661 #define CUT_ONE 0
1663 /* Given a non-parametric tableau, add cuts until an integer
1664 * sample point is obtained or until the tableau is determined
1665 * to be integer infeasible.
1666 * As long as there is any non-integer value in the sample point,
1667 * we add appropriate cuts, if possible, for each of these
1668 * non-integer values and then resolve the violated
1669 * cut constraints using restore_lexmin.
1670 * If one of the corresponding rows is equal to an integral
1671 * combination of variables/constraints plus a non-integral constant,
1672 * then there is no way to obtain an integer point and we return
1673 * a tableau that is marked empty.
1674 * The parameter cutting_strategy controls the strategy used when adding cuts
1675 * to remove non-integer points. CUT_ALL adds all possible cuts
1676 * before continuing the search. CUT_ONE adds only one cut at a time.
1678 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab,
1679 int cutting_strategy)
1681 int var;
1682 int row;
1683 int flags;
1685 if (!tab)
1686 return NULL;
1687 if (tab->empty)
1688 return tab;
1690 while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
1691 do {
1692 if (ISL_FL_ISSET(flags, I_VAR)) {
1693 if (isl_tab_mark_empty(tab) < 0)
1694 goto error;
1695 return tab;
1697 row = tab->var[var].index;
1698 row = add_cut(tab, row);
1699 if (row < 0)
1700 goto error;
1701 if (cutting_strategy == CUT_ONE)
1702 break;
1703 } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
1704 if (restore_lexmin(tab) < 0)
1705 goto error;
1706 if (tab->empty)
1707 break;
1709 return tab;
1710 error:
1711 isl_tab_free(tab);
1712 return NULL;
1715 /* Check whether all the currently active samples also satisfy the inequality
1716 * "ineq" (treated as an equality if eq is set).
1717 * Remove those samples that do not.
1719 static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
1721 int i;
1722 isl_int v;
1724 if (!tab)
1725 return NULL;
1727 isl_assert(tab->mat->ctx, tab->bmap, goto error);
1728 isl_assert(tab->mat->ctx, tab->samples, goto error);
1729 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
1731 isl_int_init(v);
1732 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1733 int sgn;
1734 isl_seq_inner_product(ineq, tab->samples->row[i],
1735 1 + tab->n_var, &v);
1736 sgn = isl_int_sgn(v);
1737 if (eq ? (sgn == 0) : (sgn >= 0))
1738 continue;
1739 tab = isl_tab_drop_sample(tab, i);
1740 if (!tab)
1741 break;
1743 isl_int_clear(v);
1745 return tab;
1746 error:
1747 isl_tab_free(tab);
1748 return NULL;
1751 /* Check whether the sample value of the tableau is finite,
1752 * i.e., either the tableau does not use a big parameter, or
1753 * all values of the variables are equal to the big parameter plus
1754 * some constant. This constant is the actual sample value.
1756 static int sample_is_finite(struct isl_tab *tab)
1758 int i;
1760 if (!tab->M)
1761 return 1;
1763 for (i = 0; i < tab->n_var; ++i) {
1764 int row;
1765 if (!tab->var[i].is_row)
1766 return 0;
1767 row = tab->var[i].index;
1768 if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
1769 return 0;
1771 return 1;
1774 /* Check if the context tableau of sol has any integer points.
1775 * Leave tab in empty state if no integer point can be found.
1776 * If an integer point can be found and if moreover it is finite,
1777 * then it is added to the list of sample values.
1779 * This function is only called when none of the currently active sample
1780 * values satisfies the most recently added constraint.
1782 static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
1784 struct isl_tab_undo *snap;
1786 if (!tab)
1787 return NULL;
1789 snap = isl_tab_snap(tab);
1790 if (isl_tab_push_basis(tab) < 0)
1791 goto error;
1793 tab = cut_to_integer_lexmin(tab, CUT_ALL);
1794 if (!tab)
1795 goto error;
1797 if (!tab->empty && sample_is_finite(tab)) {
1798 struct isl_vec *sample;
1800 sample = isl_tab_get_sample_value(tab);
1802 if (isl_tab_add_sample(tab, sample) < 0)
1803 goto error;
1806 if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
1807 goto error;
1809 return tab;
1810 error:
1811 isl_tab_free(tab);
1812 return NULL;
1815 /* Check if any of the currently active sample values satisfies
1816 * the inequality "ineq" (an equality if eq is set).
1818 static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
1820 int i;
1821 isl_int v;
1823 if (!tab)
1824 return -1;
1826 isl_assert(tab->mat->ctx, tab->bmap, return -1);
1827 isl_assert(tab->mat->ctx, tab->samples, return -1);
1828 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
1830 isl_int_init(v);
1831 for (i = tab->n_outside; i < tab->n_sample; ++i) {
1832 int sgn;
1833 isl_seq_inner_product(ineq, tab->samples->row[i],
1834 1 + tab->n_var, &v);
1835 sgn = isl_int_sgn(v);
1836 if (eq ? (sgn == 0) : (sgn >= 0))
1837 break;
1839 isl_int_clear(v);
1841 return i < tab->n_sample;
1844 /* Add a div specified by "div" to the tableau "tab" and return
1845 * 1 if the div is obviously non-negative.
1847 static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
1848 int (*add_ineq)(void *user, isl_int *), void *user)
1850 int i;
1851 int r;
1852 struct isl_mat *samples;
1853 int nonneg;
1855 r = isl_tab_add_div(tab, div, add_ineq, user);
1856 if (r < 0)
1857 return -1;
1858 nonneg = tab->var[r].is_nonneg;
1859 tab->var[r].frozen = 1;
1861 samples = isl_mat_extend(tab->samples,
1862 tab->n_sample, 1 + tab->n_var);
1863 tab->samples = samples;
1864 if (!samples)
1865 return -1;
1866 for (i = tab->n_outside; i < samples->n_row; ++i) {
1867 isl_seq_inner_product(div->el + 1, samples->row[i],
1868 div->size - 1, &samples->row[i][samples->n_col - 1]);
1869 isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
1870 samples->row[i][samples->n_col - 1], div->el[0]);
1873 return nonneg;
1876 /* Add a div specified by "div" to both the main tableau and
1877 * the context tableau. In case of the main tableau, we only
1878 * need to add an extra div. In the context tableau, we also
1879 * need to express the meaning of the div.
1880 * Return the index of the div or -1 if anything went wrong.
1882 static int add_div(struct isl_tab *tab, struct isl_context *context,
1883 struct isl_vec *div)
1885 int r;
1886 int nonneg;
1888 if ((nonneg = context->op->add_div(context, div)) < 0)
1889 goto error;
1891 if (!context->op->is_ok(context))
1892 goto error;
1894 if (isl_tab_extend_vars(tab, 1) < 0)
1895 goto error;
1896 r = isl_tab_allocate_var(tab);
1897 if (r < 0)
1898 goto error;
1899 if (nonneg)
1900 tab->var[r].is_nonneg = 1;
1901 tab->var[r].frozen = 1;
1902 tab->n_div++;
1904 return tab->n_div - 1;
1905 error:
1906 context->op->invalidate(context);
1907 return -1;
1910 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
1912 int i;
1913 unsigned total = isl_basic_map_total_dim(tab->bmap);
1915 for (i = 0; i < tab->bmap->n_div; ++i) {
1916 if (isl_int_ne(tab->bmap->div[i][0], denom))
1917 continue;
1918 if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
1919 continue;
1920 return i;
1922 return -1;
1925 /* Return the index of a div that corresponds to "div".
1926 * We first check if we already have such a div and if not, we create one.
1928 static int get_div(struct isl_tab *tab, struct isl_context *context,
1929 struct isl_vec *div)
1931 int d;
1932 struct isl_tab *context_tab = context->op->peek_tab(context);
1934 if (!context_tab)
1935 return -1;
1937 d = find_div(context_tab, div->el + 1, div->el[0]);
1938 if (d != -1)
1939 return d;
1941 return add_div(tab, context, div);
1944 /* Add a parametric cut to cut away the non-integral sample value
1945 * of the give row.
1946 * Let a_i be the coefficients of the constant term and the parameters
1947 * and let b_i be the coefficients of the variables or constraints
1948 * in basis of the tableau.
1949 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1951 * The cut is expressed as
1953 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1955 * If q did not already exist in the context tableau, then it is added first.
1956 * If q is in a column of the main tableau then the "+ q" can be accomplished
1957 * by setting the corresponding entry to the denominator of the constraint.
1958 * If q happens to be in a row of the main tableau, then the corresponding
1959 * row needs to be added instead (taking care of the denominators).
1960 * Note that this is very unlikely, but perhaps not entirely impossible.
1962 * The current value of the cut is known to be negative (or at least
1963 * non-positive), so row_sign is set accordingly.
1965 * Return the row of the cut or -1.
1967 static int add_parametric_cut(struct isl_tab *tab, int row,
1968 struct isl_context *context)
1970 struct isl_vec *div;
1971 int d;
1972 int i;
1973 int r;
1974 isl_int *r_row;
1975 int col;
1976 int n;
1977 unsigned off = 2 + tab->M;
1979 if (!context)
1980 return -1;
1982 div = get_row_parameter_div(tab, row);
1983 if (!div)
1984 return -1;
1986 n = tab->n_div;
1987 d = context->op->get_div(context, tab, div);
1988 isl_vec_free(div);
1989 if (d < 0)
1990 return -1;
1992 if (isl_tab_extend_cons(tab, 1) < 0)
1993 return -1;
1994 r = isl_tab_allocate_con(tab);
1995 if (r < 0)
1996 return -1;
1998 r_row = tab->mat->row[tab->con[r].index];
1999 isl_int_set(r_row[0], tab->mat->row[row][0]);
2000 isl_int_neg(r_row[1], tab->mat->row[row][1]);
2001 isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
2002 isl_int_neg(r_row[1], r_row[1]);
2003 if (tab->M)
2004 isl_int_set_si(r_row[2], 0);
2005 for (i = 0; i < tab->n_param; ++i) {
2006 if (tab->var[i].is_row)
2007 continue;
2008 col = tab->var[i].index;
2009 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2010 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2011 tab->mat->row[row][0]);
2012 isl_int_neg(r_row[off + col], r_row[off + col]);
2014 for (i = 0; i < tab->n_div; ++i) {
2015 if (tab->var[tab->n_var - tab->n_div + i].is_row)
2016 continue;
2017 col = tab->var[tab->n_var - tab->n_div + i].index;
2018 isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
2019 isl_int_fdiv_r(r_row[off + col], r_row[off + col],
2020 tab->mat->row[row][0]);
2021 isl_int_neg(r_row[off + col], r_row[off + col]);
2023 for (i = 0; i < tab->n_col; ++i) {
2024 if (tab->col_var[i] >= 0 &&
2025 (tab->col_var[i] < tab->n_param ||
2026 tab->col_var[i] >= tab->n_var - tab->n_div))
2027 continue;
2028 isl_int_fdiv_r(r_row[off + i],
2029 tab->mat->row[row][off + i], tab->mat->row[row][0]);
2031 if (tab->var[tab->n_var - tab->n_div + d].is_row) {
2032 isl_int gcd;
2033 int d_row = tab->var[tab->n_var - tab->n_div + d].index;
2034 isl_int_init(gcd);
2035 isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
2036 isl_int_divexact(r_row[0], r_row[0], gcd);
2037 isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
2038 isl_seq_combine(r_row + 1, gcd, r_row + 1,
2039 r_row[0], tab->mat->row[d_row] + 1,
2040 off - 1 + tab->n_col);
2041 isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
2042 isl_int_clear(gcd);
2043 } else {
2044 col = tab->var[tab->n_var - tab->n_div + d].index;
2045 isl_int_set(r_row[off + col], tab->mat->row[row][0]);
2048 tab->con[r].is_nonneg = 1;
2049 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2050 return -1;
2051 if (tab->row_sign)
2052 tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
2054 row = tab->con[r].index;
2056 if (d >= n && context->op->detect_equalities(context, tab) < 0)
2057 return -1;
2059 return row;
2062 /* Construct a tableau for bmap that can be used for computing
2063 * the lexicographic minimum (or maximum) of bmap.
2064 * If not NULL, then dom is the domain where the minimum
2065 * should be computed. In this case, we set up a parametric
2066 * tableau with row signs (initialized to "unknown").
2067 * If M is set, then the tableau will use a big parameter.
2068 * If max is set, then a maximum should be computed instead of a minimum.
2069 * This means that for each variable x, the tableau will contain the variable
2070 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2071 * of the variables in all constraints are negated prior to adding them
2072 * to the tableau.
2074 static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
2075 struct isl_basic_set *dom, unsigned M, int max)
2077 int i;
2078 struct isl_tab *tab;
2079 unsigned n_var;
2080 unsigned o_var;
2082 tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
2083 isl_basic_map_total_dim(bmap), M);
2084 if (!tab)
2085 return NULL;
2087 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2088 if (dom) {
2089 tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
2090 tab->n_div = dom->n_div;
2091 tab->row_sign = isl_calloc_array(bmap->ctx,
2092 enum isl_tab_row_sign, tab->mat->n_row);
2093 if (tab->mat->n_row && !tab->row_sign)
2094 goto error;
2096 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2097 if (isl_tab_mark_empty(tab) < 0)
2098 goto error;
2099 return tab;
2102 for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
2103 tab->var[i].is_nonneg = 1;
2104 tab->var[i].frozen = 1;
2106 o_var = 1 + tab->n_param;
2107 n_var = tab->n_var - tab->n_param - tab->n_div;
2108 for (i = 0; i < bmap->n_eq; ++i) {
2109 if (max)
2110 isl_seq_neg(bmap->eq[i] + o_var,
2111 bmap->eq[i] + o_var, n_var);
2112 tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
2113 if (max)
2114 isl_seq_neg(bmap->eq[i] + o_var,
2115 bmap->eq[i] + o_var, n_var);
2116 if (!tab || tab->empty)
2117 return tab;
2119 if (bmap->n_eq && restore_lexmin(tab) < 0)
2120 goto error;
2121 for (i = 0; i < bmap->n_ineq; ++i) {
2122 if (max)
2123 isl_seq_neg(bmap->ineq[i] + o_var,
2124 bmap->ineq[i] + o_var, n_var);
2125 tab = add_lexmin_ineq(tab, bmap->ineq[i]);
2126 if (max)
2127 isl_seq_neg(bmap->ineq[i] + o_var,
2128 bmap->ineq[i] + o_var, n_var);
2129 if (!tab || tab->empty)
2130 return tab;
2132 return tab;
2133 error:
2134 isl_tab_free(tab);
2135 return NULL;
2138 /* Given a main tableau where more than one row requires a split,
2139 * determine and return the "best" row to split on.
2141 * Given two rows in the main tableau, if the inequality corresponding
2142 * to the first row is redundant with respect to that of the second row
2143 * in the current tableau, then it is better to split on the second row,
2144 * since in the positive part, both row will be positive.
2145 * (In the negative part a pivot will have to be performed and just about
2146 * anything can happen to the sign of the other row.)
2148 * As a simple heuristic, we therefore select the row that makes the most
2149 * of the other rows redundant.
2151 * Perhaps it would also be useful to look at the number of constraints
2152 * that conflict with any given constraint.
2154 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2156 struct isl_tab_undo *snap;
2157 int split;
2158 int row;
2159 int best = -1;
2160 int best_r;
2162 if (isl_tab_extend_cons(context_tab, 2) < 0)
2163 return -1;
2165 snap = isl_tab_snap(context_tab);
2167 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2168 struct isl_tab_undo *snap2;
2169 struct isl_vec *ineq = NULL;
2170 int r = 0;
2171 int ok;
2173 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2174 continue;
2175 if (tab->row_sign[split] != isl_tab_row_any)
2176 continue;
2178 ineq = get_row_parameter_ineq(tab, split);
2179 if (!ineq)
2180 return -1;
2181 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2182 isl_vec_free(ineq);
2183 if (!ok)
2184 return -1;
2186 snap2 = isl_tab_snap(context_tab);
2188 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2189 struct isl_tab_var *var;
2191 if (row == split)
2192 continue;
2193 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2194 continue;
2195 if (tab->row_sign[row] != isl_tab_row_any)
2196 continue;
2198 ineq = get_row_parameter_ineq(tab, row);
2199 if (!ineq)
2200 return -1;
2201 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2202 isl_vec_free(ineq);
2203 if (!ok)
2204 return -1;
2205 var = &context_tab->con[context_tab->n_con - 1];
2206 if (!context_tab->empty &&
2207 !isl_tab_min_at_most_neg_one(context_tab, var))
2208 r++;
2209 if (isl_tab_rollback(context_tab, snap2) < 0)
2210 return -1;
2212 if (best == -1 || r > best_r) {
2213 best = split;
2214 best_r = r;
2216 if (isl_tab_rollback(context_tab, snap) < 0)
2217 return -1;
2220 return best;
2223 static struct isl_basic_set *context_lex_peek_basic_set(
2224 struct isl_context *context)
2226 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2227 if (!clex->tab)
2228 return NULL;
2229 return isl_tab_peek_bset(clex->tab);
2232 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2234 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2235 return clex->tab;
2238 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2239 int check, int update)
2241 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2242 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2243 goto error;
2244 if (add_lexmin_eq(clex->tab, eq) < 0)
2245 goto error;
2246 if (check) {
2247 int v = tab_has_valid_sample(clex->tab, eq, 1);
2248 if (v < 0)
2249 goto error;
2250 if (!v)
2251 clex->tab = check_integer_feasible(clex->tab);
2253 if (update)
2254 clex->tab = check_samples(clex->tab, eq, 1);
2255 return;
2256 error:
2257 isl_tab_free(clex->tab);
2258 clex->tab = NULL;
2261 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2262 int check, int update)
2264 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2265 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2266 goto error;
2267 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2268 if (check) {
2269 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2270 if (v < 0)
2271 goto error;
2272 if (!v)
2273 clex->tab = check_integer_feasible(clex->tab);
2275 if (update)
2276 clex->tab = check_samples(clex->tab, ineq, 0);
2277 return;
2278 error:
2279 isl_tab_free(clex->tab);
2280 clex->tab = NULL;
2283 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2285 struct isl_context *context = (struct isl_context *)user;
2286 context_lex_add_ineq(context, ineq, 0, 0);
2287 return context->op->is_ok(context) ? 0 : -1;
2290 /* Check which signs can be obtained by "ineq" on all the currently
2291 * active sample values. See row_sign for more information.
2293 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2294 int strict)
2296 int i;
2297 int sgn;
2298 isl_int tmp;
2299 enum isl_tab_row_sign res = isl_tab_row_unknown;
2301 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2302 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2303 return isl_tab_row_unknown);
2305 isl_int_init(tmp);
2306 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2307 isl_seq_inner_product(tab->samples->row[i], ineq,
2308 1 + tab->n_var, &tmp);
2309 sgn = isl_int_sgn(tmp);
2310 if (sgn > 0 || (sgn == 0 && strict)) {
2311 if (res == isl_tab_row_unknown)
2312 res = isl_tab_row_pos;
2313 if (res == isl_tab_row_neg)
2314 res = isl_tab_row_any;
2316 if (sgn < 0) {
2317 if (res == isl_tab_row_unknown)
2318 res = isl_tab_row_neg;
2319 if (res == isl_tab_row_pos)
2320 res = isl_tab_row_any;
2322 if (res == isl_tab_row_any)
2323 break;
2325 isl_int_clear(tmp);
2327 return res;
2330 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2331 isl_int *ineq, int strict)
2333 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2334 return tab_ineq_sign(clex->tab, ineq, strict);
2337 /* Check whether "ineq" can be added to the tableau without rendering
2338 * it infeasible.
2340 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2342 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2343 struct isl_tab_undo *snap;
2344 int feasible;
2346 if (!clex->tab)
2347 return -1;
2349 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2350 return -1;
2352 snap = isl_tab_snap(clex->tab);
2353 if (isl_tab_push_basis(clex->tab) < 0)
2354 return -1;
2355 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2356 clex->tab = check_integer_feasible(clex->tab);
2357 if (!clex->tab)
2358 return -1;
2359 feasible = !clex->tab->empty;
2360 if (isl_tab_rollback(clex->tab, snap) < 0)
2361 return -1;
2363 return feasible;
2366 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2367 struct isl_vec *div)
2369 return get_div(tab, context, div);
2372 /* Add a div specified by "div" to the context tableau and return
2373 * 1 if the div is obviously non-negative.
2374 * context_tab_add_div will always return 1, because all variables
2375 * in a isl_context_lex tableau are non-negative.
2376 * However, if we are using a big parameter in the context, then this only
2377 * reflects the non-negativity of the variable used to _encode_ the
2378 * div, i.e., div' = M + div, so we can't draw any conclusions.
2380 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2382 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2383 int nonneg;
2384 nonneg = context_tab_add_div(clex->tab, div,
2385 context_lex_add_ineq_wrap, context);
2386 if (nonneg < 0)
2387 return -1;
2388 if (clex->tab->M)
2389 return 0;
2390 return nonneg;
2393 static int context_lex_detect_equalities(struct isl_context *context,
2394 struct isl_tab *tab)
2396 return 0;
2399 static int context_lex_best_split(struct isl_context *context,
2400 struct isl_tab *tab)
2402 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2403 struct isl_tab_undo *snap;
2404 int r;
2406 snap = isl_tab_snap(clex->tab);
2407 if (isl_tab_push_basis(clex->tab) < 0)
2408 return -1;
2409 r = best_split(tab, clex->tab);
2411 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2412 return -1;
2414 return r;
2417 static int context_lex_is_empty(struct isl_context *context)
2419 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2420 if (!clex->tab)
2421 return -1;
2422 return clex->tab->empty;
2425 static void *context_lex_save(struct isl_context *context)
2427 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2428 struct isl_tab_undo *snap;
2430 snap = isl_tab_snap(clex->tab);
2431 if (isl_tab_push_basis(clex->tab) < 0)
2432 return NULL;
2433 if (isl_tab_save_samples(clex->tab) < 0)
2434 return NULL;
2436 return snap;
2439 static void context_lex_restore(struct isl_context *context, void *save)
2441 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2442 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2443 isl_tab_free(clex->tab);
2444 clex->tab = NULL;
2448 static void context_lex_discard(void *save)
2452 static int context_lex_is_ok(struct isl_context *context)
2454 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2455 return !!clex->tab;
2458 /* For each variable in the context tableau, check if the variable can
2459 * only attain non-negative values. If so, mark the parameter as non-negative
2460 * in the main tableau. This allows for a more direct identification of some
2461 * cases of violated constraints.
2463 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2464 struct isl_tab *context_tab)
2466 int i;
2467 struct isl_tab_undo *snap;
2468 struct isl_vec *ineq = NULL;
2469 struct isl_tab_var *var;
2470 int n;
2472 if (context_tab->n_var == 0)
2473 return tab;
2475 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2476 if (!ineq)
2477 goto error;
2479 if (isl_tab_extend_cons(context_tab, 1) < 0)
2480 goto error;
2482 snap = isl_tab_snap(context_tab);
2484 n = 0;
2485 isl_seq_clr(ineq->el, ineq->size);
2486 for (i = 0; i < context_tab->n_var; ++i) {
2487 isl_int_set_si(ineq->el[1 + i], 1);
2488 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2489 goto error;
2490 var = &context_tab->con[context_tab->n_con - 1];
2491 if (!context_tab->empty &&
2492 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2493 int j = i;
2494 if (i >= tab->n_param)
2495 j = i - tab->n_param + tab->n_var - tab->n_div;
2496 tab->var[j].is_nonneg = 1;
2497 n++;
2499 isl_int_set_si(ineq->el[1 + i], 0);
2500 if (isl_tab_rollback(context_tab, snap) < 0)
2501 goto error;
2504 if (context_tab->M && n == context_tab->n_var) {
2505 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2506 context_tab->M = 0;
2509 isl_vec_free(ineq);
2510 return tab;
2511 error:
2512 isl_vec_free(ineq);
2513 isl_tab_free(tab);
2514 return NULL;
2517 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2518 struct isl_context *context, struct isl_tab *tab)
2520 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2521 struct isl_tab_undo *snap;
2523 if (!tab)
2524 return NULL;
2526 snap = isl_tab_snap(clex->tab);
2527 if (isl_tab_push_basis(clex->tab) < 0)
2528 goto error;
2530 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2532 if (isl_tab_rollback(clex->tab, snap) < 0)
2533 goto error;
2535 return tab;
2536 error:
2537 isl_tab_free(tab);
2538 return NULL;
2541 static void context_lex_invalidate(struct isl_context *context)
2543 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2544 isl_tab_free(clex->tab);
2545 clex->tab = NULL;
2548 static void context_lex_free(struct isl_context *context)
2550 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2551 isl_tab_free(clex->tab);
2552 free(clex);
2555 struct isl_context_op isl_context_lex_op = {
2556 context_lex_detect_nonnegative_parameters,
2557 context_lex_peek_basic_set,
2558 context_lex_peek_tab,
2559 context_lex_add_eq,
2560 context_lex_add_ineq,
2561 context_lex_ineq_sign,
2562 context_lex_test_ineq,
2563 context_lex_get_div,
2564 context_lex_add_div,
2565 context_lex_detect_equalities,
2566 context_lex_best_split,
2567 context_lex_is_empty,
2568 context_lex_is_ok,
2569 context_lex_save,
2570 context_lex_restore,
2571 context_lex_discard,
2572 context_lex_invalidate,
2573 context_lex_free,
2576 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2578 struct isl_tab *tab;
2580 if (!bset)
2581 return NULL;
2582 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2583 if (!tab)
2584 goto error;
2585 if (isl_tab_track_bset(tab, bset) < 0)
2586 goto error;
2587 tab = isl_tab_init_samples(tab);
2588 return tab;
2589 error:
2590 isl_basic_set_free(bset);
2591 return NULL;
2594 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2596 struct isl_context_lex *clex;
2598 if (!dom)
2599 return NULL;
2601 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2602 if (!clex)
2603 return NULL;
2605 clex->context.op = &isl_context_lex_op;
2607 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2608 if (restore_lexmin(clex->tab) < 0)
2609 goto error;
2610 clex->tab = check_integer_feasible(clex->tab);
2611 if (!clex->tab)
2612 goto error;
2614 return &clex->context;
2615 error:
2616 clex->context.op->free(&clex->context);
2617 return NULL;
2620 /* Representation of the context when using generalized basis reduction.
2622 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2623 * context. Any rational point in "shifted" can therefore be rounded
2624 * up to an integer point in the context.
2625 * If the context is constrained by any equality, then "shifted" is not used
2626 * as it would be empty.
2628 struct isl_context_gbr {
2629 struct isl_context context;
2630 struct isl_tab *tab;
2631 struct isl_tab *shifted;
2632 struct isl_tab *cone;
2635 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2636 struct isl_context *context, struct isl_tab *tab)
2638 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2639 if (!tab)
2640 return NULL;
2641 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2644 static struct isl_basic_set *context_gbr_peek_basic_set(
2645 struct isl_context *context)
2647 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2648 if (!cgbr->tab)
2649 return NULL;
2650 return isl_tab_peek_bset(cgbr->tab);
2653 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2655 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2656 return cgbr->tab;
2659 /* Initialize the "shifted" tableau of the context, which
2660 * contains the constraints of the original tableau shifted
2661 * by the sum of all negative coefficients. This ensures
2662 * that any rational point in the shifted tableau can
2663 * be rounded up to yield an integer point in the original tableau.
2665 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2667 int i, j;
2668 struct isl_vec *cst;
2669 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2670 unsigned dim = isl_basic_set_total_dim(bset);
2672 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2673 if (!cst)
2674 return;
2676 for (i = 0; i < bset->n_ineq; ++i) {
2677 isl_int_set(cst->el[i], bset->ineq[i][0]);
2678 for (j = 0; j < dim; ++j) {
2679 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2680 continue;
2681 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2682 bset->ineq[i][1 + j]);
2686 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2688 for (i = 0; i < bset->n_ineq; ++i)
2689 isl_int_set(bset->ineq[i][0], cst->el[i]);
2691 isl_vec_free(cst);
2694 /* Check if the shifted tableau is non-empty, and if so
2695 * use the sample point to construct an integer point
2696 * of the context tableau.
2698 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2700 struct isl_vec *sample;
2702 if (!cgbr->shifted)
2703 gbr_init_shifted(cgbr);
2704 if (!cgbr->shifted)
2705 return NULL;
2706 if (cgbr->shifted->empty)
2707 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2709 sample = isl_tab_get_sample_value(cgbr->shifted);
2710 sample = isl_vec_ceil(sample);
2712 return sample;
2715 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2717 int i;
2719 if (!bset)
2720 return NULL;
2722 for (i = 0; i < bset->n_eq; ++i)
2723 isl_int_set_si(bset->eq[i][0], 0);
2725 for (i = 0; i < bset->n_ineq; ++i)
2726 isl_int_set_si(bset->ineq[i][0], 0);
2728 return bset;
2731 static int use_shifted(struct isl_context_gbr *cgbr)
2733 if (!cgbr->tab)
2734 return 0;
2735 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2738 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2740 struct isl_basic_set *bset;
2741 struct isl_basic_set *cone;
2743 if (isl_tab_sample_is_integer(cgbr->tab))
2744 return isl_tab_get_sample_value(cgbr->tab);
2746 if (use_shifted(cgbr)) {
2747 struct isl_vec *sample;
2749 sample = gbr_get_shifted_sample(cgbr);
2750 if (!sample || sample->size > 0)
2751 return sample;
2753 isl_vec_free(sample);
2756 if (!cgbr->cone) {
2757 bset = isl_tab_peek_bset(cgbr->tab);
2758 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2759 if (!cgbr->cone)
2760 return NULL;
2761 if (isl_tab_track_bset(cgbr->cone,
2762 isl_basic_set_copy(bset)) < 0)
2763 return NULL;
2765 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2766 return NULL;
2768 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2769 struct isl_vec *sample;
2770 struct isl_tab_undo *snap;
2772 if (cgbr->tab->basis) {
2773 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2774 isl_mat_free(cgbr->tab->basis);
2775 cgbr->tab->basis = NULL;
2777 cgbr->tab->n_zero = 0;
2778 cgbr->tab->n_unbounded = 0;
2781 snap = isl_tab_snap(cgbr->tab);
2783 sample = isl_tab_sample(cgbr->tab);
2785 if (isl_tab_rollback(cgbr->tab, snap) < 0) {
2786 isl_vec_free(sample);
2787 return NULL;
2790 return sample;
2793 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2794 cone = drop_constant_terms(cone);
2795 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2796 cone = isl_basic_set_underlying_set(cone);
2797 cone = isl_basic_set_gauss(cone, NULL);
2799 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2800 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2801 bset = isl_basic_set_underlying_set(bset);
2802 bset = isl_basic_set_gauss(bset, NULL);
2804 return isl_basic_set_sample_with_cone(bset, cone);
2807 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2809 struct isl_vec *sample;
2811 if (!cgbr->tab)
2812 return;
2814 if (cgbr->tab->empty)
2815 return;
2817 sample = gbr_get_sample(cgbr);
2818 if (!sample)
2819 goto error;
2821 if (sample->size == 0) {
2822 isl_vec_free(sample);
2823 if (isl_tab_mark_empty(cgbr->tab) < 0)
2824 goto error;
2825 return;
2828 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2829 goto error;
2831 return;
2832 error:
2833 isl_tab_free(cgbr->tab);
2834 cgbr->tab = NULL;
2837 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2839 if (!tab)
2840 return NULL;
2842 if (isl_tab_extend_cons(tab, 2) < 0)
2843 goto error;
2845 if (isl_tab_add_eq(tab, eq) < 0)
2846 goto error;
2848 return tab;
2849 error:
2850 isl_tab_free(tab);
2851 return NULL;
2854 /* Add the equality described by "eq" to the context.
2855 * If "check" is set, then we check if the context is empty after
2856 * adding the equality.
2857 * If "update" is set, then we check if the samples are still valid.
2859 * We do not explicitly add shifted copies of the equality to
2860 * cgbr->shifted since they would conflict with each other.
2861 * Instead, we directly mark cgbr->shifted empty.
2863 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2864 int check, int update)
2866 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2868 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2870 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2871 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2872 goto error;
2875 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2876 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2877 goto error;
2878 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2879 goto error;
2882 if (check) {
2883 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2884 if (v < 0)
2885 goto error;
2886 if (!v)
2887 check_gbr_integer_feasible(cgbr);
2889 if (update)
2890 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2891 return;
2892 error:
2893 isl_tab_free(cgbr->tab);
2894 cgbr->tab = NULL;
2897 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2899 if (!cgbr->tab)
2900 return;
2902 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2903 goto error;
2905 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2906 goto error;
2908 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2909 int i;
2910 unsigned dim;
2911 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2913 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2914 goto error;
2916 for (i = 0; i < dim; ++i) {
2917 if (!isl_int_is_neg(ineq[1 + i]))
2918 continue;
2919 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2922 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2923 goto error;
2925 for (i = 0; i < dim; ++i) {
2926 if (!isl_int_is_neg(ineq[1 + i]))
2927 continue;
2928 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2932 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2933 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2934 goto error;
2935 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2936 goto error;
2939 return;
2940 error:
2941 isl_tab_free(cgbr->tab);
2942 cgbr->tab = NULL;
2945 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2946 int check, int update)
2948 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2950 add_gbr_ineq(cgbr, ineq);
2951 if (!cgbr->tab)
2952 return;
2954 if (check) {
2955 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2956 if (v < 0)
2957 goto error;
2958 if (!v)
2959 check_gbr_integer_feasible(cgbr);
2961 if (update)
2962 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2963 return;
2964 error:
2965 isl_tab_free(cgbr->tab);
2966 cgbr->tab = NULL;
2969 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2971 struct isl_context *context = (struct isl_context *)user;
2972 context_gbr_add_ineq(context, ineq, 0, 0);
2973 return context->op->is_ok(context) ? 0 : -1;
2976 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2977 isl_int *ineq, int strict)
2979 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2980 return tab_ineq_sign(cgbr->tab, ineq, strict);
2983 /* Check whether "ineq" can be added to the tableau without rendering
2984 * it infeasible.
2986 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2988 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2989 struct isl_tab_undo *snap;
2990 struct isl_tab_undo *shifted_snap = NULL;
2991 struct isl_tab_undo *cone_snap = NULL;
2992 int feasible;
2994 if (!cgbr->tab)
2995 return -1;
2997 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2998 return -1;
3000 snap = isl_tab_snap(cgbr->tab);
3001 if (cgbr->shifted)
3002 shifted_snap = isl_tab_snap(cgbr->shifted);
3003 if (cgbr->cone)
3004 cone_snap = isl_tab_snap(cgbr->cone);
3005 add_gbr_ineq(cgbr, ineq);
3006 check_gbr_integer_feasible(cgbr);
3007 if (!cgbr->tab)
3008 return -1;
3009 feasible = !cgbr->tab->empty;
3010 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3011 return -1;
3012 if (shifted_snap) {
3013 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3014 return -1;
3015 } else if (cgbr->shifted) {
3016 isl_tab_free(cgbr->shifted);
3017 cgbr->shifted = NULL;
3019 if (cone_snap) {
3020 if (isl_tab_rollback(cgbr->cone, cone_snap))
3021 return -1;
3022 } else if (cgbr->cone) {
3023 isl_tab_free(cgbr->cone);
3024 cgbr->cone = NULL;
3027 return feasible;
3030 /* Return the column of the last of the variables associated to
3031 * a column that has a non-zero coefficient.
3032 * This function is called in a context where only coefficients
3033 * of parameters or divs can be non-zero.
3035 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3037 int i;
3038 int col;
3040 if (tab->n_var == 0)
3041 return -1;
3043 for (i = tab->n_var - 1; i >= 0; --i) {
3044 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3045 continue;
3046 if (tab->var[i].is_row)
3047 continue;
3048 col = tab->var[i].index;
3049 if (!isl_int_is_zero(p[col]))
3050 return col;
3053 return -1;
3056 /* Look through all the recently added equalities in the context
3057 * to see if we can propagate any of them to the main tableau.
3059 * The newly added equalities in the context are encoded as pairs
3060 * of inequalities starting at inequality "first".
3062 * We tentatively add each of these equalities to the main tableau
3063 * and if this happens to result in a row with a final coefficient
3064 * that is one or negative one, we use it to kill a column
3065 * in the main tableau. Otherwise, we discard the tentatively
3066 * added row.
3068 * Return 0 on success and -1 on failure.
3070 static int propagate_equalities(struct isl_context_gbr *cgbr,
3071 struct isl_tab *tab, unsigned first)
3073 int i;
3074 struct isl_vec *eq = NULL;
3076 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3077 if (!eq)
3078 goto error;
3080 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3081 goto error;
3083 isl_seq_clr(eq->el + 1 + tab->n_param,
3084 tab->n_var - tab->n_param - tab->n_div);
3085 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3086 int j;
3087 int r;
3088 struct isl_tab_undo *snap;
3089 snap = isl_tab_snap(tab);
3091 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3092 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3093 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3094 tab->n_div);
3096 r = isl_tab_add_row(tab, eq->el);
3097 if (r < 0)
3098 goto error;
3099 r = tab->con[r].index;
3100 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3101 if (j < 0 || j < tab->n_dead ||
3102 !isl_int_is_one(tab->mat->row[r][0]) ||
3103 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3104 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3105 if (isl_tab_rollback(tab, snap) < 0)
3106 goto error;
3107 continue;
3109 if (isl_tab_pivot(tab, r, j) < 0)
3110 goto error;
3111 if (isl_tab_kill_col(tab, j) < 0)
3112 goto error;
3114 if (restore_lexmin(tab) < 0)
3115 goto error;
3118 isl_vec_free(eq);
3120 return 0;
3121 error:
3122 isl_vec_free(eq);
3123 isl_tab_free(cgbr->tab);
3124 cgbr->tab = NULL;
3125 return -1;
3128 static int context_gbr_detect_equalities(struct isl_context *context,
3129 struct isl_tab *tab)
3131 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3132 struct isl_ctx *ctx;
3133 unsigned n_ineq;
3135 ctx = cgbr->tab->mat->ctx;
3137 if (!cgbr->cone) {
3138 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3139 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3140 if (!cgbr->cone)
3141 goto error;
3142 if (isl_tab_track_bset(cgbr->cone,
3143 isl_basic_set_copy(bset)) < 0)
3144 goto error;
3146 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3147 goto error;
3149 n_ineq = cgbr->tab->bmap->n_ineq;
3150 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3151 if (!cgbr->tab)
3152 return -1;
3153 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3154 propagate_equalities(cgbr, tab, n_ineq) < 0)
3155 return -1;
3157 return 0;
3158 error:
3159 isl_tab_free(cgbr->tab);
3160 cgbr->tab = NULL;
3161 return -1;
3164 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3165 struct isl_vec *div)
3167 return get_div(tab, context, div);
3170 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3172 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3173 if (cgbr->cone) {
3174 int k;
3176 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3177 return -1;
3178 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3179 return -1;
3180 if (isl_tab_allocate_var(cgbr->cone) <0)
3181 return -1;
3183 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3184 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3185 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3186 if (k < 0)
3187 return -1;
3188 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3189 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3190 return -1;
3192 return context_tab_add_div(cgbr->tab, div,
3193 context_gbr_add_ineq_wrap, context);
3196 static int context_gbr_best_split(struct isl_context *context,
3197 struct isl_tab *tab)
3199 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3200 struct isl_tab_undo *snap;
3201 int r;
3203 snap = isl_tab_snap(cgbr->tab);
3204 r = best_split(tab, cgbr->tab);
3206 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3207 return -1;
3209 return r;
3212 static int context_gbr_is_empty(struct isl_context *context)
3214 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3215 if (!cgbr->tab)
3216 return -1;
3217 return cgbr->tab->empty;
3220 struct isl_gbr_tab_undo {
3221 struct isl_tab_undo *tab_snap;
3222 struct isl_tab_undo *shifted_snap;
3223 struct isl_tab_undo *cone_snap;
3226 static void *context_gbr_save(struct isl_context *context)
3228 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3229 struct isl_gbr_tab_undo *snap;
3231 if (!cgbr->tab)
3232 return NULL;
3234 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3235 if (!snap)
3236 return NULL;
3238 snap->tab_snap = isl_tab_snap(cgbr->tab);
3239 if (isl_tab_save_samples(cgbr->tab) < 0)
3240 goto error;
3242 if (cgbr->shifted)
3243 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3244 else
3245 snap->shifted_snap = NULL;
3247 if (cgbr->cone)
3248 snap->cone_snap = isl_tab_snap(cgbr->cone);
3249 else
3250 snap->cone_snap = NULL;
3252 return snap;
3253 error:
3254 free(snap);
3255 return NULL;
3258 static void context_gbr_restore(struct isl_context *context, void *save)
3260 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3261 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3262 if (!snap)
3263 goto error;
3264 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
3265 isl_tab_free(cgbr->tab);
3266 cgbr->tab = NULL;
3269 if (snap->shifted_snap) {
3270 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3271 goto error;
3272 } else if (cgbr->shifted) {
3273 isl_tab_free(cgbr->shifted);
3274 cgbr->shifted = NULL;
3277 if (snap->cone_snap) {
3278 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3279 goto error;
3280 } else if (cgbr->cone) {
3281 isl_tab_free(cgbr->cone);
3282 cgbr->cone = NULL;
3285 free(snap);
3287 return;
3288 error:
3289 free(snap);
3290 isl_tab_free(cgbr->tab);
3291 cgbr->tab = NULL;
3294 static void context_gbr_discard(void *save)
3296 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3297 free(snap);
3300 static int context_gbr_is_ok(struct isl_context *context)
3302 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3303 return !!cgbr->tab;
3306 static void context_gbr_invalidate(struct isl_context *context)
3308 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3309 isl_tab_free(cgbr->tab);
3310 cgbr->tab = NULL;
3313 static void context_gbr_free(struct isl_context *context)
3315 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3316 isl_tab_free(cgbr->tab);
3317 isl_tab_free(cgbr->shifted);
3318 isl_tab_free(cgbr->cone);
3319 free(cgbr);
3322 struct isl_context_op isl_context_gbr_op = {
3323 context_gbr_detect_nonnegative_parameters,
3324 context_gbr_peek_basic_set,
3325 context_gbr_peek_tab,
3326 context_gbr_add_eq,
3327 context_gbr_add_ineq,
3328 context_gbr_ineq_sign,
3329 context_gbr_test_ineq,
3330 context_gbr_get_div,
3331 context_gbr_add_div,
3332 context_gbr_detect_equalities,
3333 context_gbr_best_split,
3334 context_gbr_is_empty,
3335 context_gbr_is_ok,
3336 context_gbr_save,
3337 context_gbr_restore,
3338 context_gbr_discard,
3339 context_gbr_invalidate,
3340 context_gbr_free,
3343 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3345 struct isl_context_gbr *cgbr;
3347 if (!dom)
3348 return NULL;
3350 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3351 if (!cgbr)
3352 return NULL;
3354 cgbr->context.op = &isl_context_gbr_op;
3356 cgbr->shifted = NULL;
3357 cgbr->cone = NULL;
3358 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3359 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3360 if (!cgbr->tab)
3361 goto error;
3362 check_gbr_integer_feasible(cgbr);
3364 return &cgbr->context;
3365 error:
3366 cgbr->context.op->free(&cgbr->context);
3367 return NULL;
3370 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3372 if (!dom)
3373 return NULL;
3375 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3376 return isl_context_lex_alloc(dom);
3377 else
3378 return isl_context_gbr_alloc(dom);
3381 /* Construct an isl_sol_map structure for accumulating the solution.
3382 * If track_empty is set, then we also keep track of the parts
3383 * of the context where there is no solution.
3384 * If max is set, then we are solving a maximization, rather than
3385 * a minimization problem, which means that the variables in the
3386 * tableau have value "M - x" rather than "M + x".
3388 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3389 struct isl_basic_set *dom, int track_empty, int max)
3391 struct isl_sol_map *sol_map = NULL;
3393 if (!bmap)
3394 goto error;
3396 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3397 if (!sol_map)
3398 goto error;
3400 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3401 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3402 sol_map->sol.dec_level.sol = &sol_map->sol;
3403 sol_map->sol.max = max;
3404 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3405 sol_map->sol.add = &sol_map_add_wrap;
3406 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3407 sol_map->sol.free = &sol_map_free_wrap;
3408 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3409 ISL_MAP_DISJOINT);
3410 if (!sol_map->map)
3411 goto error;
3413 sol_map->sol.context = isl_context_alloc(dom);
3414 if (!sol_map->sol.context)
3415 goto error;
3417 if (track_empty) {
3418 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3419 1, ISL_SET_DISJOINT);
3420 if (!sol_map->empty)
3421 goto error;
3424 isl_basic_set_free(dom);
3425 return &sol_map->sol;
3426 error:
3427 isl_basic_set_free(dom);
3428 sol_map_free(sol_map);
3429 return NULL;
3432 /* Check whether all coefficients of (non-parameter) variables
3433 * are non-positive, meaning that no pivots can be performed on the row.
3435 static int is_critical(struct isl_tab *tab, int row)
3437 int j;
3438 unsigned off = 2 + tab->M;
3440 for (j = tab->n_dead; j < tab->n_col; ++j) {
3441 if (tab->col_var[j] >= 0 &&
3442 (tab->col_var[j] < tab->n_param ||
3443 tab->col_var[j] >= tab->n_var - tab->n_div))
3444 continue;
3446 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3447 return 0;
3450 return 1;
3453 /* Check whether the inequality represented by vec is strict over the integers,
3454 * i.e., there are no integer values satisfying the constraint with
3455 * equality. This happens if the gcd of the coefficients is not a divisor
3456 * of the constant term. If so, scale the constraint down by the gcd
3457 * of the coefficients.
3459 static int is_strict(struct isl_vec *vec)
3461 isl_int gcd;
3462 int strict = 0;
3464 isl_int_init(gcd);
3465 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3466 if (!isl_int_is_one(gcd)) {
3467 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3468 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3469 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3471 isl_int_clear(gcd);
3473 return strict;
3476 /* Determine the sign of the given row of the main tableau.
3477 * The result is one of
3478 * isl_tab_row_pos: always non-negative; no pivot needed
3479 * isl_tab_row_neg: always non-positive; pivot
3480 * isl_tab_row_any: can be both positive and negative; split
3482 * We first handle some simple cases
3483 * - the row sign may be known already
3484 * - the row may be obviously non-negative
3485 * - the parametric constant may be equal to that of another row
3486 * for which we know the sign. This sign will be either "pos" or
3487 * "any". If it had been "neg" then we would have pivoted before.
3489 * If none of these cases hold, we check the value of the row for each
3490 * of the currently active samples. Based on the signs of these values
3491 * we make an initial determination of the sign of the row.
3493 * all zero -> unk(nown)
3494 * all non-negative -> pos
3495 * all non-positive -> neg
3496 * both negative and positive -> all
3498 * If we end up with "all", we are done.
3499 * Otherwise, we perform a check for positive and/or negative
3500 * values as follows.
3502 * samples neg unk pos
3503 * <0 ? Y N Y N
3504 * pos any pos
3505 * >0 ? Y N Y N
3506 * any neg any neg
3508 * There is no special sign for "zero", because we can usually treat zero
3509 * as either non-negative or non-positive, whatever works out best.
3510 * However, if the row is "critical", meaning that pivoting is impossible
3511 * then we don't want to limp zero with the non-positive case, because
3512 * then we we would lose the solution for those values of the parameters
3513 * where the value of the row is zero. Instead, we treat 0 as non-negative
3514 * ensuring a split if the row can attain both zero and negative values.
3515 * The same happens when the original constraint was one that could not
3516 * be satisfied with equality by any integer values of the parameters.
3517 * In this case, we normalize the constraint, but then a value of zero
3518 * for the normalized constraint is actually a positive value for the
3519 * original constraint, so again we need to treat zero as non-negative.
3520 * In both these cases, we have the following decision tree instead:
3522 * all non-negative -> pos
3523 * all negative -> neg
3524 * both negative and non-negative -> all
3526 * samples neg pos
3527 * <0 ? Y N
3528 * any pos
3529 * >=0 ? Y N
3530 * any neg
3532 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3533 struct isl_sol *sol, int row)
3535 struct isl_vec *ineq = NULL;
3536 enum isl_tab_row_sign res = isl_tab_row_unknown;
3537 int critical;
3538 int strict;
3539 int row2;
3541 if (tab->row_sign[row] != isl_tab_row_unknown)
3542 return tab->row_sign[row];
3543 if (is_obviously_nonneg(tab, row))
3544 return isl_tab_row_pos;
3545 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3546 if (tab->row_sign[row2] == isl_tab_row_unknown)
3547 continue;
3548 if (identical_parameter_line(tab, row, row2))
3549 return tab->row_sign[row2];
3552 critical = is_critical(tab, row);
3554 ineq = get_row_parameter_ineq(tab, row);
3555 if (!ineq)
3556 goto error;
3558 strict = is_strict(ineq);
3560 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3561 critical || strict);
3563 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3564 /* test for negative values */
3565 int feasible;
3566 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3567 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3569 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3570 if (feasible < 0)
3571 goto error;
3572 if (!feasible)
3573 res = isl_tab_row_pos;
3574 else
3575 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3576 : isl_tab_row_any;
3577 if (res == isl_tab_row_neg) {
3578 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3579 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3583 if (res == isl_tab_row_neg) {
3584 /* test for positive values */
3585 int feasible;
3586 if (!critical && !strict)
3587 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3589 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3590 if (feasible < 0)
3591 goto error;
3592 if (feasible)
3593 res = isl_tab_row_any;
3596 isl_vec_free(ineq);
3597 return res;
3598 error:
3599 isl_vec_free(ineq);
3600 return isl_tab_row_unknown;
3603 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3605 /* Find solutions for values of the parameters that satisfy the given
3606 * inequality.
3608 * We currently take a snapshot of the context tableau that is reset
3609 * when we return from this function, while we make a copy of the main
3610 * tableau, leaving the original main tableau untouched.
3611 * These are fairly arbitrary choices. Making a copy also of the context
3612 * tableau would obviate the need to undo any changes made to it later,
3613 * while taking a snapshot of the main tableau could reduce memory usage.
3614 * If we were to switch to taking a snapshot of the main tableau,
3615 * we would have to keep in mind that we need to save the row signs
3616 * and that we need to do this before saving the current basis
3617 * such that the basis has been restore before we restore the row signs.
3619 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3621 void *saved;
3623 if (!sol->context)
3624 goto error;
3625 saved = sol->context->op->save(sol->context);
3627 tab = isl_tab_dup(tab);
3628 if (!tab)
3629 goto error;
3631 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3633 find_solutions(sol, tab);
3635 if (!sol->error)
3636 sol->context->op->restore(sol->context, saved);
3637 else
3638 sol->context->op->discard(saved);
3639 return;
3640 error:
3641 sol->error = 1;
3644 /* Record the absence of solutions for those values of the parameters
3645 * that do not satisfy the given inequality with equality.
3647 static void no_sol_in_strict(struct isl_sol *sol,
3648 struct isl_tab *tab, struct isl_vec *ineq)
3650 int empty;
3651 void *saved;
3653 if (!sol->context || sol->error)
3654 goto error;
3655 saved = sol->context->op->save(sol->context);
3657 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3659 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3660 if (!sol->context)
3661 goto error;
3663 empty = tab->empty;
3664 tab->empty = 1;
3665 sol_add(sol, tab);
3666 tab->empty = empty;
3668 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3670 sol->context->op->restore(sol->context, saved);
3671 return;
3672 error:
3673 sol->error = 1;
3676 /* Compute the lexicographic minimum of the set represented by the main
3677 * tableau "tab" within the context "sol->context_tab".
3678 * On entry the sample value of the main tableau is lexicographically
3679 * less than or equal to this lexicographic minimum.
3680 * Pivots are performed until a feasible point is found, which is then
3681 * necessarily equal to the minimum, or until the tableau is found to
3682 * be infeasible. Some pivots may need to be performed for only some
3683 * feasible values of the context tableau. If so, the context tableau
3684 * is split into a part where the pivot is needed and a part where it is not.
3686 * Whenever we enter the main loop, the main tableau is such that no
3687 * "obvious" pivots need to be performed on it, where "obvious" means
3688 * that the given row can be seen to be negative without looking at
3689 * the context tableau. In particular, for non-parametric problems,
3690 * no pivots need to be performed on the main tableau.
3691 * The caller of find_solutions is responsible for making this property
3692 * hold prior to the first iteration of the loop, while restore_lexmin
3693 * is called before every other iteration.
3695 * Inside the main loop, we first examine the signs of the rows of
3696 * the main tableau within the context of the context tableau.
3697 * If we find a row that is always non-positive for all values of
3698 * the parameters satisfying the context tableau and negative for at
3699 * least one value of the parameters, we perform the appropriate pivot
3700 * and start over. An exception is the case where no pivot can be
3701 * performed on the row. In this case, we require that the sign of
3702 * the row is negative for all values of the parameters (rather than just
3703 * non-positive). This special case is handled inside row_sign, which
3704 * will say that the row can have any sign if it determines that it can
3705 * attain both negative and zero values.
3707 * If we can't find a row that always requires a pivot, but we can find
3708 * one or more rows that require a pivot for some values of the parameters
3709 * (i.e., the row can attain both positive and negative signs), then we split
3710 * the context tableau into two parts, one where we force the sign to be
3711 * non-negative and one where we force is to be negative.
3712 * The non-negative part is handled by a recursive call (through find_in_pos).
3713 * Upon returning from this call, we continue with the negative part and
3714 * perform the required pivot.
3716 * If no such rows can be found, all rows are non-negative and we have
3717 * found a (rational) feasible point. If we only wanted a rational point
3718 * then we are done.
3719 * Otherwise, we check if all values of the sample point of the tableau
3720 * are integral for the variables. If so, we have found the minimal
3721 * integral point and we are done.
3722 * If the sample point is not integral, then we need to make a distinction
3723 * based on whether the constant term is non-integral or the coefficients
3724 * of the parameters. Furthermore, in order to decide how to handle
3725 * the non-integrality, we also need to know whether the coefficients
3726 * of the other columns in the tableau are integral. This leads
3727 * to the following table. The first two rows do not correspond
3728 * to a non-integral sample point and are only mentioned for completeness.
3730 * constant parameters other
3732 * int int int |
3733 * int int rat | -> no problem
3735 * rat int int -> fail
3737 * rat int rat -> cut
3739 * int rat rat |
3740 * rat rat rat | -> parametric cut
3742 * int rat int |
3743 * rat rat int | -> split context
3745 * If the parametric constant is completely integral, then there is nothing
3746 * to be done. If the constant term is non-integral, but all the other
3747 * coefficient are integral, then there is nothing that can be done
3748 * and the tableau has no integral solution.
3749 * If, on the other hand, one or more of the other columns have rational
3750 * coefficients, but the parameter coefficients are all integral, then
3751 * we can perform a regular (non-parametric) cut.
3752 * Finally, if there is any parameter coefficient that is non-integral,
3753 * then we need to involve the context tableau. There are two cases here.
3754 * If at least one other column has a rational coefficient, then we
3755 * can perform a parametric cut in the main tableau by adding a new
3756 * integer division in the context tableau.
3757 * If all other columns have integral coefficients, then we need to
3758 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3759 * is always integral. We do this by introducing an integer division
3760 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3761 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3762 * Since q is expressed in the tableau as
3763 * c + \sum a_i y_i - m q >= 0
3764 * -c - \sum a_i y_i + m q + m - 1 >= 0
3765 * it is sufficient to add the inequality
3766 * -c - \sum a_i y_i + m q >= 0
3767 * In the part of the context where this inequality does not hold, the
3768 * main tableau is marked as being empty.
3770 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3772 struct isl_context *context;
3773 int r;
3775 if (!tab || sol->error)
3776 goto error;
3778 context = sol->context;
3780 if (tab->empty)
3781 goto done;
3782 if (context->op->is_empty(context))
3783 goto done;
3785 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3786 int flags;
3787 int row;
3788 enum isl_tab_row_sign sgn;
3789 int split = -1;
3790 int n_split = 0;
3792 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3793 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3794 continue;
3795 sgn = row_sign(tab, sol, row);
3796 if (!sgn)
3797 goto error;
3798 tab->row_sign[row] = sgn;
3799 if (sgn == isl_tab_row_any)
3800 n_split++;
3801 if (sgn == isl_tab_row_any && split == -1)
3802 split = row;
3803 if (sgn == isl_tab_row_neg)
3804 break;
3806 if (row < tab->n_row)
3807 continue;
3808 if (split != -1) {
3809 struct isl_vec *ineq;
3810 if (n_split != 1)
3811 split = context->op->best_split(context, tab);
3812 if (split < 0)
3813 goto error;
3814 ineq = get_row_parameter_ineq(tab, split);
3815 if (!ineq)
3816 goto error;
3817 is_strict(ineq);
3818 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3819 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3820 continue;
3821 if (tab->row_sign[row] == isl_tab_row_any)
3822 tab->row_sign[row] = isl_tab_row_unknown;
3824 tab->row_sign[split] = isl_tab_row_pos;
3825 sol_inc_level(sol);
3826 find_in_pos(sol, tab, ineq->el);
3827 tab->row_sign[split] = isl_tab_row_neg;
3828 row = split;
3829 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3830 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3831 if (!sol->error)
3832 context->op->add_ineq(context, ineq->el, 0, 1);
3833 isl_vec_free(ineq);
3834 if (sol->error)
3835 goto error;
3836 continue;
3838 if (tab->rational)
3839 break;
3840 row = first_non_integer_row(tab, &flags);
3841 if (row < 0)
3842 break;
3843 if (ISL_FL_ISSET(flags, I_PAR)) {
3844 if (ISL_FL_ISSET(flags, I_VAR)) {
3845 if (isl_tab_mark_empty(tab) < 0)
3846 goto error;
3847 break;
3849 row = add_cut(tab, row);
3850 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3851 struct isl_vec *div;
3852 struct isl_vec *ineq;
3853 int d;
3854 div = get_row_split_div(tab, row);
3855 if (!div)
3856 goto error;
3857 d = context->op->get_div(context, tab, div);
3858 isl_vec_free(div);
3859 if (d < 0)
3860 goto error;
3861 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3862 if (!ineq)
3863 goto error;
3864 sol_inc_level(sol);
3865 no_sol_in_strict(sol, tab, ineq);
3866 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3867 context->op->add_ineq(context, ineq->el, 1, 1);
3868 isl_vec_free(ineq);
3869 if (sol->error || !context->op->is_ok(context))
3870 goto error;
3871 tab = set_row_cst_to_div(tab, row, d);
3872 if (context->op->is_empty(context))
3873 break;
3874 } else
3875 row = add_parametric_cut(tab, row, context);
3876 if (row < 0)
3877 goto error;
3879 if (r < 0)
3880 goto error;
3881 done:
3882 sol_add(sol, tab);
3883 isl_tab_free(tab);
3884 return;
3885 error:
3886 isl_tab_free(tab);
3887 sol->error = 1;
3890 /* Does "sol" contain a pair of partial solutions that could potentially
3891 * be merged?
3893 * We currently only check that "sol" is not in an error state
3894 * and that there are at least two partial solutions of which the final two
3895 * are defined at the same level.
3897 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3899 if (sol->error)
3900 return 0;
3901 if (!sol->partial)
3902 return 0;
3903 if (!sol->partial->next)
3904 return 0;
3905 return sol->partial->level == sol->partial->next->level;
3908 /* Compute the lexicographic minimum of the set represented by the main
3909 * tableau "tab" within the context "sol->context_tab".
3911 * As a preprocessing step, we first transfer all the purely parametric
3912 * equalities from the main tableau to the context tableau, i.e.,
3913 * parameters that have been pivoted to a row.
3914 * These equalities are ignored by the main algorithm, because the
3915 * corresponding rows may not be marked as being non-negative.
3916 * In parts of the context where the added equality does not hold,
3917 * the main tableau is marked as being empty.
3919 * Before we embark on the actual computation, we save a copy
3920 * of the context. When we return, we check if there are any
3921 * partial solutions that can potentially be merged. If so,
3922 * we perform a rollback to the initial state of the context.
3923 * The merging of partial solutions happens inside calls to
3924 * sol_dec_level that are pushed onto the undo stack of the context.
3925 * If there are no partial solutions that can potentially be merged
3926 * then the rollback is skipped as it would just be wasted effort.
3928 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3930 int row;
3931 void *saved;
3933 if (!tab)
3934 goto error;
3936 sol->level = 0;
3938 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3939 int p;
3940 struct isl_vec *eq;
3942 if (tab->row_var[row] < 0)
3943 continue;
3944 if (tab->row_var[row] >= tab->n_param &&
3945 tab->row_var[row] < tab->n_var - tab->n_div)
3946 continue;
3947 if (tab->row_var[row] < tab->n_param)
3948 p = tab->row_var[row];
3949 else
3950 p = tab->row_var[row]
3951 + tab->n_param - (tab->n_var - tab->n_div);
3953 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3954 if (!eq)
3955 goto error;
3956 get_row_parameter_line(tab, row, eq->el);
3957 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3958 eq = isl_vec_normalize(eq);
3960 sol_inc_level(sol);
3961 no_sol_in_strict(sol, tab, eq);
3963 isl_seq_neg(eq->el, eq->el, eq->size);
3964 sol_inc_level(sol);
3965 no_sol_in_strict(sol, tab, eq);
3966 isl_seq_neg(eq->el, eq->el, eq->size);
3968 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3970 isl_vec_free(eq);
3972 if (isl_tab_mark_redundant(tab, row) < 0)
3973 goto error;
3975 if (sol->context->op->is_empty(sol->context))
3976 break;
3978 row = tab->n_redundant - 1;
3981 saved = sol->context->op->save(sol->context);
3983 find_solutions(sol, tab);
3985 if (sol_has_mergeable_solutions(sol))
3986 sol->context->op->restore(sol->context, saved);
3987 else
3988 sol->context->op->discard(saved);
3990 sol->level = 0;
3991 sol_pop(sol);
3993 return;
3994 error:
3995 isl_tab_free(tab);
3996 sol->error = 1;
3999 /* Check if integer division "div" of "dom" also occurs in "bmap".
4000 * If so, return its position within the divs.
4001 * If not, return -1.
4003 static int find_context_div(struct isl_basic_map *bmap,
4004 struct isl_basic_set *dom, unsigned div)
4006 int i;
4007 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4008 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4010 if (isl_int_is_zero(dom->div[div][0]))
4011 return -1;
4012 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4013 return -1;
4015 for (i = 0; i < bmap->n_div; ++i) {
4016 if (isl_int_is_zero(bmap->div[i][0]))
4017 continue;
4018 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4019 (b_dim - d_dim) + bmap->n_div) != -1)
4020 continue;
4021 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4022 return i;
4024 return -1;
4027 /* The correspondence between the variables in the main tableau,
4028 * the context tableau, and the input map and domain is as follows.
4029 * The first n_param and the last n_div variables of the main tableau
4030 * form the variables of the context tableau.
4031 * In the basic map, these n_param variables correspond to the
4032 * parameters and the input dimensions. In the domain, they correspond
4033 * to the parameters and the set dimensions.
4034 * The n_div variables correspond to the integer divisions in the domain.
4035 * To ensure that everything lines up, we may need to copy some of the
4036 * integer divisions of the domain to the map. These have to be placed
4037 * in the same order as those in the context and they have to be placed
4038 * after any other integer divisions that the map may have.
4039 * This function performs the required reordering.
4041 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4042 struct isl_basic_set *dom)
4044 int i;
4045 int common = 0;
4046 int other;
4048 for (i = 0; i < dom->n_div; ++i)
4049 if (find_context_div(bmap, dom, i) != -1)
4050 common++;
4051 other = bmap->n_div - common;
4052 if (dom->n_div - common > 0) {
4053 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4054 dom->n_div - common, 0, 0);
4055 if (!bmap)
4056 return NULL;
4058 for (i = 0; i < dom->n_div; ++i) {
4059 int pos = find_context_div(bmap, dom, i);
4060 if (pos < 0) {
4061 pos = isl_basic_map_alloc_div(bmap);
4062 if (pos < 0)
4063 goto error;
4064 isl_int_set_si(bmap->div[pos][0], 0);
4066 if (pos != other + i)
4067 isl_basic_map_swap_div(bmap, pos, other + i);
4069 return bmap;
4070 error:
4071 isl_basic_map_free(bmap);
4072 return NULL;
4075 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4076 * some obvious symmetries.
4078 * We make sure the divs in the domain are properly ordered,
4079 * because they will be added one by one in the given order
4080 * during the construction of the solution map.
4082 static struct isl_sol *basic_map_partial_lexopt_base(
4083 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4084 __isl_give isl_set **empty, int max,
4085 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4086 __isl_take isl_basic_set *dom, int track_empty, int max))
4088 struct isl_tab *tab;
4089 struct isl_sol *sol = NULL;
4090 struct isl_context *context;
4092 if (dom->n_div) {
4093 dom = isl_basic_set_order_divs(dom);
4094 bmap = align_context_divs(bmap, dom);
4096 sol = init(bmap, dom, !!empty, max);
4097 if (!sol)
4098 goto error;
4100 context = sol->context;
4101 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4102 /* nothing */;
4103 else if (isl_basic_map_plain_is_empty(bmap)) {
4104 if (sol->add_empty)
4105 sol->add_empty(sol,
4106 isl_basic_set_copy(context->op->peek_basic_set(context)));
4107 } else {
4108 tab = tab_for_lexmin(bmap,
4109 context->op->peek_basic_set(context), 1, max);
4110 tab = context->op->detect_nonnegative_parameters(context, tab);
4111 find_solutions_main(sol, tab);
4113 if (sol->error)
4114 goto error;
4116 isl_basic_map_free(bmap);
4117 return sol;
4118 error:
4119 sol_free(sol);
4120 isl_basic_map_free(bmap);
4121 return NULL;
4124 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4125 * some obvious symmetries.
4127 * We call basic_map_partial_lexopt_base and extract the results.
4129 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
4130 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4131 __isl_give isl_set **empty, int max)
4133 isl_map *result = NULL;
4134 struct isl_sol *sol;
4135 struct isl_sol_map *sol_map;
4137 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4138 &sol_map_init);
4139 if (!sol)
4140 return NULL;
4141 sol_map = (struct isl_sol_map *) sol;
4143 result = isl_map_copy(sol_map->map);
4144 if (empty)
4145 *empty = isl_set_copy(sol_map->empty);
4146 sol_free(&sol_map->sol);
4147 return result;
4150 /* Structure used during detection of parallel constraints.
4151 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4152 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4153 * val: the coefficients of the output variables
4155 struct isl_constraint_equal_info {
4156 isl_basic_map *bmap;
4157 unsigned n_in;
4158 unsigned n_out;
4159 isl_int *val;
4162 /* Check whether the coefficients of the output variables
4163 * of the constraint in "entry" are equal to info->val.
4165 static int constraint_equal(const void *entry, const void *val)
4167 isl_int **row = (isl_int **)entry;
4168 const struct isl_constraint_equal_info *info = val;
4170 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4173 /* Check whether "bmap" has a pair of constraints that have
4174 * the same coefficients for the output variables.
4175 * Note that the coefficients of the existentially quantified
4176 * variables need to be zero since the existentially quantified
4177 * of the result are usually not the same as those of the input.
4178 * the isl_dim_out and isl_dim_div dimensions.
4179 * If so, return 1 and return the row indices of the two constraints
4180 * in *first and *second.
4182 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4183 int *first, int *second)
4185 int i;
4186 isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
4187 struct isl_hash_table *table = NULL;
4188 struct isl_hash_table_entry *entry;
4189 struct isl_constraint_equal_info info;
4190 unsigned n_out;
4191 unsigned n_div;
4193 ctx = isl_basic_map_get_ctx(bmap);
4194 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4195 if (!table)
4196 goto error;
4198 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4199 isl_basic_map_dim(bmap, isl_dim_in);
4200 info.bmap = bmap;
4201 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4202 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4203 info.n_out = n_out + n_div;
4204 for (i = 0; i < bmap->n_ineq; ++i) {
4205 uint32_t hash;
4207 info.val = bmap->ineq[i] + 1 + info.n_in;
4208 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4209 continue;
4210 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4211 continue;
4212 hash = isl_seq_get_hash(info.val, info.n_out);
4213 entry = isl_hash_table_find(ctx, table, hash,
4214 constraint_equal, &info, 1);
4215 if (!entry)
4216 goto error;
4217 if (entry->data)
4218 break;
4219 entry->data = &bmap->ineq[i];
4222 if (i < bmap->n_ineq) {
4223 *first = ((isl_int **)entry->data) - bmap->ineq;
4224 *second = i;
4227 isl_hash_table_free(ctx, table);
4229 return i < bmap->n_ineq;
4230 error:
4231 isl_hash_table_free(ctx, table);
4232 return -1;
4235 /* Given a set of upper bounds in "var", add constraints to "bset"
4236 * that make the i-th bound smallest.
4238 * In particular, if there are n bounds b_i, then add the constraints
4240 * b_i <= b_j for j > i
4241 * b_i < b_j for j < i
4243 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4244 __isl_keep isl_mat *var, int i)
4246 isl_ctx *ctx;
4247 int j, k;
4249 ctx = isl_mat_get_ctx(var);
4251 for (j = 0; j < var->n_row; ++j) {
4252 if (j == i)
4253 continue;
4254 k = isl_basic_set_alloc_inequality(bset);
4255 if (k < 0)
4256 goto error;
4257 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4258 ctx->negone, var->row[i], var->n_col);
4259 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4260 if (j < i)
4261 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4264 bset = isl_basic_set_finalize(bset);
4266 return bset;
4267 error:
4268 isl_basic_set_free(bset);
4269 return NULL;
4272 /* Given a set of upper bounds on the last "input" variable m,
4273 * construct a set that assigns the minimal upper bound to m, i.e.,
4274 * construct a set that divides the space into cells where one
4275 * of the upper bounds is smaller than all the others and assign
4276 * this upper bound to m.
4278 * In particular, if there are n bounds b_i, then the result
4279 * consists of n basic sets, each one of the form
4281 * m = b_i
4282 * b_i <= b_j for j > i
4283 * b_i < b_j for j < i
4285 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4286 __isl_take isl_mat *var)
4288 int i, k;
4289 isl_basic_set *bset = NULL;
4290 isl_ctx *ctx;
4291 isl_set *set = NULL;
4293 if (!dim || !var)
4294 goto error;
4296 ctx = isl_space_get_ctx(dim);
4297 set = isl_set_alloc_space(isl_space_copy(dim),
4298 var->n_row, ISL_SET_DISJOINT);
4300 for (i = 0; i < var->n_row; ++i) {
4301 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4302 1, var->n_row - 1);
4303 k = isl_basic_set_alloc_equality(bset);
4304 if (k < 0)
4305 goto error;
4306 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4307 isl_int_set_si(bset->eq[k][var->n_col], -1);
4308 bset = select_minimum(bset, var, i);
4309 set = isl_set_add_basic_set(set, bset);
4312 isl_space_free(dim);
4313 isl_mat_free(var);
4314 return set;
4315 error:
4316 isl_basic_set_free(bset);
4317 isl_set_free(set);
4318 isl_space_free(dim);
4319 isl_mat_free(var);
4320 return NULL;
4323 /* Given that the last input variable of "bmap" represents the minimum
4324 * of the bounds in "cst", check whether we need to split the domain
4325 * based on which bound attains the minimum.
4327 * A split is needed when the minimum appears in an integer division
4328 * or in an equality. Otherwise, it is only needed if it appears in
4329 * an upper bound that is different from the upper bounds on which it
4330 * is defined.
4332 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4333 __isl_keep isl_mat *cst)
4335 int i, j;
4336 unsigned total;
4337 unsigned pos;
4339 pos = cst->n_col - 1;
4340 total = isl_basic_map_dim(bmap, isl_dim_all);
4342 for (i = 0; i < bmap->n_div; ++i)
4343 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4344 return 1;
4346 for (i = 0; i < bmap->n_eq; ++i)
4347 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4348 return 1;
4350 for (i = 0; i < bmap->n_ineq; ++i) {
4351 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4352 continue;
4353 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4354 return 1;
4355 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4356 total - pos - 1) >= 0)
4357 return 1;
4359 for (j = 0; j < cst->n_row; ++j)
4360 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4361 break;
4362 if (j >= cst->n_row)
4363 return 1;
4366 return 0;
4369 /* Given that the last set variable of "bset" represents the minimum
4370 * of the bounds in "cst", check whether we need to split the domain
4371 * based on which bound attains the minimum.
4373 * We simply call need_split_basic_map here. This is safe because
4374 * the position of the minimum is computed from "cst" and not
4375 * from "bmap".
4377 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4378 __isl_keep isl_mat *cst)
4380 return need_split_basic_map((isl_basic_map *)bset, cst);
4383 /* Given that the last set variable of "set" represents the minimum
4384 * of the bounds in "cst", check whether we need to split the domain
4385 * based on which bound attains the minimum.
4387 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4389 int i;
4391 for (i = 0; i < set->n; ++i)
4392 if (need_split_basic_set(set->p[i], cst))
4393 return 1;
4395 return 0;
4398 /* Given a set of which the last set variable is the minimum
4399 * of the bounds in "cst", split each basic set in the set
4400 * in pieces where one of the bounds is (strictly) smaller than the others.
4401 * This subdivision is given in "min_expr".
4402 * The variable is subsequently projected out.
4404 * We only do the split when it is needed.
4405 * For example if the last input variable m = min(a,b) and the only
4406 * constraints in the given basic set are lower bounds on m,
4407 * i.e., l <= m = min(a,b), then we can simply project out m
4408 * to obtain l <= a and l <= b, without having to split on whether
4409 * m is equal to a or b.
4411 static __isl_give isl_set *split(__isl_take isl_set *empty,
4412 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4414 int n_in;
4415 int i;
4416 isl_space *dim;
4417 isl_set *res;
4419 if (!empty || !min_expr || !cst)
4420 goto error;
4422 n_in = isl_set_dim(empty, isl_dim_set);
4423 dim = isl_set_get_space(empty);
4424 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4425 res = isl_set_empty(dim);
4427 for (i = 0; i < empty->n; ++i) {
4428 isl_set *set;
4430 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4431 if (need_split_basic_set(empty->p[i], cst))
4432 set = isl_set_intersect(set, isl_set_copy(min_expr));
4433 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4435 res = isl_set_union_disjoint(res, set);
4438 isl_set_free(empty);
4439 isl_set_free(min_expr);
4440 isl_mat_free(cst);
4441 return res;
4442 error:
4443 isl_set_free(empty);
4444 isl_set_free(min_expr);
4445 isl_mat_free(cst);
4446 return NULL;
4449 /* Given a map of which the last input variable is the minimum
4450 * of the bounds in "cst", split each basic set in the set
4451 * in pieces where one of the bounds is (strictly) smaller than the others.
4452 * This subdivision is given in "min_expr".
4453 * The variable is subsequently projected out.
4455 * The implementation is essentially the same as that of "split".
4457 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4458 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4460 int n_in;
4461 int i;
4462 isl_space *dim;
4463 isl_map *res;
4465 if (!opt || !min_expr || !cst)
4466 goto error;
4468 n_in = isl_map_dim(opt, isl_dim_in);
4469 dim = isl_map_get_space(opt);
4470 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4471 res = isl_map_empty(dim);
4473 for (i = 0; i < opt->n; ++i) {
4474 isl_map *map;
4476 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4477 if (need_split_basic_map(opt->p[i], cst))
4478 map = isl_map_intersect_domain(map,
4479 isl_set_copy(min_expr));
4480 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4482 res = isl_map_union_disjoint(res, map);
4485 isl_map_free(opt);
4486 isl_set_free(min_expr);
4487 isl_mat_free(cst);
4488 return res;
4489 error:
4490 isl_map_free(opt);
4491 isl_set_free(min_expr);
4492 isl_mat_free(cst);
4493 return NULL;
4496 static __isl_give isl_map *basic_map_partial_lexopt(
4497 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4498 __isl_give isl_set **empty, int max);
4500 union isl_lex_res {
4501 void *p;
4502 isl_map *map;
4503 isl_pw_multi_aff *pma;
4506 /* This function is called from basic_map_partial_lexopt_symm.
4507 * The last variable of "bmap" and "dom" corresponds to the minimum
4508 * of the bounds in "cst". "map_space" is the space of the original
4509 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4510 * is the space of the original domain.
4512 * We recursively call basic_map_partial_lexopt and then plug in
4513 * the definition of the minimum in the result.
4515 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4516 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4517 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4518 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4520 isl_map *opt;
4521 isl_set *min_expr;
4522 union isl_lex_res res;
4524 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4526 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4528 if (empty) {
4529 *empty = split(*empty,
4530 isl_set_copy(min_expr), isl_mat_copy(cst));
4531 *empty = isl_set_reset_space(*empty, set_space);
4534 opt = split_domain(opt, min_expr, cst);
4535 opt = isl_map_reset_space(opt, map_space);
4537 res.map = opt;
4538 return res;
4541 /* Given a basic map with at least two parallel constraints (as found
4542 * by the function parallel_constraints), first look for more constraints
4543 * parallel to the two constraint and replace the found list of parallel
4544 * constraints by a single constraint with as "input" part the minimum
4545 * of the input parts of the list of constraints. Then, recursively call
4546 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4547 * and plug in the definition of the minimum in the result.
4549 * More specifically, given a set of constraints
4551 * a x + b_i(p) >= 0
4553 * Replace this set by a single constraint
4555 * a x + u >= 0
4557 * with u a new parameter with constraints
4559 * u <= b_i(p)
4561 * Any solution to the new system is also a solution for the original system
4562 * since
4564 * a x >= -u >= -b_i(p)
4566 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4567 * therefore be plugged into the solution.
4569 static union isl_lex_res basic_map_partial_lexopt_symm(
4570 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4571 __isl_give isl_set **empty, int max, int first, int second,
4572 __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4573 __isl_take isl_basic_set *dom,
4574 __isl_give isl_set **empty,
4575 int max, __isl_take isl_mat *cst,
4576 __isl_take isl_space *map_space,
4577 __isl_take isl_space *set_space))
4579 int i, n, k;
4580 int *list = NULL;
4581 unsigned n_in, n_out, n_div;
4582 isl_ctx *ctx;
4583 isl_vec *var = NULL;
4584 isl_mat *cst = NULL;
4585 isl_space *map_space, *set_space;
4586 union isl_lex_res res;
4588 map_space = isl_basic_map_get_space(bmap);
4589 set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4591 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4592 isl_basic_map_dim(bmap, isl_dim_in);
4593 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4595 ctx = isl_basic_map_get_ctx(bmap);
4596 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4597 var = isl_vec_alloc(ctx, n_out);
4598 if ((bmap->n_ineq && !list) || (n_out && !var))
4599 goto error;
4601 list[0] = first;
4602 list[1] = second;
4603 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4604 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4605 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4606 list[n++] = i;
4609 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4610 if (!cst)
4611 goto error;
4613 for (i = 0; i < n; ++i)
4614 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4616 bmap = isl_basic_map_cow(bmap);
4617 if (!bmap)
4618 goto error;
4619 for (i = n - 1; i >= 0; --i)
4620 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4621 goto error;
4623 bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4624 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4625 k = isl_basic_map_alloc_inequality(bmap);
4626 if (k < 0)
4627 goto error;
4628 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4629 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4630 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4631 bmap = isl_basic_map_finalize(bmap);
4633 n_div = isl_basic_set_dim(dom, isl_dim_div);
4634 dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
4635 dom = isl_basic_set_extend_constraints(dom, 0, n);
4636 for (i = 0; i < n; ++i) {
4637 k = isl_basic_set_alloc_inequality(dom);
4638 if (k < 0)
4639 goto error;
4640 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4641 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4642 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4645 isl_vec_free(var);
4646 free(list);
4648 return core(bmap, dom, empty, max, cst, map_space, set_space);
4649 error:
4650 isl_space_free(map_space);
4651 isl_space_free(set_space);
4652 isl_mat_free(cst);
4653 isl_vec_free(var);
4654 free(list);
4655 isl_basic_set_free(dom);
4656 isl_basic_map_free(bmap);
4657 res.p = NULL;
4658 return res;
4661 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4662 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4663 __isl_give isl_set **empty, int max, int first, int second)
4665 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4666 first, second, &basic_map_partial_lexopt_symm_map_core).map;
4669 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4670 * equalities and removing redundant constraints.
4672 * We first check if there are any parallel constraints (left).
4673 * If not, we are in the base case.
4674 * If there are parallel constraints, we replace them by a single
4675 * constraint in basic_map_partial_lexopt_symm and then call
4676 * this function recursively to look for more parallel constraints.
4678 static __isl_give isl_map *basic_map_partial_lexopt(
4679 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4680 __isl_give isl_set **empty, int max)
4682 int par = 0;
4683 int first, second;
4685 if (!bmap)
4686 goto error;
4688 if (bmap->ctx->opt->pip_symmetry)
4689 par = parallel_constraints(bmap, &first, &second);
4690 if (par < 0)
4691 goto error;
4692 if (!par)
4693 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4695 return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4696 first, second);
4697 error:
4698 isl_basic_set_free(dom);
4699 isl_basic_map_free(bmap);
4700 return NULL;
4703 /* Compute the lexicographic minimum (or maximum if "max" is set)
4704 * of "bmap" over the domain "dom" and return the result as a map.
4705 * If "empty" is not NULL, then *empty is assigned a set that
4706 * contains those parts of the domain where there is no solution.
4707 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4708 * then we compute the rational optimum. Otherwise, we compute
4709 * the integral optimum.
4711 * We perform some preprocessing. As the PILP solver does not
4712 * handle implicit equalities very well, we first make sure all
4713 * the equalities are explicitly available.
4715 * We also add context constraints to the basic map and remove
4716 * redundant constraints. This is only needed because of the
4717 * way we handle simple symmetries. In particular, we currently look
4718 * for symmetries on the constraints, before we set up the main tableau.
4719 * It is then no good to look for symmetries on possibly redundant constraints.
4721 struct isl_map *isl_tab_basic_map_partial_lexopt(
4722 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4723 struct isl_set **empty, int max)
4725 if (empty)
4726 *empty = NULL;
4727 if (!bmap || !dom)
4728 goto error;
4730 isl_assert(bmap->ctx,
4731 isl_basic_map_compatible_domain(bmap, dom), goto error);
4733 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4734 return basic_map_partial_lexopt(bmap, dom, empty, max);
4736 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4737 bmap = isl_basic_map_detect_equalities(bmap);
4738 bmap = isl_basic_map_remove_redundancies(bmap);
4740 return basic_map_partial_lexopt(bmap, dom, empty, max);
4741 error:
4742 isl_basic_set_free(dom);
4743 isl_basic_map_free(bmap);
4744 return NULL;
4747 struct isl_sol_for {
4748 struct isl_sol sol;
4749 int (*fn)(__isl_take isl_basic_set *dom,
4750 __isl_take isl_aff_list *list, void *user);
4751 void *user;
4754 static void sol_for_free(struct isl_sol_for *sol_for)
4756 if (sol_for->sol.context)
4757 sol_for->sol.context->op->free(sol_for->sol.context);
4758 free(sol_for);
4761 static void sol_for_free_wrap(struct isl_sol *sol)
4763 sol_for_free((struct isl_sol_for *)sol);
4766 /* Add the solution identified by the tableau and the context tableau.
4768 * See documentation of sol_add for more details.
4770 * Instead of constructing a basic map, this function calls a user
4771 * defined function with the current context as a basic set and
4772 * a list of affine expressions representing the relation between
4773 * the input and output. The space over which the affine expressions
4774 * are defined is the same as that of the domain. The number of
4775 * affine expressions in the list is equal to the number of output variables.
4777 static void sol_for_add(struct isl_sol_for *sol,
4778 struct isl_basic_set *dom, struct isl_mat *M)
4780 int i;
4781 isl_ctx *ctx;
4782 isl_local_space *ls;
4783 isl_aff *aff;
4784 isl_aff_list *list;
4786 if (sol->sol.error || !dom || !M)
4787 goto error;
4789 ctx = isl_basic_set_get_ctx(dom);
4790 ls = isl_basic_set_get_local_space(dom);
4791 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4792 for (i = 1; i < M->n_row; ++i) {
4793 aff = isl_aff_alloc(isl_local_space_copy(ls));
4794 if (aff) {
4795 isl_int_set(aff->v->el[0], M->row[0][0]);
4796 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4798 aff = isl_aff_normalize(aff);
4799 list = isl_aff_list_add(list, aff);
4801 isl_local_space_free(ls);
4803 dom = isl_basic_set_finalize(dom);
4805 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4806 goto error;
4808 isl_basic_set_free(dom);
4809 isl_mat_free(M);
4810 return;
4811 error:
4812 isl_basic_set_free(dom);
4813 isl_mat_free(M);
4814 sol->sol.error = 1;
4817 static void sol_for_add_wrap(struct isl_sol *sol,
4818 struct isl_basic_set *dom, struct isl_mat *M)
4820 sol_for_add((struct isl_sol_for *)sol, dom, M);
4823 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4824 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4825 void *user),
4826 void *user)
4828 struct isl_sol_for *sol_for = NULL;
4829 isl_space *dom_dim;
4830 struct isl_basic_set *dom = NULL;
4832 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4833 if (!sol_for)
4834 goto error;
4836 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4837 dom = isl_basic_set_universe(dom_dim);
4839 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4840 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4841 sol_for->sol.dec_level.sol = &sol_for->sol;
4842 sol_for->fn = fn;
4843 sol_for->user = user;
4844 sol_for->sol.max = max;
4845 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4846 sol_for->sol.add = &sol_for_add_wrap;
4847 sol_for->sol.add_empty = NULL;
4848 sol_for->sol.free = &sol_for_free_wrap;
4850 sol_for->sol.context = isl_context_alloc(dom);
4851 if (!sol_for->sol.context)
4852 goto error;
4854 isl_basic_set_free(dom);
4855 return sol_for;
4856 error:
4857 isl_basic_set_free(dom);
4858 sol_for_free(sol_for);
4859 return NULL;
4862 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4863 struct isl_tab *tab)
4865 find_solutions_main(&sol_for->sol, tab);
4868 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4869 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4870 void *user),
4871 void *user)
4873 struct isl_sol_for *sol_for = NULL;
4875 bmap = isl_basic_map_copy(bmap);
4876 bmap = isl_basic_map_detect_equalities(bmap);
4877 if (!bmap)
4878 return -1;
4880 sol_for = sol_for_init(bmap, max, fn, user);
4881 if (!sol_for)
4882 goto error;
4884 if (isl_basic_map_plain_is_empty(bmap))
4885 /* nothing */;
4886 else {
4887 struct isl_tab *tab;
4888 struct isl_context *context = sol_for->sol.context;
4889 tab = tab_for_lexmin(bmap,
4890 context->op->peek_basic_set(context), 1, max);
4891 tab = context->op->detect_nonnegative_parameters(context, tab);
4892 sol_for_find_solutions(sol_for, tab);
4893 if (sol_for->sol.error)
4894 goto error;
4897 sol_free(&sol_for->sol);
4898 isl_basic_map_free(bmap);
4899 return 0;
4900 error:
4901 sol_free(&sol_for->sol);
4902 isl_basic_map_free(bmap);
4903 return -1;
4906 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4907 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4908 void *user),
4909 void *user)
4911 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4914 /* Check if the given sequence of len variables starting at pos
4915 * represents a trivial (i.e., zero) solution.
4916 * The variables are assumed to be non-negative and to come in pairs,
4917 * with each pair representing a variable of unrestricted sign.
4918 * The solution is trivial if each such pair in the sequence consists
4919 * of two identical values, meaning that the variable being represented
4920 * has value zero.
4922 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4924 int i;
4926 if (len == 0)
4927 return 0;
4929 for (i = 0; i < len; i += 2) {
4930 int neg_row;
4931 int pos_row;
4933 neg_row = tab->var[pos + i].is_row ?
4934 tab->var[pos + i].index : -1;
4935 pos_row = tab->var[pos + i + 1].is_row ?
4936 tab->var[pos + i + 1].index : -1;
4938 if ((neg_row < 0 ||
4939 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4940 (pos_row < 0 ||
4941 isl_int_is_zero(tab->mat->row[pos_row][1])))
4942 continue;
4944 if (neg_row < 0 || pos_row < 0)
4945 return 0;
4946 if (isl_int_ne(tab->mat->row[neg_row][1],
4947 tab->mat->row[pos_row][1]))
4948 return 0;
4951 return 1;
4954 /* Return the index of the first trivial region or -1 if all regions
4955 * are non-trivial.
4957 static int first_trivial_region(struct isl_tab *tab,
4958 int n_region, struct isl_region *region)
4960 int i;
4962 for (i = 0; i < n_region; ++i) {
4963 if (region_is_trivial(tab, region[i].pos, region[i].len))
4964 return i;
4967 return -1;
4970 /* Check if the solution is optimal, i.e., whether the first
4971 * n_op entries are zero.
4973 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4975 int i;
4977 for (i = 0; i < n_op; ++i)
4978 if (!isl_int_is_zero(sol->el[1 + i]))
4979 return 0;
4980 return 1;
4983 /* Add constraints to "tab" that ensure that any solution is significantly
4984 * better that that represented by "sol". That is, find the first
4985 * relevant (within first n_op) non-zero coefficient and force it (along
4986 * with all previous coefficients) to be zero.
4987 * If the solution is already optimal (all relevant coefficients are zero),
4988 * then just mark the table as empty.
4990 static int force_better_solution(struct isl_tab *tab,
4991 __isl_keep isl_vec *sol, int n_op)
4993 int i;
4994 isl_ctx *ctx;
4995 isl_vec *v = NULL;
4997 if (!sol)
4998 return -1;
5000 for (i = 0; i < n_op; ++i)
5001 if (!isl_int_is_zero(sol->el[1 + i]))
5002 break;
5004 if (i == n_op) {
5005 if (isl_tab_mark_empty(tab) < 0)
5006 return -1;
5007 return 0;
5010 ctx = isl_vec_get_ctx(sol);
5011 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5012 if (!v)
5013 return -1;
5015 for (; i >= 0; --i) {
5016 v = isl_vec_clr(v);
5017 isl_int_set_si(v->el[1 + i], -1);
5018 if (add_lexmin_eq(tab, v->el) < 0)
5019 goto error;
5022 isl_vec_free(v);
5023 return 0;
5024 error:
5025 isl_vec_free(v);
5026 return -1;
5029 struct isl_trivial {
5030 int update;
5031 int region;
5032 int side;
5033 struct isl_tab_undo *snap;
5036 /* Return the lexicographically smallest non-trivial solution of the
5037 * given ILP problem.
5039 * All variables are assumed to be non-negative.
5041 * n_op is the number of initial coordinates to optimize.
5042 * That is, once a solution has been found, we will only continue looking
5043 * for solution that result in significantly better values for those
5044 * initial coordinates. That is, we only continue looking for solutions
5045 * that increase the number of initial zeros in this sequence.
5047 * A solution is non-trivial, if it is non-trivial on each of the
5048 * specified regions. Each region represents a sequence of pairs
5049 * of variables. A solution is non-trivial on such a region if
5050 * at least one of these pairs consists of different values, i.e.,
5051 * such that the non-negative variable represented by the pair is non-zero.
5053 * Whenever a conflict is encountered, all constraints involved are
5054 * reported to the caller through a call to "conflict".
5056 * We perform a simple branch-and-bound backtracking search.
5057 * Each level in the search represents initially trivial region that is forced
5058 * to be non-trivial.
5059 * At each level we consider n cases, where n is the length of the region.
5060 * In terms of the n/2 variables of unrestricted signs being encoded by
5061 * the region, we consider the cases
5062 * x_0 >= 1
5063 * x_0 <= -1
5064 * x_0 = 0 and x_1 >= 1
5065 * x_0 = 0 and x_1 <= -1
5066 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5067 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5068 * ...
5069 * The cases are considered in this order, assuming that each pair
5070 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5071 * That is, x_0 >= 1 is enforced by adding the constraint
5072 * x_0_b - x_0_a >= 1
5074 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5075 __isl_take isl_basic_set *bset, int n_op, int n_region,
5076 struct isl_region *region,
5077 int (*conflict)(int con, void *user), void *user)
5079 int i, j;
5080 int r;
5081 isl_ctx *ctx;
5082 isl_vec *v = NULL;
5083 isl_vec *sol = NULL;
5084 struct isl_tab *tab;
5085 struct isl_trivial *triv = NULL;
5086 int level, init;
5088 if (!bset)
5089 return NULL;
5091 ctx = isl_basic_set_get_ctx(bset);
5092 sol = isl_vec_alloc(ctx, 0);
5094 tab = tab_for_lexmin(bset, NULL, 0, 0);
5095 if (!tab)
5096 goto error;
5097 tab->conflict = conflict;
5098 tab->conflict_user = user;
5100 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5101 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5102 if (!v || (n_region && !triv))
5103 goto error;
5105 level = 0;
5106 init = 1;
5108 while (level >= 0) {
5109 int side, base;
5111 if (init) {
5112 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5113 if (!tab)
5114 goto error;
5115 if (tab->empty)
5116 goto backtrack;
5117 r = first_trivial_region(tab, n_region, region);
5118 if (r < 0) {
5119 for (i = 0; i < level; ++i)
5120 triv[i].update = 1;
5121 isl_vec_free(sol);
5122 sol = isl_tab_get_sample_value(tab);
5123 if (!sol)
5124 goto error;
5125 if (is_optimal(sol, n_op))
5126 break;
5127 goto backtrack;
5129 if (level >= n_region)
5130 isl_die(ctx, isl_error_internal,
5131 "nesting level too deep", goto error);
5132 if (isl_tab_extend_cons(tab,
5133 2 * region[r].len + 2 * n_op) < 0)
5134 goto error;
5135 triv[level].region = r;
5136 triv[level].side = 0;
5139 r = triv[level].region;
5140 side = triv[level].side;
5141 base = 2 * (side/2);
5143 if (side >= region[r].len) {
5144 backtrack:
5145 level--;
5146 init = 0;
5147 if (level >= 0)
5148 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5149 goto error;
5150 continue;
5153 if (triv[level].update) {
5154 if (force_better_solution(tab, sol, n_op) < 0)
5155 goto error;
5156 triv[level].update = 0;
5159 if (side == base && base >= 2) {
5160 for (j = base - 2; j < base; ++j) {
5161 v = isl_vec_clr(v);
5162 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5163 if (add_lexmin_eq(tab, v->el) < 0)
5164 goto error;
5168 triv[level].snap = isl_tab_snap(tab);
5169 if (isl_tab_push_basis(tab) < 0)
5170 goto error;
5172 v = isl_vec_clr(v);
5173 isl_int_set_si(v->el[0], -1);
5174 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5175 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5176 tab = add_lexmin_ineq(tab, v->el);
5178 triv[level].side++;
5179 level++;
5180 init = 1;
5183 free(triv);
5184 isl_vec_free(v);
5185 isl_tab_free(tab);
5186 isl_basic_set_free(bset);
5188 return sol;
5189 error:
5190 free(triv);
5191 isl_vec_free(v);
5192 isl_tab_free(tab);
5193 isl_basic_set_free(bset);
5194 isl_vec_free(sol);
5195 return NULL;
5198 /* Return the lexicographically smallest rational point in "bset",
5199 * assuming that all variables are non-negative.
5200 * If "bset" is empty, then return a zero-length vector.
5202 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5203 __isl_take isl_basic_set *bset)
5205 struct isl_tab *tab;
5206 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
5207 isl_vec *sol;
5209 if (!bset)
5210 return NULL;
5212 tab = tab_for_lexmin(bset, NULL, 0, 0);
5213 if (!tab)
5214 goto error;
5215 if (tab->empty)
5216 sol = isl_vec_alloc(ctx, 0);
5217 else
5218 sol = isl_tab_get_sample_value(tab);
5219 isl_tab_free(tab);
5220 isl_basic_set_free(bset);
5221 return sol;
5222 error:
5223 isl_tab_free(tab);
5224 isl_basic_set_free(bset);
5225 return NULL;
5228 struct isl_sol_pma {
5229 struct isl_sol sol;
5230 isl_pw_multi_aff *pma;
5231 isl_set *empty;
5234 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5236 if (!sol_pma)
5237 return;
5238 if (sol_pma->sol.context)
5239 sol_pma->sol.context->op->free(sol_pma->sol.context);
5240 isl_pw_multi_aff_free(sol_pma->pma);
5241 isl_set_free(sol_pma->empty);
5242 free(sol_pma);
5245 /* This function is called for parts of the context where there is
5246 * no solution, with "bset" corresponding to the context tableau.
5247 * Simply add the basic set to the set "empty".
5249 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5250 __isl_take isl_basic_set *bset)
5252 if (!bset)
5253 goto error;
5254 isl_assert(bset->ctx, sol->empty, goto error);
5256 sol->empty = isl_set_grow(sol->empty, 1);
5257 bset = isl_basic_set_simplify(bset);
5258 bset = isl_basic_set_finalize(bset);
5259 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5260 if (!sol->empty)
5261 sol->sol.error = 1;
5262 return;
5263 error:
5264 isl_basic_set_free(bset);
5265 sol->sol.error = 1;
5268 /* Given a basic map "dom" that represents the context and an affine
5269 * matrix "M" that maps the dimensions of the context to the
5270 * output variables, construct an isl_pw_multi_aff with a single
5271 * cell corresponding to "dom" and affine expressions copied from "M".
5273 static void sol_pma_add(struct isl_sol_pma *sol,
5274 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5276 int i;
5277 isl_local_space *ls;
5278 isl_aff *aff;
5279 isl_multi_aff *maff;
5280 isl_pw_multi_aff *pma;
5282 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5283 ls = isl_basic_set_get_local_space(dom);
5284 for (i = 1; i < M->n_row; ++i) {
5285 aff = isl_aff_alloc(isl_local_space_copy(ls));
5286 if (aff) {
5287 isl_int_set(aff->v->el[0], M->row[0][0]);
5288 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5290 aff = isl_aff_normalize(aff);
5291 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5293 isl_local_space_free(ls);
5294 isl_mat_free(M);
5295 dom = isl_basic_set_simplify(dom);
5296 dom = isl_basic_set_finalize(dom);
5297 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5298 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5299 if (!sol->pma)
5300 sol->sol.error = 1;
5303 static void sol_pma_free_wrap(struct isl_sol *sol)
5305 sol_pma_free((struct isl_sol_pma *)sol);
5308 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5309 __isl_take isl_basic_set *bset)
5311 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5314 static void sol_pma_add_wrap(struct isl_sol *sol,
5315 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5317 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5320 /* Construct an isl_sol_pma structure for accumulating the solution.
5321 * If track_empty is set, then we also keep track of the parts
5322 * of the context where there is no solution.
5323 * If max is set, then we are solving a maximization, rather than
5324 * a minimization problem, which means that the variables in the
5325 * tableau have value "M - x" rather than "M + x".
5327 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5328 __isl_take isl_basic_set *dom, int track_empty, int max)
5330 struct isl_sol_pma *sol_pma = NULL;
5332 if (!bmap)
5333 goto error;
5335 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5336 if (!sol_pma)
5337 goto error;
5339 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5340 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5341 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5342 sol_pma->sol.max = max;
5343 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5344 sol_pma->sol.add = &sol_pma_add_wrap;
5345 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5346 sol_pma->sol.free = &sol_pma_free_wrap;
5347 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5348 if (!sol_pma->pma)
5349 goto error;
5351 sol_pma->sol.context = isl_context_alloc(dom);
5352 if (!sol_pma->sol.context)
5353 goto error;
5355 if (track_empty) {
5356 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5357 1, ISL_SET_DISJOINT);
5358 if (!sol_pma->empty)
5359 goto error;
5362 isl_basic_set_free(dom);
5363 return &sol_pma->sol;
5364 error:
5365 isl_basic_set_free(dom);
5366 sol_pma_free(sol_pma);
5367 return NULL;
5370 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5371 * some obvious symmetries.
5373 * We call basic_map_partial_lexopt_base and extract the results.
5375 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5376 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5377 __isl_give isl_set **empty, int max)
5379 isl_pw_multi_aff *result = NULL;
5380 struct isl_sol *sol;
5381 struct isl_sol_pma *sol_pma;
5383 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5384 &sol_pma_init);
5385 if (!sol)
5386 return NULL;
5387 sol_pma = (struct isl_sol_pma *) sol;
5389 result = isl_pw_multi_aff_copy(sol_pma->pma);
5390 if (empty)
5391 *empty = isl_set_copy(sol_pma->empty);
5392 sol_free(&sol_pma->sol);
5393 return result;
5396 /* Given that the last input variable of "maff" represents the minimum
5397 * of some bounds, check whether we need to plug in the expression
5398 * of the minimum.
5400 * In particular, check if the last input variable appears in any
5401 * of the expressions in "maff".
5403 static int need_substitution(__isl_keep isl_multi_aff *maff)
5405 int i;
5406 unsigned pos;
5408 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5410 for (i = 0; i < maff->n; ++i)
5411 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5412 return 1;
5414 return 0;
5417 /* Given a set of upper bounds on the last "input" variable m,
5418 * construct a piecewise affine expression that selects
5419 * the minimal upper bound to m, i.e.,
5420 * divide the space into cells where one
5421 * of the upper bounds is smaller than all the others and select
5422 * this upper bound on that cell.
5424 * In particular, if there are n bounds b_i, then the result
5425 * consists of n cell, each one of the form
5427 * b_i <= b_j for j > i
5428 * b_i < b_j for j < i
5430 * The affine expression on this cell is
5432 * b_i
5434 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5435 __isl_take isl_mat *var)
5437 int i;
5438 isl_aff *aff = NULL;
5439 isl_basic_set *bset = NULL;
5440 isl_ctx *ctx;
5441 isl_pw_aff *paff = NULL;
5442 isl_space *pw_space;
5443 isl_local_space *ls = NULL;
5445 if (!space || !var)
5446 goto error;
5448 ctx = isl_space_get_ctx(space);
5449 ls = isl_local_space_from_space(isl_space_copy(space));
5450 pw_space = isl_space_copy(space);
5451 pw_space = isl_space_from_domain(pw_space);
5452 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5453 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5455 for (i = 0; i < var->n_row; ++i) {
5456 isl_pw_aff *paff_i;
5458 aff = isl_aff_alloc(isl_local_space_copy(ls));
5459 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5460 0, var->n_row - 1);
5461 if (!aff || !bset)
5462 goto error;
5463 isl_int_set_si(aff->v->el[0], 1);
5464 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5465 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5466 bset = select_minimum(bset, var, i);
5467 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5468 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5471 isl_local_space_free(ls);
5472 isl_space_free(space);
5473 isl_mat_free(var);
5474 return paff;
5475 error:
5476 isl_aff_free(aff);
5477 isl_basic_set_free(bset);
5478 isl_pw_aff_free(paff);
5479 isl_local_space_free(ls);
5480 isl_space_free(space);
5481 isl_mat_free(var);
5482 return NULL;
5485 /* Given a piecewise multi-affine expression of which the last input variable
5486 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5487 * This minimum expression is given in "min_expr_pa".
5488 * The set "min_expr" contains the same information, but in the form of a set.
5489 * The variable is subsequently projected out.
5491 * The implementation is similar to those of "split" and "split_domain".
5492 * If the variable appears in a given expression, then minimum expression
5493 * is plugged in. Otherwise, if the variable appears in the constraints
5494 * and a split is required, then the domain is split. Otherwise, no split
5495 * is performed.
5497 static __isl_give isl_pw_multi_aff *split_domain_pma(
5498 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5499 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5501 int n_in;
5502 int i;
5503 isl_space *space;
5504 isl_pw_multi_aff *res;
5506 if (!opt || !min_expr || !cst)
5507 goto error;
5509 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5510 space = isl_pw_multi_aff_get_space(opt);
5511 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5512 res = isl_pw_multi_aff_empty(space);
5514 for (i = 0; i < opt->n; ++i) {
5515 isl_pw_multi_aff *pma;
5517 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5518 isl_multi_aff_copy(opt->p[i].maff));
5519 if (need_substitution(opt->p[i].maff))
5520 pma = isl_pw_multi_aff_substitute(pma,
5521 isl_dim_in, n_in - 1, min_expr_pa);
5522 else if (need_split_set(opt->p[i].set, cst))
5523 pma = isl_pw_multi_aff_intersect_domain(pma,
5524 isl_set_copy(min_expr));
5525 pma = isl_pw_multi_aff_project_out(pma,
5526 isl_dim_in, n_in - 1, 1);
5528 res = isl_pw_multi_aff_add_disjoint(res, pma);
5531 isl_pw_multi_aff_free(opt);
5532 isl_pw_aff_free(min_expr_pa);
5533 isl_set_free(min_expr);
5534 isl_mat_free(cst);
5535 return res;
5536 error:
5537 isl_pw_multi_aff_free(opt);
5538 isl_pw_aff_free(min_expr_pa);
5539 isl_set_free(min_expr);
5540 isl_mat_free(cst);
5541 return NULL;
5544 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5545 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5546 __isl_give isl_set **empty, int max);
5548 /* This function is called from basic_map_partial_lexopt_symm.
5549 * The last variable of "bmap" and "dom" corresponds to the minimum
5550 * of the bounds in "cst". "map_space" is the space of the original
5551 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5552 * is the space of the original domain.
5554 * We recursively call basic_map_partial_lexopt and then plug in
5555 * the definition of the minimum in the result.
5557 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5558 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5559 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5560 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5562 isl_pw_multi_aff *opt;
5563 isl_pw_aff *min_expr_pa;
5564 isl_set *min_expr;
5565 union isl_lex_res res;
5567 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5568 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5569 isl_mat_copy(cst));
5571 opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5573 if (empty) {
5574 *empty = split(*empty,
5575 isl_set_copy(min_expr), isl_mat_copy(cst));
5576 *empty = isl_set_reset_space(*empty, set_space);
5579 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5580 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5582 res.pma = opt;
5583 return res;
5586 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5587 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5588 __isl_give isl_set **empty, int max, int first, int second)
5590 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5591 first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5594 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5595 * equalities and removing redundant constraints.
5597 * We first check if there are any parallel constraints (left).
5598 * If not, we are in the base case.
5599 * If there are parallel constraints, we replace them by a single
5600 * constraint in basic_map_partial_lexopt_symm_pma and then call
5601 * this function recursively to look for more parallel constraints.
5603 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5604 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5605 __isl_give isl_set **empty, int max)
5607 int par = 0;
5608 int first, second;
5610 if (!bmap)
5611 goto error;
5613 if (bmap->ctx->opt->pip_symmetry)
5614 par = parallel_constraints(bmap, &first, &second);
5615 if (par < 0)
5616 goto error;
5617 if (!par)
5618 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5620 return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5621 first, second);
5622 error:
5623 isl_basic_set_free(dom);
5624 isl_basic_map_free(bmap);
5625 return NULL;
5628 /* Compute the lexicographic minimum (or maximum if "max" is set)
5629 * of "bmap" over the domain "dom" and return the result as a piecewise
5630 * multi-affine expression.
5631 * If "empty" is not NULL, then *empty is assigned a set that
5632 * contains those parts of the domain where there is no solution.
5633 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5634 * then we compute the rational optimum. Otherwise, we compute
5635 * the integral optimum.
5637 * We perform some preprocessing. As the PILP solver does not
5638 * handle implicit equalities very well, we first make sure all
5639 * the equalities are explicitly available.
5641 * We also add context constraints to the basic map and remove
5642 * redundant constraints. This is only needed because of the
5643 * way we handle simple symmetries. In particular, we currently look
5644 * for symmetries on the constraints, before we set up the main tableau.
5645 * It is then no good to look for symmetries on possibly redundant constraints.
5647 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5648 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5649 __isl_give isl_set **empty, int max)
5651 if (empty)
5652 *empty = NULL;
5653 if (!bmap || !dom)
5654 goto error;
5656 isl_assert(bmap->ctx,
5657 isl_basic_map_compatible_domain(bmap, dom), goto error);
5659 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5660 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5662 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5663 bmap = isl_basic_map_detect_equalities(bmap);
5664 bmap = isl_basic_map_remove_redundancies(bmap);
5666 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5667 error:
5668 isl_basic_set_free(dom);
5669 isl_basic_map_free(bmap);
5670 return NULL;