isl_basic_map_plain_is_equal: improve error handling
[isl.git] / isl_tab_pip.c
blob441d97d7f7ce22e891f94c85d23765fee9c6c547
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_vec_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_options_private.h>
22 #include <isl_config.h>
25 * The implementation of parametric integer linear programming in this file
26 * was inspired by the paper "Parametric Integer Programming" and the
27 * report "Solving systems of affine (in)equalities" by Paul Feautrier
28 * (and others).
30 * The strategy used for obtaining a feasible solution is different
31 * from the one used in isl_tab.c. In particular, in isl_tab.c,
32 * upon finding a constraint that is not yet satisfied, we pivot
33 * in a row that increases the constant term of the row holding the
34 * constraint, making sure the sample solution remains feasible
35 * for all the constraints it already satisfied.
36 * Here, we always pivot in the row holding the constraint,
37 * choosing a column that induces the lexicographically smallest
38 * increment to the sample solution.
40 * By starting out from a sample value that is lexicographically
41 * smaller than any integer point in the problem space, the first
42 * feasible integer sample point we find will also be the lexicographically
43 * smallest. If all variables can be assumed to be non-negative,
44 * then the initial sample value may be chosen equal to zero.
45 * However, we will not make this assumption. Instead, we apply
46 * the "big parameter" trick. Any variable x is then not directly
47 * used in the tableau, but instead it is represented by another
48 * variable x' = M + x, where M is an arbitrarily large (positive)
49 * value. x' is therefore always non-negative, whatever the value of x.
50 * Taking as initial sample value x' = 0 corresponds to x = -M,
51 * which is always smaller than any possible value of x.
53 * The big parameter trick is used in the main tableau and
54 * also in the context tableau if isl_context_lex is used.
55 * In this case, each tableaus has its own big parameter.
56 * Before doing any real work, we check if all the parameters
57 * happen to be non-negative. If so, we drop the column corresponding
58 * to M from the initial context tableau.
59 * If isl_context_gbr is used, then the big parameter trick is only
60 * used in the main tableau.
63 struct isl_context;
64 struct isl_context_op {
65 /* detect nonnegative parameters in context and mark them in tab */
66 struct isl_tab *(*detect_nonnegative_parameters)(
67 struct isl_context *context, struct isl_tab *tab);
68 /* return temporary reference to basic set representation of context */
69 struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
70 /* return temporary reference to tableau representation of context */
71 struct isl_tab *(*peek_tab)(struct isl_context *context);
72 /* add equality; check is 1 if eq may not be valid;
73 * update is 1 if we may want to call ineq_sign on context later.
75 void (*add_eq)(struct isl_context *context, isl_int *eq,
76 int check, int update);
77 /* add inequality; check is 1 if ineq may not be valid;
78 * update is 1 if we may want to call ineq_sign on context later.
80 void (*add_ineq)(struct isl_context *context, isl_int *ineq,
81 int check, int update);
82 /* check sign of ineq based on previous information.
83 * strict is 1 if saturation should be treated as a positive sign.
85 enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
86 isl_int *ineq, int strict);
87 /* check if inequality maintains feasibility */
88 int (*test_ineq)(struct isl_context *context, isl_int *ineq);
89 /* return index of a div that corresponds to "div" */
90 int (*get_div)(struct isl_context *context, struct isl_tab *tab,
91 struct isl_vec *div);
92 /* add div "div" to context and return non-negativity */
93 int (*add_div)(struct isl_context *context, struct isl_vec *div);
94 int (*detect_equalities)(struct isl_context *context,
95 struct isl_tab *tab);
96 /* return row index of "best" split */
97 int (*best_split)(struct isl_context *context, struct isl_tab *tab);
98 /* check if context has already been determined to be empty */
99 int (*is_empty)(struct isl_context *context);
100 /* check if context is still usable */
101 int (*is_ok)(struct isl_context *context);
102 /* save a copy/snapshot of context */
103 void *(*save)(struct isl_context *context);
104 /* restore saved context */
105 void (*restore)(struct isl_context *context, void *);
106 /* discard saved context */
107 void (*discard)(void *);
108 /* invalidate context */
109 void (*invalidate)(struct isl_context *context);
110 /* free context */
111 void (*free)(struct isl_context *context);
114 struct isl_context {
115 struct isl_context_op *op;
118 struct isl_context_lex {
119 struct isl_context context;
120 struct isl_tab *tab;
123 /* A stack (linked list) of solutions of subtrees of the search space.
125 * "M" describes the solution in terms of the dimensions of "dom".
126 * The number of columns of "M" is one more than the total number
127 * of dimensions of "dom".
129 * If "M" is NULL, then there is no solution on "dom".
131 struct isl_partial_sol {
132 int level;
133 struct isl_basic_set *dom;
134 struct isl_mat *M;
136 struct isl_partial_sol *next;
139 struct isl_sol;
140 struct isl_sol_callback {
141 struct isl_tab_callback callback;
142 struct isl_sol *sol;
145 /* isl_sol is an interface for constructing a solution to
146 * a parametric integer linear programming problem.
147 * Every time the algorithm reaches a state where a solution
148 * can be read off from the tableau (including cases where the tableau
149 * is empty), the function "add" is called on the isl_sol passed
150 * to find_solutions_main.
152 * The context tableau is owned by isl_sol and is updated incrementally.
154 * There are currently two implementations of this interface,
155 * isl_sol_map, which simply collects the solutions in an isl_map
156 * and (optionally) the parts of the context where there is no solution
157 * in an isl_set, and
158 * isl_sol_for, which calls a user-defined function for each part of
159 * the solution.
161 struct isl_sol {
162 int error;
163 int rational;
164 int level;
165 int max;
166 int n_out;
167 struct isl_context *context;
168 struct isl_partial_sol *partial;
169 void (*add)(struct isl_sol *sol,
170 struct isl_basic_set *dom, struct isl_mat *M);
171 void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
172 void (*free)(struct isl_sol *sol);
173 struct isl_sol_callback dec_level;
176 static void sol_free(struct isl_sol *sol)
178 struct isl_partial_sol *partial, *next;
179 if (!sol)
180 return;
181 for (partial = sol->partial; partial; partial = next) {
182 next = partial->next;
183 isl_basic_set_free(partial->dom);
184 isl_mat_free(partial->M);
185 free(partial);
187 sol->free(sol);
190 /* Push a partial solution represented by a domain and mapping M
191 * onto the stack of partial solutions.
193 static void sol_push_sol(struct isl_sol *sol,
194 struct isl_basic_set *dom, struct isl_mat *M)
196 struct isl_partial_sol *partial;
198 if (sol->error || !dom)
199 goto error;
201 partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
202 if (!partial)
203 goto error;
205 partial->level = sol->level;
206 partial->dom = dom;
207 partial->M = M;
208 partial->next = sol->partial;
210 sol->partial = partial;
212 return;
213 error:
214 isl_basic_set_free(dom);
215 isl_mat_free(M);
216 sol->error = 1;
219 /* Pop one partial solution from the partial solution stack and
220 * pass it on to sol->add or sol->add_empty.
222 static void sol_pop_one(struct isl_sol *sol)
224 struct isl_partial_sol *partial;
226 partial = sol->partial;
227 sol->partial = partial->next;
229 if (partial->M)
230 sol->add(sol, partial->dom, partial->M);
231 else
232 sol->add_empty(sol, partial->dom);
233 free(partial);
236 /* Return a fresh copy of the domain represented by the context tableau.
238 static struct isl_basic_set *sol_domain(struct isl_sol *sol)
240 struct isl_basic_set *bset;
242 if (sol->error)
243 return NULL;
245 bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
246 bset = isl_basic_set_update_from_tab(bset,
247 sol->context->op->peek_tab(sol->context));
249 return bset;
252 /* Check whether two partial solutions have the same mapping, where n_div
253 * is the number of divs that the two partial solutions have in common.
255 static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
256 unsigned n_div)
258 int i;
259 unsigned dim;
261 if (!s1->M != !s2->M)
262 return 0;
263 if (!s1->M)
264 return 1;
266 dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
268 for (i = 0; i < s1->M->n_row; ++i) {
269 if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
270 s1->M->n_col-1-dim-n_div) != -1)
271 return 0;
272 if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
273 s2->M->n_col-1-dim-n_div) != -1)
274 return 0;
275 if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
276 return 0;
278 return 1;
281 /* Pop all solutions from the partial solution stack that were pushed onto
282 * the stack at levels that are deeper than the current level.
283 * If the two topmost elements on the stack have the same level
284 * and represent the same solution, then their domains are combined.
285 * This combined domain is the same as the current context domain
286 * as sol_pop is called each time we move back to a higher level.
288 static void sol_pop(struct isl_sol *sol)
290 struct isl_partial_sol *partial;
291 unsigned n_div;
293 if (sol->error)
294 return;
296 if (sol->level == 0) {
297 for (partial = sol->partial; partial; partial = sol->partial)
298 sol_pop_one(sol);
299 return;
302 partial = sol->partial;
303 if (!partial)
304 return;
306 if (partial->level <= sol->level)
307 return;
309 if (partial->next && partial->next->level == partial->level) {
310 n_div = isl_basic_set_dim(
311 sol->context->op->peek_basic_set(sol->context),
312 isl_dim_div);
314 if (!same_solution(partial, partial->next, n_div)) {
315 sol_pop_one(sol);
316 sol_pop_one(sol);
317 } else {
318 struct isl_basic_set *bset;
319 isl_mat *M;
320 unsigned n;
322 n = isl_basic_set_dim(partial->next->dom, isl_dim_div);
323 n -= n_div;
324 bset = sol_domain(sol);
325 isl_basic_set_free(partial->next->dom);
326 partial->next->dom = bset;
327 M = partial->next->M;
328 if (M) {
329 M = isl_mat_drop_cols(M, M->n_col - n, n);
330 partial->next->M = M;
331 if (!M)
332 goto error;
334 partial->next->level = sol->level;
336 if (!bset)
337 goto error;
339 sol->partial = partial->next;
340 isl_basic_set_free(partial->dom);
341 isl_mat_free(partial->M);
342 free(partial);
344 } else
345 sol_pop_one(sol);
347 if (0)
348 error: sol->error = 1;
351 static void sol_dec_level(struct isl_sol *sol)
353 if (sol->error)
354 return;
356 sol->level--;
358 sol_pop(sol);
361 static int sol_dec_level_wrap(struct isl_tab_callback *cb)
363 struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
365 sol_dec_level(callback->sol);
367 return callback->sol->error ? -1 : 0;
370 /* Move down to next level and push callback onto context tableau
371 * to decrease the level again when it gets rolled back across
372 * the current state. That is, dec_level will be called with
373 * the context tableau in the same state as it is when inc_level
374 * is called.
376 static void sol_inc_level(struct isl_sol *sol)
378 struct isl_tab *tab;
380 if (sol->error)
381 return;
383 sol->level++;
384 tab = sol->context->op->peek_tab(sol->context);
385 if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
386 sol->error = 1;
389 static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
391 int i;
393 if (isl_int_is_one(m))
394 return;
396 for (i = 0; i < n_row; ++i)
397 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
400 /* Add the solution identified by the tableau and the context tableau.
402 * The layout of the variables is as follows.
403 * tab->n_var is equal to the total number of variables in the input
404 * map (including divs that were copied from the context)
405 * + the number of extra divs constructed
406 * Of these, the first tab->n_param and the last tab->n_div variables
407 * correspond to the variables in the context, i.e.,
408 * tab->n_param + tab->n_div = context_tab->n_var
409 * tab->n_param is equal to the number of parameters and input
410 * dimensions in the input map
411 * tab->n_div is equal to the number of divs in the context
413 * If there is no solution, then call add_empty with a basic set
414 * that corresponds to the context tableau. (If add_empty is NULL,
415 * then do nothing).
417 * If there is a solution, then first construct a matrix that maps
418 * all dimensions of the context to the output variables, i.e.,
419 * the output dimensions in the input map.
420 * The divs in the input map (if any) that do not correspond to any
421 * div in the context do not appear in the solution.
422 * The algorithm will make sure that they have an integer value,
423 * but these values themselves are of no interest.
424 * We have to be careful not to drop or rearrange any divs in the
425 * context because that would change the meaning of the matrix.
427 * To extract the value of the output variables, it should be noted
428 * that we always use a big parameter M in the main tableau and so
429 * the variable stored in this tableau is not an output variable x itself, but
430 * x' = M + x (in case of minimization)
431 * or
432 * x' = M - x (in case of maximization)
433 * If x' appears in a column, then its optimal value is zero,
434 * which means that the optimal value of x is an unbounded number
435 * (-M for minimization and M for maximization).
436 * We currently assume that the output dimensions in the original map
437 * are bounded, so this cannot occur.
438 * Similarly, when x' appears in a row, then the coefficient of M in that
439 * row is necessarily 1.
440 * If the row in the tableau represents
441 * d x' = c + d M + e(y)
442 * then, in case of minimization, the corresponding row in the matrix
443 * will be
444 * a c + a e(y)
445 * with a d = m, the (updated) common denominator of the matrix.
446 * In case of maximization, the row will be
447 * -a c - a e(y)
449 static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
451 struct isl_basic_set *bset = NULL;
452 struct isl_mat *mat = NULL;
453 unsigned off;
454 int row;
455 isl_int m;
457 if (sol->error || !tab)
458 goto error;
460 if (tab->empty && !sol->add_empty)
461 return;
462 if (sol->context->op->is_empty(sol->context))
463 return;
465 bset = sol_domain(sol);
467 if (tab->empty) {
468 sol_push_sol(sol, bset, NULL);
469 return;
472 off = 2 + tab->M;
474 mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
475 1 + tab->n_param + tab->n_div);
476 if (!mat)
477 goto error;
479 isl_int_init(m);
481 isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
482 isl_int_set_si(mat->row[0][0], 1);
483 for (row = 0; row < sol->n_out; ++row) {
484 int i = tab->n_param + row;
485 int r, j;
487 isl_seq_clr(mat->row[1 + row], mat->n_col);
488 if (!tab->var[i].is_row) {
489 if (tab->M)
490 isl_die(mat->ctx, isl_error_invalid,
491 "unbounded optimum", goto error2);
492 continue;
495 r = tab->var[i].index;
496 if (tab->M &&
497 isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
498 isl_die(mat->ctx, isl_error_invalid,
499 "unbounded optimum", goto error2);
500 isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
501 isl_int_divexact(m, tab->mat->row[r][0], m);
502 scale_rows(mat, m, 1 + row);
503 isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
504 isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
505 for (j = 0; j < tab->n_param; ++j) {
506 int col;
507 if (tab->var[j].is_row)
508 continue;
509 col = tab->var[j].index;
510 isl_int_mul(mat->row[1 + row][1 + j], m,
511 tab->mat->row[r][off + col]);
513 for (j = 0; j < tab->n_div; ++j) {
514 int col;
515 if (tab->var[tab->n_var - tab->n_div+j].is_row)
516 continue;
517 col = tab->var[tab->n_var - tab->n_div+j].index;
518 isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
519 tab->mat->row[r][off + col]);
521 if (sol->max)
522 isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
523 mat->n_col);
526 isl_int_clear(m);
528 sol_push_sol(sol, bset, mat);
529 return;
530 error2:
531 isl_int_clear(m);
532 error:
533 isl_basic_set_free(bset);
534 isl_mat_free(mat);
535 sol->error = 1;
538 struct isl_sol_map {
539 struct isl_sol sol;
540 struct isl_map *map;
541 struct isl_set *empty;
544 static void sol_map_free(struct isl_sol_map *sol_map)
546 if (!sol_map)
547 return;
548 if (sol_map->sol.context)
549 sol_map->sol.context->op->free(sol_map->sol.context);
550 isl_map_free(sol_map->map);
551 isl_set_free(sol_map->empty);
552 free(sol_map);
555 static void sol_map_free_wrap(struct isl_sol *sol)
557 sol_map_free((struct isl_sol_map *)sol);
560 /* This function is called for parts of the context where there is
561 * no solution, with "bset" corresponding to the context tableau.
562 * Simply add the basic set to the set "empty".
564 static void sol_map_add_empty(struct isl_sol_map *sol,
565 struct isl_basic_set *bset)
567 if (!bset || !sol->empty)
568 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 * best is the best row so far (-1 when we have not found any row yet).
2155 * best_r is the number of other rows made redundant by row best.
2156 * When best is still -1, bset_r is meaningless, but it is initialized
2157 * to some arbitrary value (0) anyway. Without this redundant initialization
2158 * valgrind may warn about uninitialized memory accesses when isl
2159 * is compiled with some versions of gcc.
2161 static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
2163 struct isl_tab_undo *snap;
2164 int split;
2165 int row;
2166 int best = -1;
2167 int best_r = 0;
2169 if (isl_tab_extend_cons(context_tab, 2) < 0)
2170 return -1;
2172 snap = isl_tab_snap(context_tab);
2174 for (split = tab->n_redundant; split < tab->n_row; ++split) {
2175 struct isl_tab_undo *snap2;
2176 struct isl_vec *ineq = NULL;
2177 int r = 0;
2178 int ok;
2180 if (!isl_tab_var_from_row(tab, split)->is_nonneg)
2181 continue;
2182 if (tab->row_sign[split] != isl_tab_row_any)
2183 continue;
2185 ineq = get_row_parameter_ineq(tab, split);
2186 if (!ineq)
2187 return -1;
2188 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2189 isl_vec_free(ineq);
2190 if (!ok)
2191 return -1;
2193 snap2 = isl_tab_snap(context_tab);
2195 for (row = tab->n_redundant; row < tab->n_row; ++row) {
2196 struct isl_tab_var *var;
2198 if (row == split)
2199 continue;
2200 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
2201 continue;
2202 if (tab->row_sign[row] != isl_tab_row_any)
2203 continue;
2205 ineq = get_row_parameter_ineq(tab, row);
2206 if (!ineq)
2207 return -1;
2208 ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
2209 isl_vec_free(ineq);
2210 if (!ok)
2211 return -1;
2212 var = &context_tab->con[context_tab->n_con - 1];
2213 if (!context_tab->empty &&
2214 !isl_tab_min_at_most_neg_one(context_tab, var))
2215 r++;
2216 if (isl_tab_rollback(context_tab, snap2) < 0)
2217 return -1;
2219 if (best == -1 || r > best_r) {
2220 best = split;
2221 best_r = r;
2223 if (isl_tab_rollback(context_tab, snap) < 0)
2224 return -1;
2227 return best;
2230 static struct isl_basic_set *context_lex_peek_basic_set(
2231 struct isl_context *context)
2233 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2234 if (!clex->tab)
2235 return NULL;
2236 return isl_tab_peek_bset(clex->tab);
2239 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
2241 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2242 return clex->tab;
2245 static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
2246 int check, int update)
2248 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2249 if (isl_tab_extend_cons(clex->tab, 2) < 0)
2250 goto error;
2251 if (add_lexmin_eq(clex->tab, eq) < 0)
2252 goto error;
2253 if (check) {
2254 int v = tab_has_valid_sample(clex->tab, eq, 1);
2255 if (v < 0)
2256 goto error;
2257 if (!v)
2258 clex->tab = check_integer_feasible(clex->tab);
2260 if (update)
2261 clex->tab = check_samples(clex->tab, eq, 1);
2262 return;
2263 error:
2264 isl_tab_free(clex->tab);
2265 clex->tab = NULL;
2268 static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
2269 int check, int update)
2271 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2272 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2273 goto error;
2274 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2275 if (check) {
2276 int v = tab_has_valid_sample(clex->tab, ineq, 0);
2277 if (v < 0)
2278 goto error;
2279 if (!v)
2280 clex->tab = check_integer_feasible(clex->tab);
2282 if (update)
2283 clex->tab = check_samples(clex->tab, ineq, 0);
2284 return;
2285 error:
2286 isl_tab_free(clex->tab);
2287 clex->tab = NULL;
2290 static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
2292 struct isl_context *context = (struct isl_context *)user;
2293 context_lex_add_ineq(context, ineq, 0, 0);
2294 return context->op->is_ok(context) ? 0 : -1;
2297 /* Check which signs can be obtained by "ineq" on all the currently
2298 * active sample values. See row_sign for more information.
2300 static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
2301 int strict)
2303 int i;
2304 int sgn;
2305 isl_int tmp;
2306 enum isl_tab_row_sign res = isl_tab_row_unknown;
2308 isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
2309 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
2310 return isl_tab_row_unknown);
2312 isl_int_init(tmp);
2313 for (i = tab->n_outside; i < tab->n_sample; ++i) {
2314 isl_seq_inner_product(tab->samples->row[i], ineq,
2315 1 + tab->n_var, &tmp);
2316 sgn = isl_int_sgn(tmp);
2317 if (sgn > 0 || (sgn == 0 && strict)) {
2318 if (res == isl_tab_row_unknown)
2319 res = isl_tab_row_pos;
2320 if (res == isl_tab_row_neg)
2321 res = isl_tab_row_any;
2323 if (sgn < 0) {
2324 if (res == isl_tab_row_unknown)
2325 res = isl_tab_row_neg;
2326 if (res == isl_tab_row_pos)
2327 res = isl_tab_row_any;
2329 if (res == isl_tab_row_any)
2330 break;
2332 isl_int_clear(tmp);
2334 return res;
2337 static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
2338 isl_int *ineq, int strict)
2340 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2341 return tab_ineq_sign(clex->tab, ineq, strict);
2344 /* Check whether "ineq" can be added to the tableau without rendering
2345 * it infeasible.
2347 static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
2349 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2350 struct isl_tab_undo *snap;
2351 int feasible;
2353 if (!clex->tab)
2354 return -1;
2356 if (isl_tab_extend_cons(clex->tab, 1) < 0)
2357 return -1;
2359 snap = isl_tab_snap(clex->tab);
2360 if (isl_tab_push_basis(clex->tab) < 0)
2361 return -1;
2362 clex->tab = add_lexmin_ineq(clex->tab, ineq);
2363 clex->tab = check_integer_feasible(clex->tab);
2364 if (!clex->tab)
2365 return -1;
2366 feasible = !clex->tab->empty;
2367 if (isl_tab_rollback(clex->tab, snap) < 0)
2368 return -1;
2370 return feasible;
2373 static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
2374 struct isl_vec *div)
2376 return get_div(tab, context, div);
2379 /* Add a div specified by "div" to the context tableau and return
2380 * 1 if the div is obviously non-negative.
2381 * context_tab_add_div will always return 1, because all variables
2382 * in a isl_context_lex tableau are non-negative.
2383 * However, if we are using a big parameter in the context, then this only
2384 * reflects the non-negativity of the variable used to _encode_ the
2385 * div, i.e., div' = M + div, so we can't draw any conclusions.
2387 static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
2389 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2390 int nonneg;
2391 nonneg = context_tab_add_div(clex->tab, div,
2392 context_lex_add_ineq_wrap, context);
2393 if (nonneg < 0)
2394 return -1;
2395 if (clex->tab->M)
2396 return 0;
2397 return nonneg;
2400 static int context_lex_detect_equalities(struct isl_context *context,
2401 struct isl_tab *tab)
2403 return 0;
2406 static int context_lex_best_split(struct isl_context *context,
2407 struct isl_tab *tab)
2409 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2410 struct isl_tab_undo *snap;
2411 int r;
2413 snap = isl_tab_snap(clex->tab);
2414 if (isl_tab_push_basis(clex->tab) < 0)
2415 return -1;
2416 r = best_split(tab, clex->tab);
2418 if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
2419 return -1;
2421 return r;
2424 static int context_lex_is_empty(struct isl_context *context)
2426 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2427 if (!clex->tab)
2428 return -1;
2429 return clex->tab->empty;
2432 static void *context_lex_save(struct isl_context *context)
2434 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2435 struct isl_tab_undo *snap;
2437 snap = isl_tab_snap(clex->tab);
2438 if (isl_tab_push_basis(clex->tab) < 0)
2439 return NULL;
2440 if (isl_tab_save_samples(clex->tab) < 0)
2441 return NULL;
2443 return snap;
2446 static void context_lex_restore(struct isl_context *context, void *save)
2448 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2449 if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
2450 isl_tab_free(clex->tab);
2451 clex->tab = NULL;
2455 static void context_lex_discard(void *save)
2459 static int context_lex_is_ok(struct isl_context *context)
2461 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2462 return !!clex->tab;
2465 /* For each variable in the context tableau, check if the variable can
2466 * only attain non-negative values. If so, mark the parameter as non-negative
2467 * in the main tableau. This allows for a more direct identification of some
2468 * cases of violated constraints.
2470 static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
2471 struct isl_tab *context_tab)
2473 int i;
2474 struct isl_tab_undo *snap;
2475 struct isl_vec *ineq = NULL;
2476 struct isl_tab_var *var;
2477 int n;
2479 if (context_tab->n_var == 0)
2480 return tab;
2482 ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
2483 if (!ineq)
2484 goto error;
2486 if (isl_tab_extend_cons(context_tab, 1) < 0)
2487 goto error;
2489 snap = isl_tab_snap(context_tab);
2491 n = 0;
2492 isl_seq_clr(ineq->el, ineq->size);
2493 for (i = 0; i < context_tab->n_var; ++i) {
2494 isl_int_set_si(ineq->el[1 + i], 1);
2495 if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
2496 goto error;
2497 var = &context_tab->con[context_tab->n_con - 1];
2498 if (!context_tab->empty &&
2499 !isl_tab_min_at_most_neg_one(context_tab, var)) {
2500 int j = i;
2501 if (i >= tab->n_param)
2502 j = i - tab->n_param + tab->n_var - tab->n_div;
2503 tab->var[j].is_nonneg = 1;
2504 n++;
2506 isl_int_set_si(ineq->el[1 + i], 0);
2507 if (isl_tab_rollback(context_tab, snap) < 0)
2508 goto error;
2511 if (context_tab->M && n == context_tab->n_var) {
2512 context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
2513 context_tab->M = 0;
2516 isl_vec_free(ineq);
2517 return tab;
2518 error:
2519 isl_vec_free(ineq);
2520 isl_tab_free(tab);
2521 return NULL;
2524 static struct isl_tab *context_lex_detect_nonnegative_parameters(
2525 struct isl_context *context, struct isl_tab *tab)
2527 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2528 struct isl_tab_undo *snap;
2530 if (!tab)
2531 return NULL;
2533 snap = isl_tab_snap(clex->tab);
2534 if (isl_tab_push_basis(clex->tab) < 0)
2535 goto error;
2537 tab = tab_detect_nonnegative_parameters(tab, clex->tab);
2539 if (isl_tab_rollback(clex->tab, snap) < 0)
2540 goto error;
2542 return tab;
2543 error:
2544 isl_tab_free(tab);
2545 return NULL;
2548 static void context_lex_invalidate(struct isl_context *context)
2550 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2551 isl_tab_free(clex->tab);
2552 clex->tab = NULL;
2555 static void context_lex_free(struct isl_context *context)
2557 struct isl_context_lex *clex = (struct isl_context_lex *)context;
2558 isl_tab_free(clex->tab);
2559 free(clex);
2562 struct isl_context_op isl_context_lex_op = {
2563 context_lex_detect_nonnegative_parameters,
2564 context_lex_peek_basic_set,
2565 context_lex_peek_tab,
2566 context_lex_add_eq,
2567 context_lex_add_ineq,
2568 context_lex_ineq_sign,
2569 context_lex_test_ineq,
2570 context_lex_get_div,
2571 context_lex_add_div,
2572 context_lex_detect_equalities,
2573 context_lex_best_split,
2574 context_lex_is_empty,
2575 context_lex_is_ok,
2576 context_lex_save,
2577 context_lex_restore,
2578 context_lex_discard,
2579 context_lex_invalidate,
2580 context_lex_free,
2583 static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
2585 struct isl_tab *tab;
2587 if (!bset)
2588 return NULL;
2589 tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
2590 if (!tab)
2591 goto error;
2592 if (isl_tab_track_bset(tab, bset) < 0)
2593 goto error;
2594 tab = isl_tab_init_samples(tab);
2595 return tab;
2596 error:
2597 isl_basic_set_free(bset);
2598 return NULL;
2601 static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
2603 struct isl_context_lex *clex;
2605 if (!dom)
2606 return NULL;
2608 clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
2609 if (!clex)
2610 return NULL;
2612 clex->context.op = &isl_context_lex_op;
2614 clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
2615 if (restore_lexmin(clex->tab) < 0)
2616 goto error;
2617 clex->tab = check_integer_feasible(clex->tab);
2618 if (!clex->tab)
2619 goto error;
2621 return &clex->context;
2622 error:
2623 clex->context.op->free(&clex->context);
2624 return NULL;
2627 /* Representation of the context when using generalized basis reduction.
2629 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2630 * context. Any rational point in "shifted" can therefore be rounded
2631 * up to an integer point in the context.
2632 * If the context is constrained by any equality, then "shifted" is not used
2633 * as it would be empty.
2635 struct isl_context_gbr {
2636 struct isl_context context;
2637 struct isl_tab *tab;
2638 struct isl_tab *shifted;
2639 struct isl_tab *cone;
2642 static struct isl_tab *context_gbr_detect_nonnegative_parameters(
2643 struct isl_context *context, struct isl_tab *tab)
2645 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2646 if (!tab)
2647 return NULL;
2648 return tab_detect_nonnegative_parameters(tab, cgbr->tab);
2651 static struct isl_basic_set *context_gbr_peek_basic_set(
2652 struct isl_context *context)
2654 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2655 if (!cgbr->tab)
2656 return NULL;
2657 return isl_tab_peek_bset(cgbr->tab);
2660 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
2662 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2663 return cgbr->tab;
2666 /* Initialize the "shifted" tableau of the context, which
2667 * contains the constraints of the original tableau shifted
2668 * by the sum of all negative coefficients. This ensures
2669 * that any rational point in the shifted tableau can
2670 * be rounded up to yield an integer point in the original tableau.
2672 static void gbr_init_shifted(struct isl_context_gbr *cgbr)
2674 int i, j;
2675 struct isl_vec *cst;
2676 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
2677 unsigned dim = isl_basic_set_total_dim(bset);
2679 cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
2680 if (!cst)
2681 return;
2683 for (i = 0; i < bset->n_ineq; ++i) {
2684 isl_int_set(cst->el[i], bset->ineq[i][0]);
2685 for (j = 0; j < dim; ++j) {
2686 if (!isl_int_is_neg(bset->ineq[i][1 + j]))
2687 continue;
2688 isl_int_add(bset->ineq[i][0], bset->ineq[i][0],
2689 bset->ineq[i][1 + j]);
2693 cgbr->shifted = isl_tab_from_basic_set(bset, 0);
2695 for (i = 0; i < bset->n_ineq; ++i)
2696 isl_int_set(bset->ineq[i][0], cst->el[i]);
2698 isl_vec_free(cst);
2701 /* Check if the shifted tableau is non-empty, and if so
2702 * use the sample point to construct an integer point
2703 * of the context tableau.
2705 static struct isl_vec *gbr_get_shifted_sample(struct isl_context_gbr *cgbr)
2707 struct isl_vec *sample;
2709 if (!cgbr->shifted)
2710 gbr_init_shifted(cgbr);
2711 if (!cgbr->shifted)
2712 return NULL;
2713 if (cgbr->shifted->empty)
2714 return isl_vec_alloc(cgbr->tab->mat->ctx, 0);
2716 sample = isl_tab_get_sample_value(cgbr->shifted);
2717 sample = isl_vec_ceil(sample);
2719 return sample;
2722 static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
2724 int i;
2726 if (!bset)
2727 return NULL;
2729 for (i = 0; i < bset->n_eq; ++i)
2730 isl_int_set_si(bset->eq[i][0], 0);
2732 for (i = 0; i < bset->n_ineq; ++i)
2733 isl_int_set_si(bset->ineq[i][0], 0);
2735 return bset;
2738 static int use_shifted(struct isl_context_gbr *cgbr)
2740 if (!cgbr->tab)
2741 return 0;
2742 return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
2745 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
2747 struct isl_basic_set *bset;
2748 struct isl_basic_set *cone;
2750 if (isl_tab_sample_is_integer(cgbr->tab))
2751 return isl_tab_get_sample_value(cgbr->tab);
2753 if (use_shifted(cgbr)) {
2754 struct isl_vec *sample;
2756 sample = gbr_get_shifted_sample(cgbr);
2757 if (!sample || sample->size > 0)
2758 return sample;
2760 isl_vec_free(sample);
2763 if (!cgbr->cone) {
2764 bset = isl_tab_peek_bset(cgbr->tab);
2765 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
2766 if (!cgbr->cone)
2767 return NULL;
2768 if (isl_tab_track_bset(cgbr->cone,
2769 isl_basic_set_copy(bset)) < 0)
2770 return NULL;
2772 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
2773 return NULL;
2775 if (cgbr->cone->n_dead == cgbr->cone->n_col) {
2776 struct isl_vec *sample;
2777 struct isl_tab_undo *snap;
2779 if (cgbr->tab->basis) {
2780 if (cgbr->tab->basis->n_col != 1 + cgbr->tab->n_var) {
2781 isl_mat_free(cgbr->tab->basis);
2782 cgbr->tab->basis = NULL;
2784 cgbr->tab->n_zero = 0;
2785 cgbr->tab->n_unbounded = 0;
2788 snap = isl_tab_snap(cgbr->tab);
2790 sample = isl_tab_sample(cgbr->tab);
2792 if (!sample || isl_tab_rollback(cgbr->tab, snap) < 0) {
2793 isl_vec_free(sample);
2794 return NULL;
2797 return sample;
2800 cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
2801 cone = drop_constant_terms(cone);
2802 cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
2803 cone = isl_basic_set_underlying_set(cone);
2804 cone = isl_basic_set_gauss(cone, NULL);
2806 bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
2807 bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
2808 bset = isl_basic_set_underlying_set(bset);
2809 bset = isl_basic_set_gauss(bset, NULL);
2811 return isl_basic_set_sample_with_cone(bset, cone);
2814 static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
2816 struct isl_vec *sample;
2818 if (!cgbr->tab)
2819 return;
2821 if (cgbr->tab->empty)
2822 return;
2824 sample = gbr_get_sample(cgbr);
2825 if (!sample)
2826 goto error;
2828 if (sample->size == 0) {
2829 isl_vec_free(sample);
2830 if (isl_tab_mark_empty(cgbr->tab) < 0)
2831 goto error;
2832 return;
2835 if (isl_tab_add_sample(cgbr->tab, sample) < 0)
2836 goto error;
2838 return;
2839 error:
2840 isl_tab_free(cgbr->tab);
2841 cgbr->tab = NULL;
2844 static struct isl_tab *add_gbr_eq(struct isl_tab *tab, isl_int *eq)
2846 if (!tab)
2847 return NULL;
2849 if (isl_tab_extend_cons(tab, 2) < 0)
2850 goto error;
2852 if (isl_tab_add_eq(tab, eq) < 0)
2853 goto error;
2855 return tab;
2856 error:
2857 isl_tab_free(tab);
2858 return NULL;
2861 /* Add the equality described by "eq" to the context.
2862 * If "check" is set, then we check if the context is empty after
2863 * adding the equality.
2864 * If "update" is set, then we check if the samples are still valid.
2866 * We do not explicitly add shifted copies of the equality to
2867 * cgbr->shifted since they would conflict with each other.
2868 * Instead, we directly mark cgbr->shifted empty.
2870 static void context_gbr_add_eq(struct isl_context *context, isl_int *eq,
2871 int check, int update)
2873 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2875 cgbr->tab = add_gbr_eq(cgbr->tab, eq);
2877 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2878 if (isl_tab_mark_empty(cgbr->shifted) < 0)
2879 goto error;
2882 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2883 if (isl_tab_extend_cons(cgbr->cone, 2) < 0)
2884 goto error;
2885 if (isl_tab_add_eq(cgbr->cone, eq) < 0)
2886 goto error;
2889 if (check) {
2890 int v = tab_has_valid_sample(cgbr->tab, eq, 1);
2891 if (v < 0)
2892 goto error;
2893 if (!v)
2894 check_gbr_integer_feasible(cgbr);
2896 if (update)
2897 cgbr->tab = check_samples(cgbr->tab, eq, 1);
2898 return;
2899 error:
2900 isl_tab_free(cgbr->tab);
2901 cgbr->tab = NULL;
2904 static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
2906 if (!cgbr->tab)
2907 return;
2909 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
2910 goto error;
2912 if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
2913 goto error;
2915 if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
2916 int i;
2917 unsigned dim;
2918 dim = isl_basic_map_total_dim(cgbr->tab->bmap);
2920 if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
2921 goto error;
2923 for (i = 0; i < dim; ++i) {
2924 if (!isl_int_is_neg(ineq[1 + i]))
2925 continue;
2926 isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
2929 if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
2930 goto error;
2932 for (i = 0; i < dim; ++i) {
2933 if (!isl_int_is_neg(ineq[1 + i]))
2934 continue;
2935 isl_int_sub(ineq[0], ineq[0], ineq[1 + i]);
2939 if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
2940 if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
2941 goto error;
2942 if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
2943 goto error;
2946 return;
2947 error:
2948 isl_tab_free(cgbr->tab);
2949 cgbr->tab = NULL;
2952 static void context_gbr_add_ineq(struct isl_context *context, isl_int *ineq,
2953 int check, int update)
2955 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2957 add_gbr_ineq(cgbr, ineq);
2958 if (!cgbr->tab)
2959 return;
2961 if (check) {
2962 int v = tab_has_valid_sample(cgbr->tab, ineq, 0);
2963 if (v < 0)
2964 goto error;
2965 if (!v)
2966 check_gbr_integer_feasible(cgbr);
2968 if (update)
2969 cgbr->tab = check_samples(cgbr->tab, ineq, 0);
2970 return;
2971 error:
2972 isl_tab_free(cgbr->tab);
2973 cgbr->tab = NULL;
2976 static int context_gbr_add_ineq_wrap(void *user, isl_int *ineq)
2978 struct isl_context *context = (struct isl_context *)user;
2979 context_gbr_add_ineq(context, ineq, 0, 0);
2980 return context->op->is_ok(context) ? 0 : -1;
2983 static enum isl_tab_row_sign context_gbr_ineq_sign(struct isl_context *context,
2984 isl_int *ineq, int strict)
2986 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2987 return tab_ineq_sign(cgbr->tab, ineq, strict);
2990 /* Check whether "ineq" can be added to the tableau without rendering
2991 * it infeasible.
2993 static int context_gbr_test_ineq(struct isl_context *context, isl_int *ineq)
2995 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
2996 struct isl_tab_undo *snap;
2997 struct isl_tab_undo *shifted_snap = NULL;
2998 struct isl_tab_undo *cone_snap = NULL;
2999 int feasible;
3001 if (!cgbr->tab)
3002 return -1;
3004 if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
3005 return -1;
3007 snap = isl_tab_snap(cgbr->tab);
3008 if (cgbr->shifted)
3009 shifted_snap = isl_tab_snap(cgbr->shifted);
3010 if (cgbr->cone)
3011 cone_snap = isl_tab_snap(cgbr->cone);
3012 add_gbr_ineq(cgbr, ineq);
3013 check_gbr_integer_feasible(cgbr);
3014 if (!cgbr->tab)
3015 return -1;
3016 feasible = !cgbr->tab->empty;
3017 if (isl_tab_rollback(cgbr->tab, snap) < 0)
3018 return -1;
3019 if (shifted_snap) {
3020 if (isl_tab_rollback(cgbr->shifted, shifted_snap))
3021 return -1;
3022 } else if (cgbr->shifted) {
3023 isl_tab_free(cgbr->shifted);
3024 cgbr->shifted = NULL;
3026 if (cone_snap) {
3027 if (isl_tab_rollback(cgbr->cone, cone_snap))
3028 return -1;
3029 } else if (cgbr->cone) {
3030 isl_tab_free(cgbr->cone);
3031 cgbr->cone = NULL;
3034 return feasible;
3037 /* Return the column of the last of the variables associated to
3038 * a column that has a non-zero coefficient.
3039 * This function is called in a context where only coefficients
3040 * of parameters or divs can be non-zero.
3042 static int last_non_zero_var_col(struct isl_tab *tab, isl_int *p)
3044 int i;
3045 int col;
3047 if (tab->n_var == 0)
3048 return -1;
3050 for (i = tab->n_var - 1; i >= 0; --i) {
3051 if (i >= tab->n_param && i < tab->n_var - tab->n_div)
3052 continue;
3053 if (tab->var[i].is_row)
3054 continue;
3055 col = tab->var[i].index;
3056 if (!isl_int_is_zero(p[col]))
3057 return col;
3060 return -1;
3063 /* Look through all the recently added equalities in the context
3064 * to see if we can propagate any of them to the main tableau.
3066 * The newly added equalities in the context are encoded as pairs
3067 * of inequalities starting at inequality "first".
3069 * We tentatively add each of these equalities to the main tableau
3070 * and if this happens to result in a row with a final coefficient
3071 * that is one or negative one, we use it to kill a column
3072 * in the main tableau. Otherwise, we discard the tentatively
3073 * added row.
3075 * Return 0 on success and -1 on failure.
3077 static int propagate_equalities(struct isl_context_gbr *cgbr,
3078 struct isl_tab *tab, unsigned first)
3080 int i;
3081 struct isl_vec *eq = NULL;
3083 eq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
3084 if (!eq)
3085 goto error;
3087 if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
3088 goto error;
3090 isl_seq_clr(eq->el + 1 + tab->n_param,
3091 tab->n_var - tab->n_param - tab->n_div);
3092 for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
3093 int j;
3094 int r;
3095 struct isl_tab_undo *snap;
3096 snap = isl_tab_snap(tab);
3098 isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
3099 isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
3100 cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
3101 tab->n_div);
3103 r = isl_tab_add_row(tab, eq->el);
3104 if (r < 0)
3105 goto error;
3106 r = tab->con[r].index;
3107 j = last_non_zero_var_col(tab, tab->mat->row[r] + 2 + tab->M);
3108 if (j < 0 || j < tab->n_dead ||
3109 !isl_int_is_one(tab->mat->row[r][0]) ||
3110 (!isl_int_is_one(tab->mat->row[r][2 + tab->M + j]) &&
3111 !isl_int_is_negone(tab->mat->row[r][2 + tab->M + j]))) {
3112 if (isl_tab_rollback(tab, snap) < 0)
3113 goto error;
3114 continue;
3116 if (isl_tab_pivot(tab, r, j) < 0)
3117 goto error;
3118 if (isl_tab_kill_col(tab, j) < 0)
3119 goto error;
3121 if (restore_lexmin(tab) < 0)
3122 goto error;
3125 isl_vec_free(eq);
3127 return 0;
3128 error:
3129 isl_vec_free(eq);
3130 isl_tab_free(cgbr->tab);
3131 cgbr->tab = NULL;
3132 return -1;
3135 static int context_gbr_detect_equalities(struct isl_context *context,
3136 struct isl_tab *tab)
3138 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3139 struct isl_ctx *ctx;
3140 unsigned n_ineq;
3142 ctx = cgbr->tab->mat->ctx;
3144 if (!cgbr->cone) {
3145 struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
3146 cgbr->cone = isl_tab_from_recession_cone(bset, 0);
3147 if (!cgbr->cone)
3148 goto error;
3149 if (isl_tab_track_bset(cgbr->cone,
3150 isl_basic_set_copy(bset)) < 0)
3151 goto error;
3153 if (isl_tab_detect_implicit_equalities(cgbr->cone) < 0)
3154 goto error;
3156 n_ineq = cgbr->tab->bmap->n_ineq;
3157 cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
3158 if (!cgbr->tab)
3159 return -1;
3160 if (cgbr->tab->bmap->n_ineq > n_ineq &&
3161 propagate_equalities(cgbr, tab, n_ineq) < 0)
3162 return -1;
3164 return 0;
3165 error:
3166 isl_tab_free(cgbr->tab);
3167 cgbr->tab = NULL;
3168 return -1;
3171 static int context_gbr_get_div(struct isl_context *context, struct isl_tab *tab,
3172 struct isl_vec *div)
3174 return get_div(tab, context, div);
3177 static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div)
3179 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3180 if (cgbr->cone) {
3181 int k;
3183 if (isl_tab_extend_cons(cgbr->cone, 3) < 0)
3184 return -1;
3185 if (isl_tab_extend_vars(cgbr->cone, 1) < 0)
3186 return -1;
3187 if (isl_tab_allocate_var(cgbr->cone) <0)
3188 return -1;
3190 cgbr->cone->bmap = isl_basic_map_extend_space(cgbr->cone->bmap,
3191 isl_basic_map_get_space(cgbr->cone->bmap), 1, 0, 2);
3192 k = isl_basic_map_alloc_div(cgbr->cone->bmap);
3193 if (k < 0)
3194 return -1;
3195 isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
3196 if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
3197 return -1;
3199 return context_tab_add_div(cgbr->tab, div,
3200 context_gbr_add_ineq_wrap, context);
3203 static int context_gbr_best_split(struct isl_context *context,
3204 struct isl_tab *tab)
3206 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3207 struct isl_tab_undo *snap;
3208 int r;
3210 snap = isl_tab_snap(cgbr->tab);
3211 r = best_split(tab, cgbr->tab);
3213 if (r >= 0 && isl_tab_rollback(cgbr->tab, snap) < 0)
3214 return -1;
3216 return r;
3219 static int context_gbr_is_empty(struct isl_context *context)
3221 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3222 if (!cgbr->tab)
3223 return -1;
3224 return cgbr->tab->empty;
3227 struct isl_gbr_tab_undo {
3228 struct isl_tab_undo *tab_snap;
3229 struct isl_tab_undo *shifted_snap;
3230 struct isl_tab_undo *cone_snap;
3233 static void *context_gbr_save(struct isl_context *context)
3235 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3236 struct isl_gbr_tab_undo *snap;
3238 if (!cgbr->tab)
3239 return NULL;
3241 snap = isl_alloc_type(cgbr->tab->mat->ctx, struct isl_gbr_tab_undo);
3242 if (!snap)
3243 return NULL;
3245 snap->tab_snap = isl_tab_snap(cgbr->tab);
3246 if (isl_tab_save_samples(cgbr->tab) < 0)
3247 goto error;
3249 if (cgbr->shifted)
3250 snap->shifted_snap = isl_tab_snap(cgbr->shifted);
3251 else
3252 snap->shifted_snap = NULL;
3254 if (cgbr->cone)
3255 snap->cone_snap = isl_tab_snap(cgbr->cone);
3256 else
3257 snap->cone_snap = NULL;
3259 return snap;
3260 error:
3261 free(snap);
3262 return NULL;
3265 static void context_gbr_restore(struct isl_context *context, void *save)
3267 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3268 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3269 if (!snap)
3270 goto error;
3271 if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0)
3272 goto error;
3274 if (snap->shifted_snap) {
3275 if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
3276 goto error;
3277 } else if (cgbr->shifted) {
3278 isl_tab_free(cgbr->shifted);
3279 cgbr->shifted = NULL;
3282 if (snap->cone_snap) {
3283 if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
3284 goto error;
3285 } else if (cgbr->cone) {
3286 isl_tab_free(cgbr->cone);
3287 cgbr->cone = NULL;
3290 free(snap);
3292 return;
3293 error:
3294 free(snap);
3295 isl_tab_free(cgbr->tab);
3296 cgbr->tab = NULL;
3299 static void context_gbr_discard(void *save)
3301 struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
3302 free(snap);
3305 static int context_gbr_is_ok(struct isl_context *context)
3307 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3308 return !!cgbr->tab;
3311 static void context_gbr_invalidate(struct isl_context *context)
3313 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3314 isl_tab_free(cgbr->tab);
3315 cgbr->tab = NULL;
3318 static void context_gbr_free(struct isl_context *context)
3320 struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
3321 isl_tab_free(cgbr->tab);
3322 isl_tab_free(cgbr->shifted);
3323 isl_tab_free(cgbr->cone);
3324 free(cgbr);
3327 struct isl_context_op isl_context_gbr_op = {
3328 context_gbr_detect_nonnegative_parameters,
3329 context_gbr_peek_basic_set,
3330 context_gbr_peek_tab,
3331 context_gbr_add_eq,
3332 context_gbr_add_ineq,
3333 context_gbr_ineq_sign,
3334 context_gbr_test_ineq,
3335 context_gbr_get_div,
3336 context_gbr_add_div,
3337 context_gbr_detect_equalities,
3338 context_gbr_best_split,
3339 context_gbr_is_empty,
3340 context_gbr_is_ok,
3341 context_gbr_save,
3342 context_gbr_restore,
3343 context_gbr_discard,
3344 context_gbr_invalidate,
3345 context_gbr_free,
3348 static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
3350 struct isl_context_gbr *cgbr;
3352 if (!dom)
3353 return NULL;
3355 cgbr = isl_calloc_type(dom->ctx, struct isl_context_gbr);
3356 if (!cgbr)
3357 return NULL;
3359 cgbr->context.op = &isl_context_gbr_op;
3361 cgbr->shifted = NULL;
3362 cgbr->cone = NULL;
3363 cgbr->tab = isl_tab_from_basic_set(dom, 1);
3364 cgbr->tab = isl_tab_init_samples(cgbr->tab);
3365 if (!cgbr->tab)
3366 goto error;
3367 check_gbr_integer_feasible(cgbr);
3369 return &cgbr->context;
3370 error:
3371 cgbr->context.op->free(&cgbr->context);
3372 return NULL;
3375 static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
3377 if (!dom)
3378 return NULL;
3380 if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
3381 return isl_context_lex_alloc(dom);
3382 else
3383 return isl_context_gbr_alloc(dom);
3386 /* Construct an isl_sol_map structure for accumulating the solution.
3387 * If track_empty is set, then we also keep track of the parts
3388 * of the context where there is no solution.
3389 * If max is set, then we are solving a maximization, rather than
3390 * a minimization problem, which means that the variables in the
3391 * tableau have value "M - x" rather than "M + x".
3393 static struct isl_sol *sol_map_init(struct isl_basic_map *bmap,
3394 struct isl_basic_set *dom, int track_empty, int max)
3396 struct isl_sol_map *sol_map = NULL;
3398 if (!bmap)
3399 goto error;
3401 sol_map = isl_calloc_type(bmap->ctx, struct isl_sol_map);
3402 if (!sol_map)
3403 goto error;
3405 sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
3406 sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
3407 sol_map->sol.dec_level.sol = &sol_map->sol;
3408 sol_map->sol.max = max;
3409 sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
3410 sol_map->sol.add = &sol_map_add_wrap;
3411 sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
3412 sol_map->sol.free = &sol_map_free_wrap;
3413 sol_map->map = isl_map_alloc_space(isl_basic_map_get_space(bmap), 1,
3414 ISL_MAP_DISJOINT);
3415 if (!sol_map->map)
3416 goto error;
3418 sol_map->sol.context = isl_context_alloc(dom);
3419 if (!sol_map->sol.context)
3420 goto error;
3422 if (track_empty) {
3423 sol_map->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
3424 1, ISL_SET_DISJOINT);
3425 if (!sol_map->empty)
3426 goto error;
3429 isl_basic_set_free(dom);
3430 return &sol_map->sol;
3431 error:
3432 isl_basic_set_free(dom);
3433 sol_map_free(sol_map);
3434 return NULL;
3437 /* Check whether all coefficients of (non-parameter) variables
3438 * are non-positive, meaning that no pivots can be performed on the row.
3440 static int is_critical(struct isl_tab *tab, int row)
3442 int j;
3443 unsigned off = 2 + tab->M;
3445 for (j = tab->n_dead; j < tab->n_col; ++j) {
3446 if (tab->col_var[j] >= 0 &&
3447 (tab->col_var[j] < tab->n_param ||
3448 tab->col_var[j] >= tab->n_var - tab->n_div))
3449 continue;
3451 if (isl_int_is_pos(tab->mat->row[row][off + j]))
3452 return 0;
3455 return 1;
3458 /* Check whether the inequality represented by vec is strict over the integers,
3459 * i.e., there are no integer values satisfying the constraint with
3460 * equality. This happens if the gcd of the coefficients is not a divisor
3461 * of the constant term. If so, scale the constraint down by the gcd
3462 * of the coefficients.
3464 static int is_strict(struct isl_vec *vec)
3466 isl_int gcd;
3467 int strict = 0;
3469 isl_int_init(gcd);
3470 isl_seq_gcd(vec->el + 1, vec->size - 1, &gcd);
3471 if (!isl_int_is_one(gcd)) {
3472 strict = !isl_int_is_divisible_by(vec->el[0], gcd);
3473 isl_int_fdiv_q(vec->el[0], vec->el[0], gcd);
3474 isl_seq_scale_down(vec->el + 1, vec->el + 1, gcd, vec->size-1);
3476 isl_int_clear(gcd);
3478 return strict;
3481 /* Determine the sign of the given row of the main tableau.
3482 * The result is one of
3483 * isl_tab_row_pos: always non-negative; no pivot needed
3484 * isl_tab_row_neg: always non-positive; pivot
3485 * isl_tab_row_any: can be both positive and negative; split
3487 * We first handle some simple cases
3488 * - the row sign may be known already
3489 * - the row may be obviously non-negative
3490 * - the parametric constant may be equal to that of another row
3491 * for which we know the sign. This sign will be either "pos" or
3492 * "any". If it had been "neg" then we would have pivoted before.
3494 * If none of these cases hold, we check the value of the row for each
3495 * of the currently active samples. Based on the signs of these values
3496 * we make an initial determination of the sign of the row.
3498 * all zero -> unk(nown)
3499 * all non-negative -> pos
3500 * all non-positive -> neg
3501 * both negative and positive -> all
3503 * If we end up with "all", we are done.
3504 * Otherwise, we perform a check for positive and/or negative
3505 * values as follows.
3507 * samples neg unk pos
3508 * <0 ? Y N Y N
3509 * pos any pos
3510 * >0 ? Y N Y N
3511 * any neg any neg
3513 * There is no special sign for "zero", because we can usually treat zero
3514 * as either non-negative or non-positive, whatever works out best.
3515 * However, if the row is "critical", meaning that pivoting is impossible
3516 * then we don't want to limp zero with the non-positive case, because
3517 * then we we would lose the solution for those values of the parameters
3518 * where the value of the row is zero. Instead, we treat 0 as non-negative
3519 * ensuring a split if the row can attain both zero and negative values.
3520 * The same happens when the original constraint was one that could not
3521 * be satisfied with equality by any integer values of the parameters.
3522 * In this case, we normalize the constraint, but then a value of zero
3523 * for the normalized constraint is actually a positive value for the
3524 * original constraint, so again we need to treat zero as non-negative.
3525 * In both these cases, we have the following decision tree instead:
3527 * all non-negative -> pos
3528 * all negative -> neg
3529 * both negative and non-negative -> all
3531 * samples neg pos
3532 * <0 ? Y N
3533 * any pos
3534 * >=0 ? Y N
3535 * any neg
3537 static enum isl_tab_row_sign row_sign(struct isl_tab *tab,
3538 struct isl_sol *sol, int row)
3540 struct isl_vec *ineq = NULL;
3541 enum isl_tab_row_sign res = isl_tab_row_unknown;
3542 int critical;
3543 int strict;
3544 int row2;
3546 if (tab->row_sign[row] != isl_tab_row_unknown)
3547 return tab->row_sign[row];
3548 if (is_obviously_nonneg(tab, row))
3549 return isl_tab_row_pos;
3550 for (row2 = tab->n_redundant; row2 < tab->n_row; ++row2) {
3551 if (tab->row_sign[row2] == isl_tab_row_unknown)
3552 continue;
3553 if (identical_parameter_line(tab, row, row2))
3554 return tab->row_sign[row2];
3557 critical = is_critical(tab, row);
3559 ineq = get_row_parameter_ineq(tab, row);
3560 if (!ineq)
3561 goto error;
3563 strict = is_strict(ineq);
3565 res = sol->context->op->ineq_sign(sol->context, ineq->el,
3566 critical || strict);
3568 if (res == isl_tab_row_unknown || res == isl_tab_row_pos) {
3569 /* test for negative values */
3570 int feasible;
3571 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3572 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3574 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3575 if (feasible < 0)
3576 goto error;
3577 if (!feasible)
3578 res = isl_tab_row_pos;
3579 else
3580 res = (res == isl_tab_row_unknown) ? isl_tab_row_neg
3581 : isl_tab_row_any;
3582 if (res == isl_tab_row_neg) {
3583 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3584 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3588 if (res == isl_tab_row_neg) {
3589 /* test for positive values */
3590 int feasible;
3591 if (!critical && !strict)
3592 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3594 feasible = sol->context->op->test_ineq(sol->context, ineq->el);
3595 if (feasible < 0)
3596 goto error;
3597 if (feasible)
3598 res = isl_tab_row_any;
3601 isl_vec_free(ineq);
3602 return res;
3603 error:
3604 isl_vec_free(ineq);
3605 return isl_tab_row_unknown;
3608 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
3610 /* Find solutions for values of the parameters that satisfy the given
3611 * inequality.
3613 * We currently take a snapshot of the context tableau that is reset
3614 * when we return from this function, while we make a copy of the main
3615 * tableau, leaving the original main tableau untouched.
3616 * These are fairly arbitrary choices. Making a copy also of the context
3617 * tableau would obviate the need to undo any changes made to it later,
3618 * while taking a snapshot of the main tableau could reduce memory usage.
3619 * If we were to switch to taking a snapshot of the main tableau,
3620 * we would have to keep in mind that we need to save the row signs
3621 * and that we need to do this before saving the current basis
3622 * such that the basis has been restore before we restore the row signs.
3624 static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
3626 void *saved;
3628 if (!sol->context)
3629 goto error;
3630 saved = sol->context->op->save(sol->context);
3632 tab = isl_tab_dup(tab);
3633 if (!tab)
3634 goto error;
3636 sol->context->op->add_ineq(sol->context, ineq, 0, 1);
3638 find_solutions(sol, tab);
3640 if (!sol->error)
3641 sol->context->op->restore(sol->context, saved);
3642 else
3643 sol->context->op->discard(saved);
3644 return;
3645 error:
3646 sol->error = 1;
3649 /* Record the absence of solutions for those values of the parameters
3650 * that do not satisfy the given inequality with equality.
3652 static void no_sol_in_strict(struct isl_sol *sol,
3653 struct isl_tab *tab, struct isl_vec *ineq)
3655 int empty;
3656 void *saved;
3658 if (!sol->context || sol->error)
3659 goto error;
3660 saved = sol->context->op->save(sol->context);
3662 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3664 sol->context->op->add_ineq(sol->context, ineq->el, 1, 0);
3665 if (!sol->context)
3666 goto error;
3668 empty = tab->empty;
3669 tab->empty = 1;
3670 sol_add(sol, tab);
3671 tab->empty = empty;
3673 isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
3675 sol->context->op->restore(sol->context, saved);
3676 return;
3677 error:
3678 sol->error = 1;
3681 /* Compute the lexicographic minimum of the set represented by the main
3682 * tableau "tab" within the context "sol->context_tab".
3683 * On entry the sample value of the main tableau is lexicographically
3684 * less than or equal to this lexicographic minimum.
3685 * Pivots are performed until a feasible point is found, which is then
3686 * necessarily equal to the minimum, or until the tableau is found to
3687 * be infeasible. Some pivots may need to be performed for only some
3688 * feasible values of the context tableau. If so, the context tableau
3689 * is split into a part where the pivot is needed and a part where it is not.
3691 * Whenever we enter the main loop, the main tableau is such that no
3692 * "obvious" pivots need to be performed on it, where "obvious" means
3693 * that the given row can be seen to be negative without looking at
3694 * the context tableau. In particular, for non-parametric problems,
3695 * no pivots need to be performed on the main tableau.
3696 * The caller of find_solutions is responsible for making this property
3697 * hold prior to the first iteration of the loop, while restore_lexmin
3698 * is called before every other iteration.
3700 * Inside the main loop, we first examine the signs of the rows of
3701 * the main tableau within the context of the context tableau.
3702 * If we find a row that is always non-positive for all values of
3703 * the parameters satisfying the context tableau and negative for at
3704 * least one value of the parameters, we perform the appropriate pivot
3705 * and start over. An exception is the case where no pivot can be
3706 * performed on the row. In this case, we require that the sign of
3707 * the row is negative for all values of the parameters (rather than just
3708 * non-positive). This special case is handled inside row_sign, which
3709 * will say that the row can have any sign if it determines that it can
3710 * attain both negative and zero values.
3712 * If we can't find a row that always requires a pivot, but we can find
3713 * one or more rows that require a pivot for some values of the parameters
3714 * (i.e., the row can attain both positive and negative signs), then we split
3715 * the context tableau into two parts, one where we force the sign to be
3716 * non-negative and one where we force is to be negative.
3717 * The non-negative part is handled by a recursive call (through find_in_pos).
3718 * Upon returning from this call, we continue with the negative part and
3719 * perform the required pivot.
3721 * If no such rows can be found, all rows are non-negative and we have
3722 * found a (rational) feasible point. If we only wanted a rational point
3723 * then we are done.
3724 * Otherwise, we check if all values of the sample point of the tableau
3725 * are integral for the variables. If so, we have found the minimal
3726 * integral point and we are done.
3727 * If the sample point is not integral, then we need to make a distinction
3728 * based on whether the constant term is non-integral or the coefficients
3729 * of the parameters. Furthermore, in order to decide how to handle
3730 * the non-integrality, we also need to know whether the coefficients
3731 * of the other columns in the tableau are integral. This leads
3732 * to the following table. The first two rows do not correspond
3733 * to a non-integral sample point and are only mentioned for completeness.
3735 * constant parameters other
3737 * int int int |
3738 * int int rat | -> no problem
3740 * rat int int -> fail
3742 * rat int rat -> cut
3744 * int rat rat |
3745 * rat rat rat | -> parametric cut
3747 * int rat int |
3748 * rat rat int | -> split context
3750 * If the parametric constant is completely integral, then there is nothing
3751 * to be done. If the constant term is non-integral, but all the other
3752 * coefficient are integral, then there is nothing that can be done
3753 * and the tableau has no integral solution.
3754 * If, on the other hand, one or more of the other columns have rational
3755 * coefficients, but the parameter coefficients are all integral, then
3756 * we can perform a regular (non-parametric) cut.
3757 * Finally, if there is any parameter coefficient that is non-integral,
3758 * then we need to involve the context tableau. There are two cases here.
3759 * If at least one other column has a rational coefficient, then we
3760 * can perform a parametric cut in the main tableau by adding a new
3761 * integer division in the context tableau.
3762 * If all other columns have integral coefficients, then we need to
3763 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
3764 * is always integral. We do this by introducing an integer division
3765 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
3766 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
3767 * Since q is expressed in the tableau as
3768 * c + \sum a_i y_i - m q >= 0
3769 * -c - \sum a_i y_i + m q + m - 1 >= 0
3770 * it is sufficient to add the inequality
3771 * -c - \sum a_i y_i + m q >= 0
3772 * In the part of the context where this inequality does not hold, the
3773 * main tableau is marked as being empty.
3775 static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
3777 struct isl_context *context;
3778 int r;
3780 if (!tab || sol->error)
3781 goto error;
3783 context = sol->context;
3785 if (tab->empty)
3786 goto done;
3787 if (context->op->is_empty(context))
3788 goto done;
3790 for (r = 0; r >= 0 && tab && !tab->empty; r = restore_lexmin(tab)) {
3791 int flags;
3792 int row;
3793 enum isl_tab_row_sign sgn;
3794 int split = -1;
3795 int n_split = 0;
3797 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3798 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3799 continue;
3800 sgn = row_sign(tab, sol, row);
3801 if (!sgn)
3802 goto error;
3803 tab->row_sign[row] = sgn;
3804 if (sgn == isl_tab_row_any)
3805 n_split++;
3806 if (sgn == isl_tab_row_any && split == -1)
3807 split = row;
3808 if (sgn == isl_tab_row_neg)
3809 break;
3811 if (row < tab->n_row)
3812 continue;
3813 if (split != -1) {
3814 struct isl_vec *ineq;
3815 if (n_split != 1)
3816 split = context->op->best_split(context, tab);
3817 if (split < 0)
3818 goto error;
3819 ineq = get_row_parameter_ineq(tab, split);
3820 if (!ineq)
3821 goto error;
3822 is_strict(ineq);
3823 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3824 if (!isl_tab_var_from_row(tab, row)->is_nonneg)
3825 continue;
3826 if (tab->row_sign[row] == isl_tab_row_any)
3827 tab->row_sign[row] = isl_tab_row_unknown;
3829 tab->row_sign[split] = isl_tab_row_pos;
3830 sol_inc_level(sol);
3831 find_in_pos(sol, tab, ineq->el);
3832 tab->row_sign[split] = isl_tab_row_neg;
3833 row = split;
3834 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3835 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
3836 if (!sol->error)
3837 context->op->add_ineq(context, ineq->el, 0, 1);
3838 isl_vec_free(ineq);
3839 if (sol->error)
3840 goto error;
3841 continue;
3843 if (tab->rational)
3844 break;
3845 row = first_non_integer_row(tab, &flags);
3846 if (row < 0)
3847 break;
3848 if (ISL_FL_ISSET(flags, I_PAR)) {
3849 if (ISL_FL_ISSET(flags, I_VAR)) {
3850 if (isl_tab_mark_empty(tab) < 0)
3851 goto error;
3852 break;
3854 row = add_cut(tab, row);
3855 } else if (ISL_FL_ISSET(flags, I_VAR)) {
3856 struct isl_vec *div;
3857 struct isl_vec *ineq;
3858 int d;
3859 div = get_row_split_div(tab, row);
3860 if (!div)
3861 goto error;
3862 d = context->op->get_div(context, tab, div);
3863 isl_vec_free(div);
3864 if (d < 0)
3865 goto error;
3866 ineq = ineq_for_div(context->op->peek_basic_set(context), d);
3867 if (!ineq)
3868 goto error;
3869 sol_inc_level(sol);
3870 no_sol_in_strict(sol, tab, ineq);
3871 isl_seq_neg(ineq->el, ineq->el, ineq->size);
3872 context->op->add_ineq(context, ineq->el, 1, 1);
3873 isl_vec_free(ineq);
3874 if (sol->error || !context->op->is_ok(context))
3875 goto error;
3876 tab = set_row_cst_to_div(tab, row, d);
3877 if (context->op->is_empty(context))
3878 break;
3879 } else
3880 row = add_parametric_cut(tab, row, context);
3881 if (row < 0)
3882 goto error;
3884 if (r < 0)
3885 goto error;
3886 done:
3887 sol_add(sol, tab);
3888 isl_tab_free(tab);
3889 return;
3890 error:
3891 isl_tab_free(tab);
3892 sol->error = 1;
3895 /* Does "sol" contain a pair of partial solutions that could potentially
3896 * be merged?
3898 * We currently only check that "sol" is not in an error state
3899 * and that there are at least two partial solutions of which the final two
3900 * are defined at the same level.
3902 static int sol_has_mergeable_solutions(struct isl_sol *sol)
3904 if (sol->error)
3905 return 0;
3906 if (!sol->partial)
3907 return 0;
3908 if (!sol->partial->next)
3909 return 0;
3910 return sol->partial->level == sol->partial->next->level;
3913 /* Compute the lexicographic minimum of the set represented by the main
3914 * tableau "tab" within the context "sol->context_tab".
3916 * As a preprocessing step, we first transfer all the purely parametric
3917 * equalities from the main tableau to the context tableau, i.e.,
3918 * parameters that have been pivoted to a row.
3919 * These equalities are ignored by the main algorithm, because the
3920 * corresponding rows may not be marked as being non-negative.
3921 * In parts of the context where the added equality does not hold,
3922 * the main tableau is marked as being empty.
3924 * Before we embark on the actual computation, we save a copy
3925 * of the context. When we return, we check if there are any
3926 * partial solutions that can potentially be merged. If so,
3927 * we perform a rollback to the initial state of the context.
3928 * The merging of partial solutions happens inside calls to
3929 * sol_dec_level that are pushed onto the undo stack of the context.
3930 * If there are no partial solutions that can potentially be merged
3931 * then the rollback is skipped as it would just be wasted effort.
3933 static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
3935 int row;
3936 void *saved;
3938 if (!tab)
3939 goto error;
3941 sol->level = 0;
3943 for (row = tab->n_redundant; row < tab->n_row; ++row) {
3944 int p;
3945 struct isl_vec *eq;
3947 if (tab->row_var[row] < 0)
3948 continue;
3949 if (tab->row_var[row] >= tab->n_param &&
3950 tab->row_var[row] < tab->n_var - tab->n_div)
3951 continue;
3952 if (tab->row_var[row] < tab->n_param)
3953 p = tab->row_var[row];
3954 else
3955 p = tab->row_var[row]
3956 + tab->n_param - (tab->n_var - tab->n_div);
3958 eq = isl_vec_alloc(tab->mat->ctx, 1+tab->n_param+tab->n_div);
3959 if (!eq)
3960 goto error;
3961 get_row_parameter_line(tab, row, eq->el);
3962 isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
3963 eq = isl_vec_normalize(eq);
3965 sol_inc_level(sol);
3966 no_sol_in_strict(sol, tab, eq);
3968 isl_seq_neg(eq->el, eq->el, eq->size);
3969 sol_inc_level(sol);
3970 no_sol_in_strict(sol, tab, eq);
3971 isl_seq_neg(eq->el, eq->el, eq->size);
3973 sol->context->op->add_eq(sol->context, eq->el, 1, 1);
3975 isl_vec_free(eq);
3977 if (isl_tab_mark_redundant(tab, row) < 0)
3978 goto error;
3980 if (sol->context->op->is_empty(sol->context))
3981 break;
3983 row = tab->n_redundant - 1;
3986 saved = sol->context->op->save(sol->context);
3988 find_solutions(sol, tab);
3990 if (sol_has_mergeable_solutions(sol))
3991 sol->context->op->restore(sol->context, saved);
3992 else
3993 sol->context->op->discard(saved);
3995 sol->level = 0;
3996 sol_pop(sol);
3998 return;
3999 error:
4000 isl_tab_free(tab);
4001 sol->error = 1;
4004 /* Check if integer division "div" of "dom" also occurs in "bmap".
4005 * If so, return its position within the divs.
4006 * If not, return -1.
4008 static int find_context_div(struct isl_basic_map *bmap,
4009 struct isl_basic_set *dom, unsigned div)
4011 int i;
4012 unsigned b_dim = isl_space_dim(bmap->dim, isl_dim_all);
4013 unsigned d_dim = isl_space_dim(dom->dim, isl_dim_all);
4015 if (isl_int_is_zero(dom->div[div][0]))
4016 return -1;
4017 if (isl_seq_first_non_zero(dom->div[div] + 2 + d_dim, dom->n_div) != -1)
4018 return -1;
4020 for (i = 0; i < bmap->n_div; ++i) {
4021 if (isl_int_is_zero(bmap->div[i][0]))
4022 continue;
4023 if (isl_seq_first_non_zero(bmap->div[i] + 2 + d_dim,
4024 (b_dim - d_dim) + bmap->n_div) != -1)
4025 continue;
4026 if (isl_seq_eq(bmap->div[i], dom->div[div], 2 + d_dim))
4027 return i;
4029 return -1;
4032 /* The correspondence between the variables in the main tableau,
4033 * the context tableau, and the input map and domain is as follows.
4034 * The first n_param and the last n_div variables of the main tableau
4035 * form the variables of the context tableau.
4036 * In the basic map, these n_param variables correspond to the
4037 * parameters and the input dimensions. In the domain, they correspond
4038 * to the parameters and the set dimensions.
4039 * The n_div variables correspond to the integer divisions in the domain.
4040 * To ensure that everything lines up, we may need to copy some of the
4041 * integer divisions of the domain to the map. These have to be placed
4042 * in the same order as those in the context and they have to be placed
4043 * after any other integer divisions that the map may have.
4044 * This function performs the required reordering.
4046 static struct isl_basic_map *align_context_divs(struct isl_basic_map *bmap,
4047 struct isl_basic_set *dom)
4049 int i;
4050 int common = 0;
4051 int other;
4053 for (i = 0; i < dom->n_div; ++i)
4054 if (find_context_div(bmap, dom, i) != -1)
4055 common++;
4056 other = bmap->n_div - common;
4057 if (dom->n_div - common > 0) {
4058 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
4059 dom->n_div - common, 0, 0);
4060 if (!bmap)
4061 return NULL;
4063 for (i = 0; i < dom->n_div; ++i) {
4064 int pos = find_context_div(bmap, dom, i);
4065 if (pos < 0) {
4066 pos = isl_basic_map_alloc_div(bmap);
4067 if (pos < 0)
4068 goto error;
4069 isl_int_set_si(bmap->div[pos][0], 0);
4071 if (pos != other + i)
4072 isl_basic_map_swap_div(bmap, pos, other + i);
4074 return bmap;
4075 error:
4076 isl_basic_map_free(bmap);
4077 return NULL;
4080 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4081 * some obvious symmetries.
4083 * We make sure the divs in the domain are properly ordered,
4084 * because they will be added one by one in the given order
4085 * during the construction of the solution map.
4087 static struct isl_sol *basic_map_partial_lexopt_base(
4088 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4089 __isl_give isl_set **empty, int max,
4090 struct isl_sol *(*init)(__isl_keep isl_basic_map *bmap,
4091 __isl_take isl_basic_set *dom, int track_empty, int max))
4093 struct isl_tab *tab;
4094 struct isl_sol *sol = NULL;
4095 struct isl_context *context;
4097 if (dom->n_div) {
4098 dom = isl_basic_set_order_divs(dom);
4099 bmap = align_context_divs(bmap, dom);
4101 sol = init(bmap, dom, !!empty, max);
4102 if (!sol)
4103 goto error;
4105 context = sol->context;
4106 if (isl_basic_set_plain_is_empty(context->op->peek_basic_set(context)))
4107 /* nothing */;
4108 else if (isl_basic_map_plain_is_empty(bmap)) {
4109 if (sol->add_empty)
4110 sol->add_empty(sol,
4111 isl_basic_set_copy(context->op->peek_basic_set(context)));
4112 } else {
4113 tab = tab_for_lexmin(bmap,
4114 context->op->peek_basic_set(context), 1, max);
4115 tab = context->op->detect_nonnegative_parameters(context, tab);
4116 find_solutions_main(sol, tab);
4118 if (sol->error)
4119 goto error;
4121 isl_basic_map_free(bmap);
4122 return sol;
4123 error:
4124 sol_free(sol);
4125 isl_basic_map_free(bmap);
4126 return NULL;
4129 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4130 * some obvious symmetries.
4132 * We call basic_map_partial_lexopt_base and extract the results.
4134 static __isl_give isl_map *basic_map_partial_lexopt_base_map(
4135 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4136 __isl_give isl_set **empty, int max)
4138 isl_map *result = NULL;
4139 struct isl_sol *sol;
4140 struct isl_sol_map *sol_map;
4142 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
4143 &sol_map_init);
4144 if (!sol)
4145 return NULL;
4146 sol_map = (struct isl_sol_map *) sol;
4148 result = isl_map_copy(sol_map->map);
4149 if (empty)
4150 *empty = isl_set_copy(sol_map->empty);
4151 sol_free(&sol_map->sol);
4152 return result;
4155 /* Structure used during detection of parallel constraints.
4156 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4157 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4158 * val: the coefficients of the output variables
4160 struct isl_constraint_equal_info {
4161 isl_basic_map *bmap;
4162 unsigned n_in;
4163 unsigned n_out;
4164 isl_int *val;
4167 /* Check whether the coefficients of the output variables
4168 * of the constraint in "entry" are equal to info->val.
4170 static int constraint_equal(const void *entry, const void *val)
4172 isl_int **row = (isl_int **)entry;
4173 const struct isl_constraint_equal_info *info = val;
4175 return isl_seq_eq((*row) + 1 + info->n_in, info->val, info->n_out);
4178 /* Check whether "bmap" has a pair of constraints that have
4179 * the same coefficients for the output variables.
4180 * Note that the coefficients of the existentially quantified
4181 * variables need to be zero since the existentially quantified
4182 * of the result are usually not the same as those of the input.
4183 * the isl_dim_out and isl_dim_div dimensions.
4184 * If so, return 1 and return the row indices of the two constraints
4185 * in *first and *second.
4187 static int parallel_constraints(__isl_keep isl_basic_map *bmap,
4188 int *first, int *second)
4190 int i;
4191 isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
4192 struct isl_hash_table *table = NULL;
4193 struct isl_hash_table_entry *entry;
4194 struct isl_constraint_equal_info info;
4195 unsigned n_out;
4196 unsigned n_div;
4198 ctx = isl_basic_map_get_ctx(bmap);
4199 table = isl_hash_table_alloc(ctx, bmap->n_ineq);
4200 if (!table)
4201 goto error;
4203 info.n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4204 isl_basic_map_dim(bmap, isl_dim_in);
4205 info.bmap = bmap;
4206 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4207 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4208 info.n_out = n_out + n_div;
4209 for (i = 0; i < bmap->n_ineq; ++i) {
4210 uint32_t hash;
4212 info.val = bmap->ineq[i] + 1 + info.n_in;
4213 if (isl_seq_first_non_zero(info.val, n_out) < 0)
4214 continue;
4215 if (isl_seq_first_non_zero(info.val + n_out, n_div) >= 0)
4216 continue;
4217 hash = isl_seq_get_hash(info.val, info.n_out);
4218 entry = isl_hash_table_find(ctx, table, hash,
4219 constraint_equal, &info, 1);
4220 if (!entry)
4221 goto error;
4222 if (entry->data)
4223 break;
4224 entry->data = &bmap->ineq[i];
4227 if (i < bmap->n_ineq) {
4228 *first = ((isl_int **)entry->data) - bmap->ineq;
4229 *second = i;
4232 isl_hash_table_free(ctx, table);
4234 return i < bmap->n_ineq;
4235 error:
4236 isl_hash_table_free(ctx, table);
4237 return -1;
4240 /* Given a set of upper bounds in "var", add constraints to "bset"
4241 * that make the i-th bound smallest.
4243 * In particular, if there are n bounds b_i, then add the constraints
4245 * b_i <= b_j for j > i
4246 * b_i < b_j for j < i
4248 static __isl_give isl_basic_set *select_minimum(__isl_take isl_basic_set *bset,
4249 __isl_keep isl_mat *var, int i)
4251 isl_ctx *ctx;
4252 int j, k;
4254 ctx = isl_mat_get_ctx(var);
4256 for (j = 0; j < var->n_row; ++j) {
4257 if (j == i)
4258 continue;
4259 k = isl_basic_set_alloc_inequality(bset);
4260 if (k < 0)
4261 goto error;
4262 isl_seq_combine(bset->ineq[k], ctx->one, var->row[j],
4263 ctx->negone, var->row[i], var->n_col);
4264 isl_int_set_si(bset->ineq[k][var->n_col], 0);
4265 if (j < i)
4266 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4269 bset = isl_basic_set_finalize(bset);
4271 return bset;
4272 error:
4273 isl_basic_set_free(bset);
4274 return NULL;
4277 /* Given a set of upper bounds on the last "input" variable m,
4278 * construct a set that assigns the minimal upper bound to m, i.e.,
4279 * construct a set that divides the space into cells where one
4280 * of the upper bounds is smaller than all the others and assign
4281 * this upper bound to m.
4283 * In particular, if there are n bounds b_i, then the result
4284 * consists of n basic sets, each one of the form
4286 * m = b_i
4287 * b_i <= b_j for j > i
4288 * b_i < b_j for j < i
4290 static __isl_give isl_set *set_minimum(__isl_take isl_space *dim,
4291 __isl_take isl_mat *var)
4293 int i, k;
4294 isl_basic_set *bset = NULL;
4295 isl_ctx *ctx;
4296 isl_set *set = NULL;
4298 if (!dim || !var)
4299 goto error;
4301 ctx = isl_space_get_ctx(dim);
4302 set = isl_set_alloc_space(isl_space_copy(dim),
4303 var->n_row, ISL_SET_DISJOINT);
4305 for (i = 0; i < var->n_row; ++i) {
4306 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0,
4307 1, var->n_row - 1);
4308 k = isl_basic_set_alloc_equality(bset);
4309 if (k < 0)
4310 goto error;
4311 isl_seq_cpy(bset->eq[k], var->row[i], var->n_col);
4312 isl_int_set_si(bset->eq[k][var->n_col], -1);
4313 bset = select_minimum(bset, var, i);
4314 set = isl_set_add_basic_set(set, bset);
4317 isl_space_free(dim);
4318 isl_mat_free(var);
4319 return set;
4320 error:
4321 isl_basic_set_free(bset);
4322 isl_set_free(set);
4323 isl_space_free(dim);
4324 isl_mat_free(var);
4325 return NULL;
4328 /* Given that the last input variable of "bmap" represents the minimum
4329 * of the bounds in "cst", check whether we need to split the domain
4330 * based on which bound attains the minimum.
4332 * A split is needed when the minimum appears in an integer division
4333 * or in an equality. Otherwise, it is only needed if it appears in
4334 * an upper bound that is different from the upper bounds on which it
4335 * is defined.
4337 static int need_split_basic_map(__isl_keep isl_basic_map *bmap,
4338 __isl_keep isl_mat *cst)
4340 int i, j;
4341 unsigned total;
4342 unsigned pos;
4344 pos = cst->n_col - 1;
4345 total = isl_basic_map_dim(bmap, isl_dim_all);
4347 for (i = 0; i < bmap->n_div; ++i)
4348 if (!isl_int_is_zero(bmap->div[i][2 + pos]))
4349 return 1;
4351 for (i = 0; i < bmap->n_eq; ++i)
4352 if (!isl_int_is_zero(bmap->eq[i][1 + pos]))
4353 return 1;
4355 for (i = 0; i < bmap->n_ineq; ++i) {
4356 if (isl_int_is_nonneg(bmap->ineq[i][1 + pos]))
4357 continue;
4358 if (!isl_int_is_negone(bmap->ineq[i][1 + pos]))
4359 return 1;
4360 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + pos + 1,
4361 total - pos - 1) >= 0)
4362 return 1;
4364 for (j = 0; j < cst->n_row; ++j)
4365 if (isl_seq_eq(bmap->ineq[i], cst->row[j], cst->n_col))
4366 break;
4367 if (j >= cst->n_row)
4368 return 1;
4371 return 0;
4374 /* Given that the last set variable of "bset" represents the minimum
4375 * of the bounds in "cst", check whether we need to split the domain
4376 * based on which bound attains the minimum.
4378 * We simply call need_split_basic_map here. This is safe because
4379 * the position of the minimum is computed from "cst" and not
4380 * from "bmap".
4382 static int need_split_basic_set(__isl_keep isl_basic_set *bset,
4383 __isl_keep isl_mat *cst)
4385 return need_split_basic_map((isl_basic_map *)bset, cst);
4388 /* Given that the last set variable of "set" represents the minimum
4389 * of the bounds in "cst", check whether we need to split the domain
4390 * based on which bound attains the minimum.
4392 static int need_split_set(__isl_keep isl_set *set, __isl_keep isl_mat *cst)
4394 int i;
4396 for (i = 0; i < set->n; ++i)
4397 if (need_split_basic_set(set->p[i], cst))
4398 return 1;
4400 return 0;
4403 /* Given a set of which the last set variable is the minimum
4404 * of the bounds in "cst", split each basic set in the set
4405 * in pieces where one of the bounds is (strictly) smaller than the others.
4406 * This subdivision is given in "min_expr".
4407 * The variable is subsequently projected out.
4409 * We only do the split when it is needed.
4410 * For example if the last input variable m = min(a,b) and the only
4411 * constraints in the given basic set are lower bounds on m,
4412 * i.e., l <= m = min(a,b), then we can simply project out m
4413 * to obtain l <= a and l <= b, without having to split on whether
4414 * m is equal to a or b.
4416 static __isl_give isl_set *split(__isl_take isl_set *empty,
4417 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4419 int n_in;
4420 int i;
4421 isl_space *dim;
4422 isl_set *res;
4424 if (!empty || !min_expr || !cst)
4425 goto error;
4427 n_in = isl_set_dim(empty, isl_dim_set);
4428 dim = isl_set_get_space(empty);
4429 dim = isl_space_drop_dims(dim, isl_dim_set, n_in - 1, 1);
4430 res = isl_set_empty(dim);
4432 for (i = 0; i < empty->n; ++i) {
4433 isl_set *set;
4435 set = isl_set_from_basic_set(isl_basic_set_copy(empty->p[i]));
4436 if (need_split_basic_set(empty->p[i], cst))
4437 set = isl_set_intersect(set, isl_set_copy(min_expr));
4438 set = isl_set_remove_dims(set, isl_dim_set, n_in - 1, 1);
4440 res = isl_set_union_disjoint(res, set);
4443 isl_set_free(empty);
4444 isl_set_free(min_expr);
4445 isl_mat_free(cst);
4446 return res;
4447 error:
4448 isl_set_free(empty);
4449 isl_set_free(min_expr);
4450 isl_mat_free(cst);
4451 return NULL;
4454 /* Given a map of which the last input variable is the minimum
4455 * of the bounds in "cst", split each basic set in the set
4456 * in pieces where one of the bounds is (strictly) smaller than the others.
4457 * This subdivision is given in "min_expr".
4458 * The variable is subsequently projected out.
4460 * The implementation is essentially the same as that of "split".
4462 static __isl_give isl_map *split_domain(__isl_take isl_map *opt,
4463 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
4465 int n_in;
4466 int i;
4467 isl_space *dim;
4468 isl_map *res;
4470 if (!opt || !min_expr || !cst)
4471 goto error;
4473 n_in = isl_map_dim(opt, isl_dim_in);
4474 dim = isl_map_get_space(opt);
4475 dim = isl_space_drop_dims(dim, isl_dim_in, n_in - 1, 1);
4476 res = isl_map_empty(dim);
4478 for (i = 0; i < opt->n; ++i) {
4479 isl_map *map;
4481 map = isl_map_from_basic_map(isl_basic_map_copy(opt->p[i]));
4482 if (need_split_basic_map(opt->p[i], cst))
4483 map = isl_map_intersect_domain(map,
4484 isl_set_copy(min_expr));
4485 map = isl_map_remove_dims(map, isl_dim_in, n_in - 1, 1);
4487 res = isl_map_union_disjoint(res, map);
4490 isl_map_free(opt);
4491 isl_set_free(min_expr);
4492 isl_mat_free(cst);
4493 return res;
4494 error:
4495 isl_map_free(opt);
4496 isl_set_free(min_expr);
4497 isl_mat_free(cst);
4498 return NULL;
4501 static __isl_give isl_map *basic_map_partial_lexopt(
4502 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4503 __isl_give isl_set **empty, int max);
4505 union isl_lex_res {
4506 void *p;
4507 isl_map *map;
4508 isl_pw_multi_aff *pma;
4511 /* This function is called from basic_map_partial_lexopt_symm.
4512 * The last variable of "bmap" and "dom" corresponds to the minimum
4513 * of the bounds in "cst". "map_space" is the space of the original
4514 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4515 * is the space of the original domain.
4517 * We recursively call basic_map_partial_lexopt and then plug in
4518 * the definition of the minimum in the result.
4520 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_map_core(
4521 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4522 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
4523 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
4525 isl_map *opt;
4526 isl_set *min_expr;
4527 union isl_lex_res res;
4529 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
4531 opt = basic_map_partial_lexopt(bmap, dom, empty, max);
4533 if (empty) {
4534 *empty = split(*empty,
4535 isl_set_copy(min_expr), isl_mat_copy(cst));
4536 *empty = isl_set_reset_space(*empty, set_space);
4539 opt = split_domain(opt, min_expr, cst);
4540 opt = isl_map_reset_space(opt, map_space);
4542 res.map = opt;
4543 return res;
4546 /* Given a basic map with at least two parallel constraints (as found
4547 * by the function parallel_constraints), first look for more constraints
4548 * parallel to the two constraint and replace the found list of parallel
4549 * constraints by a single constraint with as "input" part the minimum
4550 * of the input parts of the list of constraints. Then, recursively call
4551 * basic_map_partial_lexopt (possibly finding more parallel constraints)
4552 * and plug in the definition of the minimum in the result.
4554 * More specifically, given a set of constraints
4556 * a x + b_i(p) >= 0
4558 * Replace this set by a single constraint
4560 * a x + u >= 0
4562 * with u a new parameter with constraints
4564 * u <= b_i(p)
4566 * Any solution to the new system is also a solution for the original system
4567 * since
4569 * a x >= -u >= -b_i(p)
4571 * Moreover, m = min_i(b_i(p)) satisfies the constraints on u and can
4572 * therefore be plugged into the solution.
4574 static union isl_lex_res basic_map_partial_lexopt_symm(
4575 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4576 __isl_give isl_set **empty, int max, int first, int second,
4577 __isl_give union isl_lex_res (*core)(__isl_take isl_basic_map *bmap,
4578 __isl_take isl_basic_set *dom,
4579 __isl_give isl_set **empty,
4580 int max, __isl_take isl_mat *cst,
4581 __isl_take isl_space *map_space,
4582 __isl_take isl_space *set_space))
4584 int i, n, k;
4585 int *list = NULL;
4586 unsigned n_in, n_out, n_div;
4587 isl_ctx *ctx;
4588 isl_vec *var = NULL;
4589 isl_mat *cst = NULL;
4590 isl_space *map_space, *set_space;
4591 union isl_lex_res res;
4593 map_space = isl_basic_map_get_space(bmap);
4594 set_space = empty ? isl_basic_set_get_space(dom) : NULL;
4596 n_in = isl_basic_map_dim(bmap, isl_dim_param) +
4597 isl_basic_map_dim(bmap, isl_dim_in);
4598 n_out = isl_basic_map_dim(bmap, isl_dim_all) - n_in;
4600 ctx = isl_basic_map_get_ctx(bmap);
4601 list = isl_alloc_array(ctx, int, bmap->n_ineq);
4602 var = isl_vec_alloc(ctx, n_out);
4603 if ((bmap->n_ineq && !list) || (n_out && !var))
4604 goto error;
4606 list[0] = first;
4607 list[1] = second;
4608 isl_seq_cpy(var->el, bmap->ineq[first] + 1 + n_in, n_out);
4609 for (i = second + 1, n = 2; i < bmap->n_ineq; ++i) {
4610 if (isl_seq_eq(var->el, bmap->ineq[i] + 1 + n_in, n_out))
4611 list[n++] = i;
4614 cst = isl_mat_alloc(ctx, n, 1 + n_in);
4615 if (!cst)
4616 goto error;
4618 for (i = 0; i < n; ++i)
4619 isl_seq_cpy(cst->row[i], bmap->ineq[list[i]], 1 + n_in);
4621 bmap = isl_basic_map_cow(bmap);
4622 if (!bmap)
4623 goto error;
4624 for (i = n - 1; i >= 0; --i)
4625 if (isl_basic_map_drop_inequality(bmap, list[i]) < 0)
4626 goto error;
4628 bmap = isl_basic_map_add(bmap, isl_dim_in, 1);
4629 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4630 k = isl_basic_map_alloc_inequality(bmap);
4631 if (k < 0)
4632 goto error;
4633 isl_seq_clr(bmap->ineq[k], 1 + n_in);
4634 isl_int_set_si(bmap->ineq[k][1 + n_in], 1);
4635 isl_seq_cpy(bmap->ineq[k] + 1 + n_in + 1, var->el, n_out);
4636 bmap = isl_basic_map_finalize(bmap);
4638 n_div = isl_basic_set_dim(dom, isl_dim_div);
4639 dom = isl_basic_set_add_dims(dom, isl_dim_set, 1);
4640 dom = isl_basic_set_extend_constraints(dom, 0, n);
4641 for (i = 0; i < n; ++i) {
4642 k = isl_basic_set_alloc_inequality(dom);
4643 if (k < 0)
4644 goto error;
4645 isl_seq_cpy(dom->ineq[k], cst->row[i], 1 + n_in);
4646 isl_int_set_si(dom->ineq[k][1 + n_in], -1);
4647 isl_seq_clr(dom->ineq[k] + 1 + n_in + 1, n_div);
4650 isl_vec_free(var);
4651 free(list);
4653 return core(bmap, dom, empty, max, cst, map_space, set_space);
4654 error:
4655 isl_space_free(map_space);
4656 isl_space_free(set_space);
4657 isl_mat_free(cst);
4658 isl_vec_free(var);
4659 free(list);
4660 isl_basic_set_free(dom);
4661 isl_basic_map_free(bmap);
4662 res.p = NULL;
4663 return res;
4666 static __isl_give isl_map *basic_map_partial_lexopt_symm_map(
4667 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4668 __isl_give isl_set **empty, int max, int first, int second)
4670 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
4671 first, second, &basic_map_partial_lexopt_symm_map_core).map;
4674 /* Recursive part of isl_tab_basic_map_partial_lexopt, after detecting
4675 * equalities and removing redundant constraints.
4677 * We first check if there are any parallel constraints (left).
4678 * If not, we are in the base case.
4679 * If there are parallel constraints, we replace them by a single
4680 * constraint in basic_map_partial_lexopt_symm and then call
4681 * this function recursively to look for more parallel constraints.
4683 static __isl_give isl_map *basic_map_partial_lexopt(
4684 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
4685 __isl_give isl_set **empty, int max)
4687 int par = 0;
4688 int first, second;
4690 if (!bmap)
4691 goto error;
4693 if (bmap->ctx->opt->pip_symmetry)
4694 par = parallel_constraints(bmap, &first, &second);
4695 if (par < 0)
4696 goto error;
4697 if (!par)
4698 return basic_map_partial_lexopt_base_map(bmap, dom, empty, max);
4700 return basic_map_partial_lexopt_symm_map(bmap, dom, empty, max,
4701 first, second);
4702 error:
4703 isl_basic_set_free(dom);
4704 isl_basic_map_free(bmap);
4705 return NULL;
4708 /* Compute the lexicographic minimum (or maximum if "max" is set)
4709 * of "bmap" over the domain "dom" and return the result as a map.
4710 * If "empty" is not NULL, then *empty is assigned a set that
4711 * contains those parts of the domain where there is no solution.
4712 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
4713 * then we compute the rational optimum. Otherwise, we compute
4714 * the integral optimum.
4716 * We perform some preprocessing. As the PILP solver does not
4717 * handle implicit equalities very well, we first make sure all
4718 * the equalities are explicitly available.
4720 * We also add context constraints to the basic map and remove
4721 * redundant constraints. This is only needed because of the
4722 * way we handle simple symmetries. In particular, we currently look
4723 * for symmetries on the constraints, before we set up the main tableau.
4724 * It is then no good to look for symmetries on possibly redundant constraints.
4726 struct isl_map *isl_tab_basic_map_partial_lexopt(
4727 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4728 struct isl_set **empty, int max)
4730 if (empty)
4731 *empty = NULL;
4732 if (!bmap || !dom)
4733 goto error;
4735 isl_assert(bmap->ctx,
4736 isl_basic_map_compatible_domain(bmap, dom), goto error);
4738 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
4739 return basic_map_partial_lexopt(bmap, dom, empty, max);
4741 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
4742 bmap = isl_basic_map_detect_equalities(bmap);
4743 bmap = isl_basic_map_remove_redundancies(bmap);
4745 return basic_map_partial_lexopt(bmap, dom, empty, max);
4746 error:
4747 isl_basic_set_free(dom);
4748 isl_basic_map_free(bmap);
4749 return NULL;
4752 struct isl_sol_for {
4753 struct isl_sol sol;
4754 int (*fn)(__isl_take isl_basic_set *dom,
4755 __isl_take isl_aff_list *list, void *user);
4756 void *user;
4759 static void sol_for_free(struct isl_sol_for *sol_for)
4761 if (!sol_for)
4762 return;
4763 if (sol_for->sol.context)
4764 sol_for->sol.context->op->free(sol_for->sol.context);
4765 free(sol_for);
4768 static void sol_for_free_wrap(struct isl_sol *sol)
4770 sol_for_free((struct isl_sol_for *)sol);
4773 /* Add the solution identified by the tableau and the context tableau.
4775 * See documentation of sol_add for more details.
4777 * Instead of constructing a basic map, this function calls a user
4778 * defined function with the current context as a basic set and
4779 * a list of affine expressions representing the relation between
4780 * the input and output. The space over which the affine expressions
4781 * are defined is the same as that of the domain. The number of
4782 * affine expressions in the list is equal to the number of output variables.
4784 static void sol_for_add(struct isl_sol_for *sol,
4785 struct isl_basic_set *dom, struct isl_mat *M)
4787 int i;
4788 isl_ctx *ctx;
4789 isl_local_space *ls;
4790 isl_aff *aff;
4791 isl_aff_list *list;
4793 if (sol->sol.error || !dom || !M)
4794 goto error;
4796 ctx = isl_basic_set_get_ctx(dom);
4797 ls = isl_basic_set_get_local_space(dom);
4798 list = isl_aff_list_alloc(ctx, M->n_row - 1);
4799 for (i = 1; i < M->n_row; ++i) {
4800 aff = isl_aff_alloc(isl_local_space_copy(ls));
4801 if (aff) {
4802 isl_int_set(aff->v->el[0], M->row[0][0]);
4803 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
4805 aff = isl_aff_normalize(aff);
4806 list = isl_aff_list_add(list, aff);
4808 isl_local_space_free(ls);
4810 dom = isl_basic_set_finalize(dom);
4812 if (sol->fn(isl_basic_set_copy(dom), list, sol->user) < 0)
4813 goto error;
4815 isl_basic_set_free(dom);
4816 isl_mat_free(M);
4817 return;
4818 error:
4819 isl_basic_set_free(dom);
4820 isl_mat_free(M);
4821 sol->sol.error = 1;
4824 static void sol_for_add_wrap(struct isl_sol *sol,
4825 struct isl_basic_set *dom, struct isl_mat *M)
4827 sol_for_add((struct isl_sol_for *)sol, dom, M);
4830 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
4831 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4832 void *user),
4833 void *user)
4835 struct isl_sol_for *sol_for = NULL;
4836 isl_space *dom_dim;
4837 struct isl_basic_set *dom = NULL;
4839 sol_for = isl_calloc_type(bmap->ctx, struct isl_sol_for);
4840 if (!sol_for)
4841 goto error;
4843 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
4844 dom = isl_basic_set_universe(dom_dim);
4846 sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
4847 sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
4848 sol_for->sol.dec_level.sol = &sol_for->sol;
4849 sol_for->fn = fn;
4850 sol_for->user = user;
4851 sol_for->sol.max = max;
4852 sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
4853 sol_for->sol.add = &sol_for_add_wrap;
4854 sol_for->sol.add_empty = NULL;
4855 sol_for->sol.free = &sol_for_free_wrap;
4857 sol_for->sol.context = isl_context_alloc(dom);
4858 if (!sol_for->sol.context)
4859 goto error;
4861 isl_basic_set_free(dom);
4862 return sol_for;
4863 error:
4864 isl_basic_set_free(dom);
4865 sol_for_free(sol_for);
4866 return NULL;
4869 static void sol_for_find_solutions(struct isl_sol_for *sol_for,
4870 struct isl_tab *tab)
4872 find_solutions_main(&sol_for->sol, tab);
4875 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
4876 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4877 void *user),
4878 void *user)
4880 struct isl_sol_for *sol_for = NULL;
4882 bmap = isl_basic_map_copy(bmap);
4883 bmap = isl_basic_map_detect_equalities(bmap);
4884 if (!bmap)
4885 return -1;
4887 sol_for = sol_for_init(bmap, max, fn, user);
4888 if (!sol_for)
4889 goto error;
4891 if (isl_basic_map_plain_is_empty(bmap))
4892 /* nothing */;
4893 else {
4894 struct isl_tab *tab;
4895 struct isl_context *context = sol_for->sol.context;
4896 tab = tab_for_lexmin(bmap,
4897 context->op->peek_basic_set(context), 1, max);
4898 tab = context->op->detect_nonnegative_parameters(context, tab);
4899 sol_for_find_solutions(sol_for, tab);
4900 if (sol_for->sol.error)
4901 goto error;
4904 sol_free(&sol_for->sol);
4905 isl_basic_map_free(bmap);
4906 return 0;
4907 error:
4908 sol_free(&sol_for->sol);
4909 isl_basic_map_free(bmap);
4910 return -1;
4913 int isl_basic_set_foreach_lexopt(__isl_keep isl_basic_set *bset, int max,
4914 int (*fn)(__isl_take isl_basic_set *dom, __isl_take isl_aff_list *list,
4915 void *user),
4916 void *user)
4918 return isl_basic_map_foreach_lexopt(bset, max, fn, user);
4921 /* Check if the given sequence of len variables starting at pos
4922 * represents a trivial (i.e., zero) solution.
4923 * The variables are assumed to be non-negative and to come in pairs,
4924 * with each pair representing a variable of unrestricted sign.
4925 * The solution is trivial if each such pair in the sequence consists
4926 * of two identical values, meaning that the variable being represented
4927 * has value zero.
4929 static int region_is_trivial(struct isl_tab *tab, int pos, int len)
4931 int i;
4933 if (len == 0)
4934 return 0;
4936 for (i = 0; i < len; i += 2) {
4937 int neg_row;
4938 int pos_row;
4940 neg_row = tab->var[pos + i].is_row ?
4941 tab->var[pos + i].index : -1;
4942 pos_row = tab->var[pos + i + 1].is_row ?
4943 tab->var[pos + i + 1].index : -1;
4945 if ((neg_row < 0 ||
4946 isl_int_is_zero(tab->mat->row[neg_row][1])) &&
4947 (pos_row < 0 ||
4948 isl_int_is_zero(tab->mat->row[pos_row][1])))
4949 continue;
4951 if (neg_row < 0 || pos_row < 0)
4952 return 0;
4953 if (isl_int_ne(tab->mat->row[neg_row][1],
4954 tab->mat->row[pos_row][1]))
4955 return 0;
4958 return 1;
4961 /* Return the index of the first trivial region or -1 if all regions
4962 * are non-trivial.
4964 static int first_trivial_region(struct isl_tab *tab,
4965 int n_region, struct isl_region *region)
4967 int i;
4969 for (i = 0; i < n_region; ++i) {
4970 if (region_is_trivial(tab, region[i].pos, region[i].len))
4971 return i;
4974 return -1;
4977 /* Check if the solution is optimal, i.e., whether the first
4978 * n_op entries are zero.
4980 static int is_optimal(__isl_keep isl_vec *sol, int n_op)
4982 int i;
4984 for (i = 0; i < n_op; ++i)
4985 if (!isl_int_is_zero(sol->el[1 + i]))
4986 return 0;
4987 return 1;
4990 /* Add constraints to "tab" that ensure that any solution is significantly
4991 * better that that represented by "sol". That is, find the first
4992 * relevant (within first n_op) non-zero coefficient and force it (along
4993 * with all previous coefficients) to be zero.
4994 * If the solution is already optimal (all relevant coefficients are zero),
4995 * then just mark the table as empty.
4997 static int force_better_solution(struct isl_tab *tab,
4998 __isl_keep isl_vec *sol, int n_op)
5000 int i;
5001 isl_ctx *ctx;
5002 isl_vec *v = NULL;
5004 if (!sol)
5005 return -1;
5007 for (i = 0; i < n_op; ++i)
5008 if (!isl_int_is_zero(sol->el[1 + i]))
5009 break;
5011 if (i == n_op) {
5012 if (isl_tab_mark_empty(tab) < 0)
5013 return -1;
5014 return 0;
5017 ctx = isl_vec_get_ctx(sol);
5018 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5019 if (!v)
5020 return -1;
5022 for (; i >= 0; --i) {
5023 v = isl_vec_clr(v);
5024 isl_int_set_si(v->el[1 + i], -1);
5025 if (add_lexmin_eq(tab, v->el) < 0)
5026 goto error;
5029 isl_vec_free(v);
5030 return 0;
5031 error:
5032 isl_vec_free(v);
5033 return -1;
5036 struct isl_trivial {
5037 int update;
5038 int region;
5039 int side;
5040 struct isl_tab_undo *snap;
5043 /* Return the lexicographically smallest non-trivial solution of the
5044 * given ILP problem.
5046 * All variables are assumed to be non-negative.
5048 * n_op is the number of initial coordinates to optimize.
5049 * That is, once a solution has been found, we will only continue looking
5050 * for solution that result in significantly better values for those
5051 * initial coordinates. That is, we only continue looking for solutions
5052 * that increase the number of initial zeros in this sequence.
5054 * A solution is non-trivial, if it is non-trivial on each of the
5055 * specified regions. Each region represents a sequence of pairs
5056 * of variables. A solution is non-trivial on such a region if
5057 * at least one of these pairs consists of different values, i.e.,
5058 * such that the non-negative variable represented by the pair is non-zero.
5060 * Whenever a conflict is encountered, all constraints involved are
5061 * reported to the caller through a call to "conflict".
5063 * We perform a simple branch-and-bound backtracking search.
5064 * Each level in the search represents initially trivial region that is forced
5065 * to be non-trivial.
5066 * At each level we consider n cases, where n is the length of the region.
5067 * In terms of the n/2 variables of unrestricted signs being encoded by
5068 * the region, we consider the cases
5069 * x_0 >= 1
5070 * x_0 <= -1
5071 * x_0 = 0 and x_1 >= 1
5072 * x_0 = 0 and x_1 <= -1
5073 * x_0 = 0 and x_1 = 0 and x_2 >= 1
5074 * x_0 = 0 and x_1 = 0 and x_2 <= -1
5075 * ...
5076 * The cases are considered in this order, assuming that each pair
5077 * x_i_a x_i_b represents the value x_i_b - x_i_a.
5078 * That is, x_0 >= 1 is enforced by adding the constraint
5079 * x_0_b - x_0_a >= 1
5081 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
5082 __isl_take isl_basic_set *bset, int n_op, int n_region,
5083 struct isl_region *region,
5084 int (*conflict)(int con, void *user), void *user)
5086 int i, j;
5087 int r;
5088 isl_ctx *ctx;
5089 isl_vec *v = NULL;
5090 isl_vec *sol = NULL;
5091 struct isl_tab *tab;
5092 struct isl_trivial *triv = NULL;
5093 int level, init;
5095 if (!bset)
5096 return NULL;
5098 ctx = isl_basic_set_get_ctx(bset);
5099 sol = isl_vec_alloc(ctx, 0);
5101 tab = tab_for_lexmin(bset, NULL, 0, 0);
5102 if (!tab)
5103 goto error;
5104 tab->conflict = conflict;
5105 tab->conflict_user = user;
5107 v = isl_vec_alloc(ctx, 1 + tab->n_var);
5108 triv = isl_calloc_array(ctx, struct isl_trivial, n_region);
5109 if (!v || (n_region && !triv))
5110 goto error;
5112 level = 0;
5113 init = 1;
5115 while (level >= 0) {
5116 int side, base;
5118 if (init) {
5119 tab = cut_to_integer_lexmin(tab, CUT_ONE);
5120 if (!tab)
5121 goto error;
5122 if (tab->empty)
5123 goto backtrack;
5124 r = first_trivial_region(tab, n_region, region);
5125 if (r < 0) {
5126 for (i = 0; i < level; ++i)
5127 triv[i].update = 1;
5128 isl_vec_free(sol);
5129 sol = isl_tab_get_sample_value(tab);
5130 if (!sol)
5131 goto error;
5132 if (is_optimal(sol, n_op))
5133 break;
5134 goto backtrack;
5136 if (level >= n_region)
5137 isl_die(ctx, isl_error_internal,
5138 "nesting level too deep", goto error);
5139 if (isl_tab_extend_cons(tab,
5140 2 * region[r].len + 2 * n_op) < 0)
5141 goto error;
5142 triv[level].region = r;
5143 triv[level].side = 0;
5146 r = triv[level].region;
5147 side = triv[level].side;
5148 base = 2 * (side/2);
5150 if (side >= region[r].len) {
5151 backtrack:
5152 level--;
5153 init = 0;
5154 if (level >= 0)
5155 if (isl_tab_rollback(tab, triv[level].snap) < 0)
5156 goto error;
5157 continue;
5160 if (triv[level].update) {
5161 if (force_better_solution(tab, sol, n_op) < 0)
5162 goto error;
5163 triv[level].update = 0;
5166 if (side == base && base >= 2) {
5167 for (j = base - 2; j < base; ++j) {
5168 v = isl_vec_clr(v);
5169 isl_int_set_si(v->el[1 + region[r].pos + j], 1);
5170 if (add_lexmin_eq(tab, v->el) < 0)
5171 goto error;
5175 triv[level].snap = isl_tab_snap(tab);
5176 if (isl_tab_push_basis(tab) < 0)
5177 goto error;
5179 v = isl_vec_clr(v);
5180 isl_int_set_si(v->el[0], -1);
5181 isl_int_set_si(v->el[1 + region[r].pos + side], -1);
5182 isl_int_set_si(v->el[1 + region[r].pos + (side ^ 1)], 1);
5183 tab = add_lexmin_ineq(tab, v->el);
5185 triv[level].side++;
5186 level++;
5187 init = 1;
5190 free(triv);
5191 isl_vec_free(v);
5192 isl_tab_free(tab);
5193 isl_basic_set_free(bset);
5195 return sol;
5196 error:
5197 free(triv);
5198 isl_vec_free(v);
5199 isl_tab_free(tab);
5200 isl_basic_set_free(bset);
5201 isl_vec_free(sol);
5202 return NULL;
5205 /* Return the lexicographically smallest rational point in "bset",
5206 * assuming that all variables are non-negative.
5207 * If "bset" is empty, then return a zero-length vector.
5209 __isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
5210 __isl_take isl_basic_set *bset)
5212 struct isl_tab *tab;
5213 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
5214 isl_vec *sol;
5216 if (!bset)
5217 return NULL;
5219 tab = tab_for_lexmin(bset, NULL, 0, 0);
5220 if (!tab)
5221 goto error;
5222 if (tab->empty)
5223 sol = isl_vec_alloc(ctx, 0);
5224 else
5225 sol = isl_tab_get_sample_value(tab);
5226 isl_tab_free(tab);
5227 isl_basic_set_free(bset);
5228 return sol;
5229 error:
5230 isl_tab_free(tab);
5231 isl_basic_set_free(bset);
5232 return NULL;
5235 struct isl_sol_pma {
5236 struct isl_sol sol;
5237 isl_pw_multi_aff *pma;
5238 isl_set *empty;
5241 static void sol_pma_free(struct isl_sol_pma *sol_pma)
5243 if (!sol_pma)
5244 return;
5245 if (sol_pma->sol.context)
5246 sol_pma->sol.context->op->free(sol_pma->sol.context);
5247 isl_pw_multi_aff_free(sol_pma->pma);
5248 isl_set_free(sol_pma->empty);
5249 free(sol_pma);
5252 /* This function is called for parts of the context where there is
5253 * no solution, with "bset" corresponding to the context tableau.
5254 * Simply add the basic set to the set "empty".
5256 static void sol_pma_add_empty(struct isl_sol_pma *sol,
5257 __isl_take isl_basic_set *bset)
5259 if (!bset || !sol->empty)
5260 goto error;
5262 sol->empty = isl_set_grow(sol->empty, 1);
5263 bset = isl_basic_set_simplify(bset);
5264 bset = isl_basic_set_finalize(bset);
5265 sol->empty = isl_set_add_basic_set(sol->empty, bset);
5266 if (!sol->empty)
5267 sol->sol.error = 1;
5268 return;
5269 error:
5270 isl_basic_set_free(bset);
5271 sol->sol.error = 1;
5274 /* Given a basic map "dom" that represents the context and an affine
5275 * matrix "M" that maps the dimensions of the context to the
5276 * output variables, construct an isl_pw_multi_aff with a single
5277 * cell corresponding to "dom" and affine expressions copied from "M".
5279 static void sol_pma_add(struct isl_sol_pma *sol,
5280 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5282 int i;
5283 isl_local_space *ls;
5284 isl_aff *aff;
5285 isl_multi_aff *maff;
5286 isl_pw_multi_aff *pma;
5288 maff = isl_multi_aff_alloc(isl_pw_multi_aff_get_space(sol->pma));
5289 ls = isl_basic_set_get_local_space(dom);
5290 for (i = 1; i < M->n_row; ++i) {
5291 aff = isl_aff_alloc(isl_local_space_copy(ls));
5292 if (aff) {
5293 isl_int_set(aff->v->el[0], M->row[0][0]);
5294 isl_seq_cpy(aff->v->el + 1, M->row[i], M->n_col);
5296 aff = isl_aff_normalize(aff);
5297 maff = isl_multi_aff_set_aff(maff, i - 1, aff);
5299 isl_local_space_free(ls);
5300 isl_mat_free(M);
5301 dom = isl_basic_set_simplify(dom);
5302 dom = isl_basic_set_finalize(dom);
5303 pma = isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom), maff);
5304 sol->pma = isl_pw_multi_aff_add_disjoint(sol->pma, pma);
5305 if (!sol->pma)
5306 sol->sol.error = 1;
5309 static void sol_pma_free_wrap(struct isl_sol *sol)
5311 sol_pma_free((struct isl_sol_pma *)sol);
5314 static void sol_pma_add_empty_wrap(struct isl_sol *sol,
5315 __isl_take isl_basic_set *bset)
5317 sol_pma_add_empty((struct isl_sol_pma *)sol, bset);
5320 static void sol_pma_add_wrap(struct isl_sol *sol,
5321 __isl_take isl_basic_set *dom, __isl_take isl_mat *M)
5323 sol_pma_add((struct isl_sol_pma *)sol, dom, M);
5326 /* Construct an isl_sol_pma structure for accumulating the solution.
5327 * If track_empty is set, then we also keep track of the parts
5328 * of the context where there is no solution.
5329 * If max is set, then we are solving a maximization, rather than
5330 * a minimization problem, which means that the variables in the
5331 * tableau have value "M - x" rather than "M + x".
5333 static struct isl_sol *sol_pma_init(__isl_keep isl_basic_map *bmap,
5334 __isl_take isl_basic_set *dom, int track_empty, int max)
5336 struct isl_sol_pma *sol_pma = NULL;
5338 if (!bmap)
5339 goto error;
5341 sol_pma = isl_calloc_type(bmap->ctx, struct isl_sol_pma);
5342 if (!sol_pma)
5343 goto error;
5345 sol_pma->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
5346 sol_pma->sol.dec_level.callback.run = &sol_dec_level_wrap;
5347 sol_pma->sol.dec_level.sol = &sol_pma->sol;
5348 sol_pma->sol.max = max;
5349 sol_pma->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
5350 sol_pma->sol.add = &sol_pma_add_wrap;
5351 sol_pma->sol.add_empty = track_empty ? &sol_pma_add_empty_wrap : NULL;
5352 sol_pma->sol.free = &sol_pma_free_wrap;
5353 sol_pma->pma = isl_pw_multi_aff_empty(isl_basic_map_get_space(bmap));
5354 if (!sol_pma->pma)
5355 goto error;
5357 sol_pma->sol.context = isl_context_alloc(dom);
5358 if (!sol_pma->sol.context)
5359 goto error;
5361 if (track_empty) {
5362 sol_pma->empty = isl_set_alloc_space(isl_basic_set_get_space(dom),
5363 1, ISL_SET_DISJOINT);
5364 if (!sol_pma->empty)
5365 goto error;
5368 isl_basic_set_free(dom);
5369 return &sol_pma->sol;
5370 error:
5371 isl_basic_set_free(dom);
5372 sol_pma_free(sol_pma);
5373 return NULL;
5376 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5377 * some obvious symmetries.
5379 * We call basic_map_partial_lexopt_base and extract the results.
5381 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_base_pma(
5382 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5383 __isl_give isl_set **empty, int max)
5385 isl_pw_multi_aff *result = NULL;
5386 struct isl_sol *sol;
5387 struct isl_sol_pma *sol_pma;
5389 sol = basic_map_partial_lexopt_base(bmap, dom, empty, max,
5390 &sol_pma_init);
5391 if (!sol)
5392 return NULL;
5393 sol_pma = (struct isl_sol_pma *) sol;
5395 result = isl_pw_multi_aff_copy(sol_pma->pma);
5396 if (empty)
5397 *empty = isl_set_copy(sol_pma->empty);
5398 sol_free(&sol_pma->sol);
5399 return result;
5402 /* Given that the last input variable of "maff" represents the minimum
5403 * of some bounds, check whether we need to plug in the expression
5404 * of the minimum.
5406 * In particular, check if the last input variable appears in any
5407 * of the expressions in "maff".
5409 static int need_substitution(__isl_keep isl_multi_aff *maff)
5411 int i;
5412 unsigned pos;
5414 pos = isl_multi_aff_dim(maff, isl_dim_in) - 1;
5416 for (i = 0; i < maff->n; ++i)
5417 if (isl_aff_involves_dims(maff->p[i], isl_dim_in, pos, 1))
5418 return 1;
5420 return 0;
5423 /* Given a set of upper bounds on the last "input" variable m,
5424 * construct a piecewise affine expression that selects
5425 * the minimal upper bound to m, i.e.,
5426 * divide the space into cells where one
5427 * of the upper bounds is smaller than all the others and select
5428 * this upper bound on that cell.
5430 * In particular, if there are n bounds b_i, then the result
5431 * consists of n cell, each one of the form
5433 * b_i <= b_j for j > i
5434 * b_i < b_j for j < i
5436 * The affine expression on this cell is
5438 * b_i
5440 static __isl_give isl_pw_aff *set_minimum_pa(__isl_take isl_space *space,
5441 __isl_take isl_mat *var)
5443 int i;
5444 isl_aff *aff = NULL;
5445 isl_basic_set *bset = NULL;
5446 isl_ctx *ctx;
5447 isl_pw_aff *paff = NULL;
5448 isl_space *pw_space;
5449 isl_local_space *ls = NULL;
5451 if (!space || !var)
5452 goto error;
5454 ctx = isl_space_get_ctx(space);
5455 ls = isl_local_space_from_space(isl_space_copy(space));
5456 pw_space = isl_space_copy(space);
5457 pw_space = isl_space_from_domain(pw_space);
5458 pw_space = isl_space_add_dims(pw_space, isl_dim_out, 1);
5459 paff = isl_pw_aff_alloc_size(pw_space, var->n_row);
5461 for (i = 0; i < var->n_row; ++i) {
5462 isl_pw_aff *paff_i;
5464 aff = isl_aff_alloc(isl_local_space_copy(ls));
5465 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0,
5466 0, var->n_row - 1);
5467 if (!aff || !bset)
5468 goto error;
5469 isl_int_set_si(aff->v->el[0], 1);
5470 isl_seq_cpy(aff->v->el + 1, var->row[i], var->n_col);
5471 isl_int_set_si(aff->v->el[1 + var->n_col], 0);
5472 bset = select_minimum(bset, var, i);
5473 paff_i = isl_pw_aff_alloc(isl_set_from_basic_set(bset), aff);
5474 paff = isl_pw_aff_add_disjoint(paff, paff_i);
5477 isl_local_space_free(ls);
5478 isl_space_free(space);
5479 isl_mat_free(var);
5480 return paff;
5481 error:
5482 isl_aff_free(aff);
5483 isl_basic_set_free(bset);
5484 isl_pw_aff_free(paff);
5485 isl_local_space_free(ls);
5486 isl_space_free(space);
5487 isl_mat_free(var);
5488 return NULL;
5491 /* Given a piecewise multi-affine expression of which the last input variable
5492 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5493 * This minimum expression is given in "min_expr_pa".
5494 * The set "min_expr" contains the same information, but in the form of a set.
5495 * The variable is subsequently projected out.
5497 * The implementation is similar to those of "split" and "split_domain".
5498 * If the variable appears in a given expression, then minimum expression
5499 * is plugged in. Otherwise, if the variable appears in the constraints
5500 * and a split is required, then the domain is split. Otherwise, no split
5501 * is performed.
5503 static __isl_give isl_pw_multi_aff *split_domain_pma(
5504 __isl_take isl_pw_multi_aff *opt, __isl_take isl_pw_aff *min_expr_pa,
5505 __isl_take isl_set *min_expr, __isl_take isl_mat *cst)
5507 int n_in;
5508 int i;
5509 isl_space *space;
5510 isl_pw_multi_aff *res;
5512 if (!opt || !min_expr || !cst)
5513 goto error;
5515 n_in = isl_pw_multi_aff_dim(opt, isl_dim_in);
5516 space = isl_pw_multi_aff_get_space(opt);
5517 space = isl_space_drop_dims(space, isl_dim_in, n_in - 1, 1);
5518 res = isl_pw_multi_aff_empty(space);
5520 for (i = 0; i < opt->n; ++i) {
5521 isl_pw_multi_aff *pma;
5523 pma = isl_pw_multi_aff_alloc(isl_set_copy(opt->p[i].set),
5524 isl_multi_aff_copy(opt->p[i].maff));
5525 if (need_substitution(opt->p[i].maff))
5526 pma = isl_pw_multi_aff_substitute(pma,
5527 isl_dim_in, n_in - 1, min_expr_pa);
5528 else if (need_split_set(opt->p[i].set, cst))
5529 pma = isl_pw_multi_aff_intersect_domain(pma,
5530 isl_set_copy(min_expr));
5531 pma = isl_pw_multi_aff_project_out(pma,
5532 isl_dim_in, n_in - 1, 1);
5534 res = isl_pw_multi_aff_add_disjoint(res, pma);
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 res;
5542 error:
5543 isl_pw_multi_aff_free(opt);
5544 isl_pw_aff_free(min_expr_pa);
5545 isl_set_free(min_expr);
5546 isl_mat_free(cst);
5547 return NULL;
5550 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5551 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5552 __isl_give isl_set **empty, int max);
5554 /* This function is called from basic_map_partial_lexopt_symm.
5555 * The last variable of "bmap" and "dom" corresponds to the minimum
5556 * of the bounds in "cst". "map_space" is the space of the original
5557 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5558 * is the space of the original domain.
5560 * We recursively call basic_map_partial_lexopt and then plug in
5561 * the definition of the minimum in the result.
5563 static __isl_give union isl_lex_res basic_map_partial_lexopt_symm_pma_core(
5564 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5565 __isl_give isl_set **empty, int max, __isl_take isl_mat *cst,
5566 __isl_take isl_space *map_space, __isl_take isl_space *set_space)
5568 isl_pw_multi_aff *opt;
5569 isl_pw_aff *min_expr_pa;
5570 isl_set *min_expr;
5571 union isl_lex_res res;
5573 min_expr = set_minimum(isl_basic_set_get_space(dom), isl_mat_copy(cst));
5574 min_expr_pa = set_minimum_pa(isl_basic_set_get_space(dom),
5575 isl_mat_copy(cst));
5577 opt = basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5579 if (empty) {
5580 *empty = split(*empty,
5581 isl_set_copy(min_expr), isl_mat_copy(cst));
5582 *empty = isl_set_reset_space(*empty, set_space);
5585 opt = split_domain_pma(opt, min_expr_pa, min_expr, cst);
5586 opt = isl_pw_multi_aff_reset_space(opt, map_space);
5588 res.pma = opt;
5589 return res;
5592 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_symm_pma(
5593 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5594 __isl_give isl_set **empty, int max, int first, int second)
5596 return basic_map_partial_lexopt_symm(bmap, dom, empty, max,
5597 first, second, &basic_map_partial_lexopt_symm_pma_core).pma;
5600 /* Recursive part of isl_basic_map_partial_lexopt_pw_multi_aff, after detecting
5601 * equalities and removing redundant constraints.
5603 * We first check if there are any parallel constraints (left).
5604 * If not, we are in the base case.
5605 * If there are parallel constraints, we replace them by a single
5606 * constraint in basic_map_partial_lexopt_symm_pma and then call
5607 * this function recursively to look for more parallel constraints.
5609 static __isl_give isl_pw_multi_aff *basic_map_partial_lexopt_pma(
5610 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5611 __isl_give isl_set **empty, int max)
5613 int par = 0;
5614 int first, second;
5616 if (!bmap)
5617 goto error;
5619 if (bmap->ctx->opt->pip_symmetry)
5620 par = parallel_constraints(bmap, &first, &second);
5621 if (par < 0)
5622 goto error;
5623 if (!par)
5624 return basic_map_partial_lexopt_base_pma(bmap, dom, empty, max);
5626 return basic_map_partial_lexopt_symm_pma(bmap, dom, empty, max,
5627 first, second);
5628 error:
5629 isl_basic_set_free(dom);
5630 isl_basic_map_free(bmap);
5631 return NULL;
5634 /* Compute the lexicographic minimum (or maximum if "max" is set)
5635 * of "bmap" over the domain "dom" and return the result as a piecewise
5636 * multi-affine expression.
5637 * If "empty" is not NULL, then *empty is assigned a set that
5638 * contains those parts of the domain where there is no solution.
5639 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
5640 * then we compute the rational optimum. Otherwise, we compute
5641 * the integral optimum.
5643 * We perform some preprocessing. As the PILP solver does not
5644 * handle implicit equalities very well, we first make sure all
5645 * the equalities are explicitly available.
5647 * We also add context constraints to the basic map and remove
5648 * redundant constraints. This is only needed because of the
5649 * way we handle simple symmetries. In particular, we currently look
5650 * for symmetries on the constraints, before we set up the main tableau.
5651 * It is then no good to look for symmetries on possibly redundant constraints.
5653 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
5654 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5655 __isl_give isl_set **empty, int max)
5657 if (empty)
5658 *empty = NULL;
5659 if (!bmap || !dom)
5660 goto error;
5662 isl_assert(bmap->ctx,
5663 isl_basic_map_compatible_domain(bmap, dom), goto error);
5665 if (isl_basic_set_dim(dom, isl_dim_all) == 0)
5666 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5668 bmap = isl_basic_map_intersect_domain(bmap, isl_basic_set_copy(dom));
5669 bmap = isl_basic_map_detect_equalities(bmap);
5670 bmap = isl_basic_map_remove_redundancies(bmap);
5672 return basic_map_partial_lexopt_pma(bmap, dom, empty, max);
5673 error:
5674 isl_basic_set_free(dom);
5675 isl_basic_map_free(bmap);
5676 return NULL;